Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer: Return average_speed and vehicle_density with area_to_pandas #121

Merged
merged 2 commits into from
Sep 11, 2024

Conversation

EwoutH
Copy link
Contributor

@EwoutH EwoutH commented Sep 2, 2024

This PR adds two new columns to the Analyzer.area_to_pandas method: average speed and vehicle density. They are calculated as:

# Average speed calculation: total distance / total time
total_distance = np.sum(df_links.loc[rows, "link_length"].values * traffic_volume_rows)
average_speed = total_distance / total_travel_time if total_travel_time > 0 else np.nan

# Vehicle density calculation: total number of vehicles / total link length
total_link_length = df_links.loc[rows, "link_length"].sum()
vehicle_density = traffic_volume / total_link_length if total_link_length > 0 else np.nan

This PR builds on #119. Please review/merge that PR first. Only the second commit (f0bcb1a) is new in this PR.

The `area_to_pandas` function has been optimized to significantly improve its execution speed.

- Set lookups: Areas are converted to sets for O(1) membership checks, making the `isin` operations more efficient.
- Vectorization: Replaced loop-based calculations with vectorized operations for `total_travel_time` and `total_free_time`, leveraging the power of `numpy` and `pandas` to perform these operations in bulk.
- Single DataFrame access: Minimized repeated access to DataFrame columns, reducing the overhead of these operations.
- Efficient grouping: Used `unique()` and `nunique()` for faster unique element counting.
This commit adds two new columns to the Analyzer.area_to_pandas method: average_speed and vehicle_density.
@EwoutH EwoutH force-pushed the area2pandas_speed_density branch from f4fcde2 to f0bcb1a Compare September 2, 2024 15:36
@toruseo toruseo merged commit 62b59ac into toruseo:main Sep 11, 2024
6 checks passed
@EwoutH
Copy link
Contributor Author

EwoutH commented Sep 11, 2024

Thanks for merging! I didn't add test for this PR, should I do that somewhere?

@toruseo
Copy link
Owner

toruseo commented Sep 11, 2024

yes, that will be fantastic

plz add new test(s) to https://github.com/toruseo/UXsim/blob/main/tests/test_other_functions.py like test_area_stats()

@EwoutH
Copy link
Contributor Author

EwoutH commented Sep 11, 2024

Thanks, I will add something like:

    assert equal_tolerance(df["average_speed"][df["area"] == "areaN"].values[0], 10.0, abs_tol=1.0)
    assert equal_tolerance(df["vehicle_density"][df["area"] == "areaN"].values[0], 0.007, abs_tol=0.001)

Just have to get in a meeting, will add and calibrate the values afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants