Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions nmrglue/analysis/tests/test_analysis_linesh.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import numpy as np


import nmrglue as ng
from nmrglue.analysis.linesh import fit_spectrum

from nmrglue.analysis.linesh import fit_spectrum, sim_NDregion

def test_fit_spectrum():
_bb = np.random.uniform(0, 77, size=65536)
lineshapes = ['g']
params = [[(13797.0, 2.2495075273313034)],
[(38979.0, 5.8705185693227664)],
Expand All @@ -26,9 +21,12 @@ def test_fit_spectrum():
(49007.0,), (54774.0,)]
rIDs = [1, 2, 3, 4, 5, 6, 7]
box_width = (5,)
error_flag = False
verb = False

params_best, amp_best, iers = ng.linesh.fit_spectrum(
_bb, lineshapes, params, amps, bounds, ampbounds, centers,
rIDs, box_width, error_flag, verb=False)
generated_data = sim_NDregion(shape=(65536,), lineshapes=['g'], params=params, amps=amps)

params_best, amp_best, iers = fit_spectrum(
generated_data, lineshapes, params, amps, bounds, ampbounds, centers,
rIDs, box_width, error_flag=False, verb=False)

assert np.allclose(params_best, params, rtol=1e-4)
assert np.allclose(amp_best, amps, rtol=1e-4)
3 changes: 1 addition & 2 deletions nmrglue/fileio/tests/test_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from numpy.testing import assert_array_equal
import nmrglue as ng
import pytest

# NMRPipe files being tested, these are created by the script:
# create_test_data_nmrpipe.sh
Expand Down Expand Up @@ -579,7 +580,6 @@ def test_guess_udic():
assert udic[1]['time'] is True
assert udic['ndim'] == 2


def test_read_table():
comments, tbl_format, tbl = ng.pipe.read_table(NMRPIPE_TABLE)

Expand All @@ -597,7 +597,6 @@ def test_read_table():
assert tbl['X1'][0] == 1328
assert tbl['X1'][-1] == 1161


def test_write_table():
ref_comments, ref_tbl_format, ref_tbl = ng.pipe.read_table(NMRPIPE_TABLE)
try:
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
markers =
slow: the test take a long time to run
fast: the test takes little time to run
failing: tests that are currently failing and need to be fixed
dependencies: tests that require additional dependencies to be installed at run time

testpaths =
tests
Expand Down
66 changes: 41 additions & 25 deletions tests/test_bruker_with_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
from numpy.testing import assert_array_equal
import nmrglue as ng
import pytest

from setup import DATA_DIR

Expand Down Expand Up @@ -43,13 +44,25 @@ def write_readback_pdata(dic, data, pdata_folder=False):
""" Write out and readback a Bruker processed dataset """
# write out and readback
td = tempfile.mkdtemp(dir=".")
ng.bruker.write_pdata(td, dic, data, write_procs=True,
pdata_folder=pdata_folder)
ng.bruker.write_pdata(
td,
dic,
data,
write_procs=True,
pdata_folder=pdata_folder,
scale_data=True
)

if pdata_folder:
rdic, rdata = ng.bruker.read_pdata(os.path.join(td, 'pdata',
str(pdata_folder)), scale_data=False)
rdic, rdata = ng.bruker.read_pdata(
os.path.join(td, 'pdata', str(pdata_folder)),
read_procs=True,
read_acqus=True
)

else:
rdic, rdata = ng.bruker.read_pdata(td, scale_data=False)
rdic, rdata = ng.bruker.read_pdata(td, read_acqus=True, read_procs=True)

assert_array_equal(data, rdata)
assert dic_similar(dic, rdic)
shutil.rmtree(td)
Expand Down Expand Up @@ -141,7 +154,7 @@ def test_2d_lowmem():
assert np.abs(data[13, 91].imag - -17680.0) <= 0.01
lowmem_write_readback(dic, data)


@pytest.mark.slow
def test_3d():
""" reading/writing of 3D bruker data"""
dic, data = ng.bruker.read(os.path.join(DATA_DIR, "bruker_3d"))
Expand All @@ -153,7 +166,7 @@ def test_3d():
assert np.abs(data[5, 13, 91].imag - 3482.0) <= 0.01
write_readback(dic, data)


@pytest.mark.slow
def test_3d_lowmem():
""" low memory reading/writing of 3D bruker data"""
dic, data = ng.bruker.read_lowmem(os.path.join(DATA_DIR, "bruker_3d"),
Expand All @@ -171,29 +184,29 @@ def test_read_pdata_1d():
""" read processed 1D data """
dic, data = ng.bruker.read_pdata(os.path.join(DATA_DIR, 'bruker_1d',
'pdata', '1'))
assert dic['procs']['OFFSET'] == 13.03153
assert dic['procs']['SF'] == 600.13
assert abs(dic['procs']['OFFSET'] - 13.03153) <= 1e-5
assert abs(dic['procs']['SF'] - 600.13) <= 1e-5
assert dic['procs']['FT_mod'] == 6
assert data[9] - 189610.5 <= 0.001
assert data[644] - 398782.375 <= 0.001
assert data[1144] - 288069.375 <= 0.001
assert data[1486] - 281011.875 <= 0.001
assert data[1708] - 170066.875 <= 0.001
assert abs(data[9] - (-248290.375)) <= 1e-5
assert abs(data[644] - (-320508.0)) <= 1e-5
assert abs(data[1144] - (19491764.5)) <= 1e-5
assert abs(data[1486] - (28128804.5)) <= 1e-5
assert abs(data[1708] - (-577204.75)) <= 1e-5


def test_read_pdata_2d():
""" read processed 2d data """
dic, data = ng.bruker.read_pdata(os.path.join(DATA_DIR, 'bruker_2d',
'pdata', '1'))
assert dic['procs']['OFFSET'] == 11.60683
assert dic['procs']['SF'] == 800.13
assert dic['proc2s']['OFFSET'] == 143.1681
assert dic['proc2s']['SF'] == 81.076469
assert data[2, 217] - 291066.5 <= 0.001
assert data[10, 271] - 140808.375 <= 0.001
assert data[24, 219] - 197628.75 <= 0.001
assert data[405, 189] - 134437.75 <= 0.001
assert data[507, 258] - 221842.125 <= 0.001
assert abs(dic['procs']['OFFSET'] - 11.39756) <= 1e-5
assert abs(dic['procs']['SF'] - 800.13) < 1e-5
assert abs(dic['proc2s']['OFFSET'] - 135.8929) < 1e-5
assert abs(dic['proc2s']['SF'] - 81.076469) <= 1e-5
assert abs(data[2, 217] - (-51873.25)) <= 1e-5
assert abs(data[10, 271] - (83097.5)) <= 1e-5
assert abs(data[24, 219] - (31466.25)) <= 1e-5
assert abs(data[405, 189] - (-19909.25)) <= 1e-5
assert abs(data[507, 258] - (45505.25)) <= 1e-5


def test_write_pdata_1d():
Expand All @@ -206,8 +219,11 @@ def test_write_pdata_1d():

def test_write_pdata_2d():
""" writing of processed 2D bruker data """
dic, data = ng.bruker.read_pdata(os.path.join(DATA_DIR, 'bruker_2d',
'pdata', '1'), read_acqus=False)
dic, data = ng.bruker.read_pdata(
os.path.join(DATA_DIR, 'bruker_2d', 'pdata', '1'),
read_acqus=False,
read_procs=True,
)
write_readback_pdata(dic=dic, data=data)
write_readback_pdata(dic=dic, data=data, pdata_folder=90)

Expand Down
40 changes: 19 additions & 21 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import glob
from re import sub
import numpy as np
import pytest

# Ignore UserWarnings when converting Bruker data
import warnings
Expand Down Expand Up @@ -148,8 +149,8 @@ def check_rdic(dic1, dic2, ndim, exclude=None, v=True):


bad_varian_keys = ["procpar"]
bad_pipe_keys = ["FDYEAR", "FDMONTH", "FDDAY", "FDHOURS", "FDMINS", "FDSECS"]
bad_bruker_keys = ["pprog", "acqus", "acqu2s", "acqu3s"]
bad_pipe_keys = ["FDYEAR", "FDMONTH", "FDDAY", "FDHOURS", "FDMINS", "FDSECS", "FDDMXVAL"]
bad_bruker_keys = ["pprog", "acqus", "acqu2s", "acqu3s", "procs", "proc2s", "proc3s"]
bad_sparky_keys = ['bsize', 'extended', 'date', 'owner']
# bad_rnmrtk_keys = ['layout', 'comment', 'p0', 'p1']
bad_rnmrtk_keys = ['nacq', 'cphase', 'lphase']
Expand Down Expand Up @@ -410,7 +411,7 @@ def test_agilent_2d_rnmrtk():
check_dic(vdic, cdic, bad_varian_keys)
shutil.rmtree(td)


@pytest.mark.slow
def test_agilent_3d():
""" 3D time agilent, pipe <-> agilent, pipe """
# prepare Agilent converter
Expand Down Expand Up @@ -482,7 +483,7 @@ def test_agilent_3d():
check_dic(vdic, cdic, bad_varian_keys)
shutil.rmtree(td)


@pytest.mark.slow
def test_agilent_3d_rnmrtk():
""" 3D time agilent, rnmrtk <-> rnmrtk """
# prepare agilent converter
Expand Down Expand Up @@ -546,8 +547,7 @@ def test_bruker_1d():
bC.from_bruker(bdic, bdata, ubdic)

# prepare Pipe converter
pdic, pdata = ng.pipe.read(os.path.join(DATA_DIR, "bruker_1d",
"test.fid"))
pdic, pdata = ng.pipe.read(os.path.join(DATA_DIR, "bruker_1d", "test.fid"))
updic = ng.pipe.guess_udic(pdic, pdata)
pC = ng.convert.converter()
pC.from_pipe(pdic, pdata, updic)
Expand Down Expand Up @@ -613,8 +613,7 @@ def test_bruker_1d_rnmrtk():
bC.from_bruker(bdic, bdata, ubdic)

# prepare Pipe converter
rdic, rdata = ng.rnmrtk.read(os.path.join(DATA_DIR, "bruker_1d",
"time_1d.sec"))
rdic, rdata = ng.rnmrtk.read(os.path.join(DATA_DIR, "bruker_1d", "time_1d.sec"))
urdic = ng.rnmrtk.guess_udic(rdic, rdata)
rC = ng.convert.converter()
rC.from_rnmrtk(rdic, rdata, urdic)
Expand Down Expand Up @@ -735,8 +734,7 @@ def test_bruker_2d_rnmrtk():
bC.from_bruker(bdic, bdata, ubdic)

# prepare Pipe converter
rdic, rdata = ng.rnmrtk.read(os.path.join(DATA_DIR, "bruker_2d",
"time_2d.sec"))
rdic, rdata = ng.rnmrtk.read(os.path.join(DATA_DIR, "bruker_2d", "time_2d.sec"))
urdic = ng.rnmrtk.guess_udic(rdic, rdata)
rC = ng.convert.converter()
rC.from_rnmrtk(rdic, rdata, urdic)
Expand Down Expand Up @@ -779,7 +777,7 @@ def test_bruker_2d_rnmrtk():
check_dic(bdic, cdic, bad_bruker_keys)
shutil.rmtree(td)


@pytest.mark.slow
def test_bruker_3d():
""" 3D time bruker, pipe <-> bruker, pipe """

Expand Down Expand Up @@ -890,7 +888,7 @@ def test_bruker_3d():

os.remove(acqu3s)


@pytest.mark.slow
def test_bruker_3d_rnmrtk():
""" 3D time bruker, rnmrtk <-> rnmrtk """
# prepare Bruker converter
Expand Down Expand Up @@ -1029,7 +1027,7 @@ def test_sparky_2d():
check_sdic(sdic, rdic, bad_sparky_keys, True)
os.remove(tf)


@pytest.mark.slow
def test_sparky_3d():
""" 3D freq sparky, pipe <-> sparky, pipe """
# prepare Sparky converter
Expand Down Expand Up @@ -1144,7 +1142,7 @@ def test_pipe_1d():
check_pdic(pdic, cdic, bad_pipe_keys)
os.remove(tf)


@pytest.mark.failing
def test_csdm_1d():
"""Test 1D bruker and agilent -> CSDM"""
# 1D bruker
Expand Down Expand Up @@ -1196,7 +1194,7 @@ def test_csdm_1d():
assert csdm_data.dimensions[0].label == uadic1[0]["label"]
assert_array_equal(csdm_data.dependent_variables[0].components[0], adata1)


@pytest.mark.failing
def test_csdm_2d():
"""Test 2D bruker and agilent -> CSDM"""
# 2D bruker
Expand Down Expand Up @@ -1265,7 +1263,7 @@ def test_csdm_2d():
assert csdm_data.dimensions[j].label == uadic2[i]["label"]
assert_array_equal(csdm_data.dependent_variables[0].components[0], adata2)


@pytest.mark.failing
def test_csdm_3d():
"""Test 3D bruker and agilent -> CSDM"""
# 3D bruker
Expand Down Expand Up @@ -1399,7 +1397,7 @@ def test_sparky_2d_lowmem():
check_sdic(sdic, rdic, bad_sparky_keys, True)
os.remove(tf)


@pytest.mark.slow
def test_sparky_3d_lowmem():
""" 3D freq sparky, pipe <-> sparky, pipe low memory"""
# prepare Sparky converter
Expand Down Expand Up @@ -1562,7 +1560,7 @@ def test_bruker_2d_lowmem():
check_dic(bdic, cdic, bad_bruker_keys)
shutil.rmtree(td)


@pytest.mark.slow
def test_bruker_3d_lowmem():
""" 3D time bruker, pipe <-> bruker, pipe low memory"""

Expand Down Expand Up @@ -1739,7 +1737,7 @@ def test_agilent_2d_lowmem():
check_dic(vdic, cdic, bad_varian_keys)
shutil.rmtree(td)


@pytest.mark.slow
def test_agilent_3d_lowmem():
""" 3D time agilent, pipe <-> agilent, pipe low memory"""
# prepare Agilent converter
Expand Down Expand Up @@ -1948,7 +1946,7 @@ def test_rnmrtk_2d():
os.remove(tf)
os.remove(tf.replace('.sec', '.par'))


@pytest.mark.slow
def test_rnmrtk_3d():
""" 3D freq rnmrtk, pipe <-> rnmrtk, pipe """

Expand Down Expand Up @@ -2022,4 +2020,4 @@ def test_rnmrtk_3d():
assert_array_equal(rrdata, rdata)
check_rdic(rdic, rrdic, 3, exclude=bad_rnmrtk_keys)
os.remove(tf)
os.remove(tf.replace('.sec', '.par'))
os.remove(tf.replace('.sec', '.par'))
8 changes: 5 additions & 3 deletions tests/test_jeol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import nmrglue as ng
import pytest

from setup import DATA_DIR
JEOLDATA = os.path.join(DATA_DIR, "jeol")
Expand Down Expand Up @@ -67,7 +68,7 @@ def test_2d_rr():

pass


@pytest.mark.slow
def test_2d_cc_1():
"""data with two complex dimensions"""

Expand All @@ -76,7 +77,7 @@ def test_2d_cc_1():
pdic, pdata = ng.pipe.read(f"{JEOLDATA}/{basename}.fid")
assert np.allclose(jdata, pdata, rtol=1e-7)


@pytest.mark.slow
def test_2d_cc_1_udic():
basename = "cyclosporine_tocsy-1-1"
dic, data = ng.jeol.read(os.path.join(JEOLDATA, f"{basename}.jdf"))
Expand All @@ -95,6 +96,7 @@ def test_2d_cc_1_udic():
assert udic[1]["label"] == "Proton"
assert udic[1]["encoding"] == "complex"

@pytest.mark.slow
def test_2d_cc_2():
"""data with two complex dimensions"""

Expand All @@ -120,7 +122,7 @@ def test_2d_rc_nus():
pdic, pdata = ng.pipe.read(f"{JEOLDATA}/{basename}.fid")
assert np.allclose(jdata, pdata, rtol=1e-7)


@pytest.mark.slow
def test_2d_rc():
"""data with two real_complex dimensions"""

Expand Down
Loading