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 Estimation Enhancements #58

Draft
wants to merge 68 commits into
base: BayDAG
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
9075771
estimation mode enhancements
Feb 20, 2023
54cb3d1
blacken
Feb 21, 2023
828e505
Merge branch 'BayDAG_av_extension' into BayDAG_estimation
Feb 21, 2023
b461dd2
ao preprocessor, cdap & jtfc estimation
Mar 2, 2023
0248793
blacken
Mar 2, 2023
9d17c41
hh_joint from survey only if adding joint utility
Mar 2, 2023
7ba0ddd
blacken
Mar 2, 2023
9c1d345
Merge pull request #59 from SANDAG/BayDAG
dhensle Mar 7, 2023
da724c3
write out mode choice logsum
dhensle Mar 16, 2023
92acbfa
tracing for proto population
dhensle Mar 16, 2023
398c5a9
dropping all traceable tables after disagg
dhensle Mar 16, 2023
2cae898
Merge branch 'BayDAG' into BayDAG_estimation
dhensle Mar 21, 2023
049efcf
blacken
dhensle Mar 21, 2023
cad3bf7
pandas < 2.0
dhensle Apr 6, 2023
879d098
annotate and col selection in disagg access
dhensle Apr 6, 2023
1318320
annotate auto ownership for selection of disaggregate accessibilities
dhensle Apr 6, 2023
67b1b90
added to annotate helper
dhensle Apr 6, 2023
d2fdc58
school escorting estimation enhancements
dhensle Apr 7, 2023
03e4b67
tracing improvements for disaggregate accessibilities
dhensle Apr 7, 2023
b466836
skip data bundle writing functionality
dhensle Apr 7, 2023
a9dda1c
fixing restart issue with missing destination_size
dhensle Apr 8, 2023
560db34
missed hdf cleanup
dhensle Apr 8, 2023
53ecea8
Added estimation functionality for external worker identification model.
jfdman Apr 25, 2023
8205609
Added code to estimate external worker location choice model
jfdman Apr 25, 2023
47f22e2
miscellaneous est fixes
dhensle May 15, 2023
7354f7a
update simple_simulate.py to include work_from_home_model estimation …
jfdman May 18, 2023
7163ddf
Merge branch 'BayDAG_estimation' of https://github.com/SANDAG/activit…
jfdman May 18, 2023
5847bda
Added code for external_non_mandatory_identification_model estimation…
jfdman May 18, 2023
88fe4a7
added landuse to trip dest locals_d
dhensle Jun 21, 2023
a0483cc
Merge branch 'BayDAG_estimation' of https://github.com/SANDAG/activit…
dhensle Jun 21, 2023
90fce8d
adding transit pass estimation functions
dhensle Jul 4, 2023
901e2fc
transponder ownership estimation call
dhensle Jul 4, 2023
c5c0d80
estimation stop frequency improvements
dhensle Jul 15, 2023
c0fdfde
sampling location choice in estimation mode, reindex and landuse avai…
dhensle Jul 19, 2023
a25c891
cdap larch updates
dhensle Jul 27, 2023
851ba21
blacken
dhensle Jul 27, 2023
cb4a0cb
added estimation functionality for external_non_mandatory_destination…
jfdman Aug 14, 2023
7ac64a1
nmtf estimation & enhancements
dhensle Aug 17, 2023
56ec71b
assertions in location choice estimation
dhensle Aug 17, 2023
fd49296
Merge branch 'BayDAG_estimation' of https://github.com/SANDAG/activit…
dhensle Aug 17, 2023
49a20fb
blacken
dhensle Aug 17, 2023
0950b80
Added estimation functionality for telecommute_frequency_model
jfdman Sep 12, 2023
fad6808
Updated trip_matrices.py to not overwrite trip destination with parki…
JoeJimFlood Sep 25, 2023
14e62e0
Merge pull request #64 from SANDAG/parkloc_fix
dhensle Jan 25, 2024
5adfc7b
remove alts from alts_long in veh type choice
dhensle Feb 2, 2024
15b7f31
logging and correct order for veh_type name
dhensle Feb 3, 2024
2a99bed
correct veh type alts for choices
dhensle Feb 6, 2024
8cccce3
Updated origin adjustment to parking zone to only happen if the trip …
JoeJimFlood Feb 21, 2024
851319d
Final trips table now reports origin and destination TAZs regardless …
JoeJimFlood Feb 21, 2024
5fb2b83
Previous commit is only necessary for 2- and 3-zone systems
JoeJimFlood Feb 21, 2024
11a3d33
Removed lookup of trip mode choice configs so user specifies auto mod…
JoeJimFlood Feb 21, 2024
8fa7202
Merge pull request #65 from SANDAG/parkloc_fix2
JoeJimFlood Mar 13, 2024
389131e
Fixed bugs found by Ali
JoeJimFlood Mar 14, 2024
f969542
adding school escort columns that were missed if no escorting
dhensle Mar 18, 2024
627e286
fixing disaggregate accessibility bug in zone sampler
aletzdy Mar 19, 2024
92bf60d
Made code changes to get categorical vehicle types to work
JoeJimFlood Mar 28, 2024
f1bdd88
Changed settings call to old format
JoeJimFlood Mar 28, 2024
b0061bc
Fixed a couple more model settings calls
JoeJimFlood Mar 28, 2024
a3f6056
Removed state variable from function call and added default empty arr…
JoeJimFlood Mar 28, 2024
7449923
Fixed call of reading in alts preprocessor
JoeJimFlood Mar 28, 2024
74833cb
Replaced l with , in vehicle_type_choice.py
JoeJimFlood Mar 28, 2024
2c11873
Merge pull request #66 from SANDAG/BayDAG_estimation_vehtypefix
JoeJimFlood Mar 29, 2024
422ca6c
Load skims into shared memory to be accessed by later models
aber-sandag Apr 8, 2024
31c3c35
Don't use shared memory when running singleprocessed (temp fix)
aber-sandag Apr 10, 2024
f3b9ee8
Merge pull request #67 from SANDAG/BayDAG_estimation_skim_mem
bhargavasana Apr 12, 2024
7775033
Fix skims in trip_mode_choice annotate_trips
aber-sandag Apr 15, 2024
98713f3
fix to #841
dhensle Apr 19, 2024
e4dc5ac
reducing memory needs in parking_location_choice
dhensle May 1, 2024
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
22 changes: 20 additions & 2 deletions activitysim/abm/models/auto_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# See full license in LICENSE.txt.
import logging

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

from .util import estimation
from .util import estimation, annotate

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,6 +32,21 @@ def auto_ownership_simulate(households, households_merged, chunk_size, trace_hh_

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

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

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

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

if estimator:
estimator.write_model_settings(model_settings, model_settings_file_name)
estimator.write_spec(model_settings)
Expand Down Expand Up @@ -69,5 +84,8 @@ def auto_ownership_simulate(households, households_merged, chunk_size, trace_hh_
"auto_ownership", households.auto_ownership, value_counts=True
)

if model_settings.get("annotate_households"):
annotate.annotate_households(model_settings, trace_label)

if trace_hh_id:
tracing.trace_df(households, label="auto_ownership", warn_if_empty=True)
10 changes: 10 additions & 0 deletions activitysim/abm/models/cdap.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):
for hhsize in range(2, cdap.MAX_HHSIZE + 1):
spec = cdap.get_cached_spec(hhsize)
estimator.write_table(spec, "spec_%s" % hhsize, append=False)
if add_joint_tour_utility:
joint_spec = cdap.get_cached_joint_spec(hhsize)
estimator.write_table(
joint_spec, "joint_spec_%s" % hhsize, append=False
)

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

Expand Down Expand Up @@ -184,6 +189,11 @@ def cdap_simulate(persons_merged, persons, households, chunk_size, trace_hh_id):
if estimator:
estimator.write_choices(choices)
choices = estimator.get_survey_values(choices, "persons", "cdap_activity")
if add_joint_tour_utility:
hh_joint.index.name = "household_id"
hh_joint = estimator.get_survey_values(
hh_joint, "households", "has_joint_tour"
)
estimator.write_override_choices(choices)
estimator.end_estimation()

Expand Down
57 changes: 42 additions & 15 deletions activitysim/abm/models/disaggregate_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ def zone_sampler(self):
maz_candidates = maz_candidates[
~maz_candidates.MAZ.isin(maz_sample_idx)
]

# Need to make sure we sample from TAZs that still exist in the maz_candidates
taz_candidates = taz_candidates[taz_candidates.index.isin(maz_candidates.TAZ)]

# Calculate the remaining samples to collect
n_samples_remaining = n_samples - len(maz_sample_idx)
n_samples_remaining = (
Expand Down Expand Up @@ -569,14 +573,13 @@ def merge_persons(self):
inject.add_table("proto_persons_merged", persons_merged)


def get_disaggregate_logsums(network_los, chunk_size, trace_hh_id):
def get_disaggregate_logsums(
network_los, chunk_size, trace_hh_id, disagg_model_settings
):
logsums = {}
persons_merged = pipeline.get_table("proto_persons_merged").sort_index(
inplace=False
)
disagg_model_settings = read_disaggregate_accessibility_yaml(
"disaggregate_accessibility.yaml"
)

for model_name in [
"workplace_location",
Expand Down Expand Up @@ -696,8 +699,14 @@ def compute_disaggregate_accessibility(network_los, chunk_size, trace_hh_id):
tracing.register_traceable_table(tablename, df)
del df

disagg_model_settings = read_disaggregate_accessibility_yaml(
"disaggregate_accessibility.yaml"
)

# Run location choice
logsums = get_disaggregate_logsums(network_los, chunk_size, trace_hh_id)
logsums = get_disaggregate_logsums(
network_los, chunk_size, trace_hh_id, disagg_model_settings
)
logsums = {k + "_accessibility": v for k, v in logsums.items()}

# Combined accessibility table
Expand Down Expand Up @@ -736,20 +745,20 @@ def compute_disaggregate_accessibility(network_los, chunk_size, trace_hh_id):
logsums["proto_disaggregate_accessibility"] = access_df

# Drop any tables prematurely created
for tablename in [
"school_destination_size",
"workplace_destination_size",
]:
pipeline.drop_table(tablename)
# FIXME: dropping size tables breaks restart functionality for location choice models.
# hopefully this pipeline mess just goes away with move away from orca....
# for tablename in [
# "school_destination_size",
# "workplace_destination_size",
# ]:
# pipeline.drop_table(tablename)

for ch in list(pipeline.get_rn_generator().channels.keys()):
pipeline.get_rn_generator().drop_channel(ch)

# Drop any prematurely added traceables
for trace in [
x for x in inject.get_injectable("traceable_tables") if "proto_" not in x
]:
tracing.deregister_traceable_table(trace)
# Dropping all traceable tables
for table in inject.get_injectable("traceable_tables"):
tracing.deregister_traceable_table(table)

# need to clear any premature tables that were added during the previous run
orca._TABLES.clear()
Expand All @@ -760,4 +769,22 @@ def compute_disaggregate_accessibility(network_los, chunk_size, trace_hh_id):
# Inject accessibility results into pipeline
[inject.add_table(k, df) for k, df in logsums.items()]

# available post-processing
for annotations in disagg_model_settings.get("postprocess_proto_tables", []):
tablename = annotations["tablename"]
df = pipeline.get_table(tablename)
assert df is not None
assert annotations is not None
assign_columns(
df=df,
model_settings={
**annotations["annotate"],
**disagg_model_settings["suffixes"],
},
trace_label=tracing.extend_trace_label(
"disaggregate_accessibility.postprocess", tablename
),
)
pipeline.replace_table(tablename, df)

return
2 changes: 1 addition & 1 deletion activitysim/abm/models/joint_tour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def joint_tour_destination(
estimator.write_model_settings(model_settings, model_settings_file_name)

choices_df, save_sample_df = tour_destination.run_tour_destination(
tours,
joint_tours,
persons_merged,
want_logsums,
want_sample_table,
Expand Down
13 changes: 11 additions & 2 deletions activitysim/abm/models/joint_tour_frequency_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ def joint_tour_frequency_composition(

model_settings = config.read_model_settings(model_settings_file_name)

# FIXME setting index as "alt" causes crash in estimation mode...
# happens in school escorting too!
# alt_tdd = simulate.read_model_alts(
# "joint_tour_frequency_composition_alternatives.csv", set_index="alt"
# )
alt_tdd = simulate.read_model_alts(
"joint_tour_frequency_composition_alternatives.csv", set_index="alt"
"joint_tour_frequency_composition_alternatives.csv", set_index=None
)
alt_tdd.index = alt_tdd["alt"].values

# - only interested in households with more than one cdap travel_active person and
# - at least one non-preschooler
Expand Down Expand Up @@ -94,14 +100,16 @@ def joint_tour_frequency_composition(
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)

# FIXME set_alt_id - do we need this for interaction_simulate estimation bundle tables?
estimator.set_alt_id("alt_id")

# 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(
Expand Down Expand Up @@ -134,6 +142,7 @@ def joint_tour_frequency_composition(
# - 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
# FIXME: not all models are guaranteed to have PNUM
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")
Expand Down
4 changes: 4 additions & 0 deletions activitysim/abm/models/joint_tour_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def participants_chooser(probs, choosers, spec, trace_label):
probs[choice_col] = np.where(probs[choice_col] > 0, 1, 0)
non_choice_col = [col for col in probs.columns if col != choice_col][0]
probs[non_choice_col] = 1 - probs[choice_col]
if iter > MAX_ITERATIONS + 1:
raise RuntimeError(
f"{num_tours_remaining} tours could not be satisfied even with forcing participation"
)
else:
raise RuntimeError(
f"{num_tours_remaining} tours could not be satisfied after {iter} iterations"
Expand Down
Loading
Loading