|
1
|
|
|
# Import packages |
|
|
|
|
|
|
2
|
|
|
import pandas as pd |
|
3
|
|
|
import numpy as np |
|
4
|
|
|
import csv |
|
5
|
|
|
import matplotlib.pyplot as plt |
|
6
|
|
|
from dir_walker import walker |
|
7
|
|
|
import os |
|
8
|
|
|
import warnings |
|
9
|
|
|
|
|
10
|
|
|
#-----------------------------------MODULE CHECKS------------------------------ |
|
11
|
|
|
|
|
12
|
|
|
# Check for modules, try to exit gracefully if not found |
|
13
|
|
|
import sys |
|
14
|
|
|
import imp |
|
15
|
|
|
try: |
|
16
|
|
|
imp.find_module('numpy') |
|
17
|
|
|
foundnp = True |
|
18
|
|
|
except ImportError: |
|
19
|
|
|
foundnp = False |
|
20
|
|
|
try: |
|
21
|
|
|
imp.find_module('matplotlib') |
|
22
|
|
|
foundplot = True |
|
23
|
|
|
except ImportError: |
|
24
|
|
|
foundplot = False |
|
25
|
|
|
try: |
|
26
|
|
|
imp.find_module('pandas') |
|
27
|
|
|
foundpd = True |
|
28
|
|
|
except ImportError: |
|
29
|
|
|
foundplot = False |
|
30
|
|
|
if not foundnp: |
|
31
|
|
|
print("Numpy is required. Exiting") |
|
32
|
|
|
sys.exit() |
|
33
|
|
|
if not foundplot: |
|
34
|
|
|
print("Matplotlib is required. Exiting") |
|
35
|
|
|
sys.exit() |
|
36
|
|
|
if not foundpd: |
|
|
|
|
|
|
37
|
|
|
print("Pandas is required. Exiting") |
|
38
|
|
|
sys.exit() |
|
39
|
|
|
|
|
40
|
|
|
#------------------------------------------------------------------------------- |
|
41
|
|
|
class cv: |
|
42
|
|
|
_colors = {'red': 'r', 'green': 'g', 'blue': 'b', 'yellow': 'y', 'cyan': 'c', 'magenta': 'm', 'black': 'k', 'white': 'w'} |
|
|
|
|
|
|
43
|
|
|
_linestyles = {' ': ' ', '-': 'solid', '--': 'dashed', '-.': 'dash_dot', ':': 'dotted'} |
|
44
|
|
|
_markers = {' ': ' ', '.': '.', '_' : '_', 'o': 'o', '*': '*', '+': '+', 'x': 'x', 'square': 's', 'triangle': '^', 'diamond': 'd'} |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
# Find relevant files in folder |
|
47
|
|
|
path = os.getcwd() |
|
48
|
|
|
extension = ['csv','txt','dat'] # Should include all the possible ones |
|
|
|
|
|
|
49
|
|
|
os.chdir(path) |
|
50
|
|
|
result = [i for i in glob.glob('*.{}'.format(extension))] |
|
|
|
|
|
|
51
|
|
|
print("Plotting the following:") |
|
52
|
|
|
print(result) |
|
53
|
|
|
#Make x-axis |
|
54
|
|
|
t = np.linspace(325, 1100, 776) |
|
55
|
|
|
|
|
56
|
|
|
def det(a, b): |
|
57
|
|
|
return a[0] * b[1] - a[1] * b[0] |
|
58
|
|
|
|
|
59
|
|
|
#set initial values (for later use) |
|
60
|
|
|
k=0 |
|
|
|
|
|
|
61
|
|
|
totalexport=[] |
|
|
|
|
|
|
62
|
|
|
#iterate through CSVs |
|
63
|
|
|
while k < len(csvresult): |
|
|
|
|
|
|
64
|
|
|
horiz=.04 |
|
|
|
|
|
|
65
|
|
|
exportlist=[] |
|
|
|
|
|
|
66
|
|
|
path = os.getcwd() |
|
67
|
|
|
path = path + "/" + csvresult[k] |
|
68
|
|
|
cv=pd.read_csv(path, delimiter=",", error_bad_lines=False) |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
#Extract Data from Current Column |
|
71
|
|
|
currentwhole=cv.values[:,1] |
|
|
|
|
|
|
72
|
|
|
ind=np.where(currentwhole == ' Current/A') |
|
|
|
|
|
|
73
|
|
|
startdata=ind[0][0] |
|
|
|
|
|
|
74
|
|
|
startdata=startdata + 1 |
|
|
|
|
|
|
75
|
|
|
currentdatastr=cv.values[startdata:,1] |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
#Extract Data from Potential Column |
|
78
|
|
|
potentialdatastr=cv.values[startdata:,0] |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
#Convert all elements from strings to floats so they can be math. manipulated |
|
81
|
|
|
currentdataOG = [ float(x) for x in currentdatastr ] |
|
|
|
|
|
|
82
|
|
|
potentialdata = [ float(x) for x in potentialdatastr ] |
|
|
|
|
|
|
83
|
|
|
#Multiply current data to fit axis properly |
|
84
|
|
|
currentdata = [ i * (100000) for i in currentdataOG ] |
|
|
|
|
|
|
85
|
|
|
#assign attributes to plot |
|
86
|
|
|
colour = 'b' |
|
87
|
|
|
plotlabel = csvresult[k] |
|
88
|
|
|
plotlabel = plotlabel[:-4] |
|
89
|
|
|
totalexport.append(plotlabel) |
|
90
|
|
|
#plot graph |
|
91
|
|
|
plt.plot(potentialdata, currentdata, colour, label= plotlabel) |
|
|
|
|
|
|
92
|
|
|
plt.draw() |
|
93
|
|
|
|
|
94
|
|
|
count=1 |
|
|
|
|
|
|
95
|
|
|
while count <= 2: |
|
96
|
|
|
xline1=[] #x values of line 1 to fill in as code proceeds |
|
|
|
|
|
|
97
|
|
|
yline1=[] |
|
|
|
|
|
|
98
|
|
|
xline2=[] |
|
|
|
|
|
|
99
|
|
|
yline2=[] |
|
|
|
|
|
|
100
|
|
|
#Point Clicks and Intersections |
|
101
|
|
View Code Duplication |
if count==1: |
|
|
|
|
|
|
102
|
|
|
print('>> Please choose two points for first line (onset of oxidation)') |
|
103
|
|
|
line1 = plt.ginput(2) # it will wait for two clicks |
|
104
|
|
|
line1=np.array(line1) |
|
|
|
|
|
|
105
|
|
|
x1=line1[0,0] |
|
|
|
|
|
|
106
|
|
|
xline1.append(x1) |
|
107
|
|
|
y1=line1[0,1] |
|
|
|
|
|
|
108
|
|
|
yline1.append(y1) |
|
109
|
|
|
print('>> Please choose two points for first line (onset of oxidation)') |
|
110
|
|
|
line2 = plt.ginput(2) |
|
111
|
|
|
line2=np.array(line2) |
|
|
|
|
|
|
112
|
|
|
x2=line2[0,0] |
|
|
|
|
|
|
113
|
|
|
xline2.append(x2) |
|
114
|
|
|
y2=line2[0,1] |
|
|
|
|
|
|
115
|
|
|
yline2.append(y2) |
|
116
|
|
View Code Duplication |
if count==2: |
|
|
|
|
|
|
117
|
|
|
print('>> Please choose two points for first line (onset of reduction)') |
|
118
|
|
|
line1 = plt.ginput(2) # it will wait for two clicks |
|
119
|
|
|
line1=np.array(line1) |
|
|
|
|
|
|
120
|
|
|
x1=line1[0,0] |
|
|
|
|
|
|
121
|
|
|
xline1.append(x1) |
|
122
|
|
|
y1=line1[0,1] |
|
|
|
|
|
|
123
|
|
|
yline1.append(y1) |
|
124
|
|
|
print('>> Please choose two points for first line (onset of reduction)') |
|
125
|
|
|
line2 = plt.ginput(2) |
|
126
|
|
|
line2=np.array(line2) |
|
|
|
|
|
|
127
|
|
|
x2=line2[0,0] |
|
|
|
|
|
|
128
|
|
|
xline2.append(x2) |
|
129
|
|
|
y2=line2[0,1] |
|
|
|
|
|
|
130
|
|
|
yline2.append(y2) |
|
131
|
|
|
#Find intersection |
|
132
|
|
|
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0]) |
|
|
|
|
|
|
133
|
|
|
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) |
|
134
|
|
|
div = det(xdiff, ydiff) |
|
135
|
|
|
#if div == 0: |
|
136
|
|
|
#raise Exception('lines do not intersect') |
|
137
|
|
|
d = (det(*line1), det(*line2)) |
|
138
|
|
|
|
|
139
|
|
|
#calculate onset |
|
140
|
|
|
x = det(d, xdiff) / div |
|
141
|
|
|
xline1.append(x) |
|
142
|
|
|
xline2.append(x) |
|
143
|
|
|
#make point of intersection |
|
144
|
|
|
y = det(d, ydiff) / div |
|
145
|
|
|
yline1.append(y) |
|
146
|
|
|
yline2.append(y) |
|
147
|
|
|
|
|
148
|
|
|
plt.plot(xline1,yline1,'r') |
|
|
|
|
|
|
149
|
|
|
plt.plot(xline2,yline2,'r') |
|
|
|
|
|
|
150
|
|
|
plt.draw() |
|
151
|
|
|
|
|
152
|
|
|
exportlist.append(x) |
|
153
|
|
|
#calculate homo/lumo |
|
154
|
|
|
holu = x - 4.8 |
|
155
|
|
|
exportlist.append(holu) |
|
156
|
|
|
#round these values |
|
157
|
|
|
x = round(x,2) |
|
|
|
|
|
|
158
|
|
|
holu = round(holu,2) |
|
|
|
|
|
|
159
|
|
|
#make these strings instead of integers |
|
160
|
|
|
x = str(x) |
|
161
|
|
|
holu = str(holu) |
|
162
|
|
|
#print message on command prompt |
|
163
|
|
|
if count==1: |
|
|
|
|
|
|
164
|
|
|
print('------------') |
|
165
|
|
|
print("Oxidation onset:") |
|
166
|
|
|
print(x + " V") |
|
167
|
|
|
print('------------') |
|
168
|
|
|
if count==2: |
|
|
|
|
|
|
169
|
|
|
print('------------') |
|
170
|
|
|
print("Reduction onset:") |
|
171
|
|
|
print(x + " V") |
|
172
|
|
|
print('------------') |
|
173
|
|
|
count=count+1 |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
totalexport.append(exportlist) |
|
176
|
|
|
#write texts to put on chart |
|
177
|
|
|
ox=exportlist[0] |
|
|
|
|
|
|
178
|
|
|
ox=round(ox,3) |
|
|
|
|
|
|
179
|
|
|
ox=str(ox) |
|
|
|
|
|
|
180
|
|
|
homo=exportlist[1] |
|
|
|
|
|
|
181
|
|
|
homo=round(homo,3) |
|
|
|
|
|
|
182
|
|
|
homo=str(homo) |
|
|
|
|
|
|
183
|
|
|
red=exportlist[2] |
|
|
|
|
|
|
184
|
|
|
red=round(red,3) |
|
|
|
|
|
|
185
|
|
|
red=str(red) |
|
|
|
|
|
|
186
|
|
|
lumo=exportlist[3] |
|
|
|
|
|
|
187
|
|
|
lumo=round(lumo,3) |
|
|
|
|
|
|
188
|
|
|
lumo=str(lumo) |
|
|
|
|
|
|
189
|
|
|
oxtxt = 'OX' '$_{onset}$' + ' = ' + ox + "V" |
|
190
|
|
|
redtxt = 'RED' '$_{onset}$' + ' = ' + red + "V" |
|
191
|
|
|
homotxt = 'HOMO = ' + homo + "eV" |
|
192
|
|
|
lumotxt = 'LUMO = ' + lumo + "eV" |
|
193
|
|
|
#change initial value and make first text label |
|
194
|
|
|
plt.gca().set_position((.1, .28, .8, .65)) # to make a bit of room for extra text |
|
195
|
|
|
vert = 0.14 |
|
196
|
|
|
plt.figtext(horiz,vert,plotlabel,style='italic') |
|
|
|
|
|
|
197
|
|
|
#plt.figure().add_subplot(111).plot(range(10), range(10)) |
|
198
|
|
|
txtlist = [oxtxt,redtxt,homotxt, lumotxt] |
|
|
|
|
|
|
199
|
|
|
i=0 |
|
|
|
|
|
|
200
|
|
|
#iterate through text list to make text boxes of values |
|
201
|
|
|
while i < len(txtlist): |
|
202
|
|
|
if i == 2: |
|
203
|
|
|
horiz=horiz+0.25 |
|
|
|
|
|
|
204
|
|
|
vert=vert+0.1 |
|
|
|
|
|
|
205
|
|
|
vert=vert-0.05 |
|
|
|
|
|
|
206
|
|
|
curr=txtlist[i] |
|
|
|
|
|
|
207
|
|
|
plt.figtext(horiz,vert,curr) |
|
|
|
|
|
|
208
|
|
|
i=i+1 |
|
|
|
|
|
|
209
|
|
|
continue |
|
210
|
|
|
#----------------------------------EXCEL EXPORT--------------------------------- |
|
211
|
|
|
|
|
212
|
|
|
# Create a Pandas dataframe title for data. |
|
213
|
|
|
df0 = pd.DataFrame({'Data': ['Onset of Oxidation (V)','HOMO (eV)','Onset of Reduction (V)','LUMO (eV)']}) |
|
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
# Create a Pandas Excel writer using XlsxWriter as the engine. |
|
216
|
|
|
folder = 'Processed CV Data' |
|
217
|
|
|
if not os.path.exists(folder): |
|
218
|
|
|
os.makedirs(folder) |
|
219
|
|
|
|
|
220
|
|
|
calcname=csvresult[k] |
|
|
|
|
|
|
221
|
|
|
calcname=calcname + '-calcs.xlsx' |
|
|
|
|
|
|
222
|
|
|
writer = pd.ExcelWriter(os.path.join(folder,calcname), engine='xlsxwriter') |
|
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
#Loop through data from total export list |
|
225
|
|
|
j=0 |
|
|
|
|
|
|
226
|
|
|
index=1 |
|
|
|
|
|
|
227
|
|
|
while j < len(totalexport): |
|
228
|
|
|
plotlabel=totalexport[j] |
|
|
|
|
|
|
229
|
|
|
data=totalexport[j+1] |
|
|
|
|
|
|
230
|
|
|
df = pd.DataFrame({plotlabel: data}) |
|
231
|
|
|
df.to_excel(writer, sheet_name='Sheet1', startcol=index, index=False) |
|
232
|
|
|
j=j+2 |
|
|
|
|
|
|
233
|
|
|
index=index+1 |
|
|
|
|
|
|
234
|
|
|
continue |
|
235
|
|
|
|
|
236
|
|
|
# Convert the dataframe to an XlsxWriter Excel object. |
|
237
|
|
|
df0.to_excel(writer, sheet_name='Sheet1', startrow=1, header = False, index=False) |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
# Get the xlsxwriter workbook and worksheet objects. |
|
240
|
|
|
workbook = writer.book |
|
|
|
|
|
|
241
|
|
|
worksheet = writer.sheets['Sheet1'] |
|
242
|
|
|
|
|
243
|
|
|
# Set the column width and format. |
|
244
|
|
|
worksheet.set_column('A:A', 20) |
|
245
|
|
|
worksheet.set_column('B:B', 15) |
|
246
|
|
|
worksheet.set_column('C:C', 15) |
|
247
|
|
|
worksheet.set_column('D:D', 15) |
|
248
|
|
|
|
|
249
|
|
|
# Close the Pandas Excel writer and output the Excel file. |
|
250
|
|
|
writer.save() |
|
251
|
|
|
msg= csvresult[k][:-4] + ' CV Calculations exported --------->' |
|
|
|
|
|
|
252
|
|
|
print(msg) |
|
253
|
|
|
|
|
254
|
|
|
#---------> DATA TO CREATE GRAPH IN EXCEL |
|
255
|
|
|
|
|
256
|
|
|
# Create a Pandas Excel writer using XlsxWriter as the engine. |
|
257
|
|
|
dataname=csvresult[k] |
|
|
|
|
|
|
258
|
|
|
dataname=dataname + '-dataset.xlsx' |
|
|
|
|
|
|
259
|
|
|
writer = pd.ExcelWriter(os.path.join(folder,dataname), engine='xlsxwriter') |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
#Create wavelength column |
|
262
|
|
|
potentialdatastr=[float(x) for x in potentialdatastr] |
|
|
|
|
|
|
263
|
|
|
df = pd.DataFrame({'Potential/ V': potentialdatastr}) |
|
264
|
|
|
df.to_excel(writer, sheet_name='Sheet1', startcol=0, startrow=0, index=False) |
|
265
|
|
|
df1 = pd.DataFrame({'Current/ A': currentdatastr}) |
|
266
|
|
|
df1.to_excel(writer, sheet_name='Sheet1', startcol=1, startrow=0, index=False) |
|
267
|
|
|
i=0 |
|
|
|
|
|
|
268
|
|
|
currentdatastr=[float(x) for x in currentdatastr] |
|
|
|
|
|
|
269
|
|
|
microcurrentdatastr=[x*1000000 for x in currentdatastr] |
|
|
|
|
|
|
270
|
|
|
df2 = pd.DataFrame({'Current/ microA': microcurrentdatastr}) |
|
271
|
|
|
df2.to_excel(writer, sheet_name='Sheet1', startcol=2, startrow=0, index=False) |
|
272
|
|
|
|
|
273
|
|
|
# Get the xlsxwriter workbook and worksheet objects. |
|
274
|
|
|
workbook = writer.book |
|
|
|
|
|
|
275
|
|
|
worksheet = writer.sheets['Sheet1'] |
|
276
|
|
|
|
|
277
|
|
|
# Set the column width and format. |
|
278
|
|
|
worksheet.set_column('A:A', 11) |
|
279
|
|
|
worksheet.set_column('B:B', 11) |
|
280
|
|
|
worksheet.set_column('C:C', 11) |
|
281
|
|
|
|
|
282
|
|
|
# Close the Pandas Excel writer and output the Excel file. |
|
283
|
|
|
writer.save() |
|
284
|
|
|
msg= csvresult[k][:-4] + ' CV Dataset exported --------->' |
|
|
|
|
|
|
285
|
|
|
print(msg) |
|
286
|
|
|
|
|
287
|
|
|
#------------------------------------------------------------------------------- |
|
288
|
|
|
|
|
289
|
|
|
#Bold axis numbers and change font sizes |
|
290
|
|
|
ax=plt.gca() |
|
|
|
|
|
|
291
|
|
|
for tick in ax.xaxis.get_major_ticks(): |
|
292
|
|
|
tick.label1.set_fontsize(9) |
|
293
|
|
|
tick.label1.set_fontweight('bold') |
|
294
|
|
|
for tick in ax.yaxis.get_major_ticks(): |
|
295
|
|
|
tick.label1.set_fontsize(9) |
|
296
|
|
|
tick.label1.set_fontweight('bold') |
|
297
|
|
|
#Labels |
|
298
|
|
|
plt.xlabel('Potential (V) / V',weight='bold') |
|
|
|
|
|
|
299
|
|
|
plt.ylabel('Current (I) / x10$^{-1}$${\mu}A$',weight='bold') |
|
|
|
|
|
|
300
|
|
|
plt.draw() |
|
301
|
|
|
#Graph Finished Message |
|
302
|
|
|
sep=" " |
|
|
|
|
|
|
303
|
|
|
name = os.getlogin() |
|
304
|
|
|
name = name.split(sep, 1)[0] |
|
305
|
|
|
msg = 'Hey ' + name + ', ' + csvresult[k][:-4] + "'s graph has finished processing." |
|
306
|
|
|
picname= csvresult[k][:-4] + '.png' |
|
|
|
|
|
|
307
|
|
|
print(msg) |
|
308
|
|
|
plt.savefig(os.path.join(folder,picname), bbox_inches='tight') |
|
|
|
|
|
|
309
|
|
|
k=k+1 |
|
|
|
|
|
|
310
|
|
|
#case where if you reach your last CSV, then break the loop |
|
311
|
|
|
plt.show() |
|
312
|
|
|
if k == len(csvresult): |
|
313
|
|
|
plt.title('Cyclic Voltammetry',weight='bold') |
|
|
|
|
|
|
314
|
|
|
plt.legend(loc='best') |
|
315
|
|
|
break |
|
316
|
|
|
continue |
|
317
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.