|
| 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 | + infile = test_data(fname='resampled_anat_moved.nii') |
| 36 | + |
| 37 | + with pytest.raises(FileExistsError): |
| 38 | + convert.main([str(infile), str(outfile)]) |
| 39 | + |
| 40 | + convert.main([str(infile), str(outfile), '--force']) |
| 41 | + assert outfile.is_file() |
| 42 | + |
| 43 | + # Verify that we did overwrite |
| 44 | + converted2 = nib.load(outfile) |
| 45 | + assert not ( |
| 46 | + converted2.shape == converted.shape |
| 47 | + and np.allclose(converted2.affine, converted.affine) |
| 48 | + and np.allclose(converted2.get_fdata(), converted.get_fdata()) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize('data_dtype', ('u1', 'i2', 'float32', 'float', 'int64')) |
| 53 | +def test_convert_dtype(tmp_path, data_dtype): |
| 54 | + infile = test_data(fname='anatomical.nii') |
| 55 | + outfile = tmp_path / 'output.nii.gz' |
| 56 | + |
| 57 | + orig = nib.load(infile) |
| 58 | + assert not outfile.exists() |
| 59 | + |
| 60 | + # np.dtype() will give us the dtype for the system endianness if that |
| 61 | + # mismatches the data file, we will fail equality, so get the dtype that |
| 62 | + # matches the requested precision but in the endianness of the file |
| 63 | + expected_dtype = np.dtype(data_dtype).newbyteorder(orig.header.endianness) |
| 64 | + |
| 65 | + convert.main([str(infile), str(outfile), '--out-dtype', data_dtype]) |
| 66 | + assert outfile.is_file() |
| 67 | + |
| 68 | + converted = nib.load(outfile) |
| 69 | + assert np.allclose(converted.affine, orig.affine) |
| 70 | + assert converted.shape == orig.shape |
| 71 | + assert converted.get_data_dtype() == expected_dtype |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize('ext,img_class', [ |
| 75 | + ('mgh', nib.MGHImage), |
| 76 | + ('img', nib.Nifti1Pair), |
| 77 | +]) |
| 78 | +def test_convert_by_extension(tmp_path, ext, img_class): |
| 79 | + infile = test_data(fname='anatomical.nii') |
| 80 | + outfile = tmp_path / f'output.{ext}' |
| 81 | + |
| 82 | + orig = nib.load(infile) |
| 83 | + assert not outfile.exists() |
| 84 | + |
| 85 | + convert.main([str(infile), str(outfile)]) |
| 86 | + assert outfile.is_file() |
| 87 | + |
| 88 | + converted = nib.load(outfile) |
| 89 | + assert np.allclose(converted.affine, orig.affine) |
| 90 | + assert converted.shape == orig.shape |
| 91 | + assert converted.__class__ == img_class |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize('ext,img_class', [ |
| 95 | + ('mgh', nib.MGHImage), |
| 96 | + ('img', nib.Nifti1Pair), |
| 97 | + ('nii', nib.Nifti2Image), |
| 98 | +]) |
| 99 | +def test_convert_imgtype(tmp_path, ext, img_class): |
| 100 | + infile = test_data(fname='anatomical.nii') |
| 101 | + outfile = tmp_path / f'output.{ext}' |
| 102 | + |
| 103 | + orig = nib.load(infile) |
| 104 | + assert not outfile.exists() |
| 105 | + |
| 106 | + convert.main([str(infile), str(outfile), '--image-type', img_class.__name__]) |
| 107 | + assert outfile.is_file() |
| 108 | + |
| 109 | + converted = nib.load(outfile) |
| 110 | + assert np.allclose(converted.affine, orig.affine) |
| 111 | + assert converted.shape == orig.shape |
| 112 | + assert converted.__class__ == img_class |
| 113 | + |
| 114 | + |
| 115 | +def test_convert_nifti_int_fail(tmp_path): |
| 116 | + infile = test_data(fname='anatomical.nii') |
| 117 | + outfile = tmp_path / f'output.nii' |
| 118 | + |
| 119 | + orig = nib.load(infile) |
| 120 | + assert not outfile.exists() |
| 121 | + |
| 122 | + with pytest.raises(ValueError): |
| 123 | + convert.main([str(infile), str(outfile), '--out-dtype', 'int']) |
| 124 | + assert not outfile.exists() |
| 125 | + |
| 126 | + with pytest.warns(UserWarning): |
| 127 | + convert.main([str(infile), str(outfile), '--out-dtype', 'int', '--force']) |
| 128 | + assert outfile.is_file() |
| 129 | + |
| 130 | + converted = nib.load(outfile) |
| 131 | + assert np.allclose(converted.affine, orig.affine) |
| 132 | + assert converted.shape == orig.shape |
| 133 | + # Note: '--force' ignores the error, but can't interpret it enough to do |
| 134 | + # the cast anyway |
| 135 | + assert converted.get_data_dtype() == orig.get_data_dtype() |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize('orig_dtype,alias,expected_dtype', [ |
| 139 | + ('int64', 'mask', 'uint8'), |
| 140 | + ('int64', 'compat', 'int32'), |
| 141 | + ('int64', 'smallest', 'uint8'), |
| 142 | + ('float64', 'mask', 'uint8'), |
| 143 | + ('float64', 'compat', 'float32'), |
| 144 | +]) |
| 145 | +def test_convert_aliases(tmp_path, orig_dtype, alias, expected_dtype): |
| 146 | + orig_fname = tmp_path / 'orig.nii' |
| 147 | + out_fname = tmp_path / 'out.nii' |
| 148 | + |
| 149 | + arr = np.arange(24).reshape((2, 3, 4)) |
| 150 | + img = nib.Nifti1Image(arr, np.eye(4), dtype=orig_dtype) |
| 151 | + img.to_filename(orig_fname) |
| 152 | + |
| 153 | + assert orig_fname.exists() |
| 154 | + assert not out_fname.exists() |
| 155 | + |
| 156 | + convert.main([str(orig_fname), str(out_fname), '--out-dtype', alias]) |
| 157 | + assert out_fname.is_file() |
| 158 | + |
| 159 | + expected_dtype = np.dtype(expected_dtype).newbyteorder(img.header.endianness) |
| 160 | + |
| 161 | + converted = nib.load(out_fname) |
| 162 | + assert converted.get_data_dtype() == expected_dtype |
0 commit comments