-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
726 lines (616 loc) · 28.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
import numpy as np
import pandas as pd
import os
import orca_wfrc.orca as sim
from simpledbf import Dbf5
from urbansim_wfrc.utils import misc
from urbansim_wfrc.developer import sqftproforma, developer
from urbansim_wfrc.models import SegmentedMNLLocationChoiceModel
from urbansim_defaults import utils
#import WFRCDeveloper
import json
def get_run_no():
if 'run_no' not in sim.list_injectables():
sim.add_injectable("run_no", misc.get_run_number())
return sim.get_injectable("run_no")
def get_run_filename():
if 'run_no' not in sim.list_injectables():
get_run_no()
return os.path.join(misc.runs_dir(), "run%d.h5" % sim.get_injectable("run_no"))
## not used, however, leaving this here just in case
#def df2dbf(df, dbf_path):
#type2spec = {int: ('N', 20, 0),
#np.int64: ('N', 20, 0),
#np.int32: ('N', 20, 0),
#float: ('N', 36, 15),
#np.float32: ('N', 36, 15),
#np.float64: ('N', 36, 15),
#str: ('C', 14, 0)
#}
#types = [type(df[i].iloc[0]) for i in df.columns]
#specs = [type2spec[t] for t in types]
#db = ps.open(dbf_path, 'w')
#db.header = list(df.columns)
#db.field_spec = specs
#for i, row in df.T.iteritems():
#db.write(row)
#db.close()
def dbf2df(dbf_path, index=None, cols=False, incl_index=False):
dbf = Dbf5(dbf_path)
df = dbf.to_dataframe()
if cols:
if incl_index:
cols.append(index)
df = df[cols].copy()
if index:
df.set_index(index, inplace=True)
return df
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)
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# choosers.to_frame().to_csv(str(year) + 'choosers_fromlcm0.csv')
choosers_df = utils.to_frame(choosers, [], cfg, additional_columns=[out_fname])
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# choosers_df.to_csv(str(year) + 'choosers_fromlcm1.csv')
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 = utils.to_frame(buildings, join_tbls, cfg,
additional_columns=additional_columns)
# if cfg == '.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# locations_df.to_csv(str(year) + 'locations_df.csv')
# buildings.to_frame().to_csv(str(year) + 'buildings.csv')
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()
utils.check_nas(units)
# if cfg == '.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# units.to_csv(str(year) + 'units_id.csv')
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")
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# choosers_df.to_csv(str(year) + 'choosers_dfbeforemover.csv')
movers = choosers_df[choosers_df[out_fname] == -1]
print("There are %d total movers for this LCM" % len(movers))
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# movers.to_csv(str(year) + 'movers_jsutfromChooser.csv')
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 = utils.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 = sim.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(
sim.get_table(submarket_table).index).fillna(1)
# write final shifters to the submarket_table for use in debugging
sim.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
sim.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())
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# units.to_csv(str(year)+'units_id_beforeplugin.csv')
# movers.to_csv(str(year) + 'movers_beforeplugin.csv')
# print('current cfg: ', cfg)
new_units, _ = utils.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
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# new_units.to_csv(str(year)+'new_unitsNA_id.csv')
new_units = new_units.dropna()
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# new_units.to_csv(str(year)+'new_units_id.csv')
# go from units back to buildings
new_buildings = pd.Series(units.loc[new_units.values][out_fname].values,
index=new_units.index)
# if cfg =='.\configs\elcm_davis.yaml':
# year = sim.get_injectable('year')
# new_buildings.to_csv(str(year)+'newbuilding_id.csv')
# if (cfg =='.\configs\elcm_davis.yaml') and (sim.get_injectable('year') > 2022):
# new_buildings = new_buildings[~new_buildings.index.duplicated(keep='first')]
choosers.update_col_from_series(out_fname, new_buildings)
utils._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 run_developer(forms, agents, buildings, buildings_all, 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=34647265,
residential=True, bldg_sqft_per_job=400.0,
min_unit_size=400, remove_developed_buildings=True,
unplace_agents=['households', 'jobs']):
"""
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
buildings_all:
Buildings for the entire region, used to write back to buildings table
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
Returns
-------
Writes the result back to the buildings table and returns the new
buildings with available debugging information on each new building
"""
if year == 2019:
target_vacancy = target_vacancy / 3
dev = developer.Developer(feasibility.to_frame())
#dev = WFRCDeveloper.WFRCDeveloper(feasibility.to_frame())
target_units = 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)
sim.add_table("feasibility", dev.feasibility)
year = sim.get_injectable('year')
if new_buildings is None:
return
if len(new_buildings) == 0:
return new_buildings
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)
new_buildings["note"] = "simulated"
ret_buildings = new_buildings
if add_more_columns_callback is not None:
new_buildings = add_more_columns_callback(new_buildings)
if year is not None:
new_buildings["year_built"] = year
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)
old_buildings_all = buildings_all.to_frame(buildings.local_columns)
new_buildings = new_buildings[buildings.local_columns]
if remove_developed_buildings:
redev_buildings = old_buildings.parcel_id.isin(new_buildings.parcel_id)
redev_buildings_all = old_buildings_all.parcel_id.isin(new_buildings.parcel_id)
l = len(old_buildings)
drop_buildings = old_buildings[redev_buildings]
drop_buildings_all = old_buildings_all[redev_buildings_all]
old_buildings = old_buildings[np.logical_not(redev_buildings)]
old_buildings_all = old_buildings_all[np.logical_not(redev_buildings_all)]
l2 = len(old_buildings)
print("before dropped l:" + str(l))
print("after dropped l2: " + str(l2))
#print redev_buildings
#print drop_buildings
if l2-l > 0:
print("Dropped {} buildings because they were redeveloped".
format(l2 - l))
for tbl in unplace_agents:
agents = sim.get_table(tbl)
agents = agents.to_frame(agents.local_columns)
#displaced_agents = agents.building_id.isin(drop_buildings.index)
displaced_agents = agents.building_id.isin(drop_buildings_all.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"))))
sim.add_table(tbl, agents)
all_buildings = dev.merge(old_buildings_all, new_buildings)
sim.add_table("buildings", all_buildings)
return ret_buildings
def compute_range(travel_data, attr, travel_time_attr, dist, agg=np.sum):
"""
Compute a zone-based accessibility query using the urbansim format
travel data dataframe.
Parameters
----------
travel_data : dataframe
The dataframe of urbansim format travel data. Has from_zone_id as
first index, to_zone_id as second index, and different impedances
between zones as columns.
attr : series
The attr to aggregate. Should be indexed by zone_id and the values
will be aggregated.
travel_time_attr : string
The column name in travel_data to use as the impedance.
dist : float
The max distance to aggregate up to
agg : function, optional, np.sum by default
The numpy function to use for aggregation
"""
travel_data = travel_data.reset_index(level=1)
td_ind = travel_data.groupby('to_zone_id').sum()
travel_data = travel_data[travel_data[travel_time_attr] < dist]
travel_data["attr"] = attr[travel_data.to_zone_id].values
travel_data = travel_data.groupby(level=0).attr.apply(agg)
return pd.merge(td_ind, pd.DataFrame(travel_data), how='left', left_index=True, right_index=True).attr.fillna(0)
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": [int(x) for x in list(zones_df.index)],
"years": []
}
else:
d = self.zone_output
assert d["index"] == [int(x) for x in 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
----------
add_xy : dictionary (optional)
Used to add x, y values to the output - an example dictionary is
pasted below - the parameters should be fairly self explanatory.
Note that from_epsg and to_epsg can be omitted in which case the
coordinate system is not changed. NOTE: pyproj is required
if changing coordinate systems::
{
"xy_table": "parcels",
"foreign_key": "parcel_id",
"x_col": "x",
"y_col": "y",
"from_epsg": 3740,
"to_epsg": 4326
}
Returns
-------
Nothing
"""
if self.parcel_output is None:
return
po = self.parcel_output
if add_xy is not None:
x_name, y_name = add_xy["x_col"], add_xy["y_col"]
xy_joinname = add_xy["foreign_key"]
xy_df = sim.get_table(add_xy["xy_table"])
po[x_name] = misc.reindex(xy_df[x_name], po[xy_joinname])
po[y_name] = misc.reindex(xy_df[y_name], po[xy_joinname])
if "from_epsg" in add_xy and "to_epsg" in add_xy:
import pyproj
p1 = pyproj.Proj('+init=epsg:%d' % add_xy["from_epsg"])
p2 = pyproj.Proj('+init=epsg:%d' % add_xy["to_epsg"])
x2, y2 = pyproj.transform(p1, p2,
po[x_name].values,
po[y_name].values)
po[x_name], po[y_name] = x2, y2
po.to_csv(self.parcel_indicator_file, index_label="development_id")
def write_zone_output(self):
"""
Write the zone-level output to a file.
"""
if self.zone_output is None:
return
outf = open(self.zone_indicator_file, "w")
json.dump(self.zone_output, outf)
outf.close()
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
year = sim.get_injectable('year')
# print('forms: ', forms)
# year = sim.get_injectable('year')
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)
far_predictions['residential'].max_profit = far_predictions['residential'].max_profit / np.power(far_predictions['residential'].max_profit_far * far_predictions['residential'].shape_area, 1)
far_predictions['industrial'].max_profit = far_predictions['industrial'].max_profit / np.power(far_predictions['industrial'].max_profit_far*far_predictions['industrial'].shape_area,1)
far_predictions['retail'].max_profit = far_predictions['retail'].max_profit / np.power(far_predictions['retail'].max_profit_far*far_predictions['retail'].shape_area,1)
far_predictions['office'].max_profit = far_predictions['office'].max_profit / np.power(far_predictions['office'].max_profit_far*far_predictions['office'].shape_area,1)
sim.add_table("feasibility", far_predictions)