-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasources.py
234 lines (181 loc) · 7.99 KB
/
datasources.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
import numpy as np
import pandas as pd
import os
from urbansim_defaults import datasources
from urbansim_defaults import utils
from urbansim.utils import misc
import orca
@orca.injectable('building_sqft_per_job', cache=True)
def building_sqft_per_job(settings):
return settings['building_sqft_per_job']
@orca.table('jobs', cache=True)
def jobs(store):
# nets = store['nets']
###go from establishments to jobs
# df = nets.loc[np.repeat(nets.index.values, nets.emp11.values)]\
# .reset_index()
# df.index.name = 'job_id'
df = store['jobs']
return df
# the estimation data is not in the buildings table - they are the same
@orca.table('homesales', cache=True)
def homesales(store):
# we need to read directly from the store here. Why? The buildings
# table itself drops a bunch of columns we need - most notably the
# redfin_sales_price column. Why? Because the developer model will
# append rows (new buildings) to the buildings table and we don't want
# the developer model to know about redfin_sales_price (which is
# meaningless for forecast buildings)
df = store['buildings']
df = df.dropna(subset=["redfin_sale_price"])
df["price_per_sqft"] = df.eval('redfin_sale_price / sqft_per_unit')
df = df.query("sqft_per_unit > 200")
df = df.dropna(subset=["price_per_sqft"])
return df
@orca.column('homesales', 'node_id', cache=True)
def node_id(homesales, parcels):
return misc.reindex(parcels.node_id, homesales.parcel_id)
@orca.column('homesales', 'zone_id', cache=True)
def zone_id(homesales, parcels):
return misc.reindex(parcels.zone_id, homesales.parcel_id)
@orca.column('homesales', cache=True)
def base_price_per_sqft(homesales):
s = homesales.price_per_sqft.groupby(homesales.zone_id).quantile()
return misc.reindex(s, homesales.zone_id)
@orca.column('buildings', cache=True)
def base_price_per_sqft(homesales, buildings):
s = homesales.price_per_sqft.groupby(homesales.zone_id).quantile()
return misc.reindex(s, buildings.zone_id).reindex(buildings.index).fillna(s.quantile())
# non-residential rent data
@orca.table('costar', cache=True)
def costar(store):
df = store['costar']
df = df[df.PropertyType.isin(["Office", "Retail", "Industrial"])]
return df
@orca.table(cache=True)
def zoning_lookup():
return pd.read_csv(os.path.join(misc.data_dir(), "zoning_lookup.csv"),
index_col="id")
# need to reindex from geom id to the id used on parcels
def geom_id_to_parcel_id(df, parcels):
s = parcels.geom_id # get geom_id
s = pd.Series(s.index, index=s.values) # invert series
df["new_index"] = s.loc[df.index] # get right parcel_id for each geom_id
df = df.dropna(subset=["new_index"])
df = df.set_index("new_index", drop=True)
df.index.name = "parcel_id"
return df
# zoning for use in the "baseline" scenario
# comes in the hdf5
@orca.table('zoning_baseline', cache=True)
def zoning_baseline(parcels, zoning_lookup):
df = pd.read_csv(os.path.join(misc.data_dir(), "2015_08_13_zoning_parcels.csv"),
index_col="geom_id")
df = pd.merge(df, zoning_lookup.to_frame(), left_on="zoning_id", right_index=True)
df = geom_id_to_parcel_id(df, parcels)
d = {
"HS": "type1",
"HT": "type2",
"HM": "type3",
"OF": "type4",
"HO": "type5",
"IL": "type7",
"IW": "type8",
"IH": "type9",
"RS": "type10",
"RB": "type11",
"MR": "type12",
"MT": "type13",
"ME": "type14"
}
df.columns = [d.get(x, x) for x in df.columns]
return df
@orca.table('zoning_np', cache=True)
def zoning_np(parcels_geography):
scenario_zoning = pd.read_csv(os.path.join(misc.data_dir(),
'zoning_mods_np.csv'))
return pd.merge(parcels_geography.to_frame(),
scenario_zoning,
on=['jurisdiction', 'pda_id', 'tpp_id', 'exp_id'],
how='left')
# this is really bizarre, but the parcel table I have right now has empty
# zone_ids for a few parcels. Not enough to worry about so just filling with
# the mode
@orca.table('parcels', cache=True)
def parcels(store, net):
df = store['parcels']
df["zone_id"] = df.zone_id.replace(0, 1)
df['_node_id'] = net.get_node_ids(df['x'], df['y'])
cfg = {
"fill_nas": {
"zone_id": {
"how": "mode",
"type": "int"
},
"_node_id": {
"how": "mode",
"type": "int"
},
"shape_area": {
"how": "median",
"type": "float"
}
}
}
df = utils.table_reprocess(cfg, df)
return df
@orca.column('parcels', cache=True)
def pda(parcels, parcels_geography):
return parcels_geography.pda_id.reindex(parcels.index)
@orca.table(cache=True)
def parcels_geography(parcels):
df = pd.read_csv(os.path.join(misc.data_dir(), "2015_08_13_parcels_geography.csv"),
index_col="geom_id")
return geom_id_to_parcel_id(df, parcels)
@orca.table(cache=True)
def development_projects(parcels, settings):
df = pd.read_csv(os.path.join(misc.data_dir(), "development_projects.csv"))
for fld in ['residential_sqft', 'residential_price', 'non_residential_price']:
df[fld] = 0
df["redfin_sale_year"] = 2012 # hedonic doesn't tolerate nans
df["stories"] = df.stories.fillna(1)
df["building_sqft"] = df.building_sqft.fillna(0)
df["non_residential_sqft"] = df.non_residential_sqft.fillna(0)
df["building_type_id"] = df.building_type.map(settings["building_type_map2"])
df = df.dropna(subset=["geom_id"]) # need a geom_id to link to parcel_id
df = df.dropna(subset=["year_built"]) # need a year built to get built
df["geom_id"] = df.geom_id.astype("int")
df = df.query('residential_units != "rent"')
df["residential_units"] = df.residential_units.astype("int")
df = df.set_index("geom_id")
df = geom_id_to_parcel_id(df, parcels).reset_index() # use parcel id
# we don't predict prices for schools and hotels right now
df = df.query("building_type_id <= 4 or building_type_id >= 7")
print "Describe of development projects"
print df[orca.get_table('buildings').local_columns].describe()
return df
@orca.table('buildings', cache=True)
def buildings(store, households, jobs, building_sqft_per_job, settings):
# start with buildings from urbansim_defaults
df = datasources.buildings(store, households, jobs,
building_sqft_per_job, settings)
df = df.drop(['development_type_id', 'improvement_value', 'sqft_per_unit', 'nonres_rent_per_sqft', 'res_price_per_sqft', 'redfin_sale_price', 'redfin_home_type', 'costar_property_type', 'costar_rent'], axis=1)
# set the vacancy rate in each building to 5% for testing purposes
#vacancy = .25
#df["residential_units"] = (households.building_id.value_counts() *
# (1.0+vacancy)).apply(np.floor).astype('int')
df["residential_units"] = df.residential_units.fillna(0)
# BRUTE FORCE INCREASE THE CAPACITY FOR MORE JOBS
print "WARNING: this has the hard-coded version which unrealistically increases non-residential square footage to house all the base year jobs"
df["non_residential_sqft"] = (df.non_residential_sqft * 1.33).astype('int')
# we should only be using the "buildings" table during simulation, and in
# simulation we want to normalize the prices to 2012 style prices
df["redfin_sale_year"] = 2012
return df
# this specifies the relationships between tables
orca.broadcast('parcels_geography', 'buildings', cast_index=True,
onto_on='parcel_id')
orca.broadcast('nodes', 'homesales', cast_index=True, onto_on='node_id')
orca.broadcast('nodes', 'costar', cast_index=True, onto_on='node_id')
orca.broadcast('logsums', 'homesales', cast_index=True, onto_on='zone_id')
orca.broadcast('logsums', 'costar', cast_index=True, onto_on='zone_id')