Skip to content

Commit 64c5b5e

Browse files
normalize: Fix failing tests due to decimals
1 parent cf5e816 commit 64c5b5e

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Orange/preprocess/normalize.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ def normalize_by_sd(self, dist, var: ContinuousVariable) -> ContinuousVariable:
5252
compute_val = Norm(var, avg, 1 / sd)
5353
else:
5454
compute_val = Norm(var, 0, 1 / sd)
55-
num_decimals = var.number_of_decimals + int(np.ceil(np.log10(sd)))
56-
num_decimals = max(num_decimals, 0) # num decimals can't be negative
55+
56+
# When dealing with integers, and multiplying by something smaller than
57+
# 1, the number of decimals should be decreased, but this integer will
58+
# likely turn into a float, which should have some default number of
59+
# decimals
60+
num_decimals = var.number_of_decimals + int(np.round(np.log10(sd)))
61+
num_decimals = max(num_decimals, 3) # num decimals can't be negative
62+
5763
return var.copy(compute_value=compute_val, number_of_decimals=num_decimals)
5864

5965
def normalize_by_span(self, dist, var: ContinuousVariable) -> ContinuousVariable:
@@ -65,6 +71,9 @@ def normalize_by_span(self, dist, var: ContinuousVariable) -> ContinuousVariable
6571
compute_val = Norm(var, dmi, 1 / diff)
6672
else:
6773
compute_val = Norm(var, (dma + dmi) / 2, 2 / diff)
68-
num_decimals = var.number_of_decimals + int(np.ceil(np.log10(diff)))
69-
num_decimals = max(num_decimals, 0) # num decimals can't be negative
70-
return var.copy(compute_value=compute_val, number_of_decimals=num_decimals)
74+
if not np.isnan(diff):
75+
num_decimals = var.number_of_decimals + int(np.ceil(np.log10(diff)))
76+
num_decimals = max(num_decimals, 0) # num decimals can't be negative
77+
return var.copy(compute_value=compute_val, number_of_decimals=num_decimals)
78+
else:
79+
return var.copy(compute_value=compute_val)

Orange/tests/test_normalize.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,12 @@ def test_number_of_decimals(self):
159159
data = Table.from_list(Domain((foo,)), [[1], [2], [3]])
160160

161161
normalized = Normalize()(data)
162-
norm_foo = normalized.domain.attributes[0]
162+
norm_foo: ContinuousVariable = normalized.domain.attributes[0]
163163

164-
self.assertEqual(norm_foo.number_of_decimals, 3)
165-
self.assertEqual(norm_foo.format_str, "%g")
166-
self.assertEqual(norm_foo.adjust_decimals, 2)
164+
self.assertGreater(norm_foo.number_of_decimals, 0)
167165

168166
for val1, val2 in zip(normalized[:, "Foo"],
169-
["-1.22474", "0", "1.22474"]):
167+
["-1.22474", "0.000", "1.22474"]):
170168
self.assertEqual(str(val1[0]), val2)
171169

172170

0 commit comments

Comments
 (0)