-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfit_tools.py
48 lines (40 loc) · 1.66 KB
/
fit_tools.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
#By: Faustin Carter ([email protected]), 2017
import numpy as np
import lmfit as lf
import warnings
def do_lmfit(xdata, ydata, fit_fn, params, **kwargs):
"""Run any fit from models on your data.
Paramters
---------
xdata : np.array
The points at which to calculate the model.
ydata : np.array
The data to compare to the calculated model.
fit_fn : callable
Model function to pass to minimizer. Must have signature"""
#pop model kwargs off the top
model_kwargs = kwargs.pop('model_kwargs', {})
#Override any of the default Parameter settings
if kwargs is not None:
for key, val in kwargs.items():
#Allow for turning on and off parameter variation
if '_vary' in key:
key = key.split('_')[0]
if key in params.keys():
assert ((val is True) or (val is False)), "Must pass bool for vary"
params[key].vary = val
#Allow for overriding the range
elif '_range' in key:
key = key.split('_')[0]
if key in params.keys():
assert len(val) == 2, "Must pass min and max for range! np.inf or -np.inf are OK."
params[key].min = val[0]
params[key].max = val[1]
#Allow for overriding the default guesses
elif key in params.keys():
params[key].value = val
else:
warnings.warn("Unknown keyword: "+key, UserWarning)
minObj = lf.Minimizer(fit_fn, params, fcn_args=(xdata, ydata), fcn_kws=model_kwargs)
fit_result = minObj.minimize()
return fit_result