|
1 | 1 | import unittest |
2 | | -import warnings |
3 | | -from functools import wraps, partial |
4 | 2 | from itertools import chain |
| 3 | +from functools import partial, wraps |
5 | 4 |
|
6 | 5 | import numpy as np |
7 | | -import scipy as sp |
8 | | -from scipy.sparse import csr_matrix, issparse, csc_matrix |
| 6 | +from scipy.sparse import csr_matrix, issparse, lil_matrix, csc_matrix |
9 | 7 |
|
10 | 8 | from Orange.statistics.util import bincount, countnans, contingency, stats, \ |
11 | 9 | nanmin, nanmax, unique, nanunique, mean, nanmean, digitize, var |
@@ -128,44 +126,6 @@ def test_nanmin_nanmax(self): |
128 | 126 | nanmax(X_sparse, axis=axis), |
129 | 127 | np.nanmax(X, axis=axis)) |
130 | 128 |
|
131 | | - def test_unique(self): |
132 | | - for X in self.data: |
133 | | - X_sparse = csr_matrix(X) |
134 | | - np.testing.assert_array_equal( |
135 | | - unique(X_sparse, return_counts=False), |
136 | | - np.unique(X, return_counts=False)) |
137 | | - |
138 | | - for a1, a2 in zip(unique(X_sparse, return_counts=True), |
139 | | - np.unique(X, return_counts=True)): |
140 | | - np.testing.assert_array_equal(a1, a2) |
141 | | - |
142 | | - def test_unique_explicit_zeros(self): |
143 | | - x1 = csr_matrix(np.eye(3)) |
144 | | - x2 = csr_matrix(np.eye(3)) |
145 | | - |
146 | | - # set some of-diagonal to explicit zeros |
147 | | - with warnings.catch_warnings(): |
148 | | - warnings.filterwarnings("ignore", |
149 | | - category=sp.sparse.SparseEfficiencyWarning) |
150 | | - x2[0, 1] = 0 |
151 | | - x2[1, 0] = 0 |
152 | | - |
153 | | - np.testing.assert_array_equal( |
154 | | - unique(x1, return_counts=False), |
155 | | - unique(x2, return_counts=False), |
156 | | - ) |
157 | | - np.testing.assert_array_equal( |
158 | | - unique(x1, return_counts=True), |
159 | | - unique(x2, return_counts=True), |
160 | | - ) |
161 | | - |
162 | | - def test_nanunique(self): |
163 | | - x = csr_matrix(np.array([0, 1, 1, np.nan])) |
164 | | - np.testing.assert_array_equal( |
165 | | - nanunique(x), |
166 | | - np.array([0, 1]) |
167 | | - ) |
168 | | - |
169 | 129 | def test_mean(self): |
170 | 130 | for X in self.data: |
171 | 131 | X_sparse = csr_matrix(X) |
@@ -420,3 +380,59 @@ def test_weights_with_transposed_x(self, array): |
420 | 380 |
|
421 | 381 | expected = [3, 0, 2, 1] |
422 | 382 | np.testing.assert_equal(bincount(x, w)[0], expected) |
| 383 | + |
| 384 | + |
| 385 | +class TestUnique(unittest.TestCase): |
| 386 | + @dense_sparse |
| 387 | + def test_returns_unique_values(self, array): |
| 388 | + # pylint: disable=bad-whitespace |
| 389 | + x = array([[-1., 1., 0., 2., 3., np.nan], |
| 390 | + [ 0., 0., 0., 3., 5., np.nan], |
| 391 | + [-1., 0., 0., 1., 7., 6.]]) |
| 392 | + expected = [-1, 0, 1, 2, 3, 5, 6, 7, np.nan, np.nan] |
| 393 | + |
| 394 | + np.testing.assert_equal(unique(x, return_counts=False), expected) |
| 395 | + |
| 396 | + @dense_sparse |
| 397 | + def test_returns_counts(self, array): |
| 398 | + # pylint: disable=bad-whitespace |
| 399 | + x = array([[-1., 1., 0., 2., 3., np.nan], |
| 400 | + [ 0., 0., 0., 3., 5., np.nan], |
| 401 | + [-1., 0., 0., 1., 7., 6.]]) |
| 402 | + expected = [2, 6, 2, 1, 2, 1, 1, 1, 1, 1] |
| 403 | + |
| 404 | + np.testing.assert_equal(unique(x, return_counts=True)[1], expected) |
| 405 | + |
| 406 | + def test_sparse_explicit_zeros(self): |
| 407 | + # Use `lil_matrix` to fix sparse warning for matrix construction |
| 408 | + x = lil_matrix(np.eye(3)) |
| 409 | + x[0, 1] = 0 |
| 410 | + x[1, 0] = 0 |
| 411 | + x = x.tocsr() |
| 412 | + # Test against identity matrix |
| 413 | + y = csr_matrix(np.eye(3)) |
| 414 | + |
| 415 | + np.testing.assert_array_equal( |
| 416 | + unique(y, return_counts=True), |
| 417 | + unique(x, return_counts=True), |
| 418 | + ) |
| 419 | + |
| 420 | + @dense_sparse |
| 421 | + def test_nanunique_ignores_nans_in_values(self, array): |
| 422 | + # pylint: disable=bad-whitespace |
| 423 | + x = array([[-1., 1., 0., 2., 3., np.nan], |
| 424 | + [ 0., 0., 0., 3., 5., np.nan], |
| 425 | + [-1., 0., 0., 1., 7., 6.]]) |
| 426 | + expected = [-1, 0, 1, 2, 3, 5, 6, 7] |
| 427 | + |
| 428 | + np.testing.assert_equal(nanunique(x, return_counts=False), expected) |
| 429 | + |
| 430 | + @dense_sparse |
| 431 | + def test_nanunique_ignores_nans_in_counts(self, array): |
| 432 | + # pylint: disable=bad-whitespace |
| 433 | + x = array([[-1., 1., 0., 2., 3., np.nan], |
| 434 | + [ 0., 0., 0., 3., 5., np.nan], |
| 435 | + [-1., 0., 0., 1., 7., 6.]]) |
| 436 | + expected = [2, 6, 2, 1, 2, 1, 1, 1] |
| 437 | + |
| 438 | + np.testing.assert_equal(nanunique(x, return_counts=True)[1], expected) |
0 commit comments