-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
1052 lines (891 loc) · 38.9 KB
/
utils.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import orca
import numpy as np
import pandas as pd
from urbansim.models import RegressionModel, SegmentedRegressionModel, \
MNLDiscreteChoiceModel, SegmentedMNLDiscreteChoiceModel, \
GrowthRateTransition, transition
from urbansim.models.supplydemand import supply_and_demand
from urbansim.developer import sqftproforma, developer
from urbansim.utils import misc
def conditional_upzone(scenario, scenario_inputs, attr_name, upzone_name):
"""
Parameters
----------
scenario : str
The name of the active scenario (set to "baseline" if no scenario
zoning)
scenario_inputs : dict
Dictionary of scenario options - keys are scenario names and values
are also dictionaries of key-value paris for scenario inputs. Right
now "zoning_table_name" should be set to the table that contains the
scenario based zoning for that scenario
attr_name : str
The name of the attribute in the baseline zoning table
upzone_name : str
The name of the attribute in the scenario zoning table
Returns
-------
The new zoning per parcel which is increased if the scenario based
zoning is higher than the baseline zoning
"""
zoning_baseline = orca.get_table(
scenario_inputs["baseline"]["zoning_table_name"])
attr = zoning_baseline[attr_name]
if scenario != "baseline":
zoning_scenario = orca.get_table(
scenario_inputs[scenario]["zoning_table_name"])
upzone = zoning_scenario[upzone_name].dropna()
attr = pd.concat([attr, upzone], axis=1).max(skipna=True, axis=1)
return attr
def enable_logging():
"""
A quick shortcut to enable logging at log level INFO
"""
from urbansim.utils import logutil
logutil.set_log_level(logutil.logging.INFO)
logutil.log_to_stream()
def check_nas(df):
"""
Checks for nas and errors if they are found (also prints a report on how
many nas are found in each column)
Parameters
----------
df : DataFrame
DataFrame to check for nas
Returns
-------
Nothing
"""
df_cnt = len(df)
fail = False
df = df.replace([np.inf, -np.inf], np.nan)
for col in df.columns:
s_cnt = df[col].count()
if df_cnt != s_cnt:
fail = True
print "Found %d nas or inf (out of %d) in column %s" % \
(df_cnt-s_cnt, df_cnt, col)
assert not fail, "NAs were found in dataframe, please fix"
def table_reprocess(cfg, df):
"""
Reprocesses a table with the given configuration, mainly by filling nas
with the given configuration.
Parameters
----------
cfg : dict
The configuration is specified as a nested dictionary, javascript
style, and a simple config is given below. Most parameters should be
fairly self-explanatory. "filter" filters the dataframe using the
query command in Pandas. The "fill_nas" parameter is another
dictionary which takes each column and specifies how to fill nas -
options include "drop", "zero", "median", "mode", and "mean". The
"type" must also be specified since items like "median" usually
return floats but the result is often desired to be an "int" - the
type is thus specified to avoid ambiguity.::
{
"filter": "building_type_id >= 1 and building_type_id <= 14",
"fill_nas": {
"building_type_id": {
"how": "drop",
"type": "int"
},
"residential_units": {
"how": "zero",
"type": "int"
},
"year_built": {
"how": "median",
"type": "int"
},
"building_type_id": {
"how": "mode",
"type": "int"
}
}
}
df : DataFrame to process
Returns
-------
New DataFrame which is reprocessed according the configuration
"""
df_cnt = len(df)
if "filter" in cfg:
df = df.query(cfg["filter"])
assert "fill_nas" in cfg
cfg = cfg["fill_nas"]
for fname in cfg:
filltyp, dtyp = cfg[fname]["how"], cfg[fname]["type"]
s_cnt = df[fname].count()
fill_cnt = df_cnt - s_cnt
if filltyp == "zero":
val = 0
elif filltyp == "mode":
val = df[fname].dropna().value_counts().idxmax()
elif filltyp == "median":
val = df[fname].dropna().quantile()
elif filltyp == "mean":
val = df[fname].dropna().mean()
elif filltyp == "drop":
df = df.dropna(subset=[fname])
else:
assert 0, "Fill type not found!"
print "Filling column {} with value {} ({} values)".\
format(fname, val, fill_cnt)
df[fname] = df[fname].fillna(val).astype(dtyp)
return df
def to_frame(tbl, join_tbls, cfg, additional_columns=[]):
"""
Leverage all the built in functionality of the sim framework to join to
the specified tables, only accessing the columns used in cfg (the model
yaml configuration file), an any additionally passed columns (the sim
framework is smart enough to figure out which table to grab the column
off of)
Parameters
----------
tbl : DataFrameWrapper
The table to join other tables to
join_tbls : list of DataFrameWrappers or strs
A list of tables to join to "tbl"
cfg : str
The filename of a yaml configuration file from which to parse the
strings which are actually used by the model
additional_columns : list of strs
A list of additional columns to include
Returns
-------
A single DataFrame with the index from tbl and the columns used by cfg
and any additional columns specified
"""
join_tbls = join_tbls if isinstance(join_tbls, list) else [join_tbls]
tables = [tbl] + join_tbls
cfg = yaml_to_class(cfg).from_yaml(str_or_buffer=cfg)
tables = [t for t in tables if t is not None]
columns = misc.column_list(tables, cfg.columns_used()) + additional_columns
if len(tables) > 1:
df = orca.merge_tables(target=tables[0].name,
tables=tables, columns=columns)
else:
df = tables[0].to_frame(columns)
check_nas(df)
return df
def yaml_to_class(cfg):
"""
Convert the name of a yaml file and get the Python class of the model
associated with the configuration
Parameters
----------
cfg : str
The name of the yaml configuration file
Returns
-------
Nothing
"""
import yaml
model_type = yaml.load(open(cfg))["model_type"]
return {
"regression": RegressionModel,
"segmented_regression": SegmentedRegressionModel,
"discretechoice": MNLDiscreteChoiceModel,
"segmented_discretechoice": SegmentedMNLDiscreteChoiceModel
}[model_type]
def hedonic_estimate(cfg, tbl, join_tbls):
"""
Estimate the hedonic model for the specified table
Parameters
----------
cfg : string
The name of the yaml config file from which to read the hedonic model
tbl : DataFrameWrapper
A dataframe for which to estimate the hedonic
join_tbls : list of strings
A list of land use dataframes to give neighborhood info around the
buildings - will be joined to the buildings using existing broadcasts
"""
cfg = misc.config(cfg)
df = to_frame(tbl, join_tbls, cfg)
return yaml_to_class(cfg).fit_from_cfg(df, cfg)
def hedonic_simulate(cfg, tbl, join_tbls, out_fname):
"""
Simulate the hedonic model for the specified table
Parameters
----------
cfg : string
The name of the yaml config file from which to read the hedonic model
tbl : DataFrameWrapper
A dataframe for which to estimate the hedonic
join_tbls : list of strings
A list of land use dataframes to give neighborhood info around the
buildings - will be joined to the buildings using existing broadcasts
out_fname : string
The output field name (should be present in tbl) to which to write
the resulting column to
"""
cfg = misc.config(cfg)
df = to_frame(tbl, join_tbls, cfg)
price_or_rent, _ = yaml_to_class(cfg).predict_from_cfg(df, cfg)
tbl.update_col_from_series(out_fname, price_or_rent)
def lcm_estimate(cfg, choosers, chosen_fname, buildings, join_tbls):
"""
Estimate the location choices for the specified choosers
Parameters
----------
cfg : string
The name of the yaml config file from which to read the location
choice model
choosers : DataFrameWrapper
A dataframe of agents doing the choosing
chosen_fname : str
The name of the column (present in choosers) which contains the ids
that identify the chosen alternatives
buildings : DataFrameWrapper
A dataframe of buildings which the choosers are locating in and which
have a supply.
join_tbls : list of strings
A list of land use dataframes to give neighborhood info around the
buildings - will be joined to the buildings using existing broadcasts
"""
cfg = misc.config(cfg)
choosers = to_frame(choosers, [], cfg, additional_columns=[chosen_fname])
alternatives = to_frame(buildings, join_tbls, cfg)
return yaml_to_class(cfg).fit_from_cfg(choosers,
chosen_fname,
alternatives,
cfg)
def lcm_simulate(cfg, choosers, buildings, join_tbls, out_fname,
supply_fname, vacant_fname,
enable_supply_correction=None):
"""
Simulate the location choices for the specified choosers
Parameters
----------
cfg : string
The name of the yaml config file from which to read the location
choice model
choosers : DataFrameWrapper
A dataframe of agents doing the choosing
buildings : DataFrameWrapper
A dataframe of buildings which the choosers are locating in and which
have a supply
join_tbls : list of strings
A list of land use dataframes to give neighborhood info around the
buildings - will be joined to the buildings using existing broadcasts.
out_fname : string
The column name to write the simulated location to
supply_fname : string
The string in the buildings table that indicates the amount of
available units there are for choosers, vacant or not
vacant_fname : string
The string in the buildings table that indicates the amount of vacant
units there will be for choosers
enable_supply_correction : Python dict
Should contain keys "price_col" and "submarket_col" which are set to
the column names in buildings which contain the column for prices and
an identifier which segments buildings into submarkets
"""
cfg = misc.config(cfg)
choosers_df = to_frame(choosers, [], cfg, additional_columns=[out_fname])
additional_columns = [supply_fname, vacant_fname]
if enable_supply_correction is not None and \
"submarket_col" in enable_supply_correction:
additional_columns += [enable_supply_correction["submarket_col"]]
if enable_supply_correction is not None and \
"price_col" in enable_supply_correction:
additional_columns += [enable_supply_correction["price_col"]]
locations_df = to_frame(buildings, join_tbls, cfg,
additional_columns=additional_columns)
available_units = buildings[supply_fname]
vacant_units = buildings[vacant_fname]
print "There are %d total available units" % available_units.sum()
print " and %d total choosers" % len(choosers)
print " but there are %d overfull buildings" % \
len(vacant_units[vacant_units < 0])
vacant_units = vacant_units[vacant_units > 0]
# sometimes there are vacant units for buildings that are not in the
# locations_df, which happens for reasons explained in the warning below
indexes = np.repeat(vacant_units.index.values,
vacant_units.values.astype('int'))
isin = pd.Series(indexes).isin(locations_df.index)
missing = len(isin[isin == False])
indexes = indexes[isin.values]
units = locations_df.loc[indexes].reset_index()
check_nas(units)
print " for a total of %d temporarily empty units" % vacant_units.sum()
print " in %d buildings total in the region" % len(vacant_units)
if missing > 0:
print "WARNING: %d indexes aren't found in the locations df -" % \
missing
print " this is usually because of a few records that don't join "
print " correctly between the locations df and the aggregations tables"
movers = choosers_df[choosers_df[out_fname] == -1]
print "There are %d total movers for this LCM" % len(movers)
if enable_supply_correction is not None:
assert isinstance(enable_supply_correction, dict)
assert "price_col" in enable_supply_correction
price_col = enable_supply_correction["price_col"]
assert "submarket_col" in enable_supply_correction
submarket_col = enable_supply_correction["submarket_col"]
lcm = yaml_to_class(cfg).from_yaml(str_or_buffer=cfg)
if enable_supply_correction.get("warm_start", False) is True:
raise NotImplementedError()
multiplier_func = enable_supply_correction.get("multiplier_func", None)
if multiplier_func is not None:
multiplier_func = orca.get_injectable(multiplier_func)
kwargs = enable_supply_correction.get('kwargs', {})
new_prices, submarkets_ratios = supply_and_demand(
lcm,
movers,
units,
submarket_col,
price_col,
base_multiplier=None,
multiplier_func=multiplier_func,
**kwargs)
# we will only get back new prices for those alternatives
# that pass the filter - might need to specify the table in
# order to get the complete index of possible submarkets
submarket_table = enable_supply_correction.get("submarket_table", None)
if submarket_table is not None:
submarkets_ratios = submarkets_ratios.reindex(
orca.get_table(submarket_table).index).fillna(1)
# write final shifters to the submarket_table for use in debugging
orca.get_table(submarket_table)["price_shifters"] = submarkets_ratios
print "Running supply and demand"
print "Simulated Prices"
print buildings[price_col].describe()
print "Submarket Price Shifters"
print submarkets_ratios.describe()
# we want new prices on the buildings, not on the units, so apply
# shifters directly to buildings and ignore unit prices
orca.add_column(buildings.name,
price_col+"_hedonic", buildings[price_col])
new_prices = buildings[price_col] * \
submarkets_ratios.loc[buildings[submarket_col]].values
buildings.update_col_from_series(price_col, new_prices)
print "Adjusted Prices"
print buildings[price_col].describe()
if len(movers) > vacant_units.sum():
print "WARNING: Not enough locations for movers"
print " reducing locations to size of movers for performance gain"
movers = movers.head(vacant_units.sum())
new_units, _ = yaml_to_class(cfg).predict_from_cfg(movers, units, cfg)
# new_units returns nans when there aren't enough units,
# get rid of them and they'll stay as -1s
new_units = new_units.dropna()
# go from units back to buildings
new_buildings = pd.Series(units.loc[new_units.values][out_fname].values,
index=new_units.index)
choosers.update_col_from_series(out_fname, new_buildings)
_print_number_unplaced(choosers, out_fname)
if enable_supply_correction is not None:
new_prices = buildings[price_col]
if "clip_final_price_low" in enable_supply_correction:
new_prices = new_prices.clip(lower=enable_supply_correction[
"clip_final_price_low"])
if "clip_final_price_high" in enable_supply_correction:
new_prices = new_prices.clip(upper=enable_supply_correction[
"clip_final_price_high"])
buildings.update_col_from_series(price_col, new_prices)
vacant_units = buildings[vacant_fname]
print " and there are now %d empty units" % vacant_units.sum()
print " and %d overfull buildings" % len(vacant_units[vacant_units < 0])
def simple_relocation(choosers, relocation_rate, fieldname):
"""
Run a simple rate based relocation model
Parameters
----------
tbl : DataFrameWrapper or DataFrame
Table of agents that might relocate
rate : float
Rate of relocation
location_fname : str
The field name in the resulting dataframe to set to -1 (to unplace
new agents)
Returns
-------
Nothing
"""
print "Total agents: %d" % len(choosers)
_print_number_unplaced(choosers, fieldname)
print "Assigning for relocation..."
chooser_ids = np.random.choice(choosers.index, size=int(relocation_rate *
len(choosers)), replace=False)
choosers.update_col_from_series(fieldname,
pd.Series(-1, index=chooser_ids))
_print_number_unplaced(choosers, fieldname)
def simple_transition(tbl, rate, location_fname):
"""
Run a simple growth rate transition model on the table passed in
Parameters
----------
tbl : DataFrameWrapper
Table to be transitioned
rate : float
Growth rate
location_fname : str
The field name in the resulting dataframe to set to -1 (to unplace
new agents)
Returns
-------
Nothing
"""
transition = GrowthRateTransition(rate)
df = tbl.to_frame(tbl.local_columns)
print "%d agents before transition" % len(df.index)
df, added, copied, removed = transition.transition(df, None)
print "%d agents after transition" % len(df.index)
df.loc[added, location_fname] = -1
orca.add_table(tbl.name, df)
def full_transition(agents, agent_controls, year, settings, location_fname):
"""
Run a transition model based on control totals specified in the usual
UrbanSim way
Parameters
----------
agents : DataFrameWrapper
Table to be transitioned
agent_controls : DataFrameWrapper
Table of control totals
year : int
The year, which will index into the controls
settings : dict
Contains the configuration for the transition model - is specified
down to the yaml level with a "total_column" which specifies the
control total and an "add_columns" param which specified which
columns to add when calling to_frame (should be a list of the columns
needed to do the transition
location_fname : str
The field name in the resulting dataframe to set to -1 (to unplace
new agents)
Returns
-------
Nothing
"""
ct = agent_controls.to_frame()
hh = agents.to_frame(agents.local_columns +
settings['add_columns'])
print "Total agents before transition: {}".format(len(hh))
tran = transition.TabularTotalsTransition(ct, settings['total_column'])
model = transition.TransitionModel(tran)
new, added_hh_idx, new_linked = model.transition(hh, year)
new.loc[added_hh_idx, location_fname] = -1
print "Total agents after transition: {}".format(len(new))
orca.add_table(agents.name, new)
def _print_number_unplaced(df, fieldname):
print "Total currently unplaced: %d" % \
df[fieldname].value_counts().get(-1, 0)
def run_feasibility(parcels, parcel_price_callback,
parcel_use_allowed_callback, residential_to_yearly=True,
parcel_filter=None, only_built=True, forms_to_test=None,
config=None, pass_through=[]):
"""
Execute development feasibility on all parcels
Parameters
----------
parcels : DataFrame Wrapper
The data frame wrapper for the parcel data
parcel_price_callback : function
A callback which takes each use of the pro forma and returns a series
with index as parcel_id and value as yearly_rent
parcel_use_allowed_callback : function
A callback which takes each form of the pro forma and returns a series
with index as parcel_id and value and boolean whether the form
is allowed on the parcel
residential_to_yearly : boolean (default true)
Whether to use the cap rate to convert the residential price from total
sales price per sqft to rent per sqft
parcel_filter : string
A filter to apply to the parcels data frame to remove parcels from
consideration - is typically used to remove parcels with buildings
older than a certain date for historical preservation, but is
generally useful
only_built : boolean
Only return those buildings that are profitable - only those buildings
that "will be built"
forms_to_test : list of strings (optional)
Pass the list of the names of forms to test for feasibility - if set to
None will use all the forms available in ProFormaConfig
config : SqFtProFormaConfig configuration object. Optional. Defaults to
None
pass_through : list of strings
Will be passed to the feasibility lookup function - is used to pass
variables from the parcel dataframe to the output dataframe, usually
for debugging
Returns
-------
Adds a table called feasibility to the sim object (returns nothing)
"""
pf = sqftproforma.SqFtProForma(config) if config \
else sqftproforma.SqFtProForma()
df = parcels.to_frame()
if parcel_filter:
df = df.query(parcel_filter)
# add prices for each use
for use in pf.config.uses:
# assume we can get the 80th percentile price for new development
df[use] = parcel_price_callback(use)
# convert from cost to yearly rent
if residential_to_yearly:
df["residential"] *= pf.config.cap_rate
print "Describe of the yearly rent by use"
print df[pf.config.uses].describe()
d = {}
forms = forms_to_test or pf.config.forms
for form in forms:
print "Computing feasibility for form %s" % form
allowed = parcel_use_allowed_callback(form).loc[df.index]
d[form] = pf.lookup(form, df[allowed], only_built=only_built,
pass_through=pass_through)
if residential_to_yearly and "residential" in pass_through:
d[form]["residential"] /= pf.config.cap_rate
far_predictions = pd.concat(d.values(), keys=d.keys(), axis=1)
orca.add_table("feasibility", far_predictions)
def _remove_developed_buildings(old_buildings, new_buildings, unplace_agents):
redev_buildings = old_buildings.parcel_id.isin(new_buildings.parcel_id)
l = len(old_buildings)
drop_buildings = old_buildings[redev_buildings]
old_buildings = old_buildings[np.logical_not(redev_buildings)]
l2 = len(old_buildings)
if l2-l > 0:
print "Dropped {} buildings because they were redeveloped".\
format(l2-l)
for tbl in unplace_agents:
agents = orca.get_table(tbl)
cols = agents.local_columns
if "building_id" not in cols:
# if it's a unit-level model, need to add building_id
# explicitly
cols += ["building_id"]
agents = agents.to_frame(cols)
displaced_agents = agents.building_id.isin(drop_buildings.index)
print "Unplaced {} before: {}".format(tbl, len(agents.query(
"building_id == -1")))
agents.building_id[displaced_agents] = -1
print "Unplaced {} after: {}".format(tbl, len(agents.query(
"building_id == -1")))
orca.add_table(tbl, agents)
return old_buildings
def run_developer(forms, agents, buildings, supply_fname, parcel_size,
ave_unit_size, total_units, feasibility, year=None,
target_vacancy=.1, form_to_btype_callback=None,
add_more_columns_callback=None, max_parcel_size=2000000,
residential=True, bldg_sqft_per_job=400.0,
min_unit_size=400, remove_developed_buildings=True,
unplace_agents=['households', 'jobs'],
num_units_to_build=None):
"""
Run the developer model to pick and build buildings
Parameters
----------
forms : string or list of strings
Passed directly dev.pick
agents : DataFrame Wrapper
Used to compute the current demand for units/floorspace in the area
buildings : DataFrame Wrapper
Used to compute the current supply of units/floorspace in the area
supply_fname : string
Identifies the column in buildings which indicates the supply of
units/floorspace
parcel_size : Series
Passed directly to dev.pick
ave_unit_size : Series
Passed directly to dev.pick - average residential unit size
total_units : Series
Passed directly to dev.pick - total current residential_units /
job_spaces
feasibility : DataFrame Wrapper
The output from feasibility above (the table called 'feasibility')
year : int
The year of the simulation - will be assigned to 'year_built' on the
new buildings
target_vacancy : float
The target vacancy rate - used to determine how much to build
form_to_btype_callback : function
Will be used to convert the 'forms' in the pro forma to
'building_type_id' in the larger model
add_more_columns_callback : function
Takes a dataframe and returns a dataframe - is used to make custom
modifications to the new buildings that get added
max_parcel_size : float
Passed directly to dev.pick - max parcel size to consider
min_unit_size : float
Passed directly to dev.pick - min unit size that is valid
residential : boolean
Passed directly to dev.pick - switches between adding/computing
residential_units and job_spaces
bldg_sqft_per_job : float
Passed directly to dev.pick - specified the multiplier between
floor spaces and job spaces for this form (does not vary by parcel
as ave_unit_size does)
remove_redeveloped_buildings : optional, boolean (default True)
Remove all buildings on the parcels which are being developed on
unplace_agents : optional , list of strings (default ['households', 'jobs'])
For all tables in the list, will look for field building_id and set
it to -1 for buildings which are removed - only executed if
remove_developed_buildings is true
num_units_to_build: optional, int
If num_units_to_build is passed, build this many units rather than
computing it internally by using the length of agents adn the sum of
the relevant supply columin - this trusts the caller to know how to compute
this.
Returns
-------
Writes the result back to the buildings table and returns the new
buildings with available debugging information on each new building
"""
dev = developer.Developer(feasibility.to_frame())
target_units = num_units_to_build or dev.\
compute_units_to_build(len(agents),
buildings[supply_fname].sum(),
target_vacancy)
print "{:,} feasible buildings before running developer".format(
len(dev.feasibility))
new_buildings = dev.pick(forms,
target_units,
parcel_size,
ave_unit_size,
total_units,
max_parcel_size=max_parcel_size,
min_unit_size=min_unit_size,
drop_after_build=True,
residential=residential,
bldg_sqft_per_job=bldg_sqft_per_job)
orca.add_table("feasibility", dev.feasibility)
if new_buildings is None:
return
if len(new_buildings) == 0:
return new_buildings
if year is not None:
new_buildings["year_built"] = year
if not isinstance(forms, list):
# form gets set only if forms is a list
new_buildings["form"] = forms
if form_to_btype_callback is not None:
new_buildings["building_type_id"] = new_buildings.\
apply(form_to_btype_callback, axis=1)
new_buildings["stories"] = new_buildings.stories.apply(np.ceil)
ret_buildings = new_buildings
if add_more_columns_callback is not None:
new_buildings = add_more_columns_callback(new_buildings)
print "Adding {:,} buildings with {:,} {}".\
format(len(new_buildings),
int(new_buildings[supply_fname].sum()),
supply_fname)
print "{:,} feasible buildings after running developer".format(
len(dev.feasibility))
old_buildings = buildings.to_frame(buildings.local_columns)
new_buildings = new_buildings[buildings.local_columns]
if remove_developed_buildings:
old_buildings = \
_remove_developed_buildings(old_buildings, new_buildings, unplace_agents)
all_buildings = dev.merge(old_buildings, new_buildings)
orca.add_table("buildings", all_buildings)
if "residential_units" in orca.list_tables() and residential:
# need to add units to the units table as well
old_units = orca.get_table("residential_units")
old_units = old_units.to_frame(old_units.local_columns)
new_units = pd.DataFrame({
"unit_residential_price": 0,
"num_units": 1,
"deed_restricted": 0,
"unit_num": np.concatenate([np.arange(i) for i in \
new_buildings.residential_units.values]),
"building_id": np.repeat(new_buildings.index.values,
new_buildings.residential_units.\
astype('int32').values)
}).sort(columns=["building_id", "unit_num"]).reset_index(drop=True)
print "Adding {:,} units to the residential_units table".\
format(len(new_units))
all_units = dev.merge(old_units, new_units)
all_units.index.name = "unit_id"
orca.add_table("residential_units", all_units)
return ret_buildings
# pondered returning ret_buildings, new_units but users can get_table
# the units if they want them - better to avoid breaking the api
return ret_buildings
def scheduled_development_events(buildings, new_buildings,
remove_developed_buildings=True,
unplace_agents=['households', 'jobs']):
"""
This acts somewhat like developer, but is not dependent on real estate feasibility
in order to build - these are buildings that we force to be built, usually because
we know they are scheduled to be built at some point in the future because of our
knowledge of existing permits (or maybe we just read the newspaper).
Parameters
----------
buildings : DataFrame wrapper
Just pass in the building dataframe wrapper
new_buildings : DataFrame
The new buildings to add to out buildings table. They should have the same
columns as the local columns in the buildings table.
"""
print "Adding {:,} buildings as scheduled development events".format(
len(new_buildings))
old_buildings = buildings.to_frame(buildings.local_columns)
new_buildings = new_buildings[buildings.local_columns]
print "Res units before: {:,}".format(old_buildings.residential_units.sum())
print "Non-res sqft before: {:,}".format(old_buildings.non_residential_sqft.sum())
if remove_developed_buildings:
old_buildings = \
_remove_developed_buildings(old_buildings, new_buildings, unplace_agents)
all_buildings = developer.Developer.merge(old_buildings, new_buildings)
print "Res units after: {:,}".format(all_buildings.residential_units.sum())
print "Non-res sqft after: {:,}".format(all_buildings.non_residential_sqft.sum())
orca.add_table("buildings", all_buildings)
return new_buildings
class SimulationSummaryData(object):
"""
Keep track of zone-level and parcel-level output for use in the
simulation explorer. Writes the correct format and filenames that the
simulation explorer expects.
Parameters
----------
run_number : int
The run number for this run
zone_indicator_file : optional, str
A template for the zone_indicator_filename - use {} notation and the
run_number will be substituted. Should probably not be modified if
using the simulation explorer.
parcel_indicator_file : optional, str
A template for the parcel_indicator_filename - use {} notation and the
run_number will be substituted. Should probably not be modified if
using the simulation explorer.
"""
def __init__(self,
run_number,
zone_indicator_file="runs/run{}_simulation_output.json",
parcel_indicator_file="runs/run{}_parcel_output.csv"):
self.run_num = run_number
self.zone_indicator_file = zone_indicator_file.format(run_number)
self.parcel_indicator_file = \
parcel_indicator_file.format(run_number)
self.parcel_output = None
self.zone_output = None
def add_zone_output(self, zones_df, name, year, round=2):
"""
Pass in a dataframe and this function will store the results in the
simulation state to write out at the end (to describe how the simulation
changes over time)
Parameters
----------
zones_df : DataFrame
dataframe of indicators whose index is the zone_id and columns are
indicators describing the simulation
name : string
The name of the dataframe to use to differentiate all the sources of
the indicators
year : int
The year to associate with these indicators
round : int
The number of decimal places to round to in the output json
Returns
-------
Nothing
"""
# this creates a hierarchical json data structure to encapsulate
# zone-level indicators over the simulation years. "index" is the ids
# of the shapes that this will be joined to and "year" is the list of
# years. Each indicator then get put under a two-level dictionary of
# column name and then year. this is not the most efficient data
# structure but since the number of zones is pretty small, it is a
# simple and convenient data structure
if self.zone_output is None:
d = {
"index": list(zones_df.index),
"years": []
}
else:
d = self.zone_output
assert d["index"] == list(zones_df.index), "Passing in zones " \
"dataframe that is not aligned on the same index as a previous " \
"dataframe"
if year not in d["years"]:
d["years"].append(year)
for col in zones_df.columns:
d.setdefault(col, {})
d[col]["original_df"] = name
s = zones_df[col]
dtype = s.dtype
if dtype == "float64" or dtype == "float32":
s = s.fillna(0)
d[col][year] = [float(x) for x in list(s.round(round))]
elif dtype == "int64" or dtype == "int32":
s = s.fillna(0)
d[col][year] = [int(x) for x in list(s)]
else:
d[col][year] = list(s)
self.zone_output = d
def add_parcel_output(self, new_parcel_output):
"""
Add new parcel-level indicators to the parcel output.
Parameters
----------
new_parcel_output : DataFrame
Adds a new set of parcel data for output exploration - this data
is merged with previous data that has been added. This data is
generally used to capture new developments that UrbanSim has
predicted, thus it doesn't override previous years' indicators
Returns
-------
Nothing
"""
if new_parcel_output is None:
return
if self.parcel_output is not None:
# merge with old parcel output
self.parcel_output = \
pd.concat([self.parcel_output, new_parcel_output]).\
reset_index(drop=True)
else:
self.parcel_output = new_parcel_output
def write_parcel_output(self,
add_xy=None):
"""
Write the parcel-level output to a csv file
Parameters
----------