forked from OrMSC/activitysim_visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong_term_distance.py
More file actions
203 lines (184 loc) · 6.62 KB
/
Copy pathlong_term_distance.py
File metadata and controls
203 lines (184 loc) · 6.62 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
"""Long-term travel distance distributions."""
from __future__ import annotations
import polars as pl
from processor.models import RunData
from processor.summarize.contracts import output_schema, summary
from processor.summarize.summaries.long_term_shared import _student_filter_expr
from processor.summarize.summaries.summary_helpers import (
_configured_geography_columns,
_configured_geography_dimensions,
)
from runtime.config import Config
@output_schema(
schema={
"distance_bin": pl.Int32,
"geography_type": pl.Utf8,
"geography_id": pl.Utf8,
"person_count": pl.Float64,
}
)
def tlfd(rd: RunData, config: Config) -> dict[str, pl.DataFrame]:
def _bin_dist(df: pl.DataFrame, dist_col: str) -> pl.DataFrame:
return df.with_columns(
pl.col(dist_col).fill_null(0.0).clip(0, 9999)
).with_columns(
pl.col(dist_col).cast(pl.Int32).clip(0, 51).alias("distance_bin")
)
def _make_tlfd(persons: pl.DataFrame, dist_col: str) -> pl.DataFrame:
empty = work_tlfd.empty()
if dist_col not in persons.columns:
return empty
df = _bin_dist(
persons.select(
dist_col,
"finalweight",
"home_zone_id",
*_configured_geography_columns(
persons,
config=config,
role_prefix="home",
),
),
dist_col,
)
distance_bins = pl.DataFrame(
{"distance_bin": list(range(0, 52))}, schema={"distance_bin": pl.Int32}
)
outputs: list[pl.DataFrame] = []
for geography_type, geography_col in _configured_geography_dimensions(
df,
config=config,
base_type="maz" if config.use_maz else "taz",
base_col="home_zone_id",
role_prefix="home",
):
geographies = (
df.filter(pl.col(geography_col).is_not_null())
.select(pl.col(geography_col).cast(pl.Utf8).alias("geography_id"))
.drop_nulls()
.unique()
.sort("geography_id")
)
if geographies.is_empty():
continue
by_geo = (
df.filter(pl.col(geography_col).is_not_null())
.with_columns(pl.col(geography_col).cast(pl.Utf8).alias("geography_id"))
.group_by(["distance_bin", "geography_id"])
.agg(person_count=pl.col("finalweight").sum())
)
outputs.append(
distance_bins.join(geographies, how="cross")
.join(by_geo, on=["distance_bin", "geography_id"], how="left")
.with_columns(
pl.lit(geography_type).alias("geography_type"),
pl.col("person_count").fill_null(0.0).cast(pl.Float64),
)
.select(
"distance_bin", "geography_type", "geography_id", "person_count"
)
)
total = (
df.group_by("distance_bin")
.agg(person_count=pl.col("finalweight").sum())
.with_columns(
pl.lit("all_geographies").alias("geography_type"),
pl.lit("all_geographies").alias("geography_id"),
)
.select("distance_bin", "geography_type", "geography_id", "person_count")
)
outputs.append(
distance_bins.with_columns(
pl.lit("all_geographies").alias("geography_type"),
pl.lit("all_geographies").alias("geography_id"),
)
.join(
total,
on=["distance_bin", "geography_type", "geography_id"],
how="left",
)
.with_columns(pl.col("person_count").fill_null(0.0).cast(pl.Float64))
.select("distance_bin", "geography_type", "geography_id", "person_count")
)
return (
pl.concat(outputs, how="vertical")
.sort(["geography_type", "geography_id", "distance_bin"])
.select("distance_bin", "geography_type", "geography_id", "person_count")
)
ptype_col = "person_type" if "person_type" in rd.per.columns else None
workers = (
rd.per.filter(
(pl.col("workplace_zone_id") > 0)
& (
pl.col("is_worker")
.cast(pl.Utf8)
.str.to_lowercase()
.is_in(["true", "1"])
)
)
if "is_worker" in rd.per.columns
else rd.per.head(0)
)
if ptype_col is not None:
univ = (
rd.per.filter(
(pl.col("school_zone_id") > 0)
& _student_filter_expr()
& (pl.col(ptype_col).cast(pl.Utf8) == "3")
)
if "is_student" in rd.per.columns
else rd.per.head(0)
)
schl = (
rd.per.filter(
(pl.col("school_zone_id") > 0)
& _student_filter_expr()
& (pl.col(ptype_col).cast(pl.Utf8).cast(pl.Int32, strict=False) >= 6)
)
if "is_student" in rd.per.columns
else rd.per.head(0)
)
else:
univ = rd.per.head(0)
schl = rd.per.head(0)
return {
"work": _make_tlfd(workers, "distance_to_work"),
"univ": _make_tlfd(univ, "distance_to_school"),
"schl": _make_tlfd(schl, "distance_to_school"),
}
@summary(
id="work_location_distance_distribution_by_geography",
schema={
"distance_bin": pl.Int32,
"geography_type": pl.Utf8,
"geography_id": pl.Utf8,
"person_count": pl.Float64,
},
required_columns={"per": ("distance_to_work", "finalweight")},
)
def work_tlfd(rd: RunData, config: Config) -> pl.DataFrame:
return tlfd(rd, config)["work"]
@summary(
id="university_location_distance_distribution_by_geography",
schema={
"distance_bin": pl.Int32,
"geography_type": pl.Utf8,
"geography_id": pl.Utf8,
"person_count": pl.Float64,
},
required_columns={"per": ("distance_to_school", "finalweight")},
)
def univ_tlfd(rd: RunData, config: Config) -> pl.DataFrame:
return tlfd(rd, config)["univ"]
@summary(
id="school_location_distance_distribution_by_geography",
schema={
"distance_bin": pl.Int32,
"geography_type": pl.Utf8,
"geography_id": pl.Utf8,
"person_count": pl.Float64,
},
required_columns={"per": ("distance_to_school", "finalweight")},
)
def schl_tlfd(rd: RunData, config: Config) -> pl.DataFrame:
return tlfd(rd, config)["schl"]