Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Color widget failed after removing Variable.make #4041

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Orange/data/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,13 @@ def test_colors(self):
var = DiscreteVariable.make("a", values=["F", "M"])
self.assertIsNone(var._colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertIs(var._colors, var.colors)
self.assertEqual(var.colors.shape, (2, 3))
self.assertFalse(var.colors.flags.writeable)

var.colors = np.arange(6).reshape((2, 3))
np.testing.assert_almost_equal(var.colors, [[0, 1, 2], [3, 4, 5]])
self.assertFalse(var.colors.flags.writeable)
with self.assertRaises(ValueError):
var.colors[0] = [42, 41, 40]
var.set_color(0, [42, 41, 40])
np.testing.assert_almost_equal(var.colors, [[42, 41, 40], [3, 4, 5]])

var = DiscreteVariable.make("x", values=["A", "B"])
var.attributes["colors"] = ['#0a0b0c', '#0d0e0f']
Expand All @@ -216,11 +212,8 @@ def test_colors(self):
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)

def test_no_nonstringvalues(self):
self.assertRaises(TypeError, DiscreteVariable, "foo", values=["a", 42])
Expand Down Expand Up @@ -433,7 +426,6 @@ def test_adjust_decimals(self):
def test_colors(self):
a = ContinuousVariable("a")
self.assertEqual(a.colors, ((0, 0, 255), (255, 255, 0), False))
self.assertIs(a.colors, a._colors)

a = ContinuousVariable("a")
a.attributes["colors"] = ['#010203', '#040506', True]
Expand Down
91 changes: 35 additions & 56 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import scipy.sparse as sp

from Orange.data import _variable
from Orange.util import Registry, color_to_hex, hex_to_color, Reprable, \
from Orange.util import Registry, hex_to_color, Reprable,\
OrangeDeprecationWarning

__all__ = ["Unknown", "MISSING_VALUES", "make_variable", "is_discrete_values",
Expand Down Expand Up @@ -326,6 +326,14 @@ def __init__(self, name="", compute_value=None, *, sparse=False):
def name(self):
return self._name

@property
def colors(self): # unreachable; pragma: no cover
return self._colors

@colors.setter
def colors(self, value):
self._colors = value

def make_proxy(self):
"""
Copy the variable and set the master to `self.master` or to `self`.
Expand Down Expand Up @@ -451,7 +459,7 @@ def __reduce__(self):
# Use make to unpickle variables.
return make_variable, (self.__class__, self._compute_value, self.name), self.__dict__

def copy(self, compute_value=None, name=None, **kwargs):
def copy(self, compute_value=None, *, name=None, **kwargs):
var = type(self)(name=name or self.name,
compute_value=compute_value or self.compute_value,
sparse=self.sparse, **kwargs)
Expand Down Expand Up @@ -513,22 +521,16 @@ def format_str(self):
def format_str(self, value):
self._format_str = value

@property
@Variable.colors.getter
def colors(self):
if self._colors is None:
try:
col1, col2, black = self.attributes["colors"]
self._colors = (hex_to_color(col1), hex_to_color(col2), black)
except (KeyError, ValueError):
# Stored colors were not available or invalid, use defaults
self._colors = ((0, 0, 255), (255, 255, 0), False)
return self._colors

@colors.setter
def colors(self, value):
col1, col2, black = self._colors = value
self.attributes["colors"] = \
[color_to_hex(col1), color_to_hex(col2), black]
if self._colors is not None:
return self._colors
try:
col1, col2, black = self.attributes["colors"]
return (hex_to_color(col1), hex_to_color(col2), black)
except (KeyError, ValueError):
# User-provided colors were not available or invalid
return ((0, 0, 255), (255, 255, 0), False)

# noinspection PyAttributeOutsideInit
@number_of_decimals.setter
Expand Down Expand Up @@ -565,7 +567,7 @@ def repr_val(self, val):

str_val = repr_val

def copy(self, compute_value=None, name=None, **kwargs):
def copy(self, compute_value=None, *, name=None, **kwargs):
var = super().copy(compute_value=compute_value, name=name,
number_of_decimals=self.number_of_decimals,
**kwargs)
Expand Down Expand Up @@ -690,29 +692,21 @@ def mapper(value, col_idx=None):

return mapper

@property
@Variable.colors.getter
def colors(self):
if self._colors is None:
if self._colors is not None:
colors = np.array(self._colors)
elif not self.values:
colors = np.zeros((0, 3)) # to match additional colors in vstacks
else:
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

@colors.setter
def colors(self, value):
self._colors = value
self._colors.flags.writeable = False
self.attributes["colors"] = [color_to_hex(col) for col in value]

def set_color(self, i, color):
self.colors = self.colors
self._colors.flags.writeable = True
self._colors[i, :] = color
self._colors.flags.writeable = False
self.attributes["colors"][i] = color_to_hex(color)
default = tuple(ColorPaletteGenerator.palette(self))
colors = self.attributes.get('colors', ())
colors = tuple(hex_to_color(color) for color in colors) \
+ default[len(colors):]
colors = np.array(colors)
colors.flags.writeable = False
return colors

def to_val(self, s):
"""
Expand Down Expand Up @@ -789,22 +783,7 @@ def __reduce__(self):
self.values, self.ordered), \
__dict__

@staticmethod
def ordered_values(values):
"""
Return a sorted list of values. If there exists a prescribed order for
such set of values, it is returned. Otherwise, values are sorted
alphabetically.
"""
for presorted in DiscreteVariable.presorted_values:
if values == set(presorted):
return presorted
try:
return sorted(values, key=float)
except ValueError:
return sorted(values)

def copy(self, compute_value=None, name=None, **_):
def copy(self, compute_value=None, *, name=None, **_):
return super().copy(compute_value=compute_value, name=name,
values=self.values, ordered=self.ordered)

Expand Down Expand Up @@ -920,7 +899,7 @@ def __init__(self, *args, have_date=0, have_time=0, **kwargs):
self.have_date = have_date
self.have_time = have_time

def copy(self, compute_value=None, name=None, **_):
def copy(self, compute_value=None, *, name=None, **_):
return super().copy(compute_value=compute_value, name=name,
have_date=self.have_date, have_time=self.have_time)

Expand Down
Loading