Skip to content

Commit

Permalink
Common settings for fastmath, sharrow_skip, and other compute control…
Browse files Browse the repository at this point in the history
…s across components (#824)

* prevent crash when sharrow-test fails in interaction-simulate

* sharrow_fastmath

* activate sharrow without fastmath in example

* change fastmath default to F for escort

* skip and fastmath in sharrow_settings

* fix deprecation handling

* fix fastmath setting on escort configs

also forbid extra in pydantic settings

* pin pandera on other envs

* remove unused LOGSUM_CHOOSER_COLUMNS

* fix sharrow skip in simple simulate

* fix regressions

* sharrow_settings in cdap

* add missing sharrow_settings

* pandas compute options

* sharrow_settings -> compute_settings

* fix transit-pass-subsidy model for compute_settings

* strip outdated code

* change class name to PydanticCompute for clarity
  • Loading branch information
jpn-- committed Apr 18, 2024
1 parent c5bab95 commit 751ee44
Show file tree
Hide file tree
Showing 43 changed files with 533 additions and 242 deletions.
1 change: 1 addition & 0 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def atwork_subtour_frequency(
trace_label=trace_label,
trace_choice_name="atwork_subtour_frequency",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# convert indexes to alternative names
Expand Down
3 changes: 1 addition & 2 deletions activitysim/abm/models/atwork_subtour_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def atwork_subtour_scheduling(
estimator = estimation.manager.begin_estimation(state, "atwork_subtour_scheduling")

model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
sharrow_skip = model_settings.sharrow_skip
coefficients_df = state.filesystem.read_model_coefficients(model_settings)
model_spec = simulate.eval_coefficients(
state, model_spec, coefficients_df, estimator
Expand Down Expand Up @@ -96,7 +95,7 @@ def atwork_subtour_scheduling(
estimator=estimator,
chunk_size=state.settings.chunk_size,
trace_label=trace_label,
sharrow_skip=sharrow_skip,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
6 changes: 4 additions & 2 deletions activitysim/abm/models/auto_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

from activitysim.core import (
config,
expressions,
estimation,
expressions,
simulate,
tracing,
workflow,
)
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
from activitysim.core.configuration.logit import LogitComponentSettings

from .util import annotate

logger = logging.getLogger(__name__)


class AutoOwnershipSettings(LogitComponentSettings):
class AutoOwnershipSettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `auto_ownership` component.
"""
Expand Down Expand Up @@ -99,6 +100,7 @@ def auto_ownership_simulate(
trace_choice_name="auto_ownership",
log_alt_losers=log_alt_losers,
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
9 changes: 8 additions & 1 deletion activitysim/abm/models/cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
tracing,
workflow,
)
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
from activitysim.core.configuration.base import (
ComputeSettings,
PreprocessorSettings,
PydanticReadable,
)
from activitysim.core.util import reindex

logger = logging.getLogger(__name__)
Expand All @@ -34,6 +38,7 @@ class CdapSettings(PydanticReadable, extra="forbid"):
annotate_households: PreprocessorSettings | None = None
COEFFICIENTS: Path
CONSTANTS: dict[str, Any] = {}
compute_settings: ComputeSettings | None = None


@workflow.step
Expand Down Expand Up @@ -202,6 +207,7 @@ def cdap_simulate(
trace_hh_id=trace_hh_id,
trace_label=trace_label,
add_joint_tour_utility=add_joint_tour_utility,
compute_settings=model_settings.compute_settings,
)
else:
choices = cdap.run_cdap(
Expand All @@ -215,6 +221,7 @@ def cdap_simulate(
chunk_size=state.settings.chunk_size,
trace_hh_id=trace_hh_id,
trace_label=trace_label,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/free_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def free_parking(
trace_label=trace_label,
trace_choice_name="free_parking_at_work",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

free_parking_alt = model_settings.FREE_PARKING_ALT
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def joint_tour_composition(
trace_label=trace_label,
trace_choice_name="composition",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# convert indexes to alternative names
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def joint_tour_frequency(
trace_label=trace_label,
trace_choice_name="joint_tour_frequency",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# convert indexes to alternative names
Expand Down
3 changes: 2 additions & 1 deletion activitysim/abm/models/joint_tour_frequency_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
logger = logging.getLogger(__name__)


class JointTourFrequencyCompositionSettings(LogitComponentSettings):
class JointTourFrequencyCompositionSettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `joint_tour_frequency_composition` component.
"""
Expand Down Expand Up @@ -140,6 +140,7 @@ def joint_tour_frequency_composition(
trace_choice_name=trace_label,
estimator=estimator,
explicit_chunk_size=0,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def joint_tour_participation(
trace_choice_name="participation",
custom_chooser=participants_chooser,
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# choice is boolean (participate or not)
Expand Down
3 changes: 1 addition & 2 deletions activitysim/abm/models/joint_tour_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def joint_tour_scheduling(
estimator = estimation.manager.begin_estimation(state, "joint_tour_scheduling")

model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC)
sharrow_skip = model_settings.sharrow_skip
coefficients_df = state.filesystem.read_model_coefficients(model_settings)
model_spec = simulate.eval_coefficients(
state, model_spec, coefficients_df, estimator
Expand All @@ -128,7 +127,7 @@ def joint_tour_scheduling(
estimator=estimator,
chunk_size=state.settings.chunk_size,
trace_label=trace_label,
sharrow_skip=sharrow_skip,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
6 changes: 6 additions & 0 deletions activitysim/abm/models/location_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def _location_sample(
chunk_tag=chunk_tag,
trace_label=trace_label,
zone_layer=zone_layer,
compute_settings=model_settings.compute_settings.subcomponent_settings(
"sample"
),
)

return choices
Expand Down Expand Up @@ -696,6 +699,9 @@ def run_location_simulate(
trace_choice_name=model_settings.DEST_CHOICE_COLUMN_NAME,
estimator=estimator,
skip_choice=skip_choice,
compute_settings=model_settings.compute_settings.subcomponent_settings(
"simulate"
),
)

if not want_logsums:
Expand Down
3 changes: 2 additions & 1 deletion activitysim/abm/models/mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def add_null_results(state, trace_label, mandatory_tour_frequency_settings):
state.add_table("persons", persons)


class MandatoryTourFrequencySettings(LogitComponentSettings):
class MandatoryTourFrequencySettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `mandatory_tour_frequency` component.
"""
Expand Down Expand Up @@ -134,6 +134,7 @@ def mandatory_tour_frequency(
trace_label=trace_label,
trace_choice_name="mandatory_tour_frequency",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# convert indexes to alternative names
Expand Down
5 changes: 3 additions & 2 deletions activitysim/abm/models/non_mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from activitysim.abm.models.util import annotate
from activitysim.abm.models.util.overlap import (
person_max_window,
person_available_periods,
person_max_window,
)
from activitysim.abm.models.util.school_escort_tours_trips import (
recompute_tour_count_statistics,
Expand Down Expand Up @@ -161,7 +161,7 @@ class NonMandatoryTourSpecSegment(PydanticReadable):
COEFFICIENTS: Path


class NonMandatoryTourFrequencySettings(LogitComponentSettings):
class NonMandatoryTourFrequencySettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `non_mandatory_tour_frequency` component.
"""
Expand Down Expand Up @@ -321,6 +321,7 @@ def non_mandatory_tour_frequency(
trace_choice_name="non_mandatory_tour_frequency",
estimator=estimator,
explicit_chunk_size=model_settings.explicit_chunk,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
47 changes: 18 additions & 29 deletions activitysim/abm/models/school_escorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import logging
from typing import Any
from typing import Any, Literal

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -367,7 +367,7 @@ def create_school_escorting_bundles_table(choosers, tours, stage):
return bundles


class SchoolEscortSettings(BaseLogitComponentSettings):
class SchoolEscortSettings(BaseLogitComponentSettings, extra="forbid"):
"""
Settings for the `telecommute_frequency` component.
"""
Expand All @@ -391,21 +391,6 @@ class SchoolEscortSettings(BaseLogitComponentSettings):
GENDER_WEIGHT: float = 10.0
AGE_WEIGHT: float = 1.0

sharrow_skip: bool | dict[str, bool] = False
"""Setting to skip sharrow.
Sharrow can be skipped (or not) for all school escorting stages by giving
simply true or false. Alternatively, it can be skipped only for particular
stages by giving a mapping of stage name to skipping. For example:
```yaml
sharrow_skip:
OUTBOUND: true
INBOUND: false
OUTBOUND_COND: true
```
"""

SIMULATE_CHOOSER_COLUMNS: list[str] | None = None

SPEC: None = None
Expand All @@ -428,6 +413,13 @@ class SchoolEscortSettings(BaseLogitComponentSettings):
explicit_chunk: int = 0
"""If > 0, use this chunk size instead of adaptive chunking."""

LOGIT_TYPE: Literal["MNL"] = "MNL"
"""Logit model mathematical form.
* "MNL"
Multinomial logit model.
"""


@workflow.step
def school_escorting(
Expand Down Expand Up @@ -508,19 +500,15 @@ def school_escorting(
state, model_spec_raw, coefficients_df, estimator
)

# allow for skipping sharrow entirely in this model with `sharrow_skip: true`
# allow for skipping sharrow entirely in this model with `compute_settings.sharrow_skip: true`
# or skipping stages selectively with a mapping of the stages to skip
sharrow_skip = model_settings.sharrow_skip
stage_sharrow_skip = False # default is false unless set below
if sharrow_skip:
if isinstance(sharrow_skip, dict):
stage_sharrow_skip = sharrow_skip.get(stage.upper(), False)
else:
stage_sharrow_skip = True
if stage_sharrow_skip:
locals_dict["_sharrow_skip"] = True
else:
locals_dict.pop("_sharrow_skip", None)
stage_compute_settings = model_settings.compute_settings.subcomponent_settings(
stage.upper()
)
# if stage_sharrow_skip:
# locals_dict["_sharrow_skip"] = True
# else:
# locals_dict.pop("_sharrow_skip", None)

# reduce memory by limiting columns if selected columns are supplied
chooser_columns = model_settings.SIMULATE_CHOOSER_COLUMNS
Expand Down Expand Up @@ -584,6 +572,7 @@ def school_escorting(
trace_choice_name="school_escorting_" + stage,
estimator=estimator,
explicit_chunk_size=model_settings.explicit_chunk,
compute_settings=stage_compute_settings,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/stop_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def stop_frequency(
trace_label=tracing.extend_trace_label(trace_label, segment_name),
trace_choice_name="stops",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

# convert indexes to alternative names
Expand Down
3 changes: 2 additions & 1 deletion activitysim/abm/models/telecommute_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
logger = logging.getLogger("activitysim")


class TelecommuteFrequencySettings(LogitComponentSettings):
class TelecommuteFrequencySettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `telecommute_frequency` component.
"""
Expand Down Expand Up @@ -99,6 +99,7 @@ def telecommute_frequency(
trace_label=trace_label,
trace_choice_name="telecommute_frequency",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
Expand Down
3 changes: 2 additions & 1 deletion activitysim/abm/models/transit_pass_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
logger = logging.getLogger("activitysim")


class TransitPassOwnershipSettings(LogitComponentSettings):
class TransitPassOwnershipSettings(LogitComponentSettings, extra="forbid"):
"""
Settings for the `transit_pass_ownership` component.
"""
Expand Down Expand Up @@ -93,6 +93,7 @@ def transit_pass_ownership(
trace_label=trace_label,
trace_choice_name="transit_pass_ownership",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/transit_pass_subsidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def transit_pass_subsidy(
trace_label=trace_label,
trace_choice_name="transit_pass_subsidy",
estimator=estimator,
compute_settings=model_settings.compute_settings,
)

if estimator:
Expand Down
Loading

0 comments on commit 751ee44

Please sign in to comment.