From 8ed204d1cee73c9c0f01f3306b3df055ae670dce Mon Sep 17 00:00:00 2001 From: nikicc Date: Tue, 18 Jul 2017 17:20:23 +0200 Subject: [PATCH] Gui: Fixups according to improved sparsity --- Orange/widgets/data/tests/test_owfile.py | 3 +-- Orange/widgets/data/tests/test_owmergedata.py | 3 +-- Orange/widgets/model/tests/test_tree.py | 7 ++----- .../widgets/unsupervised/tests/test_owmanifoldlearning.py | 3 +-- Orange/widgets/visualize/owheatmap.py | 3 +-- Orange/widgets/visualize/owscatterplotgraph.py | 8 +------- Orange/widgets/visualize/owsieve.py | 3 +-- Orange/widgets/visualize/tests/test_owscatterplot.py | 7 ++----- Orange/widgets/visualize/tests/test_owsieve.py | 3 +-- 9 files changed, 11 insertions(+), 29 deletions(-) diff --git a/Orange/widgets/data/tests/test_owfile.py b/Orange/widgets/data/tests/test_owfile.py index fce9f8299dc..bf3e7a5fa67 100644 --- a/Orange/widgets/data/tests/test_owfile.py +++ b/Orange/widgets/data/tests/test_owfile.py @@ -308,8 +308,7 @@ def test_no_specified_reader(self): self.assertTrue(self.widget.Error.missing_reader.is_shown()) def test_domain_edit_on_sparse_data(self): - iris = Table("iris") - iris.X = sp.csr_matrix(iris.X) + iris = Table("iris").to_sparse() f = tempfile.NamedTemporaryFile(suffix='.pickle', delete=False) pickle.dump(iris, f) diff --git a/Orange/widgets/data/tests/test_owmergedata.py b/Orange/widgets/data/tests/test_owmergedata.py index f125c1fc38a..9348e0c5c1d 100644 --- a/Orange/widgets/data/tests/test_owmergedata.py +++ b/Orange/widgets/data/tests/test_owmergedata.py @@ -435,8 +435,7 @@ def test_sparse(self): """ data = Table("iris")[::25] data_ed_dense = Table("titanic")[::300] - data_ed_sparse = Table("titanic")[::300] - data_ed_sparse.X = sp.csr_matrix(data_ed_sparse.X) + data_ed_sparse = Table("titanic")[::300].to_sparse() self.send_signal("Data", data) self.send_signal("Extra Data", data_ed_dense) diff --git a/Orange/widgets/model/tests/test_tree.py b/Orange/widgets/model/tests/test_tree.py index 18f48061892..2f3d919d39b 100644 --- a/Orange/widgets/model/tests/test_tree.py +++ b/Orange/widgets/model/tests/test_tree.py @@ -1,6 +1,5 @@ # pylint: disable=protected-access import numpy as np -import scipy.sparse as sp from Orange.base import Model from Orange.data import Table @@ -49,8 +48,7 @@ def test_sparse_data_classification(self): table1 = Table("iris") self.send_signal("Data", table1) model_dense = self.get_output("Model") - table2 = Table("iris") - table2.X = sp.csr_matrix(table2.X) + table2 = Table("iris").to_sparse() self.send_signal("Data", table2) model_sparse = self.get_output("Model") self.assertTrue(np.array_equal(model_dense._code, model_sparse._code)) @@ -64,8 +62,7 @@ def test_sparse_data_regression(self): table1 = Table("housing") self.send_signal("Data", table1) model_dense = self.get_output("Model") - table2 = Table("housing") - table2.X = sp.csr_matrix(table2.X) + table2 = Table("housing").to_sparse() self.send_signal("Data", table2) model_sparse = self.get_output("Model") self.assertTrue(np.array_equal(model_dense._code, model_sparse._code)) diff --git a/Orange/widgets/unsupervised/tests/test_owmanifoldlearning.py b/Orange/widgets/unsupervised/tests/test_owmanifoldlearning.py index 7578637a357..bdca5b1ac48 100644 --- a/Orange/widgets/unsupervised/tests/test_owmanifoldlearning.py +++ b/Orange/widgets/unsupervised/tests/test_owmanifoldlearning.py @@ -66,8 +66,7 @@ def _compare_tables(self, _output, n_components): np.testing.assert_array_equal(self.iris.metas, _output.metas) def test_sparse_data(self): - data = Table("iris") - data.X = sparse.csr_matrix(data.X) + data = Table("iris").to_sparse() self.assertTrue(sparse.issparse(data.X)) self.widget.manifold_method_index = 2 self.send_signal(self.widget.Inputs.data, data) diff --git a/Orange/widgets/visualize/owheatmap.py b/Orange/widgets/visualize/owheatmap.py index 982ed57308f..0c515c4bb97 100644 --- a/Orange/widgets/visualize/owheatmap.py +++ b/Orange/widgets/visualize/owheatmap.py @@ -663,8 +663,7 @@ def set_dataset(self, data=None): if data is not None and sp.issparse(data.X): try: - data = data.copy() - data.X = data.X.toarray() + data = data.to_dense() except MemoryError: data = None self.Error.not_enough_memory() diff --git a/Orange/widgets/visualize/owscatterplotgraph.py b/Orange/widgets/visualize/owscatterplotgraph.py index e60265a0b2a..38f95e718e0 100644 --- a/Orange/widgets/visualize/owscatterplotgraph.py +++ b/Orange/widgets/visualize/owscatterplotgraph.py @@ -623,13 +623,7 @@ def sparse_to_dense(self): domain = data.domain all_attrs = domain.variables + domain.metas attrs = list(set(all_attrs) & attrs) - selected_data = data[:, attrs] - if sp.issparse(selected_data.X): - selected_data.X = selected_data.X.toarray() - if sp.issparse(selected_data.Y): - selected_data.Y = selected_data.Y.toarray() - if sp.issparse(selected_data.metas): - selected_data.metas = selected_data.metas.toarray() + selected_data = data[:, attrs].to_dense() return selected_data def _clear_plot_widget(self): diff --git a/Orange/widgets/visualize/owsieve.py b/Orange/widgets/visualize/owsieve.py index 6afebdb7386..1137876eded 100644 --- a/Orange/widgets/visualize/owsieve.py +++ b/Orange/widgets/visualize/owsieve.py @@ -209,7 +209,7 @@ def discretizer(data): discretize = Discretize( method=EqualFreq(n=4), remove_const=False, discretize_classes=True, discretize_metas=True) - return discretize(data) + return discretize(data).to_dense() return data if not data.is_sparse() and not init: @@ -219,7 +219,6 @@ def discretizer(data): self.attr_y} new_domain = data.domain.select_columns(attrs) data = Table.from_table(new_domain, data) - data.X = data.X.toarray() return discretizer(data) @Inputs.features diff --git a/Orange/widgets/visualize/tests/test_owscatterplot.py b/Orange/widgets/visualize/tests/test_owscatterplot.py index 4f455920a7f..baec6ef9d0f 100644 --- a/Orange/widgets/visualize/tests/test_owscatterplot.py +++ b/Orange/widgets/visualize/tests/test_owscatterplot.py @@ -317,11 +317,8 @@ def test_sparse(self): GH-2152 GH-2157 """ - table = Table("iris") - table.X = sp.csr_matrix(table.X) - self.assertTrue(sp.issparse(table.X)) - table.Y = sp.csr_matrix(table._Y) # pylint: disable=protected-access - self.assertTrue(sp.issparse(table.Y)) + table = Table("iris").to_sparse(sparse_attributes=True, + sparse_class=True) self.send_signal(self.widget.Inputs.data, table) self.widget.set_subset_data(table[:30]) data = self.get_output("Data") diff --git a/Orange/widgets/visualize/tests/test_owsieve.py b/Orange/widgets/visualize/tests/test_owsieve.py index 2280ba1ef0d..a8e72b44c5f 100644 --- a/Orange/widgets/visualize/tests/test_owsieve.py +++ b/Orange/widgets/visualize/tests/test_owsieve.py @@ -3,7 +3,6 @@ from math import isnan from unittest.mock import patch import numpy as np -import scipy.sparse as sp from AnyQt.QtCore import QEvent, QPoint, Qt from AnyQt.QtGui import QMouseEvent @@ -100,7 +99,7 @@ def test_sparse_data(self): output = self.get_output("Data") self.assertFalse(output.is_sparse()) - table.X = sp.csr_matrix(table.X) + table = table.to_sparse() self.send_signal(self.widget.Inputs.data, table) self.assertEqual(len(self.widget.discrete_data.domain), 2) output = self.get_output("Data")