Skip to content

Commit 5da9715

Browse files
committed
unit test
1 parent 0e34b7d commit 5da9715

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
configs*/
2+
output/
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from __future__ import annotations
2+
3+
# ActivitySim
4+
# See full license in LICENSE.txt.
5+
import importlib.resources
6+
import os
7+
from shutil import copytree
8+
9+
import pandas as pd
10+
import pytest
11+
import yaml
12+
13+
from activitysim.core import workflow
14+
15+
16+
def example_path(dirname):
17+
resource = os.path.join("examples", "prototype_mtc", dirname)
18+
return str(importlib.resources.files("activitysim").joinpath(resource))
19+
20+
21+
def dir_test_path(dirname):
22+
return os.path.join(os.path.dirname(__file__), dirname)
23+
24+
25+
data_dir = example_path("data")
26+
new_configs_dir = dir_test_path("configs")
27+
new_settings_file = os.path.join(new_configs_dir, "settings.yaml")
28+
# copy example configs to test/skip_failed_choices/configs if not already there
29+
if not os.path.exists(new_configs_dir):
30+
copytree(example_path("configs"), new_configs_dir)
31+
32+
33+
def update_settings(settings_file, key, value):
34+
with open(settings_file, "r") as f:
35+
settings = yaml.safe_load(f)
36+
f.close()
37+
38+
settings[key] = value
39+
40+
with open(settings_file, "w") as f:
41+
yaml.safe_dump(settings, f)
42+
f.close()
43+
44+
45+
def update_uec_csv(uec_file, expression, coef_value):
46+
# read in the uec file
47+
df = pd.read_csv(uec_file)
48+
# append a new row, set expression and coef_value
49+
df.loc[len(df), "Expression"] = expression
50+
# from the 4th column onward are coefficients
51+
for col in df.columns[3:]:
52+
df.loc[len(df) - 1, col] = coef_value
53+
df.to_csv(uec_file, index=False)
54+
55+
56+
@pytest.fixture
57+
def state():
58+
configs_dir = new_configs_dir
59+
output_dir = dir_test_path("output")
60+
data_dir = example_path("data")
61+
62+
# turn the global setting on to skip failed choices
63+
update_settings(new_settings_file, "skip_failed_choices", True)
64+
65+
# make some choices fail by setting extreme coefficients in the uec
66+
# auto ownership
67+
auto_ownership_uec_file = os.path.join(new_configs_dir, "auto_ownership.csv")
68+
# forcing households in home zone 8 (recoded 7) to fail auto ownership choice
69+
update_uec_csv(auto_ownership_uec_file, "@df.home_zone_id==7", -999.0)
70+
71+
# work location choice
72+
work_location_choice_uec_file = os.path.join(
73+
new_configs_dir, "workplace_location.csv"
74+
)
75+
# forcing workers from home zone 18 to fail work location choice
76+
# as if there is a network connection problem for zone 18
77+
update_uec_csv(work_location_choice_uec_file, "@df.home_zone_id==18", -999.0)
78+
79+
# trip mode choice
80+
trip_mode_choice_uec_file = os.path.join(new_configs_dir, "trip_mode_choice.csv")
81+
# forcing trips on drivealone tours to fail trip mode choice
82+
update_uec_csv(trip_mode_choice_uec_file, "@df.tour_mode=='DRIVEALONEFREE'", -999.0)
83+
84+
state = workflow.State.make_default(
85+
configs_dir=configs_dir,
86+
output_dir=output_dir,
87+
data_dir=data_dir,
88+
)
89+
90+
from activitysim.abm.tables.skims import network_los_preload
91+
92+
state.get(network_los_preload)
93+
94+
state.logging.config_logger()
95+
return state
96+
97+
98+
def test_skip_failed_choices(state):
99+
100+
# check that the setting is indeed set to True
101+
assert state.settings.skip_failed_choices is True
102+
103+
state.run(models=state.settings.models, resume_after=None)
104+
105+
# check that the number of skipped households is recorded in state
106+
assert state.get("num_skipped_households", 0) == 943
107+
108+
# check that there are no DRIVEALONEFREE tours in the final tours
109+
final_tours_df = state.get_dataframe("tours")
110+
assert "DRIVEALONEFREE" not in final_tours_df["tour_mode"].values
111+
112+
# check that there are no households in home zone 8 (recoded 7) in the final households
113+
final_households_df = state.get_dataframe("households")
114+
assert not any(final_households_df["home_zone_id"] == 7)
115+
116+
# check that there are no workers from households in home zone 18 in the final persons
117+
final_persons_df = state.get_dataframe("persons")
118+
assert not any(
119+
(final_persons_df["home_zone_id"] == 18)
120+
& (final_persons_df["is_worker"] == True)
121+
)

0 commit comments

Comments
 (0)