@@ -838,6 +838,24 @@ def run_location_choice(
838838 )
839839 state .tracing .trace_df (choices_df , estimation_trace_label )
840840
841+ if want_logsums & (not skip_choice ):
842+ # grabbing index, could be person_id or proto_person_id
843+ index_name = choices_df .index .name
844+ # merging mode choice logsum of chosen alternative to choices
845+ choices_df = (
846+ pd .merge (
847+ choices_df .reset_index (),
848+ location_sample_df .reset_index ()[
849+ [index_name , model_settings .ALT_DEST_COL_NAME , ALT_LOGSUM ]
850+ ],
851+ how = "left" ,
852+ left_on = [index_name , "choice" ],
853+ right_on = [index_name , model_settings .ALT_DEST_COL_NAME ],
854+ )
855+ .drop (columns = model_settings .ALT_DEST_COL_NAME )
856+ .set_index (index_name )
857+ )
858+
841859 choices_list .append (choices_df )
842860
843861 if want_sample_table :
@@ -855,7 +873,7 @@ def run_location_choice(
855873 else :
856874 # this will only happen with small samples (e.g. singleton) with no (e.g.) school segs
857875 logger .warning ("%s no choices" , trace_label )
858- choices_df = pd .DataFrame (columns = ["choice" , "logsum" ])
876+ choices_df = pd .DataFrame (columns = ["choice" , "logsum" , ALT_LOGSUM ])
859877
860878 if len (sample_list ) > 0 :
861879 save_sample_df = pd .concat (sample_list )
@@ -898,7 +916,8 @@ def iterate_location_choice(
898916 Returns
899917 -------
900918 adds choice column model_settings['DEST_CHOICE_COLUMN_NAME']
901- adds logsum column model_settings['DEST_CHOICE_LOGSUM_COLUMN_NAME']- if provided
919+ adds destination choice logsum column model_settings['DEST_CHOICE_LOGSUM_COLUMN_NAME']- if provided
920+ adds mode choice logsum to selected destination column model_settings['MODE_CHOICE_LOGSUM_COLUMN_NAME']- if provided
902921 adds annotations to persons table
903922 """
904923
@@ -908,7 +927,11 @@ def iterate_location_choice(
908927 chooser_filter_column = model_settings .CHOOSER_FILTER_COLUMN_NAME
909928
910929 dest_choice_column_name = model_settings .DEST_CHOICE_COLUMN_NAME
911- logsum_column_name = model_settings .DEST_CHOICE_LOGSUM_COLUMN_NAME
930+ dc_logsum_column_name = model_settings .DEST_CHOICE_LOGSUM_COLUMN_NAME
931+ mc_logsum_column_name = model_settings .MODE_CHOICE_LOGSUM_COLUMN_NAME
932+ want_logsums = (dc_logsum_column_name is not None ) | (
933+ mc_logsum_column_name is not None
934+ )
912935
913936 sample_table_name = model_settings .DEST_CHOICE_SAMPLE_TABLE_NAME
914937 want_sample_table = (
@@ -959,7 +982,7 @@ def iterate_location_choice(
959982 persons_merged_df_ ,
960983 network_los ,
961984 shadow_price_calculator = spc ,
962- want_logsums = logsum_column_name is not None ,
985+ want_logsums = want_logsums ,
963986 want_sample_table = want_sample_table ,
964987 estimator = estimator ,
965988 model_settings = model_settings ,
@@ -1034,10 +1057,15 @@ def iterate_location_choice(
10341057 )
10351058
10361059 # add the dest_choice_logsum column to persons dataframe
1037- if logsum_column_name :
1038- persons_df [logsum_column_name ] = (
1060+ if dc_logsum_column_name :
1061+ persons_df [dc_logsum_column_name ] = (
10391062 choices_df ["logsum" ].reindex (persons_df .index ).astype ("float" )
10401063 )
1064+ # add the mode choice logsum column to persons dataframe
1065+ if mc_logsum_column_name :
1066+ persons_df [mc_logsum_column_name ] = (
1067+ choices_df [ALT_LOGSUM ].reindex (persons_df .index ).astype ("float" )
1068+ )
10411069
10421070 if save_sample_df is not None :
10431071 # might be None for tiny samples even if sample_table_name was specified
@@ -1077,9 +1105,13 @@ def iterate_location_choice(
10771105 if state .settings .trace_hh_id :
10781106 state .tracing .trace_df (households_df , label = trace_label , warn_if_empty = True )
10791107
1080- if logsum_column_name :
1108+ if dc_logsum_column_name :
1109+ tracing .print_summary (
1110+ dc_logsum_column_name , choices_df ["logsum" ], value_counts = True
1111+ )
1112+ if mc_logsum_column_name :
10811113 tracing .print_summary (
1082- logsum_column_name , choices_df ["logsum" ], value_counts = True
1114+ mc_logsum_column_name , choices_df [ALT_LOGSUM ], value_counts = True
10831115 )
10841116
10851117 return persons_df
0 commit comments