From 6438f593802db999a70d21e842f90486ffd0e6c5 Mon Sep 17 00:00:00 2001 From: David Hensle Date: Thu, 2 May 2024 14:48:36 -0700 Subject: [PATCH 1/5] trip_destination alts preprocessor --- activitysim/abm/models/trip_destination.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/activitysim/abm/models/trip_destination.py b/activitysim/abm/models/trip_destination.py index 9789f805e..6b6b11e85 100644 --- a/activitysim/abm/models/trip_destination.py +++ b/activitysim/abm/models/trip_destination.py @@ -60,6 +60,7 @@ class TripDestinationSettings(LocationComponentSettings, extra="forbid"): PRIMARY_DEST: str = "tour_leg_dest" # must be created in preprocessor REDUNDANT_TOURS_MERGED_CHOOSER_COLUMNS: list[str] | None = None preprocessor: PreprocessorSettings | None = None + alt_preprocessor: PreprocessorSettings | None = None CLEANUP: bool fail_some_trips_for_testing: bool = False """This setting is used by testing code to force failed trip_destination.""" @@ -1245,6 +1246,7 @@ def run_trip_destination( state.filesystem, model_settings_file_name ) preprocessor_settings = model_settings.preprocessor + alt_preprocessor_settings = model_settings.alt_preprocessor logsum_settings = state.filesystem.read_model_settings( model_settings.LOGSUM_SETTINGS ) @@ -1369,6 +1371,15 @@ def run_trip_destination( trace_label=nth_trace_label, ) + if alt_preprocessor_settings: + expressions.assign_columns( + state, + df=alternatives, + model_settings=alt_preprocessor_settings, + locals_dict=locals_dict, + trace_label=tracing.extend_trace_label(nth_trace_label, "alts"), + ) + if isinstance( nth_trips["trip_period"].dtype, pd.api.types.CategoricalDtype ): From 33097eb767229d44dc283473e736d5f4ece7c421 Mon Sep 17 00:00:00 2001 From: David Hensle Date: Sat, 4 May 2024 13:59:37 -0700 Subject: [PATCH 2/5] non_hh_veh cat, drop unused cols for alts --- activitysim/abm/models/trip_destination.py | 10 ++++------ activitysim/abm/models/vehicle_allocation.py | 4 ++++ activitysim/core/interaction_sample.py | 9 +++++++++ activitysim/core/interaction_sample_simulate.py | 9 +++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/activitysim/abm/models/trip_destination.py b/activitysim/abm/models/trip_destination.py index 6b6b11e85..8a3320a20 100644 --- a/activitysim/abm/models/trip_destination.py +++ b/activitysim/abm/models/trip_destination.py @@ -60,7 +60,7 @@ class TripDestinationSettings(LocationComponentSettings, extra="forbid"): PRIMARY_DEST: str = "tour_leg_dest" # must be created in preprocessor REDUNDANT_TOURS_MERGED_CHOOSER_COLUMNS: list[str] | None = None preprocessor: PreprocessorSettings | None = None - alt_preprocessor: PreprocessorSettings | None = None + alts_preprocessor: PreprocessorSettings | None = None CLEANUP: bool fail_some_trips_for_testing: bool = False """This setting is used by testing code to force failed trip_destination.""" @@ -1246,7 +1246,7 @@ def run_trip_destination( state.filesystem, model_settings_file_name ) preprocessor_settings = model_settings.preprocessor - alt_preprocessor_settings = model_settings.alt_preprocessor + alts_preprocessor_settings = model_settings.alts_preprocessor logsum_settings = state.filesystem.read_model_settings( model_settings.LOGSUM_SETTINGS ) @@ -1336,8 +1336,6 @@ def run_trip_destination( # returns a series of size_terms for each chooser's dest_zone_id and purpose with chooser index size_term_matrix = DataFrameMatrix(alternatives) - # don't need size terms in alternatives, just zone_id index - alternatives = alternatives.drop(alternatives.columns, axis=1) alternatives.index.name = model_settings.ALT_DEST_COL_NAME sample_list = [] @@ -1371,11 +1369,11 @@ def run_trip_destination( trace_label=nth_trace_label, ) - if alt_preprocessor_settings: + if alts_preprocessor_settings: expressions.assign_columns( state, df=alternatives, - model_settings=alt_preprocessor_settings, + model_settings=alts_preprocessor_settings, locals_dict=locals_dict, trace_label=tracing.extend_trace_label(nth_trace_label, "alts"), ) diff --git a/activitysim/abm/models/vehicle_allocation.py b/activitysim/abm/models/vehicle_allocation.py index 9dcaf8c71..9061e99fb 100644 --- a/activitysim/abm/models/vehicle_allocation.py +++ b/activitysim/abm/models/vehicle_allocation.py @@ -259,6 +259,10 @@ def vehicle_allocation( choices.loc[choices["alt_choice"] == alt, "choice"] = choosers.loc[ choices["alt_choice"] == alt, alt ] + # add non-household vehicle option to categories + if alts_from_spec[-1] not in choices["choice"].cat.categories: + choices["choice"] = choices["choice"].cat.add_categories(alts_from_spec[-1]) + # set choice for non-household vehicle option choices.loc[ choices["alt_choice"] == alts_from_spec[-1], "choice" ] = alts_from_spec[-1] diff --git a/activitysim/core/interaction_sample.py b/activitysim/core/interaction_sample.py index 5a05b7e8b..b87068150 100644 --- a/activitysim/core/interaction_sample.py +++ b/activitysim/core/interaction_sample.py @@ -259,6 +259,15 @@ def _interaction_sample( sharrow_enabled=sharrow_enabled, ) + alternatives = util.drop_unused_columns( + alternatives, + spec, + locals_d, + custom_chooser=None, + sharrow_enabled=sharrow_enabled, + additional_columns=['tdd'] + ) + if sharrow_enabled: ( interaction_utilities, diff --git a/activitysim/core/interaction_sample_simulate.py b/activitysim/core/interaction_sample_simulate.py index 3d729ad49..3621701f8 100644 --- a/activitysim/core/interaction_sample_simulate.py +++ b/activitysim/core/interaction_sample_simulate.py @@ -160,6 +160,15 @@ def _interaction_sample_simulate( sharrow_enabled=sharrow_enabled, ) + alternatives = util.drop_unused_columns( + alternatives, + spec, + locals_d, + custom_chooser=None, + sharrow_enabled=sharrow_enabled, + additional_columns=['tdd'] + ) + interaction_df = alternatives.join(choosers, how="left", rsuffix="_chooser") logger.info( f"{trace_label} end merging choosers and alternatives to create interaction_df" From a61b2d59c8f29b86937a6e911848d5e7b726c587 Mon Sep 17 00:00:00 2001 From: David Hensle Date: Sat, 4 May 2024 14:02:31 -0700 Subject: [PATCH 3/5] blacken --- activitysim/core/interaction_sample.py | 2 +- activitysim/core/interaction_sample_simulate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activitysim/core/interaction_sample.py b/activitysim/core/interaction_sample.py index b87068150..6d4b929c5 100644 --- a/activitysim/core/interaction_sample.py +++ b/activitysim/core/interaction_sample.py @@ -265,7 +265,7 @@ def _interaction_sample( locals_d, custom_chooser=None, sharrow_enabled=sharrow_enabled, - additional_columns=['tdd'] + additional_columns=["tdd"], ) if sharrow_enabled: diff --git a/activitysim/core/interaction_sample_simulate.py b/activitysim/core/interaction_sample_simulate.py index 3621701f8..8951a3d07 100644 --- a/activitysim/core/interaction_sample_simulate.py +++ b/activitysim/core/interaction_sample_simulate.py @@ -166,7 +166,7 @@ def _interaction_sample_simulate( locals_d, custom_chooser=None, sharrow_enabled=sharrow_enabled, - additional_columns=['tdd'] + additional_columns=["tdd"], ) interaction_df = alternatives.join(choosers, how="left", rsuffix="_chooser") From 4c4a9d98ef5463e0da8af433baf29db92a62f62f Mon Sep 17 00:00:00 2001 From: David Hensle Date: Sat, 4 May 2024 14:41:13 -0700 Subject: [PATCH 4/5] adding missed alts columns used in xborder model --- activitysim/core/interaction_sample.py | 2 +- activitysim/core/interaction_sample_simulate.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activitysim/core/interaction_sample.py b/activitysim/core/interaction_sample.py index 6d4b929c5..6f4c9a6ea 100644 --- a/activitysim/core/interaction_sample.py +++ b/activitysim/core/interaction_sample.py @@ -265,7 +265,7 @@ def _interaction_sample( locals_d, custom_chooser=None, sharrow_enabled=sharrow_enabled, - additional_columns=["tdd"], + additional_columns=["tdd", "origin_destination"], ) if sharrow_enabled: diff --git a/activitysim/core/interaction_sample_simulate.py b/activitysim/core/interaction_sample_simulate.py index 8951a3d07..a317d92af 100644 --- a/activitysim/core/interaction_sample_simulate.py +++ b/activitysim/core/interaction_sample_simulate.py @@ -166,7 +166,7 @@ def _interaction_sample_simulate( locals_d, custom_chooser=None, sharrow_enabled=sharrow_enabled, - additional_columns=["tdd"], + additional_columns=["tdd", "origin_destination"], ) interaction_df = alternatives.join(choosers, how="left", rsuffix="_chooser") From 6cb139b7675f903770e3235f229c41c4a99951d7 Mon Sep 17 00:00:00 2001 From: David Hensle Date: Mon, 6 May 2024 10:06:44 -0700 Subject: [PATCH 5/5] remove unneeded addition to categorical --- activitysim/abm/models/vehicle_allocation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/activitysim/abm/models/vehicle_allocation.py b/activitysim/abm/models/vehicle_allocation.py index 9061e99fb..632839b11 100644 --- a/activitysim/abm/models/vehicle_allocation.py +++ b/activitysim/abm/models/vehicle_allocation.py @@ -259,9 +259,7 @@ def vehicle_allocation( choices.loc[choices["alt_choice"] == alt, "choice"] = choosers.loc[ choices["alt_choice"] == alt, alt ] - # add non-household vehicle option to categories - if alts_from_spec[-1] not in choices["choice"].cat.categories: - choices["choice"] = choices["choice"].cat.add_categories(alts_from_spec[-1]) + # set choice for non-household vehicle option choices.loc[ choices["alt_choice"] == alts_from_spec[-1], "choice"