From 985a52c4819494ad4d058841a42b9fd5b221b5e9 Mon Sep 17 00:00:00 2001 From: Marko Toplak Date: Wed, 26 Jan 2022 16:50:18 +0100 Subject: [PATCH] Ensure unlockable sparse arrays after Table.copy --- Orange/data/table.py | 7 ++++--- Orange/tests/test_table.py | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Orange/data/table.py b/Orange/data/table.py index 9cefb1f2b34..6fd95fd3517 100644 --- a/Orange/data/table.py +++ b/Orange/data/table.py @@ -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() diff --git a/Orange/tests/test_table.py b/Orange/tests/test_table.py index f82c9921ae7..7963a45bb08 100644 --- a/Orange/tests/test_table.py +++ b/Orange/tests/test_table.py @@ -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(