Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion activitysim/abm/models/atwork_subtour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd

from activitysim.abm.models.util import tour_destination
from activitysim.core import config, estimation, los, tracing, workflow
from activitysim.core import config, estimation, los, tracing, workflow, expressions
from activitysim.core.configuration.logit import TourLocationComponentSettings
from activitysim.core.util import assign_in_place

Expand Down Expand Up @@ -120,3 +120,11 @@ def atwork_subtour_destination(
state.tracing.trace_df(
tours, label="atwork_subtour_destination", columns=["destination"]
)

expressions.annotate_tables(
state,
locals_dict={},
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
30 changes: 19 additions & 11 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class AtworkSubtourFrequencySettings(LogitComponentSettings, extra="forbid"):
Settings for the `atwork_subtour_frequency` component.
"""

preprocessor: PreprocessorSettings | None = None
"""Setting for the preprocessor."""
# no additional fields are required for this component
pass


@workflow.step
Expand Down Expand Up @@ -92,15 +92,15 @@ def atwork_subtour_frequency(
nest_spec = config.get_logit_model_settings(model_settings)
constants = config.get_model_constants(model_settings)

# - preprocessor
preprocessor_settings = model_settings.preprocessor
if preprocessor_settings:
expressions.assign_columns(
state,
df=work_tours,
model_settings=preprocessor_settings,
trace_label=trace_label,
)
# preprocess choosers
expressions.annotate_preprocessors(
state,
df=work_tours,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)

if estimator:
estimator.write_spec(model_settings)
Expand Down Expand Up @@ -164,3 +164,11 @@ def atwork_subtour_frequency(

if trace_hh_id:
state.tracing.trace_df(tours, label="atwork_subtour_frequency.tours")

expressions.annotate_tables(
state,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
19 changes: 8 additions & 11 deletions activitysim/abm/models/atwork_subtour_mode_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,18 @@ def atwork_subtour_mode_choice(
)
state.add_table("tours", tours)

# - annotate tours table
if model_settings.annotate_tours:
tours = state.get_dataframe("tours")
expressions.assign_columns(
state,
df=tours,
model_settings=model_settings.annotate_tours,
trace_label=tracing.extend_trace_label(trace_label, "annotate_tours"),
)
state.add_table("tours", tours)

if trace_hh_id:
state.tracing.trace_df(
tours[tours.tour_category == "atwork"],
label=tracing.extend_trace_label(trace_label, mode_column_name),
slicer="tour_id",
index_label="tour_id",
)

expressions.annotate_tables(
state,
locals_dict=constants,
skims=skims,
model_settings=model_settings,
trace_label=trace_label,
)
8 changes: 8 additions & 0 deletions activitysim/abm/models/atwork_subtour_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,11 @@ def atwork_subtour_scheduling(
trace_label,
"tour_map",
)

expressions.annotate_tables(
state,
locals_dict=constants,
skims=skims,
model_settings=model_settings,
trace_label=trace_label,
)
39 changes: 18 additions & 21 deletions activitysim/abm/models/auto_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
from activitysim.core.configuration.logit import LogitComponentSettings

from .util import annotate

logger = logging.getLogger(__name__)


Expand All @@ -28,8 +26,8 @@ class AutoOwnershipSettings(LogitComponentSettings, extra="forbid"):
Settings for the `auto_ownership` component.
"""

preprocessor: PreprocessorSettings | None = None
annotate_households: PreprocessorSettings | None = None
# no additional fields are required for this component
pass


@workflow.step
Expand Down Expand Up @@ -69,20 +67,14 @@ def auto_ownership_simulate(

logger.info("Running %s with %d households", trace_label, len(choosers))

# - preprocessor
preprocessor_settings = model_settings.preprocessor
if preprocessor_settings:
locals_d = {}
if constants is not None:
locals_d.update(constants)

expressions.assign_columns(
state,
df=choosers,
model_settings=preprocessor_settings,
locals_dict=locals_d,
trace_label=trace_label,
)
expressions.annotate_preprocessors(
state,
df=choosers,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)

if estimator:
estimator.write_model_settings(model_settings, model_settings_file_name)
Expand Down Expand Up @@ -120,8 +112,13 @@ def auto_ownership_simulate(
"auto_ownership", households.auto_ownership, value_counts=True
)

if model_settings.annotate_households:
annotate.annotate_households(state, model_settings, trace_label)

if trace_hh_id:
state.tracing.trace_df(households, label="auto_ownership", warn_if_empty=True)

expressions.annotate_tables(
state,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
41 changes: 25 additions & 16 deletions activitysim/abm/models/cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ class CdapSettings(PydanticReadable, extra="forbid"):
JOINT_TOUR_COEFFICIENTS: str = "cdap_joint_tour_coefficients.csv"
JOINT_TOUR_USEFUL_COLUMNS: list[str] | None = None
"""Columns to include from the persons table that will be need to calculate household joint tour utility."""
annotate_persons: PreprocessorSettings | None = None
annotate_households: PreprocessorSettings | None = None
COEFFICIENTS: Path
CONSTANTS: dict[str, Any] = {}
compute_settings: ComputeSettings | None = None

preprocessor: PreprocessorSettings | None = None
"""Preprocess choosers tables before running the model."""
annotate_persons: PreprocessorSettings | None = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic documentation tools will work better if annotate_persons has its own docstring, even though it's clear to a human reader the one docstring below applies to both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"""Postprocess persons table after model completion."""
annotate_households: PreprocessorSettings | None = None
"""Postprocess households table after model completion."""


@workflow.step
def cdap_simulate(
Expand Down Expand Up @@ -171,6 +176,16 @@ def cdap_simulate(
index=True,
)

# preprocess choosers
expressions.annotate_preprocessors(
state,
df=persons_merged,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)

if estimator:
estimator.write_model_settings(model_settings, "cdap.yaml")
estimator.write_spec(model_settings, tag="INDIV_AND_HHSIZE1_SPEC")
Expand Down Expand Up @@ -241,31 +256,25 @@ def cdap_simulate(
cap_cat_type = pd.api.types.CategoricalDtype(["", "M", "N", "H"], ordered=False)
choices = choices.astype(cap_cat_type)
persons["cdap_activity"] = choices

expressions.assign_columns(
state,
df=persons,
model_settings=model_settings.annotate_persons,
trace_label=tracing.extend_trace_label(trace_label, "annotate_persons"),
)

state.add_table("persons", persons)

# - annotate households table
if add_joint_tour_utility:
hh_joint = hh_joint.reindex(households.index)
households["has_joint_tour"] = hh_joint

expressions.assign_columns(
state,
df=households,
model_settings=model_settings.annotate_households,
trace_label=tracing.extend_trace_label(trace_label, "annotate_households"),
)
state.add_table("households", households)

tracing.print_summary("cdap_activity", persons.cdap_activity, value_counts=True)
logger.info(
"cdap crosstabs:\n%s"
% pd.crosstab(persons.ptype, persons.cdap_activity, margins=True)
)

expressions.annotate_tables(
state,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
35 changes: 17 additions & 18 deletions activitysim/abm/models/free_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class FreeParkingSettings(LogitComponentSettings, extra="forbid"):
Settings for the `free_parking` component.
"""

preprocessor: PreprocessorSettings | None = None
"""Setting for the preprocessor."""

FREE_PARKING_ALT: int
"""The code for free parking."""

Expand Down Expand Up @@ -78,21 +75,6 @@ def free_parking(

constants = model_settings.CONSTANTS or {}

# - preprocessor
preprocessor_settings = model_settings.preprocessor
if preprocessor_settings:
locals_d = {}
if constants is not None:
locals_d.update(constants)

expressions.assign_columns(
state,
df=choosers,
model_settings=preprocessor_settings,
locals_dict=locals_d,
trace_label=trace_label,
)

model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
coefficients_df = state.filesystem.read_model_coefficients(model_settings)
model_spec = simulate.eval_coefficients(
Expand All @@ -101,6 +83,15 @@ def free_parking(

nest_spec = config.get_logit_model_settings(model_settings)

expressions.annotate_preprocessors(
state,
df=choosers,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)

if estimator:
estimator.write_model_settings(model_settings, model_settings_file_name)
estimator.write_spec(file_name=model_settings.SPEC)
Expand Down Expand Up @@ -144,3 +135,11 @@ def free_parking(

if state.settings.trace_hh_id:
state.tracing.trace_df(persons, label=trace_label, warn_if_empty=True)

expressions.annotate_tables(
state,
locals_dict=constants,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
51 changes: 29 additions & 22 deletions activitysim/abm/models/joint_tour_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from activitysim.core.configuration.base import PreprocessorSettings
from activitysim.core.configuration.logit import LogitComponentSettings


logger = logging.getLogger(__name__)


Expand All @@ -36,8 +37,7 @@ class JointTourCompositionSettings(LogitComponentSettings, extra="forbid"):
Settings for the `joint_tour_composition` component.
"""

preprocessor: PreprocessorSettings | None = None
"""Setting for the preprocessor."""
pass


@workflow.step
Expand Down Expand Up @@ -78,26 +78,6 @@ def joint_tour_composition(
"Running joint_tour_composition with %d joint tours" % joint_tours.shape[0]
)

# - run preprocessor
preprocessor_settings = model_settings.preprocessor
if preprocessor_settings:
locals_dict = {
"persons": persons,
"hh_time_window_overlap": lambda *x: hh_time_window_overlap(state, *x),
}

expressions.assign_columns(
state,
df=households,
model_settings=preprocessor_settings,
locals_dict=locals_dict,
trace_label=trace_label,
)

joint_tours_merged = pd.merge(
joint_tours, households, left_on="household_id", right_index=True, how="left"
)

# - simple_simulate
model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
coefficients_df = state.filesystem.read_model_coefficients(model_settings)
Expand All @@ -108,6 +88,25 @@ def joint_tour_composition(
nest_spec = config.get_logit_model_settings(model_settings)
constants = config.get_model_constants(model_settings)

locals_dict = {
"persons": persons,
"hh_time_window_overlap": lambda *x: hh_time_window_overlap(state, *x),
}
locals_dict.update(constants)

expressions.annotate_preprocessors(
state,
df=households,
locals_dict=locals_dict,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)

joint_tours_merged = pd.merge(
joint_tours, households, left_on="household_id", right_index=True, how="left"
)

if estimator:
estimator.write_spec(model_settings)
estimator.write_model_settings(model_settings, model_settings_file_name)
Expand Down Expand Up @@ -156,3 +155,11 @@ def joint_tour_composition(
label="joint_tour_composition.joint_tours",
slicer="household_id",
)

expressions.annotate_tables(
state,
locals_dict=locals_dict,
skims=None,
model_settings=model_settings,
trace_label=trace_label,
)
Loading
Loading