Skip to content

Commit

Permalink
Merge pull request biolab#1807 from kernc/discretevar/ncolors--nvalues
Browse files Browse the repository at this point in the history
DiscreteVariable: reset colors when new values added
(cherry picked from commit 5fabe61)
  • Loading branch information
janezd authored and astaric committed Jan 10, 2017
1 parent 27e369e commit 6e3847e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
14 changes: 6 additions & 8 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,11 @@ def __init__(self, name="", values=(), ordered=False, base_value=-1, compute_val
@property
def colors(self):
if self._colors is None:
if "colors" in self.attributes:
self._colors = np.array(
[hex_to_color(col) for col in self.attributes["colors"]],
dtype=np.uint8)
else:
from Orange.widgets.utils.colorpalette import \
ColorPaletteGenerator
self._colors = ColorPaletteGenerator.palette(self)
from Orange.widgets.utils.colorpalette import ColorPaletteGenerator
self._colors = ColorPaletteGenerator.palette(self)
colors = self.attributes.get('colors')
if colors:
self._colors[:len(colors)] = [hex_to_color(color) for color in colors]
self._colors.flags.writeable = False
return self._colors

Expand Down Expand Up @@ -636,6 +633,7 @@ def add_value(self, s):
""" Add a value `s` to the list of values.
"""
self.values.append(s)
self._colors = None

def val_from_str_add(self, s):
"""
Expand Down
13 changes: 12 additions & 1 deletion Orange/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_repr(self):
repr(var),
"DiscreteVariable('a', values=['1', '2', '3', '4', '5', ...])")

@unittest.skipUnless(is_on_path("PyQt4"), "PyQt4 is not importable")
@unittest.skipUnless(is_on_path("PyQt4") or is_on_path("PyQt5"), "PyQt is not importable")
def test_colors(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertIsNone(var._colors)
Expand All @@ -236,6 +236,17 @@ def test_colors(self):
var.attributes["colors"] = ['#0a0b0c', '#0d0e0f']
np.testing.assert_almost_equal(var.colors, [[10, 11, 12], [13, 14, 15]])

# Test ncolors adapts to nvalues
var = DiscreteVariable.make('foo', values=['d', 'r'])
self.assertEqual(len(var.colors), 2)
var.add_value('e')
self.assertEqual(len(var.colors), 3)
user_defined = (0, 0, 0)
var.set_color(2, user_defined)
var.add_value('k')
self.assertEqual(len(var.colors), 4)
np.testing.assert_array_equal(var.colors[2], user_defined)


@variabletest(ContinuousVariable)
class TestContinuousVariable(VariableTest):
Expand Down

0 comments on commit 6e3847e

Please sign in to comment.