File size: 8,477 Bytes
6e5ef42 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
import numpy as np
import pytest
from scipy.sparse import coo_array
def test_shape_constructor():
empty1d = coo_array((3,))
assert empty1d.shape == (3,)
assert np.array_equal(empty1d.toarray(), np.zeros((3,)))
empty2d = coo_array((3, 2))
assert empty2d.shape == (3, 2)
assert np.array_equal(empty2d.toarray(), np.zeros((3, 2)))
with pytest.raises(TypeError, match='invalid input format'):
coo_array((3, 2, 2))
def test_dense_constructor():
res1d = coo_array([1, 2, 3])
assert res1d.shape == (3,)
assert np.array_equal(res1d.toarray(), np.array([1, 2, 3]))
res2d = coo_array([[1, 2, 3], [4, 5, 6]])
assert res2d.shape == (2, 3)
assert np.array_equal(res2d.toarray(), np.array([[1, 2, 3], [4, 5, 6]]))
with pytest.raises(ValueError, match='shape must be a 1- or 2-tuple'):
coo_array([[[3]], [[4]]])
def test_dense_constructor_with_shape():
res1d = coo_array([1, 2, 3], shape=(3,))
assert res1d.shape == (3,)
assert np.array_equal(res1d.toarray(), np.array([1, 2, 3]))
res2d = coo_array([[1, 2, 3], [4, 5, 6]], shape=(2, 3))
assert res2d.shape == (2, 3)
assert np.array_equal(res2d.toarray(), np.array([[1, 2, 3], [4, 5, 6]]))
with pytest.raises(ValueError, match='shape must be a 1- or 2-tuple'):
coo_array([[[3]], [[4]]], shape=(2, 1, 1))
def test_dense_constructor_with_inconsistent_shape():
with pytest.raises(ValueError, match='inconsistent shapes'):
coo_array([1, 2, 3], shape=(4,))
with pytest.raises(ValueError, match='inconsistent shapes'):
coo_array([1, 2, 3], shape=(3, 1))
with pytest.raises(ValueError, match='inconsistent shapes'):
coo_array([[1, 2, 3]], shape=(3,))
with pytest.raises(ValueError,
match='axis 0 index 2 exceeds matrix dimension 2'):
coo_array(([1], ([2],)), shape=(2,))
with pytest.raises(ValueError, match='negative axis 0 index: -1'):
coo_array(([1], ([-1],)))
def test_1d_sparse_constructor():
empty1d = coo_array((3,))
res = coo_array(empty1d)
assert res.shape == (3,)
assert np.array_equal(res.toarray(), np.zeros((3,)))
def test_1d_tuple_constructor():
res = coo_array(([9,8], ([1,2],)))
assert res.shape == (3,)
assert np.array_equal(res.toarray(), np.array([0, 9, 8]))
def test_1d_tuple_constructor_with_shape():
res = coo_array(([9,8], ([1,2],)), shape=(4,))
assert res.shape == (4,)
assert np.array_equal(res.toarray(), np.array([0, 9, 8, 0]))
def test_non_subscriptability():
coo_2d = coo_array((2, 2))
with pytest.raises(TypeError,
match="'coo_array' object does not support item assignment"):
coo_2d[0, 0] = 1
with pytest.raises(TypeError,
match="'coo_array' object is not subscriptable"):
coo_2d[0, :]
def test_reshape():
arr1d = coo_array([1, 0, 3])
assert arr1d.shape == (3,)
col_vec = arr1d.reshape((3, 1))
assert col_vec.shape == (3, 1)
assert np.array_equal(col_vec.toarray(), np.array([[1], [0], [3]]))
row_vec = arr1d.reshape((1, 3))
assert row_vec.shape == (1, 3)
assert np.array_equal(row_vec.toarray(), np.array([[1, 0, 3]]))
arr2d = coo_array([[1, 2, 0], [0, 0, 3]])
assert arr2d.shape == (2, 3)
flat = arr2d.reshape((6,))
assert flat.shape == (6,)
assert np.array_equal(flat.toarray(), np.array([1, 2, 0, 0, 0, 3]))
def test_nnz():
arr1d = coo_array([1, 0, 3])
assert arr1d.shape == (3,)
assert arr1d.nnz == 2
arr2d = coo_array([[1, 2, 0], [0, 0, 3]])
assert arr2d.shape == (2, 3)
assert arr2d.nnz == 3
def test_transpose():
arr1d = coo_array([1, 0, 3]).T
assert arr1d.shape == (3,)
assert np.array_equal(arr1d.toarray(), np.array([1, 0, 3]))
arr2d = coo_array([[1, 2, 0], [0, 0, 3]]).T
assert arr2d.shape == (3, 2)
assert np.array_equal(arr2d.toarray(), np.array([[1, 0], [2, 0], [0, 3]]))
def test_transpose_with_axis():
arr1d = coo_array([1, 0, 3]).transpose(axes=(0,))
assert arr1d.shape == (3,)
assert np.array_equal(arr1d.toarray(), np.array([1, 0, 3]))
arr2d = coo_array([[1, 2, 0], [0, 0, 3]]).transpose(axes=(0, 1))
assert arr2d.shape == (2, 3)
assert np.array_equal(arr2d.toarray(), np.array([[1, 2, 0], [0, 0, 3]]))
with pytest.raises(ValueError, match="axes don't match matrix dimensions"):
coo_array([1, 0, 3]).transpose(axes=(0, 1))
with pytest.raises(ValueError, match="repeated axis in transpose"):
coo_array([[1, 2, 0], [0, 0, 3]]).transpose(axes=(1, 1))
def test_1d_row_and_col():
res = coo_array([1, -2, -3])
assert np.array_equal(res.col, np.array([0, 1, 2]))
assert np.array_equal(res.row, np.zeros_like(res.col))
assert res.row.dtype == res.col.dtype
assert res.row.flags.writeable is False
res.col = [1, 2, 3]
assert len(res.coords) == 1
assert np.array_equal(res.col, np.array([1, 2, 3]))
assert res.row.dtype == res.col.dtype
with pytest.raises(ValueError, match="cannot set row attribute"):
res.row = [1, 2, 3]
def test_1d_toformats():
res = coo_array([1, -2, -3])
for f in [res.tocsc, res.tocsr, res.todia, res.tolil, res.tobsr]:
with pytest.raises(ValueError, match='Cannot convert'):
f()
for f in [res.tocoo, res.todok]:
assert np.array_equal(f().toarray(), res.toarray())
@pytest.mark.parametrize('arg', [1, 2, 4, 5, 8])
def test_1d_resize(arg: int):
den = np.array([1, -2, -3])
res = coo_array(den)
den.resize(arg, refcheck=False)
res.resize(arg)
assert res.shape == den.shape
assert np.array_equal(res.toarray(), den)
@pytest.mark.parametrize('arg', zip([1, 2, 3, 4], [1, 2, 3, 4]))
def test_1d_to_2d_resize(arg: tuple[int, int]):
den = np.array([1, 0, 3])
res = coo_array(den)
den.resize(arg, refcheck=False)
res.resize(arg)
assert res.shape == den.shape
assert np.array_equal(res.toarray(), den)
@pytest.mark.parametrize('arg', [1, 4, 6, 8])
def test_2d_to_1d_resize(arg: int):
den = np.array([[1, 0, 3], [4, 0, 0]])
res = coo_array(den)
den.resize(arg, refcheck=False)
res.resize(arg)
assert res.shape == den.shape
assert np.array_equal(res.toarray(), den)
def test_sum_duplicates():
arr1d = coo_array(([2, 2, 2], ([1, 0, 1],)))
assert arr1d.nnz == 3
assert np.array_equal(arr1d.toarray(), np.array([2, 4]))
arr1d.sum_duplicates()
assert arr1d.nnz == 2
assert np.array_equal(arr1d.toarray(), np.array([2, 4]))
def test_eliminate_zeros():
arr1d = coo_array(([0, 0, 1], ([1, 0, 1],)))
assert arr1d.nnz == 3
assert arr1d.count_nonzero() == 1
assert np.array_equal(arr1d.toarray(), np.array([0, 1]))
arr1d.eliminate_zeros()
assert arr1d.nnz == 1
assert arr1d.count_nonzero() == 1
assert np.array_equal(arr1d.toarray(), np.array([0, 1]))
assert np.array_equal(arr1d.col, np.array([1]))
assert np.array_equal(arr1d.row, np.array([0]))
def test_1d_add_dense():
den_a = np.array([0, -2, -3, 0])
den_b = np.array([0, 1, 2, 3])
exp = den_a + den_b
res = coo_array(den_a) + den_b
assert type(res) == type(exp)
assert np.array_equal(res, exp)
def test_1d_add_sparse():
den_a = np.array([0, -2, -3, 0])
den_b = np.array([0, 1, 2, 3])
# Currently this routes through CSR format, so 1d sparse addition
# isn't supported.
with pytest.raises(ValueError,
match='Cannot convert a 1d sparse array'):
coo_array(den_a) + coo_array(den_b)
def test_1d_matmul_vector():
den_a = np.array([0, -2, -3, 0])
den_b = np.array([0, 1, 2, 3])
exp = den_a @ den_b
res = coo_array(den_a) @ den_b
assert np.ndim(res) == 0
assert np.array_equal(res, exp)
def test_1d_matmul_multivector():
den = np.array([0, -2, -3, 0])
other = np.array([[0, 1, 2, 3], [3, 2, 1, 0]]).T
exp = den @ other
res = coo_array(den) @ other
assert type(res) == type(exp)
assert np.array_equal(res, exp)
def test_2d_matmul_multivector():
den = np.array([[0, 1, 2, 3], [3, 2, 1, 0]])
arr2d = coo_array(den)
exp = den @ den.T
res = arr2d @ arr2d.T
assert np.array_equal(res.toarray(), exp)
def test_1d_diagonal():
den = np.array([0, -2, -3, 0])
with pytest.raises(ValueError, match='diagonal requires two dimensions'):
coo_array(den).diagonal()
|