Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Formulas in Coefficients File #332

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions activitysim/abm/models/joint_tour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 4 additions & 5 deletions activitysim/abm/models/util/vectorize_tour_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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,
Expand Down
21 changes: 18 additions & 3 deletions activitysim/core/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -140,9 +141,23 @@ 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')
print(file_path)
try:
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

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)

return coefficients
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):
Expand All @@ -163,7 +178,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down