Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] OWSieve: Fix crash for attribute with no values #1934

Merged
merged 2 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Orange/widgets/visualize/owsieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class ChiSqStats:
pair of attributes. The class is also used for ranking.
"""
def __init__(self, data, attr1, attr2):
attr1 = data.domain[attr1]
attr2 = data.domain[attr2]
if attr1.is_discrete and not attr1.values or \
attr2.is_discrete and not attr2.values:
self.p = np.nan
return
self.observed = get_contingency(data, attr1, attr2)
self.n = np.sum(self.observed)
self.probs_x = self.observed.sum(axis=0) / self.n
Expand Down Expand Up @@ -403,15 +409,22 @@ def _oper(attr, txt):
view = self.canvasView

chi = ChiSqStats(self.discrete_data, disc_x, disc_y)
n = chi.n
max_ylabel_w = max((width(val) for val in disc_y.values), default=0)
max_ylabel_w = min(max_ylabel_w, 200)
x_off = width(attr_x.name) + max_ylabel_w
y_off = 15
square_size = min(view.width() - x_off - 35, view.height() - y_off - 50)
square_size = min(view.width() - x_off - 35, view.height() - y_off - 80)
square_size = max(square_size, 10)
self.canvasView.setSceneRect(0, 0, view.width(), view.height())

if not disc_x.values or not disc_y.values:
text_ = "Features {} and {} have no values".format(disc_x, disc_y) \
if not disc_x.values and not disc_y.values and \
disc_x != disc_y else "Feature {} has no values".format(
disc_x if not disc_x.values else disc_y)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a normal if statement, not expression? I guess it would be so much easier to read. Refuse if you disagree.

text(text_, view.width() / 2 + 70, view.height() / 2,
Qt.AlignRight | Qt.AlignVCenter)
return
n = chi.n
curr_x = x_off
max_xlabel_h = 0
self.areas = []
Expand Down Expand Up @@ -452,6 +465,7 @@ def _oper(attr, txt):
Qt.AlignLeft | Qt.AlignVCenter, bold=True, vertical=True)
text(attr_x.name, x_off + square_size / 2, bottom,
Qt.AlignHCenter | Qt.AlignTop, bold=True)
bottom += 30
xl = text("χ²={:.2f}, p={:.3f}".format(chi.chisq, chi.p),
0, bottom)
# Assume similar height for both lines
Expand Down
11 changes: 11 additions & 0 deletions Orange/widgets/visualize/tests/test_owsieve.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import numpy as np

from AnyQt.QtCore import QEvent, QPoint, Qt
from AnyQt.QtGui import QMouseEvent

from Orange.data import DiscreteVariable, Domain, Table
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
from Orange.widgets.visualize.owsieve import OWSieveDiagram

Expand All @@ -26,3 +29,11 @@ def _select_data(self):
QEvent.MouseButtonPress, QPoint(), Qt.LeftButton,
Qt.LeftButton, Qt.KeyboardModifiers()))
return [0, 4, 6, 7, 11, 17, 19, 21, 22, 24, 26, 39, 40, 43, 44, 46]

def test_missing_values(self):
"""Check widget for dataset with missing values"""
attrs = [DiscreteVariable("c1", ["a", "b", "c"])]
class_var = DiscreteVariable("cls", [])
X = np.array([1, 2, 0, 1, 0, 2])[:, None]
data = Table(Domain(attrs, class_var), X, np.array([np.nan] * 6))
self.send_signal("Data", data)