1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import sparse
from sparse.numba_backend._utils import assert_eq
import pytest
import numpy as np
import scipy.sparse as sps
FORMATS_ND = [
sparse.COO,
sparse.DOK,
sparse.GCXS,
]
FORMATS_2D = [
sparse.numba_backend._compressed.CSC,
sparse.numba_backend._compressed.CSR,
]
FORMATS = FORMATS_2D + FORMATS_ND
@pytest.mark.parametrize("format1", FORMATS)
@pytest.mark.parametrize("format2", FORMATS)
def test_conversion(format1, format2):
x = sparse.random((10, 10), density=0.5, format=format1, fill_value=0.5)
y = x.asformat(format2)
assert_eq(x, y)
def test_extra_kwargs():
x = sparse.full((2, 2), 1, format="gcxs", compressed_axes=[1])
y = sparse.full_like(x, 1)
assert_eq(x, y)
@pytest.mark.parametrize("format1", FORMATS_ND)
@pytest.mark.parametrize("format2", FORMATS_ND)
def test_conversion_scalar(format1, format2):
x = sparse.random((), format=format1, fill_value=0.5)
y = x.asformat(format2)
assert_eq(x, y)
def test_non_canonical_conversion():
"""
Regression test for gh-602.
Adapted from https://github.com/LiberTEM/sparseconverter/blob/4cfc0ee2ad4c37b07742db8f3643bcbd858a4e85/src/sparseconverter/__init__.py#L154-L183
"""
data = np.array((2.0, 1.0, 3.0, 3.0, 1.0))
indices = np.array((1, 0, 0, 1, 1), dtype=int)
indptr = np.array((0, 2, 5), dtype=int)
x = sps.csr_matrix((data, indices, indptr), shape=(2, 2))
ref = np.array(((1.0, 2.0), (3.0, 4.0)))
gcxs_check = sparse.GCXS(x)
assert np.all(gcxs_check[:1].todense() == ref[:1]) and np.all(gcxs_check[1:].todense() == ref[1:])
|