Skip to content

Commit 71afa1c

Browse files
committed
Table: Fix ensure_copy for sparse matrices
`ensure_copy` is checking whether an array is a view through the `.base` argument which doesn't exist on `scipy.sparse` matrices. Since `scipy.sparse` don't work with views (e.g. indexing returns a copy of the matrix) the check should only be performed for dense matrices.
1 parent 4a484f6 commit 71afa1c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Orange/data/table.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,15 +912,19 @@ def is_copy(self):
912912

913913
def ensure_copy(self):
914914
"""
915-
Ensure that the table owns its data; copy arrays when necessary
915+
Ensure that the table owns its data; copy arrays when necessary.
916+
The check is skipped for sparse matrices since they don't have views like numpy arrays.
916917
"""
917-
if self.X.base is not None:
918+
def is_view(x):
919+
return not sp.issparse(x) and x.base is not None
920+
921+
if is_view(self.X):
918922
self.X = self.X.copy()
919-
if self._Y.base is not None:
923+
if is_view(self._Y):
920924
self._Y = self._Y.copy()
921-
if self.metas.base is not None:
925+
if is_view(self.metas):
922926
self.metas = self.metas.copy()
923-
if self.W.base is not None:
927+
if is_view(self.W):
924928
self.W = self.W.copy()
925929

926930
def copy(self):

0 commit comments

Comments
 (0)