Skip to content

Commit

Permalink
feat: update saving axes labels function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Mar 6, 2024
1 parent dfb1367 commit d0cb921
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 4 deletions.
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
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.1',
version='1.2.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
Expand Down
219 changes: 218 additions & 1 deletion tests/lib/converter/test_share.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,226 @@
import numpy as np
import pytest
from chem_spectra.lib.converter.share import (
parse_solvent, reduce_pts
parse_params, parse_solvent, reduce_pts
)

@pytest.fixture
def expected_default_params():
default_itg = {'stack': [], 'refArea': 1, 'refFactor': 1, 'shift': 0}
default_mpy = {'stack': [], 'smExtext': False, 'shift': 0}
default_wavelength = {'name': 'CuKalpha', 'value': 0.15406, 'label': 'Cu K-alpha', 'unit': 'nm'}
return {
'select_x': None,
'ref_name': None,
'ref_value': None,
'peaks_str': None,
'delta': 0.0,
'mass': 0,
'scan': None,
'thres': None,
'clear': False,
'integration': default_itg,
'multiplicity': default_mpy,
'fname': '',
'waveLength': default_wavelength,
'list_max_min_peaks': None,
'cyclicvolta': None,
'jcamp_idx': 0,
'axesUnits': None,
'detector': None,
}

def test_parse_params_without_params(expected_default_params):
parsed_data = parse_params(None)
assert expected_default_params == parsed_data

def test_parse_params_select_x():
params = {'select_x': None}
parsed_data = parse_params(params)
assert parsed_data['select_x'] is None

params = {'select_x': 0.0}
parsed_data = parse_params(params)
assert parsed_data['select_x'] == 0.0

def test_parse_params_ref_name():
params = {'ref_name': None}
parsed_data = parse_params(params)
assert parsed_data['ref_name'] is None

params = {'ref_name': 'just a text'}
parsed_data = parse_params(params)
assert parsed_data['ref_name'] == 'just a text'

def test_parse_params_ref_value():
params = {'ref_value': None}
parsed_data = parse_params(params)
assert parsed_data['ref_value'] is None

params = {'ref_value': 0.0}
parsed_data = parse_params(params)
assert parsed_data['ref_value'] == 0.0

def test_parse_params_peaks_str():
params = {'peaks_str': None}
parsed_data = parse_params(params)
assert parsed_data['peaks_str'] is None

params = {'peaks_str': 'just a text'}
parsed_data = parse_params(params)
assert parsed_data['peaks_str'] == 'just a text'

def test_parse_params_delta():
params = {'select_x': None}
parsed_data = parse_params(params)
assert parsed_data['delta'] == 0.0

params = {'select_x': 0.0}
parsed_data = parse_params(params)
assert parsed_data['delta'] == 0.0

params = {'select_x': 1.0, 'ref_name': '- - -'}
parsed_data = parse_params(params)
assert parsed_data['delta'] == 0.0

params = {'select_x': 1.0, 'ref_name': 'just a text'}
parsed_data = parse_params(params)
assert parsed_data['delta'] == 0.0

params = {'select_x': 1.0, 'ref_name': 'just a text', 'ref_value': 1.5}
parsed_data = parse_params(params)
assert parsed_data['delta'] == 0.5

def test_parse_params_mass():
params = {'mass': None}
parsed_data = parse_params(params)
assert parsed_data['mass'] == 0

params = {'mass': 1.5}
parsed_data = parse_params(params)
assert parsed_data['mass'] == 1.5

def test_parse_params_scan():
params = {'scan': None}
parsed_data = parse_params(params)
assert parsed_data['scan'] is None

params = {'scan': 2}
parsed_data = parse_params(params)
assert parsed_data['scan'] == 2

def test_parse_params_thres():
params = {'thres': None}
parsed_data = parse_params(params)
assert parsed_data['thres'] is None

params = {'thres': 2}
parsed_data = parse_params(params)
assert parsed_data['thres'] == 2

def test_parse_params_clear():
params = {'clear': None}
parsed_data = parse_params(params)
assert parsed_data['clear'] == False

params = {'clear': False}
parsed_data = parse_params(params)
assert parsed_data['clear'] == False

params = {'clear': True}
parsed_data = parse_params(params)
assert parsed_data['clear'] == True

def test_parse_params_ext():
params = {'ext': None}
parsed_data = parse_params(params)
assert parsed_data['ext'] == ''

params = {'ext': '.jdx'}
parsed_data = parse_params(params)
assert parsed_data['ext'] == '.jdx'

def test_parse_params_fname():
params = {'fname': 'just a normal text'}
parsed_data = parse_params(params)
assert parsed_data['fname'] == ''

params = {'fname': 'original.jdx'}
parsed_data = parse_params(params)
assert parsed_data['fname'] == 'original'

params = {'fname': 'original.changed.jdx'}
parsed_data = parse_params(params)
assert parsed_data['fname'] == 'original.changed'

params = {'fname': 'original.peak.jdx'}
parsed_data = parse_params(params)
assert parsed_data['fname'] == 'original'

params = {'fname': 'original.edit.jdx'}
parsed_data = parse_params(params)
assert parsed_data['fname'] == 'original'

def test_parse_params_integration():
#TODO: need to be updated
assert 1==1

def test_parse_params_multiplicity():
#TODO: need to be updated
assert 1==1

def test_parse_params_waveLength():
#TODO: need to be updated
assert 1==1

def test_parse_params_list_max_min_peaks():
#TODO: need to be updated
assert 1==1

def test_parse_params_cyclic_volta():
#TODO: need to be updated
assert 1==1

def test_parse_params_jcamp_idx():
params = {'jcamp_idx': None}
parsed_data = parse_params(params)
assert parsed_data['jcamp_idx'] == 0

params = {'jcamp_idx': 1}
parsed_data = parse_params(params)
assert parsed_data['jcamp_idx'] == 1

def test_parse_params_axesUnits():
params = {'axesUnits': None}
parsed_data = parse_params(params)
assert parsed_data['axesUnits'] is None

params = {'axesUnits': '{"axes": null}'}
parsed_data = parse_params(params)
assert parsed_data['axesUnits'] is None

params = {'axesUnits': '{"axes": []}'}
parsed_data = parse_params(params)
assert parsed_data['axesUnits'] is None

params = {'jcamp_idx': 1, 'axesUnits': '{"axes": [{"xUnit": "label x", "yUnit": "label y"}]}'}
parsed_data = parse_params(params)
assert parsed_data['axesUnits'] is None

params = {'axesUnits': '{"axes": [{"xUnit": "label x", "yUnit": "label y"}]}'}
parsed_data = parse_params(params)
assert parsed_data['axesUnits'] == {"xUnit": "label x", "yUnit": "label y"}

def test_parse_params_data_type_mapping():
#TODO: need to be updated
assert 1==1

def test_parse_params_detector():
#TODO: need to be updated
assert 1==1

def test_parse_solvent():
#TODO: need to be updated
assert 1==1

def test_reduce_pts_when_does_not_have_any_x():
Expand Down

0 comments on commit d0cb921

Please sign in to comment.