Skip to content

Commit ac4bfb0

Browse files
committed
bike model initial commit
1 parent f5a032d commit ac4bfb0

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

resident/configs/bike_comfort.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Label,Description,Expression,StrongAndFearless,EnthusedAndConfident,InterstedButConcerned,NoWayNoHow
2+
util_age,Age of persons in years,@df.age,coef_age_strong,coef_age_enthused,coef_age_concerned,0
3+
util_female,Female dummy,@df.female,coef_female_strong,coef_female_enthused,coef_female_concerned,0
4+
util_worker,Is worker dummy,@df.is_worker,coef_worker_strong,coef_worker_enthused,coef_worker_concerned,0
5+
util_holds_license,Person holds a license,@has_license,coef_has_license_strong,coef_has_license_enthused,coef_has_license_concerned,0
6+
#util_mobility_difficulty,Has mobility difficulty dummy,@df.has_mobility_difficulty,coef_mobility_difficulty_strong,coef_mobility_difficulty_enthused,coef_mobility_difficulty_concerned,0
7+
util_logsum_work_0auto,Work accessibility -- 0 autos,workplace_location_accessibility_0,coef_logsum_work_0auto_strong,coef_logsum_work_0auto_enthused,coef_logsum_work_0auto_concerned,0
8+
util_logsum_shop_0auto,Shopping accessibility -- 0 auto,shopping_accessibility_0,coef_logsum_shop_0auto_strong,coef_logsum_shop_0auto_enthused,coef_logsum_shop_0auto_concerned,0
9+
util_logsum_other_0auto,Other accessibility -- 0 autos,othdiscr_accessibility_0,coef_logsum_other_0auto_strong,coef_logsum_other_0auto_enthused,coef_logsum_other_0auto_concerned,0

resident/configs/bike_comfort.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
SPEC: bike_comfort.csv
3+
COEFFICIENTS: bike_comfort_coefficients.csv
4+
5+
LOGIT_TYPE: MNL
6+
7+
COMFORT_LEVEL:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
coefficient_name,value,constrain
2+
coef_age_strong,0,F
3+
coef_age_enthused,0,F
4+
coef_age_concerned,0,F
5+
coef_female_strong,0,F
6+
coef_female_enthused,0,F
7+
coef_female_concerned,0,F
8+
coef_worker_strong,0,F
9+
coef_worker_enthused,0,F
10+
coef_worker_concerned,0,F
11+
coef_has_license_strong,0,F
12+
coef_has_license_enthused,0,F
13+
coef_has_license_concerned,0,F
14+
coef_mobility_difficulty_strong,0,F
15+
coef_mobility_difficulty_enthused,0,F
16+
coef_mobility_difficulty_concerned,0,F
17+
coef_logsum_work_0auto_strong,0,F
18+
coef_logsum_work_0auto_enthused,0,F
19+
coef_logsum_work_0auto_concerned,0,F
20+
coef_logsum_shop_0auto_strong,0,F
21+
coef_logsum_shop_0auto_enthused,0,F
22+
coef_logsum_shop_0auto_concerned,0,F
23+
coef_logsum_other_0auto_strong,0,F
24+
coef_logsum_other_0auto_enthused,0,F
25+
coef_logsum_other_0auto_concerned,0,F

resident/extensions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from . import external_identification
33
from . import external_location_choice
44
from . import adjust_auto_operating_cost
5+
from . import bike_comfort
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# ActivitySim
2+
# See full license in LICENSE.txt.
3+
from __future__ import annotations
4+
5+
import logging
6+
7+
import pandas as pd
8+
9+
from activitysim.core import (
10+
config,
11+
estimation,
12+
expressions,
13+
simulate,
14+
tracing,
15+
workflow,
16+
)
17+
from activitysim.core.configuration.base import PreprocessorSettings
18+
from activitysim.core.configuration.logit import LogitComponentSettings
19+
20+
logger = logging.getLogger("activitysim")
21+
22+
class BikeComfortSettings(LogitComponentSettings, extra = "forbid"):
23+
"""
24+
Settings for the 'bike_comfort' model.
25+
"""
26+
CHOOSE_FILTER_COLUMN_NAME: str = "adult"
27+
"""Column name in the dataframe to filter persons eligible for license holding status model."""
28+
29+
@workflow.steps
30+
def bike_comfort(
31+
state: workflow.State,
32+
persons_merged: pd.DataFrame,
33+
persons: pd.DataFrame,
34+
model_settings: BikeComfortSettings | None = None,
35+
model_settings_file_name: str = "bike_comfort.yaml",
36+
trace_label: str = "bike_comfort",
37+
) -> None:
38+
"""
39+
This model predicts the bike comfort level for each person.
40+
The alternatives of this model are NoWyNoHow, InterestedButConcerned, EnthsuedAndConfident, StrongAndFearless
41+
"""
42+
43+
if model_settings is None:
44+
model_settings = BikeComfortSettings.read_settings_file(
45+
state.filesystem,
46+
model_settings_file_name,
47+
)
48+
49+
choosers = persons_merged
50+
chooser_filter_columun_name = model_settings.CHOOSE_FILTER_COLUMN_NAME
51+
choosers = choosers[(choosers[chooser_filter_columun_name])]
52+
logger.info("Running %s with %d persons", trace_label, len(choosers))
53+
54+
estimator = estimation.manager.begin_estimation(state, 'bike_comfort')
55+
56+
constants = config.get_model_constants(model_settings)
57+
58+
# - processor
59+
expressions.annotate_preprocesors(
60+
state,
61+
df=choosers,
62+
locals_dict=constants,
63+
skims=None,
64+
model_settings=model_settings,
65+
trace_label=trace_label,
66+
)
67+
68+
model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
69+
coefficients_df = state.filesystem.read_model_coefficients(model_settings)
70+
model_spec = simulate.eval_coefficients(
71+
state, model_spec, coefficients_df, estimator
72+
)
73+
nest_spec = config.get_logit_model_settings(model_settings)
74+
75+
if estimator:
76+
estimator.write_model_settings(model_settings, model_settings_file_name)
77+
estimator.write_spec(model_settings)
78+
estimator.write_coefficients(coefficients_df, model_settings)
79+
estimator.write_choosers(choosers)
80+
81+
choices = simulate.simple_simulate(
82+
state,
83+
choosers=choosers,
84+
spec=model_spec,
85+
nest_spec=nest_spec,
86+
locals_d=constants,
87+
trace_label=trace_label,
88+
trace_choice_name="bike_comfort",
89+
estimator=estimator,
90+
compute_settings=model_settings.compute_settings,
91+
)
92+
93+
choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
94+
bike_comfort_cat = pd.api.types.CategoricalDtype(
95+
model_spec.columns.tolist() + [""],
96+
ordered=False,
97+
)
98+
99+
choices = choices.astype(bike_comfort_cat)
100+
101+
if estimator:
102+
estimator.write_choices(choices)
103+
choices = estimator.get_survey_values(
104+
choices, "persons", "bike_comfort")
105+
estimator.write_override_choices(choices)
106+
estimator.end_estimation()
107+
108+
persons["bike_comfort"] = choices.reindex(persons.index).fillna("")
109+
110+
state.add_table("persons", persons)
111+
112+
tracing.print_summary(
113+
"bike_comfort", persons.telecommute_frequency, value_counts=True
114+
)
115+
116+
if state.settings.trace_hh_id:
117+
state.tracing.trace_df(persons, label=trace_label, warn_if_empty=True)
118+
119+
expressions.annotate_tables(
120+
state,
121+
locals_dict=constants,
122+
skims=None,
123+
model_settings=model_settings,
124+
trace_label=trace_label,
125+
)

0 commit comments

Comments
 (0)