Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c80988b

Browse files
committed
Replaced hard-coded paths in tests/setup.py
Now using os.path functions and the __file__ variable to work out the location of DATA_DIR instead of hard-coding it
1 parent a998c1d commit c80988b

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

tests/setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
""" nmrglue test setup module """
22
# location of the data directory
3-
DATA_DIR = "/home/jhelmus/code/python/nmrglue/data/"
3+
import os.path
4+
TESTS_DIR = os.path.dirname(__file__)
5+
NMRGLUE_ROOT = os.path.dirname(TESTS_DIR)
6+
DATA_DIR = os.path.join(NMRGLUE_ROOT, 'data')

tests/test_simpson.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
import nmrglue.fileio.simpson as simpson
33
import numpy as np
44
from numpy.testing import assert_allclose, assert_raises
5+
import os.path
56

67
from setup import DATA_DIR
78

8-
DD_1D = DATA_DIR + 'simpson_1d/'
9-
DD_2D = DATA_DIR + 'simpson_2d/'
9+
DD_1D = os.path.join(DATA_DIR, 'simpson_1d')
10+
DD_2D = os.path.join(DATA_DIR, 'simpson_2d')
1011

1112
def test_1d_time():
1213
""" reading 1D time domain files """
1314
# read the text, binary, xreim, and rawbin data
14-
text_dic, text_data = simpson.read(DD_1D + '1d_text.fid')
15-
bin_dic, bin_data = simpson.read(DD_1D + '1d_bin.fid')
16-
xreim_units, xreim_data = simpson.read(DD_1D + '1d_ftext.fid')
17-
rd, rawbin_data = simpson.read(DD_1D + '1d_rawbin.fid', spe=False, ndim=1)
15+
text_dic, text_data = simpson.read(os.path.join(DD_1D, '1d_text.fid'))
16+
bin_dic, bin_data = simpson.read(os.path.join(DD_1D, '1d_bin.fid'))
17+
xreim_units, xreim_data = simpson.read(os.path.join(DD_1D, '1d_ftext.fid'))
18+
rd, rawbin_data = simpson.read(os.path.join(DD_1D, '1d_rawbin.fid'), spe=False, ndim=1)
1819

1920
# check data in text file
2021
assert text_data.shape == (4096, )
@@ -33,10 +34,10 @@ def test_1d_time():
3334
def test_1d_freq():
3435
""" reading 1D freq domain files """
3536
# read the text, binary, xreim, and rawbin data
36-
text_dic, text_data = simpson.read(DD_1D + '1d_text.spe')
37-
bin_dic, bin_data = simpson.read(DD_1D + '1d_bin.spe')
38-
xreim_units, xreim_data = simpson.read(DD_1D + '1d_ftext.spe')
39-
rd, rawbin_data = simpson.read(DD_1D + '1d_rawbin.spe', spe=True, ndim=1)
37+
text_dic, text_data = simpson.read(os.path.join(DD_1D, '1d_text.spe'))
38+
bin_dic, bin_data = simpson.read(os.path.join(DD_1D, '1d_bin.spe'))
39+
xreim_units, xreim_data = simpson.read(os.path.join(DD_1D, '1d_ftext.spe'))
40+
rd, rawbin_data = simpson.read(os.path.join(DD_1D, '1d_rawbin.spe'), spe=True, ndim=1)
4041

4142
# check data in text file
4243
assert text_data.shape == (4096, )
@@ -55,10 +56,10 @@ def test_1d_freq():
5556
def test_2d_time():
5657
""" reading 2D time domain files """
5758
# read the text, binary, xreim, and rawbin data
58-
text_dic, text_data = simpson.read(DD_2D + '2d_text.fid')
59-
bin_dic, bin_data = simpson.read(DD_2D + '2d.fid')
60-
xyreim_units, xyreim_data = simpson.read(DD_2D + '2d_ftext.fid')
61-
rd, rawbin_data = simpson.read(DD_2D + '2d_raw.fid', NP=128, NI=48, ndim=2,
59+
text_dic, text_data = simpson.read(os.path.join(DD_2D, '2d_text.fid'))
60+
bin_dic, bin_data = simpson.read(os.path.join(DD_2D, '2d.fid'))
61+
xyreim_units, xyreim_data = simpson.read(os.path.join(DD_2D, '2d_ftext.fid'))
62+
rd, rawbin_data = simpson.read(os.path.join(DD_2D, '2d_raw.fid'), NP=128, NI=48, ndim=2,
6263
spe=False)
6364

6465
# check data in text file
@@ -79,10 +80,10 @@ def test_2d_time():
7980
def test_2d_freq():
8081
""" reading 2D freq domain files """
8182
# read the text, binary, xreim, and rawbin data
82-
text_dic, text_data = simpson.read(DD_2D + '2d_text.spe')
83-
bin_dic, bin_data = simpson.read(DD_2D + '2d.spe')
84-
xyreim_units, xyreim_data = simpson.read(DD_2D + '2d_ftext.spe')
85-
rd, rawbin_data = simpson.read(DD_2D + '2d_raw.spe', ndim=2, NP=256,
83+
text_dic, text_data = simpson.read(os.path.join(DD_2D, '2d_text.spe'))
84+
bin_dic, bin_data = simpson.read(os.path.join(DD_2D, '2d.spe'))
85+
xyreim_units, xyreim_data = simpson.read(os.path.join(DD_2D, '2d_ftext.spe'))
86+
rd, rawbin_data = simpson.read(os.path.join(DD_2D, '2d_raw.spe'), ndim=2, NP=256,
8687
NI=512, spe=True)
8788

8889
# check data in text file
@@ -106,14 +107,14 @@ def test_exceptions_read():
106107
""" raising exceptions due to missing read parameters """
107108

108109
# missing spe parameter
109-
assert_raises(ValueError, simpson.read, DD_1D + '1d_rawbin.fid')
110+
assert_raises(ValueError, simpson.read, os.path.join(DD_1D, '1d_rawbin.fid'))
110111

111112
# missing ndim parameter
112-
assert_raises(ValueError, simpson.read, DD_1D + '1d_rawbin.fid', spe=False)
113+
assert_raises(ValueError, simpson.read, os.path.join(DD_1D, '1d_rawbin.fid'), spe=False)
113114

114115
# missing NP/NI parameter
115-
assert_raises(ValueError, simpson.read, DD_2D + '2d_raw.fid', spe=False,
116+
assert_raises(ValueError, simpson.read, os.path.join(DD_2D, '2d_raw.fid'), spe=False,
116117
ndim=2)
117118

118119
# bad ftype
119-
assert_raises(ValueError, simpson.read, DD_1D + '1d_rawbin.fid', ftype='a')
120+
assert_raises(ValueError, simpson.read, os.path.join(DD_1D, '1d_rawbin.fid'), ftype='a')

tests/test_sparky.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import tempfile
44
import os
5+
import os.path
56

67
from numpy.testing import assert_array_equal
78
import nmrglue as ng
@@ -44,31 +45,31 @@ def lowmem_write_readback(dic, data):
4445
# tests
4546
def test_2d():
4647
""" reading/writing of 2D sparky file """
47-
dic, data = ng.sparky.read(DATA_DIR + "sparky_2d/data.ucsf")
48+
dic, data = ng.sparky.read(os.path.join(DATA_DIR, "sparky_2d", "data.ucsf"))
4849
assert data.shape == (2048, 4096)
4950
assert round(data[0, 1], 2) == 1601.83
5051
assert round(data[15, 20], 2) == 4281.06
5152
write_readback(dic, data)
5253

5354
def test_2d_lowmem():
5455
""" lowmemory reading/writing of 2D sparky file """
55-
dic, data = ng.sparky.read_lowmem(DATA_DIR + "sparky_2d/data.ucsf")
56+
dic, data = ng.sparky.read_lowmem(os.path.join(DATA_DIR, "sparky_2d", "data.ucsf")
5657
assert data.shape == (2048, 4096)
5758
assert round(data[0, 1], 2) == 1601.83
5859
assert round(data[15, 20], 2) == 4281.06
5960
lowmem_write_readback(dic, data)
6061

6162
def test_3d():
6263
""" reading/writing of 3D sparky file """
63-
dic, data = ng.sparky.read(DATA_DIR + "sparky_3d/data.ucsf")
64+
dic, data = ng.sparky.read(os.path.join(DATA_DIR, "sparky_3d", "data.ucsf")
6465
assert data.shape == (128, 128, 4096)
6566
assert round(data[0, 1, 2], 2) == 25980.13
6667
assert round(data[11, 15, 20], 2) == -15256.97
6768
write_readback(dic, data)
6869

6970
def test_3d_lowmem():
7071
""" lowmemory reading/writing of 3D sparky file """
71-
dic, data = ng.sparky.read_lowmem(DATA_DIR + "sparky_3d/data.ucsf")
72+
dic, data = ng.sparky.read_lowmem(os.path.join(DATA_DIR, "sparky_3d", "data.ucsf")
7273
assert data.shape == (128, 128, 4096)
7374
assert round(data[0, 1, 2], 2) == 25980.13
7475
assert round(data[11, 15, 20], 2) == -15256.97

0 commit comments

Comments
 (0)