Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] KMeans: should not crash when there is less data rows than k #2172

Merged
merged 1 commit into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Orange/widgets/unsupervised/owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ def run_optimization(self):
try:
self.controlArea.setDisabled(True)
if not self.check_data_size(self.k_from, self.Error):
return
return False
self.check_data_size(self.k_to, self.Warning)
needed_ks = [k for k in range(self.k_from, self.k_to + 1)
if k not in self.clusterings]
if not needed_ks:
return # Skip showing progress bar
return True # Skip showing progress bar
with self.progressBar(len(needed_ks)) as progress:
for k in needed_ks:
progress.advance()
Expand All @@ -227,6 +227,7 @@ def run_optimization(self):
self.mainArea.hide()
finally:
self.controlArea.setDisabled(False)
return True

def cluster(self):
if self.k in self.clusterings or \
Expand All @@ -242,8 +243,7 @@ def cluster(self):
def apply(self):
self.clear_messages()
if self.data is not None:
if self.optimize_k:
self.run_optimization()
if self.optimize_k and self.run_optimization():
self.mainArea.show()
self.update_results()
else:
Expand Down
27 changes: 27 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ def test_report(self):
self.assertIs(report_table.call_args[0][1],
widget.table_view)

def test_not_enough_rows(self):
"""
Widget should not crash when there is less rows than k_from.
GH-2172
"""
table = Table("iris")
self.widget.controls.k_from.setValue(2)
self.widget.controls.k_to.setValue(9)
self.send_signal("Data", table[0:1, :])

def test_from_to_table(self):
"""
From and To spins and number of rows in a scores table changes.
GH-2172
"""
table = Table("iris")
k_from, k_to = 2, 9
self.widget.controls.k_from.setValue(k_from)
self.send_signal("Data", table)
check = lambda x: 2 if x - k_from + 1 < 2 else x - k_from + 1
for i in range(k_from, k_to):
self.widget.controls.k_to.setValue(i)
self.assertEqual(len(self.widget.table_view.model().scores), check(i))
for i in range(k_to, k_from, -1):
self.widget.controls.k_to.setValue(i)
self.assertEqual(len(self.widget.table_view.model().scores), check(i))


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