-
Notifications
You must be signed in to change notification settings - Fork 0
/
Approximation.py
230 lines (162 loc) · 7.07 KB
/
Approximation.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
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
181
182
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd
from math import pi
import sys
import os
# # Читаем спектр образца из файла
#read data from file
il_datafile = sys.argv[1]
cnt_spectra = sys.argv[2:]
for datafile in cnt_spectra:
string = datafile.split('/')
name = string.pop()
dirname = '/'.join(string)+'/'+'results'
if not os.path.exists(dirname):
os.makedirs(dirname)
# print(name,dirname)
# print(datafile)
dataOCP=pd.read_csv(datafile, names=('wavelength', 'absorbance'), sep=';')
dataOCP.drop(dataOCP.index[:2], inplace=True)
#dataOCP.info() #control output
dataOCP=dataOCP.astype(float) #convert object to float type
#dataOCP.dtypes #show data types
# # Убираем ступеньки из спектра
n1=dataOCP.loc[dataOCP['wavelength'] == 1800.0].index.values[0]
n2=dataOCP.loc[dataOCP['wavelength'] == 860.0].index.values[0]
dataOCP.loc[n2:,'step free']=dataOCP.loc[n2:,'absorbance']
dataOCP.loc[n1:n2-1, 'step free']=dataOCP.loc[n1:n2-1,'absorbance']+(dataOCP.loc[n2,'absorbance']-\
dataOCP.loc[n2-1,'absorbance'])
dataOCP.loc[:n1-1,'step free']=dataOCP.loc[:n1-1,'absorbance']+(dataOCP.loc[n2,'absorbance']-\
dataOCP.loc[n2-1,'absorbance']+dataOCP.loc[n1,'absorbance']-dataOCP.loc[n1-1,'absorbance'])
#try to remove noise
dataOCP['rol_mean'] = dataOCP['step free'].rolling(window=3,center=True).median()
difference = np.abs(dataOCP['step free'] - dataOCP['rol_mean'])
outlier_idx = difference > 0.01
dataOCP.loc[outlier_idx, 'step free']=dataOCP.loc[outlier_idx, 'rol_mean']
#dataOCP.info()
# energy column
h=4.135667e-15 #Planck constant
cv=299792458 #speed of light
nano=1e-9 #nano scaling
dataOCP['energy']=h*cv/(dataOCP['wavelength']*nano)
#dataOCP.info()
#dataOCP
# # Спектр до вычета ионной жидкости
plt.plot(dataOCP['energy'],dataOCP['step free'])
plt.title('energy axis')
plt.xlabel('energy, eV')
plt.show()
plt.plot(dataOCP['wavelength'],dataOCP['step free'])
plt.title('wavelength axis')
plt.xlabel('wavelength, nm')
plt.show()
# # Читаем данные ионной жидкости
dataIL=pd.read_csv(il_datafile, names=('wavelength','absorbance'),sep=';')
dataIL.drop(dataIL.index[:2], inplace=True)
#dataIL.info() #control output
dataIL=dataIL.astype(float) #convert object to float type
#dataIL.dtypes
n1=dataOCP.loc[dataOCP['wavelength'] == 1800.0].index.values[0]
n2=dataOCP.loc[dataOCP['wavelength'] == 860.0].index.values[0]
dataIL.loc[n2:,'step free']=dataIL.loc[n2:,'absorbance']
dataIL.loc[n1:n2-1, 'step free']=dataIL.loc[n1:n2-1,'absorbance']+dataIL.loc[n2,'absorbance']-\
dataIL.loc[n2-1,'absorbance']
dataIL.loc[:n1-1,'step free']=dataIL.loc[:n1-1,'absorbance']+dataIL.loc[n2,'absorbance']-\
dataIL.loc[n2-1,'absorbance']+dataIL.loc[n1,'absorbance']-dataIL.loc[n1-1,'absorbance']
#dataIL.info()
dataIL['energy']=h*cv/(dataIL['wavelength']*nano)
#dataIL.info()
#dataIL
# # Спектр ионной жидкости
dataIL['rol_mean'] = dataIL['step free'].rolling(window=3,center=True).median()
difference = np.abs(dataIL['step free'] - dataIL['rol_mean'])
outlier_idx = difference > 0.01
dataIL.loc[outlier_idx, 'step free']=dataIL.loc[outlier_idx, 'rol_mean']
#dataIL.info()
plt.plot(dataIL['energy'],dataIL['step free'])
plt.title('energy axis')
plt.xlabel('energy, eV')
plt.show()
plt.plot(dataIL['wavelength'],dataIL['step free'])
plt.title('wavelength axis')
plt.xlabel('wavelength, nm')
plt.show()
# # Вычитаем ионную жидкость из спектра образца
dataOCP['data-IL']=dataOCP['step free']-dataIL['step free']
plt.plot(dataOCP['energy'],dataOCP['data-IL'])
plt.title('energy axis')
plt.xlabel('energy, eV')
plt.show()
plt.plot(dataOCP['wavelength'],dataOCP['data-IL'])
plt.title('wavelength axis')
plt.xlabel('wavelength, nm')
plt.show()
# # Ищем background
# Lorentz-Fano model
def LorentzFano(x, center,gammaL,numL, resonance,gammaF,q,numF):
return numL/(pi*gammaL*(1.0+((x-center)/gammaL)**2)) + (2*(x-resonance)/gammaF + q)**2/((2*(x-resonance)/gammaF)**2 + 1.0)*numF
# Lorentz model
def Lorentz(x, center,gammaL,numL):
return numL/(pi*gammaL*(1.0+((x-center)/gammaL)**2))
#Fano model
def Fano(x, resonance,gammaF,q,numF):
return numF*(2*(x-resonance)/gammaF + q)**2/((2*(x-resonance)/gammaF)**2 + 1.0)
x=np.array(dataOCP['energy'])
x1=np.concatenate((x[725:777], x[144:430]))
y=np.array(dataOCP['data-IL'])
y1=np.concatenate((y[725:777], y[144:430]))
minparam=(4,0.1,0, 4,0.1,-10,0.005) #lower bound for approximation parameters
maxparam=(6,3,10, 6,3,0,1) #upper bound for approximation parameters
LFopt, LFcov = curve_fit(LorentzFano, x1, y1, bounds=(minparam,maxparam), method='trf')
Lopt, Lcov = curve_fit(Lorentz, x1, y1, bounds=(minparam[0:3],maxparam[0:3]), method='trf')
Fopt, Fcov = curve_fit(Fano, x1, y1, bounds=(minparam[3:7],maxparam[3:7]), method='trf')
print('Lorentz-Fano parameters')
print(LFopt)
print('Lorentz parameters')
print(Lopt)
print('Fano parameters')
print(Fopt)
plt.plot(x,y)
LF=plt.plot(x, LorentzFano(x, *LFopt), color='red', label='Lorentz-Fano')
#L=plt.plot(x, Lorentz(x, *Lopt), label='Lorentz')
#F=plt.plot(x, Fano(x, *Fopt), color='green', label='Fano')
plt.xlabel('energy, eV')
plt.ylabel('absorbance')
plt.legend()
plt.show()
u=np.array(dataOCP['wavelength'])
plt.plot(u,y)
LF=plt.plot(u, LorentzFano(x, *LFopt), color='red', label='Lorentz-Fano')
#L=plt.plot(x, Lorentz(x, *Lopt), label='Lorentz')
#F=plt.plot(x, Fano(x, *Fopt), color='green', label='Fano')
plt.xlabel('wavelength, nm')
plt.ylabel('absorbance')
plt.legend()
plt.show()
# # Записываем данные в таблицу
dataOCP['Lorentz-Fano']=LorentzFano(x, *LFopt)
dataOCP['Lorentz']=Lorentz(x, *Lopt)
dataOCP['Fano']=Fano(x, *Fopt)
#calculate data - approximation
dataOCP['data-LF']=dataOCP['data-IL']-dataOCP['Lorentz-Fano']
dataOCP['data-L']=dataOCP['data-IL']-dataOCP['Lorentz']
dataOCP['data-F']=dataOCP['data-IL']-dataOCP['Fano']
#dataOCP
DLF=plt.plot(x, dataOCP['data-LF'], color='red', label='data - Lorentz-Fano')
#DL=plt.plot(x, dataOCP['data-L'], label='Lorentz', color='blue')
#DF=plt.plot(x, dataOCP['data-F'], label='Fano', color='green')
plt.xlabel('energy, eV')
plt.ylabel('absorbance')
plt.legend()
plt.show()
DLF=plt.plot(u, dataOCP['data-LF'], color='red', label='data - Lorentz-Fano')
#DL=plt.plot(x, dataOCP['data-L'], label='Lorentz', color='blue')
#DF=plt.plot(x, dataOCP['data-F'], label='Fano', color='green')
plt.xlabel('wavelength, eV')
plt.ylabel('absorbance')
plt.legend()
plt.show()
filename=dirname+'/'+name
dataOCP.to_csv(filename, sep=';')