Skip to content

Commit 0f5234e

Browse files
committed
Fix chooser column deprecation regressions
1 parent c53002f commit 0f5234e

18 files changed

Lines changed: 318 additions & 93 deletions

‎activitysim/abm/models/location_choice.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ def location_sample(
242242
chunk_tag,
243243
trace_label,
244244
):
245-
choosers = persons_merged
245+
# The former column selection returned an independent frame. Preserve that
246+
# isolation so component preprocessors cannot leak annotations into the
247+
# shared persons table or into later location-choice segments.
248+
choosers = persons_merged.copy()
246249

247250
# create wrapper with keys for this lookup - in this case there is a home_zone_id in the choosers
248251
# and a zone_id in the alternatives which get merged during interaction
@@ -434,7 +437,8 @@ def location_presample(
434437
HOME_TAZ in persons_merged
435438
) # 'TAZ' should already be in persons_merged from land_use
436439

437-
choosers = persons_merged
440+
# Keep chooser annotations local to this model segment.
441+
choosers = persons_merged.copy()
438442

439443
# create wrapper with keys for this lookup - in this case there is a HOME_TAZ in the choosers
440444
# and a DEST_TAZ in the alternatives which get merged during interaction
@@ -669,7 +673,9 @@ def run_location_simulate(
669673
"""
670674
assert not persons_merged.empty
671675

672-
choosers = persons_merged
676+
# Preprocessors annotate choosers in place. Use a copy so those temporary
677+
# columns do not affect subsequent segments that share persons_merged.
678+
choosers = persons_merged.copy()
673679

674680
alt_dest_col_name = model_settings.ALT_DEST_COL_NAME
675681

‎activitysim/abm/models/school_escorting.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class SchoolEscortSettings(BaseLogitComponentSettings, extra="forbid"):
343343
This setting is now obsolete and does nothing. Its functionality has been
344344
replaced by :func:`activitysim.core.util.drop_unused_columns`.
345345
346-
.. deprecated:: 1.4
346+
.. deprecated:: 1.6
347347
"""
348348

349349
@field_validator("SIMULATE_CHOOSER_COLUMNS", mode="before")
@@ -576,10 +576,14 @@ def school_escorting(
576576
)
577577

578578
if stage_num >= 1:
579-
choosers["alt"] = choices
580-
choosers = choosers.join(alts, how="left", on="alt")
579+
# The raw alternative columns are only needed to construct bundle
580+
# records. Do not retain them on the chooser state: the final
581+
# stage would otherwise try to join the same columns a second time.
582+
bundle_choosers = choosers.assign(alt=choices).join(
583+
alts, how="left", on="alt"
584+
)
581585
bundles = create_school_escorting_bundles_table(
582-
choosers[choosers["alt"] > 1], tours, stage
586+
bundle_choosers[bundle_choosers["alt"] > 1], tours, stage
583587
)
584588
escort_bundles.append(bundles)
585589

‎activitysim/abm/models/util/tour_destination.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,10 @@ def destination_presample(
637637

638638
orig_maz = model_settings.CHOOSER_ORIG_COL_NAME
639639
assert orig_maz in choosers
640-
if ORIG_TAZ not in choosers:
641-
choosers[ORIG_TAZ] = network_los.map_maz_to_taz(choosers[orig_maz])
640+
# This is the TAZ for the configured tour origin. A wider chooser table
641+
# may already contain a same-named home TAZ, which is incorrect for models
642+
# such as at-work subtour destination choice.
643+
choosers[ORIG_TAZ] = network_los.map_maz_to_taz(choosers[orig_maz])
642644

643645
# create wrapper with keys for this lookup - in this case there is a HOME_TAZ in the choosers
644646
# and a DEST_TAZ in the alternatives which get merged during interaction

‎activitysim/abm/models/util/tour_od.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ def run_od_sample(
736736
coefficients_file_name=model_settings.COEFFICIENTS,
737737
)
738738

739-
choosers = tours
739+
# Preserve the independent-frame behavior of the former column subset.
740+
choosers = tours.copy()
740741

741742
# interaction_sample requires that choosers.index.is_monotonic_increasing
742743
if not choosers.index.is_monotonic_increasing:
@@ -987,7 +988,9 @@ def run_od_simulate(
987988
)
988989

989990
# merge persons into tours
990-
choosers = tours
991+
# Preprocessors may annotate choosers in place; keep those columns local to
992+
# this segment instead of mutating the shared tours table.
993+
choosers = tours.copy()
991994

992995
# interaction_sample requires that choosers.index.is_monotonic_increasing
993996
if not choosers.index.is_monotonic_increasing:

‎activitysim/abm/models/util/tour_scheduling.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def run_tour_scheduling(
2424
trace_label: str,
2525
):
2626

27+
# The deprecated chooser-column filter returned a new frame. Retain that
28+
# isolation because vectorized scheduling annotates merged chooser data.
29+
persons_merged = persons_merged.copy()
30+
2731
timetable = state.get_injectable("timetable")
2832

2933
# - run preprocessor to annotate choosers

‎activitysim/abm/models/util/vectorize_tour_scheduling.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TourSchedulingSettings(LogitComponentSettings, extra="forbid"):
5151
This setting is now obsolete and does nothing. Its functionality has been
5252
replaced by :func:`activitysim.core.util.drop_unused_columns`.
5353
54-
.. deprecated:: 1.4
54+
.. deprecated:: 1.6
5555
"""
5656

5757
@field_validator("SIMULATE_CHOOSER_COLUMNS", mode="before")

‎activitysim/abm/test/test_misc/test_tour_destination_sampling.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_destination_presample_uses_taz_stable_mapping(monkeypatch):
6060
def fake_destination_sample(
6161
_state,
6262
_spec_segment_name,
63-
_choosers,
63+
choosers,
6464
destination_size_terms,
6565
_skims,
6666
_estimator,
@@ -72,6 +72,7 @@ def fake_destination_sample(
7272
stable_alt_positions=None,
7373
n_total_alts=None,
7474
):
75+
captured["origin_taz"] = choosers[tour_destination.ORIG_TAZ].copy()
7576
captured["alt_dest_col_name"] = alt_dest_col_name
7677
captured["zone_layer"] = zone_layer
7778
captured["active_taz_index"] = destination_size_terms.index.copy()
@@ -106,7 +107,12 @@ def fake_choose_maz_for_taz(
106107
state = workflow.State().default_settings()
107108
state.settings.use_explicit_error_terms = True
108109
choosers = pd.DataFrame(
109-
{"origin": [101]},
110+
{
111+
"origin": [101],
112+
# A merged person table may carry a home TAZ that does not match
113+
# the configured tour origin.
114+
tour_destination.ORIG_TAZ: [99],
115+
},
110116
index=pd.Index([7001], name="tour_id"),
111117
)
112118
model_settings = type(
@@ -152,6 +158,10 @@ def fake_choose_maz_for_taz(
152158
assert captured["zone_layer"] == "taz"
153159
assert captured["n_total_alts"] == 3
154160
assert list(captured["stable_alt_positions"]) == [0, 2]
161+
pd.testing.assert_series_equal(
162+
captured["origin_taz"],
163+
pd.Series([1], index=choosers.index, name=tour_destination.ORIG_TAZ),
164+
)
155165
pd.testing.assert_index_equal(
156166
captured["full_taz_index"],
157167
pd.Index([1, 2, 3], name=tour_destination.DEST_TAZ),

‎activitysim/core/configuration/logit.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class TourLocationComponentSettings(LocationComponentSettings, extra="forbid"):
267267
This setting is now obsolete and does nothing. Its functionality has been
268268
replaced by :func:`activitysim.core.util.drop_unused_columns`.
269269
270-
.. deprecated:: 1.4
270+
.. deprecated:: 1.6
271271
"""
272272

273273
ALT_DEST_COL_NAME: str
@@ -305,7 +305,7 @@ class TourModeComponentSettings(TemplatedLogitComponentSettings, extra="forbid")
305305
This setting is now obsolete and does nothing. Its functionality has been
306306
replaced by :func:`activitysim.core.util.drop_unused_columns`.
307307
308-
.. deprecated:: 1.4
308+
.. deprecated:: 1.6
309309
"""
310310

311311
@field_validator("LOGSUM_CHOOSER_COLUMNS", mode="before")

‎activitysim/core/interaction_sample.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,7 @@ def _interaction_sample(
572572
# written out above, so here we only need to preserve the columns used to
573573
# identify the traced rows in the interaction dataframe
574574
trace_columns = (
575-
["person_id", "proto_person_id", "proto_household_id"]
576-
if have_trace_targets
577-
else []
575+
util.traceable_id_columns(choosers) if have_trace_targets else []
578576
)
579577

580578
choosers = util.drop_unused_columns(

‎activitysim/core/interaction_sample_simulate.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,33 @@ def _interaction_sample_simulate(
154154
if compute_settings is None:
155155
compute_settings = ComputeSettings()
156156

157-
# check if tracing is enabled and if we have trace targets
158-
# if not estimation mode, drop unused columns
159-
if (
160-
(not have_trace_targets)
161-
and (estimator is None)
162-
and (compute_settings.drop_unused_columns)
163-
):
157+
if compute_settings.drop_unused_columns:
158+
identity_columns = (
159+
util.traceable_id_columns(choosers)
160+
if have_trace_targets or estimator is not None
161+
else []
162+
)
163+
estimator_chooser_columns = (
164+
[estimator.chooser_id_column_name]
165+
if estimator is not None and estimator.chooser_id_column_name is not None
166+
else []
167+
)
168+
estimator_alternative_columns = (
169+
[estimator.alt_id_column_name]
170+
if estimator is not None and estimator.alt_id_column_name is not None
171+
else []
172+
)
164173
choosers = util.drop_unused_columns(
165174
choosers,
166175
spec,
167176
locals_d,
168177
custom_chooser=None,
169178
sharrow_enabled=sharrow_enabled,
170-
additional_columns=compute_settings.protect_columns,
179+
additional_columns=(
180+
identity_columns
181+
+ estimator_chooser_columns
182+
+ compute_settings.protect_columns
183+
),
171184
)
172185

173186
alternatives = util.drop_unused_columns(
@@ -176,7 +189,11 @@ def _interaction_sample_simulate(
176189
locals_d,
177190
custom_chooser=None,
178191
sharrow_enabled=sharrow_enabled,
179-
additional_columns=["tdd"] + compute_settings.protect_columns,
192+
additional_columns=(
193+
["tdd"]
194+
+ estimator_alternative_columns
195+
+ compute_settings.protect_columns
196+
),
180197
)
181198

182199
interaction_df = alternatives.join(choosers, how="left", rsuffix="_chooser")

0 commit comments

Comments
 (0)