-
Notifications
You must be signed in to change notification settings - Fork 2
/
demand_variable_generation.py
358 lines (272 loc) · 14.1 KB
/
demand_variable_generation.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 4 10:50:11 2023
Module: Generate demand-related input attributes for new micro-geotype
@author: xiaodanxu
"""
# load environment
import pandas as pd
from pandas import read_csv
import os
from os import listdir
import numpy as np
# define constant
unit_converter = {
# distance
'meter_to_mile': 0.000621371,
# area
'm2_to_acre':0.000247105,
'acre_to_sqmile': 0.0015625,
}
# define data path
os.chdir('C:/FHWA_R2')
# load inputs -- the data needs to adopt the same geographic resolution for census tracts
census_year = '2020'
boundary_and_area = read_csv('spatial_boundary/CleanData/combined_tracts_' + census_year + '.csv')
population_data = read_csv('Demography/CleanData/acs_data_tracts_112023.csv')
employment_location_data = read_csv('Demand/CleanData/wac_tract_2021.csv')
land_use_data = read_csv('Land_use/CleanData/imputed_NLCD_data_dev_only.csv')
urban_definition_data = read_csv('spatial_boundary/CleanData/urban_divisions_2021.csv')
path_to_emp_od = 'Demand/CleanData/OD_distance'
employment_od_data = listdir(path_to_emp_od)
#create output dataframe
output_demand_attributes = boundary_and_area[['GEOID', 'ALAND', 'pct_water']]
# remove tracts with all waters
output_demand_attributes = \
output_demand_attributes.loc[output_demand_attributes['pct_water'] < 1]
# <codecell>
"""
part 1 - density based attributes
"""
output_demand_attributes.loc[:, 'land_area_acre'] = \
output_demand_attributes.loc[:, 'ALAND'] * unit_converter['m2_to_acre']
### pop density
population_data_short = population_data.loc[:, ['GEOID', 'populationE', 'householdsE']] # In variable name, E means estimate, M means MOE
output_demand_attributes = pd.merge(output_demand_attributes,
population_data_short,
on = 'GEOID', how = 'left')
output_demand_attributes.loc[:, 'pop_per_acre'] = \
output_demand_attributes.loc[:, 'populationE'] / output_demand_attributes.loc[:, 'land_area_acre']
# check missing
print('total missing values in pop density is: ')
print(output_demand_attributes.loc[:, 'pop_per_acre'].isnull().sum())
# check infinity
print('total infinity values in pop density is: ')
print(sum(np.isinf(output_demand_attributes.loc[:, 'pop_per_acre'])))
### job density
employment_location_short = employment_location_data[['GEOID', 'total_jobs']]
output_demand_attributes = pd.merge(output_demand_attributes,
employment_location_short,
on = 'GEOID', how = 'left')
# fill missing with 0
output_demand_attributes.loc[:, 'total_jobs'] = \
output_demand_attributes.loc[:, 'total_jobs'].fillna(0)
output_demand_attributes.loc[:, 'jobs_per_acre'] = \
output_demand_attributes.loc[:, 'total_jobs'] / output_demand_attributes.loc[:, 'land_area_acre']
# check missing
print('total missing values in job density is: ')
print(output_demand_attributes.loc[:, 'jobs_per_acre'].isnull().sum())
# check infinity
print('total infinity values in job density is: ')
print(sum(np.isinf(output_demand_attributes.loc[:, 'jobs_per_acre'])))
# <codecell>
"""
part 2 - diversity based attributes
"""
### job residence balance--> use person as there are more non-zero values
output_demand_attributes.loc[:, 'jobs_resident_bal'] = \
output_demand_attributes.loc[:, 'total_jobs'] / output_demand_attributes.loc[:, 'populationE']
output_demand_attributes.loc[:, 'jobs_resident_bal'] = \
output_demand_attributes.loc[:, 'jobs_resident_bal'].fillna(0) # NA created if 0/0, replace with 0
# check missing
print('total missing values in job house balance is: ')
print(output_demand_attributes.loc[:, 'jobs_resident_bal'].isnull().sum())
# check infinity
print('total infinity values in job house balance is: ')
print(sum(np.isinf(output_demand_attributes.loc[:, 'jobs_resident_bal'])))
# <codecell>
### job diversity
# calculate 8-tier employment classification
employment_location_data.loc[:, 'office_jobs'] = \
employment_location_data.loc[:, 'naics_51'] + employment_location_data.loc[:, 'naics_52'] + \
employment_location_data.loc[:, 'naics_53'] + employment_location_data.loc[:, 'naics_55']
employment_location_data.loc[:, 'retail_jobs'] = \
employment_location_data.loc[:, 'naics_4445']
employment_location_data.loc[:, 'industry_jobs'] = \
employment_location_data.loc[:, 'naics_11'] + employment_location_data.loc[:, 'naics_21'] + \
employment_location_data.loc[:, 'naics_22'] + employment_location_data.loc[:, 'naics_23'] + \
employment_location_data.loc[:, 'naics_3133'] + employment_location_data.loc[:, 'naics_42'] + \
employment_location_data.loc[:, 'naics_4849']
employment_location_data.loc[:, 'service_jobs'] = \
employment_location_data.loc[:, 'naics_54'] + employment_location_data.loc[:, 'naics_56'] + \
employment_location_data.loc[:, 'naics_81']
employment_location_data.loc[:, 'recreation_jobs'] = \
employment_location_data.loc[:, 'naics_71'] + employment_location_data.loc[:, 'naics_72']
employment_location_data.loc[:, 'education_jobs'] = \
employment_location_data.loc[:, 'naics_61']
employment_location_data.loc[:, 'healthcare_jobs'] = \
employment_location_data.loc[:, 'naics_62']
employment_location_data.loc[:, 'government_jobs'] = \
employment_location_data.loc[:, 'naics_92']
tier_list = ['office_jobs', 'retail_jobs', 'industry_jobs', 'service_jobs',
'recreation_jobs', 'education_jobs', 'healthcare_jobs', 'government_jobs']
employment_location_data.loc[:, 'total_jobs'] = \
employment_location_data.loc[:, 'total_jobs'].fillna(0)
employment_location_data.loc[:, tier_list] = \
employment_location_data[tier_list].div(employment_location_data['total_jobs'], axis=0)
entropy_list = []
for tier in tier_list:
# print('calculate entropy for ' + tier)
e_var = 'e_' + tier
entropy_list.append(e_var)
employment_location_data.loc[:, e_var] = 0
# only calculate entropy for non-zero values
non_zero_id = (employment_location_data[tier] > 0)
employment_location_data.loc[non_zero_id, e_var] = \
-employment_location_data.loc[non_zero_id, tier] * \
np.log(employment_location_data.loc[non_zero_id, tier])
# if the tract has 0 employment, it will not have job diversity as outcome -> diversity = N/A
employment_location_data.loc[:, 'job_diversity'] = np.nan
# if diversity = 0, it means the tract only has 1 industry
non_zero_zone = (employment_location_data['total_jobs'] > 0)
employment_location_data.loc[non_zero_zone, 'job_diversity'] = \
employment_location_data.loc[non_zero_zone, entropy_list].sum(axis = 1) / \
np.log(8)
employment_diversity = employment_location_data[['GEOID', 'job_diversity']]
output_demand_attributes = pd.merge(output_demand_attributes,
employment_diversity,
on = 'GEOID', how = 'left')
# check missing
print('total missing values in job diversity is: ')
print(output_demand_attributes.loc[:, 'job_diversity'].isnull().sum())
# check infinity
print('total infinity values in job diversity is: ')
print(sum(np.isinf(output_demand_attributes.loc[:, 'job_diversity'])))
# <codecell>
### job sink magnitude and trip distance distribution
## update April 01, 2024 --split bin 4 at 50 mile per Mona's request
od_attribute_exist = 0 # if 0, execute the data generation, if 1, load existing output
out_od_attributes = None # create empty data frame to hold generated attributes
dist_bin = [-1, 1.3, 3, 8, 50, 150, 5000]
dist_bin_label = ['jobs 0-1.3 miles', 'jobs 1.3-3 miles', 'jobs 3-8 miles',
'jobs 8-50 miles', 'jobs 50-150 miles', 'remote jobs']
if od_attribute_exist == 0: # run heavy comutation to generate od attributes
od_dist_by_tract = None # create empty data frame to hold input data
trip_by_home_tract = None # create empty data frame to hold input data
trip_by_work_tract = None # create empty data frame to hold input data
for data in employment_od_data:
print('processing od data ' + data)
od_data = read_csv(os.path.join(path_to_emp_od, data))
# trip distance distribution
od_data.loc[:, 'dist_bin'] = \
pd.cut(od_data.loc[:, 'distance'], bins = dist_bin,
labels = dist_bin_label)
od_data_by_dist = pd.pivot_table(od_data,
values = 'S000',
index = 'w_tract',
columns = 'dist_bin',
aggfunc = "sum")
od_data_by_dist = od_data_by_dist.reset_index()
od_data_by_dist = od_data_by_dist.fillna(0)
od_data_by_dist.loc[:, 'total_emp'] = \
od_data_by_dist.loc[:, dist_bin_label].sum(axis = 1)
od_data_by_dist = od_data_by_dist.loc[od_data_by_dist['total_emp'] > 0 ]
# fraction only available to zones with non-zero jobs
# calculate fraction of trips by distance bin
od_data_by_dist.loc[:, dist_bin_label] = od_data_by_dist.loc[:, dist_bin_label].div(
od_data_by_dist.loc[:, 'total_emp'], axis = 0)
od_dist_by_tract = pd.concat([od_dist_by_tract, od_data_by_dist])
# trip sink magnitude (aggregation performed after concat to account for work/home in different states)
od_by_home = od_data.groupby('h_tract')[['S000']].sum()
od_by_home = od_by_home.reset_index()
od_by_home.columns = ['GEOID', 'jobs_by_home']
trip_by_home_tract = pd.concat([trip_by_home_tract, od_by_home])
od_by_work = od_data.groupby('w_tract')[['S000']].sum()
od_by_work = od_by_work.reset_index()
od_by_work.columns = ['GEOID', 'jobs_by_work']
trip_by_work_tract = pd.concat([trip_by_work_tract, od_by_work])
# break
print('if O-D data contains duplicated geoid:')
print(od_dist_by_tract['w_tract'].duplicated().any())
trip_by_home_tract = trip_by_home_tract.groupby('GEOID')[['jobs_by_home']].sum()
trip_by_home_tract = trip_by_home_tract.reset_index()
trip_by_work_tract = trip_by_work_tract.groupby('GEOID')[['jobs_by_work']].sum()
trip_by_work_tract = trip_by_work_tract.reset_index()
out_od_attributes = od_dist_by_tract
out_od_attributes = out_od_attributes.rename(columns = {'w_tract': 'GEOID'})
out_od_attributes = pd.merge(out_od_attributes, trip_by_home_tract,
on = 'GEOID', how = 'left')
out_od_attributes = pd.merge(out_od_attributes, trip_by_work_tract,
on = 'GEOID', how = 'left')
out_od_attributes = out_od_attributes.fillna(0)
out_od_attributes.loc[:, 'job_sink_mag'] = \
out_od_attributes.loc[:, 'jobs_by_work'] / out_od_attributes.loc[:, 'jobs_by_home']
out_od_attributes.to_csv('Demand/CleanData/lehd_od_trip_characteristics_v2.csv', index = False)
else: # load pre-generated data
out_od_attributes = read_csv('Demand/CleanData/lehd_od_trip_characteristics_v2.csv')
# <codecell>
# append OD characteristics to output metrics
var_list = ['GEOID', 'jobs 0-1.3 miles', 'jobs 1.3-3 miles', 'jobs 3-8 miles',
'jobs 8-50 miles', 'jobs 50-150 miles', 'remote jobs', 'job_sink_mag']
out_od_attributes_short = out_od_attributes[var_list]
output_demand_attributes = pd.merge(output_demand_attributes,
out_od_attributes_short,
on = 'GEOID', how = 'left')
# check missing
for var in var_list:
if var == 'GEOID':
continue
else:
print('total missing values in ' + var)
print(output_demand_attributes.loc[:, var].isnull().sum())
# check infinity
print('total infinity values in ' + var)
print(sum(np.isinf(output_demand_attributes.loc[:, var])))
# <codecell>
### land use and land cover attributes
var_list = ['Impervious Developed', 'Developed Open Space']
# land_use_data_selected = land_use_data.loc[land_use_data['land_type'].isin(var_list)]
# land_use_data_wide = pd.pivot_table(land_use_data_selected,
# values = 'fraction', index = ['GEOID'],
# columns = 'land_type',
# aggfunc = "sum")
# land_use_data_wide = land_use_data_wide.fillna(0)
# land_use_data_wide = land_use_data_wide.reset_index()
# land_use_data_wide.loc[:, 'Agriculture Land'] = land_use_data_wide.loc[:, 'Pasture Land'] + \
# land_use_data_wide.loc[:, 'Crop Land']
# land_use_data_wide = \
# land_use_data_wide[['GEOID', 'Impervious Developed', 'Developed Open Space']]
#land_use_data_wide
output_demand_attributes = pd.merge(output_demand_attributes,
land_use_data,
on = 'GEOID', how = 'left')
# check missing
for var in var_list:
if var == 'GEOID':
continue
else:
print('total missing values in ' + var)
print(output_demand_attributes.loc[:, var].isnull().sum())
# check infinity
print('total infinity values in ' + var)
print(sum(np.isinf(output_demand_attributes.loc[:, var])))
# <codecell>
### land use and land cover attributes
var_list = ['GEOID', 'census_urban_area']
urban_definition_data_select = urban_definition_data[var_list]
output_demand_attributes = pd.merge(output_demand_attributes,
urban_definition_data_select,
on = 'GEOID', how = 'left')
# check missing
for var in var_list:
if var == 'GEOID':
continue
else:
print('total missing values in ' + var)
print(output_demand_attributes.loc[:, var].isnull().sum())
# check infinity
print('total infinity values in ' + var)
print(sum(np.isinf(output_demand_attributes.loc[:, var])))
# <codecell>
# Log April 01, 2024 - generate V2 of the data after biennual review
output_demand_attributes.to_csv('Demand/CleanData/microtype_inputs_demand_V2.csv', index = False)