Skip to content

Commit

Permalink
fix zones_to_time_matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hussein-Mahfouz committed Nov 15, 2024
1 parent 37787ad commit 596d2f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion scripts/3.1_assign_primary_feasible_zones.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def main(config_file):
# If travel_times is not true or loading failed, create a new travel time matrix
logger.info("No travel time matrix found. Creating a new travel time matrix.")
# Create a new travel time matrix based on distances between zones
travel_times = zones_to_time_matrix(zones=boundaries, id_col=config.zone_id)
travel_times = zones_to_time_matrix(
zones=boundaries, id_col=config.zone_id, time_units="m"
)
logger.info("Travel time estimates created")
# save travel_times as parquet

Expand Down
4 changes: 3 additions & 1 deletion src/acbm/assigning/feasible_zones_primary.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def get_possible_zones(

if travel_times is None:
logger.info("Travel time matrix not provided: Creating travel times estimates")
travel_times = zones_to_time_matrix(zones=boundaries, id_col=zone_id)
travel_times = zones_to_time_matrix(
zones=boundaries, id_col=zone_id, time_units="m"
)

list_of_modes = activity_chains["mode"].unique()
print(f"Unique modes found in the dataset are: {list_of_modes}")
Expand Down
8 changes: 7 additions & 1 deletion src/acbm/assigning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def _get_activities_per_zone_df(activities_per_zone: dict) -> pd.DataFrame:
def zones_to_time_matrix(
zones: gpd.GeoDataFrame,
id_col: Optional[str] = None,
time_units: str = "s",
) -> pd.DataFrame:
"""
Calculates the distance matrix between the centroids of the given zones and returns it as a DataFrame. The matrix also adds
Expand All @@ -302,7 +303,8 @@ def zones_to_time_matrix(
A GeoDataFrame containing the zones.
id_col: str, optional
The name of the column in the zones GeoDataFrame to use as the ID. If None, the index values are used. Default is None.
time_units: str, optional
The units to use for the travel time. Options are 's' for seconds and 'm' for minutes. Default is 's'.
Returns
-------
pd.DataFrame
Expand Down Expand Up @@ -347,6 +349,10 @@ def zones_to_time_matrix(
mode_data["time"] = mode_data["distance"] / speed
long_format_data.append(mode_data)

# Convert time to the desired units
if time_units == "m":
mode_data["time"] = mode_data["time"] / 60 # Convert seconds to minutes

# Concatenate the list into a single DataFrame
return pd.concat(long_format_data, ignore_index=True)

Expand Down

0 comments on commit 596d2f8

Please sign in to comment.