Skip to content

Commit

Permalink
Ensure unlockable sparse arrays after Table.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Jan 26, 2022
1 parent dfd2734 commit 985a52c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,9 +1465,10 @@ def ensure_copy(self):
"""

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 not sp.issparse(x):
return x.base is not None
else:
return x.data.base is not None

if is_view(self._X):
self._X = self._X.copy()
Expand Down
4 changes: 4 additions & 0 deletions Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,13 @@ def test_copy_sparse(self):
self.assertNotEqual(id(t.metas), id(copy.metas))

# ensure that copied sparse arrays do not share data
# and that both are unlockable
with t.unlocked():
t.X[0, 0] = 42
self.assertEqual(copy.X[0, 0], 5.1)
with copy.unlocked():
copy.X[0, 0] = 43
self.assertEqual(t.X[0, 0], 42)

def test_concatenate(self):
d1 = data.Domain(
Expand Down

0 comments on commit 985a52c

Please sign in to comment.