Skip to content

Commit

Permalink
consolidate joint tour destination
Browse files Browse the repository at this point in the history
  • Loading branch information
toliwaga committed Jan 11, 2019
1 parent fa8da76 commit 61b8d97
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 60 deletions.
125 changes: 77 additions & 48 deletions activitysim/abm/models/joint_tour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
])


@inject.step()
def joint_tour_destination_sample(
tours,
joint_tours,
households_merged,
skim_dict,
land_use, size_terms,
Expand Down Expand Up @@ -79,9 +78,8 @@ def joint_tour_destination_sample(
Parameters
----------
tours: pipeline table
households_merged : pipeline table
injected merge table created on the fly
joint_tours: pandas.DataFrame
households_merged : pandas.DataFrame
skim_dict
joint_tour_destination_sample_spec
land_use
Expand All @@ -92,24 +90,15 @@ def joint_tour_destination_sample(
Returns
-------
none
choices : pandas.DataFrame
destination_sample df
"""

trace_label = 'joint_tour_destination_sample'
model_settings = config.read_model_settings('joint_tour_destination.yaml')
model_spec = simulate.read_model_spec(file_name='non_mandatory_tour_destination_sample.csv')

joint_tours = tours.to_frame()
joint_tours = joint_tours[joint_tours.tour_category == 'joint']

# - if no joint tours
if joint_tours.shape[0] == 0:
tracing.no_results(trace_label)
return

households_merged = households_merged.to_frame()

# same size terms as non_mandatory
size_terms = tour_destination_size_terms(land_use, size_terms, 'non_mandatory')

Expand Down Expand Up @@ -186,18 +175,18 @@ def joint_tour_destination_sample(
# - NARROW
choices['tour_type_id'] = choices['tour_type_id'].astype(np.uint8)

inject.add_table('joint_tour_destination_sample', choices)

if trace_hh_id:
tracing.trace_df(choices,
label="joint_tour_destination_sample",
transpose=True)

return choices


@inject.step()
def joint_tour_destination_logsums(
tours,
joint_tours,
persons_merged,
destination_sample,
skim_dict, skim_stack,
chunk_size, trace_hh_id):

Expand All @@ -211,21 +200,9 @@ def joint_tour_destination_logsums(

trace_label = 'joint_tour_destination_logsums'

# use inject.get_table as this won't exist if there are no joint_tours
destination_sample = inject.get_table('joint_tour_destination_sample', default=None)
if destination_sample is None:
tracing.no_results(trace_label)
return

destination_sample = destination_sample.to_frame()

model_settings = config.read_model_settings('joint_tour_destination.yaml')
logsum_settings = config.read_model_settings(model_settings['LOGSUM_SETTINGS'])

joint_tours = tours.to_frame()
joint_tours = joint_tours[joint_tours.tour_category == 'joint']

persons_merged = persons_merged.to_frame()
joint_tours_merged = pd.merge(joint_tours, persons_merged,
left_on='person_id', right_index=True, how='left')

Expand Down Expand Up @@ -269,17 +246,18 @@ def joint_tour_destination_logsums(
logsums = pd.concat(logsums_list)

destination_sample['mode_choice_logsum'] = logsums
pipeline.replace_table("joint_tour_destination_sample", destination_sample)

if trace_hh_id:
tracing.trace_df(destination_sample,
label="joint_tour_destination_logsums")
tracing.trace_df(destination_sample, label="joint_tour_destination_logsums")

return destination_sample


@inject.step()
def joint_tour_destination_simulate(
tours,
joint_tours,
households_merged,
destination_sample,
skim_dict,
land_use, size_terms,
chunk_size, trace_hh_id):
Expand All @@ -290,26 +268,14 @@ def joint_tour_destination_simulate(

trace_label = 'joint_tour_destination_simulate'

# use inject.get_table as this won't exist if there are no joint_tours
destination_sample = inject.get_table('joint_tour_destination_sample', default=None)
if destination_sample is None:
tracing.no_results(trace_label)
return

model_settings = config.read_model_settings('joint_tour_destination.yaml')

# - tour types are subset of non_mandatory tour types and use same expressions
model_spec = simulate.read_model_spec(file_name='non_mandatory_tour_destination.csv')

destination_sample = destination_sample.to_frame()
tours = tours.to_frame()
joint_tours = tours[tours.tour_category == 'joint']

# interaction_sample_simulate insists choosers appear in same order as alts
joint_tours = joint_tours.sort_index()

households_merged = households_merged.to_frame()

destination_size_terms = tour_destination_size_terms(land_use, size_terms, 'non_mandatory')

alt_dest_col_name = model_settings["ALT_DEST_COL_NAME"]
Expand Down Expand Up @@ -380,6 +346,69 @@ def joint_tour_destination_simulate(

choices = pd.concat(choices_list)

return choices


@inject.step()
def joint_tour_destination(
tours,
persons_merged,
households_merged,
skim_dict,
skim_stack,
land_use, size_terms,
chunk_size, trace_hh_id):
"""
Run the three-part destination choice algorithm to choose a destination for each joint tour
Parameters
----------
tours : injected table
households_merged : injected table
skim_dict : skim.SkimDict
land_use : injected table
size_terms : injected table
chunk_size : int
trace_hh_id : int or None
Returns
-------
adds/assigns choice column 'destination' for joint tours in tours table
"""

tours = tours.to_frame()
joint_tours = tours[tours.tour_category == 'joint']

persons_merged = persons_merged.to_frame()
households_merged = households_merged.to_frame()

# - if no joint tours
if joint_tours.shape[0] == 0:
tracing.no_results('joint_tour_destination')
return

destination_sample = joint_tour_destination_sample(
joint_tours,
households_merged,
skim_dict,
land_use, size_terms,
chunk_size, trace_hh_id)

destination_sample = joint_tour_destination_logsums(
joint_tours,
persons_merged,
destination_sample,
skim_dict, skim_stack,
chunk_size, trace_hh_id)

choices = joint_tour_destination_simulate(
joint_tours,
households_merged,
destination_sample,
skim_dict,
land_use, size_terms,
chunk_size, trace_hh_id)

# add column as we want joint_tours table for tracing.
joint_tours['destination'] = choices

Expand Down
4 changes: 1 addition & 3 deletions activitysim/abm/test/configs/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ models:
- joint_tour_frequency
- joint_tour_composition
- joint_tour_participation
- joint_tour_destination_sample
- joint_tour_destination_logsums
- joint_tour_destination_simulate
- joint_tour_destination
- joint_tour_scheduling
- non_mandatory_tour_frequency
- non_mandatory_tour_destination
Expand Down
4 changes: 1 addition & 3 deletions docs/abmexample.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,7 @@ The ``models`` setting contains the specification of the data pipeline model ste
- joint_tour_frequency
- joint_tour_composition
- joint_tour_participation
- joint_tour_destination_sample
- joint_tour_destination_logsums
- joint_tour_destination_simulate
- joint_tour_destination
- joint_tour_scheduling
- non_mandatory_tour_frequency
- non_mandatory_tour_destination
Expand Down
4 changes: 1 addition & 3 deletions example/configs/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ models:
- joint_tour_frequency
- joint_tour_composition
- joint_tour_participation
- joint_tour_destination_sample
- joint_tour_destination_logsums
- joint_tour_destination_simulate
- joint_tour_destination
- joint_tour_scheduling
- non_mandatory_tour_frequency
- non_mandatory_tour_destination
Expand Down
4 changes: 1 addition & 3 deletions example_mp/configs/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ models:
- joint_tour_frequency
- joint_tour_composition
- joint_tour_participation
- _joint_tour_destination_sample
- _joint_tour_destination_logsums
- joint_tour_destination_simulate
- joint_tour_destination
- joint_tour_scheduling
- non_mandatory_tour_frequency
- non_mandatory_tour_destination
Expand Down

0 comments on commit 61b8d97

Please sign in to comment.