Skip to content

Commit 6fedb03

Browse files
committed
✨ Refactor test_interaction_sample_simulate.py to include tests for global constants
Refactor the test file to include a new test case that verifies global constants (`SAMPLE_SCALE` and `SIMULATE_SCALE`) are available in both destination-choice substeps. This ensures consistency across sampling and simulation operations, enhancing reliability and maintainability of the codebase.
1 parent 5552330 commit 6fedb03

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

‎activitysim/core/test/test_interaction_sample_simulate.py‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd
88
import pytest
99

10-
from activitysim.core import interaction_sample_simulate, workflow
10+
from activitysim.core import interaction_sample, interaction_sample_simulate, workflow
1111
from activitysim.core.logit import AltsContext
1212

1313

@@ -18,6 +18,64 @@ def state() -> workflow.State:
1818
return state
1919

2020

21+
def test_global_constants_available_in_sampling_and_simulation(tmp_path):
22+
"""Global constants are available in both destination-choice substeps."""
23+
configs_dir = tmp_path.joinpath("configs")
24+
configs_dir.mkdir()
25+
configs_dir.joinpath("constants.yaml").write_text(
26+
"SAMPLE_SCALE: 3.0\nSIMULATE_SCALE: 2.0\n"
27+
)
28+
tmp_path.joinpath("data").mkdir()
29+
30+
state = workflow.State()
31+
state.initialize_filesystem(
32+
working_dir=tmp_path, configs_dir=("configs",)
33+
).default_settings()
34+
state.settings.check_for_variability = False
35+
36+
choosers = pd.DataFrame(
37+
{"chooser_attr": [1.0, 2.0]},
38+
index=pd.Index([0, 1], name="person_id"),
39+
)
40+
alternatives = pd.DataFrame(
41+
{"alt_attr": [1.0, 2.0]},
42+
index=pd.Index([10, 20], name="alt_id"),
43+
)
44+
45+
# Sampling and simulation use separate specifications in location and
46+
# destination choice, so exercise each expression-evaluation path.
47+
sample_spec = pd.DataFrame(
48+
{"coefficient": [1.0]},
49+
index=pd.Index(["alt_attr * SAMPLE_SCALE"], name="Expression"),
50+
)
51+
sample = interaction_sample.interaction_sample(
52+
state,
53+
choosers,
54+
alternatives,
55+
sample_spec,
56+
sample_size=0,
57+
alt_col_name="alt_id",
58+
)
59+
sampled_alternatives = sample.join(alternatives, on="alt_id")
60+
61+
simulate_spec = pd.DataFrame(
62+
{"coefficient": [1.0]},
63+
index=pd.Index(["alt_attr * SIMULATE_SCALE"], name="Expression"),
64+
)
65+
results = interaction_sample_simulate.interaction_sample_simulate(
66+
state,
67+
choosers,
68+
sampled_alternatives,
69+
simulate_spec,
70+
choice_column="alt_id",
71+
want_logsums=True,
72+
skip_choice=True,
73+
)
74+
75+
expected_logsum = np.logaddexp(2.0, 4.0)
76+
np.testing.assert_allclose(results["logsums"], expected_logsum)
77+
78+
2179
def test_interaction_sample_simulate_parity(state):
2280
# Run interaction_sample_simulate with and without explicit error terms and check that results are similar.
2381

0 commit comments

Comments
 (0)