fit_functions¶
lmfit fit wrappers
generate_model(xvals, yvals, yerrors=None, npeaks=None, min_peak_power=None, peak_distance_idx=6, model='Gaussian', background='slope', initial_parameters=None, fix_parameters=None)
¶
Generate lmfit profile models See: https://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes E.G.: mod, pars = generate_model(x, y, npeaks=1, model='Gauss', backgroud='slope')
Peak Search: The number of peaks and initial peak centers will be estimated using the find_peaks function. If npeaks is given, the largest npeaks will be used initially. 'min_peak_power' and 'peak_distance_idx' can be input to tailor the peak search results. If the peak search returns < npeaks, fitting parameters will initially choose npeaks equally distributed points
Peak Models: Choice of peak model: 'Gaussian', 'Lorentzian', 'Voight',' PseudoVoight' Background Models: Choice of background model: 'slope', 'exponential'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xvals
|
ndarray
|
array(n) position data |
required |
yvals
|
ndarray
|
array(n) intensity data |
required |
yerrors
|
ndarray | None
|
None or array(n) - error data to pass to fitting function as weights: 1/errors^2 |
None
|
npeaks
|
int | None
|
None or int number of peaks to fit. None will guess the number of peaks |
None
|
min_peak_power
|
float | None
|
float, only return peaks with power greater than this. If None compare against std(y) |
None
|
peak_distance_idx
|
int
|
int, group adjacent maxima if closer in index than this |
6
|
model
|
str
|
str or lmfit.Model, specify the peak model 'Gaussian','Lorentzian','Voight' |
'Gaussian'
|
background
|
str
|
str, specify the background model: 'slope', 'exponential' |
'slope'
|
initial_parameters
|
dict | None
|
None or dict of initial values for parameters |
None
|
fix_parameters
|
dict | None
|
None or dict of parameters to fix at positions |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Model, Parameters]
|
model, parameters |
Source code in mmg_toolbox/fitting/fit_functions.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
generate_model_script(xvals, yvals, yerrors=None, npeaks=None, min_peak_power=None, peak_distance_idx=6, model='Gaussian', background='slope', initial_parameters=None, fix_parameters=None, only_lmfit=False)
¶
Generate script to create lmfit profile models E.G.: string = generate_mode_stringl(x, y, npeaks=1, model='Gauss', backgroud='slope')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xvals
|
ndarray
|
array(n) position data |
required |
yvals
|
ndarray
|
array(n) intensity data |
required |
yerrors
|
ndarray
|
None or array(n) - error data to pass to fitting function as weights: 1/errors^2 |
None
|
npeaks
|
int | None
|
None or int number of peaks to fit. None will guess the number of peaks |
None
|
min_peak_power
|
float | None
|
float, only return peaks with power greater than this. If None compare against std(y) |
None
|
peak_distance_idx
|
int
|
int, group adjacent maxima if closer in index than this |
6
|
model
|
str
|
str or lmfit.Model, specify the peak model 'Gaussian','Lorentzian','Voight' |
'Gaussian'
|
background
|
str
|
str, specify the background model: 'slope', 'exponential' |
'slope'
|
initial_parameters
|
dict | None
|
None or dict of initial values for parameters |
None
|
fix_parameters
|
dict | None
|
None or dict of parameters to fix at positions |
None
|
only_lmfit
|
bool
|
if True, only include lmfit imports |
False
|
Returns:
| Type | Description |
|---|---|
str
|
str |
Source code in mmg_toolbox/fitting/fit_functions.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
modelfit(xvals, yvals, yerrors=None, model=None | Model, initial_parameters=None, fix_parameters=None, method='leastsq', print_result=False, plot_result=False)
¶
Fit x,y data to a model from lmfit E.G.: res = modelfit(x, y, model='Gauss') print(res.fit_report()) res.plot() val = res.params['amplitude'].value err = res.params['amplitude'].stderr
Model: from lmfit import models model1 = model.GaussianModel() model2 = model.LinearModel() model = model1 + model2 res = model.fit(y, x=x)
Provide initial guess: res = modelfit(x, y, model=VoightModel(), initial_parameters={'center':1.23})
Fix parameter: res = modelfit(x, y, model=VoightModel(), fix_parameters={'sigma': fwhm/2.3548200})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xvals
|
ndarray
|
array(n) position data |
required |
yvals
|
ndarray
|
array(n) intensity data |
required |
yerrors
|
ndarray | None
|
None or array(n) - error data to pass to fitting function as weights: 1/errors^2 |
None
|
model
|
lmfit.Model |
None | Model
|
|
initial_parameters
|
dict | None
|
None or dict of initial values for parameters |
None
|
fix_parameters
|
dict | None
|
None or dict of parameters to fix at positions |
None
|
method
|
str
|
str method name, from lmfit fitting methods |
'leastsq'
|
print_result
|
bool
|
if True, prints the fit results using fit.fit_report() |
False
|
plot_result
|
bool
|
if True, plots the results using fit.plot() |
False
|
Returns:
| Type | Description |
|---|---|
ModelResult
|
lmfit.model.ModelResult < fit results object |
Source code in mmg_toolbox/fitting/fit_functions.py
multipeakfit(xvals, yvals, yerrors=None, npeaks=None, min_peak_power=None, peak_distance_idx=10, model='Gaussian', background='slope', initial_parameters=None, fix_parameters=None, method='leastsq', remove_peaks=True, print_result=False, plot_result=False)
¶
Fit x,y data to a model with multiple peaks using lmfit See: https://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes
E.G.: res = multipeakfit(x, y, npeaks=None, model='Gauss', plot_result=True) val = res.params['p1_amplitude'].value err = res.params['p1_amplitude'].stderr
Peak Search: The number of peaks and initial peak centers will be estimated using the find_peaks function. If npeaks is given, the largest npeaks will be used initially. 'min_peak_power' and 'peak_distance_idx' can be input to tailor the peak search results. If the peak search returns < npeaks, fitting parameters will initially choose npeaks equally distributed points
Peak Models: Choice of peak model: 'Gaussian', 'Lorentzian', 'Voight',' PseudoVoight' Background Models: Choice of background model: 'slope', 'exponential'
Peak Parameters (%d=number of peak): Parameters in '.._parameters' dicts and in output results. Each peak (upto npeaks) has a set number of parameters: 'p%d_amplitude', 'p%d_center', 'p%d_dsigma', pvoight only: 'p%d_fraction' output only: 'p%d_fwhm', 'p%d_height' Background parameters: 'bkg_slope', 'bkg_intercept', or for exponential: 'bkg_amplitude', 'bkg_decay'
Provide initial guess: res = multipeakfit(x, y, model='Voight', initial_parameters={'p1_center':1.23})
Fix parameter: res = multipeakfit(x, y, model='gauss', fix_parameters={'p1_sigma': fwhm/2.3548200})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xvals
|
ndarray
|
array(n) position data |
required |
yvals
|
ndarray
|
array(n) intensity data |
required |
yerrors
|
ndarray | None
|
None or array(n) - error data to pass to fitting function as weights: 1/errors^2 |
None
|
npeaks
|
int | None
|
None or int number of peaks to fit. None will guess the number of peaks |
None
|
min_peak_power
|
float | None
|
float, only return peaks with power greater than this. If None compares against std(y) |
None
|
peak_distance_idx
|
int | None
|
int, group adjacent maxima if closer in index than this |
10
|
model
|
str
|
str or lmfit.Model, specify the peak model 'Gaussian','Lorentzian','Voight' |
'Gaussian'
|
background
|
str
|
str, specify the background model: 'slope', 'exponential' |
'slope'
|
initial_parameters
|
dict | None
|
None or dict of initial values for parameters |
None
|
fix_parameters
|
dict | None
|
None or dict of parameters to fix at positions |
None
|
method
|
str
|
str method name, from lmfit fitting methods |
'leastsq'
|
remove_peaks
|
bool
|
bool, remove peaks consistent with zero and fit again |
True
|
print_result
|
bool
|
if True, prints the fit results using fit.fit_report() |
False
|
plot_result
|
bool
|
if True, plots the results using fit.plot() |
False
|
Returns:
| Type | Description |
|---|---|
FitResults
|
FitResults object |
Source code in mmg_toolbox/fitting/fit_functions.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
peak2dfit(xdata, ydata, image_data, initial_parameters=None, fix_parameters=None, print_result=False, plot_result=False)
¶
Fit Gaussian Peak in 2D *** requires lmfit > 1.0.3 *** Not yet finished!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xdata
|
ndarray
|
|
required |
ydata
|
ndarray
|
|
required |
image_data
|
ndarray
|
|
required |
initial_parameters
|
dict | None
|
|
None
|
fix_parameters
|
dict | None
|
|
None
|
print_result
|
bool
|
|
False
|
plot_result
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in mmg_toolbox/fitting/fit_functions.py
peakfit(xvals, yvals, yerrors=None, model='Voight', background='slope', initial_parameters=None, fix_parameters=None, method='leastsq', print_result=False, plot_result=False)
¶
Fit x,y data to a peak model using lmfit
E.G.: res = peakfit(x, y, model='Gauss') print(res) res.plot() val = res.params['amplitude'].value err = res.params['amplitude'].stderr
Peak Models: Choice of peak model: 'Gaussian', 'Lorentzian', 'Voight',' PseudoVoight' Background Models: Choice of background model: 'slope', 'exponential'
Peak Parameters: 'amplitude', 'center', 'sigma', pvoight only: 'fraction' output only: 'fwhm', 'height' Background parameters: 'bkg_slope', 'bkg_intercept', or for exponential: 'bkg_amplitude', 'bkg_decay'
Provide initial guess: res = peakfit(x, y, model='Voight', initial_parameters={'center': 1.23})
Fix parameter: res = peakfit(x, y, model='gauss', fix_parameters={'sigma': fwhm/2.3548200})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xvals
|
ndarray
|
array(n) position data |
required |
yvals
|
ndarray
|
array(n) intensity data |
required |
yerrors
|
ndarray | None
|
None or array(n) - error data to pass to fitting function as weights: 1/errors^2 |
None
|
model
|
str
|
str, specify the peak model: 'Gaussian','Lorentzian','Voight' |
'Voight'
|
background
|
str
|
str, specify the background model: 'slope', 'exponential' |
'slope'
|
initial_parameters
|
dict | None
|
None or dict of initial values for parameters |
None
|
fix_parameters
|
dict | None
|
None or dict of parameters to fix at positions |
None
|
method
|
str
|
str method name, from lmfit fitting methods |
'leastsq'
|
print_result
|
bool
|
if True, prints the fit results using fit.fit_report() |
False
|
plot_result
|
bool
|
if True, plots the results using fit.plot() |
False
|
Returns:
| Type | Description |
|---|---|
FitResults
|
fit result object |