def gen_matmul_warning(draw): a = draw( st.sampled_from( [ sparse.GCXS.from_numpy( np.random.choice( [0, np.nan, 2], size=[100, 100], p=[0.99, 0.001, 0.009] ) ), sparse.COO.from_numpy( np.random.choice( [0, np.nan, 2], size=[100, 100], p=[0.99, 0.001, 0.009] ) ), sparse.GCXS.from_numpy( np.random.choice( [0, np.nan, 2], size=[100, 100], p=[0.99, 0.001, 0.009] ) ), np.random.choice( [0, np.nan, 2], size=[100, 100], p=[0.99, 0.001, 0.009] ), ] ) ) if not isinstance(a, np.ndarray): b = draw( st.sampled_from( [sparse.random((100, 100), density=0.01), scipy.sparse.random(100, 100)] ) ) else: b = draw(st.sampled_from([sparse.random((100, 100), density=0.01)])) return a, b
def setup(self): np.random.seed(0) self.x = sparse.random((100, 1, 100), density=0.01) self.y = sparse.random((100, 100), density=0.01) self.x.sum_duplicates() self.y.sum_duplicates()
def test_elemwise_binary(func, shape, format): xs = sparse.random(shape, density=0.5, format=format) ys = sparse.random(shape, density=0.5, format=format) x = xs.todense() y = ys.todense() assert_eq(func(xs, ys), func(x, y))
def test_elemwise_nonzero_input_fv(func, shape): xs = sparse.random(shape, density=0.5, fill_value=np.random.rand()) ys = sparse.random(shape, density=0.5, fill_value=np.random.rand()) x = xs.todense() y = ys.todense() assert_eq(func(xs, ys), func(x, y))
def test_stack(shape, axis): xx = sparse.random(shape, density=0.5, format="gcxs") x = xx.todense() yy = sparse.random(shape, density=0.5, format="gcxs") y = yy.todense() zz = sparse.random(shape, density=0.5, format="gcxs") z = zz.todense() assert_eq(np.stack([x, y, z], axis=axis), sparse.stack([xx, yy, zz], axis=axis))
def test_bitwise_binary_bool(func, shape): # Small arrays need high density to have nnz entries xs = sparse.random(shape, density=0.5).astype(bool) ys = sparse.random(shape, density=0.5).astype(bool) x = xs.todense() y = ys.todense() assert_eq(func(xs, ys), func(x, y))
def test_elemwise_trinary(func, shape, formats): xs = sparse.random(shape, density=0.5, format=formats[0]) ys = sparse.random(shape, density=0.5, format=formats[1]) zs = sparse.random(shape, density=0.5, format=formats[2]) x = xs.todense() y = ys.todense() z = zs.todense() fs = sparse.elemwise(func, xs, ys, zs) assert_eq(fs, func(x, y, z))
def setup(self, p, dens_arg): np.random.seed(0) self.x = sparse.random((100, 100), density=0.01, format=p) self.y = sparse.random((100, 100), density=0.01, format=p) if dens_arg == 0: self.x = self.x.todense() elif dens_arg == 1: self.y = self.y.todense() self.x @ self.y
def test_to_scipy_sparse(): s = sparse.random((3, 5), density=0.5, format="gcxs", compressed_axes=(0,)) a = s.to_scipy_sparse() b = scipy.sparse.csr_matrix(s.todense()) assert_eq(a, b) s = sparse.random((3, 5), density=0.5, format="gcxs", compressed_axes=(1,)) a = s.to_scipy_sparse() b = scipy.sparse.csc_matrix(s.todense()) assert_eq(a, b)
def test_bitwise_binary(func, shape, format): # Small arrays need high density to have nnz entries # Casting floats to int will result in all zeros, hence the * 100 xs = (sparse.random(shape, density=0.5, format=format) * 100).astype( np.int_) ys = (sparse.random(shape, density=0.5, format=format) * 100).astype( np.int_) x = xs.todense() y = ys.todense() assert_eq(func(xs, ys), func(x, y))
def test_sparse_parafac(): """Test for sparse parafac""" # Make sure the algorithm stays sparse. This will run out of memory on # most machines if the algorithm densifies. random_state = 1234 rank = 3 factors = [sparse.random((2862, rank), random_state=random_state), sparse.random((14036, rank), random_state=random_state)] weights = np.ones(rank) tensor = kruskal_to_tensor((weights, factors)) _ = parafac(tensor, rank=rank, init='random', n_iter_max=1, random_state=random_state)
def test_nonzero_outout_fv_ufunc(func, format): xs = sparse.random((2, 3, 4), density=0.5, format=format) ys = sparse.random((2, 3, 4), density=0.5, format=format) x = xs.todense() y = ys.todense() f = func(x, y) fs = func(xs, ys) assert isinstance(fs, format) assert_eq(f, fs)
def test_elemwise_mixed_broadcast(format): s1 = sparse.random((2, 3, 4), density=0.5, format=format) s2 = sparse.random(4, density=0.5) 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))
def test_bitshift_binary(func, shape): # Small arrays need high density to have nnz entries # Casting floats to int will result in all zeros, hence the * 100 xs = (sparse.random(shape, density=0.5) * 100).astype(np.int_) # Can't merge into test_bitwise_binary because left/right shifting # with something >= 64 isn't defined. ys = (sparse.random(shape, density=0.5) * 64).astype(np.int_) x = xs.todense() y = ys.todense() assert_eq(func(xs, ys), func(x, y))
def test_sparsearray_elemwise(format): xs = sparse.random((3, 4), density=0.5, format=format) ys = sparse.random((3, 4), density=0.5, format=format) x = xs.todense() y = ys.todense() fs = sparse.elemwise(operator.add, xs, ys) if format == "gcxs": assert isinstance(fs, GCXS) else: assert isinstance(fs, COO) assert_eq(fs, x + y)
def test_elemwise_mixed_empty(format): s1 = sparse.random((2, 0, 4), density=0.5, format=format) x2 = np.random.rand(2, 0, 4) x1 = s1.todense() assert_eq(s1 * x2, x1 * x2)
def test_dot_with_sparse(): A = sparse.random((1024, 64)) B = sparse.random((64)) ans = sparse.dot(A, B) # dot(sparse.array, sparse.array) res = utils.dot(A, B) assert_eq(ans, res) # dot(sparse.array, dask.array) res = utils.dot(A, da.from_array(B, chunks=B.shape)) assert_eq(ans, res.compute()) # dot(dask.array, sparse.array) res = utils.dot(da.from_array(A, chunks=A.shape), B) assert_eq(ans, res.compute())
def test_setitem_value_error(sd): shape, index, value_shape = sd s = sparse.random(shape, 0.5, format="dok") value = np.random.rand(*value_shape) with pytest.raises(ValueError): s[index] = value
def test_stack(): """stack(), by design, does not allow for mixed type inputs""" y = sparse.random((50, 50), density=0.25) x = y.todense() xx = np.stack([x, x]) yy = np.stack([y, y]) assert_eq(xx, yy)
def test_elemwise_mixed(shape1, shape2, format): s1 = sparse.random(shape1, density=0.5, format=format) x2 = np.random.rand(*shape2) x1 = s1.todense() assert_eq(s1 * x2, x1 * x2)
def random(shape, format='dense', density=1.): if format == "dense": return np.random.random(shape) elif format == "coo": return sparse.random(shape, density=density, format='coo') else: raise NotImplementedError
def test_einsum_random(self): for _ in range(10): # do 10 random tests num_arrs = random.randint(3, 5) # use between 3 and 5 arrays as input arrs = [ sp.random((20, ) * random.randint(1, 5), 0.05) for _ in range(num_arrs) ] # pick indices at random with replacement from the first 7 letters of the alphabet dims = [ ''.join(np.random.choice(list("abcdefg"), arr.ndim)) for arr in arrs ] all_inds = set.union(*(set(inds) for inds in dims)) # of all of the distinct indices that appear in any input, # pick a random subset of them (of size at most 5) to appear in the output output = ''.join( random.sample(all_inds, random.randint(0, min(len(all_inds), 5)))) specification = ','.join(dims) + '->' + output with self.subTest(spec=specification): print(specification) start = time.perf_counter() spr = einsum_sparse(specification, *arrs) mid = time.perf_counter() der = np.einsum(specification, *[todense(arr) for arr in arrs]) end = time.perf_counter() print(" sparse: {0}".format(mid - start)) print(" dense: {0}".format(end - mid)) self.assertTrue(np.allclose(todense(spr), der))
def test_ternary(func, arg_order): y = sparse.random((50, 50), density=0.25) x = y.todense() xx = func(x, x, x) args = [(x, y)[i] for i in arg_order] yy = func(*args) assert_eq(xx, yy)
def test_change_compressed_axes(): coo = sparse.random((3, 4, 5), density=0.5) s = GCXS.from_coo(coo, compressed_axes=(0, 1)) b = GCXS.from_coo(coo, compressed_axes=(1, 2)) assert_eq(s, b) s.change_compressed_axes((1, 2)) assert_eq(s, b)
def test_slicing(index, compressed_axes): s = sparse.random((2, 3, 4), density=0.5, format="gcxs", compressed_axes=compressed_axes) x = s.todense() assert_eq(x[index], s[index])
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)
def test_setitem(shape, index, value): s = sparse.random(shape, 0.5, format='dok') x = s.todense() s[index] = value x[index] = value assert_eq(x, s)
def test_flatten(in_shape): s = sparse.random(in_shape, format="gcxs", density=0.5) x = s.todense() a = s.flatten() e = x.flatten() assert_eq(e, a)
def gen_sparse_random_elemwise_trinary(draw, **kwargs): seed = draw(st.integers(min_value=0, max_value=100)) format = draw( st.sampled_from( [ [sparse.COO, sparse.COO, sparse.COO], [sparse.GCXS, sparse.GCXS, sparse.GCXS], [sparse.COO, sparse.GCXS, sparse.GCXS], ] ) ) shape = st.sampled_from([(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]) return ( sparse.random(shape[0], random_state=seed, format=format, **kwargs), sparse.random(shape[1], random_state=seed, format=format, **kwargs), sparse.random(shape[2], random_state=seed, format=format, **kwargs), )
def test_elemwise_binary_empty(): x = COO({}, shape=(10, 10)) y = sparse.random((10, 10), density=0.5) for z in [x * y, y * x]: assert z.nnz == 0 assert z.coords.shape == (2, 0) assert z.data.shape == (0, )