Skip to content

Commit

Permalink
Added simple config file and config loading function
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed Nov 4, 2017
1 parent 91f00cd commit 482ade0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
34 changes: 32 additions & 2 deletions sciunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from __future__ import print_function
import os
import sys
import inspect
from copy import copy
Expand All @@ -23,7 +24,7 @@

KERNEL = ('ipykernel' in sys.modules)
LOGGING = True

HERE = os.path.dirname(os.path.realpath(__file__))

def log(*args, **kwargs):
if LOGGING:
Expand All @@ -44,6 +45,31 @@ def log(*args, **kwargs):
display(HTML(output))


def config_get(key, default=None):
try:
assert isinstance(key,str), "Config key must be a string"
config_path = os.path.join(HERE,'config.json')
try:
with open(config_path) as f:
config = json.load(f)
value = config[key]
except FileNotFoundError:
raise Error("Config file not found at '%s'" % config_path)
except json.JSONDecodeError:
log("Config file JSON at '%s' was invalid" % config_path)
raise Error("Config file not found at '%s'" % config_path)
except KeyError:
raise Error("Config file does not contain key '%s'" % key)
except Exception as e:
if default is not None:
log(e)
log("Using default value of %s" % default)
value = default
else:
raise e
return value


class SciUnit(object):
"""Abstract base class for models, tests, and scores."""
def __init__(self):
Expand Down Expand Up @@ -821,7 +847,11 @@ def value_color(cls, value):
if value is None or np.isnan(value):
rgb = (128,128,128)
else:
rgb = tuple([x*256 for x in cm.RdYlGn(int(180*value+38))[:3]])
cmap_low = config_get('cmap_low',38)
cmap_high = config_get('cmap_high',218)
cmap_range = cmap_high - cmap_low
cmap = cm.RdYlGn(int(cmap_range*value+cmap_low))[:3]
rgb = tuple([x*256 for x in cmap])
return rgb

@property
Expand Down
1 change: 1 addition & 0 deletions sciunit/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cmap_high": 218, "cmap_low": 38}

0 comments on commit 482ade0

Please sign in to comment.