pytest.skip("NEP18 is not enabled", allow_module_level=True) @settings(deadline=None) @given( func=st.sampled_from( [ np.mean, np.std, np.var, np.sum, lambda x: np.sum(x, axis=0), lambda x: np.transpose(x), ] ), y=gen_sparse_random((50, 50), density=0.25), ) def test_unary(func, y): x = y.todense() xx = func(x) yy = func(y) assert_eq(xx, yy) @settings(deadline=None) @given( arg_order=st.sampled_from([(0, 1), (1, 0), (1, 1)]), func=st.sampled_from([np.dot, np.result_type, np.tensordot, np.matmul]), y=gen_sparse_random((50, 50), density=0.25), ) def test_binary(func, arg_order, y):
e = operator.matmul(a, b) assert_eq(e, operator.matmul(sa, sb)) assert_eq(e, operator.matmul(a, sb)) assert_eq(e, operator.matmul(sa, b)) @pytest.mark.parametrize( "a_dense, b_dense, o_type", [ (False, False, sparse.SparseArray), (False, True, np.ndarray), (True, False, np.ndarray), ], ) @given( a=gen_sparse_random((3, 4), density=0.8), b=gen_sparse_random((4, 5), density=0.8) ) def test_dot_type(a_dense, b_dense, o_type, a, b): if a_dense: a = a.todense() if b_dense: b = b.todense() assert isinstance(sparse.dot(a, b), o_type) @pytest.mark.xfail @given( sa=gen_sparse_random((3, 4, 5), density=0.5),
@given( shape=st.sampled_from([(2, ), (2, 3), (2, 3, 4)]), density=st.sampled_from([0.1, 0.3, 0.5, 0.7]), ) def test_random_shape_nnz(shape, density): s = sparse.random(shape, density, format="dok") assert isinstance(s, DOK) assert s.shape == shape expected_nnz = density * np.prod(shape) assert np.floor(expected_nnz) <= s.nnz <= np.ceil(expected_nnz) @given(gen_sparse_random((2, 3, 4), density=0.5, format="dok")) def test_convert_to_coo(s1): s2 = sparse.COO(s1) assert_eq(s1, s2) @given(gen_sparse_random((2, 3, 4), density=0.5, format="coo")) def test_convert_from_coo(s1): s2 = DOK(s1) assert_eq(s1, s2) def test_convert_from_numpy(): x = np.random.rand(2, 3, 4)
def random_sparse_small(request): dtype = request.param if np.issubdtype(dtype, np.integer): def data_rvs(n): return np.random.randint(-10, 10, n) else: data_rvs = None return sparse.random( (20, 30, 40), density=0.25, format="gcxs", data_rvs=data_rvs ).astype(dtype) @settings(deadline=None) @given(p=gen_reductions(), x=gen_sparse_random((20, 30, 40))) def test_reductions(p, x): reduction, kwargs = p y = x.todense() xx = getattr(x, reduction)(**kwargs) yy = getattr(y, reduction)(**kwargs) assert_eq(xx, yy) @pytest.mark.xfail( reason=("Setting output dtype=float16 produces results " "inconsistent with numpy") ) @pytest.mark.filterwarnings("ignore:overflow") @given( reduction=st.sampled_from(["sum", "mean"]), kwargs=st.sampled_from([{"dtype": np.float16}]),
def test_elemwise_unsupported(s1): class A: pass x2 = A() with pytest.raises(TypeError): s1 + x2 assert sparse.elemwise(operator.add, s1, x2) is NotImplemented @settings(deadline=None) @given( s1=gen_sparse_random_elemwise_mixed((2, 3, 4), density=0.5), s2=gen_sparse_random(4, density=0.5), ) def test_elemwise_mixed_broadcast(s1, s2): x3 = np.random.rand(3, 4) x1 = s1.todense() x2 = s2.todense() def func(x1, x2, x3): return x1 * x2 * x3 assert_eq(sparse.elemwise(func, s1, s2, x3), func(x1, x2, x3)) @given( func=st.sampled_from([
}), ("mean", {}), ("mean", { "dtype": np.float32 }), ("prod", {}), ("max", {}), ("min", {}), ("std", {}), ("var", {}), ], ) @given( axis=st.sampled_from([None, 0, 1, 2, (0, 2), -3, (1, -1)]), keepdims=st.sampled_from([True, False]), x=gen_sparse_random((20, 30, 40), density=0.25), ) def test_reductions(reduction, x, axis, keepdims, kwargs): y = x.todense() xx = getattr(x, reduction)(axis=axis, keepdims=keepdims, **kwargs) yy = getattr(y, reduction)(axis=axis, keepdims=keepdims, **kwargs) assert_eq(xx, yy) @pytest.mark.xfail(reason=("Setting output dtype=float16 produces results " "inconsistent with numpy")) @pytest.mark.filterwarnings("ignore:overflow") @given( x=gen_sparse_random((20, 30, 40)), reduction=st.sampled_from(["sum", "mean"]), kwargs=st.sampled_from([{
format=scipy_type, dtype=dtype) ref = COO.from_scipy_sparse(orig) result = CLS.from_scipy_sparse(orig) assert_eq(ref, result) result_via_init = CLS(orig) assert_eq(ref, result_via_init) @settings(deadline=None) @given( cls_str=st.sampled_from(["coo", "dok", "csr", "csc", "gcxs"]), rs=gen_sparse_random((20, 30), density=0.25), ) def test_to_sparse(cls_str, rs): result = rs.asformat(cls_str) assert_eq(rs, result) @settings(deadline=None) @given(copy=st.sampled_from([True, False]), rs=gen_sparse_random((20, 30), density=0.25)) def test_transpose(copy, rs): from operator import is_, is_not t = rs.transpose(copy=copy) tt = t.transpose(copy=copy)