Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sparse_transform():
# data should not be altered by transform before fitting the model.
model = AffineWarping()
data = sparse.random((10, 11, 12), density=.1)
for dtype in (np.float64, np.float32, np.int64, np.int32):
X = data.astype(dtype)
model.fit(X, iterations=0, verbose=False)
assert_array_equal(X.coords, model.transform(X).coords)
def test_slicing_errors(index):
s = sparse.random((2, 3, 4), density=0.5, format='gxcs')
with pytest.raises(IndexError):
s[index]
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)
assert isinstance(fs, COO)
assert_eq(fs, x + y)
def test_binary_broadcasting(func, shape1, shape2):
density1 = 1 if np.prod(shape1) == 1 else 0.5
density2 = 1 if np.prod(shape2) == 1 else 0.5
xs = sparse.random(shape1, density=density1)
x = xs.todense()
ys = sparse.random(shape2, density=density2)
y = ys.todense()
expected = func(x, y)
actual = func(xs, ys)
assert isinstance(actual, COO)
assert_eq(expected, actual)
assert np.count_nonzero(expected) == actual.nnz
def test_reshape(a, b):
s = sparse.random(a, density=0.5, format='gxcs')
x = s.todense()
assert_eq(x.reshape(b), s.reshape(b))
def test_failed_densification():
import os
from importlib import reload
os.environ['SPARSE_AUTO_DENSIFY'] = '1'
reload(sparse._settings)
s = sparse.random((3, 4, 5), density=0.5)
x = np.array(s)
assert isinstance(x, np.ndarray)
assert_eq(s, x)
del os.environ['SPARSE_AUTO_DENSIFY']
reload(sparse._settings)
def test_reshape_function():
s = sparse.random((5, 3), density=0.5)
x = s.todense()
shape = (3, 5)
s2 = np.reshape(s, shape)
assert isinstance(s2, COO)
assert_eq(s2, x.reshape(shape))
def random_sparse(request):
dtype = request.param
if np.issubdtype(dtype, np.integer):
def data_rvs(n):
return np.random.randint(-1000, 1000, n)
else:
data_rvs = None
return sparse.random((20, 30, 40), density=.25, data_rvs=data_rvs).astype(dtype)
def test_resize(a, b):
s = sparse.random(a, density=0.5)
orig_size = s.size
x = s.todense()
x = np.resize(x, b)
s.resize(b)
temp = x.reshape(x.size)
temp[orig_size:] = s.fill_value
assert isinstance(s, sparse.SparseArray)
assert_eq(x, s)
def test_gt():
s = sparse.random((2, 3, 4), density=0.5)
x = s.todense()
m = x.mean()
assert_eq(x > m, s > m)
m = s.data[2]
assert_eq(x > m, s > m)
assert_eq(x >= m, s >= m)