Skip to content

Commit

Permalink
Merge pull request #23 from PrimozGodec/kmeans-update
Browse files Browse the repository at this point in the history
Limited max content length of dropdowns and report in interactive k-means
  • Loading branch information
kernc committed Mar 30, 2017
2 parents fcde40d + 66eef19 commit f52cd06
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
20 changes: 14 additions & 6 deletions orangecontrib/educational/widgets/owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from Orange.widgets.widget import OWWidget
from Orange.data import DiscreteVariable, ContinuousVariable, Table, Domain
from Orange.widgets import gui, settings, widget
from Orange.canvas import report

from orangecontrib.educational.widgets.utils.kmeans import Kmeans
from orangecontrib.educational.widgets.utils.color_transform import \
Expand Down Expand Up @@ -172,19 +173,17 @@ def __init__(self):
self.options_box = gui.widgetBox(self.controlArea, "Data")
opts = dict(
widget=self.options_box, master=self, orientation=Qt.Horizontal,
callback=self.restart, sendSelectedValue=True)
policy = QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
callback=self.restart, sendSelectedValue=True,
maximumContentsLength=15)

self.cbx = gui.comboBox(value='attr_x', label='X: ', **opts)
self.cbx.setSizePolicy(policy)
self.cby = gui.comboBox(value='attr_y', label='Y: ', **opts)
self.cby.setSizePolicy(policy)

self.centroids_box = gui.widgetBox(self.controlArea, "Centroids")
self.centroid_numbers_spinner = gui.spin(
self.centroids_box, self, 'number_of_clusters',
minv=1, maxv=10, step=1, label='Number of centroids:',
alignment=Qt.AlignRight, callback=self.number_of_clusters_change,
sizePolicy=policy)
alignment=Qt.AlignRight, callback=self.number_of_clusters_change)
self.restart_button = gui.button(
self.centroids_box, self, "Randomize Positions",
callback=self.restart)
Expand Down Expand Up @@ -575,3 +574,12 @@ def send_data(self):
centroids = Table(Domain(km.data.domain.attributes), km.centroids)
self.send("Annotated Data", annotated_data)
self.send("Centroids", centroids)

def send_report(self):
if self.data is None:
return
caption = report.render_items_vert((
("Number of centroids:", self.number_of_clusters),
))
self.report_plot(self.scatter)
self.report_caption(caption)
19 changes: 19 additions & 0 deletions orangecontrib/educational/widgets/tests/test_owkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,22 @@ def test_scatter_point_dropped(self):
self.assertEqual(self.widget.k_means.k, 1)

self.assertEqual(self.widget.k_means.centroids[0].tolist(), [1, 2])

def test_send_report(self):
"""
Just test report not crashes
"""
w = self.widget

w.set_data(self.data)
w.centroid_numbers_spinner.setValue(3)
self.process_events(lambda: w.scatter.svg())
w.report_button.click()

self.assertIn("Number of centroids", w.report_html)

# if no data
w.set_data(None)
w.report_button.click()

self.assertNotIn("Number of centroids", w.report_html)

0 comments on commit f52cd06

Please sign in to comment.