Skip to content

Commit a36277d

Browse files
janezdastaric
authored andcommitted
Merge pull request #1934 from VesnaT/fix_sieve
[FIX] OWSieve: Fix crash for attribute with no values (cherry picked from commit 022b5d7)
1 parent 44d39e0 commit a36277d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Orange/widgets/visualize/owsieve.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ class ChiSqStats:
2929
pair of attributes. The class is also used for ranking.
3030
"""
3131
def __init__(self, data, attr1, attr2):
32+
attr1 = data.domain[attr1]
33+
attr2 = data.domain[attr2]
34+
if attr1.is_discrete and not attr1.values or \
35+
attr2.is_discrete and not attr2.values:
36+
self.p = np.nan
37+
return
3238
self.observed = get_contingency(data, attr1, attr2)
3339
self.n = np.sum(self.observed)
3440
self.probs_x = self.observed.sum(axis=0) / self.n
@@ -403,15 +409,22 @@ def _oper(attr, txt):
403409
view = self.canvasView
404410

405411
chi = ChiSqStats(self.discrete_data, disc_x, disc_y)
406-
n = chi.n
407412
max_ylabel_w = max((width(val) for val in disc_y.values), default=0)
408413
max_ylabel_w = min(max_ylabel_w, 200)
409414
x_off = width(attr_x.name) + max_ylabel_w
410415
y_off = 15
411-
square_size = min(view.width() - x_off - 35, view.height() - y_off - 50)
416+
square_size = min(view.width() - x_off - 35, view.height() - y_off - 80)
412417
square_size = max(square_size, 10)
413418
self.canvasView.setSceneRect(0, 0, view.width(), view.height())
414-
419+
if not disc_x.values or not disc_y.values:
420+
text_ = "Features {} and {} have no values".format(disc_x, disc_y) \
421+
if not disc_x.values and not disc_y.values and \
422+
disc_x != disc_y else "Feature {} has no values".format(
423+
disc_x if not disc_x.values else disc_y)
424+
text(text_, view.width() / 2 + 70, view.height() / 2,
425+
Qt.AlignRight | Qt.AlignVCenter)
426+
return
427+
n = chi.n
415428
curr_x = x_off
416429
max_xlabel_h = 0
417430
self.areas = []
@@ -452,6 +465,7 @@ def _oper(attr, txt):
452465
Qt.AlignLeft | Qt.AlignVCenter, bold=True, vertical=True)
453466
text(attr_x.name, x_off + square_size / 2, bottom,
454467
Qt.AlignHCenter | Qt.AlignTop, bold=True)
468+
bottom += 30
455469
xl = text("χ²={:.2f}, p={:.3f}".format(chi.chisq, chi.p),
456470
0, bottom)
457471
# Assume similar height for both lines

Orange/widgets/visualize/tests/test_owsieve.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
3+
import numpy as np
4+
35
from AnyQt.QtCore import QEvent, QPoint, Qt
46
from AnyQt.QtGui import QMouseEvent
57

8+
from Orange.data import DiscreteVariable, Domain, Table
69
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
710
from Orange.widgets.visualize.owsieve import OWSieveDiagram
811

@@ -26,3 +29,11 @@ def _select_data(self):
2629
QEvent.MouseButtonPress, QPoint(), Qt.LeftButton,
2730
Qt.LeftButton, Qt.KeyboardModifiers()))
2831
return [0, 4, 6, 7, 11, 17, 19, 21, 22, 24, 26, 39, 40, 43, 44, 46]
32+
33+
def test_missing_values(self):
34+
"""Check widget for dataset with missing values"""
35+
attrs = [DiscreteVariable("c1", ["a", "b", "c"])]
36+
class_var = DiscreteVariable("cls", [])
37+
X = np.array([1, 2, 0, 1, 0, 2])[:, None]
38+
data = Table(Domain(attrs, class_var), X, np.array([np.nan] * 6))
39+
self.send_signal("Data", data)

0 commit comments

Comments
 (0)