Skip to content

Commit

Permalink
Add tests Table.from_table sparsity hadling
Browse files Browse the repository at this point in the history
  • Loading branch information
nikicc committed Nov 17, 2017
1 parent 026a3ef commit 489f3af
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions Orange/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,11 +2701,16 @@ def _compare_tables(self, table1, table2):
for x in table2.domain.metas])


class TestTableSparseDenseTransformations(unittest.TestCase):
class SparseCV:
def __call__(self, data):
return sp.csr_matrix((len(data), 1))


class TestTableSparseDense(unittest.TestCase):
def setUp(self):
self.iris = Table('iris')

def test_conversion(self):
def test_sparse_dense_transformation(self):
iris = Table('iris')
iris_sparse = iris.to_sparse(sparse_attributes=True)
self.assertTrue(sp.issparse(iris_sparse.X))
Expand All @@ -2722,6 +2727,54 @@ def test_conversion(self):
self.assertFalse(sp.issparse(dense_iris.Y))
self.assertFalse(sp.issparse(dense_iris.metas))

def test_from_table_add_one_sparse_column(self):
# add one sparse feature, should remain dense
domain = self.iris.domain.copy()
domain.attributes += (
ContinuousVariable('S1', compute_value=SparseCV(), sparse=True),
)
d = self.iris.transform(domain)
self.assertFalse(sp.issparse(d.X))

def test_from_table_add_lots_of_sparse_columns(self):
n_attrs = len(self.iris.domain.attributes)

# add 2*n_attrs+1 sparse feature, should became sparse
domain = self.iris.domain.copy()
domain.attributes += tuple(
ContinuousVariable('S' + str(i), compute_value=SparseCV(), sparse=True)
for i in range(2*n_attrs + 1)
)
d = self.iris.transform(domain)
self.assertTrue(sp.issparse(d.X))

def test_from_table_replace_attrs_with_sparse(self):
# replace attrs with a sparse feature, should became sparse
domain = self.iris.domain.copy()
domain.attributes = (
ContinuousVariable('S1', compute_value=SparseCV(), sparse=True),
)
d = self.iris.transform(domain)
self.assertTrue(sp.issparse(d.X))

def test_from_table_sparse_metas(self):
# replace metas with a sparse feature, should became sparse
domain = self.iris.domain.copy()
domain._metas = (
ContinuousVariable('S1', compute_value=SparseCV(), sparse=True),
)
d = self.iris.transform(domain)
self.assertTrue(sp.issparse(d.metas))

def test_from_table_sparse_metas_with_strings(self):
# replace metas with text and 100 sparse features, should be dense
domain = self.iris.domain.copy()
domain._metas = (StringVariable('text'),) + tuple(
ContinuousVariable('S' + str(i), compute_value=SparseCV(), sparse=True)
for i in range(100)
)
d = self.iris.transform(domain)
self.assertFalse(sp.issparse(d.metas))

if __name__ == "__main__":
unittest.main()
Expand Down

0 comments on commit 489f3af

Please sign in to comment.