Skip to content

Commit

Permalink
Merge pull request #775 from SANDAG/BayDAG_cont9_mc_logsum
Browse files Browse the repository at this point in the history
BayDAG Contribution #9: Mode Choice Logsum Extraction
  • Loading branch information
jpn-- authored Mar 26, 2024
2 parents 94c4db8 + d1f4db8 commit bf074a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
48 changes: 40 additions & 8 deletions activitysim/abm/models/location_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,24 @@ def run_location_choice(
)
state.tracing.trace_df(choices_df, estimation_trace_label)

if want_logsums & (not skip_choice):
# grabbing index, could be person_id or proto_person_id
index_name = choices_df.index.name
# merging mode choice logsum of chosen alternative to choices
choices_df = (
pd.merge(
choices_df.reset_index(),
location_sample_df.reset_index()[
[index_name, model_settings.ALT_DEST_COL_NAME, ALT_LOGSUM]
],
how="left",
left_on=[index_name, "choice"],
right_on=[index_name, model_settings.ALT_DEST_COL_NAME],
)
.drop(columns=model_settings.ALT_DEST_COL_NAME)
.set_index(index_name)
)

choices_list.append(choices_df)

if want_sample_table:
Expand All @@ -855,7 +873,7 @@ def run_location_choice(
else:
# this will only happen with small samples (e.g. singleton) with no (e.g.) school segs
logger.warning("%s no choices", trace_label)
choices_df = pd.DataFrame(columns=["choice", "logsum"])
choices_df = pd.DataFrame(columns=["choice", "logsum", ALT_LOGSUM])

if len(sample_list) > 0:
save_sample_df = pd.concat(sample_list)
Expand Down Expand Up @@ -898,7 +916,8 @@ def iterate_location_choice(
Returns
-------
adds choice column model_settings['DEST_CHOICE_COLUMN_NAME']
adds logsum column model_settings['DEST_CHOICE_LOGSUM_COLUMN_NAME']- if provided
adds destination choice logsum column model_settings['DEST_CHOICE_LOGSUM_COLUMN_NAME']- if provided
adds mode choice logsum to selected destination column model_settings['MODE_CHOICE_LOGSUM_COLUMN_NAME']- if provided
adds annotations to persons table
"""

Expand All @@ -908,7 +927,11 @@ def iterate_location_choice(
chooser_filter_column = model_settings.CHOOSER_FILTER_COLUMN_NAME

dest_choice_column_name = model_settings.DEST_CHOICE_COLUMN_NAME
logsum_column_name = model_settings.DEST_CHOICE_LOGSUM_COLUMN_NAME
dc_logsum_column_name = model_settings.DEST_CHOICE_LOGSUM_COLUMN_NAME
mc_logsum_column_name = model_settings.MODE_CHOICE_LOGSUM_COLUMN_NAME
want_logsums = (dc_logsum_column_name is not None) | (
mc_logsum_column_name is not None
)

sample_table_name = model_settings.DEST_CHOICE_SAMPLE_TABLE_NAME
want_sample_table = (
Expand Down Expand Up @@ -959,7 +982,7 @@ def iterate_location_choice(
persons_merged_df_,
network_los,
shadow_price_calculator=spc,
want_logsums=logsum_column_name is not None,
want_logsums=want_logsums,
want_sample_table=want_sample_table,
estimator=estimator,
model_settings=model_settings,
Expand Down Expand Up @@ -1034,10 +1057,15 @@ def iterate_location_choice(
)

# add the dest_choice_logsum column to persons dataframe
if logsum_column_name:
persons_df[logsum_column_name] = (
if dc_logsum_column_name:
persons_df[dc_logsum_column_name] = (
choices_df["logsum"].reindex(persons_df.index).astype("float")
)
# add the mode choice logsum column to persons dataframe
if mc_logsum_column_name:
persons_df[mc_logsum_column_name] = (
choices_df[ALT_LOGSUM].reindex(persons_df.index).astype("float")
)

if save_sample_df is not None:
# might be None for tiny samples even if sample_table_name was specified
Expand Down Expand Up @@ -1077,9 +1105,13 @@ def iterate_location_choice(
if state.settings.trace_hh_id:
state.tracing.trace_df(households_df, label=trace_label, warn_if_empty=True)

if logsum_column_name:
if dc_logsum_column_name:
tracing.print_summary(
dc_logsum_column_name, choices_df["logsum"], value_counts=True
)
if mc_logsum_column_name:
tracing.print_summary(
logsum_column_name, choices_df["logsum"], value_counts=True
mc_logsum_column_name, choices_df[ALT_LOGSUM], value_counts=True
)

return persons_df
Expand Down
3 changes: 3 additions & 0 deletions activitysim/core/configuration/logit.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class TourLocationComponentSettings(LocationComponentSettings, extra="forbid"):
CHOOSER_FILTER_COLUMN_NAME: str | None = None
DEST_CHOICE_COLUMN_NAME: str | None = None
DEST_CHOICE_LOGSUM_COLUMN_NAME: str | None = None
"""Column name for logsum calculated across all sampled destinations."""
MODE_CHOICE_LOGSUM_COLUMN_NAME: str | None = None
"""Column name for logsum calculated across all sampled modes to selected destination."""
DEST_CHOICE_SAMPLE_TABLE_NAME: str | None = None
CHOOSER_TABLE_NAME: str | None = None
CHOOSER_SEGMENT_COLUMN_NAME: str | None = None
Expand Down

0 comments on commit bf074a5

Please sign in to comment.