Skip to content

Commit a10f06a

Browse files
committed
SOM: Fix crash when color is constant
1 parent 5c5c3be commit a10f06a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Orange/widgets/unsupervised/owsom.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,17 @@ def set_color_bins(self):
833833
binning = decimal_binnings(col, min_bins=4)[-1]
834834
self.thresholds = binning.thresholds[1:-1]
835835
self.bin_labels = (binning.labels[1:-1], binning.short_labels[1:-1])
836+
if not self.bin_labels[0] and binning.labels:
837+
# Nan's are already filtered out, but it doesn't hurt much
838+
# to use nanmax/nanmin
839+
if np.nanmin(col) == np.nanmax(col):
840+
# Handle a degenerate case with a single value
841+
# Use the second threshold (because value must be smaller),
842+
# but the first threshold as label (because that's the
843+
# actual value in the data.
844+
self.thresholds = binning.thresholds[1:]
845+
self.bin_labels = (binning.labels[:1],
846+
binning.short_labels[:1])
836847
palette = BinnedContinuousPalette.from_palette(
837848
self.attr_color.palette, binning.thresholds)
838849
self.colors = palette
@@ -871,6 +882,8 @@ def create_legend(self):
871882

872883
def _bin_names(self):
873884
labels, short_labels = self.bin_labels
885+
if len(labels) <= 1:
886+
return labels
874887
return \
875888
[f"< {labels[0]}"] \
876889
+ [f"{x} - {y}" for x, y in zip(labels, short_labels[1:])] \

Orange/widgets/unsupervised/tests/test_owsom.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from Orange.data import Table, Domain
1010
from Orange.widgets.tests.base import WidgetTest
11+
from Orange.widgets.tests.utils import simulate
1112
from Orange.widgets.utils.annotated_data import ANNOTATED_DATA_FEATURE_NAME
1213
from Orange.widgets.unsupervised.owsom import OWSOM, SomView, SOM
1314

@@ -220,6 +221,14 @@ def test_attr_color_change(self):
220221
self.assertIsNotNone(widget.thresholds)
221222
widget._redraw.assert_called()
222223

224+
def test_colored_circles_with_constant(self):
225+
with self.iris.unlocked():
226+
self.iris.X[:, 0] = 1
227+
self.send_signal(self.widget.Inputs.data, self.iris)
228+
combo = self.widget.controls.attr_color
229+
simulate.combobox_activate_index(
230+
combo, combo.model().indexOf(self.iris.domain.attributes[0]))
231+
223232
@_patch_recompute_som
224233
def test_cell_sizes(self):
225234
widget = self.widget

0 commit comments

Comments
 (0)