-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_large_multinomial_logit.py
285 lines (204 loc) · 7.19 KB
/
test_large_multinomial_logit.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
import orca
import numpy as np
import pandas as pd
import pytest
from choicemodels import MultinomialLogitResults
from urbansim_templates import modelmanager
from urbansim_templates.models import LargeMultinomialLogitStep
from urbansim_templates.utils import validate_template
@pytest.fixture
def orca_session():
d1 = {'oid': np.arange(10),
'obsval': np.random.random(10),
'choice': np.random.choice(np.arange(20), size=10)}
d2 = {'aid': np.arange(20),
'altval': np.random.random(20)}
obs = pd.DataFrame(d1).set_index('oid')
orca.add_table('obs', obs)
alts = pd.DataFrame(d2).set_index('aid')
orca.add_table('alts', alts)
def test_template_validity():
"""
Run the template through the standard validation check.
"""
assert validate_template(LargeMultinomialLogitStep)
def test_observation_sampling(orca_session):
modelmanager.initialize()
m = LargeMultinomialLogitStep()
m.choosers = 'obs'
m.alternatives = 'alts'
m.choice_column = 'choice'
m.model_expression = 'obsval + altval'
m.fit()
assert(len(m.mergedchoicetable.to_frame()) == 200)
m.chooser_sample_size = 5
m.fit()
assert(len(m.mergedchoicetable.to_frame()) == 100)
m.name = 'mnl-test'
modelmanager.register(m)
modelmanager.initialize()
m = modelmanager.get_step('mnl-test')
assert(m.chooser_sample_size == 5)
modelmanager.remove_step('mnl-test')
@pytest.fixture
def data():
num_obs = 100
num_alts = 120
d1 = {'oid': np.arange(num_obs),
'obsval': np.random.random(num_obs),
'choice': np.random.choice(np.arange(num_alts), size=num_obs)}
d2 = {'aid': np.arange(num_alts),
'altval': np.random.random(num_alts)}
obs = pd.DataFrame(d1).set_index('oid')
orca.add_table('obs', obs)
alts = pd.DataFrame(d2).set_index('aid')
orca.add_table('alts', alts)
@pytest.fixture
def m(data):
"""
Build a fitted model.
"""
m = LargeMultinomialLogitStep()
m.choosers = 'obs'
m.alternatives = 'alts'
m.choice_column = 'choice'
m.model_expression = 'obsval + altval'
m.alt_sample_size = 10
m.fit()
return m
def test_property_persistence(m):
"""
Test persistence of properties across registration, saving, and reloading.
"""
m.fit()
m.name = 'my-model'
m.tags = ['tag1']
m.chooser_filters = 'filters1'
m.chooser_sample_size = 100
m.alt_filters = 'filter2'
m.out_choosers = 'choosers2'
m.out_alternatives = 'alts2'
m.out_column = 'choices'
m.out_chooser_filters = 'filters3'
m.out_alt_filters = 'filters4'
m.constrained_choices = True
m.alt_capacity = 'cap'
m.chooser_size = 'size'
m.max_iter = 17
d1 = m.to_dict()
modelmanager.initialize()
modelmanager.register(m)
modelmanager.initialize()
d2 = modelmanager.get_step('my-model').to_dict()
assert d1 == d2
modelmanager.remove_step('my-model')
def test_simulation_unconstrained(m):
"""
Test simulation chooser filters with unconstrained choices.
"""
obs = orca.get_table('obs').to_frame()
obs.loc[:24, 'choice'] = -1
orca.add_table('obs', obs)
m.out_chooser_filters = 'choice == -1'
m.run()
assert len(m.choices) == 25
obs = orca.get_table('obs').to_frame()
assert sum(obs.choice == -1) == 0
assert obs.loc[:24, 'choice'].equals(m.choices)
def test_simulation_single_occupancy(m):
"""
Test simulation of single-occupancy choices.
"""
m.constrained_choices = True
m.run()
obs = orca.get_table('obs').to_frame()
assert len(obs) == len(obs.choice.unique())
def test_simulation_constrained(m):
"""
Test simulation of choices with explicit capacities and sizes.
"""
obs = orca.get_table('obs').to_frame()
obs.loc[:,'choice'] = -1
obs['size'] = np.random.choice([1,2], size=len(obs))
orca.add_table('obs', obs)
alts = orca.get_table('alts').to_frame()
alts['cap'] = np.random.choice([1,2,3], size=len(alts))
orca.add_table('alts', alts)
m.constrained_choices = True
m.alt_capacity = 'cap'
m.chooser_size = 'size'
m.run()
obs = orca.get_table('obs').to_frame()
assert all(~obs.choice.isin([-1]))
def test_simulation_no_valid_choosers(m):
"""
If there are no valid choosers after applying filters, simulation should exit.
"""
m.out_chooser_filters = 'choice == -1'
m.run()
def test_simulation_no_valid_alternatives(m):
"""
If there are no valid alternatives after applying filters, simulation should exit.
"""
m.out_alt_filters = 'altval == -1'
m.run()
def test_output_column_autocreation(m):
"""
Test on-the-fly creation of the output column.
"""
m.out_column = 'potato_chips'
m.run()
assert('potato_chips' in orca.get_table('obs').columns)
assert(m.choices.equals(orca.get_table('obs').to_frame()['potato_chips']))
def test_diagnostic_attributes(data):
"""
Test that diagnostic attributes are available when expected.
"""
m = LargeMultinomialLogitStep()
m.choosers = 'obs'
m.alternatives = 'alts'
m.choice_column = 'choice'
m.model_expression = 'obsval + altval'
m.alt_sample_size = 10
assert(m.model is None)
assert(m.mergedchoicetable is None)
assert(m.probabilities is None)
assert(m.choices is None)
m.fit()
assert(isinstance(m.model, MultinomialLogitResults))
len_mct = len(m.mergedchoicetable.to_frame())
len_obs_alts = len(orca.get_table(m.choosers).to_frame()) * m.alt_sample_size
assert(len_mct == len_obs_alts)
name = m.name
modelmanager.register(m)
modelmanager.initialize()
m = modelmanager.get_step(name)
assert(isinstance(m.model, MultinomialLogitResults))
m.run()
len_mct = len(m.mergedchoicetable.to_frame())
len_probs = len(m.probabilities)
len_choices = len(m.choices)
len_obs = len(orca.get_table(m.choosers).to_frame())
len_obs_alts = len_obs * m.alt_sample_size
assert(len_mct == len_obs_alts)
assert(len_probs == len_obs_alts)
assert(len_choices == len_obs)
modelmanager.remove_step(name)
def test_simulation_join_key_as_filter(m):
"""
This tests that it's possible to use a join key as a both a data filter for one of
the tables, and as a choice column for the model.
This came up because MergedChoiceTable doesn't allow the observations and
alternatives to have any column names in common -- the rationale is to maintain data
traceability by avoiding any of-the-fly renaming or dropped columns.
In the templates, in order to support things like using 'households.building_id' as a
filter column and 'buildings.building_id' as a choice column, we apply the filters
and then drop columns that are no longer needed before merging the tables.
"""
obs = orca.get_table('obs')
obs['aid'] = obs.get_column('choice')
m.out_choosers = 'obs'
m.out_chooser_filters = 'aid > 50'
m.out_alternatives = 'alts'
m.out_column = 'aid'
m.run()