Skip to content

Commit

Permalink
Merge pull request #4867 from janezd/table-numpy-convert-to-csr
Browse files Browse the repository at this point in the history
Table.numpy: Convert non-csr/csc matrices to csr
  • Loading branch information
markotoplak authored Jun 26, 2020
2 parents 9fe7128 + 2317523 commit 03a1609
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,8 @@ def ninstances(array):
% (ninstances(array), shape_1))

if sp.issparse(array):
if not (sp.isspmatrix_csr(array) or sp.isspmatrix_csc(array)):
array = array.tocsr()
array.data = np.asarray(array.data)
has_inf = _check_inf(array.data)
else:
Expand Down
23 changes: 23 additions & 0 deletions Orange/data/tests/test_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

import numpy as np
import scipy.sparse as sp

from Orange.data import (
ContinuousVariable, DiscreteVariable, StringVariable,
Expand Down Expand Up @@ -85,6 +86,28 @@ def test_from_numpy(self):
self.assertRaises(ValueError, func, dom, X, Y, metas[:4])
self.assertRaises(ValueError, func, dom, X, Y[:4])

def test_from_numpy_sparse(self):
domain = Domain([ContinuousVariable(c) for c in "abc"])
x = np.arange(12).reshape(4, 3)

t = Table.from_numpy(domain, x, None, None)
self.assertFalse(sp.issparse(t.X))

t = Table.from_numpy(domain, sp.csr_matrix(x))
self.assertTrue(sp.isspmatrix_csr(t.X))

t = Table.from_numpy(domain, sp.csc_matrix(x))
self.assertTrue(sp.isspmatrix_csc(t.X))

t = Table.from_numpy(domain, sp.coo_matrix(x))
self.assertTrue(sp.isspmatrix_csr(t.X))

t = Table.from_numpy(domain, sp.lil_matrix(x))
self.assertTrue(sp.isspmatrix_csr(t.X))

t = Table.from_numpy(domain, sp.bsr_matrix(x))
self.assertTrue(sp.isspmatrix_csr(t.X))


class TestTableFilters(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 03a1609

Please sign in to comment.