Skip to content

Commit

Permalink
Merge pull request #68 from Urban-Analytics-Technology-Platform/fix_w…
Browse files Browse the repository at this point in the history
…orkzone_assignment

Fix workzone assignment
  • Loading branch information
Hussein-Mahfouz authored Nov 19, 2024
2 parents 84b0dc0 + 8155c1d commit aa9fdb9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 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
2 changes: 1 addition & 1 deletion src/acbm/assigning/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def plot_workzone_assignment_heatmap(
)

ax = sns.heatmap(
heatmap_data, cmap="RdBu", ax=axes[i], cbar=i == len(categories) - 1
heatmap_data, cmap="viridis", ax=axes[i], cbar=i == len(categories) - 1
)
axes[i].set_title(f"Demand Difference: % of {category} Total")
axes[i].set_xlabel("Home Zone")
Expand Down
11 changes: 4 additions & 7 deletions src/acbm/assigning/select_zone_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,16 +488,13 @@ def select_work_zone_optimization(
for person_id, origins in self.activities_to_assign.items()
if (person_id, from_zone, to_zone) in assignment_vars
)
# Calculate the assigned percentage based on the total flows to each destination zone
if to_zone in self.total_flows:
total_people = self.total_flows[to_zone]
# Calculate the assigned percentage based on the total flows from each origin zone
if from_zone in self.total_flows:
total_people = self.total_flows[from_zone]
assigned_percentage = assigned_flow / total_people
# If the origin zone is not in the total flows, set the assigned percentage to 0
else:
assigned_percentage = 0
logger.warning(
f"Warning: Destination {to_zone} not found in total_flows."
)
logger.warning(f"Warning: Origin {from_zone} not found in total_flows.")

if use_percentages:
# to satisfy both constraints, abs_dev will be a positive number (it is the larger of the two)
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 @@ -286,6 +286,7 @@ def _get_activities_per_zone_df(activities_per_zone: dict) -> pd.DataFrame:

def zones_to_time_matrix(
zones: gpd.GeoDataFrame,
time_units: str,
id_col: Optional[str] = None,
) -> pd.DataFrame:
"""
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.
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 aa9fdb9

Please sign in to comment.