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

BayDAG Contributions #657

Merged
merged 76 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
6c1fc02
Merge pull request #1 from SANDAG/BayDAG
lmz Aug 5, 2022
27e7df8
code changes to add joint tour utilities in cdap
i-am-sijia Aug 25, 2022
ca182ac
add joint tour frequency composition component
i-am-sijia Aug 25, 2022
e91bfe4
post process jtfc result to tours
i-am-sijia Aug 25, 2022
219ec6f
update joint tour participation
i-am-sijia Aug 25, 2022
5715ffe
update #inmtf to tm2 specs
i-am-sijia Aug 25, 2022
4654131
allow alternative id > 127
i-am-sijia Aug 25, 2022
e7edfe2
set up testing infrastructure
i-am-sijia Aug 26, 2022
6aba263
cdap test script
i-am-sijia Aug 26, 2022
7d74e31
cdap configs
i-am-sijia Aug 26, 2022
291736a
set up testing infrastructure
i-am-sijia Aug 26, 2022
e0bd205
joint tour test script
i-am-sijia Aug 26, 2022
ec53b4c
joint tour configs
i-am-sijia Aug 26, 2022
625c6b8
set up testing infrastructure
i-am-sijia Aug 26, 2022
02c417b
nm tour frequency test script
i-am-sijia Aug 26, 2022
fbb139c
nm tour frequency configs
i-am-sijia Aug 26, 2022
5b17b52
add jtfc alt table dictionary to yaml
i-am-sijia Sep 26, 2022
b9a2e11
update jtfc yaml
i-am-sijia Sep 26, 2022
2b3b325
jtfc move coef values to coef.csv
i-am-sijia Sep 26, 2022
583eaf1
jtfc update preprocessor
i-am-sijia Sep 26, 2022
0833caf
add jtfc alt table dictionary to yaml
i-am-sijia Sep 26, 2022
d43003c
nmtf consolidate all -999 to one coef
i-am-sijia Sep 26, 2022
5a38ebb
code changes to fix bug in parking location choice model
AshishKuls Sep 27, 2022
ad20b94
Revert "code changes to fix bug in parking location choice model"
AshishKuls Oct 18, 2022
2f156dc
parking location choice bug fix
AshishKuls Oct 18, 2022
1fd55a1
move coefficients to csv file
AshishKuls Oct 19, 2022
a639661
restore the original mandatory channels
i-am-sijia Nov 2, 2022
ff519ba
restore the original mandatory channels
i-am-sijia Nov 2, 2022
7c67f35
RSG Phase 7 Development (#49)
dhensle Nov 7, 2022
43cd89c
set up testing infrastructure (#40)
i-am-sijia Nov 7, 2022
1641acc
BayDAG auto ownership configs and test (#41)
i-am-sijia Nov 7, 2022
c7e6a4c
merging BayDAG-cdap from BayAreaMetro
dhensle Nov 7, 2022
ff2851c
Merge branch 'BayDAG-cdap_merge' into BayDAG
dhensle Nov 7, 2022
b99aedd
blacken
dhensle Nov 7, 2022
465a196
more formatting
dhensle Nov 7, 2022
b86de00
merging BayDAG-jtfcp from BayAreaMetro
dhensle Nov 7, 2022
bbc1dd6
Merge branch 'jtfcp_merge' into BayDAG
dhensle Nov 7, 2022
bc9a76f
blacken
dhensle Nov 7, 2022
bfb83a2
merging BayDAG-nmtf from BayAreaMetro
dhensle Nov 7, 2022
179563f
merging nmtf
dhensle Nov 7, 2022
778d1cc
merging cdap testing and configs from BayAreaMetro
dhensle Nov 7, 2022
2d6f355
mergin cdap testing and configs from BayAreaMetro
dhensle Nov 7, 2022
750b00d
mergin jtfcp-testing from BayAreaMetro
dhensle Nov 7, 2022
0e4d5d0
blacken
dhensle Nov 7, 2022
84c3a2e
merging nmtf-testing from BayAreaMetro
dhensle Nov 8, 2022
74c11d3
Merge branch 'nmtf-testing_merge' into BayDAG
dhensle Nov 8, 2022
ab638c1
blacken
dhensle Nov 8, 2022
963f818
BayDAG parking location choice configs and test (#45)
AshishKuls Nov 8, 2022
5bb4315
merging marking-location from BayAreaMetro
dhensle Nov 8, 2022
2d0ae39
Merge branch 'parking-location_merge' into BayDAG
dhensle Nov 8, 2022
c086f6a
blacken
dhensle Nov 8, 2022
537adfc
cdap bug fixes
dhensle Nov 8, 2022
91898dd
adding sklearn to dependencies
dhensle Nov 10, 2022
68223df
Update activitysim-dev.yml
dhensle Nov 18, 2022
79ba1f4
Update activitysim-test.yml
dhensle Nov 18, 2022
ae125a6
Update setup.cfg
dhensle Nov 18, 2022
5f05f31
Memory Bug Fix
dhensle Dec 6, 2022
44a1f1e
working with 2k no sp
Dec 13, 2022
7cdb324
optimize nearest zone determination
Dec 14, 2022
076a291
Changed data type of participant_id in candidates in joint_tour_parti…
JoeJimFlood Dec 15, 2022
8fb8d25
better comments
Dec 15, 2022
07bd9b2
fix overflow in jtp participant id
Dec 15, 2022
79a26ba
blacken
Dec 15, 2022
c458fc3
forcing participation
Dec 16, 2022
03846d2
Merge branch 'BayDAG' into resident_debug
dhensle Dec 16, 2022
7386a4d
Merge pull request #53 from SANDAG/resident_debug
dhensle Dec 16, 2022
9506f16
Trip Scheduling (#51)
dhensle Dec 21, 2022
c474a26
Trip Scheduling Bug & Trip Mode Choice Annotate (#55)
dhensle Dec 21, 2022
c718ee4
adding locals_dict to annotate
Jan 5, 2023
8122271
chooser cols in final trips table
Jan 24, 2023
80336dc
BayDAG merge with ActivitySim v1.2 (#56)
dhensle Feb 11, 2023
05d5008
do not require all alts in data file
Feb 21, 2023
ded281a
Merge pull request #57 from SANDAG/BayDAG_av_extension
dhensle Feb 27, 2023
e1ee2f9
int8 to int16 lost in v1.2 merge
dhensle Mar 7, 2023
d5d3d61
merging in develop
dhensle Mar 9, 2023
8e26470
Merge branch 'develop' into develop_BayDAG_merge
dhensle Mar 9, 2023
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
13 changes: 9 additions & 4 deletions HOW_TO_RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@

00. Run black to ensure that the codebase passes all style checks.
This check should only take a few seconds. These checks are also done on
Travis and are platform independent, so they should not be necessary to
GitHub Actions and are platform independent, so they should not be necessary to
replicate locally, but are listed here for completeness.
```sh
black --check --diff .
```

00. Run the regular test suite on Windows. Travis tests are done on Linux,
but most users are on Windows, and the test suite should also be run
on Windows to ensure that it works on that platform as well. If you
00. Run the regular test suite on Windows. Most GitHub Actions tests are done on Linux,
Linux (it's faster to start up and run a new clean VM for testing) but most
users are on Windows, and the test suite should also be run on Windows to
ensure that it works on that platform as well. If you
are not preparing this release on Windows, you should be sure to run
at least through this step on a Windows machine before finalizing a
release.
Expand Down Expand Up @@ -135,6 +136,10 @@
```sh
gh release create v1.2.3
```
The process of creating and tagging a release will automatically
trigger various GitHub Actions scripts to build, test, and publish the
new release to PyPI and conda forge, assuming there are no errors.

For a development pre-release, include the `--prerelease` argument.
As the project's policy is that only formally released code is merged
to the main branch, any pre-release should also be built against a
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
joint_tour_frequency,
joint_tour_participation,
joint_tour_scheduling,
joint_tour_frequency_composition,
location_choice,
mandatory_scheduling,
mandatory_tour_frequency,
Expand Down
71 changes: 59 additions & 12 deletions activitysim/abm/models/cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):
file_name=model_settings["FIXED_RELATIVE_PROPORTIONS_SPEC"]
)

add_joint_tour_utility = model_settings.get("ADD_JOINT_TOUR_UTILITY", False)

if add_joint_tour_utility:
# Rules and coefficients for generating cdap joint tour specs for different household sizes
joint_tour_coefficients_file_name = model_settings.get(
"JOINT_TOUR_COEFFICIENTS", "cdap_joint_tour_coefficients.csv"
)
cdap_joint_tour_coefficients = pd.read_csv(
config.config_file_path(joint_tour_coefficients_file_name), comment="#"
)

persons_merged = persons_merged.to_frame()

# add tour-based chunk_id so we can chunk all trips in tour together
Expand All @@ -101,11 +112,27 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):
# (also when multiprocessing locutor might not see all household sizes)
logger.info("Pre-building cdap specs")
for hhsize in range(2, cdap.MAX_HHSIZE + 1):
spec = cdap.build_cdap_spec(cdap_interaction_coefficients, hhsize, cache=True)
spec = cdap.build_cdap_spec(
cdap_interaction_coefficients,
hhsize,
cache=True,
joint_tour_alt=add_joint_tour_utility,
)
if inject.get_injectable("locutor", False):
spec.to_csv(
config.output_file_path("cdap_spec_%s.csv" % hhsize), index=True
)
if add_joint_tour_utility:
# build cdap joint tour spec
# joint_spec_dependency = spec.loc[[c for c in spec.index if c.startswith(('M_p', 'N_p', 'H_p'))]]
joint_spec = cdap.build_cdap_joint_spec(
cdap_joint_tour_coefficients, hhsize, cache=True
)
if inject.get_injectable("locutor", False):
joint_spec.to_csv(
config.output_file_path("cdap_joint_spec_%s.csv" % hhsize),
index=True,
)

if estimator:
estimator.write_model_settings(model_settings, "cdap.yaml")
Expand All @@ -127,17 +154,32 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):

logger.info("Running cdap_simulate with %d persons", len(persons_merged.index))

choices = cdap.run_cdap(
persons=persons_merged,
person_type_map=person_type_map,
cdap_indiv_spec=cdap_indiv_spec,
cdap_interaction_coefficients=cdap_interaction_coefficients,
cdap_fixed_relative_proportions=cdap_fixed_relative_proportions,
locals_d=constants,
chunk_size=chunk_size,
trace_hh_id=trace_hh_id,
trace_label=trace_label,
)
if add_joint_tour_utility:
choices, hh_joint = cdap.run_cdap(
persons=persons_merged,
person_type_map=person_type_map,
cdap_indiv_spec=cdap_indiv_spec,
cdap_interaction_coefficients=cdap_interaction_coefficients,
cdap_fixed_relative_proportions=cdap_fixed_relative_proportions,
locals_d=constants,
chunk_size=chunk_size,
trace_hh_id=trace_hh_id,
trace_label=trace_label,
add_joint_tour_utility=add_joint_tour_utility,
)
else:
choices = cdap.run_cdap(
persons=persons_merged,
person_type_map=person_type_map,
cdap_indiv_spec=cdap_indiv_spec,
cdap_interaction_coefficients=cdap_interaction_coefficients,
cdap_fixed_relative_proportions=cdap_fixed_relative_proportions,
locals_d=constants,
chunk_size=chunk_size,
trace_hh_id=trace_hh_id,
trace_label=trace_label,
add_joint_tour_utility=add_joint_tour_utility,
)

if estimator:
estimator.write_choices(choices)
Expand All @@ -161,6 +203,11 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):

# - annotate households table
households = households.to_frame()

if add_joint_tour_utility:
hh_joint = hh_joint.reindex(households.index)
households["has_joint_tour"] = hh_joint

expressions.assign_columns(
df=households,
model_settings=model_settings.get("annotate_households"),
Expand Down
204 changes: 204 additions & 0 deletions activitysim/abm/models/joint_tour_frequency_composition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# ActivitySim
# See full license in LICENSE.txt.
import logging

import numpy as np
import pandas as pd
import os
from activitysim.core.interaction_simulate import interaction_simulate

from activitysim.core import simulate
from activitysim.core import tracing
from activitysim.core import pipeline
from activitysim.core import config
from activitysim.core import inject
from activitysim.core import expressions

from .util import estimation

from .util.overlap import hh_time_window_overlap
from .util.tour_frequency import process_joint_tours_frequency_composition

logger = logging.getLogger(__name__)


@inject.step()
def joint_tour_frequency_composition(
households_merged, persons, chunk_size, trace_hh_id
):
"""
This model predicts the frequency and composition of fully joint tours.
"""

trace_label = "joint_tour_frequency_composition"
model_settings_file_name = "joint_tour_frequency_composition.yaml"

model_settings = config.read_model_settings(model_settings_file_name)

alt_tdd = simulate.read_model_alts(
"joint_tour_frequency_composition_alternatives.csv", set_index="alt"
)

# - only interested in households with more than one cdap travel_active person and
# - at least one non-preschooler
households_merged = households_merged.to_frame()
choosers = households_merged[households_merged.participates_in_jtf_model].copy()

# - only interested in persons in choosers households
persons = persons.to_frame()
persons = persons[persons.household_id.isin(choosers.index)]

logger.info("Running %s with %d households", trace_label, len(choosers))

# alt preprocessor
alt_preprocessor_settings = model_settings.get("ALTS_PREPROCESSOR", None)
if alt_preprocessor_settings:

locals_dict = {}

alt_tdd = alt_tdd.copy()

expressions.assign_columns(
df=alt_tdd,
model_settings=alt_preprocessor_settings,
locals_dict=locals_dict,
trace_label=trace_label,
)

# - preprocessor
preprocessor_settings = model_settings.get("preprocessor", None)
if preprocessor_settings:

locals_dict = {
"persons": persons,
"hh_time_window_overlap": hh_time_window_overlap,
}

expressions.assign_columns(
df=choosers,
model_settings=preprocessor_settings,
locals_dict=locals_dict,
trace_label=trace_label,
)

estimator = estimation.manager.begin_estimation("joint_tour_frequency_composition")

model_spec = simulate.read_model_spec(file_name=model_settings["SPEC"])
coefficients_df = simulate.read_model_coefficients(model_settings)
model_spec = simulate.eval_coefficients(model_spec, coefficients_df, estimator)

constants = config.get_model_constants(model_settings)

if estimator:
estimator.write_spec(model_settings)
estimator.write_model_settings(model_settings, model_settings_file_name)
estimator.write_coefficients(coefficients_df, model_settings)
estimator.write_choosers(choosers)
estimator.write_alternatives(alts)

assert choosers.index.name == "household_id"
assert "household_id" not in choosers.columns
choosers["household_id"] = choosers.index

estimator.set_chooser_id(choosers.index.name)

# The choice value 'joint_tour_frequency_composition' assigned by interaction_simulate
# is the index value of the chosen alternative in the alternatives table.
choices = interaction_simulate(
choosers=choosers,
alternatives=alt_tdd,
spec=model_spec,
locals_d=constants,
chunk_size=chunk_size,
trace_label=trace_label,
trace_choice_name=trace_label,
estimator=estimator,
)

if estimator:
estimator.write_choices(choices)
choices = estimator.get_survey_values(
choices, "households", "joint_tour_frequency_composition"
)
estimator.write_override_choices(choices)
estimator.end_estimation()

# add joint tour frequency composition column to household table
households_merged["joint_tour_frequency_composition"] = choices.reindex(
households_merged.index
).fillna(0)

# - create joint_tours based on choices

# - 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 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")
temp_point_persons = temp_point_persons[["person_id", "home_zone_id"]]

# create a tours table of tour_category "joint" and different tour_types (e.g. shopping, eat)
# and add the composition column (adults or children or mixed) to the tour

# Choices
# hhid choice
# 11111 1
# 22222 2
# 33333 3

# Alts
# alt purpose1 purpose2 party1 party2 eat shop
# 1 5 0 3 0 1 0
# 2 5 6 1 3 1 1
# 3 6 0 1 0 0 1

# Joint Tours
# hhid type category composition
# 11111 eat joint mixed
# 22222 eat joint adults
# 22222 shop joint mixed
# 33333 shop joint adults

joint_tours = process_joint_tours_frequency_composition(
choices, alt_tdd, temp_point_persons
)

tours = pipeline.extend_table("tours", joint_tours)

tracing.register_traceable_table("tours", joint_tours)
pipeline.get_rn_generator().add_channel("tours", joint_tours)

# we expect there to be an alt with no tours - which we can use to backfill non-travelers
no_tours_alt = 0
households_merged["joint_tour_frequency_composition"] = (
choices.reindex(households_merged.index).fillna(no_tours_alt).astype(str)
)

households_merged["num_hh_joint_tours"] = (
joint_tours.groupby("household_id")
.size()
.reindex(households_merged.index)
.fillna(0)
.astype(np.int8)
)

pipeline.replace_table("households", households_merged)

tracing.print_summary(
"joint_tour_frequency_composition",
households_merged.joint_tour_frequency_composition,
value_counts=True,
)

if trace_hh_id:
tracing.trace_df(
households_merged, label="joint_tour_frequency_composition.households"
)

tracing.trace_df(
joint_tours,
label="joint_tour_frequency_composition.joint_tours",
slicer="household_id",
)
Loading