Skip to content

Commit 55d77e0

Browse files
authored
Merge pull request #5807 from markotoplak/fix-sparse-copy
[FIX] Ensure unlockable sparse arrays after Table.copy
2 parents 31305bc + 2b0cd0c commit 55d77e0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Orange/data/table.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,9 +1465,10 @@ def ensure_copy(self):
14651465
"""
14661466

14671467
def is_view(x):
1468-
# Sparse matrices don't have views like numpy arrays. Since indexing on
1469-
# them creates copies in constructor we can skip this check here.
1470-
return not sp.issparse(x) and x.base is not None
1468+
if not sp.issparse(x):
1469+
return x.base is not None
1470+
else:
1471+
return x.data.base is not None
14711472

14721473
if is_view(self._X):
14731474
self._X = self._X.copy()

Orange/tests/test_table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,13 @@ def test_copy_sparse(self):
495495
self.assertNotEqual(id(t.metas), id(copy.metas))
496496

497497
# ensure that copied sparse arrays do not share data
498+
# and that both are unlockable
498499
with t.unlocked():
499500
t.X[0, 0] = 42
500501
self.assertEqual(copy.X[0, 0], 5.1)
502+
with copy.unlocked():
503+
copy.X[0, 0] = 43
504+
self.assertEqual(t.X[0, 0], 42)
501505

502506
def test_concatenate(self):
503507
d1 = data.Domain(

0 commit comments

Comments
 (0)