Skip to content

Commit

Permalink
Merge pull request #3156 from matejklemen/enh_boxplot_disc_sorting
Browse files Browse the repository at this point in the history
[ENH] OWBoxPlot: add option to sort discrete distributions by size
  • Loading branch information
astaric authored Aug 24, 2018
2 parents df4915a + 628495f commit 7deb7c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Orange/widgets/visualize/owboxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class Outputs:
sig_threshold = Setting(0.05)
stretched = Setting(True)
show_labels = Setting(True)
sort_freqs = Setting(False)
auto_commit = Setting(True)

_sorting_criteria_attrs = {
Expand Down Expand Up @@ -255,6 +256,9 @@ def __init__(self):
gui.checkBox(
box, self, 'show_labels', "Show box labels",
callback=self.display_changed)
self.sort_cb = gui.checkBox(
box, self, 'sort_freqs', "Sort by subgroup frequencies",
callback=self.display_changed)
gui.rubber(box)

gui.auto_commit(self.controlArea, self, "auto_commit",
Expand Down Expand Up @@ -457,11 +461,13 @@ def update_display_box(self):
else:
self.stretching_box.show()
self.display_box.hide()
self.sort_cb.setEnabled(self.group_var is not None)

def clear_scene(self):
self.closeContext()
self.box_scene.clearSelection()
self.box_scene.clear()
self.box_view.viewport().update()
self.attr_labels = []
self.labels = []
self.boxes = []
Expand Down Expand Up @@ -563,22 +569,28 @@ def display_changed_disc(self):
self.labels = [
QGraphicsTextItem(str(int(sum(self.dist))))]

self.order = list(range(len(self.attr_labels)))

self.draw_axis_disc()
if self.group_var:
self.boxes = \
[self.strudel(cont, i) for i, cont in enumerate(self.conts)
if np.sum(cont) > 0]
self.conts = self.conts[np.sum(np.array(self.conts), axis=1) > 0]

if self.sort_freqs:
self.order = sorted(self.order, key=(-np.sum(self.conts, axis=1)).__getitem__)
else:
self.boxes = [self.strudel(self.dist)]

for row, box in enumerate(self.boxes):
for row, box_index in enumerate(self.order):
y = (-len(self.boxes) + row) * 40 + 10
box = self.boxes[box_index]
bars, labels = box[::2], box[1::2]

self.__draw_group_labels(y, row)
self.__draw_group_labels(y, box_index)
if not self.stretched:
self.__draw_row_counts(y, row)
self.__draw_row_counts(y, box_index)
if self.show_labels and self.attribute is not self.group_var:
self.__draw_bar_labels(y, bars, labels)
self.__draw_bars(y, bars)
Expand Down
14 changes: 14 additions & 0 deletions Orange/widgets/visualize/tests/test_owboxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ def test_empty_groups(self):
self.send_signal(self.widget.Inputs.data, table)
self.assertEqual(2, len(self.widget.boxes))

def test_sorting_disc_group_var(self):
"""Test if subgroups are sorted by their size"""
table = Table("adult_sample")
self.send_signal(self.widget.Inputs.data, table)
self.__select_variable("education")
self.__select_group("workclass")

# checkbox not checked - preserve original order of selected grouping attribute
self.assertListEqual(self.widget.order, [0, 1, 2, 3, 4, 5, 6])

# checkbox checked - sort by frequencies
self.widget.controls.sort_freqs.setChecked(True)
self.assertListEqual(self.widget.order, [0, 1, 4, 5, 3, 2, 6])

def _select_data(self):
items = [item for item in self.widget.box_scene.items()
if isinstance(item, FilterGraphicsRectItem)]
Expand Down

0 comments on commit 7deb7c3

Please sign in to comment.