Skip to content
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
9 changes: 5 additions & 4 deletions Orange/widgets/unsupervised/owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class Warning(widget.OWWidget.Warning):
"Too few ({}) unique data instances for {} clusters"
)

INIT_METHODS = "Initialize with KMeans++", "Random initialization"
INIT_METHODS = (("Initialize with KMeans++", "k-means++"),
("Random initialization", "random"))

resizing_enabled = False
buttons_area_orientation = Qt.Vertical
Expand Down Expand Up @@ -181,7 +182,7 @@ def __init__(self):

box = gui.vBox(self.controlArea, "Initialization")
gui.comboBox(
box, self, "smart_init", items=self.INIT_METHODS,
box, self, "smart_init", items=[m[0] for m in self.INIT_METHODS],
callback=self.invalidate)

layout = QGridLayout()
Expand Down Expand Up @@ -316,7 +317,7 @@ def __launch_tasks(self, ks):
self._compute_clustering,
data=self.data,
k=k,
init=['random', 'k-means++'][self.smart_init],
init=self.INIT_METHODS[self.smart_init][1],
n_init=self.n_init,
max_iter=self.max_iterations,
silhouette=True,
Expand Down Expand Up @@ -485,7 +486,7 @@ def send_report(self):
k_clusters = self.k_from + self.selected_row()
else:
k_clusters = self.k
init_method = self.INIT_METHODS[self.smart_init]
init_method = self.INIT_METHODS[self.smart_init][0]
init_method = init_method[0].lower() + init_method[1:]
self.report_items((
("Number of clusters", k_clusters),
Expand Down
15 changes: 15 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,21 @@ def test_do_not_recluster_on_same_data(self):
self.commit_and_wait()
self.assertEqual(call_count + 1, commit.call_count)

def test_correct_smart_init(self):
# due to a bug where wrong init was passed to _compute_clustering
self.send_signal(self.widget.Inputs.data, self.iris[::10], wait=5000)
self.widget.smart_init = 0
with patch.object(self.widget, "_compute_clustering",
wraps=self.widget._compute_clustering) as compute:
self.commit_and_wait()
self.assertEqual(compute.call_args[1]['init'], "k-means++")
self.widget.invalidate() # reset caches
self.widget.smart_init = 1
with patch.object(self.widget, "_compute_clustering",
wraps=self.widget._compute_clustering) as compute:
self.commit_and_wait()
self.assertEqual(compute.call_args[1]['init'], "random")


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