Skip to content

Commit e83520e

Browse files
Luke LBmichaelosthege
Luke LB
authored andcommitted
Remove ShapeWarning in favor of raising ValueError directly
1 parent 2b7a3c2 commit e83520e

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

pymc/model.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
from pymc.distributions import joint_logpt
6262
from pymc.distributions.logprob import _get_scaling
6363
from pymc.distributions.transforms import _default_transform
64-
from pymc.exceptions import ImputationWarning, SamplingError, ShapeError, ShapeWarning
64+
from pymc.exceptions import ImputationWarning, SamplingError, ShapeError
6565
from pymc.initial_point import make_initial_point_fn
6666
from pymc.math import flatten_list
6767
from pymc.util import (
@@ -1180,25 +1180,20 @@ def set_data(
11801180
# NOTE: If there are multiple pm.MutableData containers sharing this dim, but the user only
11811181
# changes the values for one of them, they will run into shape problems nonetheless.
11821182
if length_changed:
1183+
if original_coords is not None:
1184+
if new_coords is None:
1185+
raise ValueError(
1186+
f"The '{name}' variable already had {len(original_coords)} coord values defined for "
1187+
f"its {dname} dimension. With the new values this dimension changes to length "
1188+
f"{new_length}, so new coord values for the {dname} dimension are required."
1189+
)
11831190
if isinstance(length_tensor, TensorConstant):
11841191
raise ShapeError(
11851192
f"Resizing dimension '{dname}' is impossible, because "
11861193
f"a 'TensorConstant' stores its length. To be able "
11871194
f"to change the dimension length, 'fixed' in "
11881195
f"'model.add_coord' must be set to `False`."
11891196
)
1190-
if length_tensor.owner is None:
1191-
# This is the case if the dimension was initialized
1192-
# from custom coords, but dimension length was not
1193-
# stored in TensorConstant e.g by 'fixed' set to False
1194-
1195-
warnings.warn(
1196-
f"You're changing the shape of a variable "
1197-
f"in the '{dname}' dimension which was initialized "
1198-
f"from coords. Make sure to update the corresponding "
1199-
f"coords, otherwise you'll get shape issues.",
1200-
ShapeWarning,
1201-
)
12021197
else:
12031198
length_belongs_to = length_tensor.owner.inputs[0].owner.inputs[0]
12041199
if not isinstance(length_belongs_to, SharedVariable):
@@ -1210,13 +1205,6 @@ def set_data(
12101205
actual=new_length,
12111206
expected=old_length,
12121207
)
1213-
if original_coords is not None:
1214-
if new_coords is None:
1215-
raise ValueError(
1216-
f"The '{name}' variable already had {len(original_coords)} coord values defined for "
1217-
f"its {dname} dimension. With the new values this dimension changes to length "
1218-
f"{new_length}, so new coord values for the {dname} dimension are required."
1219-
)
12201208
if isinstance(length_tensor, ScalarSharedVariable):
12211209
# Updating the shared variable resizes dependent nodes that use this dimension for their `size`.
12221210
length_tensor.set_value(new_length)

pymc/tests/test_model.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,40 @@ def test_nested_model_coords():
702702
assert set(m2.RV_dims) < set(m1.RV_dims)
703703

704704

705+
def test_shapeerror_from_resize_immutable_dims():
706+
"""
707+
Trying to resize an immutable dimension should raise a ShapeError.
708+
Even if the variable being updated is a SharedVariable and has other
709+
dimensions that are mutable.
710+
"""
711+
with pm.Model() as pmodel:
712+
a = pm.Normal("a", mu=[1, 2, 3], dims="fixed")
713+
714+
m = pm.MutableData("m", [[1, 2, 3]], dims=("one", "fixed"))
715+
716+
# This is fine because the "fixed" dim is not resized
717+
pm.set_data({"m": [[1, 2, 3], [3, 4, 5]]})
718+
719+
with pytest.raises(ShapeError, match="was initialized from 'a'"):
720+
# Can't work because the "fixed" dimension is linked to a constant shape:
721+
# Note that the new data tries to change both dimensions
722+
with pmodel:
723+
pm.set_data({"m": [[1, 2], [3, 4]]})
724+
725+
726+
def test_valueerror_from_resize_without_coords_update():
727+
"""
728+
Resizing a mutable dimension that had coords,
729+
without passing new coords raises a ValueError.
730+
"""
731+
with pm.Model() as pmodel:
732+
pmodel.add_coord("shared", [1, 2, 3])
733+
pm.MutableData("m", [1, 2, 3], dims=("shared"))
734+
with pytest.raises(ValueError, match="'m' variable already had 3"):
735+
# tries to resize m but without passing coords so raise ValueError
736+
pm.set_data({"m": [1, 2, 3, 4]})
737+
738+
705739
@pytest.mark.parametrize("jacobian", [True, False])
706740
def test_model_logp(jacobian):
707741
with pm.Model() as m:

0 commit comments

Comments
 (0)