/
dataread.py
547 lines (473 loc) · 15.5 KB
/
dataread.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python
# coding: utf-8
# Script to open and graph data from electrochemical
# based on PyEIS from Kristian B. Knudsen
# (kknu@berkeley.edu || kristianbknudsen@gmail.com)
import numpy as np
import pandas as pd
from scipy.signal import lfilter, lfilter_zi, filtfilt, butter
import matplotlib.pyplot as plt
plt.rc("text", usetex=True)
plt.rc("xtick", labelsize=15)
plt.rc("ytick", labelsize=15)
def correct_text_exp(text_header):
"""Corrects the text of '*.csv' and '*.txt'
files into readable parameters without
spaces, ., or /
"""
if text_header == "freq / Hz" or text_header == "Freq(Hz)":
return "f"
elif text_header == "neg. Phase / °" or text_header == "Phase(deg)":
return "Z_phase"
elif text_header == "Idc / uA":
return "I_avg"
elif text_header == "Z / Ohm" or text_header == "Module (Ohms)":
return "Z_mag"
elif text_header == "Z' / Ohm" or text_header == "ZR(Ohms)":
return "re"
elif text_header == "Z'' / Ohm" or text_header == "Zi(Ohms)":
return "im"
elif text_header == "Cs / F":
return "Cs"
if text_header == "V" or text_header == "WE1 Volts(mV)":
return "volt"
elif text_header == "µA" or text_header == "mA" or text_header == "A":
return "current"
elif text_header == "WE1 Idir(mA)" or text_header == "WE1 Idir(uA)":
return "current"
elif text_header == "WE1 Idir(A)":
return "current"
elif text_header == "s" or text_header == "Time(sec)":
return "time"
elif text_header == "T(sec)":
return "time"
else:
return text_header
"""
Reading functions
Inputs
-----------
- path: path of datafile(s) as a string
- file_name: datafile(s) including extension,
e.g. ['EIS_data1', 'EIS_data2']
"""
def Teq4CV_expread(path, file_name):
"""Function that return dataframe from *.txt
Cyclic Voltammetry data
from Teq4 potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 3),
skipfooter=1,
engine="python",
encoding="utf-8",
)
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(correct_text_exp(exp_test_header_names.columns[j]))
return pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 3),
skiprows=int(1),
names=names_exp,
skipfooter=1,
engine="python",
encoding="utf-8",
)
def Teq4Amp_expread(path, file_name):
"""Function that return dataframe from *.txt
amperometrics data
from Teq4 potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 3),
skipfooter=1,
engine="python",
encoding="utf-8",
)
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(correct_text_exp(exp_test_header_names.columns[j]))
return pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 3),
skiprows=int(1),
names=names_exp,
skipfooter=1,
engine="python",
encoding="utf-8",
)
def Teq4EIS_expread(path, file_name):
"""Function that return dataframe from *.txt
impedance data from Teq4
potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 6),
skipfooter=1,
engine="python",
encoding="utf-8",
)
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(correct_text_exp(exp_test_header_names.columns[j]))
return pd.read_csv(
path + file_name,
sep="\t",
usecols=range(0, 6),
skiprows=int(1),
names=names_exp,
skipfooter=1,
engine="python",
encoding="utf-8",
)
def PS4EIS_expread(path, file_name):
"""Function that return a dataframe from cvs impedance data
from PalmSens4 potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name,
usecols=range(0, 7),
skiprows=int(5),
skipfooter=2,
engine="python",
encoding="utf_16_le",
)
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(correct_text_exp(exp_test_header_names.columns[j]))
return pd.read_csv(
path + file_name,
sep=",",
usecols=range(0, 7),
skiprows=int(6),
names=names_exp,
skipfooter=2,
engine="python",
encoding="utf_16_le",
)
def PS4CV_expread(path, file_name):
"""Function that return a dataframe from cvs cyclic
voltammetry data
from PalmSens4 potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name, sep=",", skiprows=int(5), encoding="utf_16_le"
) # locates number of skiplines
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(
correct_text_exp(exp_test_header_names.columns[j])
) # reads column text
return pd.read_csv(
path + file_name,
sep=",",
skiprows=int(6),
names=names_exp,
skipfooter=2,
engine="python",
encoding="utf_16_le",
)
def PS4Amp_expread(path, file_name):
"""Function that return dataframe from *.txt
amperometrics data
from PalmSens4 potentiostat/galvanostat"""
exp_test_header_names = pd.read_csv(
path + file_name, sep=",", skiprows=int(5), encoding="utf_16_le"
) # locates number of skiplines
names_exp = []
for j in range(len(exp_test_header_names.columns)):
names_exp.append(
correct_text_exp(exp_test_header_names.columns[j])
) # reads column text
return pd.read_csv(
path + file_name,
sep=",",
skiprows=int(6),
names=names_exp,
skipfooter=2,
engine="python",
encoding="utf_16_le",
)
"""
Plotting functions
Inputs
-----------
- df: dataframe(s) including extension
- g_title: Name of the graph. For latex code use r'$\bf My\,Title$'
- g_xlim/g_xlim: Change the x/y-axis limits on plot, if equal to 'none' state auto value.
- g_xlabel/g_ylabel: Name of the x/y-axis. For latex code use r'$\bf my\,units (unit)$'
- savefig: if not equal to 'none', save the figure in '.eps' format. Otherwise, just show the graphic on the screen.
"""
def CV_plot(
df,
g_title="none",
g_xlabel="none",
g_ylabel="none",
g_xlim="none",
g_ylim="none",
savefig="none",
):
"""Return a cyclic voltamperogram
from a dataframe containing the
'volt' and 'current' variables"""
plt.figure(dpi=120)
plt.plot(df["volt"].values, df["current"].values)
if g_title == "none":
plt.title(r"$\bf Cyclic\; voltamperogram$", fontsize=20)
elif g_title != "none":
plt.title(g_title, fontsize=20)
if g_xlabel == "none":
plt.xlabel(r"$\bf Volt$", fontsize=18)
elif g_xlabel != "none":
plt.xlabel(g_xlabel, fontsize=18)
if g_ylabel == "none":
plt.ylabel(r"$\bf Current$", fontsize=18)
elif g_ylabel != "none":
plt.ylabel(g_ylabel, fontsize=18)
plt.rc("xtick", labelsize=15)
plt.rc("ytick", labelsize=15)
if g_xlim != "none":
plt.xlim(g_xlim[0], g_xlim[1])
if g_ylim != "none":
plt.ylim(g_ylim[0], g_ylim[1])
if savefig != "none":
plt.savefig(savefig, format="eps")
plt.show()
def amp_plot(
df,
g_title="none",
g_xlabel="none",
g_ylabel="none",
g_xlim="none",
g_ylim="none",
savefig="none",
):
"""Take a pandas dataframe from a *.txt
of Teq4 and plot an amperometrics register"""
plt.figure(dpi=120)
plt.plot(df["time"].values, df["current"].values)
if g_title == "none":
plt.title(r"\bf Amperometric Plot", fontsize=20)
elif g_title != "none":
plt.title(g_title, fontsize=20)
if g_xlabel == "none":
plt.xlabel(r"$\bf Time$", fontsize=18)
elif g_xlabel != "none":
plt.xlabel(g_xlabel, fontsize=18)
if g_ylabel == "none":
plt.ylabel(r"$\bf Current$", fontsize=18)
elif g_ylabel != "none":
plt.ylabel(g_ylabel, fontsize=18)
if g_xlim != "none":
plt.xlim(g_xlim[0], g_xlim[1])
if g_ylim != "none":
plt.ylim(g_ylim[0], g_ylim[1])
if savefig != "none":
plt.savefig(savefig, format="eps")
plt.show()
def Nyq_plot(df, g_title="none", g_xlim="none", g_ylim="none", savefig="none"):
"""Take a pandas dataframe from a *.txt
of Teq4 and make a Nyquist plot"""
plt.figure(dpi=120)
plt.scatter(df["re"].values, df["im"].values)
plt.xlabel(r"$\bf Z_{real}\; (\Omega)$", fontsize=20)
plt.ylabel(r"$\bf Z_{im}\; (\Omega)$", fontsize=20)
if g_title == "none":
plt.title(r"\bf Nyquist Plot", fontsize=20)
elif g_title != "none":
plt.title(g_title, fontsize=20)
if g_xlim != "none":
plt.xlim(g_xlim[0], g_xlim[1])
if g_ylim != "none":
plt.ylim(g_ylim[0], g_ylim[1])
if savefig != "none":
plt.savefig(savefig, format="eps")
plt.show()
def Bode_plot(df, g_title="none", g_xlim="none", g_ylim="none", savefig="none"):
"""Take a pandas dataframe from a *.txt
of Teq4 and make a Bode graph
log(f) vs Z_im"""
plt.figure(dpi=120)
plt.scatter(np.log10(df["f"].values), df["im"].values)
plt.xlabel(r"$\bf \log(f)\; (\mathrm{Hz})$", fontsize=20)
plt.ylabel(r"$\bf Z_{im}\; (\Omega)$", fontsize=20)
if g_title == "none":
plt.title(r"\bf Bode Plot", fontsize=20)
elif g_title != "none":
plt.title(g_title, fontsize=20)
if g_xlim != "none":
plt.xlim(g_xlim[0], g_xlim[1])
if g_ylim != "none":
plt.ylim(g_ylim[0], g_ylim[1])
if savefig != "none":
plt.savefig(savefig, format="eps")
plt.show()
def bookBode_plot(df, g_title="none", g_xlim="none", g_ylim="none", savefig="none"):
"""Take a pandas dataframe from a *.txt
of Teq4 and make a Bode graph
according to book style
Zmodule vs log(f)"""
fig = plt.figure(dpi=120)
ax1 = fig.add_subplot(111)
ax1.plot(np.log10(df["f"].values), df["Z_mag"].values, "bo")
ax1.set_ylabel(r"$\bf \mid Z \mid\; [\Omega]$", color="b", fontsize=20)
ax1.set_xlabel(r"$\bf \log(f)\; (\mathrm{Hz})$", fontsize=20)
ax2 = ax1.twinx()
ax2.plot(np.log10(df["f"].values), df["Z_phase"].values, "ro")
ax2.set_ylabel(r"$\bf \varphi\; [^{\circ}]$", color="r", fontsize=20)
for tl in ax2.get_yticklabels():
tl.set_color("r")
if g_title == "none":
plt.title(r"\bf Bode Plot", fontsize=20)
elif g_title != "none":
plt.title(g_title, fontsize=20)
if g_xlim != "none":
plt.xlim(g_xlim[0], g_xlim[1])
if g_ylim != "none":
plt.ylim(g_ylim[0], g_ylim[1])
if savefig != "none":
plt.savefig(savefig, format="eps")
plt.show()
def homemadeCV_expread(path, file_name):
"""Function that return dataframe from *.txt
Cyclic Voltammetry data
from our homemade potentiostat"""
read_one = pd.read_csv(
path + file_name,
sep="\s+",
header=None,
names=["volt", "current"],
engine="python",
)
# Now, the error in measuring the potential difference
# that the instrument has is corrected.
d = {"volt": -read_one["volt"].values, "current": read_one["current"].values}
return pd.DataFrame(data=d)
def FilterVC(df, b="none", a="none"):
"""Function that filters the data corresponding
to cyclic voltammetry experiments.
Return a dataframe with the values filtered in
columns called volt and current."""
if b == "none":
f = 4 # order of the filter
elif b != "none":
f = b
if a == "none":
g = 0.05 # The denominator coefficient
# vector of the filter.
elif a != "none":
g = a
b, a = butter(f, g)
y = df["current"].values
zi = lfilter_zi(b, a)
z, _ = lfilter(b, a, y, zi=zi * y[0])
mid = len(df["volt"].values) // 2
# Apply the filter again, to have a result filtered
# at an order the same as filtfilt.
z2, _ = lfilter(b, a, z, zi=zi * z[0])
# Use filtfilt to apply the filter.
x_f1 = filtfilt(b, a, df["volt"].values[0:mid])
y_f1 = filtfilt(b, a, df["current"].values[0:mid])
x_f2 = filtfilt(b, a, df["volt"].values[mid:])
y_f2 = filtfilt(b, a, df["current"].values[mid:])
xfiltered = np.concatenate([x_f1, x_f2])
yfiltered = np.concatenate([y_f1, y_f2])
d = {"volt": xfiltered, "current": yfiltered}
return pd.DataFrame(data=d)
def homemadeAMP_expread(path, file_name):
return pd.read_csv(
path + file_name,
sep="\s+",
header=None,
names=["time", "current"],
engine="python",
)
def FilterAMP(df, b="none", a="none"):
"""Function that filters the data corresponding
to amperometric experiments.
Return a dataframe with the values filtered in
columns called time and current."""
if b == "none":
f = 4 # order of the filter
elif b != "none":
f = b
if a == "none":
g = 0.05 # The denominator coefficient
# vector of the filter.
elif a != "none":
g = a
b, a = butter(f, g)
y = df["current"].values
zi = lfilter_zi(b, a)
z, _ = lfilter(b, a, y, zi=zi * y[0])
mid = len(df["current"].values) // 2
# Apply the filter again, to have a result filtered
# at an order the same as filtfilt.
z2, _ = lfilter(b, a, z, zi=zi * z[0])
# Use filtfilt to apply the filter.
xfiltered = filtfilt(b, a, df["time"].values)
yfiltered = filtfilt(b, a, df["current"].values)
d = {"time": xfiltered, "current": yfiltered}
return pd.DataFrame(data=d)
def FiltKalmanVC(df):
V = df["volt"].values
I = df["current"].values
fls = FixedLagSmoother(dim_x=2, dim_z=1, N=8)
fls.x = np.array([0.0, 0.5])
fls.F = np.array([[1.0, 1.0], [0.0, 1.0]])
fls.H = np.array([[1.0, 0.0]])
fls.P *= 1 # state matrix
fls.R *= 5.0 #
fls.Q *= 0.00001 # noise matrix
kf = KalmanFilter(dim_x=2, dim_z=1)
kf.x = np.array([0.0, 0.05])
kf.F = np.array([[1.0, 1.0], [0.0, 1.0]])
kf.H = np.array([[1.0, 0.0]])
kf.P *= 1
kf.R *= 5.0
kf.Q *= 0.00001
zs1 = V
for z in zs1:
fls.smooth(z)
kf_x1, _, _, _ = kf.batch_filter(zs1)
x1_smooth = np.array(fls.xSmooth)[:, 0]
zs2 = I
for z in zs2:
fls.smooth(z)
kf_x2, _, _, _ = kf.batch_filter(zs2)
x2_smooth = np.array(fls.xSmooth)[:, 0]
d = {"volt": kf_x1[:, 0], "current": kf_x2[:, 0]}
return pd.DataFrame(data=d)
def FiltKalmanAMP(df):
t = df["time"].values
I = df["current"].values
fls = FixedLagSmoother(dim_x=2, dim_z=1, N=8)
fls.x = np.array([0.0, 0.5])
fls.F = np.array([[1.0, 1.0], [0.0, 1.0]])
fls.H = np.array([[1.0, 0.0]])
fls.P *= 1 # state matrix
fls.R *= 5.0 #
fls.Q *= 0.00001 # noise matrix
kf = KalmanFilter(dim_x=2, dim_z=1)
kf.x = np.array([0.0, 0.05])
kf.F = np.array([[1.0, 1.0], [0.0, 1.0]])
kf.H = np.array([[1.0, 0.0]])
kf.P *= 1
kf.R *= 5.0
kf.Q *= 0.00001
zs1 = t
for z in zs1:
fls.smooth(z)
kf_x1, _, _, _ = kf.batch_filter(zs1)
x1_smooth = np.array(fls.xSmooth)[:, 0]
zs2 = I
for z in zs2:
fls.smooth(z)
kf_x2, _, _, _ = kf.batch_filter(zs2)
x2_smooth = np.array(fls.xSmooth)[:, 0]
d = {"time": kf_x1[:, 0], "current": kf_x2[:, 0]}
return pd.DataFrame(data=d)