forked from BayAreaMetro/bayarea_urbansim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baus.py
541 lines (412 loc) · 17.3 KB
/
baus.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
from __future__ import print_function
import os
import sys
import time
import traceback
from baus import models
from baus import slr
from baus import earthquake
from baus import ual
from baus import validation
import numpy as np
import pandas as pd
import orca
import socket
import argparse
import warnings
from baus.utils import compare_summary
warnings.filterwarnings("ignore")
# Suppress scientific notation in pandas output
pd.set_option('display.float_format', lambda x: '%.3f' % x)
SLACK = MAPS = "URBANSIM_SLACK" in os.environ
LOGS = True
RANDOM_SEED = True
INTERACT = False
SCENARIO = None
MODE = "simulation"
S3 = False
EVERY_NTH_YEAR = 5
BRANCH = os.popen('git rev-parse --abbrev-ref HEAD').read()
CURRENT_COMMIT = os.popen('git rev-parse HEAD').read()
COMPARE_TO_NO_PROJECT = True
NO_PROJECT = 611
IN_YEAR, OUT_YEAR = 2010, 2050
COMPARE_AGAINST_LAST_KNOWN_GOOD = False
LAST_KNOWN_GOOD_RUNS = {
"0": 1057,
"1": 1058,
"2": 1059,
"3": 1060,
"4": 1059,
"5": 1059
}
orca.add_injectable("years_per_iter", EVERY_NTH_YEAR)
orca.add_injectable("base_year", IN_YEAR)
orca.add_injectable("slack_enabled", SLACK)
parser = argparse.ArgumentParser(description='Run UrbanSim models.')
parser.add_argument(
'-c', action='store_true', dest='console',
help='run from the console (logs to stdout), no slack or maps')
parser.add_argument('-i', action='store_true', dest='interactive',
help='enter interactive mode after imports')
parser.add_argument('-s', action='store', dest='scenario',
help='specify which scenario to run')
parser.add_argument('-k', action='store_true', dest='skip_base_year',
help='skip base year - used for debugging')
parser.add_argument('-y', action='store', dest='out_year', type=int,
help='The year to which to run the simulation.')
parser.add_argument('--mode', action='store', dest='mode',
help='which mode to run (see code for mode options)')
parser.add_argument('--random-seed', action='store_true', dest='random_seed',
help='set a random seed for consistent stochastic output')
parser.add_argument('--disable-slack', action='store_true', dest='noslack',
help='disable slack outputs')
options = parser.parse_args()
if options.console:
SLACK = MAPS = LOGS = False
if options.interactive:
SLACK = MAPS = LOGS = False
INTERACT = True
if options.out_year:
OUT_YEAR = options.out_year
if options.scenario:
orca.add_injectable("scenario", options.scenario)
SKIP_BASE_YEAR = options.skip_base_year
if options.mode:
MODE = options.mode
if options.random_seed:
RANDOM_SEED = True
if options.noslack:
SLACK = False
SCENARIO = orca.get_injectable("scenario")
if INTERACT:
import code
code.interact(local=locals())
sys.exit()
run_num = orca.get_injectable("run_number")
if LOGS:
print('***The Standard stream is being written to /runs/run{0}.log***'
.format(run_num))
sys.stdout = sys.stderr = open("runs/run%d.log" % run_num, 'w')
if RANDOM_SEED:
np.random.seed(12)
if SLACK:
from slacker import Slacker
slack = Slacker(os.environ["SLACK_TOKEN"])
host = socket.gethostname()
if MAPS:
from baus.utils import ue_config, ue_files
def get_simulation_models(SCENARIO):
# ual has a slightly different set of models - might be able to get rid
# of the old version soon
models = [
"slr_inundate",
"slr_remove_dev",
"eq_code_buildings",
"earthquake_demolish",
"neighborhood_vars", # street network accessibility
"regional_vars", # road network accessibility
"nrh_simulate", # non-residential rent hedonic
# uses conditional probabilities
"household_relocation",
"households_transition",
# update building/unit/hh correspondence
"reconcile_unplaced_households",
"jobs_relocation",
"jobs_transition",
"balance_rental_and_ownership_hedonics",
"price_vars",
"scheduled_development_events",
# preserve some units
"preserve_affordable",
# run the subsidized residential acct system
"lump_sum_accounts",
"subsidized_residential_developer_lump_sum_accts",
# run the subsidized office acct system
"office_lump_sum_accounts",
"subsidized_office_developer_lump_sum_accts",
"alt_feasibility",
"residential_developer",
"developer_reprocess",
"retail_developer",
"office_developer",
"accessory_units",
"calculate_vmt_fees",
# (for buildings that were removed)
"remove_old_units",
# set up units for new residential buildings
"initialize_new_units",
# update building/unit/hh correspondence
"reconcile_unplaced_households",
"rsh_simulate", # residential sales hedonic for units
"rrh_simulate", # residential rental hedonic for units
# (based on higher of predicted price or rent)
"assign_tenure_to_new_units",
# we first put Q1 households only into deed-restricted units,
# then any additional unplaced Q1 households, Q2, Q3, and Q4
# households are placed in either deed-restricted units or
# market-rate units
"hlcm_owner_lowincome_simulate",
"hlcm_renter_lowincome_simulate",
# allocate owners to vacant owner-occupied units
"hlcm_owner_simulate",
# allocate renters to vacant rental units
"hlcm_renter_simulate",
# we have to run the hlcm above before this one - we first want to
# try and put unplaced households into their appropraite tenured
# units and then when that fails, force them to place using the
# code below. technically the hlcms above could be moved above the
# developer again, but we would have to run the hedonics twice and
# also the assign_tenure_to_new_units twice.
# force placement of any unplaced households, in terms of rent/own
# is a noop except in the final simulation year
# 09 11 2020 ET: enabled for all simulation years
"hlcm_owner_simulate_no_unplaced",
"hlcm_owner_lowincome_simulate_no_unplaced",
# this one crashes right no because there are no unplaced, so
# need to fix the crash in urbansim
# 09 11 2020 ET: appears to be working
"hlcm_renter_simulate_no_unplaced",
"hlcm_renter_lowincome_simulate_no_unplaced",
# update building/unit/hh correspondence
"reconcile_placed_households",
"proportional_elcm", # start with a proportional jobs model
"elcm_simulate", # displaced by new dev
# save_intermediate_tables", # saves output for visualization
"topsheet",
"simulation_validation",
"parcel_summary",
"building_summary",
"diagnostic_output",
"geographic_summary",
"travel_model_output",
# "travel_model_2_output",
"hazards_slr_summary",
"hazards_eq_summary",
"slack_report"
]
# calculate VMT taxes
vmt_settings = \
orca.get_injectable("policy")["acct_settings"]["vmt_settings"]
if SCENARIO in vmt_settings["com_for_com_scenarios"] and \
SCENARIO not in vmt_settings["db_geography_scenarios"]:
models.insert(models.index("office_developer"),
"subsidized_office_developer_vmt")
if SCENARIO in vmt_settings["com_for_res_scenarios"] or \
SCENARIO in vmt_settings["res_for_res_scenarios"]:
models.insert(models.index("diagnostic_output"),
"calculate_vmt_fees")
models.insert(models.index("alt_feasibility"),
"subsidized_residential_feasibility")
models.insert(models.index("alt_feasibility"),
"subsidized_residential_developer_vmt")
# calculate jobs-housing fees
jobs_housing_settings = \
orca.get_injectable("policy")[
"acct_settings"]["jobs_housing_fee_settings"]
if SCENARIO in jobs_housing_settings["jobs_housing_com_for_res_scenarios"]:
models.insert(models.index("diagnostic_output"),
"calculate_jobs_housing_fees")
# models.insert(models.index("alt_feasibility"),
# "subsidized_residential_feasibility")
# models.insert(models.index("alt_feasibility"),
# "subsidized_residential_developer_jobs_housing")
return models
def run_models(MODE, SCENARIO):
if MODE == "preprocessing":
orca.run([
"preproc_jobs",
"preproc_households",
"preproc_buildings",
"initialize_residential_units"
])
elif MODE == "fetch_data":
orca.run(["fetch_from_s3"])
elif MODE == "debug":
orca.run(["simulation_validation"], [2010])
elif MODE == "simulation":
# see above for docs on this
if not SKIP_BASE_YEAR:
orca.run([
"slr_inundate",
"slr_remove_dev",
"eq_code_buildings",
"earthquake_demolish",
"neighborhood_vars", # local accessibility vars
"regional_vars", # regional accessibility vars
"rsh_simulate", # residential sales hedonic for units
"rrh_simulate", # residential rental hedonic for units
"nrh_simulate",
# (based on higher of predicted price or rent)
"assign_tenure_to_new_units",
# uses conditional probabilities
"household_relocation",
"households_transition",
# update building/unit/hh correspondence
"reconcile_unplaced_households",
"jobs_transition",
# we first put Q1 households only into deed-restricted units,
# then any additional unplaced Q1 households, Q2, Q3, and Q4
# households are placed in either deed-restricted units or
# market-rate units
"hlcm_owner_lowincome_simulate",
"hlcm_renter_lowincome_simulate",
# allocate owners to vacant owner-occupied units
"hlcm_owner_simulate",
# allocate renters to vacant rental units
"hlcm_renter_simulate",
# we have to run the hlcm above before this one - we first want
# to try and put unplaced households into their appropraite
# tenured units and then when that fails, force them to place
# using the code below.
# force placement of any unplaced households, in terms of
# rent/own, is a noop except in the final simulation year
# 09 11 2020 ET: enabled for all simulation years
"hlcm_owner_simulate_no_unplaced",
"hlcm_owner_lowincome_simulate_no_unplaced",
# this one crashes right no because there are no unplaced, so
# need to fix the crash in urbansim
# 09 11 2020 ET: appears to be working
"hlcm_renter_simulate_no_unplaced",
"hlcm_renter_lowincome_simulate_no_unplaced",
# update building/unit/hh correspondence
"reconcile_placed_households",
"elcm_simulate",
"price_vars",
# "scheduled_development_events",
"topsheet",
"simulation_validation",
"parcel_summary",
"building_summary",
"geographic_summary",
"travel_model_output",
# "travel_model_2_output",
"hazards_slr_summary",
"hazards_eq_summary",
"diagnostic_output",
"config",
"slack_report"
], iter_vars=[IN_YEAR])
# start the simulation in the next round - only the models above run
# for the IN_YEAR
years_to_run = range(IN_YEAR+EVERY_NTH_YEAR, OUT_YEAR+1,
EVERY_NTH_YEAR)
models = get_simulation_models(SCENARIO)
orca.run(models, iter_vars=years_to_run)
elif MODE == "estimation":
orca.run([
"neighborhood_vars", # local accessibility variables
"regional_vars", # regional accessibility variables
"rsh_estimate", # residential sales hedonic
"nrh_estimate", # non-res rent hedonic
"rsh_simulate",
"nrh_simulate",
"hlcm_estimate", # household lcm
"elcm_estimate", # employment lcm
], iter_vars=[2010])
# Estimation steps
'''
orca.run([
"load_rental_listings", # required to estimate rental hedonic
"neighborhood_vars", # street network accessibility
"regional_vars", # road network accessibility
"rrh_estimate", # estimate residential rental hedonic
"hlcm_owner_estimate", # estimate location choice owners
"hlcm_renter_estimate", # estimate location choice renters
])
'''
elif MODE == "feasibility":
orca.run([
"neighborhood_vars", # local accessibility vars
"regional_vars", # regional accessibility vars
"rsh_simulate", # residential sales hedonic
"nrh_simulate", # non-residential rent hedonic
"price_vars",
"subsidized_residential_feasibility"
], iter_vars=[2010])
# the whole point of this is to get the feasibility dataframe
# for debugging
df = orca.get_table("feasibility").to_frame()
df = df.stack(level=0).reset_index(level=1, drop=True)
df.to_csv("output/feasibility.csv")
else:
raise "Invalid mode"
print("Started", time.ctime())
print("Current Branch : ", BRANCH.rstrip())
print("Current Commit : ", CURRENT_COMMIT.rstrip())
print("Current Scenario : ", orca.get_injectable('scenario').rstrip())
print("Random Seed : ", RANDOM_SEED)
if SLACK and MODE == "simulation":
slack.chat.post_message(
'#urbansim_sim_update',
'Starting simulation %d on host %s (scenario: %s)' %
(run_num, host, SCENARIO), as_user=True)
try:
run_models(MODE, SCENARIO)
except Exception as e:
print(traceback.print_exc())
if SLACK and MODE == "simulation":
slack.chat.post_message(
'#urbansim_sim_update',
'DANG! Simulation failed for %d on host %s'
% (run_num, host), as_user=True)
else:
raise e
sys.exit(0)
print("Finished", time.ctime())
if MAPS and MODE == "simulation" and 'travel_model_output' \
in get_simulation_models(SCENARIO):
files_msg1, files_msg2 = ue_files(run_num)
config_resp = ue_config(run_num, host)
if SLACK and MODE == "simulation":
slack.chat.post_message(
'#urbansim_sim_update',
'Completed simulation %d on host %s' % (run_num, host), as_user=True)
"""slack.chat.post_message(
'#sim_updates',
'Urbanexplorer is available at ' +
'http://urbanforecast.com/sim_explorer%d.html' % run_num, as_user=True)
slack.chat.post_message(
'#sim_updates',
'Final topsheet is available at ' +
'http://urbanforecast.com/runs/run%d_topsheet_2050.log' % run_num,
as_user=True)
slack.chat.post_message(
'#sim_updates',
'Targets comparison is available at ' +
'http://urbanforecast.com/runs/run%d_targets_comparison_2050.csv' %
run_num, as_user=True)"""
summary = ""
if MODE == "simulation" and COMPARE_AGAINST_LAST_KNOWN_GOOD:
# compute and write the difference report at the superdistrict level
prev_run = LAST_KNOWN_GOOD_RUNS[SCENARIO]
# fetch the previous run off of the internet for comparison - the "last
# known good run" should always be available on EC2
df1 = pd.read_csv(("http://urbanforecast.com/runs/run%d_superdistrict" +
"_summaries_2050.csv") % prev_run)
df1 = df1.set_index(df1.columns[0]).sort_index()
df2 = pd.read_csv("runs/run%d_superdistrict_summaries_2050.csv" % run_num)
df2 = df2.set_index(df2.columns[0]).sort_index()
supnames = \
pd.read_csv("data/superdistricts.csv", index_col="number").name
summary = compare_summary(df1, df2, supnames)
with open("runs/run%d_difference_report.log" % run_num, "w") as f:
f.write(summary)
if SLACK and MODE == "simulation" and COMPARE_AGAINST_LAST_KNOWN_GOOD:
if len(summary.strip()) != 0:
sum_lines = len(summary.strip().split("\n"))
slack.chat.post_message(
'#urbansim_sim_update',
('Difference report is available at ' +
'http://urbanforecast.com/runs/run%d_difference_report.log ' +
'- %d line(s)') % (run_num, sum_lines),
as_user=True)
else:
slack.chat.post_message(
'#urbansim_sim_update',
"No differences with reference run.",
as_user=True)
if S3:
os.system('ls runs/run%d_* ' % run_num +
'| xargs -I file aws s3 cp file ' +
's3://bayarea-urbansim-results')