-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathtest_model_builder.py
354 lines (290 loc) · 11.6 KB
/
test_model_builder.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
# Copyright 2024 The PyMC Labs Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright 2023 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hashlib
import json
import sys
import tempfile
import numpy as np
import pandas as pd
import pymc as pm
import pytest
import xarray as xr
from pymc_marketing.model_builder import ModelBuilder
@pytest.fixture(scope="module")
def toy_X():
x = np.linspace(start=0, stop=1, num=100)
return pd.DataFrame({"input": x})
@pytest.fixture(scope="module")
def toy_y(toy_X):
rng = np.random.default_rng(42)
y = 5 * toy_X["input"] + 3
y = y + rng.normal(0, 1, size=len(toy_X))
y = pd.Series(y, name="output")
return y
@pytest.fixture(scope="module")
def fitted_model_instance(toy_X):
sampler_config = {
"draws": 100,
"tune": 100,
"chains": 2,
"target_accept": 0.95,
}
model_config = {
"a": {"loc": 0, "scale": 10, "dims": ("numbers",)},
"b": {"loc": 0, "scale": 10},
"obs_error": 2,
}
model = ModelBuilderTest(
model_config=model_config,
sampler_config=sampler_config,
test_parameter="test_paramter",
)
model.fit(
toy_X,
chains=1,
draws=100,
tune=100,
)
return model
@pytest.fixture(scope="module")
def not_fitted_model_instance():
sampler_config = {"draws": 100, "tune": 100, "chains": 2, "target_accept": 0.95}
model_config = {
"a": {"loc": 0, "scale": 10, "dims": ("numbers",)},
"b": {"loc": 0, "scale": 10},
"obs_error": 2,
}
return ModelBuilderTest(
model_config=model_config,
sampler_config=sampler_config,
test_parameter="test_paramter",
)
class ModelBuilderTest(ModelBuilder):
def __init__(self, model_config=None, sampler_config=None, test_parameter=None):
self.test_parameter = test_parameter
super().__init__(model_config=model_config, sampler_config=sampler_config)
_model_type = "test_model"
version = "0.1"
def build_model(self, X: pd.DataFrame, y: pd.Series, model_config=None):
coords = {"numbers": np.arange(len(X))}
self._generate_and_preprocess_model_data(X, y)
with pm.Model(coords=coords) as self.model:
if model_config is None:
model_config = self.default_model_config
x = pm.MutableData("x", self.X["input"].values)
y_data = pm.MutableData("y_data", self.y)
# prior parameters
a_loc = model_config["a"]["loc"]
a_scale = model_config["a"]["scale"]
b_loc = model_config["b"]["loc"]
b_scale = model_config["b"]["scale"]
obs_error = model_config["obs_error"]
# priors
a = pm.Normal("a", a_loc, sigma=a_scale, dims=model_config["a"]["dims"])
b = pm.Normal("b", b_loc, sigma=b_scale)
obs_error = pm.HalfNormal("σ_model_fmc", obs_error)
# observed data
pm.Normal("output", a + b * x, obs_error, shape=x.shape, observed=y_data)
def _save_input_params(self, idata):
idata.attrs["test_paramter"] = json.dumps(self.test_parameter)
@property
def output_var(self):
return "output"
def _data_setter(self, X: pd.DataFrame, y: pd.Series = None):
with self.model:
pm.set_data({"x": X["input"].values})
if y is not None:
y = y.values if isinstance(y, pd.Series) else y
pm.set_data({"y_data": y})
@property
def _serializable_model_config(self):
return self.model_config
def _generate_and_preprocess_model_data(self, X: pd.DataFrame, y: pd.Series):
self.X = X
self.y = y
@property
def default_model_config(self) -> dict:
return {
"a": {"loc": 0, "scale": 10, "dims": ("numbers",)},
"b": {"loc": 0, "scale": 10},
"obs_error": 2,
}
@property
def default_sampler_config(self) -> dict:
return {
"draws": 1_000,
"tune": 1_000,
"chains": 3,
"target_accept": 0.95,
}
def test_model_and_sampler_config():
default = ModelBuilderTest()
assert default.model_config == default.default_model_config
assert default.sampler_config == default.default_sampler_config
nondefault = ModelBuilderTest(
model_config={"obs_error": 3}, sampler_config={"draws": 42}
)
assert nondefault.model_config != nondefault.default_model_config
assert nondefault.sampler_config != nondefault.default_sampler_config
assert nondefault.model_config == default.model_config | {"obs_error": 3}
assert nondefault.sampler_config == default.sampler_config | {"draws": 42}
def test_save_input_params(fitted_model_instance):
assert fitted_model_instance.idata.attrs["test_paramter"] == '"test_paramter"'
def test_save_load(fitted_model_instance):
rng = np.random.default_rng(42)
temp = tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False)
fitted_model_instance.save(temp.name)
test_builder2 = ModelBuilderTest.load(temp.name)
assert fitted_model_instance.idata.groups() == test_builder2.idata.groups()
assert fitted_model_instance.id == test_builder2.id
x_pred = rng.uniform(low=0, high=1, size=100)
prediction_data = pd.DataFrame({"input": x_pred})
pred1 = fitted_model_instance.predict(prediction_data)
pred2 = test_builder2.predict(prediction_data)
assert pred1.shape == pred2.shape
temp.close()
def test_initial_build_and_fit(fitted_model_instance, check_idata=True) -> ModelBuilder:
if check_idata:
assert fitted_model_instance.idata is not None
assert "posterior" in fitted_model_instance.idata.groups()
def test_save_without_fit_raises_runtime_error():
model_builder = ModelBuilderTest()
with pytest.raises(RuntimeError):
model_builder.save("saved_model")
def test_empty_sampler_config_fit(toy_X, toy_y):
sampler_config = {}
model_builder = ModelBuilderTest(sampler_config=sampler_config)
model_builder.idata = model_builder.fit(
X=toy_X, y=toy_y, chains=1, draws=100, tune=100
)
assert model_builder.idata is not None
assert "posterior" in model_builder.idata.groups()
def test_fit(fitted_model_instance):
rng = np.random.default_rng(42)
assert fitted_model_instance.idata is not None
assert "posterior" in fitted_model_instance.idata.groups()
assert fitted_model_instance.idata.posterior.dims["draw"] == 100
prediction_data = pd.DataFrame({"input": rng.uniform(low=0, high=1, size=100)})
fitted_model_instance.predict(prediction_data)
post_pred = fitted_model_instance.sample_posterior_predictive(
prediction_data, extend_idata=True, combined=True
)
assert (
post_pred[fitted_model_instance.output_var].shape[0]
== prediction_data.input.shape[0]
)
def test_fit_no_t(toy_X):
model_builder = ModelBuilderTest()
model_builder.idata = model_builder.fit(X=toy_X, chains=1, draws=100, tune=100)
assert model_builder.model is not None
assert model_builder.idata is not None
assert "posterior" in model_builder.idata.groups()
@pytest.mark.skipif(
sys.platform == "win32",
reason="Permissions for temp files not granted on windows CI.",
)
def test_predict(fitted_model_instance):
rng = np.random.default_rng(42)
x_pred = rng.uniform(low=0, high=1, size=100)
prediction_data = pd.DataFrame({"input": x_pred})
pred = fitted_model_instance.predict(prediction_data)
# Perform elementwise comparison using numpy
assert type(pred) == np.ndarray
assert len(pred) > 0
@pytest.mark.parametrize("combined", [True, False])
def test_sample_posterior_predictive(fitted_model_instance, combined):
rng = np.random.default_rng(42)
n_pred = 100
x_pred = rng.uniform(low=0, high=1, size=n_pred)
prediction_data = pd.DataFrame({"input": x_pred})
pred = fitted_model_instance.sample_posterior_predictive(
prediction_data, combined=combined, extend_idata=True
)
chains = fitted_model_instance.idata.sample_stats.dims["chain"]
draws = fitted_model_instance.idata.sample_stats.dims["draw"]
expected_shape = (n_pred, chains * draws) if combined else (chains, draws, n_pred)
assert pred[fitted_model_instance.output_var].shape == expected_shape
assert np.issubdtype(pred[fitted_model_instance.output_var].dtype, np.floating)
def test_model_config_formatting():
model_config = {
"a": {
"loc": [0, 0],
"scale": 10,
"dims": [
"x",
],
},
}
model_builder = ModelBuilderTest()
converted_model_config = model_builder._model_config_formatting(model_config)
np.testing.assert_equal(converted_model_config["a"]["dims"], ("x",))
np.testing.assert_equal(converted_model_config["a"]["loc"], np.array([0, 0]))
def test_id():
model_builder = ModelBuilderTest()
expected_id = hashlib.sha256(
str(model_builder.model_config.values()).encode()
+ model_builder.version.encode()
+ model_builder._model_type.encode()
).hexdigest()[:16]
assert model_builder.id == expected_id
@pytest.mark.parametrize("name", ["prior_predictive", "posterior_predictive"])
def test_sample_xxx_predictive_keeps_second(
fitted_model_instance, toy_X, name: str
) -> None:
rng = np.random.default_rng(42)
method_name = f"sample_{name}"
method = getattr(fitted_model_instance, method_name)
X_pred = toy_X
kwargs = {
"X_pred": X_pred,
"combined": False,
"extend_idata": True,
"random_seed": rng,
}
first_sample = method(**kwargs)
second_sample = method(**kwargs)
with pytest.raises(AssertionError):
xr.testing.assert_allclose(first_sample, second_sample)
sample = getattr(fitted_model_instance.idata, name)
xr.testing.assert_allclose(sample, second_sample)
def test_prediction_kwarg(fitted_model_instance, toy_X):
result = fitted_model_instance.sample_posterior_predictive(
toy_X,
extend_idata=True,
predictions=True,
)
assert "predictions" in fitted_model_instance.idata
assert "predictions_constant_data" in fitted_model_instance.idata
assert isinstance(result, xr.Dataset)
def test_fit_after_prior_keeps_prior(toy_X, toy_y):
model = ModelBuilderTest()
model.sample_prior_predictive(toy_X)
assert "prior" in model.idata
assert "prior_predictive" in model.idata
model.fit(X=toy_X, y=toy_y, chains=1, draws=100, tune=100)
assert "prior" in model.idata
assert "prior_predictive" in model.idata