Skip to content

Commit 59a5621

Browse files
committed
Mosaic: Handle situation when quantiles can't be computed
1 parent c3c186d commit 59a5621

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

Orange/widgets/visualize/owboxplot.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ def __init__(self, dist):
6464
q.append(float(dist[0, i]))
6565
thresh_i += 1
6666
if thresh_i == 3:
67+
self.q25, self.median, self.q75 = q
6768
break
68-
self.q25, self.median, self.q75, *_ = q + [-1] * 3
69+
else:
70+
self.q25 = self.q75 = None
71+
self.median = q[1] if len(q) == 2 else None
6972

7073

7174
class OWBoxPlot(widget.OWWidget):
@@ -429,9 +432,10 @@ def display_changed(self):
429432
else:
430433
stat = self.stats[box_index]
431434

432-
if self.compare == OWBoxPlot.CompareMedians:
435+
if self.compare == OWBoxPlot.CompareMedians and \
436+
stat.median is not None:
433437
pos = stat.median + 5 / self.scale_x
434-
elif self.compare == OWBoxPlot.CompareMeans:
438+
elif self.compare == OWBoxPlot.CompareMeans or stat.q25 is None:
435439
pos = stat.mean + 5 / self.scale_x
436440
else:
437441
pos = stat.q25
@@ -714,28 +718,31 @@ def move_label(label, frm, to):
714718
mean_lab.setPos(m, -22)
715719
line(m, -1)
716720

717-
msc = stat.median * self.scale_x
718-
med_t = centered_text(stat.median, msc)
719-
med_box_width2 = med_t.boundingRect().width()
720-
line(msc)
721-
722-
x = stat.q25 * self.scale_x
723-
t = centered_text(stat.q25, x)
724-
t_box = t.boundingRect()
725-
med_left = msc - med_box_width2
726-
if x + t_box.width() / 2 >= med_left - 5:
727-
move_label(t, x, med_left - t_box.width() - 5)
728-
else:
729-
line(x)
730-
731-
x = stat.q75 * self.scale_x
732-
t = centered_text(stat.q75, x)
733-
t_box = t.boundingRect()
734-
med_right = msc + med_box_width2
735-
if x - t_box.width() / 2 <= med_right + 5:
736-
move_label(t, x, med_right + 5)
737-
else:
738-
line(x)
721+
if stat.median is not None:
722+
msc = stat.median * self.scale_x
723+
med_t = centered_text(stat.median, msc)
724+
med_box_width2 = med_t.boundingRect().width()
725+
line(msc)
726+
727+
if stat.q25 is not None:
728+
x = stat.q25 * self.scale_x
729+
t = centered_text(stat.q25, x)
730+
t_box = t.boundingRect()
731+
med_left = msc - med_box_width2
732+
if x + t_box.width() / 2 >= med_left - 5:
733+
move_label(t, x, med_left - t_box.width() - 5)
734+
else:
735+
line(x)
736+
737+
if stat.q75 is not None:
738+
x = stat.q75 * self.scale_x
739+
t = centered_text(stat.q75, x)
740+
t_box = t.boundingRect()
741+
med_right = msc + med_box_width2
742+
if x - t_box.width() / 2 <= med_right + 5:
743+
move_label(t, x, med_right + 5)
744+
else:
745+
line(x)
739746

740747
return labels
741748

@@ -755,17 +762,19 @@ def line(x0, y0, x1, y1, *args):
755762
var_line = line(stat.mean - stat.dev, 0, stat.mean + stat.dev, 0, box)
756763
var_line.setPen(self._pen_paramet)
757764

758-
mbox = QGraphicsRectItem(stat.q25 * scale_x, -height / 2,
759-
(stat.q75 - stat.q25) * scale_x, height,
760-
box)
761-
mbox.setBrush(self._box_brush)
762-
mbox.setPen(QPen(Qt.NoPen))
763-
mbox.setZValue(-200)
764-
765-
median_line = line(stat.median, -height / 2,
766-
stat.median, height / 2, box)
767-
median_line.setPen(self._pen_median)
768-
median_line.setZValue(-150)
765+
if stat.q25 is not None and stat.q75 is not None:
766+
mbox = QGraphicsRectItem(stat.q25 * scale_x, -height / 2,
767+
(stat.q75 - stat.q25) * scale_x, height,
768+
box)
769+
mbox.setBrush(self._box_brush)
770+
mbox.setPen(QPen(Qt.NoPen))
771+
mbox.setZValue(-200)
772+
773+
if stat.median is not None:
774+
median_line = line(stat.median, -height / 2,
775+
stat.median, height / 2, box)
776+
median_line.setPen(self._pen_median)
777+
median_line.setZValue(-150)
769778

770779
return box
771780

0 commit comments

Comments
 (0)