Skip to content

Commit

Permalink
Merge pull request #2180 from VesnaT/manifold_sparse
Browse files Browse the repository at this point in the history
OWManifoldLearning: Show error msg when data is sparse
  • Loading branch information
lanzagar authored Apr 7, 2017
2 parents d514b5b + c27cce1 commit 68727b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
46 changes: 25 additions & 21 deletions Orange/widgets/unsupervised/owmanifoldlearning.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class OWManifoldLearning(OWWidget):
class Error(OWWidget.Error):
n_neighbors_too_small = Msg("Neighbors must be greater than {}.")
manifold_error = Msg("{}")
sparse_not_supported = Msg("Sparse data is not supported.")

def __init__(self):
self.data = None
Expand Down Expand Up @@ -235,27 +236,30 @@ def apply(self):
data = None
self.clear_messages()
if self.data:
with self.progressBar():
self.progressBarSet(10)
domain = Domain([ContinuousVariable("C{}".format(i))
for i in range(self.n_components)],
self.data.domain.class_vars,
self.data.domain.metas)

method = self.MANIFOLD_METHODS[self.manifold_method_index]
projector = method(**self.get_method_parameters())
try:
self.progressBarSet(20)
X = projector(self.data).embedding_
data = Table(domain, X, self.data.Y, self.data.metas)
except ValueError as e:
if e.args[0] == "for method='hessian', n_neighbors must " \
"be greater than [n_components * (" \
"n_components + 3) / 2]":
n = self.n_components * (self.n_components + 3) / 2
self.Error.n_neighbors_too_small("{}".format(n))
else:
self.Error.manifold_error(e.args[0])
if self.data.is_sparse():
self.Error.sparse_not_supported()
else:
with self.progressBar():
self.progressBarSet(10)
domain = Domain([ContinuousVariable("C{}".format(i))
for i in range(self.n_components)],
self.data.domain.class_vars,
self.data.domain.metas)

method = self.MANIFOLD_METHODS[self.manifold_method_index]
projector = method(**self.get_method_parameters())
try:
self.progressBarSet(20)
X = projector(self.data).embedding_
data = Table(domain, X, self.data.Y, self.data.metas)
except ValueError as e:
if e.args[0] == "for method='hessian', n_neighbors " \
"must be greater than [n_components" \
" * (n_components + 3) / 2]":
n = self.n_components * (self.n_components + 3) / 2
self.Error.n_neighbors_too_small("{}".format(n))
else:
self.Error.manifold_error(e.args[0])
self.send("Transformed data", data)

def get_method_parameters(self):
Expand Down
12 changes: 12 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owmanifoldlearning.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import numpy as np
from scipy import sparse

from Orange.data import Table
from Orange.widgets.unsupervised.owmanifoldlearning import OWManifoldLearning
Expand Down Expand Up @@ -61,3 +62,14 @@ def _compare_tables(self, _output, n_components):
self.assertEqual((len(self.iris), n_components), _output.X.shape)
np.testing.assert_array_equal(self.iris.Y, _output.Y)
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)
self.assertTrue(sparse.issparse(data.X))
self.send_signal("Data", data)
self.widget.apply_button.button.click()
self.assertTrue(self.widget.Error.sparse_not_supported.is_shown())
self.send_signal("Data", None)
self.widget.apply_button.button.click()
self.assertFalse(self.widget.Error.sparse_not_supported.is_shown())

0 comments on commit 68727b4

Please sign in to comment.