From 25ae93c8c6a2a03d942d6813a611cf76b2d269ac Mon Sep 17 00:00:00 2001 From: David Hensle Date: Thu, 26 Jan 2023 14:13:22 -0800 Subject: [PATCH 1/2] work zone to -1 if wfh, removed unneeded extensions --- activitysim/abm/models/work_from_home.py | 17 +++- .../prototype_mwcog/extensions/__init__.py | 4 - .../extensions/telecommute_frequency.py | 99 ------------------- .../extensions/transit_pass_ownership.py | 84 ---------------- .../extensions/transit_pass_subsidy.py | 84 ---------------- .../extensions/work_from_home.py | 98 ------------------ .../examples/prototype_mwcog/simulation.py | 2 - 7 files changed, 16 insertions(+), 372 deletions(-) delete mode 100644 activitysim/examples/prototype_mwcog/extensions/__init__.py delete mode 100644 activitysim/examples/prototype_mwcog/extensions/telecommute_frequency.py delete mode 100644 activitysim/examples/prototype_mwcog/extensions/transit_pass_ownership.py delete mode 100644 activitysim/examples/prototype_mwcog/extensions/transit_pass_subsidy.py delete mode 100644 activitysim/examples/prototype_mwcog/extensions/work_from_home.py diff --git a/activitysim/abm/models/work_from_home.py b/activitysim/abm/models/work_from_home.py index d5f3c5cd2..97a80d301 100755 --- a/activitysim/abm/models/work_from_home.py +++ b/activitysim/abm/models/work_from_home.py @@ -15,6 +15,8 @@ def work_from_home(persons_merged, persons, chunk_size, trace_hh_id): """ This model predicts whether a person (worker) works from home. The output from this model is TRUE (if works from home) or FALSE (works away from home). + The workplace location choice is overridden for workers who work from home + and set to -1. """ trace_label = "work_from_home" @@ -22,7 +24,9 @@ def work_from_home(persons_merged, persons, chunk_size, trace_hh_id): choosers = persons_merged.to_frame() model_settings = config.read_model_settings(model_settings_file_name) - chooser_filter_column_name = model_settings.get("CHOOSER_FILTER_COLUMN_NAME") + chooser_filter_column_name = model_settings.get( + "CHOOSER_FILTER_COLUMN_NAME", "is_worker" + ) choosers = choosers[choosers[chooser_filter_column_name]] logger.info("Running %s with %d persons", trace_label, len(choosers)) @@ -154,6 +158,17 @@ def work_from_home(persons_merged, persons, chunk_size, trace_hh_id): persons[chooser_filter_column_name] & ~persons["work_from_home"] ) + # setting workplace_zone_id to -1 if person works from home + # this will exclude them from the telecommute frequency model choosers + # See https://github.com/ActivitySim/activitysim/issues/627 + dest_choice_column_name = model_settings.get( + "DEST_CHOICE_COLUMN_NAME", "workplace_zone_id" + ) + if dest_choice_column_name in persons.columns: + persons[dest_choice_column_name] = np.where( + persons.work_from_home == True, -1, persons[dest_choice_column_name] + ) + pipeline.replace_table("persons", persons) tracing.print_summary("work_from_home", persons.work_from_home, value_counts=True) diff --git a/activitysim/examples/prototype_mwcog/extensions/__init__.py b/activitysim/examples/prototype_mwcog/extensions/__init__.py deleted file mode 100644 index a5b766f31..000000000 --- a/activitysim/examples/prototype_mwcog/extensions/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from . import work_from_home -from . import telecommute_frequency -from . import transit_pass_ownership -from . import transit_pass_subsidy diff --git a/activitysim/examples/prototype_mwcog/extensions/telecommute_frequency.py b/activitysim/examples/prototype_mwcog/extensions/telecommute_frequency.py deleted file mode 100644 index a299d2832..000000000 --- a/activitysim/examples/prototype_mwcog/extensions/telecommute_frequency.py +++ /dev/null @@ -1,99 +0,0 @@ -# ActivitySim -# See full license in LICENSE.txt. -import logging - -import pandas as pd - -from activitysim.abm.models.util import estimation -from activitysim.core import config, expressions, inject, pipeline, simulate, tracing - -logger = logging.getLogger(__name__) - - -@inject.custom_step() -def telecommute_frequency(persons_merged, persons, chunk_size, trace_hh_id): - """ - This model predicts the frequency of telecommute for a person (worker) who - does not works from home. The alternatives of this model are 'No Telecommute', - '1 day per week', '2 to 3 days per week' and '4 days per week'. This model - reflects the choices of people who prefer a combination of working from home and - office during a week. - - The main interface to the telecommute frequency model is the telecommute_frequency() function. - This function is registered as an orca step in the example Pipeline. - """ - - trace_label = "telecommute_frequency" - model_settings_file_name = "telecommute_frequency.yaml" - - choosers = persons_merged.to_frame() - choosers = choosers[choosers.workplace_zone_id > -1] - - logger.info("Running %s with %d persons", trace_label, len(choosers)) - - model_settings = config.read_model_settings(model_settings_file_name) - estimator = estimation.manager.begin_estimation("telecommute_frequency") - - constants = config.get_model_constants(model_settings) - - # - preprocessor - preprocessor_settings = model_settings.get("preprocessor", None) - if preprocessor_settings: - - locals_d = {} - if constants is not None: - locals_d.update(constants) - - expressions.assign_columns( - df=choosers, - model_settings=preprocessor_settings, - locals_dict=locals_d, - trace_label=trace_label, - ) - - model_spec = simulate.read_model_spec(file_name=model_settings["SPEC"]) - coefficients_df = simulate.read_model_coefficients(model_settings) - model_spec = simulate.eval_coefficients(model_spec, coefficients_df, estimator) - - nest_spec = config.get_logit_model_settings(model_settings) - - if estimator: - estimator.write_model_settings(model_settings, model_settings_file_name) - estimator.write_spec(model_settings) - estimator.write_coefficients(coefficients_df) - estimator.write_choosers(choosers) - - choices = simulate.simple_simulate( - choosers=choosers, - spec=model_spec, - nest_spec=nest_spec, - locals_d=constants, - chunk_size=chunk_size, - trace_label=trace_label, - trace_choice_name="telecommute_frequency", - estimator=estimator, - ) - - choices = pd.Series(model_spec.columns[choices.values], index=choices.index) - - if estimator: - estimator.write_choices(choices) - choices = estimator.get_survey_values( - choices, "persons", "telecommute_frequency" - ) - estimator.write_override_choices(choices) - estimator.end_estimation() - - persons = persons.to_frame() - persons["telecommute_frequency"] = ( - choices.reindex(persons.index).fillna("").astype(str) - ) - - pipeline.replace_table("persons", persons) - - tracing.print_summary( - "telecommute_frequency", persons.telecommute_frequency, value_counts=True - ) - - if trace_hh_id: - tracing.trace_df(persons, label=trace_label, warn_if_empty=True) diff --git a/activitysim/examples/prototype_mwcog/extensions/transit_pass_ownership.py b/activitysim/examples/prototype_mwcog/extensions/transit_pass_ownership.py deleted file mode 100644 index e2d020443..000000000 --- a/activitysim/examples/prototype_mwcog/extensions/transit_pass_ownership.py +++ /dev/null @@ -1,84 +0,0 @@ -# ActivitySim -# See full license in LICENSE.txt. -import logging - -from activitysim.abm.models.util import estimation -from activitysim.core import config, expressions, inject, pipeline, simulate, tracing - -logger = logging.getLogger("activitysim") - - -@inject.custom_step() -def transit_pass_ownership(persons_merged, persons, chunk_size, trace_hh_id): - """ - Transit pass ownership model. - """ - - trace_label = "transit_pass_ownership" - model_settings_file_name = "transit_pass_ownership.yaml" - - choosers = persons_merged.to_frame() - logger.info("Running %s with %d persons", trace_label, len(choosers)) - - model_settings = config.read_model_settings(model_settings_file_name) - estimator = estimation.manager.begin_estimation("transit_pass_ownership") - - constants = config.get_model_constants(model_settings) - - # - preprocessor - preprocessor_settings = model_settings.get("preprocessor", None) - if preprocessor_settings: - - locals_d = {} - if constants is not None: - locals_d.update(constants) - - expressions.assign_columns( - df=choosers, - model_settings=preprocessor_settings, - locals_dict=locals_d, - trace_label=trace_label, - ) - - model_spec = simulate.read_model_spec(file_name=model_settings["SPEC"]) - coefficients_df = simulate.read_model_coefficients(model_settings) - model_spec = simulate.eval_coefficients(model_spec, coefficients_df, estimator) - - nest_spec = config.get_logit_model_settings(model_settings) - - if estimator: - estimator.write_model_settings(model_settings, model_settings_file_name) - estimator.write_spec(model_settings) - estimator.write_coefficients(coefficients_df) - estimator.write_choosers(choosers) - - choices = simulate.simple_simulate( - choosers=choosers, - spec=model_spec, - nest_spec=nest_spec, - locals_d=constants, - chunk_size=chunk_size, - trace_label=trace_label, - trace_choice_name="transit_pass_ownership", - estimator=estimator, - ) - - if estimator: - estimator.write_choices(choices) - choices = estimator.get_survey_values( - choices, "persons", "transit_pass_ownership" - ) - estimator.write_override_choices(choices) - estimator.end_estimation() - - persons = persons.to_frame() - persons["transit_pass_ownership"] = choices.reindex(persons.index) - - pipeline.replace_table("persons", persons) - - tracing.print_summary( - "transit_pass_ownership", persons.transit_pass_ownership, value_counts=True - ) - - if trace_hh_id: - tracing.trace_df(persons, label=trace_label, warn_if_empty=True) diff --git a/activitysim/examples/prototype_mwcog/extensions/transit_pass_subsidy.py b/activitysim/examples/prototype_mwcog/extensions/transit_pass_subsidy.py deleted file mode 100644 index 7a4408fe3..000000000 --- a/activitysim/examples/prototype_mwcog/extensions/transit_pass_subsidy.py +++ /dev/null @@ -1,84 +0,0 @@ -# ActivitySim -# See full license in LICENSE.txt. -import logging - -from activitysim.abm.models.util import estimation -from activitysim.core import config, expressions, inject, pipeline, simulate, tracing - -logger = logging.getLogger("activitysim") - - -@inject.custom_step() -def transit_pass_subsidy(persons_merged, persons, chunk_size, trace_hh_id): - """ - Transit pass subsidy model. - """ - - trace_label = "transit_pass_subsidy" - model_settings_file_name = "transit_pass_subsidy.yaml" - - choosers = persons_merged.to_frame() - logger.info("Running %s with %d persons", trace_label, len(choosers)) - - model_settings = config.read_model_settings(model_settings_file_name) - estimator = estimation.manager.begin_estimation("transit_pass_subsidy") - - constants = config.get_model_constants(model_settings) - - # - preprocessor - preprocessor_settings = model_settings.get("preprocessor", None) - if preprocessor_settings: - - locals_d = {} - if constants is not None: - locals_d.update(constants) - - expressions.assign_columns( - df=choosers, - model_settings=preprocessor_settings, - locals_dict=locals_d, - trace_label=trace_label, - ) - - model_spec = simulate.read_model_spec(file_name=model_settings["SPEC"]) - coefficients_df = simulate.read_model_coefficients(model_settings) - model_spec = simulate.eval_coefficients(model_spec, coefficients_df, estimator) - - nest_spec = config.get_logit_model_settings(model_settings) - - if estimator: - estimator.write_model_settings(model_settings, model_settings_file_name) - estimator.write_spec(model_settings) - estimator.write_coefficients(coefficients_df) - estimator.write_choosers(choosers) - - choices = simulate.simple_simulate( - choosers=choosers, - spec=model_spec, - nest_spec=nest_spec, - locals_d=constants, - chunk_size=chunk_size, - trace_label=trace_label, - trace_choice_name="transit_pass_subsidy", - estimator=estimator, - ) - - if estimator: - estimator.write_choices(choices) - choices = estimator.get_survey_values( - choices, "persons", "transit_pass_subsidy" - ) - estimator.write_override_choices(choices) - estimator.end_estimation() - - persons = persons.to_frame() - persons["transit_pass_subsidy"] = choices.reindex(persons.index) - - pipeline.replace_table("persons", persons) - - tracing.print_summary( - "transit_pass_subsidy", persons.transit_pass_subsidy, value_counts=True - ) - - if trace_hh_id: - tracing.trace_df(persons, label=trace_label, warn_if_empty=True) diff --git a/activitysim/examples/prototype_mwcog/extensions/work_from_home.py b/activitysim/examples/prototype_mwcog/extensions/work_from_home.py deleted file mode 100644 index 0cc032ea7..000000000 --- a/activitysim/examples/prototype_mwcog/extensions/work_from_home.py +++ /dev/null @@ -1,98 +0,0 @@ -# ActivitySim -# See full license in LICENSE.txt. -import logging - -import numpy as np - -from activitysim.abm.models.util import estimation -from activitysim.core import config, expressions, inject, pipeline, simulate, tracing - -logger = logging.getLogger(__name__) - - -@inject.custom_step() -def work_from_home(persons_merged, persons, chunk_size, trace_hh_id): - """ - This model predicts the whether a person (worker) works from home. The output - from this model is TRUE (if works from home) or FALSE (works away from home). - The workplace location choice is overridden for workers who work from home - and set to -1. - - The main interface to the work from home model is the work_from_home() function. - This function is registered as an orca step in the example Pipeline. - """ - - trace_label = "work_from_home" - model_settings_file_name = "work_from_home.yaml" - - choosers = persons_merged.to_frame() - choosers = choosers[choosers.workplace_zone_id > -1] - logger.info("Running %s with %d persons", trace_label, len(choosers)) - - model_settings = config.read_model_settings(model_settings_file_name) - estimator = estimation.manager.begin_estimation("work_from_home") - - constants = config.get_model_constants(model_settings) - - # - preprocessor - preprocessor_settings = model_settings.get("preprocessor", None) - if preprocessor_settings: - - locals_d = {} - if constants is not None: - locals_d.update(constants) - - expressions.assign_columns( - df=choosers, - model_settings=preprocessor_settings, - locals_dict=locals_d, - trace_label=trace_label, - ) - - model_spec = simulate.read_model_spec(file_name=model_settings["SPEC"]) - coefficients_df = simulate.read_model_coefficients(model_settings) - model_spec = simulate.eval_coefficients(model_spec, coefficients_df, estimator) - - nest_spec = config.get_logit_model_settings(model_settings) - - if estimator: - estimator.write_model_settings(model_settings, model_settings_file_name) - estimator.write_spec(model_settings) - estimator.write_coefficients(coefficients_df) - estimator.write_choosers(choosers) - - choices = simulate.simple_simulate( - choosers=choosers, - spec=model_spec, - nest_spec=nest_spec, - locals_d=constants, - chunk_size=chunk_size, - trace_label=trace_label, - trace_choice_name="work_from_home", - estimator=estimator, - ) - - work_from_home_alt = model_settings["WORK_FROM_HOME_ALT"] - choices = choices == work_from_home_alt - - dest_choice_column_name = model_settings["DEST_CHOICE_COLUMN_NAME"] - print(dest_choice_column_name) - - if estimator: - estimator.write_choices(choices) - choices = estimator.get_survey_values(choices, "persons", "work_from_home") - estimator.write_override_choices(choices) - estimator.end_estimation() - - persons = persons.to_frame() - persons["work_from_home"] = choices.reindex(persons.index).fillna(0).astype(bool) - persons[dest_choice_column_name] = np.where( - persons.work_from_home is True, -1, persons[dest_choice_column_name] - ) - - pipeline.replace_table("persons", persons) - - tracing.print_summary("work_from_home", persons.work_from_home, value_counts=True) - - if trace_hh_id: - tracing.trace_df(persons, label=trace_label, warn_if_empty=True) diff --git a/activitysim/examples/prototype_mwcog/simulation.py b/activitysim/examples/prototype_mwcog/simulation.py index b5cd37ea8..8313dd45e 100644 --- a/activitysim/examples/prototype_mwcog/simulation.py +++ b/activitysim/examples/prototype_mwcog/simulation.py @@ -4,8 +4,6 @@ import argparse import sys -import extensions # noqa: F401 - from activitysim.cli.run import add_run_args, run if __name__ == "__main__": From 9133c2e0f89113a1ef423cfcd23c25d717fd5860 Mon Sep 17 00:00:00 2001 From: Jeff Newman Date: Fri, 27 Jan 2023 12:06:48 -0600 Subject: [PATCH 2/2] ignore deprecation warning in tests not clear why this started being a problem suddenly in only the xborder-MP model --- activitysim/core/config.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/activitysim/core/config.py b/activitysim/core/config.py index bd115018d..7024d0512 100644 --- a/activitysim/core/config.py +++ b/activitysim/core/config.py @@ -733,6 +733,11 @@ def filter_warnings(): ".*object-dtype columns with all-bool values will not be included in reductions.*" ), ) + warnings.filterwarnings( + "ignore", + category=DeprecationWarning, + message=".*will attempt to set the values inplace instead of always setting a new array.*", + ) # beginning in sharrow version 2.5, a CacheMissWarning is emitted when a sharrow # flow cannot be loaded from cache and needs to be compiled. These are performance