17
17
import pytest
18
18
19
19
from aesara import shared
20
+ from aesara .compile .sharedvalue import SharedVariable
20
21
from aesara .tensor .sharedvar import ScalarSharedVariable
21
22
from aesara .tensor .var import TensorVariable
22
23
@@ -32,7 +33,7 @@ class TestData(SeededTest):
32
33
def test_deterministic (self ):
33
34
data_values = np .array ([0.5 , 0.4 , 5 , 2 ])
34
35
with pm .Model () as model :
35
- X = pm .Data ("X" , data_values )
36
+ X = pm .MutableData ("X" , data_values )
36
37
pm .Normal ("y" , 0 , 1 , observed = X )
37
38
model .logp (model .recompute_initial_point ())
38
39
@@ -43,7 +44,7 @@ def test_sample(self):
43
44
x_pred = np .linspace (- 3 , 3 , 200 , dtype = "float32" )
44
45
45
46
with pm .Model ():
46
- x_shared = pm .Data ("x_shared" , x )
47
+ x_shared = pm .MutableData ("x_shared" , x )
47
48
b = pm .Normal ("b" , 0.0 , 10.0 )
48
49
pm .Normal ("obs" , b * x_shared , np .sqrt (1e-2 ), observed = y )
49
50
@@ -95,8 +96,8 @@ def test_sample_posterior_predictive_after_set_data(self):
95
96
96
97
def test_sample_after_set_data (self ):
97
98
with pm .Model () as model :
98
- x = pm .Data ("x" , [1.0 , 2.0 , 3.0 ])
99
- y = pm .Data ("y" , [1.0 , 2.0 , 3.0 ])
99
+ x = pm .MutableData ("x" , [1.0 , 2.0 , 3.0 ])
100
+ y = pm .MutableData ("y" , [1.0 , 2.0 , 3.0 ])
100
101
beta = pm .Normal ("beta" , 0 , 10.0 )
101
102
pm .Normal ("obs" , beta * x , np .sqrt (1e-2 ), observed = y )
102
103
pm .sample (
@@ -131,8 +132,8 @@ def test_shared_data_as_index(self):
131
132
See https://github.com/pymc-devs/pymc/issues/3813
132
133
"""
133
134
with pm .Model () as model :
134
- index = pm .Data ("index" , [2 , 0 , 1 , 0 , 2 ])
135
- y = pm .Data ("y" , [1.0 , 2.0 , 3.0 , 2.0 , 1.0 ])
135
+ index = pm .MutableData ("index" , [2 , 0 , 1 , 0 , 2 ])
136
+ y = pm .MutableData ("y" , [1.0 , 2.0 , 3.0 , 2.0 , 1.0 ])
136
137
alpha = pm .Normal ("alpha" , 0 , 1.5 , size = 3 )
137
138
pm .Normal ("obs" , alpha [index ], np .sqrt (1e-2 ), observed = y )
138
139
@@ -163,7 +164,7 @@ def test_shared_data_as_rv_input(self):
163
164
See https://github.com/pymc-devs/pymc/issues/3842
164
165
"""
165
166
with pm .Model () as m :
166
- x = pm .Data ("x" , [1.0 , 2.0 , 3.0 ])
167
+ x = pm .MutableData ("x" , [1.0 , 2.0 , 3.0 ])
167
168
y = pm .Normal ("y" , mu = x , size = (2 , 3 ))
168
169
assert y .eval ().shape == (2 , 3 )
169
170
idata = pm .sample (
@@ -221,7 +222,7 @@ def test_shared_scalar_as_rv_input(self):
221
222
222
223
def test_creation_of_data_outside_model_context (self ):
223
224
with pytest .raises ((IndexError , TypeError )) as error :
224
- pm .Data ("data" , [1.1 , 2.2 , 3.3 ])
225
+ pm .ConstantData ("data" , [1.1 , 2.2 , 3.3 ])
225
226
error .match ("No model on context stack" )
226
227
227
228
def test_set_data_to_non_data_container_variables (self ):
@@ -244,8 +245,8 @@ def test_set_data_to_non_data_container_variables(self):
244
245
@pytest .mark .xfail (reason = "Depends on ModelGraph" )
245
246
def test_model_to_graphviz_for_model_with_data_container (self ):
246
247
with pm .Model () as model :
247
- x = pm .Data ("x" , [1.0 , 2.0 , 3.0 ])
248
- y = pm .Data ("y" , [1.0 , 2.0 , 3.0 ])
248
+ x = pm .ConstantData ("x" , [1.0 , 2.0 , 3.0 ])
249
+ y = pm .MutableData ("y" , [1.0 , 2.0 , 3.0 ])
249
250
beta = pm .Normal ("beta" , 0 , 10.0 )
250
251
obs_sigma = floatX (np .sqrt (1e-2 ))
251
252
pm .Normal ("obs" , beta * x , obs_sigma , observed = y )
@@ -262,12 +263,14 @@ def test_model_to_graphviz_for_model_with_data_container(self):
262
263
pm .model_to_graphviz (model , formatting = formatting )
263
264
264
265
exp_without = [
265
- 'x [label="x\n ~\n Data" shape=box style="rounded, filled"]' ,
266
+ 'x [label="x\n ~\n ConstantData" shape=box style="rounded, filled"]' ,
267
+ 'y [label="x\n ~\n MutableData" shape=box style="rounded, filled"]' ,
266
268
'beta [label="beta\n ~\n Normal"]' ,
267
269
'obs [label="obs\n ~\n Normal" style=filled]' ,
268
270
]
269
271
exp_with = [
270
- 'x [label="x\n ~\n Data" shape=box style="rounded, filled"]' ,
272
+ 'x [label="x\n ~\n ConstantData" shape=box style="rounded, filled"]' ,
273
+ 'y [label="x\n ~\n MutableData" shape=box style="rounded, filled"]' ,
271
274
'beta [label="beta\n ~\n Normal(mu=0.0, sigma=10.0)"]' ,
272
275
f'obs [label="obs\n ~\n Normal(mu=f(f(beta), x), sigma={ obs_sigma } )" style=filled]' ,
273
276
]
@@ -290,7 +293,7 @@ def test_explicit_coords(self):
290
293
}
291
294
# pass coordinates explicitly, use numpy array in Data container
292
295
with pm .Model (coords = coords ) as pmodel :
293
- pm .Data ("observations" , data , dims = ("rows" , "columns" ))
296
+ pm .MutableData ("observations" , data , dims = ("rows" , "columns" ))
294
297
295
298
assert "rows" in pmodel .coords
296
299
assert pmodel .coords ["rows" ] == ("R1" , "R2" , "R3" , "R4" , "R5" )
@@ -310,7 +313,7 @@ def test_symbolic_coords(self):
310
313
Their lengths are then automatically linked to the corresponding Tensor dimension.
311
314
"""
312
315
with pm .Model () as pmodel :
313
- intensity = pm .Data ("intensity" , np .ones ((2 , 3 )), dims = ("row" , "column" ))
316
+ intensity = pm .MutableData ("intensity" , np .ones ((2 , 3 )), dims = ("row" , "column" ))
314
317
assert "row" in pmodel .dim_lengths
315
318
assert "column" in pmodel .dim_lengths
316
319
assert isinstance (pmodel .dim_lengths ["row" ], TensorVariable )
@@ -327,7 +330,7 @@ def test_no_resize_of_implied_dimensions(self):
327
330
# Imply a dimension through RV params
328
331
pm .Normal ("n" , mu = [1 , 2 , 3 ], dims = "city" )
329
332
# _Use_ the dimension for a data variable
330
- inhabitants = pm .Data ("inhabitants" , [100 , 200 , 300 ], dims = "city" )
333
+ inhabitants = pm .MutableData ("inhabitants" , [100 , 200 , 300 ], dims = "city" )
331
334
332
335
# Attempting to re-size the dimension through the data variable would
333
336
# cause shape problems in InferenceData conversion, because the RV remains (3,).
@@ -343,7 +346,7 @@ def test_implicit_coords_series(self):
343
346
name = "sales" ,
344
347
)
345
348
with pm .Model () as pmodel :
346
- pm .Data ("sales" , ser_sales , dims = "date" , export_index_as_coords = True )
349
+ pm .ConstantData ("sales" , ser_sales , dims = "date" , export_index_as_coords = True )
347
350
348
351
assert "date" in pmodel .coords
349
352
assert len (pmodel .coords ["date" ]) == 22
@@ -360,7 +363,9 @@ def test_implicit_coords_dataframe(self):
360
363
361
364
# infer coordinates from index and columns of the DataFrame
362
365
with pm .Model () as pmodel :
363
- pm .Data ("observations" , df_data , dims = ("rows" , "columns" ), export_index_as_coords = True )
366
+ pm .ConstantData (
367
+ "observations" , df_data , dims = ("rows" , "columns" ), export_index_as_coords = True
368
+ )
364
369
365
370
assert "rows" in pmodel .coords
366
371
assert "columns" in pmodel .coords
@@ -370,23 +375,30 @@ def test_data_kwargs(self):
370
375
strict_value = True
371
376
allow_downcast_value = False
372
377
with pm .Model ():
373
- data = pm .Data (
374
- "data " ,
378
+ data = pm .MutableData (
379
+ "mdata " ,
375
380
value = [[1.0 ], [2.0 ], [3.0 ]],
376
381
strict = strict_value ,
377
382
allow_downcast = allow_downcast_value ,
378
383
)
379
384
assert data .container .strict is strict_value
380
385
assert data .container .allow_downcast is allow_downcast_value
381
386
387
+ def test_data_mutable_default_warning (self ):
388
+ with pm .Model ():
389
+ with pytest .warns (FutureWarning , match = "`mutable` kwarg was not specified" ):
390
+ data = pm .Data ("x" , [1 , 2 , 3 ])
391
+ assert isinstance (data , SharedVariable )
392
+ pass
393
+
382
394
383
395
def test_data_naming ():
384
396
"""
385
397
This is a test for issue #3793 -- `Data` objects in named models are
386
398
not given model-relative names.
387
399
"""
388
400
with pm .Model ("named_model" ) as model :
389
- x = pm .Data ("x" , [1.0 , 2.0 , 3.0 ])
401
+ x = pm .ConstantData ("x" , [1.0 , 2.0 , 3.0 ])
390
402
y = pm .Normal ("y" )
391
403
assert y .name == "named_model_y"
392
404
assert x .name == "named_model_x"
0 commit comments