Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joint tours #232

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af292dc
joint_tour_frequency.yaml with all uecs
toliwaga Feb 16, 2018
abbb726
student and employment category strings as ints
toliwaga Feb 16, 2018
64844da
ptype_cat strings replaced with ints
toliwaga Feb 16, 2018
79beb7e
remove some print_elapsed_time calls
toliwaga Feb 17, 2018
db33fcf
replace orca add_column with annotation files
toliwaga Feb 19, 2018
f2d182e
install sphinx numpydoc sphinx_rtd_theme int ravis rather than build_…
toliwaga Feb 19, 2018
3148370
dont track data_dict.txt
toliwaga Feb 19, 2018
179eb12
replace inject.column decorators with expression files
toliwaga Feb 20, 2018
c97c61c
pcodestyle
toliwaga Feb 20, 2018
27d6ef9
create joint tours table after joint_tour_frequency
toliwaga Feb 22, 2018
184e2c7
joint_party_composition
toliwaga Feb 25, 2018
ed59f7c
joint tour participation - regression not revised
toliwaga Mar 2, 2018
a2ee185
joint_tour_participation regression tests updated
toliwaga Mar 3, 2018
7780778
joint_tour_destination
toliwaga Mar 11, 2018
0e27e6a
fixed typo in docs
bstabler Mar 17, 2018
21b656c
joint_tour_scheduling
toliwaga Mar 24, 2018
179b998
docs tweak
toliwaga Mar 24, 2018
50742ac
Merge branch 'joint-tours' of https://github.com/ActivitySim/activity…
toliwaga Mar 24, 2018
2ce5b6b
joint tour tests and subtour mode bugfix
toliwaga Mar 29, 2018
ebf3797
mode_choice macro dummies replaced with annotations
toliwaga Apr 1, 2018
88d4f69
merge doc changes from master
toliwaga Apr 1, 2018
1610e2a
add custom_shooser hook so joint tour participation can re-choose par…
toliwaga Apr 2, 2018
913002e
update docs link
bstabler Apr 3, 2018
9fa3b42
fixed bad expressions in joint_tour_composition.csv and added better …
toliwaga Apr 3, 2018
6133a1b
tour_scheduling preprocess and expression file rename
toliwaga Apr 3, 2018
69cbcd8
set joint_tour_participation participants_chooser max iters default t…
toliwaga Apr 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ example/data/*
.idea
.ipynb_checkpoints
.coverage*
.pytest_cache


# Byte-compiled / optimized / DLL files
Expand Down
1 change: 0 additions & 1 deletion activitysim/abm/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from activitysim.core import pipeline
from activitysim.core import inject

# FIXME - really?
warnings.filterwarnings('ignore', category=pd.io.pytables.PerformanceWarning)
pd.options.mode.chained_assignment = None

Expand Down
10 changes: 9 additions & 1 deletion activitysim/abm/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
import auto_ownership
import mandatory_tour_frequency
import mandatory_scheduling
import joint_tour_frequency
import joint_tour_composition
import joint_tour_participation
import joint_tour_destination
import joint_tour_scheduling
import joint_tour_mode_choice
import non_mandatory_tour_frequency
import non_mandatory_destination
import non_mandatory_scheduling
import school_location
import workplace_location
import mode_choice
import tour_mode_choice
import cdap
import create_trips
import trip_mode_choice
import atwork_subtour_frequency
import atwork_subtour_destination
import atwork_subtour_scheduling
import atwork_subtour_mode_choice

import utility_steps

Expand Down
9 changes: 5 additions & 4 deletions activitysim/abm/models/annotate_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from activitysim.core import tracing
from activitysim.core import inject
from activitysim.core import pipeline
from activitysim.core import timetable as tt
from activitysim.core import assign
# from activitysim.core import timetable as tt
# from activitysim.core import assign
from activitysim.core import config

from .util import expressions
Expand All @@ -26,6 +26,8 @@ def annotate_table(configs_dir):
# model_settings name should have been provided as a step argument
model_name = inject.get_step_arg('model_name')

trace_label = 'annotate_table.%s' % model_name

model_settings = config.read_model_settings(configs_dir, '%s.yaml' % model_name)

df_name = model_settings['DF']
Expand All @@ -34,8 +36,7 @@ def annotate_table(configs_dir):
results = expressions.compute_columns(
df,
model_settings=model_settings,
configs_dir=configs_dir,
trace_label=None)
trace_label=trace_label)

assign_in_place(df, results)

Expand Down
101 changes: 40 additions & 61 deletions activitysim/abm/models/atwork_subtour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
from .util import expressions
from activitysim.core.util import assign_in_place

from .util.logsums import compute_logsums
from .util.logsums import mode_choice_logsums_spec
from .util import logsums as logsum

from .util.expressions import skim_time_period_label
from .util.tour_destination import tour_destination_size_terms

logger = logging.getLogger(__name__)
DUMP = False
Expand All @@ -44,13 +44,13 @@ def atwork_subtour_destination_settings(configs_dir):
def atwork_subtour_destination_sample(tours,
persons_merged,
atwork_subtour_destination_sample_spec,
atwork_subtour_destination_settings,
skim_dict,
destination_size_terms,
chunk_size,
trace_hh_id):
land_use, size_terms,
configs_dir, chunk_size, trace_hh_id):

trace_label = 'atwork_subtour_location_sample'
model_settings = inject.get_injectable('atwork_subtour_destination_settings')
model_settings = config.read_model_settings(configs_dir, 'atwork_subtour_destination.yaml')

persons_merged = persons_merged.to_frame()

Expand All @@ -59,32 +59,30 @@ def atwork_subtour_destination_sample(tours,

# merge persons into tours
choosers = pd.merge(tours, persons_merged, left_on='person_id', right_index=True)
# FIXME - MEMORY HACK - only include columns actually used in spec
chooser_columns = model_settings['SIMULATE_CHOOSER_COLUMNS']
choosers = choosers[chooser_columns]

alternatives = destination_size_terms.to_frame()
alternatives = tour_destination_size_terms(land_use, size_terms, 'atwork')

constants = config.get_model_constants(model_settings)

sample_size = model_settings["SAMPLE_SIZE"]
alt_col_name = model_settings["ALT_COL_NAME"]
chooser_col_name = 'workplace_taz'
alt_col_name = model_settings["ALT_DEST_COL_NAME"]

logger.info("Running atwork_subtour_location_sample with %d persons" % 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
# the skims will be available under the name "skims" for any @ expressions
skims = skim_dict.wrap(chooser_col_name, 'TAZ')
skims = skim_dict.wrap('workplace_taz', 'TAZ')

locals_d = {
'skims': skims
}
if constants is not None:
locals_d.update(constants)

# FIXME - MEMORY HACK - only include columns actually used in spec
chooser_columns = model_settings['SIMULATE_CHOOSER_COLUMNS']
choosers = choosers[chooser_columns]

choices = interaction_sample(
choosers,
alternatives,
Expand All @@ -97,19 +95,18 @@ def atwork_subtour_destination_sample(tours,
trace_label=trace_label)

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)


@inject.step()
def atwork_subtour_destination_logsums(persons_merged,
land_use,
skim_dict, skim_stack,
atwork_subtour_destination_sample,
configs_dir,
chunk_size,
trace_hh_id):
configs_dir, chunk_size, trace_hh_id):
"""
add logsum column to existing workplace_location_sample able

Expand All @@ -133,22 +130,19 @@ def atwork_subtour_destination_logsums(persons_merged,
"""

trace_label = 'atwork_subtour_destination_logsums'
model_settings = inject.get_injectable('atwork_subtour_destination_settings')
model_settings = config.read_model_settings(configs_dir, 'atwork_subtour_destination.yaml')

logsums_spec = mode_choice_logsums_spec(configs_dir, 'work')

alt_col_name = model_settings["ALT_COL_NAME"]
chooser_col_name = 'workplace_taz'
logsum_settings = config.read_model_settings(configs_dir, 'logsum.yaml')

# FIXME - just using settings from tour_mode_choice
logsum_settings = config.read_model_settings(configs_dir, 'tour_mode_choice.yaml')
logsum_spec = logsum.get_logsum_spec(
logsum_settings, selector='subtour', segment='workbased',
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
chooser_columns = model_settings['LOGSUM_CHOOSER_COLUMNS']
persons_merged = persons_merged[chooser_columns]
persons_merged = logsum.filter_chooser_columns(persons_merged, logsum_settings)

# merge persons into tours
choosers = pd.merge(atwork_subtour_destination_sample,
Expand All @@ -157,26 +151,22 @@ def atwork_subtour_destination_logsums(persons_merged,
right_index=True,
how="left")

choosers['in_period'] = skim_time_period_label(model_settings['IN_PERIOD'])
choosers['out_period'] = skim_time_period_label(model_settings['OUT_PERIOD'])

# FIXME - should do this in expression file?
choosers['dest_topology'] = reindex(land_use.TOPOLOGY, choosers[alt_col_name])
choosers['dest_density_index'] = reindex(land_use.density_index, choosers[alt_col_name])

logger.info("Running atwork_subtour_destination_logsums with %s rows" % len(choosers))

tracing.dump_df(DUMP, persons_merged, trace_label, 'persons_merged')
tracing.dump_df(DUMP, choosers, trace_label, 'choosers')

logsums = compute_logsums(
choosers, logsums_spec, logsum_settings,
skim_dict, skim_stack, chooser_col_name, alt_col_name, chunk_size, trace_hh_id, trace_label)
logsums = logsum.compute_logsums(
choosers, logsum_spec,
logsum_settings, model_settings,
skim_dict, skim_stack,
chunk_size, trace_hh_id,
trace_label)

# "add_column series should have an index matching the table to which it is being added"
# when the index has duplicates, however, in the special case that the series index exactly
# matches the table index, then the series value order is preserved. logsums does have a
# matching index, since atwork_subtour_destination_sample was on left side of merge de-dup merge
# matching index, since atwork_subtour_destination_sample was on left side of merge
inject.add_column("atwork_subtour_destination_sample", "mode_choice_logsum", logsums)


Expand All @@ -191,32 +181,35 @@ def atwork_subtour_destination_simulate(tours,
atwork_subtour_destination_sample,
atwork_subtour_destination_spec,
skim_dict,
destination_size_terms,
configs_dir,
chunk_size,
trace_hh_id):
land_use, size_terms,
configs_dir, chunk_size, trace_hh_id):
"""
atwork_subtour_destination model on atwork_subtour_destination_sample
annotated with mode_choice logsum to select a destination from sample alternatives
"""

trace_label = 'atwork_subtour_destination_simulate'
model_settings = inject.get_injectable('atwork_subtour_destination_settings')
model_settings = config.read_model_settings(configs_dir, 'atwork_subtour_destination.yaml')

tours = tours.to_frame()
subtours = tours[tours.tour_category == 'subtour']
# merge persons into tours
choosers = pd.merge(subtours,
persons_merged.to_frame(),
left_on='person_id', right_index=True)
# FIXME - MEMORY HACK - only include columns actually used in spec
chooser_columns = model_settings['SIMULATE_CHOOSER_COLUMNS']
choosers = choosers[chooser_columns]

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

# alternatives are pre-sampled and annotated with logsums and pick_count
# but we have to merge additional alt columns into alt sample list
atwork_subtour_destination_sample = atwork_subtour_destination_sample.to_frame()
destination_size_terms = destination_size_terms.to_frame()

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

alternatives = \
pd.merge(atwork_subtour_destination_sample, destination_size_terms,
left_on=alt_col_name, right_index=True, how="left")
Expand All @@ -225,8 +218,6 @@ def atwork_subtour_destination_simulate(tours,

constants = config.get_model_constants(model_settings)

sample_pool_size = len(destination_size_terms.index)

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

# create wrapper with keys for this lookup - in this case there is a TAZ in the choosers
Expand All @@ -236,15 +227,10 @@ def atwork_subtour_destination_simulate(tours,

locals_d = {
'skims': skims,
'sample_pool_size': float(sample_pool_size)
}
if constants is not None:
locals_d.update(constants)

# FIXME - MEMORY HACK - only include columns actually used in spec
chooser_columns = model_settings['SIMULATE_CHOOSER_COLUMNS']
choosers = choosers[chooser_columns]

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

choices = interaction_sample_simulate(
Expand All @@ -258,23 +244,16 @@ def atwork_subtour_destination_simulate(tours,
trace_label=trace_label,
trace_choice_name='workplace_location')

tracing.print_summary('subtour destination', choices, describe=True)

subtours['destination'] = choices

results = expressions.compute_columns(
df=subtours,
model_settings='annotate_tours_with_dest',
configs_dir=configs_dir,
trace_label=trace_label)

assign_in_place(tours, subtours[['destination']])
assign_in_place(tours, results)

pipeline.replace_table("tours", tours)

pipeline.drop_table('atwork_subtour_destination_sample')

tracing.print_summary('subtour destination', subtours.destination, describe=True)

if trace_hh_id:
tracing.trace_df(tours,
label=trace_label,
Expand Down
8 changes: 2 additions & 6 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ def atwork_subtour_frequency(tours,

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

# reindex since we are working with a subset of tours
choices = choices.reindex(tours.index)

# add atwork_subtour_frequency column to tours
tours['atwork_subtour_frequency'] = choices
# reindex since we are working with a subset of tours
tours['atwork_subtour_frequency'] = choices.reindex(tours.index)
pipeline.replace_table("tours", tours)

# - create atwork_subtours based on atwork_subtour_frequency choice names
Expand All @@ -98,8 +96,6 @@ def atwork_subtour_frequency(tours,

subtours = process_atwork_subtours(work_tours, atwork_subtour_frequency_alternatives)

print subtours

tours = pipeline.extend_table("tours", subtours)
tracing.register_traceable_table('tours', tours)
pipeline.get_rn_generator().add_channel(subtours, 'tours')
Expand Down
Loading