|
1
|
|
|
"""This module test the file reading functions.""" |
|
2
|
|
|
|
|
3
|
|
|
import copy |
|
4
|
|
|
import pandas as pd |
|
5
|
|
|
import matplotlib.pyplot as plt |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
View Code Duplication |
def read_cycle(data): |
|
|
|
|
|
|
9
|
|
|
"""This function reads a segment of datafile (corresponding a cycle) |
|
10
|
|
|
and generates a dataframe with columns 'Potential' and 'Current' |
|
11
|
|
|
|
|
12
|
|
|
Parameters |
|
13
|
|
|
__________ |
|
14
|
|
|
data: segment of data file |
|
15
|
|
|
Returns |
|
16
|
|
|
_______ |
|
17
|
|
|
A dataframe with potential and current columns |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
current = [] |
|
21
|
|
|
potential = [] |
|
22
|
|
|
for i in data[3:]: |
|
23
|
|
|
current.append(float(i.split("\t")[4])) |
|
24
|
|
|
potential.append(float(i.split("\t")[3])) |
|
25
|
|
|
zipped_list = list(zip(potential, current)) |
|
26
|
|
|
dataframe = pd.DataFrame(zipped_list, columns=['Potential', 'Current']) |
|
27
|
|
|
return dataframe |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
def read_file(file): |
|
|
|
|
|
|
31
|
|
|
"""This function reads the raw data file, gets the scanrate and stepsize |
|
32
|
|
|
and then reads the lines according to cycle number. Once it reads the data |
|
33
|
|
|
for one cycle, it calls read_cycle function to denerate a dataframe. It |
|
34
|
|
|
does the same thing for all the cycles and finally returns a dictionary, |
|
35
|
|
|
the keys of which are the cycle numbers and the values are the |
|
36
|
|
|
corresponding dataframes. |
|
37
|
|
|
|
|
38
|
|
|
Parameters |
|
39
|
|
|
__________ |
|
40
|
|
|
file: raw data file |
|
41
|
|
|
|
|
42
|
|
|
Returns: |
|
43
|
|
|
________ |
|
44
|
|
|
dict_of_df: dictionary of dataframes with keys = cycle numbers and |
|
45
|
|
|
values = dataframes for each cycle |
|
46
|
|
|
n_cycle: number of cycles in the raw file |
|
47
|
|
|
""" |
|
48
|
|
|
dict_of_df = {} |
|
49
|
|
|
h_val = 0 |
|
50
|
|
|
l_val = 0 |
|
51
|
|
|
n_cycle = 0 |
|
52
|
|
|
#a = [] |
|
53
|
|
|
with open(file, 'rt') as f_val: |
|
54
|
|
|
print(file + ' Opened') |
|
55
|
|
|
for line in f_val: |
|
56
|
|
|
record = 0 |
|
57
|
|
|
if not (h_val and l_val): |
|
58
|
|
|
if line.startswith('SCANRATE'): |
|
59
|
|
|
scan_rate = float(line.split()[2]) |
|
60
|
|
|
h_val = 1 |
|
61
|
|
|
if line.startswith('STEPSIZE'): |
|
62
|
|
|
step_size = float(line.split()[2]) |
|
63
|
|
|
l_val = 1 |
|
64
|
|
|
if line.startswith('CURVE'): |
|
65
|
|
|
n_cycle += 1 |
|
66
|
|
|
if n_cycle > 1: |
|
67
|
|
|
number = n_cycle - 1 |
|
68
|
|
|
data = read_cycle(a_val) |
|
|
|
|
|
|
69
|
|
|
key_name = 'cycle_' + str(number) |
|
70
|
|
|
#key_name = number |
|
71
|
|
|
dict_of_df[key_name] = copy.deepcopy(data) |
|
72
|
|
|
a_val = [] |
|
73
|
|
|
if n_cycle: |
|
74
|
|
|
a_val.append(line) |
|
75
|
|
|
return dict_of_df, number |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
#df = pd.DataFrame(list(dict1['df_1'].items())) |
|
79
|
|
|
#list1, list2 = list(dict1['df_1'].items()) |
|
80
|
|
|
#list1, list2 = list(dict1.get('df_'+str(1))) |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
def data_frame(dict_cycle, number): |
|
|
|
|
|
|
83
|
|
|
"""Reads the dictionary of dataframes and returns dataframes for each cycle |
|
84
|
|
|
|
|
85
|
|
|
Parameters |
|
86
|
|
|
__________ |
|
87
|
|
|
dict_cycle: Dictionary of dataframes |
|
88
|
|
|
n: cycle number |
|
89
|
|
|
|
|
90
|
|
|
Returns: |
|
91
|
|
|
_______ |
|
92
|
|
|
Dataframe correcponding to the cycle number |
|
93
|
|
|
""" |
|
94
|
|
|
list1, list2 = (list(dict_cycle.get('cycle_'+str(number)).items())) |
|
95
|
|
|
zipped_list = list(zip(list1[1], list2[1])) |
|
96
|
|
|
data = pd.DataFrame(zipped_list, columns=['Potential', 'Current']) |
|
97
|
|
|
return data |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
def plot_fig(dict_cycle, number): |
|
|
|
|
|
|
100
|
|
|
"""For basic plotting of the cycle data |
|
101
|
|
|
|
|
102
|
|
|
Parameters |
|
103
|
|
|
__________ |
|
104
|
|
|
dict: dictionary of dataframes for all the cycles |
|
105
|
|
|
n: number of cycles |
|
106
|
|
|
|
|
107
|
|
|
Saves the plot in a file called cycle.png |
|
108
|
|
|
""" |
|
109
|
|
|
|
|
110
|
|
|
for i in range(number): |
|
111
|
|
|
print(i+1) |
|
112
|
|
|
data = data_frame(dict_cycle, i+1) |
|
113
|
|
|
plt.plot(data.Potential, data.Current, label="Cycle{}".format(i+1)) |
|
114
|
|
|
|
|
115
|
|
|
print(data.head()) |
|
|
|
|
|
|
116
|
|
|
plt.xlabel('Voltage') |
|
117
|
|
|
plt.ylabel('Current') |
|
118
|
|
|
plt.legend() |
|
119
|
|
|
plt.savefig('cycle.png') |
|
120
|
|
|
print('executed') |
|
121
|
|
|
|