Skip to content

Commit

Permalink
Merge pull request #6989 from ales-erjavec/fixes/owhierarchicalcluste…
Browse files Browse the repository at this point in the history
…ring-colors-index-error

[FIX] owhierarchicalclustering: Fix IndexError
  • Loading branch information
janezd authored Jan 17, 2025
2 parents 876acb2 + bb1c1c7 commit f3d9e19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Orange/widgets/unsupervised/owhierarchicalclustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ def data(self, index, role=Qt.DisplayRole):
return font
if role == Qt.BackgroundRole:
if self.__colors is not None:
return self.__colors[index.row()]
if index.row() < len(self.__colors):
return self.__colors[index.row()]
else:
return QColor()
elif not any(self) and self.subset: # no labels, no color, but subset
return QColor(0, 0, 0)
if role == Qt.UserRole and self.subset:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring, protected-access
import unittest
import warnings

import numpy as np

from AnyQt.QtCore import QPoint, Qt
from AnyQt.QtGui import QColor
from AnyQt.QtTest import QTest

import Orange.misc
Expand All @@ -13,7 +15,7 @@
from Orange.misc import DistMatrix
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin
from Orange.widgets.unsupervised.owhierarchicalclustering import \
OWHierarchicalClustering
OWHierarchicalClustering, SelectedLabelsModel


class TestOWHierarchicalClustering(WidgetTest, WidgetOutputsTestMixin):
Expand Down Expand Up @@ -226,3 +228,16 @@ def test_many_values_warning(self):

self.send_signal(self.widget.Inputs.distances, None)
self.assertFalse(w.Warning.many_clusters.is_shown())


class TestSelectedLabelsModel(unittest.TestCase):
def test_model_extend(self):
model = SelectedLabelsModel()
model[:] = ["1"]
model.set_colors([QColor(Qt.blue)])
index = model.index(0)
self.assertEqual(index.data(Qt.DisplayRole), "1")
self.assertEqual(index.data(Qt.BackgroundRole), QColor(Qt.blue))
model[:]= ["1", "2"]
index1 = model.index(1)
self.assertEqual(index1.data(Qt.BackgroundRole), QColor()) # should be invalid color

0 comments on commit f3d9e19

Please sign in to comment.