Skip to content

Commit aaeac3e

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 aaeac3e

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def test_name(self):
3131
desc.name = None
3232
self.assertEqual(desc.name, "x")
3333

34+
def test_no_compute_value(self):
35+
x = ContinuousVariable("x", compute_value=lambda x: 42)
36+
desc = owcolor.AttrDesc(x)
37+
self.assertIsNone(desc.var.compute_value)
38+
3439
def test_reset(self):
3540
x = ContinuousVariable("x")
3641
desc = owcolor.AttrDesc(x)
@@ -89,20 +94,20 @@ def test_create_variable(self):
8994
desc.set_color(2, [7, 8, 9])
9095
desc.name = "z"
9196
desc.set_value(1, "d")
92-
var = desc.create_variable()
97+
var = desc.create_variable(self.var)
9398
self.assertIsInstance(var, DiscreteVariable)
9499
self.assertEqual(var.name, "z")
95100
self.assertEqual(var.values, ("a", "d", "c"))
96101
np.testing.assert_equal(var.colors, [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
97102
self.assertIsInstance(var.compute_value, Identity)
98-
self.assertIs(var.compute_value.variable, desc.var)
103+
self.assertIs(var.compute_value.variable, self.var)
99104

100105
palette = desc.var.attributes["palette"] = object()
101-
var = desc.create_variable()
106+
var = desc.create_variable(self.var)
102107
self.assertIs(desc.var.attributes["palette"], palette)
103108
self.assertFalse(hasattr(var.attributes, "palette"))
104109
self.assertIsInstance(var.compute_value, Identity)
105-
self.assertIs(var.compute_value.variable, desc.var)
110+
self.assertIs(var.compute_value.variable, self.var)
106111

107112
def test_reset(self):
108113
desc = self.desc
@@ -228,8 +233,8 @@ def test_from_dict_exceptions(self):
228233

229234
class ContAttrDescTest(unittest.TestCase):
230235
def setUp(self):
231-
x = ContinuousVariable("x")
232-
self.desc = owcolor.ContAttrDesc(x)
236+
self.var = ContinuousVariable("x")
237+
self.desc = owcolor.ContAttrDesc(self.var)
233238

234239
def test_palette(self):
235240
desc = self.desc
@@ -246,19 +251,19 @@ def test_create_variable(self):
246251
palette_name = _find_other_palette(
247252
colorpalettes.ContinuousPalettes[desc.palette_name]).name
248253
desc.palette_name = palette_name
249-
var = desc.create_variable()
254+
var = desc.create_variable(self.var)
250255
self.assertIsInstance(var, ContinuousVariable)
251256
self.assertEqual(var.name, "z")
252257
self.assertEqual(var.palette.name, palette_name)
253258
self.assertIsInstance(var.compute_value, Identity)
254-
self.assertIs(var.compute_value.variable, desc.var)
259+
self.assertIs(var.compute_value.variable, self.var)
255260

256261
colors = desc.var.attributes["colors"] = object()
257-
var = desc.create_variable()
262+
var = desc.create_variable(self.var)
258263
self.assertIs(desc.var.attributes["colors"], colors)
259264
self.assertFalse(hasattr(var.attributes, "colors"))
260265
self.assertIsInstance(var.compute_value, Identity)
261-
self.assertIs(var.compute_value.variable, desc.var)
266+
self.assertIs(var.compute_value.variable, self.var)
262267

263268
def test_reset(self):
264269
desc = self.desc

0 commit comments

Comments
 (0)