-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSKID_GUI.py
590 lines (527 loc) · 36.8 KB
/
SKID_GUI.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 24 18:35:01 2023
@author: Andrey.Bezrukov
"""
developer_contact = 'Andrey.Bezrukov@ul.ie'
software_version = '1.1.0'
print('################################################################################################')
print(' Sorption kinetics isotherm determination, SKID, V{0}'.format(software_version))
print(' Author: Andrey A. Bezrukov')
print('################################################################################################')
import time
from datetime import datetime
import os
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy import interpolate
from sklearn.cluster import DBSCAN
import numpy as np
import PySimpleGUI as sg
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
'''
with warnings.catch_warnings():
warnings.simplefilter("ignore")
'''
plt.rcParams['toolbar'] = 'toolmanager'
from matplotlib.backend_tools import ToolBase, ToolToggleBase
import matplotlib.patches as mpatches
### declare custom exceptions
class CannotReadFile(Exception):
pass
class WrongFileFormat(Exception):
pass
dmdt_warning_threshold = 0.05
##################################################
####### define functions
##################################################
class file_Sorption:
def read_file(self, path, filename):
#print(path +'/'+ filename)
### read MS Excel file format
if (filename[-5:]=='.xlsx')|(filename[-4:]=='.xls'):
try:
df = pd.read_excel(path + filename, sheet_name='DVS Data')
if df.columns[0] == 'DVS-INTRINSIC DATA FILE':
self.filename = filename
self.instrument = df.columns[0].split()[0]
self.sample_mass = df.iloc[4, 7]
self.fluid = 'water vapor'
comments = str(df.iloc[3, 1])
method = str(df.iloc[1, 1])
df.columns = df.iloc[22, :]
df = df.iloc[23:, :]
df = df.reset_index(drop=True)
df = df.astype('float')
# renaming columns to common notation
df = df[['Time (min)', 'dm (%) - ref', 'Target RH (%)', 'Actual RH (%)', 'Target Sample Temp', 'Actual Sample Temp', 'dm/dt']]
df.columns = ['time', 'uptake', 'RH_target', 'RH_actual', 'temp_target', 'temp_actual', 'dmdt']
self.temperature = 'Actual Sample Temp: {0:.2f} +- {1:.2f}'.format(df[df.temp_target==df.temp_target.min()].temp_actual.mean(), df[df.temp_target==df.temp_target.min()].temp_actual.std())
self.equilibration_interval = '---'
self.comments = comments
self.method = method
self.data = df
#print(self.data.head())
elif df.columns[0] == 'DVS-Advantage-Plus-Data-File':
self.filename = filename
self.instrument = df.columns[0][:-10]
self.sample_mass = df.iloc[27, 1]
self.fluid = 'water vapor'
comments = str(df.iloc[8, 1])
method = str(df.iloc[3, 1])
df.columns = df.iloc[39, :]
df = df.iloc[40:, :30]
df = df.reset_index(drop=True)
df = df[[i for i in df.columns if (i == i) & (i != 'Chiller State')]]
df = df.astype('float')
# renaming columns to common notation
df = df[['Time [minutes]', 'dm (%) - ref', 'Mass [mg]', 'Target Partial Pressure (Solvent A) [%]', 'Measured Partial Pressure (Solvent A) [%]', 'Target Incubator Temp. [celsius]', 'Measured Preheater Temp. [celsius]', 'dm/dt [%/minute]']]
df.columns = ['time', 'uptake', 'mass', 'RH_target', 'RH_actual', 'temp_target', 'temp_actual', 'dmdt']
self.temperature = 'Temp. [celsius]: {0:.2f} +- {1:.2f}'.format(df[df.temp_target==df.temp_target.min()].temp_actual.mean(), df[df.temp_target==df.temp_target.min()].temp_actual.std())
self.equilibration_interval = '---'
self.comments = comments
self.method = method
self.data = df
#print(self.data.head())
except Exception as e:
print(datetime.now())
print(e)
#print(self.__dict__.keys())
# test if import was correct
if (list(self.__dict__.keys()) == ['filename', 'instrument', 'sample_mass', 'fluid', 'temperature', 'equilibration_interval', 'comments', 'method', 'data']) :
print(datetime.now())
print('File {0} read succesfull'.format(filename))
self.import_success = True
else:
self.import_success = False
### read generic csv file format
elif (filename[-4:]=='.csv'):
try:
df = pd.read_csv(path + filename)
self.filename = filename
self.fluid = 'water vapor'
df = df.astype('float')
# renaming columns to common notation
df = df[['Time[min]','Mass[mg]','RH_target[%]','RH_measured[%]','Temperature[degreeC]','Temperature[degreeC]',]]
df.columns = ['time', 'mass', 'RH_target', 'RH_actual', 'temp_target', 'temp_actual']
self.temperature = 'Temp. [celsius]: {0:.2f} +- {1:.2f}'.format(df[df.temp_target==df.temp_target.min()].temp_actual.mean(), df[df.temp_target==df.temp_target.min()].temp_actual.std())
# zero mass at the start of first adsorption cycle
df['cycle_split'] = df['RH_target'].diff().fillna(0)
m0_index = df.index[(df['cycle_split']>0)].to_list()[0]
self.sample_mass = df.iloc[m0_index, :]['mass']
# calc uptake as wt. %
df['uptake'] = df['mass']/df.iloc[m0_index, :]['mass']*100-100
# calc dm/dt [%/minute]
df['dmdt'] = (df['mass'].pct_change()/df['time'].diff()).rolling(window=10, min_periods=1, center=True).mean()*100
self.data = df[['time','uptake', 'mass', 'RH_target', 'RH_actual', 'temp_target', 'temp_actual', 'dmdt']]
#print(self.data.head())
except Exception as e:
print(datetime.now())
print(e)
#print(self.__dict__.keys())
# test if import was correct
if (list(self.__dict__.keys()) == ['filename', 'fluid', 'temperature','sample_mass', 'data']) :
print(datetime.now())
print('File {0} read succesfull'.format(filename))
self.import_success = True
else:
self.import_success = False
##################################################
####### run analysis
##################################################
################### Start GUI ############################
sg.change_look_and_feel('DefaultNoMoreNagging')
layout1 = [
[sg.Text('Figure settings: font size', size=(19, 1)), sg.In(default_text='16', size=(5, 1), key= 'text_size'), sg.Text(' dpi', size=(5, 1)), sg.In(default_text='100', size=(5, 1), key= 'dpi'),],
[sg.Text('STEP 1: read kinetics data', auto_size_text=False, justification='left', font=("Helvetica", 12, "bold"))],
[sg.Text('Read kinetics data for isotherm determination:', auto_size_text=False, justification='left')],
[sg.InputText('Choose file', key='kinetics_file'), sg.FilesBrowse(),sg.Help('Help')],
[sg.Button('Open')],
[sg.Text('STEP 2: determine isotherm', auto_size_text=False, justification='left', font=("Helvetica", 12, "bold"))],
[sg.Text('Select calculation parameters:', auto_size_text=False, justification='left')],
[sg.Text(' cycle number', size=(32, 1)), sg.In(default_text='1', size=(5, 1), key= 'cycle_number')],
[sg.Text(' first derivative moving average window', size=(32, 1)), sg.In(default_text='50', size=(5, 1), key= 'window1')],
[sg.Text(' second derivative moving average window', size=(32, 1)), sg.In(default_text='10', size=(5, 1), key= 'window2')],
[sg.Text(' number of iterations for k determination', size=(32, 1)), sg.In(default_text='10', size=(5, 1), key= 'k_iterations')],
[sg.Button('Calculate isotherm') ],
[sg.Text('STEP 3: save result', auto_size_text=False, justification='left', font=("Helvetica", 12, "bold"))],
[sg.Text('Save calculated isotherm:', auto_size_text=False, justification='left')],
[sg.Text(' File name:', size=(12, 1)), sg.In(default_text='Isotherm', size=(27, 1), key= 'isotherm_file')],
[sg.Text(' Save as type:', size=(12, 1)), sg.Combo(['CSV (Comma delimited) (*.csv)', 'Adsorption Isotherm File (*.AIF)'],default_value='CSV (Comma delimited) (*.csv)', size=(27, 1), key= 'isotherm_file_type')],
[sg.Button('Save')],
]
kinetics_uploaded = False
isotherm_calculated = False
window = sg.Window('Sorption kinetics isotherm determination, SKID, V'+software_version, layout1, default_element_size=(40, 1), grab_anywhere=False)
while True:
event, values = window.read()
################# window closed ################
if event == sg.WIN_CLOSED:
break
########### test input values ############
input_values_valid = True
def value_test_int(v, message):
try:
if int(v)<=0:
print(datetime.now())
print('Error:', message)
sg.popup('Error', message)
return False
else: return True
except:
print(datetime.now())
print('Error:', message)
sg.popup('Error', message)
return False
def input_values_test():
return all([value_test_int(values['cycle_number'], '\'cycle number\' parameter MUST be positive nonzero integer'),
value_test_int(values['window1'], '\'first derivative moving average window\' parameter MUST be positive nonzero integer'),
value_test_int(values['window2'], '\'second derivative moving average window\' parameter MUST be positive nonzero integer'),
value_test_int(values['k_iterations'], '\'number of iterations for k determination\' parameter MUST be positive nonzero integer'),
value_test_int(values['text_size'], 'Figure \'font size\' parameter MUST be positive nonzero integer'),
value_test_int(values['dpi'], 'Figure \'dpi\' parameter MUST be positive nonzero integer'),
])
########### help button pressed ############
if event == 'Help':
print(datetime.now())
print('Help:\nThe method requires humidity swing kinetics data.\nAt the moment there are 2 supported options for kinetics data:\nOption 1: kinetics data collected using Adventure DVS and Intrinsic DVS instruments (Surface Measurement Systems).\nFiles must be uploaded in MS Excel file format, only \'DVS Data\' tab is required.\nOption 2: kinetics data in generic \'.csv\' file format.\nFiles must contain following columns:\nTime[min],Mass[mg],Temperature[degreeC],RH_target[%],RH_measured[%]')
sg.popup('The method requires humidity swing kinetics data.\nAt the moment there are 2 supported options for kinetics data:\nOption 1: kinetics data collected using Adventure DVS and Intrinsic DVS instruments (Surface Measurement Systems).\nFiles must be uploaded in MS Excel file format, only \'DVS Data\' tab is required.\nOption 2: kinetics data in generic \'.csv\' file format.\nFiles must contain following columns:\nTime[min],Mass[mg],Temperature[degreeC],RH_target[%],RH_measured[%]')
continue
################# Import kinetics data ################
if event == 'Open':
if not input_values_test():
continue
if values['kinetics_file']=='Choose file':
print('Choose file')
sg.popup('Choose file')
continue
path = values['kinetics_file'][:-len(values['kinetics_file'].split('/')[-1])]
filename = values['kinetics_file'].split('/')[-1]
print(datetime.now())
print('Reading file:', filename)
## import kinetics
Sorption_kinetics = file_Sorption()
try:
Sorption_kinetics.read_file(path, filename)
if Sorption_kinetics.import_success:
if Sorption_kinetics.fluid != 'water vapor':
del Sorption_kinetics
print('Wrong fluid: ',Sorption_kinetics.fluid)
else:
del Sorption_kinetics
print(datetime.now())
print(filename, 'failed')
#continue
except Exception as e:
print(datetime.now())
print(e)
print(filename, 'failed')
#continue
kinetics_uploaded = True
## break down kinetics to individual adsorption-desorption cycles
Sorption_kinetics.data['cycle_split'] = Sorption_kinetics.data['RH_target'].diff().fillna(0)
split_index_ads = Sorption_kinetics.data.index[(Sorption_kinetics.data['cycle_split']>0)].to_list()
split_index_des = Sorption_kinetics.data.index[(Sorption_kinetics.data['cycle_split']<0)].to_list()
## plot kinetics
fig, ax = plt.subplots(figsize=(12, 9), dpi=int(values['dpi']))
fig.subplots_adjust(right=0.75)
ax.plot(Sorption_kinetics.data['time'], Sorption_kinetics.data['uptake'], c='g')
ax2 = ax.twinx()
ax2.plot(Sorption_kinetics.data['time'], Sorption_kinetics.data['RH_actual'], c='b')
#ax2.plot(Sorption_kinetics.data['time'], Sorption_kinetics.data['temp_actual'], c='r')
ax.set_xlabel('Time, min', fontsize=values['text_size'])
ax.set_ylabel('Uptake, wt.%', fontsize=values['text_size'], c='g')
ax.tick_params(axis='x', labelsize=values['text_size'])
ax.tick_params(axis='y', labelsize=values['text_size'])
ax2.set_ylabel('RH actual, %', fontsize=values['text_size'], c='b')
ax2.tick_params(axis='x', labelsize=values['text_size'])
ax2.tick_params(axis='y', labelsize=values['text_size'])
fig.suptitle(''.join([filename, ', ', str(Sorption_kinetics.sample_mass), ' mg sample' ]),
fontsize=values['text_size'])
for cycle_number in range(len(split_index_ads)):
try:
left, right, bottom, top = (Sorption_kinetics.data.iloc[split_index_ads[cycle_number], :]['time'],
Sorption_kinetics.data.iloc[split_index_ads[cycle_number+1], :]['time'],
Sorption_kinetics.data.uptake.min(),
Sorption_kinetics.data.uptake.max(),
)
except:
left, right, bottom, top = (Sorption_kinetics.data.iloc[split_index_ads[cycle_number], :]['time'],
Sorption_kinetics.data.iloc[Sorption_kinetics.data.index.max(), :]['time'],
Sorption_kinetics.data.uptake.min(),
Sorption_kinetics.data.uptake.max(),
)
ax.text((left+right)/2,
(top+bottom)/2,
'Cycle number {}'.format(str(cycle_number+1)),
size=values['text_size'], rotation=90, ha="center", va="center", weight='bold')
rect=mpatches.Rectangle((left,bottom),right-left,top-bottom,
alpha=0.1,
facecolor=['red', 'green', 'blue'][cycle_number%3])
ax.add_patch(rect)
ax3 = ax.twinx()
ax3.spines.right.set_position(("axes", 1.12))
ax3.plot(Sorption_kinetics.data['time'], Sorption_kinetics.data['temp_actual'], c='r')
ax3.set_ylabel('Temperature, °C', fontsize=values['text_size'], c='r')
ax3.tick_params(axis='y', labelsize=values['text_size'])
plt.show(block=False)
################# Calculate isotherm ################
if (event == 'Calculate isotherm')&(kinetics_uploaded!=True):
print(datetime.now())
print('Error, open kinetics (step 1) before calculating isotherm')
sg.popup('Error, open kinetics (step 1) before calculating isotherm')
if (event == 'Calculate isotherm')&(kinetics_uploaded==True):
if not input_values_test():
continue
window1 = int(values['window1'])
window2 = int(values['window2'])
path = values['kinetics_file'][:-len(values['kinetics_file'].split('/')[-1])]
filename = values['kinetics_file'].split('/')[-1]
cycle_number = int(values['cycle_number'])-1
if ((cycle_number+1)>len(split_index_ads))|((cycle_number+1)>len(split_index_des)):
print(datetime.now())
print('Error, \'cycle number\' parameter exceeds max: {0}'.format(min(len(split_index_ads), len(split_index_des) )))
sg.popup('Error, \'cycle number\' parameter exceeds max: {0}'.format(min(len(split_index_ads), len(split_index_des) )))
continue
print(datetime.now())
print('Isotherm calculation parameters:\ncycle number={0}\nfirst derivative moving average window={1}\nsecond derivative moving average window={2}\nnumber of iterations for k determination={3}'.format(cycle_number+1,
window1,
window2,
values['k_iterations'],
))
fig, ax = plt.subplot_mosaic(
"""
AABB
.HH.
"""
,figsize=(10, 9), constrained_layout=True, dpi=int(values['dpi']))
fig.suptitle(''.join([filename, ', ', str(Sorption_kinetics.sample_mass), ' mg sample' ]),
fontsize=values['text_size'])
# uptake adsorption
try:
w_ads = pd.DataFrame({'time':Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['time'] - Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['time'].min(),
'uptake':Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['uptake'],
'RH_actual':Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['RH_actual'],
'temp_target':Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['temp_target']
})
except Exception as e:
print(datetime.now())
print(e)
#continue
ax['A'].plot(w_ads['time'],
w_ads['uptake'],
c='g',
label=''.join([filename, ', ', str(Sorption_kinetics.sample_mass), ' mg' ])
)
#ax['A'].legend()
ax['A'].set_xlabel('time, min', fontsize=values['text_size'])
ax['A'].set_ylabel('uptake, wt.%', fontsize=values['text_size'], c='g')
ax['A'].tick_params(axis='x', labelsize=values['text_size'])
ax['A'].tick_params(axis='y', labelsize=values['text_size'])
ax['A'].set_title('Adsorption kinetics', fontsize=values['text_size'])
ax2 = ax['A'].twinx()
ax2.plot(w_ads['time'],
w_ads['RH_actual'],
c='b'
)
ax2.set_ylabel('RH actual, %', fontsize=values['text_size'], c='b')
ax2.tick_params(axis='y', labelsize=values['text_size'])
## test dmdt
if abs(Sorption_kinetics.data.iloc[split_index_des[cycle_number], :]['dmdt'])>dmdt_warning_threshold:
print(datetime.now())
print('Warning, dm/dt > {} at the end of adsorption interval, consider re-measuring kinetics data using lower dm/dt parameter value in order to reach equilibrium'.format(dmdt_warning_threshold))
sg.popup('Warning, dm/dt > {} at the end of adsorption interval, consider re-measuring kinetics data using lower dm/dt parameter value in order to reach equilibrium'.format(dmdt_warning_threshold), title='Warning')
# first derivative adsorption
first_derivative_dwdt_ads = pd.DataFrame({'dwdt':(w_ads['uptake'].diff()/w_ads['time'].diff()).rolling(window=window1, min_periods=1, center=True).mean(),
'uptake':w_ads['uptake']
})
# first derivative adsorption where second derivative is negatinve
first_derivative_dwdt_dw_ads = pd.DataFrame({'dwdt_dw':(first_derivative_dwdt_ads['dwdt'].diff()/first_derivative_dwdt_ads['uptake'].diff()).rolling(window=window2, min_periods=1, center=True).mean(),
'uptake':first_derivative_dwdt_ads['uptake']
})
def normalize(x):
return (x-x.min())/(x.max()-x.min())
X = np.array([[i[0], i[1]] for i in zip(normalize(first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0]['uptake']), normalize(first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0]['dwdt'])) ])
clustering_ads = DBSCAN(eps=0.1, min_samples=1).fit(X)
#print(clustering_ads.labels_)
# uptake desorption
try:
temp_index = [split_index_des[cycle_number], split_index_ads[cycle_number+1]]
except:
temp_index = [split_index_des[cycle_number], Sorption_kinetics.data.index.max()]
try:
w_des = pd.DataFrame({'time':Sorption_kinetics.data.iloc[temp_index[0]:temp_index[1], :]['time'] - Sorption_kinetics.data.iloc[temp_index[0]:temp_index[1], :]['time'].min(),
'uptake':Sorption_kinetics.data.iloc[temp_index[0]:temp_index[1], :]['uptake'],
'RH_actual':Sorption_kinetics.data.iloc[temp_index[0]:temp_index[1], :]['RH_actual'],
'temp_target':Sorption_kinetics.data.iloc[temp_index[0]:temp_index[1], :]['temp_target'],
})
except Exception as e:
print(datetime.now())
print(e)
#continue
ax['B'].plot(w_des['time'],
w_des['uptake'],
c='g',
label=''.join([filename, ', ', str(Sorption_kinetics.sample_mass), ' mg' ])
)
#ax['B'].legend()
ax['B'].set_xlabel('time, min', fontsize=values['text_size'])
ax['B'].set_ylabel('uptake, wt.%', fontsize=values['text_size'], c='g')
ax['B'].tick_params(axis='x', labelsize=values['text_size'])
ax['B'].tick_params(axis='y', labelsize=values['text_size'])
ax['B'].set_title('Desorption kinetics', fontsize=values['text_size'])
ax3 = ax['B'].twinx()
ax3.plot(w_des['time'],
w_des['RH_actual'],
c='b'
)
ax3.set_ylabel('RH actual, %', fontsize=values['text_size'], c='b')
ax3.tick_params(axis='y', labelsize=values['text_size'])
## test dmdt
if abs(Sorption_kinetics.data.iloc[temp_index[1], :]['dmdt'])>dmdt_warning_threshold:
print(datetime.now())
print('Warning, dm/dt > {} at the end of desorption interval, consider re-measuring kinetics data using lower dm/dt parameter value in order to reach equilibrium'.format(dmdt_warning_threshold))
sg.popup('Warning, dm/dt > {} at the end of desorption interval, consider re-measuring kinetics data using lower dm/dt parameter value in order to reach equilibrium'.format(dmdt_warning_threshold), title='Warning')
# first derivative desorption
first_derivative_dwdt_des = pd.DataFrame({'dwdt':(w_des['uptake'].diff()/w_des['time'].diff()).rolling(window=window1, min_periods=1, center=True).mean(),
'uptake':w_des['uptake']
})
# first derivative desorption where second derivative is negatinve
first_derivative_dwdt_dw_des = pd.DataFrame({'dwdt_dw':(first_derivative_dwdt_des['dwdt'].diff()/first_derivative_dwdt_des['uptake'].diff()).rolling(window=window2, min_periods=1, center=True).mean(),
'uptake':first_derivative_dwdt_des['uptake']
})
def normalize(x):
return (x-x.min())/(x.max()-x.min())
X = np.array([[i[0], i[1]] for i in zip(normalize(first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0]['uptake']), normalize(first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0]['dwdt'])) ])
clustering_des = DBSCAN(eps=0.1, min_samples=1).fit(X)
#print(clustering_des.labels_)
### perform RH calibration
### use DVS adventure k for TGA pan = 0.3
k_adventure_TGA_min = 0.01
k_adventure_TGA_max = 5
try:
f_des = interpolate.interp1d(first_derivative_dwdt_des['uptake'], first_derivative_dwdt_des['dwdt'], fill_value="extrapolate")
def interpolate_first_derivative_dwdt_dw_des(t):
return f_des(t)
f_ads = interpolate.interp1d(first_derivative_dwdt_ads['uptake'], first_derivative_dwdt_ads['dwdt'], fill_value="extrapolate")
def interpolate_first_derivative_dwdt_dw_ads(t):
return f_ads(t)
first_derivative_dwdt_dw_intersection = [first_derivative_dwdt_ads['uptake'][first_derivative_dwdt_dw_ads['dwdt_dw']<0][clustering_ads.labels_==np.bincount(clustering_ads.labels_).argmax()].min(),
first_derivative_dwdt_des['uptake'][first_derivative_dwdt_dw_des['dwdt_dw']<0][clustering_des.labels_==np.bincount(clustering_des.labels_).argmax()].max()]
#print('Intersection: ',first_derivative_dwdt_dw_intersection)
desdata_interpolated = interpolate_first_derivative_dwdt_dw_des(first_derivative_dwdt_ads[(first_derivative_dwdt_ads.uptake>first_derivative_dwdt_dw_intersection[0])&(first_derivative_dwdt_ads.uptake<first_derivative_dwdt_dw_intersection[1])]['uptake'])
adsdata_interpolated = interpolate_first_derivative_dwdt_dw_ads(first_derivative_dwdt_ads[(first_derivative_dwdt_ads.uptake>first_derivative_dwdt_dw_intersection[0])&(first_derivative_dwdt_ads.uptake<first_derivative_dwdt_dw_intersection[1])]['uptake'])
RH_max = Sorption_kinetics.data.iloc[split_index_ads[cycle_number]:split_index_des[cycle_number], :]['RH_target'].max()
for attempt in range(int(values['k_iterations'])):
k_adventure_TGA = (k_adventure_TGA_min+k_adventure_TGA_max)/2
#print(k_adventure_TGA)
if ((-desdata_interpolated/k_adventure_TGA*Sorption_kinetics.sample_mass)<(RH_max - adsdata_interpolated/k_adventure_TGA*Sorption_kinetics.sample_mass)).all():
k_adventure_TGA_max = k_adventure_TGA
else:
k_adventure_TGA_min = k_adventure_TGA
first_derivative_dwdt_ads['dwdt_scaled'] = RH_max - first_derivative_dwdt_ads['dwdt']/k_adventure_TGA*Sorption_kinetics.sample_mass
first_derivative_dwdt_des['dwdt_scaled'] = -first_derivative_dwdt_des['dwdt']/k_adventure_TGA*Sorption_kinetics.sample_mass
ax['H'].scatter(first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0][clustering_ads.labels_==np.bincount(clustering_ads.labels_).argmax()]['dwdt_scaled'],
first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0][clustering_ads.labels_==np.bincount(clustering_ads.labels_).argmax()]['uptake'],
#c='b',
label='adsorption',
s=5)
ax['H'].scatter(first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0][clustering_des.labels_==np.bincount(clustering_des.labels_).argmax()]['dwdt_scaled'],
first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0][clustering_des.labels_==np.bincount(clustering_des.labels_).argmax()]['uptake'],
#c='b',
label='desorption',
s=5)
ax['H'].set_xlabel('RH, %', fontsize=values['text_size'])
ax['H'].set_ylabel('uptake, wt.%', fontsize=values['text_size'])
ax['H'].tick_params(axis='x', labelsize=values['text_size'])
ax['H'].tick_params(axis='y', labelsize=values['text_size'])
ax['H'].set_title('Isotherm determined from non-equilibrium kinetics', fontsize=values['text_size'])
ax['H'].legend()
df_export_ads = pd.DataFrame(data={'adsorption_RH': first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0][clustering_ads.labels_==np.bincount(clustering_ads.labels_).argmax()]['dwdt_scaled'],
'adsorption_uptake': first_derivative_dwdt_ads[first_derivative_dwdt_dw_ads['dwdt_dw']<0][clustering_ads.labels_==np.bincount(clustering_ads.labels_).argmax()]['uptake'],
}).reset_index(drop=True)
df_export_des = pd.DataFrame(data={'desorption_RH': first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0][clustering_des.labels_==np.bincount(clustering_des.labels_).argmax()]['dwdt_scaled'],
'desorption_uptake': first_derivative_dwdt_des[first_derivative_dwdt_dw_des['dwdt_dw']<0][clustering_des.labels_==np.bincount(clustering_des.labels_).argmax()]['uptake'],
}).reset_index(drop=True)
df_export = pd.concat([df_export_ads, df_export_des], axis=1,)
df_export_temperature = (w_ads.temp_target.mean()+w_des.temp_target.mean())/2
print(datetime.now())
print('Isotherm calculation successfull')
print('Fitted parameter k: ',round(k_adventure_TGA, 5))
isotherm_calculated = True
except Exception as e:
print(datetime.now())
print(e)
pass
fig.tight_layout(#rect=[0, 0, 1, 0.97])
)
plt.show(block=False)
################# Export isotherm ################
if (event == 'Save')&(isotherm_calculated!=True):
print(datetime.now())
print('Error, calculate isotherm (step 2) before saving it')
sg.popup('Error, calculate isotherm (step 2) before saving it')
if (event == 'Save')&(isotherm_calculated==True):
### export csv
if values['isotherm_file_type'] == 'CSV (Comma delimited) (*.csv)':
with open(path+values['isotherm_file']+'.csv', 'w') as file:
file.write('Sample_material_id \"{}\"\n'.format(Sorption_kinetics.filename))
try: file.write('Instrument {}\n'.format(Sorption_kinetics.instrument))
except: file.write('Instrument unknown\n')
file.write('Adsorptive Water\n')
file.write('Temperature {} K\n'.format(str(round(df_export_temperature+273.15))))
file.write('Sample_mass {} mg\n'.format(Sorption_kinetics.sample_mass))
file.write('RH units [%]\n')
file.write('Uptake units [wt.%]\n')
file.write('\"Experimental method: calculated from non-equilibrium kinetics ("https://github.com/AndreyBezrukov/SKID")\"\n')
df_export.to_csv(path+values['isotherm_file']+'.csv',
index=False,
mode='a' )
print(datetime.now())
print('Isotherm saved to: {0}{1}{2}'.format(path, values['isotherm_file'], '.csv'))
### export AIF
if values['isotherm_file_type'] == 'Adsorption Isotherm File (*.AIF)':
f_p0 = interpolate.interp1d([0, 10, 20, 25, 27, 30, 40],[613, 1227, 2340, 3171, 3569, 4248, 7385 ], fill_value="extrapolate") ## interpolate saturation pressure
def interpolate_p0(t):
return f_p0(t)
aif_pressure = round(interpolate_p0(df_export_temperature).item())
df_export_ads_aif = df_export_ads
df_export_ads_aif['uptake_cm3_g'] = df_export_ads_aif['adsorption_uptake']/100/18.015*22.4*1000
df_export_ads['p0'] = aif_pressure ## Pa
df_export_ads['pressure'] = df_export_ads.adsorption_RH/100*aif_pressure ## Pa
with open(path+values['isotherm_file']+'.aif', 'w') as file:
file.write('data_\n')
file.write('_sample_material_id \"{}\"\n'.format(Sorption_kinetics.filename))
try: file.write('_exptl_instrument {}\n'.format(Sorption_kinetics.instrument))
except: file.write('Instrument unknown\n')
file.write('_exptl_adsorptive Water\n')
file.write('_exptl_temperature {}\n'.format(str(round(df_export_temperature+273.15))))
file.write('_adsnt_sample_mass {}\n'.format(Sorption_kinetics.sample_mass))
file.write('_units_temperature K\n')
file.write('_units_pressure Pa\n')
file.write('_units_mass mg\n')
file.write('_units_loading cm3(STP)/g\n')
file.write('_exptl_method \"gravimetric, calculated from non-equilibrium kinetics ("https://github.com/AndreyBezrukov/SKID")\"\n')
file.write('loop_\n_adsorp_pressure\n_adsorp_p0\n_adsorp_amount\n')
df_export_ads[['pressure', 'p0', 'uptake_cm3_g']].to_csv(path+values['isotherm_file']+'.aif',
sep=' ',
header=False,
index=False,
mode='a')
df_export_des_aif = df_export_des
df_export_des_aif['uptake_cm3_g'] = df_export_des_aif['desorption_uptake']/100/18.015*22.4*1000
df_export_des['p0'] = aif_pressure ## Pa
df_export_des['pressure'] = df_export_des.desorption_RH/100*aif_pressure ## Pa
with open(path+values['isotherm_file']+'.aif', 'a') as file:
file.write('loop_\n_desorp_pressure\n_desorp_p0\n_desorp_amount\n')
df_export_des[['pressure', 'p0', 'uptake_cm3_g']].to_csv(path+values['isotherm_file']+'.aif',
sep=' ',
header=False,
index=False,
mode='a')
print(datetime.now())
print('Isotherm saved to: {0}{1}{2}'.format(path, values['isotherm_file'], '.aif'))