Skip to content

Commit

Permalink
trip destination
Browse files Browse the repository at this point in the history
  • Loading branch information
toliwaga committed May 20, 2018
1 parent 772944e commit 8e35357
Show file tree
Hide file tree
Showing 49 changed files with 1,516 additions and 825 deletions.
1 change: 1 addition & 0 deletions activitysim/abm/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import stop_frequency
import trip_purpose
import trip_destination

# parameterized models
import annotate_table
31 changes: 11 additions & 20 deletions activitysim/abm/models/atwork_subtour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@ def atwork_subtour_destination_sample_spec(configs_dir):
return simulate.read_model_spec(configs_dir, 'atwork_subtour_destination_sample.csv')


@inject.injectable()
def atwork_subtour_destination_settings(configs_dir):
return config.read_model_settings(configs_dir, 'atwork_subtour_destination.yaml')


@inject.step()
def atwork_subtour_destination_sample(tours,
persons_merged,
atwork_subtour_destination_sample_spec,
atwork_subtour_destination_settings,
skim_dict,
land_use, size_terms,
configs_dir, chunk_size, trace_hh_id):
Expand All @@ -55,7 +49,7 @@ def atwork_subtour_destination_sample(tours,
persons_merged = persons_merged.to_frame()

tours = tours.to_frame()
tours = tours[tours.tour_category == 'subtour']
tours = tours[tours.tour_category == 'atwork']

# merge persons into tours
choosers = pd.merge(tours, persons_merged, left_on='person_id', right_index=True)
Expand All @@ -68,9 +62,9 @@ def atwork_subtour_destination_sample(tours,
constants = config.get_model_constants(model_settings)

sample_size = model_settings["SAMPLE_SIZE"]
alt_col_name = model_settings["ALT_DEST_COL_NAME"]
alt_dest_col_name = model_settings["ALT_DEST_COL_NAME"]

logger.info("Running atwork_subtour_location_sample with %d persons" % len(choosers))
logger.info("Running atwork_subtour_location_sample with %d tours" % len(choosers))

# create wrapper with keys for this lookup - in this case there is a workplace_taz
# in the choosers and a TAZ in the alternatives which get merged during interaction
Expand All @@ -87,7 +81,7 @@ def atwork_subtour_destination_sample(tours,
choosers,
alternatives,
sample_size=sample_size,
alt_col_name=alt_col_name,
alt_col_name=alt_dest_col_name,
spec=atwork_subtour_destination_sample_spec,
skims=skims,
locals_d=locals_d,
Expand All @@ -96,9 +90,6 @@ def atwork_subtour_destination_sample(tours,

choices['person_id'] = choosers.person_id

# add subtour orig taz so logsums can use in skims lookups
choices['workplace_taz'] = choosers.workplace_taz

inject.add_table('atwork_subtour_destination_sample', choices)


Expand Down Expand Up @@ -135,14 +126,14 @@ def atwork_subtour_destination_logsums(persons_merged,
logsum_settings = config.read_model_settings(configs_dir, 'logsum.yaml')

logsum_spec = logsum.get_logsum_spec(
logsum_settings, selector='subtour', segment='workbased',
logsum_settings, selector='atwork_subtour', segment='atwork',
configs_dir=configs_dir, want_tracing=trace_hh_id)

persons_merged = persons_merged.to_frame()
atwork_subtour_destination_sample = atwork_subtour_destination_sample.to_frame()

# FIXME - MEMORY HACK - only include columns actually used in spec
persons_merged = logsum.filter_chooser_columns(persons_merged, logsum_settings)
persons_merged = logsum.filter_chooser_columns(persons_merged, logsum_settings, model_settings)

# merge persons into tours
choosers = pd.merge(atwork_subtour_destination_sample,
Expand Down Expand Up @@ -192,7 +183,7 @@ def atwork_subtour_destination_simulate(tours,
model_settings = config.read_model_settings(configs_dir, 'atwork_subtour_destination.yaml')

tours = tours.to_frame()
subtours = tours[tours.tour_category == 'subtour']
subtours = tours[tours.tour_category == 'atwork']
# merge persons into tours
choosers = pd.merge(subtours,
persons_merged.to_frame(),
Expand All @@ -201,7 +192,7 @@ def atwork_subtour_destination_simulate(tours,
chooser_columns = model_settings['SIMULATE_CHOOSER_COLUMNS']
choosers = choosers[chooser_columns]

alt_col_name = model_settings["ALT_DEST_COL_NAME"]
alt_dest_col_name = model_settings["ALT_DEST_COL_NAME"]
chooser_col_name = 'workplace_taz'

# alternatives are pre-sampled and annotated with logsums and pick_count
Expand All @@ -212,7 +203,7 @@ def atwork_subtour_destination_simulate(tours,

alternatives = \
pd.merge(atwork_subtour_destination_sample, destination_size_terms,
left_on=alt_col_name, right_index=True, how="left")
left_on=alt_dest_col_name, right_index=True, how="left")

tracing.dump_df(DUMP, alternatives, trace_label, 'alternatives')

Expand All @@ -223,7 +214,7 @@ def atwork_subtour_destination_simulate(tours,
# create wrapper with keys for this lookup - in this case there is a TAZ in the choosers
# and a TAZ in the alternatives which get merged during interaction
# the skims will be available under the name "skims" for any @ expressions
skims = skim_dict.wrap(chooser_col_name, alt_col_name)
skims = skim_dict.wrap(chooser_col_name, alt_dest_col_name)

locals_d = {
'skims': skims,
Expand All @@ -237,7 +228,7 @@ def atwork_subtour_destination_simulate(tours,
choosers,
alternatives,
spec=atwork_subtour_destination_spec,
choice_column=alt_col_name,
choice_column=alt_dest_col_name,
skims=skims,
locals_d=locals_d,
chunk_size=chunk_size,
Expand Down
6 changes: 3 additions & 3 deletions activitysim/abm/models/atwork_subtour_mode_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def atwork_subtour_mode_choice(
tours = tours.to_frame()

subtours_merged = tours_merged.to_frame()
subtours_merged = subtours_merged[subtours_merged.tour_category == 'subtour']
subtours_merged = subtours_merged[subtours_merged.tour_category == 'atwork']

nest_spec = config.get_logit_model_settings(tour_mode_choice_settings)
constants = config.get_model_constants(tour_mode_choice_settings)
Expand Down Expand Up @@ -81,7 +81,7 @@ def atwork_subtour_mode_choice(
subtours_merged, locals_dict, skims,
tour_mode_choice_settings, trace_label)

spec = get_segment_and_unstack(tour_mode_choice_spec, segment='workbased')
spec = get_segment_and_unstack(tour_mode_choice_spec, segment='atwork')

if trace_hh_id:
tracing.trace_df(spec, tracing.extend_trace_label(trace_label, 'spec'),
Expand All @@ -103,7 +103,7 @@ def atwork_subtour_mode_choice(
pipeline.replace_table("tours", tours)

if trace_hh_id:
tracing.trace_df(tours[tours.tour_category == 'subtour'],
tracing.trace_df(tours[tours.tour_category == 'atwork'],
label=tracing.extend_trace_label(trace_label, 'mode'),
slicer='tour_id',
index_label='tour_id',
Expand Down
12 changes: 6 additions & 6 deletions activitysim/abm/models/atwork_subtour_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


@inject.injectable()
def tour_scheduling_subtour_spec(configs_dir):
return asim.read_model_spec(configs_dir, 'tour_scheduling_subtour.csv')
def tour_scheduling_atwork_spec(configs_dir):
return asim.read_model_spec(configs_dir, 'tour_scheduling_atwork.csv')


@inject.injectable()
Expand All @@ -40,7 +40,7 @@ def atwork_subtour_scheduling(
tours,
persons_merged,
tdd_alts,
tour_scheduling_subtour_spec,
tour_scheduling_atwork_spec,
atwork_subtour_scheduling_settings,
chunk_size,
trace_hh_id):
Expand All @@ -54,7 +54,7 @@ def atwork_subtour_scheduling(
persons_merged = persons_merged.to_frame()

tours = tours.to_frame()
subtours = tours[tours.tour_category == 'subtour']
subtours = tours[tours.tour_category == 'atwork']

logger.info("Running %s with %d tours" % (trace_label, len(subtours)))

Expand All @@ -75,7 +75,7 @@ def atwork_subtour_scheduling(
parent_tours,
subtours,
persons_merged,
tdd_alts, tour_scheduling_subtour_spec,
tdd_alts, tour_scheduling_atwork_spec,
constants=constants,
chunk_size=chunk_size,
trace_label=trace_label)
Expand All @@ -88,7 +88,7 @@ def atwork_subtour_scheduling(
trace_label, 'tour_map')

if trace_hh_id:
tracing.trace_df(tours[tours.tour_category == 'subtour'],
tracing.trace_df(tours[tours.tour_category == 'atwork'],
label="atwork_subtour_scheduling",
slicer='person_id',
index_label='tour_id',
Expand Down
7 changes: 4 additions & 3 deletions activitysim/abm/models/joint_tour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,14 @@ def joint_tour_destination_logsums(
joint_tours_merged = pd.merge(joint_tours, persons_merged,
left_on='person_id', right_index=True, how='left')

# FIXME - MEMORY HACK - only include columns actually used in spec
joint_tours_merged = logsum.filter_chooser_columns(joint_tours_merged, logsum_settings)
# - only include columns actually used in spec
joint_tours_merged = \
logsum.filter_chooser_columns(joint_tours_merged, logsum_settings, model_settings)

destination_sample = joint_tour_destination_sample.to_frame()

omnibus_logsum_spec = \
logsum.get_omnibus_logsum_spec(logsum_settings, selector='joint',
logsum.get_omnibus_logsum_spec(logsum_settings, selector='joint_tour',
configs_dir=configs_dir, want_tracing=trace_hh_id)

logsums_list = []
Expand Down
5 changes: 3 additions & 2 deletions activitysim/abm/models/joint_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ def joint_tour_frequency(
# - we need a person_id in order to generate the tour index (and for register_traceable_table)
# - but we don't know the tour participants yet
# - so we arbitrarily choose the first person in the household
# - to be point person for the purpose of generating an index
# - to be point person for the purpose of generating an index and setting origin
temp_point_persons = persons.loc[persons.PNUM == 1]
temp_point_persons['person_id'] = temp_point_persons.index
temp_point_persons = temp_point_persons.set_index('household_id').person_id
temp_point_persons = temp_point_persons.set_index('household_id')
temp_point_persons = temp_point_persons[['person_id', 'home_taz']]

joint_tours = \
process_joint_tours(choices, joint_tour_frequency_alternatives, temp_point_persons)
Expand Down
7 changes: 4 additions & 3 deletions activitysim/abm/models/school_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ def school_location_logsums(
logger.info("Running school_location_logsums with %s rows" % location_sample.shape[0])

persons_merged = persons_merged.to_frame()
# FIXME - MEMORY HACK - only include columns actually used in spec
persons_merged = logsum.filter_chooser_columns(persons_merged, logsum_settings)
# - only include columns actually used in spec
persons_merged = \
logsum.filter_chooser_columns(persons_merged, logsum_settings, school_location_settings)

omnibus_logsum_spec = \
logsum.get_omnibus_logsum_spec(logsum_settings, selector='nontour',
Expand All @@ -191,7 +192,7 @@ def school_location_logsums(
logsums_list = []
for school_type, school_type_id in SCHOOL_TYPE_ID.iteritems():

segment = 'university' if school_type == 'university' else 'school'
segment = 'univ' if school_type == 'university' else 'school'
logsum_spec = get_segment_and_unstack(omnibus_logsum_spec, segment)

choosers = location_sample[location_sample['school_type'] == school_type_id]
Expand Down
12 changes: 7 additions & 5 deletions activitysim/abm/models/stop_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ def process_trips(tours, stop_frequency_alts):
trips['household_id'] = reindex(tours.household_id, trips.tour_id)

trips['primary_purpose'] = reindex(tours.primary_purpose, trips.tour_id)
trips['subtour'] = reindex(tours.tour_category, trips.tour_id) == 'subtour'
trips['atwork'] = reindex(tours.tour_category, trips.tour_id) == 'atwork'

# reorder columns and drop 'direction'
trips = trips[['person_id', 'household_id', 'tour_id',
'primary_purpose', 'subtour',
'primary_purpose', 'atwork',
'trip_num', 'outbound', 'trip_count']]

trips['first'] = (trips.trip_num == 1)
Expand All @@ -126,6 +126,9 @@ def process_trips(tours, stop_frequency_alts):
canonical_trip_num = (~trips.outbound * MAX_TRIPS_PER_LEG) + trips.trip_num
trips['trip_id'] = trips.tour_id * (2 * MAX_TRIPS_PER_LEG) + canonical_trip_num

# id of next trip in inbound or outbound leg
trips['next_trip_id'] = np.where(trips['last'], 0, trips.trip_id + 1)

trips.set_index('trip_id', inplace=True, verify_integrity=True)

return trips
Expand Down Expand Up @@ -202,13 +205,12 @@ def stop_frequency(
# convert indexes to alternative names
choices = pd.Series(spec.columns[choices.values], index=choices.index)

tracing.print_summary('stop_frequency %s choices' % segment_type,
choices, value_counts=True)

choices_list.append(choices)

choices = pd.concat(choices_list)

tracing.print_summary('stop_frequency', choices, value_counts=True)

# add stop_frequency choices to tours table
assign_in_place(tours, choices.to_frame('stop_frequency'))

Expand Down
2 changes: 1 addition & 1 deletion activitysim/abm/models/tour_mode_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def tour_mode_choice_simulate(tours, persons_merged,
trace_label = 'tour_mode_choice'

primary_tours = tours.to_frame()
primary_tours = primary_tours[primary_tours.tour_category != 'subtour']
primary_tours = primary_tours[primary_tours.tour_category != 'atwork']

persons_merged = persons_merged.to_frame()

Expand Down

0 comments on commit 8e35357

Please sign in to comment.