Commit 426c5f8d authored by David Trudgian's avatar David Trudgian
Browse files

Compute Ranges

parent 52b92a9a
......@@ -5,6 +5,8 @@ from yamllint.config import YamlLintConfig, YamlLintConfigError
from yamllint import linter
from yamllint.cli import Format
logger = logging.getLogger(__name__)
class ParamFileException(Exception):
"""Exception for any errors related to the parameter file"""
......@@ -109,6 +111,27 @@ class ParamFile(object):
"""Read in metadata from the astrocyte_pkg.yml to populate vals dict"""
metadata = anyconfig.load(self.param_file)
self.vals = metadata
self.__compute_param_ranges()
def __compute_param_ranges(self):
"""Compute the parameter ranges as lists, from their definitions"""
for param in self.vals['parameters']:
if param['type'] == 'int_range':
ir = IntRange(param)
param['range'] = ir.to_list()
if param['type'] == 'real_range':
rr = IntRange(param)
param['range'] = rr.to_list()
if param['type'] == 'choice':
cr = ChoiceRange(param)
param['range'] = cr.to_list()
logger.info("Parameter %s will explore values: %s " %
(param['id'], param['range']))
class IntRange(object):
......@@ -150,6 +173,7 @@ class IntRange(object):
return vals
class RealRange(IntRange):
"""Compute real-value parameters from an real_range definition
......@@ -161,3 +185,30 @@ class RealRange(IntRange):
"""
pass
class ChoiceRange(object):
"""Compute choice parameters from an choice definition
Attributes:
param (dict): Range definition dictionary
"""
param = None
def __init__(self, param):
self.param = param
def to_list(self):
vals = []
if self.param.get('optional', False):
vals.append(None)
vals = vals + (self.param['values'])
return vals
......@@ -84,9 +84,9 @@ parameters:
flag: '-b'
position: 2
optional: true
min: 1.3
max: 1.4
step: 0.001
min: 0.1
max: 100
scale: 10
description: Regularization beta parameter
- id: 'activation'
......
import os
import pytest
from runner.param import ParamFile, ParamFileException, IntRange, RealRange
from runner.param import ParamFile, ParamFileException, IntRange, RealRange, ChoiceRange
class TestParams:
def test_validate(self):
md = ParamFile(os.path.dirname(os.path.abspath(__file__)) + '/test_data/test_params.yaml')
assert md.validate()
......@@ -15,9 +14,10 @@ class TestParams:
with pytest.raises(ParamFileException):
md.validate()
# def test_load(self):
# md = ParamFile(os.path.dirname(os.path.abspath(__file__)) + '/test_data/test_params.yaml')# md.load()
# assert md.vals
def test_load(self):
md = ParamFile(os.path.dirname(os.path.abspath(__file__)) + '/test_data/test_params.yaml')
md.load()
assert md.vals
def test_int_range_single(self):
ir = IntRange({'id': 'test', 'min': 0, 'max': 5})
......@@ -59,3 +59,29 @@ class TestParams:
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])
def test_choice(self):
cr = ChoiceRange({'id': 'test', 'values': ['One', 'Two', 'Three'] })
assert cr.to_list() == ['One', 'Two', 'Three']
def test_choice_optional(self):
cr = ChoiceRange({'id': 'test', 'values': ['One', 'Two', 'Three'], 'optional': 'true' })
assert cr.to_list() == [None, 'One', 'Two', 'Three']
def test_compute_ranges(self):
md = ParamFile(os.path.dirname(os.path.abspath(__file__)) + '/test_data/test_params.yaml')
md.load()
md.compute_param_ranges()
# min: 0
# max: 32
# step: 2
assert md.vals['parameters'][0]['range'] == range(0, 32+1, 2)
# optional: true
# min: 0.1
# max: 100
# scale: 10
assert md.vals['parameters'][1]['range'] == [None, 0.1, 1.0, 10.0, 100.0]
# values:
# - step
# - sigmoid
assert md.vals['parameters'][2]['range'] == ['step', 'sigmoid']
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