Skip to content

Commit 274300b

Browse files
committed
data.util.array_equal: More efficient comparison
1 parent 53dcc22 commit 274300b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Orange/data/util.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,16 @@ def array_equal(a1, a2):
9999
"""array_equal that supports sparse and dense arrays with missing values"""
100100
if a1.shape != a2.shape:
101101
return False
102-
i1, j1, v1 = sp.find(a1)
103-
i2, j2, v2 = sp.find(a2)
104-
a1 = v1 if sp.issparse(a1) else a1[i2, j2]
105-
a2 = v2 if sp.issparse(a2) else a2[i1, j1]
106-
index_equal = set(zip(i1, j1)) == set(zip(i2, j2))
107-
return index_equal and np.allclose(a1, a2, equal_nan=True)
102+
103+
if not (sp.issparse(a1) or sp.issparse(a2)): # Both dense: just compare
104+
return np.allclose(a1, a2, equal_nan=True)
105+
106+
v1 = np.vstack(sp.find(a1)).T
107+
v2 = np.vstack(sp.find(a2)).T
108+
if not (sp.issparse(a1) and sp.issparse(a2)): # Any dense: order indices
109+
v1.sort(axis=0)
110+
v2.sort(axis=0)
111+
return np.allclose(v1, v2, equal_nan=True)
108112

109113

110114
def assure_array_dense(a):

Orange/tests/test_util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,17 @@ def test_csc_array_equal(self):
144144
a2 = sp.csc_matrix(([5, 1, 4], ([1, 0, 0], [2, 0, 2])), shape=(2, 3))
145145
a2[0, 1] = 0 # explicitly setting to 0
146146
self.assertTrue(array_equal(a1, a2))
147+
148+
def test_csc_scr_equal(self):
149+
a1 = sp.csc_matrix(([1, 4, 5], ([0, 0, 1], [0, 2, 2])), shape=(2, 3))
150+
a2 = sp.csr_matrix(([5, 1, 4], ([1, 0, 0], [2, 0, 2])), shape=(2, 3))
151+
self.assertTrue(array_equal(a1, a2))
152+
153+
a1 = sp.csc_matrix(([1, 4, 5], ([0, 0, 1], [0, 2, 2])), shape=(2, 3))
154+
a2 = sp.csr_matrix(([1, 4, 5], ([0, 0, 1], [0, 2, 2])), shape=(2, 3))
155+
self.assertTrue(array_equal(a1, a2))
156+
157+
def test_csc_unordered_array_equal(self):
158+
a1 = sp.csc_matrix(([1, 4, 5], [0, 0, 1], [0, 1, 1, 3]), shape=(2, 3))
159+
a2 = sp.csc_matrix(([1, 5, 4], [0, 1, 0], [0, 1, 1, 3]), shape=(2, 3))
160+
self.assertTrue(array_equal(a1, a2))

0 commit comments

Comments
 (0)