Skip to content

Commit 23a9e72

Browse files
committed
mostly through test1
1 parent 5e90e65 commit 23a9e72

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

activitysim/abm/models/summarize.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010

1111
from activitysim.core import expressions, workflow
12-
from activitysim.core.configuration.base import PydanticReadable
12+
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
1313
from activitysim.core.los import Network_LOS
1414

1515
logger = logging.getLogger(__name__)
@@ -201,7 +201,7 @@ def manual_breaks(
201201
return bins
202202

203203

204-
class SummarizeSettings(PydanticReadable):
204+
class SummarizeSettings(PydanticReadable, extra="forbid"):
205205
"""
206206
Settings for the `summarize` component.
207207
"""
@@ -215,6 +215,8 @@ class SummarizeSettings(PydanticReadable):
215215
EXPORT_PIPELINE_TABLES: bool = True
216216
"""To export pipeline tables for expression development."""
217217

218+
preprocessor: PreprocessorSettings | None = None
219+
218220

219221
@workflow.step
220222
def summarize(

activitysim/abm/models/trip_matrices.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from __future__ import annotations
44

55
import logging
6+
from pathlib import Path
7+
from typing import Any
68

79
import numpy as np
810
import openmatrix as omx
@@ -15,6 +17,17 @@
1517
logger = logging.getLogger(__name__)
1618

1719

20+
class MatrixTableSettings(PydanticReadable):
21+
name: str
22+
data_field: str
23+
24+
25+
class MatrixSettings(PydanticReadable):
26+
file_name: Path
27+
tables: list[MatrixTableSettings] = []
28+
is_tap: bool = False
29+
30+
1831
class WriteTripMatricesSettings(PydanticReadable):
1932
"""
2033
Settings for the `write_trip_matrices` component.
@@ -26,6 +39,12 @@ class WriteTripMatricesSettings(PydanticReadable):
2639
HH_EXPANSION_WEIGHT_COL: str = "sample_rate"
2740
"""Column represents the sampling rate of households"""
2841

42+
MATRICES: list[MatrixSettings] = []
43+
44+
CONSTANTS: dict[str, Any] = {}
45+
46+
preprocessor: PreprocessorSettings | None = None
47+
2948

3049
@workflow.step(copy_tables=["trips"])
3150
def write_trip_matrices(
@@ -251,9 +270,11 @@ def write_trip_matrices(
251270
)
252271

253272

254-
@workflow.func
255273
def annotate_trips(
256-
state: workflow.State, trips: pd.DataFrame, network_los, model_settings
274+
state: workflow.State,
275+
trips: pd.DataFrame,
276+
network_los,
277+
model_settings: WriteTripMatricesSettings,
257278
):
258279
"""
259280
Add columns to local trips table. The annotator has
@@ -298,7 +319,7 @@ def annotate_trips(
298319

299320
# Data will be expanded by an expansion weight column from
300321
# the households pipeline table, if specified in the model settings.
301-
hh_weight_col = model_settings.get("HH_EXPANSION_WEIGHT_COL")
322+
hh_weight_col = model_settings.HH_EXPANSION_WEIGHT_COL
302323

303324
if hh_weight_col and hh_weight_col not in trips_df:
304325
logger.info("adding '%s' from households to trips table" % hh_weight_col)
@@ -314,7 +335,7 @@ def write_matrices(
314335
zone_index,
315336
orig_index,
316337
dest_index,
317-
model_settings,
338+
model_settings: WriteTripMatricesSettings,
318339
is_tap=False,
319340
):
320341
"""
@@ -329,30 +350,30 @@ def write_matrices(
329350
but the table 'data_field's must be summable types: ints, floats, bools.
330351
"""
331352

332-
matrix_settings = model_settings.get("MATRICES")
353+
matrix_settings = model_settings.MATRICES
333354

334355
if not matrix_settings:
335356
logger.error("Missing MATRICES setting in write_trip_matrices.yaml")
336357

337358
for matrix in matrix_settings:
338-
matrix_is_tap = matrix.get("is_tap", False)
359+
matrix_is_tap = matrix.is_tap
339360

340361
if matrix_is_tap == is_tap: # only write tap matrices to tap matrix files
341-
filename = matrix.get("file_name")
362+
filename = str(matrix.file_name)
342363
filepath = state.get_output_file_path(filename)
343364
logger.info("opening %s" % filepath)
344365
file = omx.open_file(str(filepath), "w") # possibly overwrite existing file
345-
table_settings = matrix.get("tables")
366+
table_settings = matrix.tables
346367

347368
for table in table_settings:
348-
table_name = table.get("name")
349-
col = table.get("data_field")
369+
table_name = table.name
370+
col = table.data_field
350371

351372
if col not in aggregate_trips:
352373
logger.error(f"missing {col} column in aggregate_trips DataFrame")
353374
return
354375

355-
hh_weight_col = model_settings.get("HH_EXPANSION_WEIGHT_COL")
376+
hh_weight_col = model_settings.HH_EXPANSION_WEIGHT_COL
356377
if hh_weight_col:
357378
aggregate_trips[col] = (
358379
aggregate_trips[col] / aggregate_trips[hh_weight_col]

activitysim/abm/models/trip_mode_choice.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
from typing import Any
67

78
import numpy as np
89
import pandas as pd
@@ -20,13 +21,13 @@
2021
workflow,
2122
)
2223
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
23-
from activitysim.core.configuration.logit import LogitComponentSettings
24+
from activitysim.core.configuration.logit import TemplatedLogitComponentSettings
2425
from activitysim.core.util import assign_in_place
2526

2627
logger = logging.getLogger(__name__)
2728

2829

29-
class TripModeChoiceSettings(LogitComponentSettings):
30+
class TripModeChoiceSettings(TemplatedLogitComponentSettings, extra="forbid"):
3031
"""
3132
Settings for the `trip_mode_choice` component.
3233
"""
@@ -41,6 +42,14 @@ class TripModeChoiceSettings(LogitComponentSettings):
4142
"""List of columns to be filtered from the dataframe to reduce memory
4243
needs filter chooser table to these fields"""
4344

45+
CHOOSER_COLS_TO_KEEP: list[str] = []
46+
47+
tvpb_mode_path_types: dict[str, Any] = {}
48+
49+
FORCE_ESCORTEE_CHAUFFEUR_MODE_MATCH: bool = True
50+
51+
annotate_trips: PreprocessorSettings | None = None
52+
4453

4554
@workflow.step
4655
def trip_mode_choice(
@@ -187,9 +196,9 @@ def trip_mode_choice(
187196
estimator.write_spec(model_settings)
188197
estimator.write_model_settings(model_settings, model_settings_file_name)
189198

190-
model_spec = state.filesystem.read_model_spec(file_name=model_settings["SPEC"])
199+
model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
191200
nest_spec = config.get_logit_model_settings(model_settings)
192-
cols_to_keep = model_settings.get("CHOOSER_COLS_TO_KEEP", None)
201+
cols_to_keep = model_settings.CHOOSER_COLS_TO_KEEP
193202

194203
choices_list = []
195204
cols_to_keep_list = []
@@ -296,7 +305,7 @@ def trip_mode_choice(
296305

297306
# add cached tvpb_logsum tap choices for modes specified in tvpb_mode_path_types
298307
if network_los.zone_system == los.THREE_ZONE:
299-
tvpb_mode_path_types = model_settings.get("tvpb_mode_path_types")
308+
tvpb_mode_path_types = model_settings.tvpb_mode_path_types
300309
for mode, path_type in tvpb_mode_path_types.items():
301310
skim_cache = tvpb_logsum_odt.cache[path_type]
302311

@@ -325,8 +334,9 @@ def trip_mode_choice(
325334

326335
assign_in_place(trips_df, choices_df)
327336

328-
if state.is_table("school_escort_tours") & model_settings.get(
329-
"FORCE_ESCORTEE_CHAUFFEUR_MODE_MATCH", True
337+
if (
338+
state.is_table("school_escort_tours")
339+
& model_settings.FORCE_ESCORTEE_CHAUFFEUR_MODE_MATCH
330340
):
331341
trips_df = (
332342
school_escort_tours_trips.force_escortee_trip_modes_to_match_chauffeur(
@@ -344,7 +354,7 @@ def trip_mode_choice(
344354

345355
state.add_table("trips", trips_df)
346356

347-
if model_settings.get("annotate_trips"):
357+
if model_settings.annotate_trips:
348358
annotate.annotate_trips(state, model_settings, trace_label, locals_dict)
349359

350360
if state.settings.trace_hh_id:

activitysim/abm/models/trip_scheduling.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import warnings
77
from builtins import range
8-
from typing import List, Literal
8+
from typing import Any, List, Literal
99

1010
import numpy as np
1111
import pandas as pd
@@ -456,6 +456,8 @@ class TripSchedulingSettings(PydanticReadable):
456456

457457
logic_version: int | None = None
458458

459+
CONSTANTS: dict[str, Any] = {}
460+
459461

460462
@workflow.step(copy_tables=False)
461463
def trip_scheduling(

activitysim/core/simulate.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,17 @@ def get_segment_coefficients(
397397
omnibus_coefficients = pd.read_csv(
398398
legacy_coeffs_file_path, comment="#", index_col="coefficient_name"
399399
)
400+
try:
401+
omnibus_coefficients_segment_name = omnibus_coefficients[segment_name]
402+
except KeyError as err:
403+
logger.error(f"No key {segment_name} found!")
404+
possible_keys = "\n- ".join(omnibus_coefficients.keys())
405+
logger.error(f"possible keys include: \n- {possible_keys}")
406+
raise
400407
coefficients_dict = assign.evaluate_constants(
401-
omnibus_coefficients[segment_name], constants=constants
408+
omnibus_coefficients_segment_name, constants=constants
402409
)
410+
403411
else:
404412
coefficients_df = filesystem.read_model_coefficients(model_settings)
405413
template_df = read_model_coefficient_template(filesystem, model_settings)

0 commit comments

Comments
 (0)