Skip to content

Commit

Permalink
Update non mandatory tour frequency. Needs closer review
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekyadav26 committed Oct 24, 2023
1 parent dba895d commit 84106a4
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions activitysim/abm/models/non_mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
from typing import Any

import numpy as np
import pandas as pd
Expand All @@ -22,6 +23,8 @@
tracing,
workflow,
)
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
from activitysim.core.configuration.logit import LogitComponentSettings
from activitysim.core.interaction_simulate import interaction_simulate

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -147,11 +150,38 @@ def extend_tour_counts(
return tour_counts


class NonMandatoryTourFrequencySettings(PydanticReadable):
"""
Settings for the `non_mandatory_tour_frequency` component.
"""

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

SPEC: str = "non_mandatory_tour_frequency.csv"
"""Filename of the non mandatory tour frequency specification (.csv) file"""

SEGMENT_COL: str = "ptype"
# not used anymore TODO remove if needed

SPEC_SEGMENTS: dict[str, Any] = {}
# check the above

annotate_persons: PreprocessorSettings | None = None
"""Preprocessor settings to annotate persons"""

annotate_tours: PreprocessorSettings | None = None
"""Preprocessor settings to annotate persons"""


@workflow.step
def non_mandatory_tour_frequency(
state: workflow.State,
persons: pd.DataFrame,
persons_merged: pd.DataFrame,
model_settings: NonMandatoryTourFrequencySettings | None = None,
model_settings_file_name: str = "non_mandatory_tour_frequency.yaml",
trace_label: str = "non_mandatory_tour_frequency",
) -> None:
"""
This model predicts the frequency of making non-mandatory trips
Expand All @@ -160,10 +190,11 @@ def non_mandatory_tour_frequency(
othdiscr, eatout, and social trips in various combination.
"""

trace_label = "non_mandatory_tour_frequency"
model_settings_file_name = "non_mandatory_tour_frequency.yaml"

model_settings = state.filesystem.read_model_settings(model_settings_file_name)
if model_settings is None:
model_settings = NonMandatoryTourFrequencySettings.read_settings_file(
state.filesystem,
model_settings_file_name,
)

# FIXME kind of tacky both that we know to add this here and del it below
# 'tot_tours' is used in model_spec expressions
Expand All @@ -177,7 +208,7 @@ def non_mandatory_tour_frequency(
choosers = choosers[choosers.cdap_activity.isin(["M", "N"])]

# - preprocessor
preprocessor_settings = model_settings.get("preprocessor", None)
preprocessor_settings = model_settings.preprocessor
if preprocessor_settings:
locals_dict = {"person_max_window": lambda x: person_max_window(state, x)}

Expand All @@ -193,8 +224,8 @@ def non_mandatory_tour_frequency(

constants = config.get_model_constants(model_settings)

model_spec = state.filesystem.read_model_spec(file_name=model_settings["SPEC"])
spec_segments = model_settings.get("SPEC_SEGMENTS", {})
model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
spec_segments = model_settings.SPEC_SEGMENTS

# segment by person type and pick the right spec for each person type
choices_list = []
Expand Down Expand Up @@ -397,7 +428,7 @@ def non_mandatory_tour_frequency(
# need to re-compute tour frequency statistics to account for school escort tours
recompute_tour_count_statistics(state)

if model_settings.get("annotate_tours"):
if model_settings.annotate_tours:
annotate.annotate_tours(state, model_settings, trace_label)

expressions.assign_columns(
Expand Down

0 comments on commit 84106a4

Please sign in to comment.