Skip to content

Commit c005042

Browse files
committed
get all disaggregate accessibility values
1 parent a8e755f commit c005042

2 files changed

Lines changed: 47 additions & 25 deletions

File tree

activitysim/abm/models/disaggregate_accessibility.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,13 @@ def merge_persons(self):
569569
inject.add_table("proto_persons_merged", persons_merged)
570570

571571

572-
def get_disaggregate_logsums(network_los, chunk_size, trace_hh_id):
572+
def get_disaggregate_logsums(
573+
network_los, chunk_size, trace_hh_id, disagg_model_settings
574+
):
573575
logsums = {}
574576
persons_merged = pipeline.get_table("proto_persons_merged").sort_index(
575577
inplace=False
576578
)
577-
disagg_model_settings = read_disaggregate_accessibility_yaml(
578-
"disaggregate_accessibility.yaml"
579-
)
580579

581580
for model_name in [
582581
"workplace_location",
@@ -696,8 +695,14 @@ def compute_disaggregate_accessibility(network_los, chunk_size, trace_hh_id):
696695
tracing.register_traceable_table(tablename, df)
697696
del df
698697

698+
disagg_model_settings = read_disaggregate_accessibility_yaml(
699+
"disaggregate_accessibility.yaml"
700+
)
701+
699702
# Run location choice
700-
logsums = get_disaggregate_logsums(network_los, chunk_size, trace_hh_id)
703+
logsums = get_disaggregate_logsums(
704+
network_los, chunk_size, trace_hh_id, disagg_model_settings
705+
)
701706
logsums = {k + "_accessibility": v for k, v in logsums.items()}
702707

703708
# Combined accessibility table
@@ -736,20 +741,20 @@ def compute_disaggregate_accessibility(network_los, chunk_size, trace_hh_id):
736741
logsums["proto_disaggregate_accessibility"] = access_df
737742

738743
# Drop any tables prematurely created
739-
for tablename in [
740-
"school_destination_size",
741-
"workplace_destination_size",
742-
]:
743-
pipeline.drop_table(tablename)
744+
# FIXME: dropping size tables breaks restart functionality for location choice models.
745+
# hopefully this pipeline mess just goes away with move away from orca....
746+
# for tablename in [
747+
# "school_destination_size",
748+
# "workplace_destination_size",
749+
# ]:
750+
# pipeline.drop_table(tablename)
744751

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

748-
# Drop any prematurely added traceables
749-
for trace in [
750-
x for x in inject.get_injectable("traceable_tables") if "proto_" not in x
751-
]:
752-
tracing.deregister_traceable_table(trace)
755+
# Dropping all traceable tables
756+
for table in inject.get_injectable("traceable_tables"):
757+
tracing.deregister_traceable_table(table)
753758

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

768+
# available post-processing
769+
for annotations in disagg_model_settings.get("postprocess_proto_tables", []):
770+
tablename = annotations["tablename"]
771+
df = pipeline.get_table(tablename)
772+
assert df is not None
773+
assert annotations is not None
774+
assign_columns(
775+
df=df,
776+
model_settings={
777+
**annotations["annotate"],
778+
**disagg_model_settings["suffixes"],
779+
},
780+
trace_label=tracing.extend_trace_label(
781+
"disaggregate_accessibility.postprocess", tablename
782+
),
783+
)
784+
pipeline.replace_table(tablename, df)
785+
763786
return

activitysim/abm/tables/disaggregate_accessibility.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,13 @@ def disaggregate_accessibility(persons, households, land_use, accessibility):
151151
accessibility_cols = [
152152
x for x in proto_accessibility_df.columns if "accessibility" in x
153153
]
154+
keep_cols = model_settings.get("KEEP_COLS", accessibility_cols)
154155

155156
# Parse the merging parameters
156157
assert merging_params is not None
157158

158159
# Check if already assigned!
159-
if set(accessibility_cols).intersection(persons_merged_df.columns) == set(
160-
accessibility_cols
161-
):
160+
if set(keep_cols).intersection(persons_merged_df.columns) == set(keep_cols):
162161
return
163162

164163
# Find the nearest zone (spatially) with accessibilities calculated
@@ -190,7 +189,7 @@ def disaggregate_accessibility(persons, households, land_use, accessibility):
190189
# because it will get slightly different logsums for households in the same zone.
191190
# This is because different destination zones were selected. To resolve, get mean by cols.
192191
right_df = (
193-
proto_accessibility_df.groupby(merge_cols)[accessibility_cols]
192+
proto_accessibility_df.groupby(merge_cols)[keep_cols]
194193
.mean()
195194
.sort_values(nearest_cols)
196195
.reset_index()
@@ -223,9 +222,9 @@ def disaggregate_accessibility(persons, households, land_use, accessibility):
223222
)
224223

225224
# Predict the nearest person ID and pull the logsums
226-
matched_logsums_df = right_df.loc[clf.predict(x_pop)][
227-
accessibility_cols
228-
].reset_index(drop=True)
225+
matched_logsums_df = right_df.loc[clf.predict(x_pop)][keep_cols].reset_index(
226+
drop=True
227+
)
229228
merge_df = pd.concat(
230229
[left_df.reset_index(drop=False), matched_logsums_df], axis=1
231230
).set_index("person_id")
@@ -257,12 +256,12 @@ def disaggregate_accessibility(persons, households, land_use, accessibility):
257256

258257
# Check that it was correctly left-joined
259258
assert all(persons_merged_df[merge_cols] == merge_df[merge_cols])
260-
assert any(merge_df[accessibility_cols].isnull())
259+
assert any(merge_df[keep_cols].isnull())
261260

262261
# Inject merged accessibilities so that it can be included in persons_merged function
263-
inject.add_table("disaggregate_accessibility", merge_df[accessibility_cols])
262+
inject.add_table("disaggregate_accessibility", merge_df[keep_cols])
264263

265-
return merge_df[accessibility_cols]
264+
return merge_df[keep_cols]
266265

267266

268267
inject.broadcast(

0 commit comments

Comments
 (0)