Skip to content

Commit

Permalink
feat: update marker for ref peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Apr 25, 2024
1 parent d0cb921 commit 0f6e847
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
4 changes: 3 additions & 1 deletion chem_spectra/controller/transform_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ def combine_images():
list_files.append(file_container)

params = extract_params(request)
extras = request.form.get('extras', default=None)

transform_model = TraModel(None, params=params, multiple_files=list_files)
tf_combine = transform_model.tf_combine(list_file_names=params['list_file_names'])
tf_combine = transform_model.tf_combine(list_file_names=params['list_file_names'], extraParams=extras)
if (not tf_combine):
abort(400)

Expand Down
34 changes: 32 additions & 2 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def tf_img(self):
]
codes, verts = zip(*path_data)
marker = mpath.Path(verts, codes)

circle = mpath.Path.unit_circle()
cirle_verts = np.concatenate([circle.vertices, verts])
cirle_codes = np.concatenate([circle.codes, codes])
cut_star_marker = mpath.Path(cirle_verts, cirle_codes)

x_peaks = []
y_peaks = []
if self.core.edit_peaks:
Expand All @@ -296,6 +302,7 @@ def tf_img(self):

x_peckers = []
y_peckers = []
x_peaks_ref, y_peaks_ref = [], []
if self.core.is_cyclic_volta:
x_peaks = []
y_peaks = []
Expand Down Expand Up @@ -328,8 +335,13 @@ def tf_img(self):
x_peaks.extend([x_max_peak])
y_peaks.extend([y_max_peak])
else:
x_peaks.extend([x_max_peak, x_min_peak])
y_peaks.extend([y_max_peak, y_min_peak])
is_ref = peak.get('isRef', False) if 'isRef' in peak else False
if is_ref:
x_peaks_ref.extend([x_max_peak, x_min_peak])
y_peaks_ref.extend([y_max_peak, y_min_peak])
else:
x_peaks.extend([x_max_peak, x_min_peak])
y_peaks.extend([y_max_peak, y_min_peak])

if 'pecker' in peak and peak['pecker'] is not None:
pecker = peak['pecker']
Expand All @@ -346,6 +358,15 @@ def tf_img(self):
peak_label = 'x: {x}\ny: {y}'.format(x=x_float, y=y_float)
plt.text(x_pos, y_pos, peak_label)

# display x value of ref peak for cyclic voltammetry
for i in range(len(x_peaks_ref)):
x_pos = x_peaks_ref[i]
y_pos = y_peaks_ref[i] + h * 0.1
x_float = '{:.2e}'.format(x_pos)
y_float = '{:.2e}'.format(y_peaks_ref[i])
peak_label = 'x: {x}\ny: {y}'.format(x=x_float, y=y_float)
plt.text(x_pos, y_pos, peak_label)

plt.plot(
x_peaks,
y_peaks,
Expand All @@ -364,6 +385,15 @@ def tf_img(self):
markersize=50,
)

plt.plot(
x_peaks_ref,
y_peaks_ref,
'r',
ls='',
marker=cut_star_marker,
markersize=50,
)

# ----- PLOT integration -----
refShift, refArea = self.refShift, self.refArea
itg_h = y_max + h * 0.1
Expand Down
8 changes: 1 addition & 7 deletions chem_spectra/lib/shared/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,9 @@ def cal_cyclic_volta_shift_prev_offset_at_index(cyclic_data, index=0):
return 0.0

spectra = spectra_list[index]
# analysed_data = spectra['list']
# arr_has_ref_value = list(filter(lambda x: ('isRef' in x and x['isRef'] == True), analysed_data))
# if len(arr_has_ref_value) > 0:
# shift = spectra['shift']
# if 'prevValue' in shift:
# offset = shift['prevValue']
hasRefPeak = spectra.get('hasRefPeak', False) == True
shift = spectra['shift']
if 'prevValue' in shift:
offset = shift['prevValue'] if hasRefPeak else -shift['prevValue']
offset = shift['prevValue'] if hasRefPeak else 0.0

return offset
49 changes: 47 additions & 2 deletions chem_spectra/model/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from chem_spectra.lib.composer.base import BaseComposer # noqa: F401
from chem_spectra.lib.converter.nmrium.base import NMRiumDataConverter
import matplotlib.pyplot as plt # noqa: E402
import matplotlib.path as mpath # noqa: E402
import numpy as np # noqa: E402

from chem_spectra.model.concern.property import decorate_sim_property

Expand Down Expand Up @@ -268,13 +270,45 @@ def tf_nmrium(self):
nicp = NIComposer(converter)
tf_jcamp = nicp.tf_jcamp()
return tf_jcamp

def tf_combine(self, list_file_names=None):

def __get_cyclic_volta_ref_peaks(self, curve_idx, extraParams):
x_peaks, y_peaks = [], []
try:
extras_dict = json.loads(extraParams) if extraParams else None
cyclic_volta_str = extras_dict['cyclicvolta'] if extras_dict else None
cyclic_volta = json.loads(cyclic_volta_str) if cyclic_volta_str else None
spectra_list = cyclic_volta['spectraList'] if cyclic_volta else None
spectra_extra = spectra_list[curve_idx] if spectra_list and curve_idx < len(spectra_list) else None
list_peaks = spectra_extra['list'] if spectra_extra else []
x_peaks, y_peaks = [], []
for peak in list_peaks:
min_peak, max_peak, isRef = peak['min'], peak['max'], peak['isRef']
if isRef == True:
x_peaks.extend([min_peak['x'], max_peak['x']])
y_peaks.extend([min_peak['y'], max_peak['y']])
except:
pass

return x_peaks, y_peaks

def tf_combine(self, list_file_names=None, extraParams=None):
if not self.multiple_files:
return False

path_data = [
(mpath.Path.MOVETO, (0, 5)),
(mpath.Path.LINETO, (0, 20)),
]
codes, verts = zip(*path_data)

circle = mpath.Path.unit_circle()
cirle_verts = np.concatenate([circle.vertices, verts])
cirle_codes = np.concatenate([circle.codes, codes])
cut_star_marker = mpath.Path(cirle_verts, cirle_codes)

plt.rcParams['figure.figsize'] = [16, 9]
plt.rcParams['font.size'] = 14
plt.rcParams['legend.loc'] = 'upper left'
curve_idx = self.params.get('jcamp_idx', 0)

xlabel, ylabel = '', ''
Expand Down Expand Up @@ -315,6 +349,17 @@ def tf_combine(self, list_file_names=None):
core_label_x = nicp.core.label['x']
core_label_y = nicp.core.label['y']
if nicp.core.is_cyclic_volta:
x_peaks, y_peaks = self.__get_cyclic_volta_ref_peaks(curve_idx, extraParams)

plt.plot(
x_peaks,
y_peaks,
'r',
ls='',
marker=cut_star_marker,
markersize=50,
)

if core_label_x not in dic_x_label:
xlabel_set.append(core_label_x)
dic_x_label[core_label_x] = 1
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/shared/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ def test_cal_cyclic_volta_shift_prev_offset_at_index(cyclic_data):
assert offset == expected_offset

def test_cal_cyclic_volta_shift_prev_offset_at_index_no_ref_value(cyclic_data_no_ref_value):
expected_offset = -2.5
expected_offset = 0
offset = cal_cyclic_volta_shift_prev_offset_at_index(cyclic_data_no_ref_value, 0)
assert offset == expected_offset

0 comments on commit 0f6e847

Please sign in to comment.