def test_CheckAndRaise_basic_c(linker): exc_msg = "this is the exception" check_and_raise = CheckAndRaise(CustomException, exc_msg) conds = at.scalar() y = check_and_raise(at.as_tensor(1), conds) y_fn = aesara.function([conds], y, mode=Mode(linker)) with pytest.raises(CustomException, match=exc_msg): y_fn(0) x = at.vector() y = check_and_raise(x, conds) y_fn = aesara.function([conds, x], y.shape, mode=Mode(linker, OPT_FAST_RUN)) x_val = np.array([1.0], dtype=aesara.config.floatX) assert np.array_equal(y_fn(0, x_val), x_val) y = check_and_raise(x, at.as_tensor(0)) y_grad = aesara.grad(y.sum(), [x]) y_fn = aesara.function([x], y_grad, mode=Mode(linker, OPT_FAST_RUN)) assert np.array_equal(y_fn(x_val), [x_val])
def test_CheckAndRaise_validation(): with pytest.raises(ValueError): CheckAndRaise(str) g1 = assert_op(np.array(1.0)) assert isinstance(g1.owner.inputs[0], Constant)
def test_CheckAndRaise_pickle(): import pickle exc_msg = "this is the exception" check_and_raise = CheckAndRaise(CustomException, exc_msg) y = check_and_raise(at.as_tensor(1), at.as_tensor(0)) y_str = pickle.dumps(y) new_y = pickle.loads(y_str) assert y.owner.op == new_y.owner.op assert y.owner.op.msg == new_y.owner.op.msg assert y.owner.op.exc_type == new_y.owner.op.exc_type
def test_CheckAndRaise_sparse_variable(): check_and_raise = CheckAndRaise(ValueError, "sparse_check") spe1 = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 3)) aspe1 = as_sparse_variable(spe1) a1 = check_and_raise(aspe1, aspe1.sum() > 2) assert a1.sum().eval() == 3 spe2 = scipy.sparse.csc_matrix(scipy.sparse.eye(5, 1)) aspe2 = as_sparse_variable(spe2) a2 = check_and_raise(aspe1, aspe2.sum() > 2) with pytest.raises(ValueError, match="sparse_check"): a2.sum().eval()
def test_CheckAndRaise_basic_c(linker): exc_msg = "this is the exception" check_and_raise = CheckAndRaise(CustomException, exc_msg) x = at.scalar() conds = at.scalar() y = check_and_raise(at.as_tensor(1), conds) y_fn = aesara.function([conds], y, mode=Mode(linker)) with pytest.raises(CustomException, match=exc_msg): y_fn(0) y = check_and_raise(at.as_tensor(1), conds) y_fn = aesara.function([conds], y.shape, mode=Mode(linker, OPT_FAST_RUN)) assert np.array_equal(y_fn(0), []) y = check_and_raise(x, at.as_tensor(0)) y_grad = aesara.grad(y, [x]) y_fn = aesara.function([x], y_grad, mode=Mode(linker, OPT_FAST_RUN)) assert np.array_equal(y_fn(1.0), [1.0])
def test_CheckAndRaise_str(): exc_msg = "this is the exception" check_and_raise = CheckAndRaise(CustomException, exc_msg) assert (str(check_and_raise) == f"CheckAndRaise{{{CustomException}(this is the exception)}}")