|
| 1 | +# ActivitySim |
| 2 | +# See full license in LICENSE.txt. |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +from activitysim.core import ( |
| 11 | + config, |
| 12 | + estimation, |
| 13 | + expressions, |
| 14 | + simulate, |
| 15 | + tracing, |
| 16 | + workflow, |
| 17 | +) |
| 18 | +from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable |
| 19 | +from activitysim.core.configuration.logit import LogitComponentSettings |
| 20 | + |
| 21 | +logger = logging.getLogger("activitysim") |
| 22 | + |
| 23 | + |
| 24 | +class TelecommuteStatusSettings(LogitComponentSettings, extra="forbid"): |
| 25 | + """ |
| 26 | + Settings for the `telecommute_status` component. |
| 27 | + """ |
| 28 | + |
| 29 | + TELECOMMUTE_ALT: int |
| 30 | + """Value that specifies if the worker is telecommuting on the simulation day.""" |
| 31 | + |
| 32 | + CHOOSER_FILTER_COLUMN_NAME: str = "is_worker" |
| 33 | + """Column name in the dataframe to represent worker.""" |
| 34 | + |
| 35 | + |
| 36 | +@workflow.step |
| 37 | +def telecommute_status( |
| 38 | + state: workflow.State, |
| 39 | + persons_merged: pd.DataFrame, |
| 40 | + persons: pd.DataFrame, |
| 41 | + model_settings: TelecommuteStatusSettings | None = None, |
| 42 | + model_settings_file_name: str = "telecommute_status.yaml", |
| 43 | + trace_label: str = "telecommute_status", |
| 44 | +) -> None: |
| 45 | + """ |
| 46 | + This model predicts whether a person (worker) telecommutes on the simulation day. |
| 47 | + The output from this model is TRUE (if telecommutes) or FALSE (if does not telecommute). |
| 48 | + """ |
| 49 | + if model_settings is None: |
| 50 | + model_settings = TelecommuteStatusSettings.read_settings_file( |
| 51 | + state.filesystem, |
| 52 | + model_settings_file_name, |
| 53 | + ) |
| 54 | + |
| 55 | + choosers = persons_merged |
| 56 | + chooser_filter_column_name = model_settings.CHOOSER_FILTER_COLUMN_NAME |
| 57 | + choosers = choosers[(choosers[chooser_filter_column_name])] |
| 58 | + logger.info("Running %s with %d persons", trace_label, len(choosers)) |
| 59 | + |
| 60 | + estimator = estimation.manager.begin_estimation(state, "telecommute_status") |
| 61 | + |
| 62 | + constants = config.get_model_constants(model_settings) |
| 63 | + |
| 64 | + # - preprocessor |
| 65 | + expressions.annotate_preprocessors( |
| 66 | + state, |
| 67 | + df=choosers, |
| 68 | + locals_dict=constants, |
| 69 | + skims=None, |
| 70 | + model_settings=model_settings, |
| 71 | + trace_label=trace_label, |
| 72 | + ) |
| 73 | + |
| 74 | + model_spec = state.filesystem.read_model_spec(file_name=model_settings.SPEC) |
| 75 | + coefficients_df = state.filesystem.read_model_coefficients(model_settings) |
| 76 | + model_spec = simulate.eval_coefficients( |
| 77 | + state, model_spec, coefficients_df, estimator |
| 78 | + ) |
| 79 | + nest_spec = config.get_logit_model_settings(model_settings) |
| 80 | + |
| 81 | + if estimator: |
| 82 | + estimator.write_model_settings(model_settings, model_settings_file_name) |
| 83 | + estimator.write_spec(model_settings) |
| 84 | + estimator.write_coefficients(coefficients_df, model_settings) |
| 85 | + estimator.write_choosers(choosers) |
| 86 | + |
| 87 | + choices = simulate.simple_simulate( |
| 88 | + state, |
| 89 | + choosers=choosers, |
| 90 | + spec=model_spec, |
| 91 | + nest_spec=nest_spec, |
| 92 | + locals_d=constants, |
| 93 | + trace_label=trace_label, |
| 94 | + trace_choice_name="is_telecommuting", |
| 95 | + estimator=estimator, |
| 96 | + compute_settings=model_settings.compute_settings, |
| 97 | + ) |
| 98 | + |
| 99 | + telecommute_alt = model_settings.TELECOMMUTE_ALT |
| 100 | + choices = choices == telecommute_alt |
| 101 | + |
| 102 | + if estimator: |
| 103 | + estimator.write_choices(choices) |
| 104 | + choices = estimator.get_survey_values(choices, "persons", "is_telecommuting") |
| 105 | + estimator.write_override_choices(choices) |
| 106 | + estimator.end_estimation() |
| 107 | + |
| 108 | + persons["is_telecommuting"] = choices.reindex(persons.index).fillna(0).astype(bool) |
| 109 | + |
| 110 | + state.add_table("persons", persons) |
| 111 | + |
| 112 | + tracing.print_summary( |
| 113 | + "telecommute_status", persons.is_telecommuting, 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