Skip to content

Commit 8092eed

Browse files
change tests to expect scalar results for (1,) shapes
+ by using more pytest.mark.parametrize, the overall number of overall tests increases, but the result should be easier to diagnose + an informative assert error message was added
1 parent aa7d466 commit 8092eed

File tree

1 file changed

+29
-67
lines changed

1 file changed

+29
-67
lines changed

pymc3/tests/test_distributions_random.py

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def setup_method(self, *args, **kwargs):
204204
self.model = pm.Model()
205205

206206
def get_random_variable(self, shape, with_vector_params=False, name=None):
207+
""" Creates a RandomVariable of the parametrized distribution. """
207208
if with_vector_params:
208209
params = {
209210
key: value * np.ones(self.shape, dtype=np.dtype(type(value)))
@@ -226,79 +227,40 @@ def get_random_variable(self, shape, with_vector_params=False, name=None):
226227

227228
@staticmethod
228229
def sample_random_variable(random_variable, size):
230+
""" Draws samples from a RandomVariable using its .random() method. """
229231
try:
230232
return random_variable.random(size=size)
231233
except AttributeError:
232234
return random_variable.distribution.random(size=size)
233235

234-
@pytest.mark.parametrize("size", [None, 5, (4, 5)], ids=str)
235-
def test_scalar_parameter_shape(self, size):
236-
rv = self.get_random_variable(None)
237-
if size is None:
238-
expected = (1,)
239-
else:
240-
expected = np.atleast_1d(size).tolist()
241-
actual = np.atleast_1d(self.sample_random_variable(rv, size)).shape
242-
assert tuple(expected) == actual
243-
244-
@pytest.mark.parametrize("size", [None, 5, (4, 5)], ids=str)
245-
def test_scalar_shape(self, size):
246-
shape = 10
236+
@pytest.mark.parametrize("size", [None, (), 1, (1,), 5, (4, 5)], ids=str)
237+
@pytest.mark.parametrize("shape", [None, ()], ids=str)
238+
def test_scalar_distribution_shape(self, shape, size):
239+
""" Draws samples of different [size] from a scalar [shape] RV. """
247240
rv = self.get_random_variable(shape)
248-
249-
if size is None:
250-
expected = []
251-
else:
252-
expected = np.atleast_1d(size).tolist()
253-
expected.append(shape)
254-
actual = np.atleast_1d(self.sample_random_variable(rv, size)).shape
255-
assert tuple(expected) == actual
256-
257-
@pytest.mark.parametrize("size", [None, 5, (4, 5)], ids=str)
258-
def test_parameters_1d_shape(self, size):
259-
rv = self.get_random_variable(self.shape, with_vector_params=True)
260-
if size is None:
261-
expected = []
262-
else:
263-
expected = np.atleast_1d(size).tolist()
264-
expected.append(self.shape)
265-
actual = self.sample_random_variable(rv, size).shape
266-
assert tuple(expected) == actual
267-
268-
@pytest.mark.parametrize("size", [None, 5, (4, 5)], ids=str)
269-
def test_broadcast_shape(self, size):
270-
broadcast_shape = (2 * self.shape, self.shape)
271-
rv = self.get_random_variable(broadcast_shape, with_vector_params=True)
272-
if size is None:
273-
expected = []
274-
else:
275-
expected = np.atleast_1d(size).tolist()
276-
expected.extend(broadcast_shape)
277-
actual = np.atleast_1d(self.sample_random_variable(rv, size)).shape
278-
assert tuple(expected) == actual
279-
280-
@pytest.mark.parametrize(
281-
"shape", [(), (1,), (1, 1), (1, 2), (10, 10, 1), (10, 10, 2)], ids=str
282-
)
283-
def test_different_shapes_and_sample_sizes(self, shape):
284-
prefix = self.distribution.__name__
285-
286-
rv = self.get_random_variable(shape, name=f"{prefix}_{shape}")
287-
for size in (None, 1, 5, (4, 5)):
288-
if size is None:
289-
s = []
290-
else:
291-
try:
292-
s = list(size)
293-
except TypeError:
294-
s = [size]
295-
if s == [1]:
296-
s = []
297-
if shape not in ((), (1,)):
298-
s.extend(shape)
299-
e = tuple(s)
300-
a = self.sample_random_variable(rv, size).shape
301-
assert e == a
241+
expected = () if size in {None, ()} else tuple(np.atleast_1d(size))
242+
actual = np.shape(self.sample_random_variable(rv, size))
243+
assert expected == actual, f"Sample size {size} from {shape}-shaped RV had shape {actual}. Expected: {expected}"
244+
245+
@pytest.mark.parametrize("size", [None, ()], ids=str)
246+
@pytest.mark.parametrize("shape", [None, (), (1,), (1, 1), (1, 2), (10, 11, 1), (9, 10, 2)], ids=str)
247+
def test_scalar_sample_shape(self, shape, size):
248+
""" Draws samples of scalar [size] from a [shape] RV. """
249+
rv = self.get_random_variable(shape)
250+
expected = () if shape in {None, ()} else tuple(np.atleast_1d(shape))
251+
actual = np.shape(self.sample_random_variable(rv, size))
252+
assert expected == actual, f"Sample size {size} from {shape}-shaped RV had shape {actual}. Expected: {expected}"
253+
254+
@pytest.mark.parametrize("size", [None, 3, (4, 5)], ids=str)
255+
@pytest.mark.parametrize("shape", [None, 1, (10, 11, 1)], ids=str)
256+
def test_vector_params(self, shape, size):
257+
shape = self.shape
258+
rv = self.get_random_variable(shape, with_vector_params=True)
259+
exp_shape = () if shape in {None, ()} else tuple(np.atleast_1d(shape))
260+
exp_size = () if size in {None, ()} else tuple(np.atleast_1d(size))
261+
expected = exp_size + exp_shape
262+
actual = np.shape(self.sample_random_variable(rv, size))
263+
assert expected == actual, f"Sample size {size} from {shape}-shaped RV had shape {actual}. Expected: {expected}"
302264

303265

304266
class TestGaussianRandomWalk(BaseTestCases.BaseTestCase):

0 commit comments

Comments
 (0)