|
18 | 18 |
|
19 | 19 | from pytensor import config, function |
20 | 20 | from pytensor.tensor.random.basic import multinomial |
| 21 | +from pytensor.tensor.variable import TensorVariable |
| 22 | +from pytensor.xtensor import as_xtensor |
| 23 | +from pytensor.xtensor.type import XTensorVariable |
21 | 24 | from scipy import interpolate |
22 | 25 |
|
23 | 26 | import pymc as pm |
@@ -68,6 +71,50 @@ def test_check_parameters_shape(): |
68 | 71 | assert check_parameters(1, *conditions).eval().shape == () |
69 | 72 |
|
70 | 73 |
|
| 74 | +def test_check_parameters_xtensor_expression_and_conditions(): |
| 75 | + expr = as_xtensor(np.array([1.0, 2.0]), dims=("batch",)) |
| 76 | + |
| 77 | + result = check_parameters(expr, expr > 0, expr < 3) |
| 78 | + |
| 79 | + assert isinstance(result, XTensorVariable) |
| 80 | + assert result.dims == expr.dims |
| 81 | + assert result.dtype == expr.dtype |
| 82 | + assert result.type.shape == expr.type.shape |
| 83 | + np.testing.assert_array_equal(result.eval(), expr.eval()) |
| 84 | + |
| 85 | + |
| 86 | +def test_check_parameters_invalid_xtensor_condition(): |
| 87 | + expr = as_xtensor(np.array([1.0, 2.0]), dims=("batch",)) |
| 88 | + result = check_parameters(expr, expr < 2, msg="parameter check msg") |
| 89 | + |
| 90 | + with pytest.raises(ParameterValueError, match="^parameter check msg*"): |
| 91 | + result.eval() |
| 92 | + |
| 93 | + |
| 94 | +def test_check_parameters_tensor_expression_xtensor_condition(): |
| 95 | + expr = pt.as_tensor_variable([1.0, 2.0]) |
| 96 | + condition = as_xtensor(np.array([True, True]), dims=("batch",)) |
| 97 | + |
| 98 | + result = check_parameters(expr, condition) |
| 99 | + |
| 100 | + assert isinstance(result, TensorVariable) |
| 101 | + np.testing.assert_array_equal(result.eval(), expr.eval()) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize("python_condition, succeeds", [(True, True), (False, False)]) |
| 105 | +def test_check_parameters_mixed_conditions(python_condition, succeeds): |
| 106 | + expr = as_xtensor(np.array([1.0, 2.0]), dims=("batch",)) |
| 107 | + tensor_condition = pt.as_tensor_variable([True, True]) |
| 108 | + |
| 109 | + result = check_parameters(expr, expr > 0, tensor_condition, python_condition) |
| 110 | + |
| 111 | + if succeeds: |
| 112 | + np.testing.assert_array_equal(result.eval(), expr.eval()) |
| 113 | + else: |
| 114 | + with pytest.raises(ParameterValueError): |
| 115 | + result.eval() |
| 116 | + |
| 117 | + |
71 | 118 | class MultinomialA(Discrete): |
72 | 119 | rv_op = multinomial |
73 | 120 |
|
|
0 commit comments