Skip to content

Commit f76ad8a

Browse files
committed
cleanup unit tests; adapt to more recent versions of SciPy
1 parent 1b7ff71 commit f76ad8a

3 files changed

Lines changed: 39 additions & 268 deletions

File tree

lazyarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,10 @@ def evaluate(self, simplify=False, empty_val=0):
510510
x = x.reshape(self._shape)
511511
elif have_scipy and sparse.issparse(self.base_value): # For sparse matrices
512512
if empty_val != 0:
513-
x = self.base_value.toarray((sparse.csc_matrix))
513+
x = self.base_value.toarray()
514514
x = np.where(x, x, np.nan)
515515
else:
516-
x = self.base_value.toarray((sparse.csc_matrix))
516+
x = self.base_value.toarray()
517517
elif isinstance(self.base_value, Iterator):
518518
x = np.fromiter(self.base_value, dtype=self.dtype or float, count=self.size)
519519
if x.shape != self._shape:

test/test_lazy_arrays_from_Sparse_Matrices.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

test/test_lazyarray.py

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def test_create_with_sparse_array():
121121
data_dia = np.array([[1, 2, 3, 4]]).repeat(3, axis=0) # For dia_matrix
122122
offsets_dia = np.array([0, -1, 2]) # For dia_matrix
123123
dia = larray(dia_matrix((data_dia, offsets_dia), shape=(4, 4))) # For dia_matrix
124-
dok = larray(dok_matrix(((row, col)), shape=(3, 3))) # For dok_matrix
125-
lil = larray(lil_matrix(data, shape=(3, 3))) # For lil_matrix
124+
dok = larray(dok_matrix((np.vstack((row, col))), shape=(2, 6))) # For dok_matrix
125+
lil = larray(lil_matrix(data, shape=(1, 6))) # For lil_matrix
126126
assert bsr.shape == (3, 3)
127127
assert coo.shape == (3, 3)
128128
assert csc.shape == (3, 3)
@@ -136,47 +136,63 @@ def test_evaluate_with_sparse_array():
136136
assert_array_equal(coo.evaluate(), coo_matrix((data, (row, col))).toarray()) # For coo_matrix
137137
assert_array_equal(csc.evaluate(), csc_matrix((data, (row, col))).toarray()) # For csc_matrix
138138
assert_array_equal(csr.evaluate(), csr_matrix((data, (row, col))).toarray()) # For csr_matrix
139-
assert_array_equal(dia.evaluate(), dia_matrix((data_dia, (row, col))).toarray()) # For dia_matrix
140-
assert_array_equal(dok.evaluate(), dok_matrix((data, (row, col))).toarray()) # For dok_matrix
141-
assert_array_equal(lil.evaluate(), lil_matrix((data, (row, col))).toarray()) # For lil_matrix
139+
assert_array_equal(dia.evaluate(), dia_matrix((data_dia, offsets_dia), shape=(4, 4)).toarray()) # For dia_matrix
140+
assert_array_equal(dok.evaluate(), dok_matrix((np.vstack((row, col))), shape=(2, 6)).toarray()) # For dok_matrix
141+
assert_array_equal(lil.evaluate(), lil_matrix(data, shape=(1, 6)).toarray()) # For lil_matrix
142+
test_evaluate_with_sparse_array()
142143

143144
def test_multiple_operations_with_sparse_array():
144145
# For bsr_matrix
145-
bsr0 = bsr /100.0
146+
bsr0 = bsr / 100.0
146147
bsr1 = 0.2 + bsr0
147148
assert_array_almost_equal(bsr0.evaluate(), np.array([[0.01, 0., 0.04], [0., 0., 0.05], [0.02, 0.03, 0.06]]))
148-
assert_array_almost_equal(bsr0.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
149+
assert_array_almost_equal(bsr1.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
149150
# For coo_matrix
150-
coo0 = coo /100.0
151+
coo0 = coo / 100.0
151152
coo1 = 0.2 + coo0
152153
assert_array_almost_equal(coo0.evaluate(), np.array([[0.01, 0., 0.04], [0., 0., 0.05], [0.02, 0.03, 0.06]]))
153-
assert_array_almost_equal(coo0.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
154+
assert_array_almost_equal(coo1.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
154155
# For csc_matrix
155-
csc0 = csc /100.0
156+
csc0 = csc / 100.0
156157
csc1 = 0.2 + csc0
157158
assert_array_almost_equal(csc0.evaluate(), np.array([[0.01, 0., 0.04], [0., 0., 0.05], [0.02, 0.03, 0.06]]))
158-
assert_array_almost_equal(csc0.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
159+
assert_array_almost_equal(csc1.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
159160
# For csr_matrix
160-
csr0 = csr /100.0
161+
csr0 = csr / 100.0
161162
csr1 = 0.2 + csr0
162163
assert_array_almost_equal(csc0.evaluate(), np.array([[0.01, 0., 0.04], [0., 0., 0.05], [0.02, 0.03, 0.06]]))
163-
assert_array_almost_equal(csc0.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
164+
assert_array_almost_equal(csc1.evaluate(), np.array([[0.21, 0.2, 0.24], [0.2, 0.2, 0.25], [0.22, 0.23, 0.26]]))
164165
# For dia_matrix
165-
dia0 = dia /100.0
166+
dia0 = dia / 100.0
166167
dia1 = 0.2 + dia0
167-
assert_array_almost_equal(dia0.evaluate(), np.array([[0.01, 0.02, 0.03, 0.04]]))
168-
assert_array_almost_equal(dia1.evaluate(), np.array([[0.21, 0.22, 0.23, 0.24]]))
169-
# For dok_matrix
170-
dok0 = dok /100.0
168+
assert_array_almost_equal(
169+
dia0.evaluate(),
170+
np.array([
171+
[0.01, 0, 0.03, 0],
172+
[0.01, 0.02, 0, 0.04],
173+
[0, 0.02, 0.03, 0],
174+
[0, 0, 0.03, 0.04]
175+
])
176+
)
177+
assert_array_almost_equal(
178+
dia1.evaluate(),
179+
np.array([
180+
[0.21, 0.2, 0.23, 0.2],
181+
[0.21, 0.22, 0.2, 0.24],
182+
[0.2, 0.22, 0.23, 0.2],
183+
[0.2, 0.2, 0.23, 0.24]
184+
])
185+
) # For dok_matrix
186+
dok0 = dok / 100.0
171187
dok1 = 0.2 + dok0
172188
assert_array_almost_equal(dok0.evaluate(), np.array([[0., 0.02, 0.02, 0., 0.01, 0.02], [0., 0., 0.01, 0.02, 0.02, 0.02]]))
173189
assert_array_almost_equal(dok1.evaluate(), np.array([[0.2, 0.22, 0.22, 0.2, 0.21, 0.22], [0.2, 0.2, 0.21, 0.22, 0.22, 0.22]]))
174190
# For lil_matrix
175-
lil0 = lil /100.0
191+
lil0 = lil / 100.0
176192
lil1 = 0.2 + lil0
177193
assert_array_almost_equal(lil0.evaluate(), np.array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06]]))
178194
assert_array_almost_equal(lil1.evaluate(), np.array([[0.21, 0.22, 0.23, 0.24, 0.25, 0.26]]))
179-
195+
test_multiple_operations_with_sparse_array()
180196

181197
def test_getitem_from_2D_sparse_array():
182198
pytest.raises(IndexError, bsr.__getitem__, (3, 0))
@@ -186,41 +202,7 @@ def test_getitem_from_2D_sparse_array():
186202
pytest.raises(IndexError, dia.__getitem__, (3, 0))
187203
pytest.raises(IndexError, dok.__getitem__, (3, 0))
188204
pytest.raises(IndexError, lil.__getitem__, (3, 0))
189-
190-
191-
# def test_columnwise_iteration_with_flat_array():
192-
# m = larray(5, shape=(4,3)) # 4 rows, 3 columns
193-
# cols = [col for col in m.by_column()]
194-
# assert cols == [5, 5, 5]
195-
#
196-
# def test_columnwise_iteration_with_structured_array():
197-
# input = np.arange(12).reshape((4,3))
198-
# m = larray(input, shape=(4,3)) # 4 rows, 3 columns
199-
# cols = [col for col in m.by_column()]
200-
# assert_array_equal(cols[0], input[:,0])
201-
# assert_array_equal(cols[2], input[:,2])
202-
#
203-
# def test_columnwise_iteration_with_function():
204-
# input = lambda i,j: 2*i + j
205-
# m = larray(input, shape=(4,3))
206-
# cols = [col for col in m.by_column()]
207-
# assert_array_equal(cols[0], np.array([0, 2, 4, 6]))
208-
# assert_array_equal(cols[1], np.array([1, 3, 5, 7]))
209-
# assert_array_equal(cols[2], np.array([2, 4, 6, 8]))
210-
#
211-
# def test_columnwise_iteration_with_flat_array_and_mask():
212-
# m = larray(5, shape=(4,3)) # 4 rows, 3 columns
213-
# mask = np.array([True, False, True])
214-
# cols = [col for col in m.by_column(mask=mask)]
215-
# assert cols == [5, 5]
216-
#
217-
# def test_columnwise_iteration_with_structured_array_and_mask():
218-
# input = np.arange(12).reshape((4,3))
219-
# m = larray(input, shape=(4,3)) # 4 rows, 3 columns
220-
# mask = np.array([False, True, True])
221-
# cols = [col for col in m.by_column(mask=mask)]
222-
# assert_array_equal(cols[0], input[:,1])
223-
# assert_array_equal(cols[1], input[:,2])
205+
# test_getitem_from_2D_sparse_array()
224206

225207

226208
def test_size_related_properties():

0 commit comments

Comments
 (0)