Skip to content

Commit c0f8a67

Browse files
committed
Normalizer: Retain attributes of attributes
1 parent 89aa265 commit c0f8a67

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

Orange/preprocess/normalize.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from Orange.data import ContinuousVariable, Domain
3+
from Orange.data import Domain
44
from Orange.statistics import distribution
55
from Orange.util import Reprable
66
from .preprocess import Normalize
@@ -51,25 +51,15 @@ def normalize_by_sd(self, dist, var):
5151
compute_val = Norm(var, avg, 1 / sd)
5252
else:
5353
compute_val = Norm(var, 0, 1 / sd)
54-
55-
return ContinuousVariable(
56-
var.name,
57-
compute_value=compute_val,
58-
sparse=var.sparse,
59-
)
54+
return var.copy(compute_value=compute_val)
6055

6156
def normalize_by_span(self, dist, var):
6257
dma, dmi = (dist.max(), dist.min()) if dist.shape[1] else (np.nan, np.nan)
6358
diff = dma - dmi
6459
if diff < 1e-15:
6560
diff = 1
6661
if self.zero_based:
67-
return ContinuousVariable(
68-
var.name,
69-
compute_value=Norm(var, dmi, 1 / diff),
70-
sparse=var.sparse)
62+
compute_val = Norm(var, dmi, 1 / diff)
7163
else:
72-
return ContinuousVariable(
73-
var.name,
74-
compute_value=Norm(var, (dma + dmi) / 2, 2 / diff),
75-
sparse=var.sparse)
64+
compute_val = Norm(var, (dma + dmi) / 2, 2 / diff)
65+
return var.copy(compute_value=compute_val)

Orange/tests/test_normalize.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,18 @@ def test_datetime_normalization(self):
142142
[0., '2003-07-23', 'a', 'b', -1., '?', 0., 'b', '?', 'b', 0],
143143
[0., '1967-03-12', 'a', 'b', 1., 'b', -1.225, 'c', '?', 'c', 1]]
144144
self.compare_tables(data_norm, solution)
145+
146+
def test_retain_vars_attributes(self):
147+
data = Table("iris")
148+
attributes = {"foo": "foo", "baz": 1}
149+
data.domain.attributes[0].attributes = attributes
150+
self.assertDictEqual(
151+
Normalize(norm_type=Normalize.NormalizeBySD)(
152+
data).domain.attributes[0].attributes, attributes)
153+
self.assertDictEqual(
154+
Normalize(norm_type=Normalize.NormalizeBySpan)(
155+
data).domain.attributes[0].attributes, attributes)
156+
157+
158+
if __name__ == "__main__":
159+
unittest.main()

0 commit comments

Comments
 (0)