Skip to content

Commit

Permalink
Eac dev/prop (#5)
Browse files Browse the repository at this point in the history
* Update property and parameter

* Updated for refactor

Co-authored-by: Eric Charles <[email protected]>
  • Loading branch information
eacharles and eacharles authored Mar 3, 2021
1 parent e911b6f commit ea2903b
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 356 deletions.
3 changes: 2 additions & 1 deletion python/cfgmdl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
from .parameter import Parameter
from .param_holder import ParamHolder
from .model import Model
from .function import Function

from . import tools, utils
4 changes: 2 additions & 2 deletions python/cfgmdl/derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __set_name__(self, owner, name):
self.loader = loader
if not callable(self.loader):
raise ValueError("Callable loader not defined for Derived object", owner, name, self.loadername, self.loader)
return
#return
setattr(owner, self.loadername, self.loader)

def __get__(self, obj, objtype=None):
Expand All @@ -70,7 +70,7 @@ def __get__(self, obj, objtype=None):

loader = self.loader

if loader is None:
if loader is None: #pragma: no cover
raise ValueError("%s.%s no loader" % (obj, self.public_name))

ts_check = self.check_timestamp(obj)
Expand Down
190 changes: 0 additions & 190 deletions python/cfgmdl/function.py

This file was deleted.

2 changes: 1 addition & 1 deletion python/cfgmdl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def param_values(self, pnames=None):
"""
l = self.get_params(pnames)
v = [p.__get__(self) for p in l]
v = [p.__get__(self)() for p in l]
return np.array(v)


Expand Down
28 changes: 26 additions & 2 deletions python/cfgmdl/param_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import numpy as np

from .array import Array
from .configurable import Configurable
from .configurable import Configurable, Property
from .utils import is_none
from .unit import Unit

class ParamHolder(Configurable):
"""Wrapper around a data value
Expand All @@ -20,6 +21,7 @@ class ParamHolder(Configurable):
bounds = Array(help='Parameter Bounds')
scale = Array(default=1., help='Paramter Scale Factor')
free = Array(dtype=bool, default=False, help='Free/Fixed Flag')
unit = Property(dtype=Unit, default=None, help='Parameter unit')

def __init__(self, *args, **kwargs):
"""Constructor"""
Expand All @@ -29,10 +31,11 @@ def __init__(self, *args, **kwargs):
# raise ValueError("value keyword provided in addition to arguments")
kwcopy['value'] = args
if is_none(kwargs.get('value', None)):
kwargs.pop('value', None)
kwargs.pop('value', None)
super(ParamHolder, self).__init__(**kwargs)
self.check_bounds()


def __str__(self, indent=0):
"""Return self as a string"""
return '{0:>{2}}{1}'.format('', '', indent) + "{_value} += {_errors} [{_bounds}] <{_scale}> {_free}".format(**self.__dict__)
Expand Down Expand Up @@ -67,4 +70,25 @@ def check_bounds(self):

def __call__(self):
"""Return the product of the value and the scale"""
base_val = self.scaled
if is_none(self.unit):
return base_val
return self.unit(base_val)

def set_from_SI(self, val):
"""Set the value from SI units"""
if is_none(self.unit):
self.value = val
return
self.value = self.unit.inverse(val)


@property
def SI(self):
"""Return the value in SI units"""
return self()

@property
def scaled(self):
"""Return the value in original units"""
return self.value * self.scale
27 changes: 7 additions & 20 deletions python/cfgmdl/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Parameter(Property):
defaults['errors'] = (np.nan, 'Errors on this parameter')
defaults['free'] = (False, 'Is this property allowed to vary?')
defaults['scale'] = (1.0, 'Scale to apply for this property')
defaults['unit'] = (None, 'Units for this parameter')

# Overwrite the default dtype
defaults['dtype'] = (ParamHolder, 'Data type')
Expand All @@ -36,6 +37,7 @@ def __init__(self, **kwargs):
self.errors = np.nan
self.free = False
self.scale = 1.
self.unit = None
super(Parameter, self).__init__(**kwargs)

def __set__(self, obj, value):
Expand All @@ -62,34 +64,19 @@ def __set__(self, obj, value):
"""
par = getattr(obj, self.private_name, None)

vals = dict(value=self.default, bounds=self.bounds, errors=self.errors, scale=self.scale, free=self.free, unit=self.unit)

if par is None:
if isinstance(value, Mapping):
par = self.dtype(**value)
vals.update(value)
else:
par = self.dtype(value=value, bounds=self.bounds, errors=self.errors, scale=self.scale, free=self.free)
vals.update(dict(value=value))
par = self.dtype(**vals)
else:
par.update(value)

super(Parameter, self).__set__(obj, par)

def __get__(self, obj, objtype=None):
"""Get the value from the client object
Parameter
---------
obj : ...
The client object
Return
------
out : ...
The requested value
"""
val = getattr(obj, self.private_name).__call__()
if self.unit is None:
return val
return self.unit(val) #pylint: disable=not-callable

def todict(self, obj):
"""Extract values as an odict """
return getattr(obj, self.private_name).todict()
Expand Down
9 changes: 2 additions & 7 deletions python/cfgmdl/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import OrderedDict as odict


from .utils import is_none, cast_type, Meta, Defs, defaults_decorator, defaults_docstring
from .utils import cast_type, Meta, Defs, defaults_decorator, defaults_docstring

try:
basestring
Expand Down Expand Up @@ -40,7 +40,6 @@ class Property(Defs):
('dtype', (None, 'Data type')),
('default', (None, 'Default value')),
('required', (False, 'Is this propery required?')),
('unit', (None, 'Units for unit')),
])

@defaults_decorator(defaults)
Expand All @@ -50,7 +49,6 @@ def __init__(self, **kwargs):
self.dtype = type(None)
self.default = None
self.required = None
self.unit = None
self.public_name = None
self.private_name = None
self.time_name = None
Expand Down Expand Up @@ -106,10 +104,7 @@ def __get__(self, obj, objtype=None):
out : ...
The requested value
"""
attr = getattr(obj, self.private_name)
if self.unit is None or is_none(attr):
return attr
return self.unit(attr) #pylint: disable=not-callable
return getattr(obj, self.private_name)

def __delete__(self, obj):
"""Set the value to the default value
Expand Down
3 changes: 1 addition & 2 deletions python/cfgmdl/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Ref(Property):
"""Property sub-class refering to properties of other objects
"""
defaults = deepcopy(Property.defaults)
defaults.pop('unit')
defaults.pop('dtype')
defaults['owner'] = (None, 'Object that owns datum')
defaults['attr'] = (None, 'Name of attribute in object that owns datum')
Expand Down Expand Up @@ -61,7 +60,7 @@ def __set__(self, obj, value):
The client object
value : ...
The referenced object
"""
"""
setattr(obj, self.owner_name, value)
_ = self.__get__(obj, self.public_name)
setattr(obj, self.time_name, time.time())
Expand Down
Loading

0 comments on commit ea2903b

Please sign in to comment.