Skip to content

Commit

Permalink
feat: add cv data with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Oct 9, 2023
1 parent a12ced7 commit d110b2b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
8 changes: 7 additions & 1 deletion chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
extrac_dic, calc_npoints, BaseComposer
)
from chem_spectra.lib.shared.calc import ( # noqa: E402
calc_mpy_center, calc_ks, get_curve_endpoint, cal_slope, cal_xyIntegration
calc_mpy_center, calc_ks, get_curve_endpoint, cal_slope, cal_xyIntegration, cal_cyclic_volta_shift_offset_at_index
)


Expand Down Expand Up @@ -141,6 +141,9 @@ def __gen_cyclic_voltammetry_data_peaks(self):
content = ['##$CSCYCLICVOLTAMMETRYDATA=\n']
if self.core.is_cyclic_volta:
listMaxMinPeaks = self.core.max_min_peaks_table
cyclicvolta_data = self.core.params['cyclicvolta']
current_jcamp_idx = self.core.params['jcamp_idx']
offset = cal_cyclic_volta_shift_offset_at_index(cyclicvolta_data, current_jcamp_idx)
if self.core.params['list_max_min_peaks'] is not None:
listMaxMinPeaks = self.core.params['list_max_min_peaks']

Expand All @@ -163,6 +166,8 @@ def __gen_cyclic_voltammetry_data_peaks(self):
if (x_max_peak == '' or x_min_peak == ''):
delta = ''
else:
x_max_peak += offset
x_min_peak += offset
delta = abs(x_max_peak - x_min_peak)

x_pecker = ''
Expand All @@ -174,6 +179,7 @@ def __gen_cyclic_voltammetry_data_peaks(self):
pecker = peak['pecker']
x_pecker = pecker['x']
y_pecker = pecker['y']
x_pecker += offset
first_expr = abs(y_min_peak) / abs(y_max_peak)
second_expr = 0.485 * abs(y_pecker) / abs(y_max_peak)
ratio = first_expr + second_expr + 0.086
Expand Down
4 changes: 4 additions & 0 deletions chem_spectra/lib/converter/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def parse_params(params):
'fname': '',
'waveLength': default_wavelength,
'list_max_min_peaks': None,
'cyclicvolta': None,
'jcamp_idx': 0,
}

select_x = params.get('select_x', None)
Expand Down Expand Up @@ -75,6 +77,8 @@ def parse_params(params):
'fname': fname,
'waveLength': waveLength,
'list_max_min_peaks': listMaxMinPeaks,
'cyclicvolta': cyclicvolta,
'jcamp_idx': jcamp_idx,
}


Expand Down
56 changes: 56 additions & 0 deletions chem_spectra/lib/shared/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,59 @@ def cal_area_multiplicity(xL, xU, data_xs, data_ys):
lower_value = Decimal(str(ks[iL]))

return float(abs(upper_value - lower_value))

def cal_cyclic_volta_shift_offset(cyclic_data):
if cyclic_data is None:
return []
if isinstance(cyclic_data, dict) == False:
return []
if 'spectraList' not in cyclic_data:
return []

spectra_list = cyclic_data['spectraList']
if isinstance(spectra_list, list) == False:
return []

list_offsets = []

for spectra in spectra_list:
offset = 0.0
analysed_data = spectra['list']
arr_has_ref_value = list(filter(lambda x: x['isRef'] == True, analysed_data))
if len(arr_has_ref_value) > 0:
shift = spectra['shift']
val = shift['val']
ref_value = arr_has_ref_value[0]
e12 = ref_value['e12']
offset = e12 - val
list_offsets.append(offset)

return list_offsets

def cal_cyclic_volta_shift_offset_at_index(cyclic_data, index=0):
if cyclic_data is None:
return 0.0
if isinstance(cyclic_data, dict) == False:
return 0.0
if 'spectraList' not in cyclic_data:
return 0.0

spectra_list = cyclic_data['spectraList']
if isinstance(spectra_list, list) == False:
return 0.0
if len(spectra_list) < index:
return 0.0

offset = 0.0

spectra = spectra_list[index]
analysed_data = spectra['list']
arr_has_ref_value = list(filter(lambda x: x['isRef'] == True, analysed_data))
if len(arr_has_ref_value) > 0:
shift = spectra['shift']
val = shift['val']
ref_value = arr_has_ref_value[0]
e12 = ref_value['e12']
offset = e12 - val

return offset
22 changes: 22 additions & 0 deletions tests/lib/shared/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ def test_cal_area_multiplicity():
data_ys = [2.0, 4.0]
area = cal_area_multiplicity(xL, xU, data_xs=data_xs, data_ys=data_ys)
assert area == 1.0

def test_cal_cyclic_volta_shift_offset_when_cyclic_data_is_none():
list_expected_offsets = []
list_offsets = cal_cyclic_volta_shift_offset(None)
assert list_offsets == list_expected_offsets

def test_cal_cyclic_volta_shift_offset_when_invalid_cyclic_data():
list_expected_offsets = []
list_offsets = cal_cyclic_volta_shift_offset({'test': []})
assert list_offsets == list_expected_offsets

def test_cal_cyclic_volta_shift_offset():
cyclic_data = {'spectraList':[{'list':[{'min':{'x':-1.48904,'y':-1.10686e-05},'max':{'x':1.80895,'y':9.51171e-06},'isRef':True,'e12':0.15995500000000007,'pecker':{'x':-0.129871,'y':7.78418e-07}}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':0,'shift':{'ref':None,'val':0}},{'list':[{'min':{'x':-1.48904,'y':-3.3747399999999995e-05},'max':{'x':0.929483,'y':0.00023741},'isRef':True,'e12':-0.27977849999999993}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':1,'shift':{'ref':None,'val':5}},{'list':[{'min':{'x':0.45977,'y':-0.000226347},'max':{'x':1.00943,'y':0.000371349},'isRef':False,'e12':0.7346}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':2,'shift':{'ref':None,'val':0}}]}
list_expected_offsets = [0.15995500000000007, -5.2797785, 0.0]
list_offsets = cal_cyclic_volta_shift_offset(cyclic_data)
assert list_offsets == list_expected_offsets

def test_cal_cyclic_volta_shift_offset_at_index():
cyclic_data = {'spectraList':[{'list':[{'min':{'x':-1.48904,'y':-1.10686e-05},'max':{'x':1.80895,'y':9.51171e-06},'isRef':True,'e12':0.15995500000000007,'pecker':{'x':-0.129871,'y':7.78418e-07}}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':0,'shift':{'ref':None,'val':0}},{'list':[{'min':{'x':-1.48904,'y':-3.3747399999999995e-05},'max':{'x':0.929483,'y':0.00023741},'isRef':True,'e12':-0.27977849999999993}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':1,'shift':{'ref':None,'val':5}},{'list':[{'min':{'x':0.45977,'y':-0.000226347},'max':{'x':1.00943,'y':0.000371349},'isRef':False,'e12':0.7346}],'selectedIdx':0,'isWorkMaxPeak':True,'jcampIdx':2,'shift':{'ref':None,'val':0}}]}
expected_offset = 0.15995500000000007
offset = cal_cyclic_volta_shift_offset_at_index(cyclic_data, 0)
assert offset == expected_offset

0 comments on commit d110b2b

Please sign in to comment.