<s> import os <EOL> import yaml <EOL> import orca <EOL> import pandas as pd <EOL> import yaml <EOL> from activitysim import activitysim as asim <EOL> from activitysim import skim as askim <EOL> from . util . mode import _mode_choice_spec <EOL> """<STR_LIT>""" <EOL> @ orca . injectable ( ) <EOL> def tour_mode_choice_settings ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return yaml . load ( f ) <EOL> @ orca . injectable ( ) <EOL> def tour_mode_choice_spec_df ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return asim . read_model_spec ( f ) <EOL> @ orca . injectable ( ) <EOL> def tour_mode_choice_coeffs ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return pd . read_csv ( f , index_col = '<STR_LIT>' ) <EOL> @ orca . injectable ( ) <EOL> def tour_mode_choice_spec ( tour_mode_choice_spec_df , <EOL> tour_mode_choice_coeffs , <EOL> tour_mode_choice_settings ) : <EOL> return _mode_choice_spec ( tour_mode_choice_spec_df , <EOL> tour_mode_choice_coeffs , <EOL> tour_mode_choice_settings ) <EOL> @ orca . injectable ( ) <EOL> def trip_mode_choice_settings ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return yaml . load ( f ) <EOL> @ orca . injectable ( ) <EOL> def trip_mode_choice_spec_df ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return asim . read_model_spec ( f ) <EOL> @ orca . injectable ( ) <EOL> def trip_mode_choice_coeffs ( configs_dir ) : <EOL> with open ( os . path . join ( configs_dir , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) as f : <EOL> return pd . read_csv ( f , index_col = '<STR_LIT>' ) <EOL> @ orca . injectable ( ) <EOL> def trip_mode_choice_spec ( trip_mode_choice_spec_df , <EOL> trip_mode_choice_coeffs , <EOL> trip_mode_choice_settings ) : <EOL> return _mode_choice_spec ( trip_mode_choice_spec_df , <EOL> trip_mode_choice_coeffs , <EOL> trip_mode_choice_settings ) <EOL> def _mode_choice_simulate ( tours , <EOL> skims , <EOL> orig_key , <EOL> dest_key , <EOL> spec , <EOL> additional_constants , <EOL> omx = None , <EOL> cache_skim_key_values = None ) : <EOL> """<STR_LIT>""" <EOL> in_skims = askim . Skims3D ( skims = skims . set_keys ( orig_key , dest_key ) , <EOL> skim_key = "<STR_LIT>" , <EOL> offset = - <NUM_LIT:1> ) <EOL> out_skims = askim . Skims3D ( skims = skims . set_keys ( dest_key , orig_key ) , <EOL> skim_key = "<STR_LIT>" , <EOL> offset = - <NUM_LIT:1> ) <EOL> skims . set_keys ( orig_key , dest_key ) <EOL> if omx is not None : <EOL> in_skims . set_omx ( omx , cache_skim_key_values = cache_skim_key_values ) <EOL> out_skims . set_omx ( omx , cache_skim_key_values = cache_skim_key_values ) <EOL> locals_d = { <EOL> "<STR_LIT>" : in_skims , <EOL> "<STR_LIT>" : out_skims , <EOL> "<STR_LIT>" : skims <EOL> } <EOL> locals_d . update ( additional_constants ) <EOL> choices , _ = asim . simple_simulate ( tours , <EOL> spec , <EOL> skims = [ in_skims , out_skims , skims ] , <EOL> locals_d = locals_d ) <EOL> alts = spec . columns <EOL> choices = choices . map ( dict ( zip ( range ( len ( alts ) ) , alts ) ) ) <EOL> return choices <EOL> def get_segment_and_unstack ( spec , segment ) : <EOL> """<STR_LIT>""" <EOL> return spec [ segment ] . unstack ( ) . reset_index ( level = "<STR_LIT>" , drop = True ) . fillna ( <NUM_LIT:0> ) <EOL> @ orca . step ( ) <EOL> def tour_mode_choice_simulate ( tours_merged , <EOL> tour_mode_choice_spec , <EOL> tour_mode_choice_settings , <EOL> skims , omx_file , cache_skim_key_values ) : <EOL> tours = tours_merged . to_frame ( ) <EOL> choices_list = [ ] <EOL> print "<STR_LIT>" , tours . tour_type . value_counts ( ) <EOL> for tour_type , segment in tours . groupby ( '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % tour_type <EOL> tour_type_tours = tours [ tours . tour_type == tour_type ] <EOL> orig_key = '<STR_LIT>' <EOL> dest_key = '<STR_LIT>' <EOL> choices = _mode_choice_simulate ( <EOL> tour_type_tours , <EOL> skims , <EOL> orig_key = orig_key , <EOL> dest_key = dest_key , <EOL> spec = get_segment_and_unstack ( tour_mode_choice_spec , tour_type ) , <EOL> additional_constants = tour_mode_choice_settings [ '<STR_LIT>' ] , <EOL> omx = omx_file , <EOL> cache_skim_key_values = cache_skim_key_values ) <EOL> choices_list . append ( choices ) <EOL> print asim . usage ( '<STR_LIT>' % tour_type ) <EOL> choices = pd . concat ( choices_list ) <EOL> print "<STR_LIT>" , choices . value_counts ( ) <EOL> orca . add_column ( "<STR_LIT>" , "<STR_LIT>" , choices ) <EOL> print asim . usage ( '<STR_LIT>' ) <EOL> @ orca . step ( ) <EOL> def trip_mode_choice_simulate ( tours_merged , <EOL> trip_mode_choice_spec , <EOL> trip_mode_choice_settings , <EOL> skims , omx_file , cache_skim_key_values ) : <EOL> trips = tours_merged . to_frame ( ) <EOL> choices_list = [ ] <EOL> print "<STR_LIT>" , trips . tour_type . value_counts ( ) <EOL> for tour_type , segment in trips . groupby ( '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % tour_type <EOL> tour_type_tours = trips [ trips . tour_type == tour_type ] <EOL> orig_key = '<STR_LIT>' <EOL> dest_key = '<STR_LIT>' <EOL> choices = _mode_choice_simulate ( <EOL> tour_type_tours , <EOL> skims , <EOL> orig_key = orig_key , <EOL> dest_key = dest_key , <EOL> spec = get_segment_and_unstack ( trip_mode_choice_spec , tour_type ) , <EOL> additional_constants = trip_mode_choice_settings [ '<STR_LIT>' ] , <EOL> omx = omx_file , <EOL> cache_skim_key_values = cache_skim_key_values ) <EOL> choices_list . append ( choices ) <EOL> print asim . usage ( '<STR_LIT>' % tour_type ) <EOL> choices = pd . concat ( choices_list ) <EOL> print "<STR_LIT>" , choices . value_counts ( ) <EOL> orca . add_column ( "<STR_LIT>" , "<STR_LIT>" , choices ) <EOL> print asim . usage ( '<STR_LIT>' ) </s>
