| 1 |
from hdmf.common import CSRMatrix |
| 2 |
from hdmf.testing import TestCase, H5RoundTripMixin |
| 3 |
|
| 4 |
import scipy.sparse as sps |
| 5 |
import numpy as np |
| 6 |
|
| 7 |
|
| 8 |
class TestCSRMatrix(TestCase): |
| 9 |
|
| 10 |
def test_from_sparse_matrix(self): |
| 11 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 12 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 13 |
indptr = np.array([0, 2, 3, 6]) |
| 14 |
shape = (3, 3) |
| 15 |
expected = CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 16 |
|
| 17 |
sps_mat = sps.csr_matrix((data, indices, indptr), shape=shape) |
| 18 |
received = CSRMatrix(data=sps_mat) |
| 19 |
self.assertContainerEqual(received, expected, ignore_hdmf_attrs=True) |
| 20 |
|
| 21 |
def test_2d_data(self): |
| 22 |
data = np.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) |
| 23 |
csr_mat = CSRMatrix(data=data) |
| 24 |
sps_mat = sps.csr_matrix(data) |
| 25 |
np.testing.assert_array_equal(csr_mat.data, sps_mat.data) |
| 26 |
|
| 27 |
def test_getattrs(self): |
| 28 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 29 |
indices = np.array([0, 2, 2, 0, 1, 2], dtype=np.int32) |
| 30 |
indptr = np.array([0, 2, 3, 6], dtype=np.int32) |
| 31 |
shape = (3, 3) |
| 32 |
csr_mat = CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 33 |
|
| 34 |
np.testing.assert_array_equal(data, csr_mat.data) |
| 35 |
np.testing.assert_array_equal(indices, csr_mat.indices) |
| 36 |
np.testing.assert_array_equal(indptr, csr_mat.indptr) |
| 37 |
np.testing.assert_array_equal(shape, csr_mat.shape) |
| 38 |
self.assertEqual(csr_mat.indices.dtype.type, np.uint32) |
| 39 |
self.assertEqual(csr_mat.indptr.dtype.type, np.uint32) |
| 40 |
# NOTE: shape is stored internally in scipy.sparse.spmat as a tuple of ints. this is then converted to ndarray |
| 41 |
# but precision differs by OS |
| 42 |
self.assertTrue(np.issubdtype(csr_mat.shape.dtype.type, np.unsignedinteger)) |
| 43 |
|
| 44 |
def test_to_spmat(self): |
| 45 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 46 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 47 |
indptr = np.array([0, 2, 3, 6]) |
| 48 |
shape = (3, 3) |
| 49 |
csr_mat = CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 50 |
spmat_array = csr_mat.to_spmat().toarray() |
| 51 |
|
| 52 |
expected = np.asarray([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) |
| 53 |
np.testing.assert_array_equal(spmat_array, expected) |
| 54 |
|
| 55 |
def test_from_dense_2D_array(self): |
| 56 |
data = np.array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) |
| 57 |
csr_mat = CSRMatrix(data=data) |
| 58 |
spmat_array = csr_mat.to_spmat().toarray() |
| 59 |
np.testing.assert_array_equal(spmat_array, data) |
| 60 |
|
| 61 |
def test_valueerror_from_ndarray(self): |
| 62 |
msg = "'data' argument cannot be ndarray of dimensionality > 2." |
| 63 |
with self.assertRaisesWith(ValueError, msg): |
| 64 |
data = np.arange(27).reshape((3, 3, 3)) |
| 65 |
_ = CSRMatrix(data=data) |
| 66 |
|
| 67 |
def test_valueerror_missing_indptr_indices_or_shape(self): |
| 68 |
data = np.arange(10) |
| 69 |
msg = "Must specify 'indptr', 'indices', and 'shape' arguments when passing data array." |
| 70 |
with self.assertRaisesWith(ValueError, msg): |
| 71 |
_ = CSRMatrix(data=data) # indptr, indices, and shape are missing |
| 72 |
with self.assertRaisesWith(ValueError, msg): |
| 73 |
_ = CSRMatrix(data=data, indptr=data, indices=data) # shape missing |
| 74 |
with self.assertRaisesWith(ValueError, msg): |
| 75 |
_ = CSRMatrix(data=data, indptr=data, shape=(2, 5)) # indices missing |
| 76 |
with self.assertRaisesWith(ValueError, msg): |
| 77 |
_ = CSRMatrix(data=data, indices=data, shape=(2, 5)) # indptr missing |
| 78 |
|
| 79 |
def test_valueerror_non_2D_shape(self): |
| 80 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 81 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 82 |
indptr = np.array([0, 2, 3, 6]) |
| 83 |
with self.assertRaisesWith(ValueError, "'shape' argument must specify two and only two dimensions."): |
| 84 |
_ = CSRMatrix(data=data, indices=indices, indptr=indptr, shape=(3, 3, 1)) |
| 85 |
with self.assertRaisesWith(ValueError, "'shape' argument must specify two and only two dimensions."): |
| 86 |
_ = CSRMatrix(data=data, indices=indices, indptr=indptr, shape=(9, )) |
| 87 |
|
| 88 |
def test_valueerror_non_1d_indptr_or_indicies(self): |
| 89 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 90 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 91 |
indptr = np.array([0, 2, 3, 6]) |
| 92 |
with self.assertRaisesWith(ValueError, "'indices' must be a 1D array of unsigned integers."): |
| 93 |
_ = CSRMatrix(data=data, indices=indices.reshape((3, 2)), indptr=indptr, shape=(3, 3)) |
| 94 |
with self.assertRaisesWith(ValueError, "'indptr' must be a 1D array of unsigned integers."): |
| 95 |
_ = CSRMatrix(data=data, indices=indices, indptr=indptr.reshape((2, 2)), shape=(3, 3)) |
| 96 |
|
| 97 |
def test_valueerror_non_int_indptr_or_indicies(self): |
| 98 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 99 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 100 |
indptr = np.array([0, 2, 3, 6]) |
| 101 |
# test indices numpy array of floats |
| 102 |
with self.assertRaisesWith(ValueError, "Cannot convert 'indices' to an array of unsigned integers."): |
| 103 |
_ = CSRMatrix(data=data, indices=indices.astype(float), indptr=indptr, shape=(3, 3)) |
| 104 |
# test indptr numpy array of floats |
| 105 |
with self.assertRaisesWith(ValueError, "Cannot convert 'indptr' to an array of unsigned integers."): |
| 106 |
_ = CSRMatrix(data=data, indices=indices, indptr=indptr.astype(float), shape=(3, 3)) |
| 107 |
# test indices list of floats |
| 108 |
with self.assertRaisesWith(ValueError, "Cannot convert 'indices' to an array of unsigned integers."): |
| 109 |
_ = CSRMatrix(data=data, indices=indices.astype(float).tolist(), indptr=indptr, shape=(3, 3)) |
| 110 |
# test indptr list of floats |
| 111 |
with self.assertRaisesWith(ValueError, "Cannot convert 'indptr' to an array of unsigned integers."): |
| 112 |
_ = CSRMatrix(data=data, indices=indices, indptr=indptr.astype(float).tolist(), shape=(3, 3)) |
| 113 |
|
| 114 |
def test_constructor_indices_missing(self): |
| 115 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 116 |
msg = "Must specify 'indptr', 'indices', and 'shape' arguments when passing data array." |
| 117 |
with self.assertRaisesWith(ValueError, msg): |
| 118 |
CSRMatrix(data=data) |
| 119 |
|
| 120 |
def test_constructor_bad_indices(self): |
| 121 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 122 |
indices = np.array([0, -2, 2, 0, 1, 2]) |
| 123 |
indptr = np.array([0, 2, 3, 6]) |
| 124 |
shape = (3, 3) |
| 125 |
msg = "Cannot convert 'indices' to an array of unsigned integers." |
| 126 |
with self.assertRaisesWith(ValueError, msg): |
| 127 |
CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 128 |
|
| 129 |
def test_constructor_bad_indices_dim(self): |
| 130 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 131 |
indices = np.array([[0, 2, 2, 0, 1, 2]]) |
| 132 |
indptr = np.array([0, 2, 3, 6]) |
| 133 |
shape = (3, 3) |
| 134 |
msg = "'indices' must be a 1D array of unsigned integers." |
| 135 |
with self.assertRaisesWith(ValueError, msg): |
| 136 |
CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 137 |
|
| 138 |
def test_constructor_bad_shape(self): |
| 139 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 140 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 141 |
indptr = np.array([0, 2, 3, 6]) |
| 142 |
shape = (3, ) |
| 143 |
msg = "'shape' argument must specify two and only two dimensions." |
| 144 |
with self.assertRaisesWith(ValueError, msg): |
| 145 |
CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 146 |
|
| 147 |
def test_array_bad_dim(self): |
| 148 |
data = np.array([[[1, 2], [3, 4], [5, 6]]]) |
| 149 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 150 |
indptr = np.array([0, 2, 3, 6]) |
| 151 |
shape = (3, 3) |
| 152 |
msg = "'data' argument cannot be ndarray of dimensionality > 2." |
| 153 |
with self.assertRaisesWith(ValueError, msg): |
| 154 |
CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 155 |
|
| 156 |
|
| 157 |
class TestCSRMatrixRoundTrip(H5RoundTripMixin, TestCase): |
| 158 |
|
| 159 |
def setUpContainer(self): |
| 160 |
data = np.array([1, 2, 3, 4, 5, 6]) |
| 161 |
indices = np.array([0, 2, 2, 0, 1, 2]) |
| 162 |
indptr = np.array([0, 2, 3, 6]) |
| 163 |
shape = (3, 3) |
| 164 |
return CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |
| 165 |
|
| 166 |
|
| 167 |
class TestCSRMatrixRoundTripFromLists(H5RoundTripMixin, TestCase): |
| 168 |
"""Test that CSRMatrix works with lists as well""" |
| 169 |
|
| 170 |
def setUpContainer(self): |
| 171 |
data = [1, 2, 3, 4, 5, 6] |
| 172 |
indices = [0, 2, 2, 0, 1, 2] |
| 173 |
indptr = [0, 2, 3, 6] |
| 174 |
shape = (3, 3) |
| 175 |
return CSRMatrix(data=data, indices=indices, indptr=indptr, shape=shape) |