|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pandas.testing as pdt |
| 8 | + |
| 9 | +from activitysim.abm.models import location_choice |
| 10 | + |
| 11 | + |
| 12 | +def test_estimation_override_preserves_destination_choice_logsum(monkeypatch): |
| 13 | + """Survey overrides should only change the chosen destination's mode logsum.""" |
| 14 | + person_index = pd.Index([1], name="person_id") |
| 15 | + location_sample = pd.DataFrame( |
| 16 | + { |
| 17 | + "alt_dest": [101, 202], |
| 18 | + location_choice.ALT_LOGSUM: [5.0, 7.0], |
| 19 | + }, |
| 20 | + index=pd.Index([1, 1], name="person_id"), |
| 21 | + ) |
| 22 | + modeled_choices = pd.DataFrame( |
| 23 | + {"choice": [101], "logsum": [-1.5]}, index=person_index |
| 24 | + ) |
| 25 | + |
| 26 | + # Keep the test focused on the estimation override and final logsum merge. |
| 27 | + monkeypatch.setattr( |
| 28 | + location_choice, |
| 29 | + "run_location_sample", |
| 30 | + lambda *args, **kwargs: location_sample.copy(), |
| 31 | + ) |
| 32 | + monkeypatch.setattr( |
| 33 | + location_choice, |
| 34 | + "run_location_logsums", |
| 35 | + lambda *args, **kwargs: location_sample.copy(), |
| 36 | + ) |
| 37 | + monkeypatch.setattr( |
| 38 | + location_choice, |
| 39 | + "run_location_simulate", |
| 40 | + lambda *args, **kwargs: modeled_choices.copy(), |
| 41 | + ) |
| 42 | + |
| 43 | + estimator = Mock() |
| 44 | + estimator.get_survey_values.return_value = pd.Series( |
| 45 | + [202], index=person_index, name="choice" |
| 46 | + ) |
| 47 | + shadow_price_calculator = Mock() |
| 48 | + shadow_price_calculator.dest_size_terms.return_value = pd.Series( |
| 49 | + [1.0, 1.0], index=[101, 202] |
| 50 | + ) |
| 51 | + model_settings = SimpleNamespace( |
| 52 | + ALT_DEST_COL_NAME="alt_dest", |
| 53 | + CHOOSER_SEGMENT_COLUMN_NAME="segment", |
| 54 | + DEST_CHOICE_COLUMN_NAME="workplace_zone_id", |
| 55 | + LOGSUM_SETTINGS="tour_mode_choice.yaml", |
| 56 | + SEGMENT_IDS={"workers": 1}, |
| 57 | + ) |
| 58 | + state = SimpleNamespace( |
| 59 | + settings=SimpleNamespace( |
| 60 | + sample_method="inverse_cdf", |
| 61 | + trace_hh_id=None, |
| 62 | + use_explicit_error_terms=False, |
| 63 | + ) |
| 64 | + ) |
| 65 | + persons = pd.DataFrame({"segment": [1]}, index=person_index) |
| 66 | + |
| 67 | + choices, sample = location_choice.run_location_choice( |
| 68 | + state=state, |
| 69 | + persons_merged_df=persons, |
| 70 | + network_los=Mock(), |
| 71 | + shadow_price_calculator=shadow_price_calculator, |
| 72 | + want_logsums=True, |
| 73 | + want_sample_table=False, |
| 74 | + estimator=estimator, |
| 75 | + model_settings=model_settings, |
| 76 | + chunk_size=0, |
| 77 | + chunk_tag="workplace_location", |
| 78 | + trace_label="workplace_location", |
| 79 | + ) |
| 80 | + |
| 81 | + expected = pd.DataFrame( |
| 82 | + { |
| 83 | + "choice": [202], |
| 84 | + "logsum": [-1.5], |
| 85 | + location_choice.ALT_LOGSUM: [7.0], |
| 86 | + }, |
| 87 | + index=person_index, |
| 88 | + ) |
| 89 | + pdt.assert_frame_equal(choices, expected) |
| 90 | + assert sample is None |
0 commit comments