Skip to content

Commit

Permalink
SOM: Fix crash when color is constant
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Feb 25, 2022
1 parent 5c5c3be commit c38d639
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Orange/widgets/unsupervised/owsom.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,18 @@ def set_color_bins(self):
binning = decimal_binnings(col, min_bins=4)[-1]
self.thresholds = binning.thresholds[1:-1]
self.bin_labels = (binning.labels[1:-1], binning.short_labels[1:-1])
if not self.bin_labels[0] and binning.labels:
# Nan's are already filtered out, but it doesn't hurt much
# to use nanmax/nanmin
mn, mx = np.nanmin(col), np.nanmax(col)
if mn == mx:
# Handle a degenerate case with a single value
# Use the second threshold (because value must be smaller),
# but the first threshold as label (because that's the
# actual value in the data.
self.thresholds = binning.thresholds[1:]
self.bin_labels = (binning.labels[:1],
binning.short_labels[:1])
palette = BinnedContinuousPalette.from_palette(
self.attr_color.palette, binning.thresholds)
self.colors = palette
Expand Down Expand Up @@ -871,6 +883,8 @@ def create_legend(self):

def _bin_names(self):
labels, short_labels = self.bin_labels
if len(labels) <= 1:
return labels
return \
[f"< {labels[0]}"] \
+ [f"{x} - {y}" for x, y in zip(labels, short_labels[1:])] \
Expand Down
9 changes: 9 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owsom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from Orange.data import Table, Domain
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.utils import simulate
from Orange.widgets.utils.annotated_data import ANNOTATED_DATA_FEATURE_NAME
from Orange.widgets.unsupervised.owsom import OWSOM, SomView, SOM

Expand Down Expand Up @@ -220,6 +221,14 @@ def test_attr_color_change(self):
self.assertIsNotNone(widget.thresholds)
widget._redraw.assert_called()

def test_colored_circles_with_constant(self):
with self.iris.unlocked():
self.iris.X[:, 0] = 1
self.send_signal(self.widget.Inputs.data, self.iris)
combo = self.widget.controls.attr_color
simulate.combobox_activate_index(
combo, combo.model().indexOf(self.iris.domain.attributes[0]))

@_patch_recompute_som
def test_cell_sizes(self):
widget = self.widget
Expand Down

0 comments on commit c38d639

Please sign in to comment.