Skip to content

Commit

Permalink
feat: update to handle shift data for cv layout (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
baolanlequang authored May 28, 2024
1 parent 6a35377 commit 7d80c31
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 16 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 @@ -212,8 +212,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
16 changes: 14 additions & 2 deletions chem_spectra/lib/converter/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,35 @@ def parse_params(params):
peaks_str = params.get('peaks_str', None)
delta = 0.0
mass = params.get('mass', 0)
mass = mass if mass else 0
scan = params.get('scan', None)
thres = params.get('thres', None)
clear = params.get('clear', False)
clear = clear if clear else False
integration = params.get('integration')
integration = json.loads(integration) if integration else default_itg
multiplicity = params.get('multiplicity')
multiplicity = json.loads(multiplicity) if multiplicity else default_mpy
ext = params.get('ext', '')
ext = ext if ext else ''
fname = params.get('fname', '').split('.')
fname = fname[:-2] if (len(fname) > 2 and (fname[-2] in ['edit', 'peak'])) else fname[:-1]
fname = '.'.join(fname)
waveLength = params.get('waveLength')
waveLength = json.loads(waveLength) if waveLength else default_wavelength
axesUnits = params.get('axesUnits')
axesUnits = json.loads(axesUnits) if axesUnits else None

jcamp_idx = params.get('jcamp_idx', 0)
jcamp_idx = jcamp_idx if jcamp_idx else 0
axesUnitsJson = params.get('axesUnits')
axesUnitsDic = json.loads(axesUnitsJson) if axesUnitsJson else None
axesUnits = None
if axesUnitsDic != None and 'axes' in axesUnitsDic:
axes = axesUnitsDic.get('axes', [{'xUnit': '', 'yUnit': ''}])
try:
axesUnits = axes[jcamp_idx]
except:
pass

cyclicvolta = params.get('cyclic_volta')
cyclicvolta = json.loads(cyclicvolta) if cyclicvolta else None
listMaxMinPeaks = None
Expand Down
10 changes: 4 additions & 6 deletions chem_spectra/lib/shared/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +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']

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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='chem-spectra-app',
version='1.1.2',
version='1.2.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
Expand Down
Loading

0 comments on commit 7d80c31

Please sign in to comment.