Repository navigation
Expand file tree
/
Copy pathicf.py
More file actions
181 lines (115 loc) · 3.84 KB
/
Copy pathicf.py
File metadata and controls
181 lines (115 loc) · 3.84 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import numpy as np
import csv
import re
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# This function will calculate an R^2 values for your fit
def r_squared(data, fit):
""" Calculate the R^2 value of a fit, f, to a data set, x """
if len(data) != len(fit):
print("Data and fit lists must be of equal length")
return None
m = np.sum(data)/len(data)
# Sum of squares between data and mean
ss = np.sum((data-m)**2)
# Sum of residuals between fit and data
res = np.sum((data-fit)**2)
# This is the definition of R^2
return 1 - res/ss
def find_closest(data, v):
""" Find the value in data closest to v """
return (np.abs(data-v)).argmin()
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def load_2col(filename):
""" load a 2 column file to return to equally sized data arrays """
xdata = []
ydata = []
with open(filename, 'r') as inputfile:
lines = inputfile.readlines()
for line in lines:
data = [_f for _f in re.split('[, \t]', line) if _f]
if is_number(data[0]) and is_number(data[1]):
xdata.append(float(data[0]))
ydata.append(float(data[1]))
return np.array(xdata), np.array(ydata)
def load_3col(filename):
""" load a 3 column file to return to equally sized numpy arrays """
x1data = []
x2data = []
x3data = []
with open(filename, 'r') as inputfile:
lines = inputfile.readlines()
for line in lines:
data = [_f for _f in re.split('[,]', line) if _f]
if is_number(data[0]) and is_number(data[1]) and is_number(data[2]):
x1data.append(float(data[0]))
x2data.append(float(data[1]))
x3data.append(float(data[2]))
return np.array(x1data), np.array(x2data), np.array(x3data)
def load_4col(filename):
""" load a 2 column file to return to equally sized lists """
x1data = []
x2data = []
x3data = []
x4data = []
with open(filename, 'r') as inputfile:
lines = inputfile.readlines()
for line in lines:
data = [_f for _f in re.split('[,]', line) if _f]
if is_number(data[1]) and is_number(data[2]) and is_number(data[3]):
x1data.append(data[0])
x2data.append(float(data[1]))
x3data.append(float(data[2]))
x4data.append(float(data[3]))
return np.array(x1data), np.array(x2data), np.array(x3data), np.array(x4data)
def fixed_parameter_function(function, xdata, params, guesses, fixes):
if len(guesses)!=len(fixes):
print("Guesses and fixes arrays must be of equal length! No fitting done")
return
fixed_params = []
j=0
for i in range(len(guesses)):
if fixes[i] == 1:
fixed_params.append(guesses[i])
else:
fixed_params.append(params[j])
j+=1
return function(xdata, *fixed_params)
def fixed_param_curve_fit(func, xdata, ydata, guesses, fixes):
params = []
for i in range(len(guesses)):
if fixes[i] != 1:
params.append(guesses[i])
popt, pcov = curve_fit(lambda x,*p: fixed_parameter_function(func, x, p, guesses, fixes), xdata, ydata, p0=params)
totpopt = []
j=0
for i in range(len(guesses)):
if fixes[i] == 1:
totpopt.append(guesses[i])
else:
totpopt.append(popt[j])
j+=1
#for i in range(len(popt)):
#for j in range(len(popt)):
#if fixes[i] == 1 or fixes[j] == 1:
#pcov[i][j]=0.0
return totpopt, pcov
def fit_plot(xdata,xdata_masked, ydata, ydata_masked, fitdata, xl="", yl="",title="",width = 0):
position_ymax = np.argmax(fitdata)
f, axarr = plt.subplots(2, sharex=True)
axarr[0].set_title(title)
axarr[1].plot(xdata, ydata, lw=2, label="Data")
axarr[1].plot(xdata_masked, fitdata, lw=2, label="Fit")
axarr[1].legend()
axarr[1].set_xlabel(xl)
axarr[1].set_ylabel(yl)
axarr[0].plot(xdata_masked, ydata_masked-fitdata, lw=2, label="Residual")
axarr[0].yaxis.tick_right()
axarr[0].set_ylabel("Residual")
f.subplots_adjust(hspace=0)
plt.show()