Skip to content

Commit

Permalink
owpaintdata: Adjust color model to input dataset
Browse files Browse the repository at this point in the history
Fix a IndexError when the input dataset's class variable has more then
17 values.
  • Loading branch information
ales-erjavec committed Feb 3, 2017
1 parent d8f221a commit 784589c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Orange/widgets/data/owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ def __init__(self):

self.input_data = None
self.input_classes = []
self.input_colors = None
self.input_has_attr2 = True
self.current_tool = None
self._selected_indices = None
Expand Down Expand Up @@ -1016,9 +1017,12 @@ def _check_and_set_data(data):
if data.domain.class_vars:
self.Warning.continuous_target()
self.input_classes = ["C1"]
self.input_colors = None
y = np.zeros(len(data))
else:
self.input_classes = y.values
self.input_colors = y.colors

y = data[:, y].Y

self.input_has_attr2 = len(data.domain.attributes) >= 2
Expand All @@ -1036,7 +1040,16 @@ def reset_to_input(self):
self.undo_stack.clear()

index = self.selected_class_label()
if self.input_colors is not None:
colors = self.input_colors
else:
colors = colorpalette.DefaultRGBColors
palette = colorpalette.ColorPaletteGenerator(
number_of_colors=len(colors), rgb_colors=colors)
self.colors = palette
self.class_model.colors = palette
self.class_model[:] = self.input_classes

newindex = min(max(index, 0), len(self.class_model) - 1)
itemmodels.select_row(self.classValuesView, newindex)

Expand Down
11 changes: 10 additions & 1 deletion Orange/widgets/data/tests/test_owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from AnyQt.QtCore import QRectF, QPointF

from Orange.data import Table
from Orange.data import Table, DiscreteVariable, ContinuousVariable, Domain
from Orange.widgets.data import owpaintdata
from Orange.widgets.data.owpaintdata import OWPaintData
from Orange.widgets.tests.base import WidgetTest
Expand Down Expand Up @@ -43,3 +43,12 @@ def test_output_shares_internal_buffer(self):
np.testing.assert_equal(output1.Y, output1_copy.Y)

self.assertTrue(np.any(output1.X != output2.X))

def test_20_values_class(self):
domain = Domain(
[ContinuousVariable("A"),
ContinuousVariable("B")],
DiscreteVariable("C", values=[chr(ord("a") + i) for i in range(20)])
)
data = Table(domain, [[0.1, 0.2, "a"], [0.4, 0.7, "t"]])
self.send_signal("Data", data)

0 comments on commit 784589c

Please sign in to comment.