Commit 73d9bd8f authored by David Trudgian's avatar David Trudgian
Browse files

Real ranges

parent 9c7256b1
......@@ -13,7 +13,7 @@ class ParamFileException(Exception):
Exception.__init__(self, *args, **kwargs)
class ParamFile:
class ParamFile(object):
"""Process and hold the parameter file that defines the experiment to run
A parameter file is a YAML file, which defines:
......@@ -111,7 +111,7 @@ class ParamFile:
self.vals = metadata
class IntRange:
class IntRange(object):
"""Compute integer parameters from an int_range definition
......@@ -141,8 +141,23 @@ class IntRange:
current = min_val
vals = []
if self.param.get('optional', False):
vals.append(None)
while current <= max_val:
vals.append(current)
current = (current * scale) + step
return vals
class RealRange(IntRange):
"""Compute real-value parameters from an real_range definition
Attributes:
param (dict): Range definition dictionary
"""
pass
import os
import pytest
from runner.param import ParamFile, ParamFileException, IntRange
from runner.param import ParamFile, ParamFileException, IntRange, RealRange
class TestParams:
......@@ -34,3 +34,28 @@ class TestParams:
def test_int_range_step_scale(self):
ir = IntRange({'id': 'test', 'min': 0, 'max': 10000, 'scale': 10, 'step': 9})
assert ir.to_list() == [0, 9, 99, 999, 9999]
def test_int_range_optional(self):
ir = IntRange({'id': 'test', 'min': 0, 'max': 5, 'optional': True})
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])
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])
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])
def test_real_range_step_scale(self):
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])
def test_real_range_optional(self):
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])
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