Skip to content

Commit

Permalink
OWSieve: Fix crash for attribute with no values
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Jan 19, 2017
1 parent 5ff92a9 commit 0f24ca0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
42 changes: 37 additions & 5 deletions Orange/widgets/visualize/owsieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,29 @@ class ChiSqStats:
pair of attributes. The class is also used for ranking.
"""
def __init__(self, data, attr1, attr2):
self.observed = get_contingency(data, attr1, attr2)
self.n = np.sum(self.observed)
self.probs_x = self.observed.sum(axis=0) / self.n
self.probs_y = self.observed.sum(axis=1) / self.n
self.expected = np.outer(self.probs_y, self.probs_x) * self.n
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.observed, self.n = np.nan, len(data)
self.probs_x, self.probs_y = [], []
if attr1.values:
observed_x = np.unique(data.get_column_view(
data.domain.index(attr1))[0], return_counts=True)[1]
self.probs_x = observed_x / self.n
self.observed = observed_x
elif attr2.values:
observed_y = np.unique(data.get_column_view(
data.domain.index(attr2))[0], return_counts=True)[1]
self.probs_y = observed_y / self.n
self.observed = observed_y
self.expected = self.observed
else:
self.observed = get_contingency(data, attr1, attr2)
self.n = np.sum(self.observed)
self.probs_x = self.observed.sum(axis=0) / self.n
self.probs_y = self.observed.sum(axis=1) / self.n
self.expected = np.outer(self.probs_y, self.probs_x) * self.n
self.residuals = \
(self.observed - self.expected) / np.sqrt(self.expected)
self.chisqs = self.residuals ** 2
Expand Down Expand Up @@ -447,6 +465,20 @@ def _oper(attr, txt):
max_xlabel_h = max(int(xl.boundingRect().height()), max_xlabel_h)
curr_x += width

if not disc_x.values and disc_y.values:
curr_y = y_off
for y in range(len(chi.probs_y) - 1, -1, -1):
py = chi.probs_y[y]
if py == 0:
continue
text(disc_y.values[y], x_off, curr_y + square_size * py / 2,
Qt.AlignRight | Qt.AlignVCenter)
curr_y += square_size * py

if not disc_x.values or not disc_y.values:
text("No data", square_size / 2 + x_off + 30,
square_size / 2 + y_off, Qt.AlignRight | Qt.AlignVCenter)

bottom = y_off + square_size + max_xlabel_h
text(attr_y.name, 0, y_off + square_size / 2,
Qt.AlignLeft | Qt.AlignVCenter, bold=True, vertical=True)
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)

0 comments on commit 0f24ca0

Please sign in to comment.