/
auxiliary.py
62 lines (49 loc) · 1.85 KB
/
auxiliary.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
import pandas as pd
# Loss function for error calculation
def error_function(par, func, xdata, ydata=None, yerr=None):
"""Calculate the error as the sum of square of residuals
Parameters
----------
par : list
Parameter values.
func : function
Function being minimized.
xdata : array
The independent data
ydata : array
The dependent data [optional, will predict if omitted]
yerr : array
The error estimats [optional]
Returns
-------
data : array
Either the predicted value or the residuals squared
(with or without error weighting) are returned."""
# The calculated value
ycalc = func(par, xdata)
if ydata is None:
# Calculation only
return ycalc
elif yerr is None:
# Error minimization
return (ycalc - ydata)**2
else:
# Error minimization with weights
return (ycalc - ydata)**2/yerr**2
# # Function to expand arrays contained in a single dataframe row
# def expand_df(data, name, groupby):
# if len(groupby) == 1:
# groupby = groupby[0]
# if isinstance(data, pd.DataFrame):
# dfexpand = pd.concat([pd.Series(np.array(x[1].values[0]),
# index=pd.Index([x[0]]*len(x[1].values[0]), name=groupby),
# name=name)
# for x in data.iterrows()], axis=0)
# else:
# dfexpand = pd.concat([pd.Series(np.array(x[1]),
# index=pd.Index([x[0]]*len(x[1]), name=groupby),
# name=name)
# for x in data.iteritems()], axis=0)
# if not(isinstance(dfexpand, pd.DataFrame)):
# dfexpand = pd.DataFrame(dfexpand)
# return dfexpand