From a8aef775757cf774bc023e1471524b27f0f4a555 Mon Sep 17 00:00:00 2001 From: Lan Le Date: Wed, 30 Aug 2023 14:30:24 +0200 Subject: [PATCH] feat: generate meta value --- chem_spectra/lib/composer/base.py | 2 +- chem_spectra/lib/composer/ni.py | 7 +++++-- .../result/edit/edit_meta_13C-DEPT135 | 4 ++++ tests/fixtures/result/edit/edit_meta_1H | 4 ++++ tests/fixtures/result/edit/edit_meta_IR | 4 ++++ tests/fixtures/result/im/im_meta_13C-DEPT135 | 4 ++++ tests/fixtures/result/im/im_meta_1H | 4 ++++ tests/fixtures/result/im/im_meta_IR | 4 ++++ tests/fixtures/result/meta_13C-CPD | 4 ++++ tests/fixtures/result/meta_13C-DEPT135 | 3 +++ tests/fixtures/result/meta_1H | 4 ++++ tests/fixtures/result/meta_IR | 4 ++++ tests/fixtures/result/meta_SVS-790A_13C | 4 ++++ tests/fixtures/result/ps/ps_meta_IR | 4 ++++ tests/lib/composer/test_base_composer.py | 8 ++++--- tests/lib/composer/test_ni_composer.py | 21 +++++++++++++++++++ 16 files changed, 79 insertions(+), 6 deletions(-) diff --git a/chem_spectra/lib/composer/base.py b/chem_spectra/lib/composer/base.py index f78e4a2b..7311312e 100644 --- a/chem_spectra/lib/composer/base.py +++ b/chem_spectra/lib/composer/base.py @@ -293,7 +293,7 @@ def gen_simulation_info(self): return [] def generate_auto_metadata(self): - content = [] + content = self.__header_auto_metadata() if self.auto_metadata is None: return content for key, value in self.auto_metadata.items(): diff --git a/chem_spectra/lib/composer/ni.py b/chem_spectra/lib/composer/ni.py index 9003216d..bf775dbc 100644 --- a/chem_spectra/lib/composer/ni.py +++ b/chem_spectra/lib/composer/ni.py @@ -26,8 +26,8 @@ class NIComposer(BaseComposer): - def __init__(self, core): - super().__init__(core) + def __init__(self, core, auto_metadata=None): + super().__init__(core, auto_metadata) self.title = core.fname self.meta = self.__compose() @@ -214,6 +214,9 @@ def __compose(self): meta.extend(self.gen_auto_peaktable()) meta.extend(self.gen_ending()) + meta.extend(self.generate_auto_metadata()) + meta.extend(self.gen_ending()) + meta.extend(self.gen_ending()) return meta diff --git a/tests/fixtures/result/edit/edit_meta_13C-DEPT135 b/tests/fixtures/result/edit/edit_meta_13C-DEPT135 index 8fa01c52..c01af222 100644 --- a/tests/fixtures/result/edit/edit_meta_13C-DEPT135 +++ b/tests/fixtures/result/edit/edit_meta_13C-DEPT135 @@ -76,5 +76,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 4.538400222309662, 5694845.0 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/edit/edit_meta_1H b/tests/fixtures/result/edit/edit_meta_1H index ebada9da..15b4f028 100644 --- a/tests/fixtures/result/edit/edit_meta_1H +++ b/tests/fixtures/result/edit/edit_meta_1H @@ -83,5 +83,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 1.40743613258479, 9729036.0 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/edit/edit_meta_IR b/tests/fixtures/result/edit/edit_meta_IR index 3d5b5b0f..07f0f1d7 100644 --- a/tests/fixtures/result/edit/edit_meta_IR +++ b/tests/fixtures/result/edit/edit_meta_IR @@ -83,5 +83,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 392.3793345973413, 0.5906721942888722 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/im/im_meta_13C-DEPT135 b/tests/fixtures/result/im/im_meta_13C-DEPT135 index 0ae3fea6..c6cb9ac2 100644 --- a/tests/fixtures/result/im/im_meta_13C-DEPT135 +++ b/tests/fixtures/result/im/im_meta_13C-DEPT135 @@ -53,3 +53,7 @@ $$ === CHEMSPECTRA PEAK TABLE EDIT === 4.538400222309662, 5694845.0 25.094301409579884, 5687140.0 127.3 + + +$$ === CHEMSPECTRA AUTO METADATA === +##END= \ No newline at end of file diff --git a/tests/fixtures/result/im/im_meta_1H b/tests/fixtures/result/im/im_meta_1H index 40734426..91f227e9 100644 --- a/tests/fixtures/result/im/im_meta_1H +++ b/tests/fixtures/result/im/im_meta_1H @@ -145,5 +145,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 7.0099466237990224, 1913776.0 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/im/im_meta_IR b/tests/fixtures/result/im/im_meta_IR index 436527a9..d72a4bc0 100644 --- a/tests/fixtures/result/im/im_meta_IR +++ b/tests/fixtures/result/im/im_meta_IR @@ -141,5 +141,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 745.0957757310398, 0.2787140606224312 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/meta_13C-CPD b/tests/fixtures/result/meta_13C-CPD index 8a142401..0101de29 100644 --- a/tests/fixtures/result/meta_13C-CPD +++ b/tests/fixtures/result/meta_13C-CPD @@ -93,5 +93,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 117.05974904621954, 3088543.0 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/meta_13C-DEPT135 b/tests/fixtures/result/meta_13C-DEPT135 index f3d8b967..66f99ed1 100644 --- a/tests/fixtures/result/meta_13C-DEPT135 +++ b/tests/fixtures/result/meta_13C-DEPT135 @@ -58,3 +58,6 @@ $$ === CHEMSPECTRA PEAK TABLE EDIT === 1.0537001328133897, 5016849.0 89.16683096721874, 4 + +$$ === CHEMSPECTRA AUTO METADATA === +##END= \ No newline at end of file diff --git a/tests/fixtures/result/meta_1H b/tests/fixtures/result/meta_1H index fc221497..e8217d15 100644 --- a/tests/fixtures/result/meta_1H +++ b/tests/fixtures/result/meta_1H @@ -138,5 +138,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 7.0099466237990224, 1913776.0 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/meta_IR b/tests/fixtures/result/meta_IR index 436527a9..d72a4bc0 100644 --- a/tests/fixtures/result/meta_IR +++ b/tests/fixtures/result/meta_IR @@ -141,5 +141,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 745.0957757310398, 0.2787140606224312 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/meta_SVS-790A_13C b/tests/fixtures/result/meta_SVS-790A_13C index badbd53c..248e60eb 100644 --- a/tests/fixtures/result/meta_SVS-790A_13C +++ b/tests/fixtures/result/meta_SVS-790A_13C @@ -191,5 +191,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === -3.570531620130702, 25781.920000000002 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/fixtures/result/ps/ps_meta_IR b/tests/fixtures/result/ps/ps_meta_IR index ea211cb5..c9275800 100644 --- a/tests/fixtures/result/ps/ps_meta_IR +++ b/tests/fixtures/result/ps/ps_meta_IR @@ -144,5 +144,9 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO === 745.0957757310398, 0.2787140606224312 ##END= + +$$ === CHEMSPECTRA AUTO METADATA === +##END= + ##END= diff --git a/tests/lib/composer/test_base_composer.py b/tests/lib/composer/test_base_composer.py index 4038b2ee..be5067d1 100644 --- a/tests/lib/composer/test_base_composer.py +++ b/tests/lib/composer/test_base_composer.py @@ -20,8 +20,8 @@ def test_base_composer_generate_auto_metadata_headers(jcamp_file_1h): composer = BaseComposer(core=base_converter) headers = composer._BaseComposer__header_auto_metadata() assert headers == [ - '\n', - '$$ === CHEMSPECTRA AUTO METADATA ===\n', + '\n', + '$$ === CHEMSPECTRA AUTO METADATA ===\n', ] def test_base_composer_generate_auto_metadata(jcamp_file_1h): @@ -30,5 +30,7 @@ def test_base_composer_generate_auto_metadata(jcamp_file_1h): composer = BaseComposer(core=base_converter, auto_metadata=metadata) auto_metadata = composer.generate_auto_metadata() assert auto_metadata == [ - '##JUST A STRING=just a string value\n' + '\n', + '$$ === CHEMSPECTRA AUTO METADATA ===\n', + '##JUST A STRING=just a string value\n' ] diff --git a/tests/lib/composer/test_ni_composer.py b/tests/lib/composer/test_ni_composer.py index c8faa0a1..c94c8771 100644 --- a/tests/lib/composer/test_ni_composer.py +++ b/tests/lib/composer/test_ni_composer.py @@ -20,6 +20,10 @@ def jcamp_file_1h_edit(): def jcamp_file_ir(): return source_ir +@pytest.fixture +def auto_metadata_dic(): + return {"TEST_KEY": "just a string value"} + def test_init_ni_composer_failed(): with pytest.raises(Exception) as error: _ = NIComposer(None) @@ -34,6 +38,23 @@ def test_init_ni_composer_success(jcamp_file_1h): assert ni_composer is not None assert ni_composer.core == ni_converter +def test_init_ni_composer_success_with_auto_metadata(jcamp_file_1h, auto_metadata_dic): + base_converter = JcampBaseConverter(jcamp_file_1h) + ni_converter = JcampNIConverter(base=base_converter) + ni_composer = NIComposer(core=ni_converter, auto_metadata=auto_metadata_dic) + + assert ni_composer is not None + assert ni_composer.auto_metadata == auto_metadata_dic + +def test_composer_auto_metadata(jcamp_file_1h, auto_metadata_dic): + base_converter = JcampBaseConverter(jcamp_file_1h) + ni_converter = JcampNIConverter(base=base_converter) + ni_composer = NIComposer(core=ni_converter, auto_metadata=auto_metadata_dic) + + assert ni_composer is not None + assert '$$ === CHEMSPECTRA AUTO METADATA ===\n' in ni_composer.meta + assert '##TEST_KEY=just a string value\n' in ni_composer.meta + def test_ni_composer_header(jcamp_file_1h): base_converter = JcampBaseConverter(jcamp_file_1h) ni_converter = JcampNIConverter(base=base_converter)