Skip to content

Commit 4a22791

Browse files
authored
Merge pull request #2031 from jerneju/ghissue-2020
[FIX] Fix Chi2 computation for variables with values with no instances
2 parents 48f8d1a + 0d67dc1 commit 4a22791

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Orange/widgets/visualize/owsieve.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, data, attr1, attr2):
4242
self.expected = np.outer(self.probs_y, self.probs_x) * self.n
4343
self.residuals = \
4444
(self.observed - self.expected) / np.sqrt(self.expected)
45+
self.residuals = np.nan_to_num(self.residuals)
4546
self.chisqs = self.residuals ** 2
4647
self.chisq = float(np.sum(self.chisqs))
4748
self.p = chi2.sf(

Orange/widgets/visualize/tests/test_owsieve.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Test methods with long descriptive names can omit docstrings
22
# pylint: disable=missing-docstring
3+
from math import isnan
34
import numpy as np
45

56
from AnyQt.QtCore import QEvent, QPoint, Qt
@@ -8,6 +9,7 @@
89
from Orange.data import DiscreteVariable, Domain, Table
910
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
1011
from Orange.widgets.visualize.owsieve import OWSieveDiagram
12+
from Orange.widgets.visualize.owsieve import ChiSqStats
1113

1214

1315
class TestOWSieveDiagram(WidgetTest, WidgetOutputsTestMixin):
@@ -46,3 +48,14 @@ def test_keyerror(self):
4648
data = Table("iris")
4749
data = data[0:1]
4850
self.send_signal("Data", data)
51+
52+
def test_chisquare(self):
53+
"""
54+
gh-2031
55+
Check if it can calculate chi square when there are no attributes which suppose to be.
56+
"""
57+
a = DiscreteVariable("a", values=["y", "n"])
58+
b = DiscreteVariable("b", values=["y", "n", "o"])
59+
table = Table(Domain([a, b]), list(zip("yynny", "ynyyn")))
60+
chi = ChiSqStats(table, 0, 1)
61+
self.assertFalse(isnan(chi.chisq))

0 commit comments

Comments
 (0)