Skip to content

Commit

Permalink
feat: read mw, mp, d, mn values for sec
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed May 15, 2024
1 parent 515a27f commit 4f7511f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 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
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,13 +1,17 @@
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.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_sec = './tests/fixtures/source/sec/SEC-ExFile.zip'

@pytest.fixture
def data_schema():
Expand All @@ -29,6 +33,10 @@ def jcamp_file_1h_edit():
def jcamp_file_ir():
return source_ir

@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 @@ -203,3 +211,19 @@ def test_ni_composer_generate_nmrium(jcamp_file_1h):
y_value = xy_points['re']
assert len(x_values) == 65536
assert len(y_value) == 65536

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 4f7511f

Please sign in to comment.