Skip to content

Commit

Permalink
reducing memory needs in parking_location_choice
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensle committed May 1, 2024
1 parent 98713f3 commit e4dc5ac
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
22 changes: 16 additions & 6 deletions activitysim/abm/models/parking_location_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from activitysim.core.interaction_sample_simulate import interaction_sample_simulate
from activitysim.core.tracing import print_elapsed_time
from activitysim.core.util import assign_in_place
from activitysim.core.util import assign_in_place, drop_unused_chooser_columns

from .util import estimation

Expand Down Expand Up @@ -99,6 +99,7 @@ def parking_destination_simulate(
destination_sample,
model_settings,
skims,
locals_dict,
chunk_size,
trace_hh_id,
trace_label,
Expand All @@ -123,11 +124,6 @@ def parking_destination_simulate(

logger.info("Running trip_destination_simulate with %d trips", len(trips))

locals_dict = config.get_model_constants(model_settings).copy()
locals_dict.update(skims)
locals_dict["timeframe"] = "trip"
locals_dict["PARKING"] = skims["op_skims"].dest_key

parking_locations = interaction_sample_simulate(
choosers=trips,
alternatives=destination_sample,
Expand Down Expand Up @@ -171,6 +167,19 @@ def choose_parking_location(
t0 = print_elapsed_time()

alt_dest_col_name = model_settings["ALT_DEST_COL_NAME"]

# remove trips and alts columns that are not used in spec
locals_dict = config.get_model_constants(model_settings).copy()
locals_dict.update(skims)
locals_dict["timeframe"] = "trip"
locals_dict["PARKING"] = skims["op_skims"].dest_key

spec = get_spec_for_segment(model_settings, "SPECIFICATION", segment_name)
trips = drop_unused_chooser_columns(trips, spec, locals_dict, custom_chooser=None)
alternatives = drop_unused_chooser_columns(
alternatives, spec, locals_dict, custom_chooser=None
)

destination_sample = logit.interaction_dataset(
trips, alternatives, alt_index_id=alt_dest_col_name
)
Expand All @@ -184,6 +193,7 @@ def choose_parking_location(
destination_sample=destination_sample,
model_settings=model_settings,
skims=skims,
locals_dict=locals_dict,
chunk_size=chunk_size,
trace_hh_id=trace_hh_id,
trace_label=trace_label,
Expand Down
75 changes: 75 additions & 0 deletions activitysim/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,78 @@ def nearest_node_index(node, nodes):
deltas = nodes - node
dist_2 = np.einsum("ij,ij->i", deltas, deltas)
return np.argmin(dist_2)


def drop_unused_chooser_columns(
choosers, spec, locals_d, custom_chooser, sharrow_enabled=False
):
"""
Drop unused columns from the chooser table, based on the spec and custom_chooser function.
"""
# keep only variables needed for spec
import re

# define a regular expression to find variables in spec
pattern = r"[a-zA-Z_][a-zA-Z0-9_]*"

unique_variables_in_spec = set(
spec.reset_index()["Expression"].apply(lambda x: re.findall(pattern, x)).sum()
)

if locals_d:
unique_variables_in_spec.add(locals_d.get("orig_col_name", None))
unique_variables_in_spec.add(locals_d.get("dest_col_name", None))
if locals_d.get("timeframe") == "trip":
orig_col_name = locals_d.get("ORIGIN", None)
dest_col_name = locals_d.get("DESTINATION", None)
stop_col_name = None
parking_col_name = locals_d.get("PARKING", None)
primary_origin_col_name = None
if orig_col_name is None and "od_skims" in locals_d:
orig_col_name = locals_d["od_skims"].orig_key
if dest_col_name is None and "od_skims" in locals_d:
dest_col_name = locals_d["od_skims"].dest_key
if stop_col_name is None and "dp_skims" in locals_d:
stop_col_name = locals_d["dp_skims"].dest_key
if primary_origin_col_name is None and "dnt_skims" in locals_d:
primary_origin_col_name = locals_d["dnt_skims"].dest_key
unique_variables_in_spec.add(orig_col_name)
unique_variables_in_spec.add(dest_col_name)
unique_variables_in_spec.add(parking_col_name)
unique_variables_in_spec.add(primary_origin_col_name)
unique_variables_in_spec.add(stop_col_name)
unique_variables_in_spec.add("trip_period")
# when using trip_scheduling_choice for trup scheduling
unique_variables_in_spec.add("last_outbound_stop")
unique_variables_in_spec.add("last_inbound_stop")

# when sharrow mode, need to keep the following columns in the choosers table
if sharrow_enabled:
unique_variables_in_spec.add("out_period")
unique_variables_in_spec.add("in_period")
unique_variables_in_spec.add("purpose_index_num")

if custom_chooser:
import inspect

custom_chooser_lines = inspect.getsource(custom_chooser)
unique_variables_in_spec.update(re.findall(pattern, custom_chooser_lines))

logger.info("Dropping unused variables in chooser table")

logger.info(
"before dropping, the choosers table has {} columns: {}".format(
len(choosers.columns), choosers.columns
)
)

# keep only variables needed for spec
choosers = choosers[[c for c in choosers.columns if c in unique_variables_in_spec]]

logger.info(
"after dropping, the choosers table has {} columns: {}".format(
len(choosers.columns), choosers.columns
)
)

return choosers

0 comments on commit e4dc5ac

Please sign in to comment.