From c04fe90fbd8efadb85ef5f3ab5fcc5241b665827 Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Tue, 25 Aug 2020 11:00:06 -0700 Subject: [PATCH 1/8] Error check for COEFFICIENT files --- activitysim/core/simulate.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/activitysim/core/simulate.py b/activitysim/core/simulate.py index 9e8af50df0..cb8fc64943 100644 --- a/activitysim/core/simulate.py +++ b/activitysim/core/simulate.py @@ -140,7 +140,11 @@ def read_model_coefficients(model_settings=None, file_name=None): file_name = model_settings['COEFFICIENTS'] file_path = config.config_file_path(file_name) - coefficients = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + try: + coefficients = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + except ValueError: + logger.exception("Coefficient File Invalid: %s" % str(file_path)) + raise return coefficients @@ -188,7 +192,11 @@ def read_model_coefficient_template(model_settings): coeffs_file_name = model_settings['COEFFICIENT_TEMPLATE'] file_path = config.config_file_path(coeffs_file_name) - template = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + try: + template = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + except ValueError: + logger.exception("Coefficient Template File Invalid: %s" % str(file_path)) + raise # by convention, an empty cell in the template indicates that # the coefficient name should be propogated to across all segments From 4c3162657e8d32fbb658eafd700276e69a6c5132 Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Tue, 25 Aug 2020 17:34:00 -0700 Subject: [PATCH 2/8] Allow coefficient files to have formulas --- activitysim/core/simulate.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/activitysim/core/simulate.py b/activitysim/core/simulate.py index cb8fc64943..9e908db0c9 100644 --- a/activitysim/core/simulate.py +++ b/activitysim/core/simulate.py @@ -10,6 +10,7 @@ import numpy as np import pandas as pd +from .assign import evaluate_constants from .skim import SkimDictWrapper, SkimStackWrapper from . import logit from . import tracing @@ -141,12 +142,18 @@ def read_model_coefficients(model_settings=None, file_name=None): file_path = config.config_file_path(file_name) try: - coefficients = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + coefficients_df = pd.read_csv(file_path, comment='#', index_col='coefficient_name') + assert {'constrain', 'value'}.issubset(coefficients_df.columns) except ValueError: logger.exception("Coefficient File Invalid: %s" % str(file_path)) raise - return coefficients + constants = model_settings['CONSTANTS'] if 'CONSTANTS' in model_settings else None + + coeffs = evaluate_constants(coefficients_df['value'], constants) + + return pd.concat([pd.DataFrame(coeffs.values(), index=coeffs.keys(), columns=['value']), + coefficients_df['constrain']], axis=1) def spec_for_segment(model_settings, spec_id, segment_name, estimator): @@ -167,7 +174,7 @@ def spec_for_segment(model_settings, spec_id, segment_name, estimator): """ spec = read_model_spec(file_name=model_settings[spec_id]) - coefficients = read_model_coefficients(model_settings) + coefficients = read_model_coefficients(model_settings, None) if len(spec.columns) > 1: # if spec is segmented @@ -192,11 +199,7 @@ def read_model_coefficient_template(model_settings): coeffs_file_name = model_settings['COEFFICIENT_TEMPLATE'] file_path = config.config_file_path(coeffs_file_name) - try: - template = pd.read_csv(file_path, comment='#', index_col='coefficient_name') - except ValueError: - logger.exception("Coefficient Template File Invalid: %s" % str(file_path)) - raise + template = pd.read_csv(file_path, comment='#', index_col='coefficient_name') # by convention, an empty cell in the template indicates that # the coefficient name should be propogated to across all segments From 09e142bf32d410b4cec3325e692ba3cd9947137c Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Tue, 25 Aug 2020 18:11:27 -0700 Subject: [PATCH 3/8] Remove duplicate coefficient. --- .../configs/non_mandatory_tour_destination_coeffs.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activitysim/examples/example_mtc/configs/non_mandatory_tour_destination_coeffs.csv b/activitysim/examples/example_mtc/configs/non_mandatory_tour_destination_coeffs.csv index bf02b811f8..4096d6e3ae 100644 --- a/activitysim/examples/example_mtc/configs/non_mandatory_tour_destination_coeffs.csv +++ b/activitysim/examples/example_mtc/configs/non_mandatory_tour_destination_coeffs.csv @@ -3,7 +3,7 @@ coef_mode_logsum,0.6755,F coef_escort_dist_0_2,-0.1499,F coef_eatout_dist_0_2,-0.5609,F coef_eatout_social_0_2,-0.5609,F -coef_eatout_dist_0_2,-0.7841,F +#coef_eatout_dist_0_2,-0.7841,F coef_othdiscr_dist_0_2,-0.1677,F coef_escort_dist_2_5,-0.8671,F coef_shopping_dist_2_5,-0.5655,F From 325b73942aa531e323b95d5bf208b665c9c5e8f2 Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Tue, 25 Aug 2020 18:12:19 -0700 Subject: [PATCH 4/8] Only look for model constants in model_settings --- activitysim/core/simulate.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/activitysim/core/simulate.py b/activitysim/core/simulate.py index 9e908db0c9..5126144c9b 100644 --- a/activitysim/core/simulate.py +++ b/activitysim/core/simulate.py @@ -141,6 +141,7 @@ def read_model_coefficients(model_settings=None, file_name=None): file_name = model_settings['COEFFICIENTS'] file_path = config.config_file_path(file_name) + print(file_path) try: coefficients_df = pd.read_csv(file_path, comment='#', index_col='coefficient_name') assert {'constrain', 'value'}.issubset(coefficients_df.columns) @@ -148,7 +149,10 @@ def read_model_coefficients(model_settings=None, file_name=None): logger.exception("Coefficient File Invalid: %s" % str(file_path)) raise - constants = model_settings['CONSTANTS'] if 'CONSTANTS' in model_settings else None + if model_settings is not None and 'CONSTANTS' in model_settings: + constants = model_settings['CONSTANTS'] + else: + constants = None coeffs = evaluate_constants(coefficients_df['value'], constants) From 37a8dc58c0978448ecb1f82957da839454ca4cce Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Tue, 25 Aug 2020 18:15:46 -0700 Subject: [PATCH 5/8] Clean-up whitespace --- activitysim/core/simulate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activitysim/core/simulate.py b/activitysim/core/simulate.py index 5126144c9b..d7650b87a3 100644 --- a/activitysim/core/simulate.py +++ b/activitysim/core/simulate.py @@ -150,7 +150,7 @@ def read_model_coefficients(model_settings=None, file_name=None): raise if model_settings is not None and 'CONSTANTS' in model_settings: - constants = model_settings['CONSTANTS'] + constants = model_settings['CONSTANTS'] else: constants = None From 53d03f84fec0f72039a0c834a6177307362c02d7 Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Thu, 27 Aug 2020 11:51:12 -0700 Subject: [PATCH 6/8] move coefficient processing up --- activitysim/abm/models/util/vectorize_tour_scheduling.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/activitysim/abm/models/util/vectorize_tour_scheduling.py b/activitysim/abm/models/util/vectorize_tour_scheduling.py index bd0eee021e..f71e45b246 100644 --- a/activitysim/abm/models/util/vectorize_tour_scheduling.py +++ b/activitysim/abm/models/util/vectorize_tour_scheduling.py @@ -76,6 +76,10 @@ def _compute_logsums(alt_tdd, tours_merged, tour_purpose, model_settings, trace_ locals_dict.update(constants) locals_dict.update(skims) + # constrained coefficients can appear in expressions + coefficients = simulate.get_segment_coefficients(logsum_settings, tour_purpose) + locals_dict.update(coefficients) + # - run preprocessor to annotate choosers # allow specification of alternate preprocessor for nontour choosers preprocessor = model_settings.get('LOGSUM_PREPROCESSOR', 'preprocessor') @@ -92,17 +96,12 @@ def _compute_logsums(alt_tdd, tours_merged, tour_purpose, model_settings, trace_ trace_label=trace_label) # - compute logsums - - coefficients = simulate.get_segment_coefficients(logsum_settings, tour_purpose) logsum_spec = simulate.read_model_spec(file_name=logsum_settings['SPEC']) logsum_spec = simulate.eval_coefficients(logsum_spec, coefficients, estimator=None) nest_spec = config.get_logit_model_settings(logsum_settings) nest_spec = simulate.eval_nest_coefficients(nest_spec, coefficients) - # constrained coefficients can appear in expressions - locals_dict.update(coefficients) - logsums = simulate.simple_simulate_logsums( choosers, logsum_spec, From b8e9ffba350544fae0d92e31b426b8d898adbd3c Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Thu, 27 Aug 2020 14:30:36 -0700 Subject: [PATCH 7/8] Update joint_tour_destination.py --- activitysim/abm/models/joint_tour_destination.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activitysim/abm/models/joint_tour_destination.py b/activitysim/abm/models/joint_tour_destination.py index e10fa2c346..8e7c940dba 100644 --- a/activitysim/abm/models/joint_tour_destination.py +++ b/activitysim/abm/models/joint_tour_destination.py @@ -326,8 +326,8 @@ def joint_tour_destination( person that's making the tour) """ - trace_label = 'non_mandatory_tour_destination' - model_settings_file_name = 'non_mandatory_tour_destination.yaml' + trace_label = 'joint_tour_destination' + model_settings_file_name = 'joint_tour_destination.yaml' model_settings = config.read_model_settings(model_settings_file_name) logsum_column_name = model_settings.get('DEST_CHOICE_LOGSUM_COLUMN_NAME') From e8170f6b159d9464885a562da057c5eb4fd6f3b6 Mon Sep 17 00:00:00 2001 From: Clint Daniels Date: Thu, 27 Aug 2020 15:33:30 -0700 Subject: [PATCH 8/8] Fix reference to joint tour destination.yaml --- .../examples/example_mtc/configs/joint_tour_destination.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/activitysim/examples/example_mtc/configs/joint_tour_destination.yaml b/activitysim/examples/example_mtc/configs/joint_tour_destination.yaml index 36d67bc306..356b0a620f 100644 --- a/activitysim/examples/example_mtc/configs/joint_tour_destination.yaml +++ b/activitysim/examples/example_mtc/configs/joint_tour_destination.yaml @@ -10,9 +10,11 @@ SEGMENTS: - eatout - social - SAMPLE_SIZE: 30 +# we can't use use household income_segment as this will also be set for non-workers +CHOOSER_SEGMENT_COLUMN_NAME: tour_type + SIMULATE_CHOOSER_COLUMNS: - tour_type - TAZ