/
functions.py
75 lines (64 loc) · 1.86 KB
/
functions.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
import numpy as np
def linear(par, xdata):
"""A linear function for minimization.
Parameters
----------
par : list or dictionary
Contains the slope and intercept,
order must be as stated.
xdata : array
An array of dependent data.
Returns
-------
ydata : array
A line calculated from the parameters
and the xdata."""
# Parse multiple input parameter
# formats for slope, intercept
if hasattr(par,'valuesdict'):
# lmfit parameter format
var = par.valuesdict()
slope = var['slope']
intercept = var['intercept']
elif hasattr(par,'keys'):
# dict format
slope = par['slope']
intercept = par['intercept']
else:
# array/list/tuple format
slope = par[0]
intercept = par[1]
# Calculate the y-data from the parameters
return intercept + slope * xdata
def exponential_decay(par, xdata):
"""An exponential decay function for minimization.
Parameters
----------
par : list or dictionary
Contains the intitial intensity ('inten')
and decay rate ('rate'),
order must be as stated.
xdata : array
An array of dependent data.
Returns
-------
ydata : array
An exponential decay calculated from the parameters
and the xdata."""
# Parse multiple input parameter
# formats for intensity, rate
if hasattr(par,'valuesdict'):
# lmfit parameter format
var = par.valuesdict()
inten = var['inten']
rate = var['rate']
elif hasattr(par,'keys'):
# dict format
inten = par['inten']
rate = par['rate']
else:
# array/list/tuple format
inten = par[0]
rate = par[1]
# Calculate the y-data from the parameters
return inten * np.exp(-1*rate*xdata)