-
Notifications
You must be signed in to change notification settings - Fork 3
/
fitparameter.py
93 lines (86 loc) · 3.53 KB
/
fitparameter.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
#imports
import methodtools
class FitParameter :
"""
class for a single warp fit parameter
"""
#################### PROPERTIES ####################
@property
def fixed(self): # boolean for whether or not the parameter is fixed
return self._fixed
@fixed.setter
def fixed(self,fixed) :
self._fixed = fixed
@property
def warp_bound_string(self) : # a string of the bounds in warp units
return f'({self._min_warp_val:.2f},{self._max_warp_val:.2f})'
@property
def fit_bound_string(self) : # a string of the bounds in fit units
return f'({self._min_fit_val:.2f},{self._max_fit_val:.2f})'
@property
def fit_bounds(self) : # a tuple of the bounds in fit units
return (self._min_fit_val,self._max_fit_val)
@property
def current_fit_value(self) : #the current value in fit units
return self._current_fit_value
@property
def current_warp_value(self) : #the current value in warp units
return self._current_warp_value
@property
def initial_fit_value(self) : #the initial value in fit units
return self._initial_fit_value
@property
def best_fit_value(self) : #the best fit value in fit units
return self._best_fit_value
@best_fit_value.setter
def best_fit_value(self,v) :
self._best_fit_value = v
self._best_warp_value = self.warp_value_from_fit_value(v)
self._current_fit_value = self._best_fit_value
self._current_warp_value = self._best_warp_value
#################### PUBLIC FUNCTIONS ####################
def __init__(self,name,fixed,bounds,initial_value) :
"""
name = parameter name string
fixed = True if parameter will not float in fitting
bounds = tuple of (lower, upper) absolute parameter bounds in warping units
initial_value = the starting value of the parameter in warping units
"""
self.name = name
self._fixed=fixed
self._normalize=True
#set the bounds in warping units
self._min_warp_val = bounds[0]
self._max_warp_val = bounds[1]
#set the offset and rescale for normalization
self._offset = 0.0 if self._min_warp_val==0. else 0.5*(bounds[1]+bounds[0])
self._rescale = (bounds[1]-bounds[0]) if self._min_warp_val==0. else 0.5*(bounds[1]-bounds[0])
#set the bounds in fit units
self._min_fit_val = self.fit_value_from_warp_value(self._min_warp_val)
self._max_fit_val = self.fit_value_from_warp_value(self._max_warp_val)
#set the initial numerical values of the parameter
self._initial_warp_value = initial_value
self._initial_fit_value = self.fit_value_from_warp_value(initial_value)
self._current_warp_value = self._initial_warp_value
self._current_fit_value = self._initial_fit_value
#initialize the best fit result values
self._best_warp_value = None
self._best_fit_value = None
@methodtools.lru_cache()
def fit_value_from_warp_value(self,value) :
"""
convert a given value from warp units to fit units
"""
if not self._normalize :
return value
else :
return (value-self._offset)/self._rescale
@methodtools.lru_cache()
def warp_value_from_fit_value(self,value) :
"""
convert a given value from fit units to warp units
"""
if not self._normalize :
return value
else :
return (self._rescale*value)+self._offset