Skip to content

Commit 7443634

Browse files
committed
TEST: Test nib-convert functionality
1 parent c79620f commit 7443634

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

nibabel/cmdline/tests/test_convert.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!python
2+
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
5+
#
6+
# See COPYING file distributed along with the NiBabel package for the
7+
# copyright and license terms.
8+
#
9+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
10+
11+
import pytest
12+
13+
import numpy as np
14+
15+
import nibabel as nib
16+
from nibabel.testing import test_data
17+
from nibabel.cmdline import convert
18+
19+
20+
def test_convert_noop(tmp_path):
21+
infile = test_data(fname='anatomical.nii')
22+
outfile = tmp_path / 'output.nii.gz'
23+
24+
orig = nib.load(infile)
25+
assert not outfile.exists()
26+
27+
convert.main([str(infile), str(outfile)])
28+
assert outfile.is_file()
29+
30+
converted = nib.load(outfile)
31+
assert np.allclose(converted.affine, orig.affine)
32+
assert converted.shape == orig.shape
33+
assert converted.get_data_dtype() == orig.get_data_dtype()
34+
35+
with pytest.raises(FileExistsError):
36+
convert.main([str(infile), str(outfile)])
37+
38+
convert.main([str(infile), str(outfile), '--force'])
39+
assert outfile.is_file()
40+
41+
42+
@pytest.mark.parametrize('data_dtype', ('u1', 'i2', 'float32', 'float', 'int64'))
43+
def test_convert_dtype(tmp_path, data_dtype):
44+
infile = test_data(fname='anatomical.nii')
45+
outfile = tmp_path / 'output.nii.gz'
46+
47+
orig = nib.load(infile)
48+
assert not outfile.exists()
49+
50+
# np.dtype() will give us the dtype for the system endianness if that
51+
# mismatches the data file, we will fail equality, so get the dtype that
52+
# matches the requested precision but in the endianness of the file
53+
expected_dtype = np.dtype(data_dtype).newbyteorder(orig.header.endianness)
54+
55+
convert.main([str(infile), str(outfile), '--out-dtype', data_dtype])
56+
assert outfile.is_file()
57+
58+
converted = nib.load(outfile)
59+
assert np.allclose(converted.affine, orig.affine)
60+
assert converted.shape == orig.shape
61+
assert converted.get_data_dtype() == expected_dtype
62+
63+
64+
@pytest.mark.parametrize('ext,img_class', [
65+
('mgh', nib.MGHImage),
66+
('img', nib.Nifti1Pair),
67+
])
68+
def test_convert_by_extension(tmp_path, ext, img_class):
69+
infile = test_data(fname='anatomical.nii')
70+
outfile = tmp_path / f'output.{ext}'
71+
72+
orig = nib.load(infile)
73+
assert not outfile.exists()
74+
75+
convert.main([str(infile), str(outfile)])
76+
assert outfile.is_file()
77+
78+
converted = nib.load(outfile)
79+
assert np.allclose(converted.affine, orig.affine)
80+
assert converted.shape == orig.shape
81+
assert converted.__class__ == img_class
82+
83+
84+
@pytest.mark.parametrize('ext,img_class', [
85+
('mgh', nib.MGHImage),
86+
('img', nib.Nifti1Pair),
87+
('nii', nib.Nifti2Image),
88+
])
89+
def test_convert_imgtype(tmp_path, ext, img_class):
90+
infile = test_data(fname='anatomical.nii')
91+
outfile = tmp_path / f'output.{ext}'
92+
93+
orig = nib.load(infile)
94+
assert not outfile.exists()
95+
96+
convert.main([str(infile), str(outfile), '--image-type', img_class.__name__])
97+
assert outfile.is_file()
98+
99+
converted = nib.load(outfile)
100+
assert np.allclose(converted.affine, orig.affine)
101+
assert converted.shape == orig.shape
102+
assert converted.__class__ == img_class
103+
104+
105+
def test_convert_nifti_int_fail(tmp_path):
106+
infile = test_data(fname='anatomical.nii')
107+
outfile = tmp_path / f'output.nii'
108+
109+
orig = nib.load(infile)
110+
assert not outfile.exists()
111+
112+
with pytest.raises(ValueError):
113+
convert.main([str(infile), str(outfile), '--out-dtype', 'int'])
114+
assert not outfile.exists()
115+
116+
with pytest.warns(UserWarning):
117+
convert.main([str(infile), str(outfile), '--out-dtype', 'int', '--force'])
118+
assert outfile.is_file()
119+
120+
converted = nib.load(outfile)
121+
assert np.allclose(converted.affine, orig.affine)
122+
assert converted.shape == orig.shape
123+
# Note: '--force' ignores the error, but can't interpret it enough to do
124+
# the cast anyway
125+
assert converted.get_data_dtype() == orig.get_data_dtype()
126+
127+
128+
@pytest.mark.parametrize('orig_dtype,alias,expected_dtype', [
129+
('int64', 'mask', 'uint8'),
130+
('int64', 'compat', 'int32'),
131+
('int64', 'smallest', 'uint8'),
132+
('float64', 'mask', 'uint8'),
133+
('float64', 'compat', 'float32'),
134+
])
135+
def test_convert_aliases(tmp_path, orig_dtype, alias, expected_dtype):
136+
orig_fname = tmp_path / 'orig.nii'
137+
out_fname = tmp_path / 'out.nii'
138+
139+
arr = np.arange(24).reshape((2, 3, 4))
140+
img = nib.Nifti1Image(arr, np.eye(4), dtype=orig_dtype)
141+
img.to_filename(orig_fname)
142+
143+
assert orig_fname.exists()
144+
assert not out_fname.exists()
145+
146+
convert.main([str(orig_fname), str(out_fname), '--out-dtype', alias])
147+
assert out_fname.is_file()
148+
149+
expected_dtype = np.dtype(expected_dtype).newbyteorder(img.header.endianness)
150+
151+
converted = nib.load(out_fname)
152+
assert converted.get_data_dtype() == expected_dtype

0 commit comments

Comments
 (0)