Skip to content

Commit c06940b

Browse files
committed
owcolor: fix handling changed compute_values
Variables stored in context settings were reused when constructing new domains. Therefore, there were problems if only their compute_values changed because the old context (matching names and values) still matched. After this fix the variable descriptors on the input are always used when creating colored variables instead of the ones stored in the context. Also, compute_values do not need to be stored in settings and are therefore removed.
1 parent 069f280 commit c06940b

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

Orange/widgets/data/owcolor.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class AttrDesc:
4242
new_name (str or `None`): a changed name or `None`
4343
"""
4444
def __init__(self, var):
45-
self.var = var
45+
# these objects are stored within context settings;
46+
# avoid storing compute_values
47+
self.var = var.copy(compute_value=None)
4648
self.new_name = None
4749

4850
def reset(self):
@@ -118,9 +120,9 @@ def set_value(self, i, value):
118120
self.new_values = list(self.var.values)
119121
self.new_values[i] = value
120122

121-
def create_variable(self):
122-
new_var = self.var.copy(name=self.name, values=self.values,
123-
compute_value=Identity(self.var))
123+
def create_variable(self, base_var):
124+
new_var = base_var.copy(name=self.name, values=self.values,
125+
compute_value=Identity(base_var))
124126
new_var.colors = np.asarray(self.colors)
125127
return new_var
126128

@@ -209,9 +211,9 @@ def palette_name(self):
209211
def palette_name(self, palette_name):
210212
self.new_palette_name = palette_name
211213

212-
def create_variable(self):
213-
new_var = self.var.copy(name=self.name,
214-
compute_value=Identity(self.var))
214+
def create_variable(self, base_var):
215+
new_var = base_var.copy(name=self.name,
216+
compute_value=Identity(base_var))
215217
new_var.attributes["palette"] = self.palette_name
216218
return new_var
217219

@@ -746,7 +748,7 @@ def make(variables):
746748
for var in variables:
747749
source = disc_dict if var.is_discrete else cont_dict
748750
desc = source.get(var.name)
749-
new_vars.append(desc.create_variable() if desc else var)
751+
new_vars.append(desc.create_variable(var) if desc else var)
750752
return new_vars
751753

752754
if self.data is None:

Orange/widgets/data/tests/test_owcolor.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ def test_create_variable(self):
8989
desc.set_color(2, [7, 8, 9])
9090
desc.name = "z"
9191
desc.set_value(1, "d")
92-
var = desc.create_variable()
92+
var = desc.create_variable(self.var)
9393
self.assertIsInstance(var, DiscreteVariable)
9494
self.assertEqual(var.name, "z")
9595
self.assertEqual(var.values, ("a", "d", "c"))
9696
np.testing.assert_equal(var.colors, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
9797
self.assertIsInstance(var.compute_value, Identity)
98-
self.assertIs(var.compute_value.variable, desc.var)
98+
self.assertIs(var.compute_value.variable, self.var)
9999

100100
palette = desc.var.attributes["palette"] = object()
101-
var = desc.create_variable()
101+
var = desc.create_variable(self.var)
102102
self.assertIs(desc.var.attributes["palette"], palette)
103103
self.assertFalse(hasattr(var.attributes, "palette"))
104104
self.assertIsInstance(var.compute_value, Identity)
105-
self.assertIs(var.compute_value.variable, desc.var)
105+
self.assertIs(var.compute_value.variable, self.var)
106106

107107
def test_reset(self):
108108
desc = self.desc
@@ -228,8 +228,8 @@ def test_from_dict_exceptions(self):
228228

229229
class ContAttrDescTest(unittest.TestCase):
230230
def setUp(self):
231-
x = ContinuousVariable("x")
232-
self.desc = owcolor.ContAttrDesc(x)
231+
self.var = ContinuousVariable("x")
232+
self.desc = owcolor.ContAttrDesc(self.var)
233233

234234
def test_palette(self):
235235
desc = self.desc
@@ -246,19 +246,19 @@ def test_create_variable(self):
246246
palette_name = _find_other_palette(
247247
colorpalettes.ContinuousPalettes[desc.palette_name]).name
248248
desc.palette_name = palette_name
249-
var = desc.create_variable()
249+
var = desc.create_variable(self.var)
250250
self.assertIsInstance(var, ContinuousVariable)
251251
self.assertEqual(var.name, "z")
252252
self.assertEqual(var.palette.name, palette_name)
253253
self.assertIsInstance(var.compute_value, Identity)
254-
self.assertIs(var.compute_value.variable, desc.var)
254+
self.assertIs(var.compute_value.variable, self.var)
255255

256256
colors = desc.var.attributes["colors"] = object()
257-
var = desc.create_variable()
257+
var = desc.create_variable(self.var)
258258
self.assertIs(desc.var.attributes["colors"], colors)
259259
self.assertFalse(hasattr(var.attributes, "colors"))
260260
self.assertIsInstance(var.compute_value, Identity)
261-
self.assertIs(var.compute_value.variable, desc.var)
261+
self.assertIs(var.compute_value.variable, self.var)
262262

263263
def test_reset(self):
264264
desc = self.desc

0 commit comments

Comments
 (0)