Skip to content

Commit

Permalink
area_to_pandas: Correctly calculate density and average speed
Browse files Browse the repository at this point in the history
It's now weighted with the link length.
  • Loading branch information
EwoutH authored Sep 2, 2024
1 parent cbd96b5 commit f4fcde2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions uxsim/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,8 +1491,14 @@ def area_to_pandas(s, areas, area_names=None, border_include=True):
total_travel_time = np.sum(df_links.loc[rows, "average_travel_time"].values * traffic_volume_rows)
total_free_time = np.sum(df_links.loc[rows, "free_travel_time"].values * traffic_volume_rows)
average_delay = max(total_travel_time / total_free_time - 1, 0)
average_speed = df_links.loc[rows, "average_travel_time"].mean()
vehicle_density = traffic_volume / df_links.loc[rows, "link"].nunique()

# Average speed calculation: total distance / total time
total_distance = np.sum(df_links.loc[rows, "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, "length"].sum()
vehicle_density = traffic_volume / total_link_length if total_link_length > 0 else np.nan
else:
total_travel_time = 0
total_free_time = 0
Expand Down Expand Up @@ -1651,9 +1657,9 @@ def link_to_pandas(s):
"""
s.link_analysis_coarse()

out = [["link", "start_node", "end_node", "traffic_volume", "vehicles_remain", "free_travel_time", "average_travel_time", "stddiv_travel_time"]]
out = [["link", "start_node", "end_node", "traffic_volume", "vehicles_remain", "free_travel_time", "average_travel_time", "stddiv_travel_time", "length"]]
for l in s.W.LINKS:
out.append([l.name, l.start_node.name, l.end_node.name, s.linkc_volume[l], s.linkc_remain[l], s.linkc_tt_free[l], s.linkc_tt_ave[l], s.linkc_tt_std[l]])
out.append([l.name, l.start_node.name, l.end_node.name, s.linkc_volume[l], s.linkc_remain[l], s.linkc_tt_free[l], s.linkc_tt_ave[l], s.linkc_tt_std[l], l.length])
s.df_linkc = pd.DataFrame(out[1:], columns=out[0])
return s.df_linkc

Expand Down

0 comments on commit f4fcde2

Please sign in to comment.