Skip to content

Commit

Permalink
feat: process generate nmrium data schema v3 and v4, v5
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Apr 24, 2024
1 parent 5fb8880 commit f25da45
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 29 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ name: Test with pytest
on: [push]

jobs:
build-linux:
run-unit-tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install dependencies
run: |
$CONDA/bin/conda env update --file environment.yml --name base
uses: conda-incubator/setup-miniconda@v3
with:
activate-environment: python-v38
environment-file: environment.yml
miniforge-version: latest
- name: Test with pytest
shell: bash -el {0}
run: |
# pip install -r requirements.txt
$CONDA/bin/python -m pytest
conda activate python-v38
python -m pytest
21 changes: 17 additions & 4 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,21 +587,34 @@ def tf_csv(self):
})
return tf_csv

def generate_nmrium(self):
def generate_nmrium(self, version=4):
typ = self.core.typ
if 'NMR' != typ:
return None

dic_data = {'actionType': 'INITIATE', 'version': 3}
spectra = self.__generate_nmrim_spectra()
dic_data['spectra'] = spectra
if version == 3:
dic_data = self.__generate_nmrium_version_3()
else:
dic_data = self.__generate_nmrium_data(version)

json_data = json.dumps(dic_data)

tf_nmrium = tempfile.NamedTemporaryFile(suffix='.nmrium')
tf_nmrium.write(bytes(json_data, 'UTF-8'))
tf_nmrium.seek(0)
return tf_nmrium

def __generate_nmrium_version_3(self):
dic_data = {'actionType': 'INITIATE', 'version': 3}
spectra = self.__generate_nmrim_spectra()
dic_data['spectra'] = spectra
return dic_data

def __generate_nmrium_data(self, version):
dic_data = {'version': version, 'data': {}, 'view': {}}
spectra = self.__generate_nmrim_spectra()
dic_data['data']['spectra'] = spectra
return dic_data

def __generate_nmrim_spectra(self):
spectra = []
Expand Down
9 changes: 9 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
- xz=5.4.2=h5eee18b_0
- zlib=1.2.13=h5eee18b_0
- pip:
- astroid==2.15.8
- atomicwrites==1.3.0
- attrs==23.1.0
- certifi==2023.7.22
Expand All @@ -32,6 +33,7 @@ dependencies:
- contourpy==1.1.1
- coverage==4.5.3
- cycler==0.10.0
- dill==0.3.7
- entrypoints==0.3
- exceptiongroup==1.1.3
- flake8==3.7.7
Expand All @@ -43,9 +45,11 @@ dependencies:
- importlib-metadata==3.6.0
- importlib-resources==5.1.3
- iniconfig==2.0.0
- isort==5.13.2
- itsdangerous==2.0.0
- jinja2==3.1.2
- kiwisolver==1.4.5
- lazy-object-proxy==1.10.0
- markupsafe==2.1.2
- matplotlib==3.7.3
- mccabe==0.6.1
Expand All @@ -57,11 +61,13 @@ dependencies:
- pandas==2.0.3
- pathlib2==2.3.4
- pillow==10.0.1
- platformdirs==4.1.0
- pluggy==0.12.0
- py==1.11.0
- pycodestyle==2.5.0
- pyflakes==2.1.1
- pyjwt==2.8.0
- pylint==2.17.7
- pymzml==2.5.2
- pyopenms==2.6.0
- pyparsing==2.4.2
Expand All @@ -74,9 +80,12 @@ dependencies:
- scipy==1.7.3
- six==1.11.0
- tomli==2.0.1
- tomlkit==0.12.3
- typing-extensions==4.9.0
- tzdata==2023.3
- urllib3==1.26.18
- werkzeug==3.0.1
- wrapt==1.16.0
- zipp==0.5.2
- -e git+https://github.com/ComPlat/nmrglue.git@e6e8a63b1848ae0525f07de0a6ec0cfdb900ba60#egg=nmrglue
prefix: /home/eln/anaconda3/envs/python-v38
5 changes: 5 additions & 0 deletions tests/fixtures/source/nmrium/nmrium_data_schema_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 3,
"actionType": "",
"spectra": []
}
7 changes: 7 additions & 0 deletions tests/fixtures/source/nmrium/nmrium_data_schema_v4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": 4,
"data": {
"spectra": []
},
"view": {}
}
7 changes: 7 additions & 0 deletions tests/fixtures/source/nmrium/nmrium_data_schema_v5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": 5,
"data": {
"spectra": []
},
"view": {}
}
1 change: 1 addition & 0 deletions tests/fixtures/source/nmrium/nmrium_test_v4.nmrium

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/fixtures/source/nmrium/nmrium_test_v5.nmrium

Large diffs are not rendered by default.

71 changes: 69 additions & 2 deletions tests/lib/composer/test_ni_composer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pytest
import numpy as np
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
Expand All @@ -8,6 +9,14 @@
source_nmr_edit = './tests/fixtures/source/1H.edit.jdx'
source_ir = './tests/fixtures/source/IR.dx'

@pytest.fixture
def data_schema():
return [
'./tests/fixtures/source/nmrium/nmrium_data_schema_v3.json',
'./tests/fixtures/source/nmrium/nmrium_data_schema_v4.json',
'./tests/fixtures/source/nmrium/nmrium_data_schema_v5.json'
]

@pytest.fixture
def jcamp_file_1h():
return source_nmr
Expand Down Expand Up @@ -67,6 +76,63 @@ def test_ni_composer_generate_nmrium_not_nmr(jcamp_file_ir):

assert nmrium_file is None

def test_ni_composer_check_schema_v3(data_schema, jcamp_file_1h):
schema_source = data_schema[0]
schema = ''
with open(schema_source, 'r') as f:
schema = f.read()
dic_schema = json.loads(schema)
expected_schema_keys = np.array(dic_schema.keys())

base_converter = JcampBaseConverter(jcamp_file_1h)
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter)
nmrium_file = ni_composer.generate_nmrium(version=3)
with open(nmrium_file.name, 'r') as f:
nmrium = f.read()
nmrium_spectra = json.loads(nmrium)
nmrium_spectra_keys = np.array(nmrium_spectra.keys())
assert np.array_equal(expected_schema_keys, nmrium_spectra_keys)
assert nmrium_spectra['version'] == 3

def test_ni_composer_check_schema_v4(data_schema, jcamp_file_1h):
schema_source = data_schema[1]
schema = ''
with open(schema_source, 'r') as f:
schema = f.read()
dic_schema = json.loads(schema)
expected_schema_keys = np.array(dic_schema.keys())

base_converter = JcampBaseConverter(jcamp_file_1h)
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter)
nmrium_file = ni_composer.generate_nmrium()
with open(nmrium_file.name, 'r') as f:
nmrium = f.read()
nmrium_spectra = json.loads(nmrium)
nmrium_spectra_keys = np.array(nmrium_spectra.keys())
assert np.array_equal(expected_schema_keys, nmrium_spectra_keys)
assert nmrium_spectra['version'] == 4

def test_ni_composer_check_schema_v5(data_schema, jcamp_file_1h):
schema_source = data_schema[2]
schema = ''
with open(schema_source, 'r') as f:
schema = f.read()
dic_schema = json.loads(schema)
expected_schema_keys = np.array(dic_schema.keys())

base_converter = JcampBaseConverter(jcamp_file_1h)
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter)
nmrium_file = ni_composer.generate_nmrium(version=5)
with open(nmrium_file.name, 'r') as f:
nmrium = f.read()
nmrium_spectra = json.loads(nmrium)
nmrium_spectra_keys = np.array(nmrium_spectra.keys())
assert np.array_equal(expected_schema_keys, nmrium_spectra_keys)
assert nmrium_spectra['version'] == 5

def test_ni_composer_nmrium_spectra(jcamp_file_1h):
base_converter = JcampBaseConverter(jcamp_file_1h)
ni_converter = JcampNIConverter(base=base_converter)
Expand Down Expand Up @@ -129,9 +195,10 @@ def test_ni_composer_generate_nmrium(jcamp_file_1h):
assert file_content != ""

dic_content = json.loads(file_content)
assert dic_content['spectra'] is not None
dic_data = dic_content['data']
assert dic_data['spectra'] is not None

xy_points = dic_content['spectra'][0]['data']
xy_points = dic_data['spectra'][0]['data']
x_values = xy_points['x']
y_value = xy_points['re']
assert len(x_values) == 65536
Expand Down
41 changes: 25 additions & 16 deletions tests/lib/converter/nmrium/test_nmrium_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
from chem_spectra.controller.helper.file_container import FileContainer
from chem_spectra.lib.converter.nmrium.base import NMRiumDataConverter

source = './tests/fixtures/source/nmrium/nmrium_test.nmrium'
source_files = [
'./tests/fixtures/source/nmrium/nmrium_test_v3.nmrium',
'./tests/fixtures/source/nmrium/nmrium_test_v4.nmrium',
'./tests/fixtures/source/nmrium/nmrium_test_v5.nmrium'
]

@pytest.fixture
def nmrium_file():
with open(source, 'rb') as f:
nmriumFile = FileContainer(FileStorage(f))
return nmriumFile
def nmrium_files():
files = []
for source in source_files:
with open(source, 'rb') as f:
nmriumFile = FileContainer(FileStorage(f))
files.append(nmriumFile)
return files

@pytest.fixture
def parsed_json_nmrium_data():
Expand Down Expand Up @@ -77,17 +84,19 @@ def test_parsing_spectra(parsed_json_nmrium_data):
assert spectrum_data != None
assert spectrum_data == expected_value

def test_read_file(nmrium_file):
nmrium_converter = NMRiumDataConverter()
nmrium_converter.file = nmrium_file
spectrum_data = nmrium_converter._NMRiumDataConverter__read_file()
assert spectrum_data != None

def test_read_file_content(nmrium_file):
nmrium_converter = NMRiumDataConverter(nmrium_file)
assert nmrium_converter.data != None
assert len(nmrium_converter.data['x']) == 65536
assert len(nmrium_converter.data['x']) == len(nmrium_converter.data['y'])
def test_read_file(nmrium_files):
for file in nmrium_files:
nmrium_converter = NMRiumDataConverter()
nmrium_converter.file = file
spectrum_data = nmrium_converter._NMRiumDataConverter__read_file()
assert spectrum_data is not None

def test_read_file_content(nmrium_files):
for file in nmrium_files:
nmrium_converter = NMRiumDataConverter(file)
assert nmrium_converter.data != None
assert len(nmrium_converter.data['x']) == 65536
assert len(nmrium_converter.data['x']) == len(nmrium_converter.data['y'])

def test_read_xy_values_failed():
nmrium_converter = NMRiumDataConverter()
Expand Down

0 comments on commit f25da45

Please sign in to comment.