Skip to content

Commit 9386f4f

Browse files
committed
Merge pull request biolab#1893 from janezd/boxplot-fix-missing-stats
Boxplot: Fix crashes when stats can't be computed (cherry picked from commit e01a903)
1 parent eb1329a commit 9386f4f

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Orange/widgets/visualize/owboxplot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,11 @@ def display_changed(self):
447447
self.order = list(range(len(self.stats)))
448448
criterion = self._sorting_criteria_attrs[self.compare]
449449
if criterion:
450-
self.order = sorted(
451-
self.order, key=lambda i: getattr(self.stats[i], criterion))
450+
vals = [getattr(stat, criterion) for stat in self.stats]
451+
overmax = max((val for val in vals if val is not None), default=0) \
452+
+ 1
453+
vals = [val if val is not None else overmax for val in vals]
454+
self.order = sorted(self.order, key=vals.__getitem__)
452455

453456
heights = 90 if self.show_annotations else 60
454457

@@ -553,6 +556,8 @@ def compute_tests(self):
553556
# The non-parametric tests can't do this, so we use statistics.tests
554557
def stat_ttest():
555558
d1, d2 = self.stats
559+
if d1.n == 0 or d2.n == 0:
560+
return np.nan, np.nan
556561
pooled_var = d1.var / d1.n + d2.var / d2.n
557562
df = pooled_var ** 2 / \
558563
((d1.var / d1.n) ** 2 / (d1.n - 1) +
@@ -566,6 +571,8 @@ def stat_ttest():
566571
# TODO: Check this function
567572
# noinspection PyPep8Naming
568573
def stat_ANOVA():
574+
if any(stat.n == 0 for stat in self.stats):
575+
return np.nan, np.nan
569576
n = sum(stat.n for stat in self.stats)
570577
grand_avg = sum(stat.n * stat.mean for stat in self.stats) / n
571578
var_between = sum(stat.n * (stat.mean - grand_avg) ** 2
@@ -892,8 +899,11 @@ def line(y0, y1):
892899
y_up = -len(self.stats) * height + 10
893900
for pos, box_index in enumerate(self.order):
894901
stat = self.stats[box_index]
895-
x = getattr(stat, crit_line) * self.scale_x
896-
xs.append(x)
902+
x = getattr(stat, crit_line)
903+
if x is None:
904+
continue
905+
x *= self.scale_x
906+
xs.append(x * self.scale_x)
897907
by = y_up + pos * height
898908
line(by + 12, 3)
899909
line(by - 12, by - 25)

Orange/widgets/visualize/tests/test_owboxplot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def select_group(i):
128128
'rest ECG', 'cholesterol',
129129
'fasting blood sugar > 120', 'diameter narrowing'])
130130

131+
def test_box_order_when_missing_stats(self):
132+
self.widget.compare = 1
133+
# The widget can't do anything smart here, but shouldn't crash
134+
self.send_signal("Data", self.iris[49:51])
135+
131136
def test_saved_selection(self):
132137
self.send_signal("Data", self.data)
133138
selected_indices = self._select_data()

0 commit comments

Comments
 (0)