Skip to content

Commit 6026e33

Browse files
committed
[FIX] ZeroDivisionError owmosaic.py
ZeroDivisionError should not be thrown when a column with only NaN-s is loaded into widget. https://sentry.io/biolab/orange3/issues/204400244/ Corrects the total value and rectangles' widths.
1 parent 4a22791 commit 6026e33

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
@@ -512,6 +512,20 @@ def update_graph(self):
512512
spacing = self.SPACING
513513
bar_width = self.BAR_WIDTH
514514

515+
def get_counts(attr_vals, values):
516+
"""This function calculates rectangles' widths.
517+
If all widths are zero then all widths are set to 1."""
518+
if attr_vals == "":
519+
counts = [conditionaldict[val] for val in values]
520+
else:
521+
counts = [conditionaldict[attr_vals + "-" + val]
522+
for val in values]
523+
total = sum(counts)
524+
if total == 0:
525+
counts = [1] * len(values)
526+
total = sum(counts)
527+
return total, counts
528+
515529
def draw_data(attr_list, x0_x1, y0_y1, side, condition,
516530
total_attrs, used_attrs, used_vals, attr_vals=""):
517531
x0, x1 = x0_x1
@@ -542,12 +556,7 @@ def draw_data(attr_list, x0_x1, y0_y1, side, condition,
542556
if whole == 0:
543557
edge = (y1 - y0) / float(len(values) - 1)
544558

545-
if attr_vals == "":
546-
counts = [conditionaldict[val] for val in values]
547-
else:
548-
counts = [conditionaldict[attr_vals + "-" + val]
549-
for val in values]
550-
total = sum(counts)
559+
total, counts = get_counts(attr_vals, values)
551560

552561
# if we are visualizing the third attribute and the first attribute
553562
# has the last value, we have to reverse the order in which the
@@ -633,15 +642,7 @@ def draw_text(side, attr, x0_x1, y0_y1,
633642
# calculate position of first attribute
634643
currpos = 0
635644

636-
if attr_vals == "":
637-
counts = [conditionaldict.get(val, 1) for val in values]
638-
else:
639-
counts = [conditionaldict.get(attr_vals + "-" + val, 1)
640-
for val in values]
641-
total = sum(counts)
642-
if total == 0:
643-
counts = [1] * len(values)
644-
total = sum(counts)
645+
total, counts = get_counts(attr_vals, values)
645646

646647
aligns = [Qt.AlignTop | Qt.AlignHCenter,
647648
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
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):
@@ -194,3 +194,21 @@ def test_does_not_crash(self):
194194
data = Table("housing.tab")
195195
widget.set_data(data)
196196
vizrank.toggle()
197+
198+
def test_nan_column(self):
199+
"""
200+
A column with only NaN-s used to throw an error
201+
(ZeroDivisionError) when loaded into widget.
202+
GH-2046
203+
"""
204+
table = Table(
205+
Domain(
206+
[ContinuousVariable("a"), ContinuousVariable("b"), ContinuousVariable("c")]),
207+
np.array([
208+
[0, np.NaN, 0],
209+
[0, np.NaN, 0],
210+
[0, np.NaN, 0]
211+
])
212+
)
213+
self.send_signal("Data", table)
214+

0 commit comments

Comments
 (0)