Skip to content

Commit

Permalink
ContinuousVariable.str_val: Print two more decimals when outside roun…
Browse files Browse the repository at this point in the history
…ding error
  • Loading branch information
janezd committed Mar 26, 2020
1 parent 43c037a commit 78940cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
31 changes: 25 additions & 6 deletions Orange/data/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,23 +498,42 @@ def test_make(self):

def test_decimals(self):
a = ContinuousVariable("a", 4)
self.assertEqual(a.str_val(4.654321), "4.6543")
self.assertEqual(a.str_val(4.654321654321), "4.6543")
self.assertEqual(a.str_val(4.6543), "4.6543")
self.assertEqual(a.str_val(4.25), "4.2500")
self.assertEqual(a.str_val(Unknown), "?")
a = ContinuousVariable("a", 5)
self.assertEqual(a.str_val(0.000000000001), "0.00000")
a = ContinuousVariable("a", 10)
self.assertEqual(a.str_val(0.000000000001), "1e-12")

def test_more_decimals(self):
a = ContinuousVariable("a", 0)
self.assertEqual(a.str_val(4), "4")
self.assertEqual(a.str_val(4.1234), "4.12")

a = ContinuousVariable("a", 2)
self.assertEqual(a.str_val(4), "4.00")
self.assertEqual(a.str_val(4.25), "4.25")
self.assertEqual(a.str_val(4.1234123), "4.1234")

for cca4 in (4 + 1e-9, 4 - 1e-9):
assert cca4 != 4
self.assertEqual(a.str_val(cca4), "4.00")

def test_adjust_decimals(self):
# Default is 3 decimals, but format is %g
a = ContinuousVariable("a")
self.assertEqual(a.str_val(5), "5")
self.assertEqual(a.str_val(4.65432), "4.65432")

# Change to no decimals
a.val_from_str_add("5")
self.assertEqual(a.str_val(4.65432), "5")
self.assertEqual(a.str_val(5), "5")

# Change to two decimals
a.val_from_str_add(" 5.12 ")
self.assertEqual(a.str_val(4.65432), "4.65")
a.val_from_str_add("5.1234")
self.assertEqual(a.str_val(4.65432), "4.6543")
self.assertEqual(a.str_val(4.65), "4.65")
self.assertEqual(a.str_val(5), "5.00")

def varcls_modified(self, name):
var = super().varcls_modified(name)
Expand Down
7 changes: 7 additions & 0 deletions Orange/data/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def __init__(self, name="", number_of_decimals=None, compute_value=None, *, spar
three, but adjusted at the first call of :obj:`to_val`.
"""
super().__init__(name, compute_value, sparse=sparse)
self._max_round_diff = 0
self.number_of_decimals = number_of_decimals

@property
Expand All @@ -553,6 +554,7 @@ def number_of_decimals(self, x):
return

self._number_of_decimals = x
self._max_round_diff = 10 ** (-x - 6)
self.adjust_decimals = 0
if self._number_of_decimals <= MAX_NUM_OF_DECIMALS:
self._format_str = "%.{}f".format(self.number_of_decimals)
Expand Down Expand Up @@ -580,6 +582,10 @@ def repr_val(self, val):
"""
if isnan(val):
return "?"
if self.format_str != "%g" \
and abs(round(val, self._number_of_decimals) - val) \
> self._max_round_diff:
return f"{val:.{self._number_of_decimals + 2}f}"
return self._format_str % val

str_val = repr_val
Expand All @@ -593,6 +599,7 @@ def copy(self, compute_value=None, *, name=None, **kwargs):
var.number_of_decimals = number_of_decimals
else:
var._number_of_decimals = self._number_of_decimals
var._max_round_diff = self._max_round_diff
var.adjust_decimals = self.adjust_decimals
var.format_str = self._format_str
return var
Expand Down
8 changes: 6 additions & 2 deletions Orange/tests/test_discretize.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ def test_create_discretized_var_formatting(self):

dvar = discretize.Discretizer.create_discretized_var(
self.var, [10.1234])
self.assertEqual(dvar.values, ("< 10.1", "≥ 10.1"))
self.assertEqual(dvar.values, ("< 10.123", "≥ 10.123"))

self.var.number_of_decimals = 3

dvar = discretize.Discretizer.create_discretized_var(
self.var, [5, 10.25])
self.assertEqual(dvar.values, ("< 5", "5 - 10.25", "≥ 10.25"))

dvar = discretize.Discretizer.create_discretized_var(
self.var, [5, 10.1234])
self.assertEqual(dvar.values, ("< 5", "5 - 10.123", "≥ 10.123"))
self.assertEqual(dvar.values, ("< 5", "5 - 10.1234", "≥ 10.1234"))

def test_discretizer_computation(self):
dvar = discretize.Discretizer.create_discretized_var(
Expand Down

0 comments on commit 78940cf

Please sign in to comment.