forked from OrMSC/activitysim_visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoint_travel.py
More file actions
455 lines (396 loc) · 14.2 KB
/
Copy pathjoint_travel.py
File metadata and controls
455 lines (396 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""Joint travel summaries."""
import polars as pl
from runtime.config import Config
from processor.models import RunData
from processor.summarize.contracts import summary
@summary(
id="jtf_distribution",
schema={
"jtf_code": pl.Int32,
"jtf_label": pl.Utf8,
"household_count": pl.Float64,
},
required_columns={"hh": ("household_id", "finalweight")},
)
def joint_tour_freq(rd: RunData, config: Config | None = None) -> pl.DataFrame:
"""Returns DataFrame: jtf_code, jtf_label, household_count."""
JTF_NAMES = [
"No Joint Tours",
"1 Shopping",
"1 Maintenance",
"1 Eating Out",
"1 Visiting",
"1 Other Discretionary",
"2 Shopping",
"2 Maintenance",
"2 Eating Out",
"2 Visiting",
"2 Other Discretionary",
"1 Shopping / 1 Maintenance",
"1 Shopping / 1 Eating Out",
"1 Shopping / 1 Visiting",
"1 Shopping / 1 Other Discretionary",
"1 Maintenance / 1 Eating Out",
"1 Maintenance / 1 Visiting",
"1 Maintenance / 1 Other Discretionary",
"1 Eating Out / 1 Visiting",
"1 Eating Out / 1 Other Discretionary",
"1 Visiting / 1 Other Discretionary",
]
jtf_lookup = pl.DataFrame(
{
"jtf_code": list(range(1, 22)),
"jtf_label": JTF_NAMES,
},
schema={
"jtf_code": pl.Int32,
"jtf_label": pl.Utf8,
},
)
if "tour_category" not in rd.tours.columns:
return jtf_lookup.with_columns(pl.lit(0.0).alias("household_count"))
joint_tours = rd.tours.filter(pl.col("tour_category") == "joint")
if "tour_purpose" not in joint_tours.columns:
return jtf_lookup.with_columns(pl.lit(0.0).alias("household_count"))
# Map purpose strings to slot letters (a-e for up to 5 NM purposes)
nm_purposes = joint_tours["tour_purpose"].drop_nulls().unique().sort().to_list()
# Take up to 5 most common (for JTF coding)
purpose_slots = {p: f"j{i}" for i, p in enumerate(nm_purposes[:5])}
slot_cols = [f"j{i}" for i in range(len(purpose_slots))]
hh_joint = pl.DataFrame({"household_id": rd.hh["household_id"]})
for purp, slot in purpose_slots.items():
counts = (
joint_tours.filter(pl.col("tour_purpose") == purp)
.group_by("household_id")
.agg(pl.len().cast(pl.Int64).alias(slot))
)
hh_joint = hh_joint.join(counts, on="household_id", how="left").with_columns(
pl.col(slot).fill_null(0)
)
for slot in slot_cols:
if slot not in hh_joint.columns:
hh_joint = hh_joint.with_columns(pl.lit(0).alias(slot))
all_hh = rd.hh.select(["household_id", "finalweight"])
hh_joint = all_hh.join(hh_joint, on="household_id", how="left")
for slot in slot_cols:
if slot in hh_joint.columns:
hh_joint = hh_joint.with_columns(pl.col(slot).fill_null(0))
# TODO: JTF coding is simplified; verify category assignment matches formal ActivitySim joint tour frequency definitions.
# Code JTF (simplified: 1=none, 2-6=single, 7-11=two same, 12-21=two different)
hh_joint = hh_joint.with_columns(pl.lit(1).alias("jtf"))
codes = [
(2, 1, 0, 0, 0, 0),
(3, 0, 1, 0, 0, 0),
(4, 0, 0, 1, 0, 0),
(5, 0, 0, 0, 1, 0),
(6, 0, 0, 0, 0, 1),
(7, 2, 0, 0, 0, 0),
(8, 0, 2, 0, 0, 0),
(9, 0, 0, 2, 0, 0),
(10, 0, 0, 0, 2, 0),
(11, 0, 0, 0, 0, 2),
(12, 1, 1, 0, 0, 0),
(13, 1, 0, 1, 0, 0),
(14, 1, 0, 0, 1, 0),
(15, 1, 0, 0, 0, 1),
(16, 0, 1, 1, 0, 0),
(17, 0, 1, 0, 1, 0),
(18, 0, 1, 0, 0, 1),
(19, 0, 0, 1, 1, 0),
(20, 0, 0, 1, 0, 1),
(21, 0, 0, 0, 1, 1),
]
for code, *vals in codes:
conds = []
for i, v in enumerate(vals):
col = f"j{i}"
if col in hh_joint.columns:
if v == 1:
conds.append(pl.col(col) >= 1)
elif v == 2:
conds.append(pl.col(col) >= 2)
if conds:
cond = conds[0]
for c in conds[1:]:
cond = cond & c
hh_joint = hh_joint.with_columns(
pl.when(cond).then(code).otherwise(pl.col("jtf")).alias("jtf")
)
summary = (
hh_joint.group_by("jtf")
.agg(household_count=pl.col("finalweight").sum())
.rename({"jtf": "jtf_code"})
)
return (
jtf_lookup.join(summary, on="jtf_code", how="left")
.with_columns(pl.col("household_count").fill_null(0.0))
.select(
pl.col("jtf_code").cast(pl.Int32),
pl.col("jtf_label").cast(pl.Utf8),
pl.col("household_count").cast(pl.Float64),
)
)
@summary(
id="joint_tours_by_household_size",
schema={
"household_size": pl.Int32,
"household_count": pl.Float64,
"joint_tour_hh_count": pl.Float64,
},
required_columns={
"tours": ("tour_category", "household_id"),
"hh": ("household_id", "HHSIZE", "finalweight"),
},
)
def joint_tours_hhsize(rd: RunData, config: Config | None = None) -> pl.DataFrame:
"""Returns DataFrame: household_size, household_count, joint_tour_hh_count."""
if (
"tour_category" not in rd.tours.columns
or "household_id" not in rd.tours.columns
or "HHSIZE" not in rd.hh.columns
or "household_id" not in rd.hh.columns
or "finalweight" not in rd.hh.columns
):
return joint_tours_hhsize.empty()
joint_tour_hhs = (
rd.tours.filter(pl.col("tour_category") == "joint")
.select("household_id")
.unique()
.with_columns(has_joint_tour=pl.lit(True))
)
return (
rd.hh.select(["household_id", "HHSIZE", "finalweight"])
.join(joint_tour_hhs, on="household_id", how="left")
.with_columns(has_joint_tour=pl.col("has_joint_tour").fill_null(False))
.group_by("HHSIZE")
.agg(
household_count=pl.col("finalweight").sum(),
joint_tour_hh_count=pl.when(pl.col("has_joint_tour"))
.then(pl.col("finalweight"))
.otherwise(0.0)
.sum(),
)
.rename({"HHSIZE": "household_size"})
.sort("household_size")
)
@summary(
id="joint_tour_party_size_distribution",
schema={
"party_size": pl.Int32,
"joint_tour_count": pl.Float64,
},
required_columns={"tours": ("tour_category", "NUMBER_HH", "finalweight")},
)
def joint_party_size(rd: RunData, config: Config | None = None) -> pl.DataFrame:
"""Joint tour party size distribution (capped at 5+). Columns: party_size (1-5), joint_tour_count."""
if "tour_category" not in rd.tours.columns or "NUMBER_HH" not in rd.tours.columns:
return joint_party_size.empty()
joint_tours = rd.tours.filter(pl.col("tour_category") == "joint")
if joint_tours.is_empty():
return joint_party_size.empty()
df = (
joint_tours.group_by("NUMBER_HH")
.agg(joint_tour_count=pl.col("finalweight").sum())
.sort("NUMBER_HH")
)
cap5 = df.filter(pl.col("NUMBER_HH") >= 5)["joint_tour_count"].sum()
df = df.filter(pl.col("NUMBER_HH") < 5).with_columns(
pl.col("NUMBER_HH").cast(pl.Int32)
)
cap_row = pl.DataFrame(
{
"NUMBER_HH": pl.Series([5], dtype=pl.Int32),
"joint_tour_count": pl.Series([cap5 or 0.0], dtype=pl.Float64),
}
)
return (
pl.concat([df, cap_row])
.sort("NUMBER_HH")
.rename({"NUMBER_HH": "party_size"})
.select(
pl.col("party_size").cast(pl.Int32),
pl.col("joint_tour_count").cast(pl.Float64),
)
)
@summary(
id="joint_tour_composition_distribution",
schema={
"tour_composition": pl.Utf8,
"joint_tour_count": pl.Float64,
},
required_columns={"tours": ("tour_category", "finalweight")},
)
def joint_composition(rd: RunData, config: Config | None = None) -> pl.DataFrame:
"""Joint tour composition. Columns: tour_composition, joint_tour_count."""
if "tour_category" not in rd.tours.columns:
return joint_composition.empty()
joint_tours = rd.tours.filter(pl.col("tour_category") == "joint")
# ActivitySim uses "composition"; fall back to "tour_composition" if somehow renamed
comp_col = (
"composition" if "composition" in joint_tours.columns else "tour_composition"
)
if comp_col not in joint_tours.columns:
return joint_composition.empty()
return (
joint_tours.group_by(comp_col)
.agg(joint_tour_count=pl.col("finalweight").sum())
.rename({comp_col: "tour_composition"})
.sort("tour_composition")
)
@summary(
id="joint_tour_composition_by_party_size",
schema={
"tour_composition": pl.Utf8,
"party_size": pl.Int64,
"joint_tour_count": pl.Float64,
},
required_columns={
"tours": (
"tour_category",
"composition",
"number_of_participants",
"finalweight",
)
},
)
def joint_composition_by_party_size(rd: RunData, config: Config) -> pl.DataFrame:
required = {
"tour_category",
"composition",
"number_of_participants",
"finalweight",
}
if not required.issubset(set(rd.tours.columns)):
return joint_composition_by_party_size.empty()
return (
rd.tours.filter(
pl.col("tour_category").cast(pl.Utf8).str.to_lowercase() == "joint"
)
.filter(
pl.col("composition").is_not_null()
& pl.col("number_of_participants").is_not_null()
)
.group_by(["composition", "number_of_participants"])
.agg(joint_tour_count=pl.col("finalweight").sum())
.rename(
{
"composition": "tour_composition",
"number_of_participants": "party_size",
}
)
.with_columns(
pl.col("tour_composition").cast(pl.Utf8),
pl.col("party_size").cast(pl.Int64),
pl.col("joint_tour_count").cast(pl.Float64),
)
.select("tour_composition", "party_size", "joint_tour_count")
.sort(["tour_composition", "party_size"])
)
@summary(
id="person_jtp_by_household_size",
schema={
"household_size": pl.Int64,
"joint_tour_person_count": pl.Float64,
"total_person_count": pl.Float64,
},
required_columns={
"per": ("household_id", "num_joint_tours", "finalweight"),
"hh": ("household_id", "hhsize"),
},
)
def joint_participation_person_by_hhsize(rd: RunData, config: Config) -> pl.DataFrame:
person_required = {"household_id", "num_joint_tours", "finalweight"}
hh_required = {"household_id", "hhsize"}
if not person_required.issubset(set(rd.per.columns)) or not hh_required.issubset(
set(rd.hh.columns)
):
return joint_participation_person_by_hhsize.empty()
persons_with_hhsize = rd.per.join(
rd.hh.select("household_id", "hhsize"),
on="household_id",
how="left",
).filter(pl.col("hhsize").is_not_null())
if persons_with_hhsize.is_empty():
return joint_participation_person_by_hhsize.empty()
total_people = persons_with_hhsize.group_by("hhsize").agg(
total_person_weight=pl.col("finalweight").sum()
)
joint_tour_people = (
persons_with_hhsize.filter(
pl.col("num_joint_tours").is_not_null() & (pl.col("num_joint_tours") > 0)
)
.group_by("hhsize")
.agg(joint_tour_person_weight=pl.col("finalweight").sum())
)
return (
total_people.join(joint_tour_people, on="hhsize", how="left")
.with_columns(
pl.col("joint_tour_person_weight").fill_null(0.0),
)
.rename({"hhsize": "household_size"})
.with_columns(
pl.col("household_size").cast(pl.Int64),
pl.col("joint_tour_person_weight")
.cast(pl.Float64)
.alias("joint_tour_person_count"),
pl.col("total_person_weight").cast(pl.Float64).alias("total_person_count"),
)
.select("household_size", "joint_tour_person_count", "total_person_count")
.sort("household_size")
)
@summary(
id="household_jtp_by_household_size_and_jtf",
schema={
"jtf": pl.Utf8,
"household_size": pl.Utf8,
"household_percent": pl.Float64,
},
required_columns={
"hh": ("household_id", "HHSIZE", "finalweight"),
"tours": ("tour_category", "household_id"),
},
)
def jtf_by_hhsize(rd: RunData, config: Config | None = None) -> pl.DataFrame:
"""Joint tour count category (0/1/2+) by HH size as proportions.
Returns DataFrame: jtf, household_size, household_percent."""
hh = rd.hh
if "tour_category" not in rd.tours.columns or "HHSIZE" not in hh.columns:
return jtf_by_hhsize.empty()
joint_tours = rd.tours.filter(pl.col("tour_category") == "joint")
jt_counts = joint_tours.group_by("household_id").agg(
pl.len().cast(pl.Int64).alias("jtours")
)
hh2 = (
hh.join(jt_counts, on="household_id", how="left")
.with_columns(pl.col("jtours").fill_null(0))
.with_columns(
pl.when(pl.col("jtours") == 0)
.then(pl.lit("0"))
.when(pl.col("jtours") == 1)
.then(pl.lit("1"))
.otherwise(pl.lit("2+"))
.alias("jtf"),
pl.col("HHSIZE").cast(pl.Utf8).alias("household_size"),
)
.filter(pl.col("HHSIZE") >= 2)
)
grouped = hh2.group_by(["household_size", "jtf"]).agg(
household_count=pl.col("finalweight").sum()
)
totals = grouped.group_by("household_size").agg(
total_households=pl.col("household_count").sum()
)
return (
grouped.join(totals, on="household_size", how="left")
.with_columns(
(pl.col("household_count") / pl.col("total_households") * 100).alias(
"household_percent"
)
)
.select(
pl.col("jtf").cast(pl.Utf8),
pl.col("household_size").cast(pl.Utf8),
pl.col("household_percent").cast(pl.Float64),
)
.sort(["household_size", "jtf"])
)