13
13
# limitations under the License.
14
14
15
15
16
- import hashlib
17
- import sys
18
- import tempfile
19
-
20
16
import numpy as np
21
17
import pandas as pd
22
18
import pymc as pm
23
- import pytest
24
19
25
20
from pymc_experimental .model_builder import ModelBuilder
26
21
@@ -30,25 +25,30 @@ class test_ModelBuilder(ModelBuilder):
30
25
version = "0.1"
31
26
32
27
def build_model (self , model_config , data = None ):
33
- if data is not None :
34
- x = pm .MutableData ("x" , data ["input" ].values )
35
- y_data = pm .MutableData ("y_data" , data ["output" ].values )
36
-
37
- # prior parameters
38
- a_loc = model_config ["a_loc" ]
39
- a_scale = model_config ["a_scale" ]
40
- b_loc = model_config ["b_loc" ]
41
- b_scale = model_config ["b_scale" ]
42
- obs_error = model_config ["obs_error" ]
43
-
44
- # priors
45
- a = pm .Normal ("a" , a_loc , sigma = a_scale )
46
- b = pm .Normal ("b" , b_loc , sigma = b_scale )
47
- obs_error = pm .HalfNormal ("σ_model_fmc" , obs_error )
48
-
49
- # observed data
50
- if data is not None :
51
- y_model = pm .Normal ("y_model" , a + b * x , obs_error , shape = x .shape , observed = y_data )
28
+
29
+ self .model_config = model_config
30
+ self .data = data
31
+
32
+ with pm .Model () as self .model :
33
+ if data is not None :
34
+ x = pm .MutableData ("x" , data ["input" ].values )
35
+ y_data = pm .MutableData ("y_data" , data ["output" ].values )
36
+
37
+ # prior parameters
38
+ a_loc = model_config ["a_loc" ]
39
+ a_scale = model_config ["a_scale" ]
40
+ b_loc = model_config ["b_loc" ]
41
+ b_scale = model_config ["b_scale" ]
42
+ obs_error = model_config ["obs_error" ]
43
+
44
+ # priors
45
+ a = pm .Normal ("a" , a_loc , sigma = a_scale )
46
+ b = pm .Normal ("b" , b_loc , sigma = b_scale )
47
+ obs_error = pm .HalfNormal ("σ_model_fmc" , obs_error )
48
+
49
+ # observed data
50
+ if data is not None :
51
+ y_model = pm .Normal ("y_model" , a + b * x , obs_error , shape = x .shape , observed = y_data )
52
52
53
53
def _data_setter (self , data : pd .DataFrame ):
54
54
with self .model :
@@ -57,7 +57,7 @@ def _data_setter(self, data: pd.DataFrame):
57
57
pm .set_data ({"y_data" : data ["output" ].values })
58
58
59
59
@classmethod
60
- def create_sample_input (cls ):
60
+ def create_sample_input (self ):
61
61
x = np .linspace (start = 0 , stop = 1 , num = 100 )
62
62
y = 5 * x + 3
63
63
y = y + np .random .normal (0 , 1 , len (x ))
@@ -101,20 +101,21 @@ def test_fit():
101
101
assert "y_model" in post_pred .keys ()
102
102
103
103
104
+ """
104
105
@pytest.mark.skipif(
105
106
sys.platform == "win32", reason="Permissions for temp files not granted on windows CI."
106
107
)
107
108
def test_save_load():
108
- model = test_ModelBuilder .initial_build_and_fit (False )
109
+ test_builder = test_ModelBuilder.initial_build_and_fit(False)
109
110
temp = tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False)
110
- model .save (temp .name )
111
- model2 = test_ModelBuilder .load (temp .name )
112
- assert model .idata .groups () == model2 .idata .groups ()
111
+ test_builder .save(temp.name)
112
+ test_builder2 = test_ModelBuilder.load(temp.name)
113
+ assert test_builder. model.idata.groups() == test_builder2.model .idata.groups()
113
114
114
115
x_pred = np.random.uniform(low=0, high=1, size=100)
115
116
prediction_data = pd.DataFrame({"input": x_pred})
116
- pred1 = model .predict (prediction_data )
117
- pred2 = model2 .predict (prediction_data )
117
+ pred1 = test_builder .predict(prediction_data)
118
+ pred2 = test_builder2 .predict(prediction_data)
118
119
assert pred1["y_model"].shape == pred2["y_model"].shape
119
120
temp.close()
120
121
@@ -170,3 +171,4 @@ def test_id():
170
171
).hexdigest()[:16]
171
172
172
173
assert model.id == expected_id
174
+ """
0 commit comments