-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitxps.py
99 lines (91 loc) · 2.88 KB
/
fitxps.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
# read the data file - parse it
# read the paramete file - parse it
# perform fit and plot
# v1 - class skelton, process data funcion
# v2 - added readData to the class
# v3 - clean up
# v4 - working fit code
# v5 - adding commandline execution logic
import pandas as pd
from multi_gauss import multiGauss as Gauss
from scipy.optimize import curve_fit as curveFit
from matplotlib import pyplot as plt
import sys
import os
class ProcessAndFit:
"""
- reads the data file data.txt
- converts the data to x, y dataframe
- reads the parameter file and saves the
- parameter in `guess'
- performs fit
- displays the final result
"""
def __init__(self, dfile=None, pfile=None):
"""
dfile -> contains the XPS binding energy data
pfile -> contains the parameters
"""
self.dfile = dfile
self.pfile = pfile
def readData(self):
"""
- reads the excel data
- assumes that first column is Binding Energy
- assumes the second column is intensity
- exports x, y where
- x -> BE panda data frame
- y -> Intensity panda data frame
"""
df = pd.read_excel(self.dfile)
x = df.columns[0]
y = df.columns[1]
return df[x], df[y]
def readParam(self):
"""
- read the fitting guess parameters from excel file
- excel file format
int BE width
12304 233.4 0.5
36303 238.2 0.5
"""
df = pd.read_excel(self.pfile)
guess = [x for row in df.iloc for x in row]
return guess
def fit(self):
"""
performs fit
"""
x, y = self.readData()
guess = self.readParam()
ytofit = Gauss(x, *guess)
# bound -> lower and upper bound of fitting params
bounds = (0, [20000] * len(guess))
popt, _ = curveFit(Gauss, x, y, p0=[*guess], bounds=bounds)
yfit = Gauss(x, *popt)
plt.plot(x, y)
plt.plot(x, ytofit)
plt.plot(x, yfit)
plt.xlabel('B.E (eV)')
plt.ylabel('Intensity (arb.units)')
plt.legend(['data', 'guess', 'fit'])
plt.show()
def __call__(self):
self.fit()
if __name__ == '__main__':
#print(sys.argv)
try:
#if (sys.argv[1] and sys.argv[2]):
dfile = sys.argv[1]
pfile = sys.argv[2]
#ProcessAndFit('testdata.xlsx', 'testguess.xlsx')()
ProcessAndFit(dfile, pfile)()
except Exception:
print('Looking for files in the current working directory')
if os.path.exists('testdata.xlsx') and \
os.path.exists('testguess.xlsx'):
ProcessAndFit('testdata.xlsx', 'testguess.xlsx')()
else:
print("Usage: python3 data.xlsx param.xlsx")
print("OR put the data and parameter file in the \
same directory as .py file")