Commit a47166fc authored by David Trudgian's avatar David Trudgian
Browse files

Use Decimal and strings for precise real params

parent b9dc0b65
......@@ -3,6 +3,7 @@ import os
import re
from collections import OrderedDict
from copy import deepcopy
from decimal import *
import anyconfig
from yamllint import linter
......@@ -322,16 +323,16 @@ class RealRange(IntRange):
def to_list(self):
min_val = float(self.param['min'])
max_val = float(self.param['max'])
min_val = Decimal(str(self.param['min']))
max_val = Decimal(str(self.param['max']))
step = float(self.param.get('step', 1.0))
scale = float(self.param.get('scale', 1.0))
step = Decimal(str(self.param.get('step', '1.0')))
scale = Decimal(str(self.param.get('scale', '1.0')))
# Special case - if only scale specified then assume no step
if 'scale' in self.param and 'step' not in self.param:
step = 0.0
step = Decimal('0.0')
current = min_val
vals = []
......@@ -340,7 +341,7 @@ class RealRange(IntRange):
vals.append(None)
while current <= max_val:
vals.append(current)
vals.append(str(current))
current = (current * scale) + step
return vals
......
......@@ -32,8 +32,8 @@ class TestParams:
# min: 0.1
# max: 100
# scale: 10
assert md.vals['parameters'][1]['range'] == [None, 0.1, 1.0, 10.0,
100.0]
assert md.vals['parameters'][1]['range'] == [None, '0.1', '1.0', '10.0',
'100.0']
# values:
# - step
# - sigmoid
......@@ -61,32 +61,31 @@ class TestParams:
assert ir.to_list() == [None, 0, 1, 2, 3, 4, 5]
def test_real_range_single(self):
rr = RealRange({'id': 'test', 'min': 0.1, 'max': 5.1})
assert rr.to_list() == pytest.approx([0.1, 1.1, 2.1, 3.1, 4.1, 5.1])
rr = RealRange({'id': 'test', 'min': '0.1', 'max': '5.1'})
assert rr.to_list() == ['0.1', '1.10', '2.100', '3.1000', '4.10000', '5.100000']
def test_real_range_step(self):
rr = RealRange({'id': 'test', 'min': 0.1, 'max': 4.9, 'step': 1.2})
assert rr.to_list() == pytest.approx([0.1, 1.3, 2.5, 3.7, 4.9])
rr = RealRange({'id': 'test', 'min': '0.1', 'max': '4.9', 'step': '1.2'})
assert rr.to_list() == ['0.1', '1.30', '2.500', '3.7000', '4.90000']
def test_real_range_scale(self):
rr = RealRange({'id': 'test', 'min': 0.001, 'max': 10.0, 'scale': 10})
assert rr.to_list() == pytest.approx([0.001, 0.01, 0.1, 1.0, 10.0])
rr = RealRange({'id': 'test', 'min': '0.001', 'max': '10.0', 'scale': '10'})
assert rr.to_list() == ['0.001', '0.010', '0.100', '1.000', '10.000']
def test_real_range_step_scale(self):
rr = RealRange({'id': 'test', 'min': 0.001, 'max': 1000.0, 'scale': 10,
rr = RealRange({'id': 'test', 'min': '0.001', 'max': '1000.0', 'scale': '10',
'step': 0.2})
assert rr.to_list() == pytest.approx([0.001, 0.21, 2.3, 23.2, 232.2])
assert rr.to_list() == ['0.001', '0.210', '2.300', '23.200', '232.200']
def test_real_range_exponent(self):
rr = RealRange({'id': 'test', 'min': 1e-6, 'max': 1e3, 'scale': 1e1,
rr = RealRange({'id': 'test', 'min': '1e-6', 'max': '1e3', 'scale': '1e1',
'step': 0.2})
assert rr.to_list() == pytest.approx([1e-6, 0.20001, 2.2001, 22.201, 222.21])
assert rr.to_list() == ['0.000001', '0.20001', '2.2001', '22.201', '222.21']
def test_real_range_optional(self):
rr = RealRange({'id': 'test', 'min': 0.001, 'max': 10.0, 'scale': 10,
rr = RealRange({'id': 'test', 'min': '0.001', 'max': '10.0', 'scale': '10',
'optional': True})
assert rr.to_list() == pytest.approx(
[None, 0.001, 0.01, 0.1, 1.0, 10.0])
assert rr.to_list() == [None, '0.001', '0.010', '0.100', '1.000', '10.000']
def test_choice(self):
cr = ChoiceRange({'id': 'test', 'values': ['One', 'Two', 'Three']})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment