Skip to content

Commit de4de14

Browse files
committed
ENH(TST): tests for nib-ls (with headers output, stats, multiple files)
1 parent 37be3f8 commit de4de14

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

nibabel/tests/test_scripts.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from .scriptrunner import ScriptRunner
2727
from .nibabel_data import needs_nibabel_data
28-
from ..testing import assert_dt_equal
28+
from ..testing import assert_dt_equal, assert_re_in
2929
from .test_parrec import (DTI_PAR_BVECS, DTI_PAR_BVALS,
3030
EXAMPLE_IMAGES as PARREC_EXAMPLES)
3131
from .test_parrec_data import BALLS, AFF_OFF
@@ -54,15 +54,60 @@ def script_test(func):
5454

5555

5656
@script_test
57-
def test_nib_ls():
57+
def _test_nib_ls_example4d(opts=[], hdrs_str=""):
5858
# test nib-ls script
5959
fname = pjoin(DATA_PATH, 'example4d.nii.gz')
6060
expected_re = (" (int16|[<>]i2) \[128, 96, 24, 2\] "
61-
"2.00x2.00x2.20x2000.00 #exts: 2 sform$")
62-
cmd = ['nib-ls', fname]
61+
"2.00x2.00x2.20x2000.00 #exts: 2%s sform$"
62+
% hdrs_str)
63+
cmd = ['nib-ls'] + opts + [fname]
6364
code, stdout, stderr = run_command(cmd)
6465
assert_equal(fname, stdout[:len(fname)])
65-
assert_not_equal(re.match(expected_re, stdout[len(fname):]), None)
66+
assert_re_in(expected_re, stdout[len(fname):])
67+
68+
@script_test
69+
def test_nib_ls():
70+
yield _test_nib_ls_example4d
71+
yield _test_nib_ls_example4d, ['-H', 'dim,bitpix'], " \[ 4 128 96 24 2 1 1 1\] 16"
72+
73+
@script_test
74+
def test_nib_ls_multiple():
75+
# verify that correctly lists/formats for multiple files
76+
fnames = [
77+
pjoin(DATA_PATH, f)
78+
for f in ('example4d.nii.gz', 'example_nifti2.nii.gz', 'small.mnc', 'nifti2.hdr')
79+
]
80+
code, stdout, stderr = run_command(['nib-ls'] + fnames)
81+
stdout_lines = stdout.split('\n')
82+
assert_equal(len(stdout_lines), 4)
83+
# they should be indented correctly. Since all files are int type -
84+
ln = max(len(f) for f in fnames)
85+
assert_equal([l[ln:ln+2] for l in stdout_lines], [' i']*4)
86+
# and if disregard type indicator which might vary
87+
assert_equal(
88+
[l[l.index('['):] for l in stdout_lines],
89+
[
90+
'[128, 96, 24, 2] 2.00x2.00x2.20x2000.00 #exts: 2 sform',
91+
'[ 32, 20, 12, 2] 2.00x2.00x2.20x2000.00 #exts: 2 sform',
92+
'[ 18, 28, 29] 9.00x8.00x7.00',
93+
'[ 91, 109, 91] 2.00x2.00x2.00'
94+
]
95+
)
96+
97+
# Now run with -s for stats
98+
code, stdout, stderr = run_command(['nib-ls', '-s'] + fnames)
99+
stdout_lines = stdout.split('\n')
100+
assert_equal(len(stdout_lines), 4)
101+
assert_equal(
102+
[l[l.index('['):] for l in stdout_lines],
103+
[
104+
'[128, 96, 24, 2] 2.00x2.00x2.20x2000.00 #exts: 2 sform [229725] 2:1.2e+03',
105+
'[ 32, 20, 12, 2] 2.00x2.00x2.20x2000.00 #exts: 2 sform [15360] 46:7.6e+02',
106+
'[ 18, 28, 29] 9.00x8.00x7.00 [14616] 0.12:93',
107+
'[ 91, 109, 91] 2.00x2.00x2.00 error'
108+
]
109+
)
110+
66111

67112

68113
@script_test

0 commit comments

Comments
 (0)