Skip to content

Commit

Permalink
feat: read mw, mp, d, mn values for sec (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
baolanlequang authored Jun 5, 2024
1 parent 7d80c31 commit 266cdc4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def __gen_header_cyclic_voltammetry(self):
'$$ === CHEMSPECTRA CYCLIC VOLTAMMETRY ===\n',
]

def __gen_header_sec(self):
core_dic = self.core.dic
sec_data_key = ['MN', 'MW', 'MP', 'D']
result = []
for key in sec_data_key:
dic_value = core_dic.get(key, [])
key_str = f"##{key}={dic_value[0]}\n" if len(dic_value) > 0 else f'##{key}=\n'
result.append(key_str)
return result

def __get_xy_of_peak(self, peak):
if peak is None:
return '', ''
Expand Down Expand Up @@ -201,6 +211,8 @@ def __compose(self):
meta.extend(self.gen_headers_root())

meta.extend(self.__gen_headers_spectrum_orig())
if self.core.is_sec:
meta.extend(self.__gen_header_sec())
meta.extend(self.gen_spectrum_orig())
meta.extend(self.__gen_headers_im())
meta.extend(self.__gen_headers_integration())
Expand Down Expand Up @@ -509,6 +521,9 @@ def tf_img(self):
plt.ylabel("Y ({})".format(self.core.label['y']), fontsize=18)
plt.locator_params(nbins=self.__plt_nbins())
plt.grid(False)

self.__generate_info_box(plt)

# Save
tf_img = tempfile.NamedTemporaryFile(suffix='.png')
plt.savefig(tf_img, format='png')
Expand All @@ -517,6 +532,25 @@ def tf_img(self):
plt.cla()
return tf_img


def __generate_info_box(self, plotlib):
if not self.core.is_sec:
return
core_dic = self.core.dic
sec_data_key = ['MN', 'MW', 'MP', 'D']
result = []
for key in sec_data_key:
dic_value = core_dic.get(key, [])
key_str = f"{key}={dic_value[0]}" if len(dic_value) > 0 else None
if key_str is not None:
result.append(key_str)

info_str = '\n'.join(result)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
ax = plotlib.gca()
ax.text(0.05, 0.95, info_str, fontsize=14, verticalalignment='top', bbox=props, transform=ax.transAxes)


def __prepare_metadata_info_for_csv(self, csv_writer: csv.DictWriter):
csv_writer.writerow({
'Anodic E(V)': 'Measurement type',
Expand Down
Binary file added tests/fixtures/source/sec/SEC-ExFile.zip
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/lib/composer/test_ni_composer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import json
import pytest
import numpy as np
import tempfile
import zipfile
from chem_spectra.lib.converter.jcamp.base import JcampBaseConverter
from chem_spectra.lib.converter.jcamp.ni import JcampNIConverter
from chem_spectra.lib.composer.ni import NIComposer
from chem_spectra.controller.helper.file_container import FileContainer
from chem_spectra.lib.converter.bagit.base import BagItBaseConverter as BagItConveter

source_nmr = './tests/fixtures/source/1H.dx'
source_nmr_edit = './tests/fixtures/source/1H.edit.jdx'
source_ir = './tests/fixtures/source/IR.dx'
source_dir_molfile = './tests/fixtures/source/molfile/c60h57fn4.mol'
source_sec = './tests/fixtures/source/sec/SEC-ExFile.zip'

@pytest.fixture
def data_schema():
Expand Down Expand Up @@ -37,6 +41,10 @@ def molfile_data():
molfile_str = molfile.read()
return molfile_str

@pytest.fixture
def bagit_file_sec():
return source_sec

def test_init_ni_composer_failed():
with pytest.raises(Exception) as error:
_ = NIComposer(None)
Expand Down Expand Up @@ -250,3 +258,19 @@ def test_ni_composer_generate_nmrium_with_molfile(jcamp_file_1h, molfile_data):
assert 'id' in molecule_data.keys()
assert molecule_data['label'] == 'P1'
assert molecule_data['molfile'] == molfile_data

def test_ni_composer_generate_jcamp(bagit_file_sec):
with tempfile.TemporaryDirectory() as td:
with zipfile.ZipFile(bagit_file_sec, 'r') as z:
z.extractall(td)

converter = BagItConveter(td)
for jcamp_file in converter.data:
base_converter = JcampBaseConverter(jcamp_file.name)
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter)
headers = ni_composer._NIComposer__gen_header_sec()
assert headers in [
['##MN=1.287E+3\n', '##MW=1.465E+3\n', '##MP=1.345E+3\n', '##D=1.139E+0\n'],
['##MN=\n', '##MW=\n', '##MP=\n', '##D=\n']
]

0 comments on commit 266cdc4

Please sign in to comment.