From 38c541dbeeb81f7276af51b513e2051327f68944 Mon Sep 17 00:00:00 2001 From: Jeff Newman Date: Wed, 1 May 2024 20:13:27 -0500 Subject: [PATCH 1/2] add ALTS_ATTRIBUTES_SAMPLING --- activitysim/abm/models/trip_destination.py | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/activitysim/abm/models/trip_destination.py b/activitysim/abm/models/trip_destination.py index 9789f805e..5f76db772 100644 --- a/activitysim/abm/models/trip_destination.py +++ b/activitysim/abm/models/trip_destination.py @@ -64,6 +64,16 @@ class TripDestinationSettings(LocationComponentSettings, extra="forbid"): fail_some_trips_for_testing: bool = False """This setting is used by testing code to force failed trip_destination.""" + ALTS_ATTRIBUTES_SAMPLING: list[str] = [] + """List of attributes to include in the alternatives table when sampling. + + These columns should exist in the land_use (for regular sampling) or land_use_taz + (for presampling) table. They will be added to the alternatives table, and available + for use in expressions in the model sampling spec. + + .. versionadded:: 1.3 + """ + @root_validator(pre=True) def deprecated_destination_prefix(cls, values): replacements = { @@ -195,6 +205,7 @@ def _destination_sample( "size_terms_array": size_term_matrix.df.to_numpy(), "timeframe": "trip", "land_use": state.get_dataframe("land_use"), + "land_use_taz": state.get_dataframe("land_use_taz"), } ) locals_dict.update(skims) @@ -242,6 +253,17 @@ def destination_sample( skims = skim_hotel.sample_skims(presample=False) alt_dest_col_name = model_settings.ALT_DEST_COL_NAME + alternative_attributes = model_settings.ALTS_ATTRIBUTES_SAMPLING + if alternative_attributes: + land_use = state.get_dataframe("land_use") + alternatives = pd.merge( + alternatives, + land_use[alternative_attributes], + left_index=True, + right_index=True, + how="left", + ) + choices = _destination_sample( state, primary_purpose, @@ -561,6 +583,17 @@ def destination_presample( network_los.map_maz_to_taz(alternatives.index) ).sum() + alternative_attributes = model_settings.ALTS_ATTRIBUTES_SAMPLING + if alternative_attributes: + land_use_taz = state.get_dataframe("land_use_taz") + alternatives = pd.merge( + alternatives, + land_use_taz[alternative_attributes], + left_index=True, + right_index=True, + how="left", + ) + # # i did this but after changing alt_dest_col_name to 'trip_dest' it # # shouldn't be needed anymore # alternatives.index.name = ALT_DEST_TAZ From 08e50ff20b20a984356df9d6d4ab2c1f66c86699 Mon Sep 17 00:00:00 2001 From: Jeff Newman Date: Wed, 1 May 2024 20:21:54 -0500 Subject: [PATCH 2/2] no land_use_taz in locals --- activitysim/abm/models/trip_destination.py | 1 - 1 file changed, 1 deletion(-) diff --git a/activitysim/abm/models/trip_destination.py b/activitysim/abm/models/trip_destination.py index 5f76db772..a641f8233 100644 --- a/activitysim/abm/models/trip_destination.py +++ b/activitysim/abm/models/trip_destination.py @@ -205,7 +205,6 @@ def _destination_sample( "size_terms_array": size_term_matrix.df.to_numpy(), "timeframe": "trip", "land_use": state.get_dataframe("land_use"), - "land_use_taz": state.get_dataframe("land_use_taz"), } ) locals_dict.update(skims)