Skip to content

Commit

Permalink
Merge pull request #3142 from robertcv/fixes/kmeans_error
Browse files Browse the repository at this point in the history
OWKMeans: add error for data with no features
  • Loading branch information
ajdapretnar authored Jul 20, 2018
2 parents 371a193 + 02c3ce2 commit a48c890
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Orange/widgets/unsupervised/owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Error(widget.OWWidget.Error):
not_enough_data = widget.Msg(
"Too few ({}) unique data instances for {} clusters"
)
no_attributes = widget.Msg("Data is missing features.")

class Warning(widget.OWWidget.Warning):
no_silhouettes = widget.Msg(
Expand Down Expand Up @@ -255,6 +256,10 @@ def enough_data_instances(self, k):
"""k cannot be larger than the number of data instances."""
return len(self.data) >= k

@property
def has_attributes(self):
return len(self.data.domain.attributes)

@staticmethod
def _compute_clustering(data, k, init, n_init, max_iter, silhouette, random_state):
# type: (Table, int, str, int, int, bool) -> KMeansModel
Expand Down Expand Up @@ -391,12 +396,18 @@ def commit(self):
# cause flickering when the clusters are computed quickly, so this is
# the better alternative
self.table_model.clear_scores()
self.mainArea.setVisible(self.optimize_k and self.data is not None)
self.mainArea.setVisible(self.optimize_k and self.data is not None and
self.has_attributes)

if self.data is None:
self.send_data()
return

if not self.has_attributes:
self.Error.no_attributes()
self.send_data()
return

if self.optimize_k:
self.run_optimization()
else:
Expand Down
7 changes: 7 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ def assert_all_same(l):
self.widget.smart_init = 1
assert_all_same([cluster() for _ in range(5)])

def test_error_no_attributes(self):
domain = Domain([])
table = Table.from_domain(domain, n_rows=10)
self.widget.auto_commit = True
self.send_signal(self.widget.Inputs.data, table)
self.assertTrue(self.widget.Error.no_attributes.is_shown())


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

0 comments on commit a48c890

Please sign in to comment.