def test_ufunc_reductions(reduction, axis, keepdims, kwargs, eqkwargs): x = sparse.random((2, 3, 4), density=.5) y = x.todense() xx = reduction(x, axis=axis, keepdims=keepdims, **kwargs) yy = reduction(y, axis=axis, keepdims=keepdims, **kwargs) assert_eq(xx, yy, **eqkwargs)
def test_argwhere(): s = sparse.random((2, 3, 4), density=0.5) x = s.todense() assert_eq(np.argwhere(s), np.argwhere(x), compare_dtype=False)
def test_array_function(func): x = sparse.random((50, 50), density=.25) y = x.todense() xx = func(x) yy = func(y) assert_eq(xx, yy)
def test_empty_reduction(): x = np.zeros((2, 3, 4), dtype=np.float_) xs = COO.from_numpy(x) assert_eq(x.sum(axis=(0, 2)), xs.sum(axis=(0, 2)))
def test_np_array(): s = sparse.random((20, 30, 40)) x = np.array(s) assert isinstance(x, np.ndarray) assert_eq(x, s)
def test_transpose(axis): x = sparse.random((2, 3, 4), density=.25) y = x.todense() xx = x.transpose(axis) yy = y.transpose(axis) assert_eq(xx, yy)
def test_single_dimension(): x = COO([1, 3], [1.0, 3.0]) assert x.shape == (4,) assert_eq(x, np.array([0, 1.0, 0, 3.0]))
def test_1d(self, shift): xs = sparse.random((100,), density=0.5) x = xs.todense() assert_eq(np.roll(x, shift), sparse.roll(xs, shift)) assert_eq(np.roll(x, shift), sparse.roll(x, shift))
def test_2d(self, shift, ax): xs = sparse.random((10, 10), density=0.5) x = xs.todense() assert_eq(np.roll(x, shift, axis=ax), sparse.roll(xs, shift, axis=ax)) assert_eq(np.roll(x, shift, axis=ax), sparse.roll(x, shift, axis=ax))
def test_convert_from_numpy(): x = np.random.rand(2, 3, 4) s = DOK(x) assert_eq(x, s)
def test_convert_to_numpy(): s = sparse.random((2, 3, 4), 0.5, format='dok') x = s.todense() assert_eq(x, s)
def test_convert_from_coo(): s1 = sparse.random((2, 3, 4), 0.5, format='coo') s2 = DOK(s1) assert_eq(s1, s2)
def test_convert_to_coo(): s1 = sparse.random((2, 3, 4), 0.5, format='dok') s2 = sparse.COO(s1) assert_eq(s1, s2)
def test_coo_fv_interface(): s1 = sparse.full((5, 5), fill_value=1 + np.random.rand()) s2 = sparse.DOK(s1) assert_eq(s1, s2) s3 = sparse.COO(s2) assert_eq(s1, s3)
def test_ufunc_reductions_kwargs(reduction, kwargs): x = sparse.random((2, 3, 4), density=.5) y = x.todense() xx = reduction(x, **kwargs) yy = reduction(y, **kwargs) assert_eq(xx, yy)
def test_multiaxis(self, shift, ax): xs = sparse.random((9, 9, 9), density=0.5) x = xs.todense() assert_eq(np.roll(x, shift, axis=ax), sparse.roll(xs, shift, axis=ax)) assert_eq(np.roll(x, shift, axis=ax), sparse.roll(x, shift, axis=ax))
def test_advanced_indexing(index): s = sparse.random((2, 3, 4), density=0.5) x = s.todense() assert_eq(x[index], s[index])
def test_original_is_copied(self, shift, ax): xs = sparse.random((10, 10), density=0.5) xc = COO(np.copy(xs.coords), np.copy(xs.data), shape=xs.shape) sparse.roll(xs, shift, axis=ax) assert_eq(xs, xc)
def test_scipy_sparse_interface(): n = 100 m = 10 row = np.random.randint(0, n, size=n, dtype=np.uint16) col = np.random.randint(0, m, size=n, dtype=np.uint16) data = np.ones(n, dtype=np.uint8) inp = (data, (row, col)) x = scipy.sparse.coo_matrix(inp) xx = sparse.COO(inp) assert_eq(x, xx, check_nnz=False) assert_eq(x.T, xx.T, check_nnz=False) assert_eq(xx.to_scipy_sparse(), x, check_nnz=False) assert_eq(COO.from_scipy_sparse(xx.to_scipy_sparse()), xx, check_nnz=False) assert_eq(x, xx, check_nnz=False) assert_eq(x.T.dot(x), xx.T.dot(xx), check_nnz=False) assert isinstance(x + xx, COO) assert isinstance(xx + x, COO)
def test_empty(self): x = np.array([]) assert_eq(np.roll(x, 1), sparse.roll(sparse.as_coo(x), 1))
def test_triul(shape, k): s = sparse.random(shape, density=0.5) x = s.todense() assert_eq(np.triu(x, k), sparse.triu(s, k)) assert_eq(np.tril(x, k), sparse.tril(s, k))
def test_to_scipy_sparse(): s = sparse.random((3, 5), density=0.5) a = s.to_scipy_sparse() b = scipy.sparse.coo_matrix(s.todense()) assert_eq(a, b)
def test_two_random_same_seed(): state = np.random.randint(100) s1 = sparse.random((2, 3, 4), 0.3, random_state=state) s2 = sparse.random((2, 3, 4), 0.3, random_state=state) assert_eq(s1, s2)
def test_reductions(reduction, axis, keepdims, kwargs, eqkwargs): x = sparse.random((2, 3, 4), density=.25) 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, **eqkwargs)
def test_reshape(a, b): s = sparse.random(a, density=0.5) x = s.todense() assert_eq(x.reshape(b), s.reshape(b))
def test_broadcast_to(shape1, shape2): a = sparse.random(shape1, density=0.5) x = a.todense() assert_eq(np.broadcast_to(x, shape2), a.broadcast_to(shape2))
def test_asformat(format): s = sparse.random((2, 3, 4), density=0.5, format='coo') s2 = s.asformat(format) assert_eq(s, s2)
def test_elemwise_noargs(): def func(): return np.float_(5.0) assert_eq(sparse.elemwise(func), func())