-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.py
executable file
·121 lines (96 loc) · 3.86 KB
/
parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
import sys
from astropy import units
class Parameter:
"""
Each physical parameter is wrapped within this class.
"""
def __init__(self, name='dummy', value=0.5, unit=None, vmin=0.0, vmax=1.0, fitted=False, dtype=float, docstring=''):
"""
Constructs the class
"""
# pass the arguments
self.name = name
self.unit = unit
# some parameters can be mere switches, they
# should remain type integer
if dtype == int:
self.value = value
self.vmin = vmin
self.vmax = vmax
# floats are transcripted into units.Quantities
else:
self.value = units.Quantity(value=value, unit=unit)
self.vmin = units.Quantity(value=vmin, unit=unit)
self.vmax = units.Quantity(value=vmax, unit=unit)
self.fitted = fitted
self.dtype = dtype
self.docstring = docstring
# check boundaries and type
self.check_boundaries()
self.check_type()
# define adjustable parameters
self.__adjustable = ['value', 'vmin', 'vmax', 'fitted']
def check_boundaries(self):
"""
Checks that value lies within boundaries.
"""
if (self.vmin > self.value) or (self.vmax < self.value):
raise ValueError('Value %s of paramters %s does not lie within boundaries (%s, %s). ' %
(str(self.value), self.name, str(self.vmin), str(self.vmax)))
def check_type(self):
"""
Checks that value, and boundaries have correct
type.
"""
for attr in ['value', 'vmin', 'vmax']:
if not isinstance(getattr(self, attr), units.Quantity):
if not isinstance(getattr(self, attr), self.dtype):
raise TypeError('Attribute %s has incorrect type!' \
' Should be %s, but found %s.' % (attr, str(self.dtype), str(type(getattr(self, attr)))))
else:
if not isinstance(getattr(self, attr).value, self.dtype):
raise TypeError('Attribute %s has incorrect type!' \
' Should be %s, but found %s.' % (attr, str(self.dtype), str(type(getattr(self, attr).value))))
def get_property(self, attr='value'):
"""
Returns value of an attribute.
"""
return getattr(self, attr.lower())
def set_property(self, attr, val):
"""
Sets value of an property.
:param attr: attribute that will be changed
:param val: new value of teh attribute
"""
# convert to lower case
attr = attr.lower()
# only some properties can be changed
if attr not in self.__adjustable:
raise ValueError('The only adjustable attributes of the class are: %s' % (str(self.__adjustable)))
elif attr in ['value', 'vmin', 'vmax']:
# first set attributes type
self.check_type()
# attach the attribute
if self.dtype == int:
setattr(self, attr, val)
else:
setattr(self, attr, units.Quantity(val, unit=self.unit))
# check that we are still within boundaries if value was changed
if attr == 'value':
self.check_boundaries()
else:
setattr(self, attr, val)
def __getattr__(self):
"""
Trial...
"""
return None
def __str__(self):
"""
String representation of the class.
"""
string = ''
for attr in ['name', 'value', 'unit', 'vmin', 'vmax', 'fitted', 'dtype', 'docstring']:
string += '%s:%s, ' % (attr, str(getattr(self, attr)))
return string