-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.3_assign_facility_all.py
365 lines (302 loc) · 12.9 KB
/
3.3_assign_facility_all.py
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
import geopandas as gpd
import pandas as pd
from libpysal.weights import Queen
import acbm
from acbm.assigning.plots import plot_desire_lines, plot_scatter_actual_reported
from acbm.assigning.select_facility import map_activity_locations, select_facility
from acbm.cli import acbm_cli
from acbm.config import load_config
from acbm.logger_config import assigning_facility_locations_logger as logger
@acbm_cli
def main(config_file):
config = load_config(config_file)
config.init_rng()
# --- Load data: activity chains
logger.info("Loading activity chains")
activity_chains = pd.read_csv(
acbm.root_path / "data/processed/activities_pam/legs.csv"
)
activity_chains = activity_chains.drop(columns=["Unnamed: 0", "freq"])
# --- Preprocess: Split activity chains by activity purpose
logger.info("Splitting activity chains by activity purpose")
activity_chains_home = activity_chains[
activity_chains["destination activity"] == "home"
]
activity_chains_work = activity_chains[
activity_chains["destination activity"] == "work"
]
activity_chains_edu = activity_chains[
activity_chains["destination activity"] == "education"
]
# secondary activities
activities_to_exclude = ["home", "work", "education"]
activity_chains_other = activity_chains[
~activity_chains["destination activity"].isin(activities_to_exclude)
]
# --- Load data: POI locations
logger.info("Loading facility data")
osm_data_gdf = gpd.read_parquet(
acbm.root_path / "data/external/boundaries/west-yorkshire_epsg_4326.parquet"
)
# --- Load data: Boundaries
logger.info("Loading study area boundaries")
boundaries = gpd.read_file(
acbm.root_path / "data/external/boundaries/study_area_zones.geojson"
)
logger.info("Study area boundaries loaded")
# --- Prepprocess: add zone column to POI data
logger.info("Adding zone column to POI data")
# ensure that osm_data_gdf and boundaries are in the same crs
osm_data_gdf = osm_data_gdf.to_crs(boundaries.crs)
osm_data_gdf = gpd.sjoin(
osm_data_gdf,
boundaries[[config.zone_id, "geometry"]],
how="inner",
predicate="within",
)
# --- Analysis: SELECTING FACILITIES
logger.info("Selecting facilities")
# Get neighboring zones
logger.info("1. Calculating neighboring zones")
# get neighbors
zone_neighbors = Queen.from_dataframe(
boundaries, idVariable=config.zone_id
).neighbors
# - HOME LOCATIONS
logger.info("2. Selecting HOME locations")
# Keep one row per household and select only household and OA21CD columns
activity_chains_home_hh = activity_chains_home.drop_duplicates(subset=["hid"])
activity_chains_home_hh = activity_chains_home_hh[
["hid", "destination activity", "dzone"]
]
activity_locations_home = select_facility(
df=activity_chains_home_hh,
unique_id_col="hid",
facilities_gdf=osm_data_gdf,
row_destination_zone_col="dzone",
row_activity_type_col="destination activity",
gdf_facility_zone_col=config.zone_id,
gdf_facility_type_col="activities",
gdf_sample_col="floor_area",
neighboring_zones=zone_neighbors,
)
# Map the activity_id and activity_geometry to the activity_chains_home_df DataFrame
activity_chains_home = map_activity_locations(
activity_chains_df=activity_chains_home,
activity_locations_dict=activity_locations_home,
id_col="hid",
)
# - WORK LOCATIONS
logger.info("3. Selecting WORK locations")
activity_locations_work = select_facility(
df=activity_chains_work,
unique_id_col="pid",
facilities_gdf=osm_data_gdf,
row_destination_zone_col="dzone",
row_activity_type_col="destination activity",
gdf_facility_zone_col=config.zone_id,
gdf_facility_type_col="activities",
gdf_sample_col="floor_area",
neighboring_zones=zone_neighbors,
)
# Map the activity_id and activity_geometry to the activity_chains_df DataFrame
activity_chains_work = map_activity_locations(
activity_chains_df=activity_chains_work,
activity_locations_dict=activity_locations_work,
id_col="pid",
)
logger.info(f"Shape of activity chains work: {activity_chains_work.shape}")
# - EDUCATION LOCATIONS
logger.info("4. Selecting EDUCATION locations")
logger.info("a. Adding eduction type as fallback")
# load in activity chains
spc_with_nts = pd.read_parquet(
acbm.root_path / "data/interim/matching/spc_with_nts_trips.parquet",
columns=["id", "education_type", "seq", "TripTotalTime", "TripDisIncSW"],
)
# we get one row per id
spc_with_nts_edu = spc_with_nts[["id", "education_type"]].drop_duplicates(
subset="id"
)
# merge the education type with the activity chains
activity_chains_edu = activity_chains_edu.merge(
spc_with_nts_edu, left_on="pid", right_on="id", how="left"
).drop(columns=["id"])
logger.info("b. Selecting education locations")
# apply the function to a row in activity_chains_ex
activity_locations_edu = select_facility(
df=activity_chains_edu,
unique_id_col="pid",
facilities_gdf=osm_data_gdf,
row_destination_zone_col="dzone",
row_activity_type_col="education_type",
gdf_facility_zone_col=config.zone_id,
gdf_facility_type_col="activities",
gdf_sample_col="floor_area",
neighboring_zones=zone_neighbors,
fallback_type="education",
)
logger.info(f"Shape of activity chains edu: {activity_chains_edu.shape}")
# Map the activity_id and activity_geometry to the activity_chains_home_df DataFrame
activity_chains_edu = map_activity_locations(
activity_chains_df=activity_chains_edu,
activity_locations_dict=activity_locations_edu,
id_col="pid",
)
# - SECONDARY LOCATIONS
logger.info("5. Selecting SECONDARY locations")
logger.info("a. creating unique_id column")
# pid and hid are not unique id columns, as there can be many different secondary
# activities done by the same person.
# We create a unique identifier that can be mapped back to the original data.
# Unique id column: Concatenate pid, seq
activity_chains_other["act_id"] = (
activity_chains_other["pid"].astype(str)
+ "_"
+ activity_chains_other["seq"].astype(str)
)
logger.info("b. Selecting secondary locations")
# apply the function to a row in activity_chains_ex
activity_locations_other = select_facility(
df=activity_chains_other,
unique_id_col="act_id",
facilities_gdf=osm_data_gdf,
row_destination_zone_col="dzone",
row_activity_type_col="purp",
gdf_facility_zone_col=config.zone_id,
gdf_facility_type_col="activities",
gdf_sample_col="floor_area",
neighboring_zones=zone_neighbors,
fallback_to_random=True,
)
# Map the activity_id and activity_geometry to the activity_chains_home_df DataFrame
activity_chains_other = map_activity_locations(
activity_chains_df=activity_chains_other,
activity_locations_dict=activity_locations_other,
id_col="act_id",
)
# --- Analysis: Merging data
logger.info("Merging all activity chains")
activity_chains_all = pd.concat(
[
activity_chains_home,
activity_chains_work,
activity_chains_edu,
activity_chains_other,
]
)
activity_chains_all = activity_chains_all.sort_values(by=["hid", "pid", "seq"])
# --- Analysis: Create start_location_id and start_location_geometry column
logger.info("Creating start_location_id and start_location_geometry columns")
# Create start_location_id and start_location_geometry by shifting end_location_id and end_location_geometry within each 'pid'
activity_chains_all["start_location_id"] = activity_chains_all.groupby("pid")[
"end_location_id"
].shift(1)
activity_chains_all["start_location_geometry"] = activity_chains_all.groupby("pid")[
"end_location_geometry"
].shift(1)
logger.info("Fill rows where seq = 1 with home location")
mask = activity_chains_all["seq"] == 1
# Aggregate duplicates by taking the first occurrence
activity_chains_home_agg = activity_chains_home.groupby("hid").first().reset_index()
# Map home location data to the start_location_id and start_location_geometry columns
activity_chains_all.loc[mask, "start_location_id"] = activity_chains_all.loc[
mask, "hid"
].map(activity_chains_home_agg.set_index("hid")["end_location_id"])
activity_chains_all.loc[mask, "start_location_geometry"] = activity_chains_all.loc[
mask, "hid"
].map(activity_chains_home_agg.set_index("hid")["end_location_geometry"])
# --- Save data
# Keep necessary columns
# select only the columns we need
activity_chains_all = activity_chains_all[
[
"pid",
"hid",
"ozone",
"dzone",
"purp",
"origin activity",
"destination activity",
"mode",
"seq",
"tst",
"tet",
"duration",
"start_location_id",
"start_location_geometry",
"end_location_id",
"end_location_geometry",
]
]
# save as parquet: note to serialize the geometries, need to convert
# non-missing values to e.g. wkt
geom_cols = ["start_location_geometry", "end_location_geometry"]
for col in geom_cols:
activity_chains_all.loc[:, col + "_wkt"] = activity_chains_all[col].map(
lambda point: point if pd.isna(point) else point.wkt
)
activity_chains_all.drop(columns=geom_cols).to_parquet(
acbm.root_path / "data/processed/activities_pam/legs_with_locations.parquet"
)
# --- Plots
logger.info("Creating plots")
# merge actual times from the NTS
activity_chains_all = activity_chains_all.merge(
spc_with_nts[["id", "seq", "TripTotalTime", "TripDisIncSW"]],
left_on=["pid", "seq"],
right_on=["id", "seq"],
how="left",
).drop(columns=["id"])
# Get unique activity types from the 'purp' column
unique_activity_types = activity_chains_all["purp"].unique()
# Plot 1: Euclidian travel distance vs reported (NTS) travel DISTANCE
logger.info("Plotting Euclidian travel distance vs reported (NTS) travel DISTANCE")
# Iterate over each unique activity type and create a plot
for activity_type in unique_activity_types:
plot_scatter_actual_reported(
activities=activity_chains_all,
activity_type=activity_type,
activity_type_col="destination activity",
x_col="TripDisIncSW",
y_col="length",
x_label="Reported Travel Distance (km)",
y_label="Actual Distance - Euclidian (km)",
title_prefix=f"Scatter plot of TripDisIncSW vs. Length for {activity_type}",
save_dir=acbm.root_path / "data/processed/plots/assigning/",
)
# Plot 2: Euclidian travel distance vs reported (NTS) travel TIME
logger.info("Plotting Euclidian travel distance vs reported (NTS) travel TIME")
# # convert duration to numeric
# activity_chains_all["duration"] = pd.to_timedelta(activity_chains_all["duration"], errors="coerce")
# activity_chains_all['duration'] = activity_chains_all['duration'].apply(lambda x: x + pd.Timedelta(days=1) if x.days < 0 else x)
# activity_chains_all["duration"] = activity_chains_all["duration"].dt.total_seconds() / 60
# activity_chains_all["duration"] = activity_chains_all["duration"].astype(int)
# Iterate over each unique activity type and create a plot
for activity_type in unique_activity_types:
plot_scatter_actual_reported(
activities=activity_chains_all,
activity_type=activity_type,
activity_type_col="destination activity",
x_col="TripTotalTime",
y_col="length",
x_label="Reported Travel TIme (min)",
y_label="Actual Distance - Euclidian (km)",
title_prefix="Scatter plot of TripTotalTime vs. Length",
save_dir=acbm.root_path / "data/processed/plots/assigning/",
)
# ....
# Plot 3: Desire lines between start and end locations
logger.info("Plotting desire lines between start and end locations")
for activity_type in unique_activity_types:
plot_desire_lines(
activities=activity_chains_all,
activity_type_col="destination activity",
activity_type=activity_type,
bin_size=5000,
boundaries=boundaries,
sample_size=1000,
save_dir=acbm.root_path / "data/processed/plots/assigning/",
)
if __name__ == "__main__":
main()