Skip to content

Commit c20f1b1

Browse files
committed
Projections widgets: Keep colors after merging
1 parent 4ad7608 commit c20f1b1

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Orange/widgets/visualize/tests/test_owprojectionwidget.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from Orange.widgets.tests.base import (
1212
WidgetTest, WidgetOutputsTestMixin, ProjectionWidgetTestMixin
1313
)
14-
from Orange.widgets.utils.colorpalettes import ContinuousPalettes
14+
from Orange.widgets.utils.colorpalettes import (
15+
ContinuousPalettes, DiscretePalette
16+
)
1517
from Orange.widgets.visualize.utils.widget import (
1618
OWDataProjectionWidget, OWProjectionWidgetBase
1719
)
@@ -113,6 +115,33 @@ def test_get_tooltip(self):
113115
and "3" in widget.get_tooltip([0, 1]))
114116
self.assertEqual(widget.get_tooltip([]), "")
115117

118+
def test_get_palette(self):
119+
widget = self.widget
120+
121+
widget.attr_color = None
122+
self.assertIsNone(widget.get_palette())
123+
124+
var = ContinuousVariable("v")
125+
var.palette = Mock()
126+
widget.attr_color = var
127+
self.assertIs(widget.get_palette(), var.palette)
128+
129+
var = DiscreteVariable("v", values=tuple("abc"))
130+
var.palette = Mock()
131+
widget.attr_color = var
132+
self.assertIs(widget.get_palette(), var.palette)
133+
134+
values = tuple("abcdefghijklmn")
135+
merged = ["a", "c", "d", "h", "n", "Others"]
136+
var = DiscreteVariable("v", values=values)
137+
var.palette = DiscretePalette(
138+
"foo", "bar", [[i] * 3 for i, _ in enumerate(values)])
139+
widget.get_color_labels = lambda: merged
140+
widget.attr_color = var
141+
np.testing.assert_equal(
142+
widget.get_palette().palette[:-1],
143+
[[var.values.index(label)] * 3 for label in merged[:-1]])
144+
116145

117146
class TestableDataProjectionWidget(OWDataProjectionWidget):
118147
def get_embedding(self):

Orange/widgets/visualize/utils/widget.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from Orange.widgets.settings import (
1717
Setting, ContextSetting, DomainContextHandler, SettingProvider
1818
)
19+
from Orange.widgets.utils import colorpalettes
1920
from Orange.widgets.utils.annotated_data import (
2021
create_annotated_table, ANNOTATED_DATA_SIGNAL_NAME, create_groups_table
2122
)
@@ -222,7 +223,17 @@ def get_palette(self):
222223
This method must be overridden if the widget offers coloring that is
223224
not based on attribute values.
224225
"""
225-
return self.attr_color and self.attr_color.palette
226+
attr = self.attr_color
227+
if not attr:
228+
return None
229+
palette = attr.palette
230+
if attr.is_discrete and len(attr.values) >= MAX_COLORS:
231+
values = self.get_color_labels()
232+
colors = [palette.palette[attr.to_val(value)]
233+
for value in values[:-1]] + [[192, 192, 192]]
234+
235+
palette = colorpalettes.DiscretePalette.from_colors(colors)
236+
return palette
226237

227238
def can_draw_density(self):
228239
"""

0 commit comments

Comments
 (0)