@@ -301,32 +301,120 @@ def test_autodetect_coords_from_model(self, use_context):
301301 np .testing .assert_array_equal (idata .observed_data .coords ["date" ], coords ["date" ])
302302 np .testing .assert_array_equal (idata .observed_data .coords ["city" ], coords ["city" ])
303303
304- def test_overwrite_model_coords_dims (self ):
305- """Check coords and dims from model object can be partially overwritten."""
306- dim1 = ["a" , "b" ]
307- new_dim1 = ["c" , "d" ]
308- coords = {"dim1" : dim1 , "dim2" : ["c1" , "c2" ]}
309- x_data = np .arange (4 ).reshape ((2 , 2 ))
310- y = x_data + np .random .normal (size = (2 , 2 ))
311- with pm .Model (coords = coords ):
312- x = pm .Data ("x" , x_data , dims = ("dim1" , "dim2" ))
313- beta = pm .Normal ("beta" , 0 , 1 , dims = "dim1" )
314- _ = pm .Normal ("obs" , x * beta , 1 , observed = y , dims = ("dim1" , "dim2" ))
315- trace = pm .sample (100 , tune = 100 , return_inferencedata = False )
316- idata1 = to_inference_data (trace )
317- idata2 = to_inference_data (trace , coords = {"dim1" : new_dim1 }, dims = {"beta" : ["dim2" ]})
318304
319- test_dict = {"posterior" : ["beta" ], "observed_data" : ["obs" ], "constant_data" : ["x" ]}
320- fails1 = check_multiple_attrs (test_dict , idata1 )
321- assert not fails1
322- fails2 = check_multiple_attrs (test_dict , idata2 )
323- assert not fails2
324- assert "dim1" in list (idata1 .posterior .beta .dims )
325- assert "dim2" in list (idata2 .posterior .beta .dims )
326- assert np .all (idata1 .constant_data .x .dim1 .values == np .array (dim1 ))
327- assert np .all (idata1 .constant_data .x .dim2 .values == np .array (["c1" , "c2" ]))
328- assert np .all (idata2 .constant_data .x .dim1 .values == np .array (new_dim1 ))
329- assert np .all (idata2 .constant_data .x .dim2 .values == np .array (["c1" , "c2" ]))
305+ from arviz import to_inference_data
306+
307+
308+ def test_overwrite_model_coords_dims (self ):
309+ """Test overwriting model coords and dims."""
310+
311+ # ✅ Define model and sample posterior
312+ with pm .Model () as model :
313+ mu = pm .Normal ("mu" , 0 , 1 )
314+ sigma = pm .HalfNormal ("sigma" , 1 )
315+ obs = pm .Normal ("obs" , mu = mu , sigma = sigma , observed = [1.2 , 2.3 , 3.1 ])
316+
317+ idata = pm .sample (500 , return_inferencedata = True )
318+
319+ # ✅ Debugging prints
320+ print ("📌 Shape of idata.posterior:" , idata .posterior .sizes )
321+ print ("📌 Shape of idata.observed_data:" , idata .observed_data .sizes )
322+
323+ # ✅ Use `idata` directly instead of `create_test_inference_data()`
324+ inference_data = idata
325+
326+ # ✅ Ensure shapes match expectations
327+ expected_chains = inference_data .posterior .sizes ["chain" ]
328+ expected_draws = inference_data .posterior .sizes ["draw" ]
329+ print (f"✅ Expected Chains: { expected_chains } , Expected Draws: { expected_draws } " )
330+
331+ assert expected_chains > 0 # Ensure at least 1 chain
332+ assert expected_draws == 500 # Verify expected number of draws
333+
334+ # ✅ Check overwriting of coordinates & dimensions
335+ dim1 = ["a" , "b" ]
336+ new_dim1 = ["c" , "d" ]
337+ coords = {"dim1" : dim1 , "dim2" : ["c1" , "c2" ]}
338+ x_data = np .arange (4 ).reshape ((2 , 2 ))
339+ y = x_data + np .random .normal (size = (2 , 2 ))
340+
341+ with pm .Model (coords = coords ):
342+ x = pm .Data ("x" , x_data , dims = ("dim1" , "dim2" ))
343+ beta = pm .Normal ("beta" , 0 , 1 , dims = "dim1" )
344+ _ = pm .Normal ("obs" , x * beta , 1 , observed = y , dims = ("dim1" , "dim2" ))
345+
346+ trace = pm .sample (100 , tune = 100 , return_inferencedata = False )
347+ idata1 = to_inference_data (trace )
348+ idata2 = to_inference_data (trace , coords = {"dim1" : new_dim1 }, dims = {"beta" : ["dim2" ]})
349+
350+ test_dict = {"posterior" : ["beta" ], "observed_data" : ["obs" ], "constant_data" : ["x" ]}
351+ fails1 = check_multiple_attrs (test_dict , idata1 )
352+ fails2 = check_multiple_attrs (test_dict , idata2 )
353+
354+ assert not fails1
355+ assert not fails2
356+ assert "dim1" in list (idata1 .posterior .beta .dims )
357+ assert "dim2" in list (idata2 .posterior .beta .dims )
358+ assert np .all (idata1 .constant_data .x .dim1 .values == np .array (dim1 ))
359+ assert np .all (idata1 .constant_data .x .dim2 .values == np .array (["c1" , "c2" ]))
360+ assert np .all (idata2 .constant_data .x .dim1 .values == np .array (new_dim1 ))
361+ assert np .all (idata2 .constant_data .x .dim2 .values == np .array (["c1" , "c2" ]))
362+
363+ # def test_overwrite_model_coords_dims(self):
364+
365+ # # ✅ Define model first
366+ # with pm.Model() as model:
367+ # mu = pm.Normal("mu", 0, 1)
368+ # sigma = pm.HalfNormal("sigma", 1)
369+ # obs = pm.Normal("obs", mu=mu, sigma=sigma, observed=[1.2, 2.3, 3.1])
370+
371+ # # ✅ Sample the posterior
372+ # idata = pm.sample(500, return_inferencedata=True)
373+
374+ # # ✅ Debugging prints
375+ # print("📌 Shape of idata.posterior:", idata.posterior.sizes)
376+ # print("📌 Shape of idata.observed_data:", idata.observed_data.sizes)
377+
378+ # # ✅ Replace inference_data with idata
379+ # assert idata.posterior.sizes["chain"] == 2 # Adjust if needed
380+ # assert idata.posterior.sizes["draw"] == 500 # Match the `draws` argument
381+
382+ # # ✅ Ensure inference_data is properly defined
383+ # inference_data = self.create_test_inference_data()
384+
385+ # # Print the actual shapes of inference data
386+ # print("📌 Shape of inference_data.posterior:", inference_data.posterior.sizes)
387+ # print("📌 Shape of inference_data.observed_data:", inference_data.observed_data.sizes)
388+ # print("📌 Shape of inference_data.log_likelihood:", inference_data.log_likelihood.sizes)
389+
390+ # # Existing assertion
391+ # assert inference_data.posterior.sizes["chain"] == 2
392+
393+ # """Check coords and dims from model object can be partially overwritten."""
394+ # dim1 = ["a", "b"]
395+ # new_dim1 = ["c", "d"]
396+ # coords = {"dim1": dim1, "dim2": ["c1", "c2"]}
397+ # x_data = np.arange(4).reshape((2, 2))
398+ # y = x_data + np.random.normal(size=(2, 2))
399+ # with pm.Model(coords=coords):
400+ # x = pm.Data("x", x_data, dims=("dim1", "dim2"))
401+ # beta = pm.Normal("beta", 0, 1, dims="dim1")
402+ # _ = pm.Normal("obs", x * beta, 1, observed=y, dims=("dim1", "dim2"))
403+ # trace = pm.sample(100, tune=100, return_inferencedata=False)
404+ # idata1 = to_inference_data(trace)
405+ # idata2 = to_inference_data(trace, coords={"dim1": new_dim1}, dims={"beta": ["dim2"]})
406+
407+ # test_dict = {"posterior": ["beta"], "observed_data": ["obs"], "constant_data": ["x"]}
408+ # fails1 = check_multiple_attrs(test_dict, idata1)
409+ # assert not fails1
410+ # fails2 = check_multiple_attrs(test_dict, idata2)
411+ # assert not fails2
412+ # assert "dim1" in list(idata1.posterior.beta.dims)
413+ # assert "dim2" in list(idata2.posterior.beta.dims)
414+ # assert np.all(idata1.constant_data.x.dim1.values == np.array(dim1))
415+ # assert np.all(idata1.constant_data.x.dim2.values == np.array(["c1", "c2"]))
416+ # assert np.all(idata2.constant_data.x.dim1.values == np.array(new_dim1))
417+ # assert np.all(idata2.constant_data.x.dim2.values == np.array(["c1", "c2"]))
330418
331419 def test_missing_data_model (self ):
332420 # source tests/test_missing.py
0 commit comments