Skip to content

Commit 7ad7488

Browse files
authored
Merge pull request #2046 from jerneju/zerodivisionerror-mosaic
[FIX] ZeroDivisionError owmosaic.py
2 parents 67c587e + 6026e33 commit 7ad7488

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

Orange/widgets/visualize/owmosaic.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,20 @@ def update_graph(self):
522522
spacing = self.SPACING
523523
bar_width = self.BAR_WIDTH
524524

525+
def get_counts(attr_vals, values):
526+
"""This function calculates rectangles' widths.
527+
If all widths are zero then all widths are set to 1."""
528+
if attr_vals == "":
529+
counts = [conditionaldict[val] for val in values]
530+
else:
531+
counts = [conditionaldict[attr_vals + "-" + val]
532+
for val in values]
533+
total = sum(counts)
534+
if total == 0:
535+
counts = [1] * len(values)
536+
total = sum(counts)
537+
return total, counts
538+
525539
def draw_data(attr_list, x0_x1, y0_y1, side, condition,
526540
total_attrs, used_attrs, used_vals, attr_vals=""):
527541
x0, x1 = x0_x1
@@ -552,12 +566,7 @@ def draw_data(attr_list, x0_x1, y0_y1, side, condition,
552566
if whole == 0:
553567
edge = (y1 - y0) / float(len(values) - 1)
554568

555-
if attr_vals == "":
556-
counts = [conditionaldict[val] for val in values]
557-
else:
558-
counts = [conditionaldict[attr_vals + "-" + val]
559-
for val in values]
560-
total = sum(counts)
569+
total, counts = get_counts(attr_vals, values)
561570

562571
# if we are visualizing the third attribute and the first attribute
563572
# has the last value, we have to reverse the order in which the
@@ -643,15 +652,7 @@ def draw_text(side, attr, x0_x1, y0_y1,
643652
# calculate position of first attribute
644653
currpos = 0
645654

646-
if attr_vals == "":
647-
counts = [conditionaldict.get(val, 1) for val in values]
648-
else:
649-
counts = [conditionaldict.get(attr_vals + "-" + val, 1)
650-
for val in values]
651-
total = sum(counts)
652-
if total == 0:
653-
counts = [1] * len(values)
654-
total = sum(counts)
655+
total, counts = get_counts(attr_vals, values)
655656

656657
aligns = [Qt.AlignTop | Qt.AlignHCenter,
657658
Qt.AlignRight | Qt.AlignVCenter,

Orange/widgets/visualize/tests/test_owmosaic.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from Orange.data import Table, DiscreteVariable, Domain, ContinuousVariable, StringVariable
99
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
10-
from Orange.widgets.visualize.owmosaic import OWMosaicDisplay, MosaicVizRank
10+
from Orange.widgets.visualize.owmosaic import OWMosaicDisplay
1111

1212

1313
class TestOWMosaicDisplay(WidgetTest, WidgetOutputsTestMixin):
@@ -205,3 +205,21 @@ def test_does_not_crash(self):
205205
data = Table("housing.tab")
206206
widget.set_data(data)
207207
vizrank.toggle()
208+
209+
def test_nan_column(self):
210+
"""
211+
A column with only NaN-s used to throw an error
212+
(ZeroDivisionError) when loaded into widget.
213+
GH-2046
214+
"""
215+
table = Table(
216+
Domain(
217+
[ContinuousVariable("a"), ContinuousVariable("b"), ContinuousVariable("c")]),
218+
np.array([
219+
[0, np.NaN, 0],
220+
[0, np.NaN, 0],
221+
[0, np.NaN, 0]
222+
])
223+
)
224+
self.send_signal("Data", table)
225+

0 commit comments

Comments
 (0)