Skip to content

Commit

Permalink
Merge pull request #1456 from nikicc/fix-ensure-copy
Browse files Browse the repository at this point in the history
[FIX] Table: Fix ensure_copy for sparse matrices
  • Loading branch information
astaric authored Jul 15, 2016
2 parents dc8cc2c + 75bbe05 commit 6e8eab1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,15 +912,20 @@ def is_copy(self):

def ensure_copy(self):
"""
Ensure that the table owns its data; copy arrays when necessary
Ensure that the table owns its data; copy arrays when necessary.
"""
if self.X.base is not None:
def is_view(x):
# Sparse matrices don't have views like numpy arrays. Since indexing on
# them creates copies in constructor we can skip this check here.
return not sp.issparse(x) and x.base is not None

if is_view(self.X):
self.X = self.X.copy()
if self._Y.base is not None:
if is_view(self._Y):
self._Y = self._Y.copy()
if self.metas.base is not None:
if is_view(self.metas):
self.metas = self.metas.copy()
if self.W.base is not None:
if is_view(self.W):
self.W = self.W.copy()

def copy(self):
Expand Down
13 changes: 13 additions & 0 deletions Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,19 @@ def test_copy(self):
self.assertFalse(np.all(t.Y == copy.Y))
self.assertFalse(np.all(t.metas == copy.metas))

def test_copy_sparse(self):
t = data.Table('iris')
t.X = csr_matrix(t.X)
copy = t.copy()

self.assertEqual((t.X != copy.X).nnz, 0) # sparse matrices match by content
np.testing.assert_equal(t.Y, copy.Y)
np.testing.assert_equal(t.metas, copy.metas)

self.assertNotEqual(id(t.X), id(copy.X))
self.assertNotEqual(id(t._Y), id(copy._Y))
self.assertNotEqual(id(t.metas), id(copy.metas))

def test_concatenate(self):
d1 = data.Domain([data.ContinuousVariable('a1')])
t1 = data.Table.from_numpy(d1, [[1],
Expand Down

0 comments on commit 6e8eab1

Please sign in to comment.