5 from refractor
import framework
as rf
11 "Base class for configuration parameters" 13 def __init__(self, required=True, default=None):
17 if default
is not None:
26 if callable(in_value):
27 out_value = in_value(**kwargs)
36 raise NotImplementedError(
"check_type must be implemented in inheriting class")
39 "Bypasses type checking for the parameter" 44 class Choice(ConfigParam):
45 "Allows the choice of multiple parameter types" 52 if isinstance(choice, ConfigParam):
55 raise ParamError(
"Arguments to Choice must be instance of ConfigParam types.")
62 choice.check_type(value)
70 raise ParamError(
"Parameter value does not match any of the parameter choices")
73 "Configuration parameter that resolves to a single scalar value" 81 if not np.isscalar(value):
82 raise ParamError(
"Value is not a scalar: %s" % value)
84 if self.
dtype is not None and not isinstance(value, self.
dtype):
85 raise ParamError(
"Type of parameter %s does not match expected %s for value: %s" % (type(value), self.
dtype, value))
88 "Configuration parameter that resolves to a numpy array with optional checking on dimensionality and units" 90 def __init__(self, dims=None, dtype=None, **kwargs):
98 if not isinstance(value, np.ndarray):
99 raise ParamError(
"Value is not a numpy array: %s" % value)
101 if self.
dims is not None and len(value.shape) != self.
dims:
102 raise ParamError(
"Number of dimensions %d do not match the expected number %s for value: %s" % (len(value.shape), self.
dims, value))
104 if self.
dtype is not None and not isinstance(value.dtype, self.
dtype):
105 raise ParamError(
"Type of parameter %s does not match expected %s for value: %s" % (value.dtype, self.
dtype, value))
108 "Configuration parameter that resolve to an iterable object like a tuple or list, but that is not required to be an array" 116 if not hasattr(value,
"__iter__"):
117 raise ParamError(
"Expected an iterable for value: %s" % value)
120 for idx, iter_val
in enumerate(value):
121 if not isinstance(iter_val, self.
val_type):
122 raise ParamError(
"Expected an instance of %s for value %s at index %d of iterable" % (self.
val_type, iter_val, idx))
125 "Configuration parameter that must be an instance of a specific type of class" 133 if not isinstance(value, self.
val_type):
137 "Configuration parameter that resolves to a dict" 144 awu_type_pattern =
r"ArrayWithUnit_\w+_\d+" 146 def __init__(self, dims=None, dtype=None, **kwargs):
156 type_str = str(type(value))
159 raise ParamError(
"Value is not an ArrayWithUnit type: %s" % type(value))
162 if self.
dims is not None and len(value.value.shape) != self.
dims:
163 raise ParamError(
"Number of dimensions %d do not match the expected number %s for ArrayWithUnit value: %s" % (len(value.value.shape), self.
dims, value))
165 if self.
dtype is not None and not isinstance(value.value.dtype, self.
dtype):
166 raise ParamError(
"Type of parameter %s does not match expected %s for ArrayWithUnit value: %s" % (value.value.dtype, self.
dtype, value))
169 "Short cut for the DoubleWithUnit type to parallel the ArrayWithUnit type parameter" 172 super().
__init__(rf.DoubleWithUnit, **kwargs)
175 "Checks that value is a C++ vector of a certain type" 186 check_str =
"\.vector_%s" % self.
vec_type 188 check_str =
"\.vector_.*" 190 type_str = str(type(value))
192 if not re.search(check_str, type_str):
193 raise ParamError(
"Value with type string %s is not a C++ vector with type vector_%s" % (type_str, self.
vec_type and self.
vec_type or ""))
def check_type(self, value)
def __init__(self, val_type, kwargs)
def __init__(self, dims=None, dtype=None, kwargs)
def check_type(self, value)
def __init__(self, dims=None, dtype=None, kwargs)
def check_type(self, value)
def __init__(self, kwargs)
def check_type(self, value)
def __init__(self, dtype=None, kwargs)
def check_type(self, value)
def __init__(self, kwargs)
def check_type(self, value)
def check_type(self, value)
def __init__(self, required=True, default=None)
def evaluate(self, in_value, kwargs)
def check_type(self, value)
def __init__(self, vargs, kwargs)
def __init__(self, val_type=None, kwargs)
def check_type(self, value)
def __init__(self, vec_type=None, kwargs)