88from . import parameters
99from .core import *
1010from .utils import InputParameterError
11-
11+
1212__all__ = ['Gaussian1DModel' , 'Gaussian2DModel' , 'ScaleModel' , 'ShiftModel' ]
13-
13+
14+
1415class Gaussian1DModel (ParametricModel ):
1516 """
16-
17+
1718 Implements 1D Gaussian model.
1819
1920 Parameters
@@ -22,85 +23,97 @@ class Gaussian1DModel(ParametricModel):
2223 Amplitude of the gaussian
2324 mean : float
2425 Mean of the gaussian
25- fwhm : float
26- FWHM
2726 stddev : float
2827 Standard deviation of the gaussian
2928 Either fwhm or stddev must be specified
29+ fwhm : float
30+ Full width at half maximum
31+ Either fwhm or stddev must be specified
3032 jacobian_func : callable, 'estimated' or None
31- if callable - a function to compute the Jacobian of
33+ if callable - a function to compute the Jacobian of
3234 func with derivatives across the rows.
3335 if 'estimated' - the Jacobian will be estimated
3436 if None - if the model has a deriv method, it will be used,
3537 if not the Jacobian will be estimated.
36-
38+
3739 """
40+
3841 param_names = ['amplitude' , 'mean' , 'stddev' ]
39- def __init__ (self , amplitude , mean , fwhm = None , stddev = None ,
42+
43+ def __init__ (self , amplitude , mean , stddev = None , fwhm = None ,
4044 jacobian_func = None , ** cons ):
45+
4146 try :
4247 param_dim = len (amplitude )
4348 except TypeError :
4449 param_dim = 1
50+
4551 if stddev is None and fwhm is None :
4652 raise InputParameterError (
4753 "Either fwhm or stddev must be specified" )
48- if stddev is not None :
54+ elif stddev is not None and fwhm is not None :
55+ raise InputParameterError ("Cannot specify both fwhm and stddev" )
56+ elif stddev is not None :
4957 stddev_val = stddev
5058 else :
5159 try :
5260 stddev_val = 0.42466 * fwhm
5361 except TypeError :
5462 stddev_val = [0.42466 * n for n in fwhm ]
63+
5564 self ._stddev = parameters .Parameter ('stddev' , stddev_val , self , param_dim )
5665 self ._mean = parameters .Parameter ('mean' , mean , self , param_dim )
5766 self ._amplitude = parameters .Parameter ('amplitude' , amplitude , self , param_dim )
67+
5868 super (Gaussian1DModel , self ).__init__ (self .param_names , n_inputs = 1 , n_outputs = 1 ,
5969 param_dim = param_dim , ** cons )
70+
6071 self .linear = False
6172 if jacobian_func is 'estimated' :
6273 self .deriv = None
6374 elif callable (jacobian_func ):
6475 self .deriv = jacobian_func
6576 else :
6677 self .deriv = self .gderiv
67-
78+
6879 def eval (self , x , params ):
69- return params [0 ] * np .exp ((- (1 / (params [2 ]** 2 )) *
70- (x - params [1 ])** 2 ))
71-
80+ return params [0 ] * np .exp (- 0.5 * (x - params [1 ])** 2 / params [2 ]** 2 )
81+
7282 def gderiv (self , p , x , y ):
7383 amplitude , mean , stddev = p
7484 deriv_dict = {}
75- deriv_dict ['amplitude' ] = np .exp ((- (1 / (stddev ** 2 )) * (x - mean )** 2 ))
76- deriv_dict ['mean' ] = 2 * amplitude * np .exp ((- (1 / (stddev ** 2 )) *
77- (x - mean )** 2 )) * (x - mean )/ (stddev ** 2 )
78- deriv_dict ['stddev' ] = 2 * amplitude * np .exp ((- (1 / (stddev ** 2 )) *
79- (x - mean )** 2 )) * ((x - mean )** 2 )/ (stddev ** 3 )
85+ deriv_dict ['amplitude' ] = np .exp (- 0.5 / stddev ** 2 * (x - mean )** 2 )
86+ deriv_dict ['mean' ] = (amplitude
87+ * np .exp (- 0.5 / stddev ** 2 * (x - mean )** 2 )
88+ * (x - mean ) / stddev ** 2 )
89+ deriv_dict ['stddev' ] = (amplitude
90+ * np .exp (- 0.5 / stddev ** 2 * (x - mean )** 2 )
91+ * (x - mean )** 2 / stddev ** 3 )
8092 derivval = [deriv_dict [par ] for par in self .param_names ]
8193 return np .array (derivval ).T
82-
94+
8395 def __call__ (self , x ):
8496 """
8597 Transforms data using this model.
86-
98+
8799 Parameters
88100 --------------
89101 x : array, of minimum dimensions 1
90-
102+
91103 Notes
92104 -----
93- See the module docstring for rules for model evaluation.
105+ See the module docstring for rules for model evaluation.
94106 """
95107 x , fmt = _convert_input (x , self .param_dim )
96108 result = self .eval (x , self .param_sets )
97109 return _convert_output (result , fmt )
98-
110+
111+
99112class Gaussian2DModel (ParametricModel ):
100113 """
101-
114+
102115 2D Gaussian.
103-
116+
104117 Parameters
105118 ----------
106119 amplitude : float
@@ -109,36 +122,41 @@ class Gaussian2DModel(ParametricModel):
109122 Mean of the gaussian in x
110123 y_mean : float
111124 Mean of the gaussian in y
112- x_fwhm : float
113- Full width at half maximum in x
114- y_fwhm : float
115- Full width at half maximum in y
116125 x_stddev : float
117126 Standard deviation of the gaussian in x
118- Either fwhm or x_stddev must be specified
127+ Either x_fwhm or x_stddev must be specified
119128 y_stddev : float
120129 Standard deviation of the gaussian in y
121- Either y_stddev or ratio should be given
130+ Either y_fwhm or y_stddev must be specified
131+ theta : float
132+ Rotation angle in radians. Note: increases clockwise.
133+ x_fwhm : float
134+ Full width at half maximum in x
135+ Either x_fwhm or x_stddev must be specified
136+ y_fwhm : float
137+ Full width at half maximum in y
138+ Either y_fwhm or y_stddev must be specified
122139 jacobian_func : callable or None
123- if callable - a function to compute the Jacobian of
140+ if callable - a function to compute the Jacobian of
124141 func with derivatives across the rows.
125142 if None - the Jacobian will be estimated
126- theta : float
127- Rotation angle in radians. Note: increases clockwise.
128143 cov_matrix : ndarray
129- A 2x2 covariance matrix. If specified, overrides stddev, fwhm, and
144+ A 2x2 covariance matrix. If specified, overrides stddev, fwhm, and
130145 theta specification.
131146 """
132- param_names = ['amplitude' , 'x_mean' , 'y_mean' , \
147+
148+ param_names = ['amplitude' , 'x_mean' , 'y_mean' ,
133149 'x_stddev' , 'y_stddev' , 'theta' ]
134-
135- def __init__ (self , amplitude , x_mean , y_mean , x_fwhm = None , y_fwhm = None ,
136- x_stddev = None , y_stddev = None , theta = 0.0 , cov_matrix = None ,
150+
151+ def __init__ (self , amplitude , x_mean , y_mean , x_stddev = None , y_stddev = None ,
152+ theta = 0.0 , x_fwhm = None , y_fwhm = None , cov_matrix = None ,
137153 jacobian_func = None , ** cons ):
154+
138155 try :
139156 param_dim = len (amplitude )
140157 except TypeError :
141158 param_dim = 1
159+
142160 if y_stddev is None and y_fwhm is None and cov_matrix is None :
143161 raise InputParameterError (
144162 "Either y_fwhm or y_stddev must be specified, or a "
@@ -147,6 +165,16 @@ def __init__(self, amplitude, x_mean, y_mean, x_fwhm=None, y_fwhm=None,
147165 raise InputParameterError (
148166 "Either x_fwhm or x_stddev must be specified, or a "
149167 "covariance matrix." )
168+ elif x_stddev is not None and x_fwhm is not None :
169+ raise InputParameterError ("Cannot specify both x_fwhm and x_stddev" )
170+ elif y_stddev is not None and y_fwhm is not None :
171+ raise InputParameterError ("Cannot specify both y_fwhm and y_stddev" )
172+ elif cov_matrix is not None and (x_stddev is not None or
173+ y_stddev is not None or
174+ x_fwhm is not None or
175+ y_fwhm is not None ):
176+ raise InputParameterError ("Cannot specify both cov_matrix and x/y_stddev or x/y_fwhm" )
177+
150178 self ._amplitude = parameters .Parameter ('amplitude' , amplitude ,
151179 self , param_dim )
152180 if cov_matrix is None :
@@ -161,13 +189,15 @@ def __init__(self, amplitude, x_mean, y_mean, x_fwhm=None, y_fwhm=None,
161189 x_stddev , y_stddev = np .sqrt (eig_vals )
162190 y_vec = eig_vecs [:,0 ]
163191 theta = np .arctan2 (y_vec [1 ],y_vec [0 ])
192+
164193 self ._x_stddev = parameters .Parameter ('x_stddev' , x_stddev ,
165194 self , param_dim )
166195 self ._y_stddev = parameters .Parameter ('y_stddev' , y_stddev ,
167196 self , param_dim )
168197 self ._x_mean = parameters .Parameter ('x_mean' , x_mean , self , param_dim )
169198 self ._y_mean = parameters .Parameter ('y_mean' , y_mean , self , param_dim )
170199 self ._theta = parameters .Parameter ('theta' , theta , self , param_dim )
200+
171201 super (Gaussian2DModel , self ).__init__ (self .param_names , n_inputs = 2 , n_outputs = 1 ,
172202 param_dim = param_dim , ** cons )
173203 self .linear = False
@@ -177,27 +207,34 @@ def __init__(self, amplitude, x_mean, y_mean, x_fwhm=None, y_fwhm=None,
177207 self .deriv = None
178208
179209 def eval (self , x , y , p ):
180- return p [0 ] * np .exp (- (
181- ((np .cos (p [5 ])/ p [1 ])** 2 + (np .sin (p [5 ])/ p [2 ])** 2 ) * ((x - p [3 ])** 2 )
182- + 2 * (np .cos (p [5 ])* np .sin (p [5 ])* (1 / (p [1 ]** 2 )- 1 / (p [2 ]** 2 ))) *
183- (x - p [3 ])* (y - p [4 ]) + ((np .sin (p [5 ])/ p [1 ])** 2 + (np .cos (p [5 ])/ p [2 ])** 2 )*
184- ((y - p [4 ])** 2 )))
185-
210+
211+ a = 0.5 * ((np .cos (p [5 ]) / p [3 ]) ** 2 +
212+ (np .sin (p [5 ]) / p [4 ]) ** 2 )
213+ b = 0.5 * (np .cos (p [5 ]) * np .sin (p [5 ]) *
214+ (1. / p [3 ] ** 2 - 1. / p [4 ] ** 2 ))
215+ c = 0.5 * ((np .sin (p [5 ]) / p [3 ]) ** 2 +
216+ (np .cos (p [5 ]) / p [4 ]) ** 2 )
217+
218+ return p [0 ] * np .exp (- (a * (x - p [1 ]) ** 2 +
219+ b * (x - p [1 ]) * (y - p [2 ]) +
220+ c * (y - p [2 ]) ** 2 ))
221+
186222 def __call__ (self , x , y ):
187223 """
188224 Transforms data using this model.
189-
225+
190226 Parameters
191227 --------------
192228 x, y : arrays, of min dimensions 2
193-
194- Note: See the module docstring for rules for model evaluation.
229+
230+ Note: See the module docstring for rules for model evaluation.
195231 """
196232 x , _ = _convert_input (x , self .param_dim )
197233 y , fmt = _convert_input (y , self .param_dim )
198234 result = self .eval (x , y , self .param_sets )
199235 return _convert_output (result , fmt )
200236
237+
201238class ShiftModel (Model ):
202239 """
203240 Shift a coordinate.
@@ -208,7 +245,7 @@ class ShiftModel(Model):
208245 offsets to be applied to a coordinate
209246 if a list - each value in the list is an offset to be applied to a
210247 column in the input coordinate array
211-
248+
212249 """
213250 param_names = ['offsets' ]
214251 def __init__ (self , offsets ):
@@ -227,17 +264,18 @@ def __call__(self, x):
227264 x , fmt = _convert_input (x , self .param_dim )
228265 result = x + self .offsets
229266 return _convert_output (result , fmt )
230-
267+
268+
231269class ScaleModel (Model ):
232270 """
233-
271+
234272 Multiply a model by a factor.
235273
236274 Parameters
237275 ---------------
238276 factors : float or a list of floats
239277 scale for a coordinate
240-
278+
241279 """
242280 param_names = ['factors' ]
243281 def __init__ (self , factors ):
@@ -248,7 +286,7 @@ def __init__(self, factors):
248286 self ._factors = parameters .Parameter ('factors' , factors , self , param_dim )
249287 super (ScaleModel , self ).__init__ (self .param_names , n_inputs = 1 , n_outputs = 1 ,
250288 param_dim = param_dim )
251-
289+
252290 def __call__ (self , x ):
253291 """
254292 Transforms data using this model.
0 commit comments