-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.py
390 lines (357 loc) · 12.6 KB
/
cv.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#-----------------------------------MODULE CHECKS------------------------------
# Check for modules, try to exit gracefully if not found
import sys
import imp
try:
imp.find_module('numpy')
foundnp = True
except ImportError:
foundnp = False
try:
imp.find_module('matplotlib')
foundplot = True
except ImportError:
foundplot = False
try:
imp.find_module('pandas')
foundpd = True
except ImportError:
foundplot = False
if not foundnp:
print("Numpy is required. Exiting")
sys.exit()
if not foundplot:
print("Matplotlib is required. Exiting")
sys.exit()
if not foundpd:
print("Pandas is required. Exiting")
sys.exit()
#-------------------------------------------------------------------------------
import os
import glob
#Stop message from appearing
import warnings
warnings.filterwarnings("ignore",".*GUI is implemented.*")
warnings.filterwarnings("ignore",".*No labelled objects found.*")
#Find relevant CSVs in folder
path = os.getcwd()
extension = 'csv'
os.chdir(path)
csvresult = [i for i in glob.glob('*.{}'.format(extension))]
print("Plotting the following:")
print(csvresult)
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import math
from tkinter import *
#Make x-axis
t = np.linspace(325, 1100, 776)
#define color for graph lines
#color_code=['b','k','r','g','c','m', 'y', 'g', 'crimson', 'teal', 'aqua']
#define determinant calculation
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
#set initial values (for later use)
k=0
totalexport=[]
#iterate through CSVs
while k < len(csvresult):
horiz=.04
exportlist=[]
path = os.getcwd()
path = path + "/" + csvresult[k]
cv=pd.read_csv(path, delimiter=",", error_bad_lines=False)
#Extract Data from Current Column
currentwhole=cv.values[:,1]
ind=np.where(currentwhole == ' Current/A')
startdata=ind[0][0]
startdata=startdata + 1
currentdatastr=cv.values[startdata:,1]
#Extract Data from Potential Column
potentialdatastr=cv.values[startdata:,0]
#Convert all elements from strings to floats so they can be math. manipulated
currentdataOG = [ float(x) for x in currentdatastr ]
potentialdata = [ float(x) for x in potentialdatastr ]
#Multiply current data to fit axis properly
currentdata = [ i * (100000) for i in currentdataOG ]
#assign attributes to plot
colour = 'b'
plotlabel = csvresult[k]
plotlabel = plotlabel[:-4]
totalexport.append(plotlabel)
#plot graph
plt.plot(potentialdata, currentdata, colour, label= plotlabel)
plt.draw()
plt.pause(0.0001)
master = Tk()
Label(master, text="Which of the following applies to this graph:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Oxidation", variable=var1).grid(row=1, sticky=SW)
var2 = IntVar()
Checkbutton(master, text="Reduction", variable=var2).grid(row=2, sticky=W)
screen_width=master.winfo_screenwidth()
screen_height=master.winfo_screenheight()
x_co=(screen_width/2)-240
y_co=(screen_height/2)-188
master.geometry("%dx%d+%d+%d" % (300,100,x_co,y_co))
mainloop()
if var1.get()==0:
maxcount=1
print('Only Reduction')
if var2.get()==0:
maxcount=1
print('Only Oxidation')
if var1.get()==1 and var2.get()==1:
print('Both Oxidation and Reduction')
maxcount=2
count=1
while count <= maxcount:
xline1=[] #x values of line 1 to fill in as code proceeds
yline1=[]
xline2=[]
yline2=[]
#Point Clicks and Intersections
if count==1:
if var1.get()==0:
print('>> Please choose two points for first line (onset of reduction)')
else:
print('>> Please choose two points for first line (onset of oxidation)')
line1 = plt.ginput(2) # it will wait for two clicks
line1=np.array(line1)
x1=line1[0,0]
xline1.append(x1)
y1=line1[0,1]
yline1.append(y1)
if var1.get()==0:
print('>> Please choose two points for second line (onset of reduction)')
else:
print('>> Please choose two points for second line (onset of oxidation)')
line2 = plt.ginput(2)
line2=np.array(line2)
x2=line2[0,0]
xline2.append(x2)
y2=line2[0,1]
yline2.append(y2)
if count==2:
print('>> Please choose two points for first line (onset of reduction)')
line1 = plt.ginput(2) # it will wait for two clicks
line1=np.array(line1)
x1=line1[0,0]
xline1.append(x1)
y1=line1[0,1]
yline1.append(y1)
print('>> Please choose two points for second line (onset of reduction)')
line2 = plt.ginput(2)
line2=np.array(line2)
x2=line2[0,0]
xline2.append(x2)
y2=line2[0,1]
yline2.append(y2)
#Find intersection
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
div = det(xdiff, ydiff)
#if div == 0:
#raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
#calculate onset
x = det(d, xdiff) / div
xline1.append(x)
xline2.append(x)
#make point of intersection
y = det(d, ydiff) / div
yline1.append(y)
yline2.append(y)
plt.plot(xline1,yline1,'r')
plt.plot(xline2,yline2,'r')
plt.draw()
exportlist.append(x)
#calculate homo/lumo
holu = (-1)*x - 4.8
exportlist.append(holu)
#round these values
x = round(x,2)
holu = round(holu,2)
#make these strings instead of integers
x = str(x)
holu = str(holu)
#print message on command prompt
if count==1:
print('------------')
if var1.get()==1:
print("Oxidation onset:")
if var1.get()==0:
print("Reduction onset:")
print(x + " V")
print('------------')
if count==2:
print('------------')
print("Reduction onset:")
print(x + " V")
print('------------')
count=count+1
continue
totalexport.append(exportlist)
#write texts to put on chart
if var1.get()==0:
ox='---'
homo='---'
if var2.get()==1:
red=exportlist[0]
red=round(red,3)
red=str(red)
lumo=exportlist[1]
lumo=round(lumo,3)
lumo=str(lumo)
if var2.get()==0:
red='---'
lumo='---'
if var1.get()==1:
ox=exportlist[0]
ox=round(ox,3)
ox=str(ox)
homo=exportlist[1]
homo=round(homo,3)
homo=str(homo)
if var2.get()==1:
red=exportlist[2]
red=round(red,3)
red=str(red)
lumo=exportlist[3]
lumo=round(lumo,3)
lumo=str(lumo)
if var2.get()==0:
red='---'
lumo='---'
oxtxt = 'OX' '$_{onset}$' + ' = ' + ox + " V"
redtxt = 'RED' '$_{onset}$' + ' = ' + red + " V"
homotxt = 'HOMO = ' + homo + " eV"
lumotxt = 'LUMO = ' + lumo + " eV"
#change initial value and make first text label
plt.gca().set_position((.1, .28, .8, .65)) # to make a bit of room for extra text
vert = 0.14
plt.figtext(horiz,vert,plotlabel,style='italic')
#plt.figure().add_subplot(111).plot(range(10), range(10))
txtlist = [oxtxt,redtxt,homotxt, lumotxt]
i=0
#iterate through text list to make text boxes of values
while i < len(txtlist):
if i == 2:
horiz=horiz+0.25
vert=vert+0.1
vert=vert-0.05
curr=txtlist[i]
plt.figtext(horiz,vert,curr)
i=i+1
continue
#----------------------------------EXCEL EXPORT---------------------------------
# Create a Pandas dataframe title for data.
df0 = pd.DataFrame({'Data': ['Onset of Oxidation (V)','HOMO (eV)','Onset of Reduction (V)','LUMO (eV)']})
# Create a Pandas Excel writer using XlsxWriter as the engine.
folder = 'Processed CV Data'
if not os.path.exists(folder):
os.makedirs(folder)
calcname=csvresult[k]
calcname=calcname + '-calcs.xlsx'
writer = pd.ExcelWriter(os.path.join(folder,calcname), engine='xlsxwriter')
#Loop through data from total export list
j=0
index=1
while j < len(totalexport):
plotlabel=totalexport[j]
data=totalexport[j+1]
df = pd.DataFrame({plotlabel: data})
df.to_excel(writer, sheet_name='Sheet1', startcol=index, index=False)
j=j+2
index=index+1
continue
# Convert the dataframe to an XlsxWriter Excel object.
df0.to_excel(writer, sheet_name='Sheet1', startrow=1, header = False, index=False)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Set the column width and format.
worksheet.set_column('A:A', 20)
worksheet.set_column('B:B', 15)
worksheet.set_column('C:C', 15)
worksheet.set_column('D:D', 15)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
msg= csvresult[k][:-4] + ' CV Calculations exported --------->'
print(msg)
#---------> DATA TO CREATE GRAPH IN EXCEL
# Create a Pandas Excel writer using XlsxWriter as the engine.
dataname=csvresult[k]
dataname=dataname + '-dataset.xlsx'
writer = pd.ExcelWriter(os.path.join(folder,dataname), engine='xlsxwriter')
#Create wavelength column
potentialdatastr=[float(x) for x in potentialdatastr]
df = pd.DataFrame({'Potential/ V': potentialdatastr})
df.to_excel(writer, sheet_name='Sheet1', startcol=0, startrow=0, index=False)
df1 = pd.DataFrame({'Current/ A': currentdatastr})
df1.to_excel(writer, sheet_name='Sheet1', startcol=1, startrow=0, index=False)
i=0
currentdatastr=[float(x) for x in currentdatastr]
microcurrentdatastr=[x*1000000 for x in currentdatastr]
df2 = pd.DataFrame({'Current/ microA': microcurrentdatastr})
df2.to_excel(writer, sheet_name='Sheet1', startcol=2, startrow=0, index=False)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Set the column width and format.
worksheet.set_column('A:A', 11)
worksheet.set_column('B:B', 11)
worksheet.set_column('C:C', 11)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
msg= csvresult[k][:-4] + ' CV Dataset exported --------->'
print(msg)
#-------------------------------------------------------------------------------
#Bold axis numbers and change font sizes
ax=plt.gca()
for tick in ax.xaxis.get_major_ticks():
tick.label1.set_fontsize(9)
tick.label1.set_fontweight('bold')
for tick in ax.yaxis.get_major_ticks():
tick.label1.set_fontsize(9)
tick.label1.set_fontweight('bold')
#Axis Ranges
## xmin=min(potentialdata)
## if xmin < 0:
## xmin=float(str(math.floor(xmin*10)/10)[:-1] + str(int(str(math.floor(xmin*10)/10)[-1:])-1))
## else:
## xmin=float(str(math.floor(xmin*10)/10)[:-1] + str(int(str(math.floor(xmin*10)/10)[-1:])+1))
## xmax=max(potentialdata)
## xmax=float(str(math.ceil(xmax*10)/10)[:-1] + str(int(str(math.ceil(xmax*10)/10)[-1:])+1))
## plt.xlim([xmin,xmax])
## ymin=min(currentdata)
## if ymin <0:
## ymin=float(str(math.floor(ymin*10)/10)[:-1] + str(int(str(math.floor(ymin*10)/10)[-1:])-1))
## else:
## ymin=float(str(math.floor(ymin*10)/10)[:-1] + str(int(str(math.floor(ymin*10)/10)[-1:])+1))
## ymax=max(currentdata)
## ymax=float(str(math.ceil(ymax*10)/10)[:-1] + str(int(str(math.ceil(ymax*10)/10)[-1:])+1))
## plt.ylim([ymin, ymax])
#Labels
plt.xlabel('Potential (V) / V',weight='bold')
plt.ylabel('Current (I) / x10$^{-1}$${\mu}A$',weight='bold')
plt.draw()
#Graph Finished Message
sep=" "
name = os.getlogin()
name = name.split(sep, 1)[0]
msg = 'Hey ' + name + ', ' + csvresult[k][:-4] + "'s graph has finished processing."
picname= csvresult[k][:-4] + '.png'
print(msg)
plt.savefig(os.path.join(folder,picname), bbox_inches='tight')
k=k+1
#horiz = horiz + 0.25
#case where if you reach your last CSV, then break the loop
plt.show()
if k == len(csvresult):
plt.title('Cyclic Voltammetry',weight='bold')
plt.legend(loc='best')
break
continue