-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerators.py
233 lines (177 loc) · 7.12 KB
/
generators.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
from __future__ import print_function
import numpy as np
import pandas as pd
import orca
from urbansim.utils import misc
from urbansim.models import util
try:
import pandana
except ImportError:
pass
def make_agg_var(agent, geog, geog_id, var_to_aggregate, agg_function, how_fillna=None):
"""
Generator function for aggregation variables. Registers with orca.
"""
var_name = agg_function + '_' + var_to_aggregate
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent)
print('Calculating {} of {} for {}'
.format(var_name, agent, geog))
groupby = agents[var_to_aggregate].groupby(agents[geog_id])
if agg_function == 'mean':
values = groupby.mean().fillna(0)
if agg_function == 'median':
values = groupby.median().fillna(0)
if agg_function == 'std':
values = groupby.std().fillna(0)
if agg_function == 'sum':
values = groupby.sum().fillna(0)
if agg_function == 'max':
values = groupby.max().fillna(0)
if agg_function == 'min':
values = groupby.min().fillna(0)
locations_index = orca.get_table(geog).index
series = pd.Series(data=values, index=locations_index)
# Fillna.
# For certain functions, must add other options,
# like puma value or neighboring value
if how_fillna is not None:
series = how_fillna(series)
else:
if agg_function == 'sum':
series = series.fillna(0)
else:
series = series.fillna(method='ffill')
series = series.fillna(method='bfill')
return series
return func
def make_disagg_var(from_geog_name, to_geog_name, var_to_disaggregate,
from_geog_id_name, name_based_on_geography=True):
"""
Generator function for disaggregating variables. Registers with orca.
"""
if name_based_on_geography:
var_name = from_geog_name + '_' + var_to_disaggregate
else:
var_name = var_to_disaggregate
@orca.column(to_geog_name, var_name, cache=True, cache_scope='iteration')
def func():
print('Disaggregating {} to {} from {}'
.format(var_to_disaggregate, to_geog_name, from_geog_name))
from_geog = orca.get_table(from_geog_name)
to_geog = orca.get_table(to_geog_name)
return misc.reindex(from_geog[var_to_disaggregate],
to_geog[from_geog_id_name]).fillna(0)
return func
def make_size_var(agent, geog, geog_id, cache=True, cache_scope='step', prefix_agent='total'):
"""
Generator function for size variables. Registers with orca.
"""
var_name = prefix_agent + '_' + agent
@orca.column(geog, var_name, cache=cache, cache_scope=cache_scope)
def func():
agents = orca.get_table(agent)
print('Calculating number of {} for {}'.format(agent, geog))
size = agents[geog_id].value_counts()
locations_index = orca.get_table(geog).index
series = pd.Series(data=size, index=locations_index)
series = series.fillna(0)
return series
return func
def make_proportion_var(agent, geog, geog_id, target_variable, target_value, prefix_agent='total'):
"""
Generator function for proportion variables. Registers with orca.
"""
try:
var_name = 'prop_%s_%s' % (target_variable, int(target_value))
except Exception:
var_name = 'prop_%s_%s' % (target_variable, target_value)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent).to_frame(
columns=[target_variable, geog_id])
locations = orca.get_table(geog)
print('Calculating proportion {} {} for {}'
.format(target_variable, target_value, geog))
agent_subset = agents[agents[target_variable] == target_value]
series = (agent_subset.groupby(geog_id).size()
* 1.0
/ locations[prefix_agent + '_' + agent])
series = series.fillna(0)
return series
return func
def make_dummy_variable(agent, geog_var, geog_id):
"""
Generator function for spatial dummy. Registers with orca.
"""
# cache_scope
try:
var_name = geog_var + '_is_' + str(geog_id)
except Exception:
var_name = geog_var + '_is_' + str(int(geog_id))
@orca.column(agent, var_name, cache=True, cache_scope='iteration')
def func():
agents = orca.get_table(agent)
return (agents[geog_var] == geog_id).astype('int32')
return func
def make_ratio_var(agent1, agent2, geog, prefix1='total', prefix2='total'):
"""
Generator function for ratio variables. Registers with orca.
"""
var_name = 'ratio_%s_to_%s' % (agent1, agent2)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
locations = orca.get_table(geog)
print('Calculating ratio of {} to {} for {}'
.format(agent1, agent2, geog))
series = (locations[prefix1 + '_' + agent1]
* 1.0
/ (locations[prefix2 + '_' + agent2] + 1.0))
series = series.fillna(0)
return series
return func
def make_density_var(agent, geog, prefix_agent='total'):
"""
Generator function for density variables. Registers with orca.
"""
var_name = 'density_%s' % (agent)
@orca.column(geog, var_name, cache=True, cache_scope='iteration')
def func():
locations = orca.get_table(geog)
print('Calculating density of {} for {}'.format(agent, geog))
series = locations[prefix_agent + '_' + agent] * 1.0 / (
locations['sum_acres'] + 1.0)
series = series.fillna(0)
return series
return func
def make_access_var(name, agent, target_variable=False, target_value=False,
radius=1000, agg_function='sum', decay='flat', log=True,
filters=False):
"""
Generator function for accessibility variables. Registers with orca.
"""
@orca.column('nodes', name, cache=True, cache_scope='iteration')
def func(net):
print('Calculating {}'.format(name))
nodes = pd.DataFrame(index=net.node_ids)
flds = [target_variable] if target_variable else []
if target_value:
flds += util.columns_in_filters(
["{} == {}".format(target_variable, target_value)])
if filters:
flds += util.columns_in_filters(filters)
flds.append('node_id')
df = orca.get_table(agent).to_frame(flds)
if target_value:
df = util.apply_filter_query(df, [
"{} == {}".format(target_variable, target_value)])
if filters:
df = util.apply_filter_query(df, filters)
net.set(df['node_id'],
variable=df[target_variable] if target_variable else None)
nodes[name] = net.aggregate(radius, type=agg_function, decay=decay)
if log:
nodes[name] = nodes[name].apply(eval('np.log1p'))
return nodes[name]
return func