-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathechem.py
346 lines (293 loc) · 12.1 KB
/
echem.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
from galvani import MPRfile
from galvani import res2sqlite as r2s
import pandas as pd
import numpy as np
from scipy.signal import savgol_filter
from scipy.interpolate import splev, splrep
import sqlite3
from bokeh.plotting import figure
import os
import matplotlib.pyplot as plt
# Different cyclers name their columns slightly differently
# These dictionaries are guides for the main things you want to plot
res_col_dict = {'Voltage': 'Voltage',
'Capacity': 'Capacity'}
mpr_col_dict = {'Voltage': 'Ewe/V',
'Capacity': 'Capacity'}
def echem_file_loader(filepath):
extension = os.path.splitext(filepath)[-1].lower()
# Biologic file
if extension == '.mpr':
gal_file = MPRfile(os.path.join(filepath))
df = pd.DataFrame(data=gal_file.data)
df = biologic_processing(df)
#arbin .res file - uses an sql server
elif extension == '.res':
Output_file = 'placeholder_string'
r2s.convert_arbin_to_sqlite(os.path.join(filepath), Output_file)
dat = sqlite3.connect(Output_file)
query = dat.execute("SELECT * From Channel_Normal_Table")
cols = [column[0] for column in query.description]
df = pd.DataFrame.from_records(data = query.fetchall(), columns = cols)
dat.close()
df.set_index('Data_Point', inplace=True)
df.sort_index(inplace=True)
df['Capacity'] = df['Charge_Capacity'] + df['Discharge_Capacity']
elif extension == '.txt':
df = pd.read_csv(os.path.join(filepath), sep='\t')
# Checking columns are ex exact match
if set(['time /s', 'I /mA', 'E /V']) - set(df.columns) == set([]):
df = ivium_processing(df)
else:
print('Columns did not match known .txt column layous')
elif extension in ['.xlsx', '.xls']:
xlsx = pd.ExcelFile(os.path.join(filepath))
names = xlsx.sheet_names
# Edit this in a bit
if len(names) == 1:
df = xlsx.parse(0)
df = new_land_processing(df)
elif "Record" in names[0]:
df_list = [xlsx.parse(0)]
col_names = df_list[0].columns
for sheet_name in names[1:]:
if "Record" in sheet_name:
if len(xlsx.parse(sheet_name, header=None)) != 0:
df_list.append(xlsx.parse(sheet_name, header=None))
for sheet in data_sheets:
sheet.columns = col_names
df = pd.concat(df_list)
df.set_index('Index', inplace=True)
df = old_land_processing(df)
else:
df_list = []
for count, name in enumerate(names):
if 'Channel' in name and 'Chart' not in name:
df_list.append(xlsx.parse(count))
if len(df_list) > 0:
df = pd.concat(df_list)
df = arbin_excel(df)
else:
raise SheetNameError('Names of sheets not recognised')
else:
print(extension)
print('We got to here')
return df
def biologic_processing(df):
# Dealing with the different column layouts for biologic files
# Renames Ewe/V to Voltage and the capacity column to Capacity
if 'half cycle' in df.columns:
if df['half cycle'].min() == 0:
df['half cycle'] = df['half cycle'] + 1
if ('Q charge/discharge/mA.h' in df.columns) and ('half cycle') in df.columns:
df['Capacity'] = abs(df['Q charge/discharge/mA.h'])
df.rename(columns = {'Ewe/V':'Voltage'}, inplace = True)
return df
elif ('dQ/mA.h' in df.columns) and ('half cycle') in df.columns:
df['Half cycle cap'] = abs(df['dQ/mA.h'])
for cycle in df['half cycle'].unique():
mask = df['half cycle'] == cycle
cycle_idx = df.index[mask]
df.loc[cycle_idx, 'Half cycle cap'] = df.loc[cycle_idx, 'Half cycle cap'].cumsum()
df.rename(columns = {'Half cycle cap':'Capacity'}, inplace = True)
df.rename(columns = {'Ewe/V':'Voltage'}, inplace = True)
return df
else:
print('Unknown column layout')
def ivium_processing(df):
df['dq'] = np.diff(df['time /s'], prepend=0)*df['I /mA']
df['Capacity'] = df['dq'].cumsum()/3600
def ivium_state(x):
if x >=0:
return 0
else:
return 1
df['state'] = df['I /mA'].map(lambda x: ivium_state(x))
df['half cycle'] = df['state'].ne(df['state'].shift()).cumsum()
for cycle in df['half cycle'].unique():
mask = df['half cycle'] == cycle
idx = df.index[mask]
df.loc[idx, 'Capacity'] = abs(df.loc[idx, 'dq']).cumsum()/3600
df['Voltage'] = df['E /V']
return df
def new_land_processing(df):
# Remove half cycle == 0 for initial resting
if 'Voltage/V' not in df.columns:
column_to_search = df.columns[df.isin(['Index']).any()][0]
df.columns = df[df[column_to_search] == 'Index'].iloc[0]
df = df[df['Current/mA'].apply(type) != str]
df = df[pd.notna(df['Current/mA'])]
def land_state(x):
if x > 0:
return 0
elif x < 0:
return 1
elif x == 0:
return 'R'
else:
print(x)
raise ValueError('Unexpected value in current - not a number')
df['state'] = df['Current/mA'].map(lambda x: land_state(x))
not_rest_idx = df[df['state'] != 'R'].index
df.loc[not_rest_idx, 'cycle change'] = df.loc[not_rest_idx, 'state'].ne(df.loc[not_rest_idx, 'state'].shift())
df['half cycle'] = (df['cycle change'] == True).cumsum()
df['Voltage'] = df['Voltage/V']
df['Capacity'] = df['Capacity/mAh']
return df
def old_land_processing(df):
df = df[df['Current/mA'].apply(type) != str]
df = df[pd.notna(df['Current/mA'])]
def land_state(x):
if x > 0:
return 0
elif x < 0:
return 1
elif x == 0:
return 'R'
else:
print(x)
raise ValueError('Unexpected value in current - not a number')
df['state'] = df['Current/mA'].map(lambda x: land_state(x))
not_rest_idx = df[df['state'] != 'R'].index
df.loc[not_rest_idx, 'cycle change'] = df.loc[not_rest_idx, 'state'].ne(df.loc[not_rest_idx, 'state'].shift())
df['half cycle'] = (df['cycle change'] == True).cumsum()
df['Voltage'] = df['Voltage/V']
df['Capacity'] = df['Capacity/mAh']
return df
def arbin_excel(df):
df.reset_index(inplace=True)
def arbin_state(x):
if x > 0:
return 0
elif x < 0:
return 1
elif x == 0:
return 'R'
else:
print(x)
raise ValueError('Unexpected value in current - not a number')
df['state'] = df['Current(A)'].map(lambda x: arbin_state(x))
not_rest_idx = df[df['state'] != 'R'].index
df.loc[not_rest_idx, 'cycle change'] = df.loc[not_rest_idx, 'state'].ne(df.loc[not_rest_idx, 'state'].shift())
df['half cycle'] = (df['cycle change'] == True).cumsum()
df['Capacity'] = df['Discharge_Capacity(Ah)'] + df['Charge_Capacity(Ah)']
for cycle in df['half cycle'].unique():
idx = df[df['half cycle'] == cycle].index
df.loc[idx, 'Capacity'] = df.loc[idx, 'Capacity'] - min(df.loc[idx, 'Capacity'])
df['Voltage'] = df['Voltage(V)']
return df
def dqdv_single_cycle(capacity, voltage,
polynomial_spline=3, s_spline=1e-5,
polyorder_1 = 5, window_size_1=101,
polyorder_2 = 5, window_size_2=1001,
final_smooth=True):
import pandas as pd
import numpy as np
from scipy.interpolate import splrep, splev
df = pd.DataFrame({'Capacity': capacity, 'Voltage':voltage})
unique_v = df.astype(float).groupby('Voltage').mean().index
unique_v_cap = df.astype(float).groupby('Voltage').mean()['Capacity']
x_volt = np.linspace(min(voltage), max(voltage), num=int(1e4))
f_lit = splrep(unique_v, unique_v_cap, k=1, s=0.0)
y_cap = splev(x_volt, f_lit)
smooth_cap = savgol_filter(y_cap, window_size_1, polyorder_1)
f_smooth = splrep(x_volt, smooth_cap, k=polynomial_spline, s=s_spline)
dqdv = splev(x_volt, f_smooth, der=1)
smooth_dqdv = savgol_filter(dqdv, window_size_2, polyorder_2)
if final_smooth:
return x_volt, smooth_dqdv, smooth_cap
else:
return x_volt, dqdv, smooth_cap
"""
PLOTTING
"""
def charge_discharge_plot(df, full_cycle, colormap=None):
"""
Function for plotting individual or multi but discrete charge discharge cycles
"""
fig, ax = plt.subplots()
try:
iter(full_cycle)
except TypeError:
cycles = [full_cycle*2 -1, full_cycle*2]
for cycle in cycles:
mask = df['half cycle'] == cycle
# Making sure cycle exists within the data
if sum(mask) > 0:
ax.plot(df['Capacity'][mask], df['Voltage'][mask])
ax.set_xlabel('Capacity / mAh')
ax.set_ylabel('Voltage / V')
return fig, ax
if not colormap:
if len(full_cycle) < 11:
colormap = 'tab10'
elif len(full_cycle) < 21:
colormap = 'tab20'
else:
raise ValueError("Too many cycles for default colormaps. Use multi_cycle_plot instead")
cm = plt.get_cmap(colormap)
for count, full_cycle_number in enumerate(full_cycle):
cycles = [full_cycle_number*2 -1, full_cycle_number*2]
for cycle in cycles:
mask = df['half cycle'] == cycle
# Making sure cycle exists within the data
if sum(mask) > 0:
ax.plot(df['Capacity'][mask], df['Voltage'][mask], color=cm(count))
from matplotlib.lines import Line2D
custom_lines = [Line2D([0], [0], color=cm(count), lw=2) for count, i in enumerate(full_cycle)]
ax.legend(custom_lines, [f'Cycle {i}' for i in full_cycle])
ax.set_xlabel('Capacity / mAh')
ax.set_ylabel('Voltage / V')
return fig, ax
def multi_cycle_plot(df, cycles, colormap='viridis'):
"""
Function for plotting continuously coloured cycles (useful for large numbers)
Supply the cycles as half cycle numbers e.g 1, 2 are discharge and charge for
first cycle
"""
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig, ax = plt.subplots()
cm = plt.get_cmap(colormap)
norm = Normalize(vmin=int(np.ceil(min(cycles)/2)), vmax=int(np.ceil(max(cycles)/2)))
sm = plt.cm.ScalarMappable(cmap=cm, norm=norm)
for cycle in cycles:
mask = df['half cycle'] == cycle
ax.plot(df['Capacity'][mask], df['Voltage'][mask], color=cm(norm(np.ceil(cycle/2))))
cbar = fig.colorbar(sm)
cbar.set_label('Cycle', rotation=270, labelpad=10)
ax.set_ylabel('Voltage / V')
ax.set_xlabel('Capacity / mAh')
return fig, ax
def multi_dqdv_plot(df, cycles, colormap='viridis',
capacity_label='Capacity',
voltage_label='Voltage',
polynomial_spline=3, s_spline=1e-5,
polyorder_1 = 5, window_size_1=101,
polyorder_2 = 5, window_size_2=1001,
final_smooth=True):
"""
Plotting multi dQ/dV cyles on the same plot with a colormap.
"""
import matplotlib.cm as cm
from matplotlib.colors import Normalize
fig, ax = plt.subplots()
cm = plt.get_cmap(colormap)
norm = Normalize(vmin=int(np.ceil(min(cycles)/2)), vmax=int(np.ceil(max(cycles)/2)))
sm = plt.cm.ScalarMappable(cmap=cm, norm=norm)
for cycle in cycles:
df_cycle = df[df['half cycle'] == cycle]
voltage, dqdv, cap = dqdv_single_cycle(df_cycle[capacity_label],
df_cycle[voltage_label],
window_size_1=window_size_1,
polyorder_1=polyorder_1,
s_spline=s_spline,
window_size_2=window_size_2,
polyorder_2=polyorder_2,
final_smooth=final_smooth)
ax.plot(voltage, dqdv, color=cm(norm(np.ceil(cycle/2))))
cbar = fig.colorbar(sm)
cbar.set_label('Cycle', rotation=270, labelpad=10)
ax.set_xlabel('Voltage / V')
ax.set_ylabel('dQ/dV / $mAhV^{-1}$')
return fig, ax