|
5 | 5 | from nibabel.testing import data_path
|
6 | 6 |
|
7 | 7 | import unittest
|
| 8 | +from unittest import mock |
8 | 9 | import pytest
|
9 | 10 |
|
10 | 11 |
|
@@ -78,6 +79,23 @@ def test_lossless_slice_scaling(tmp_path):
|
78 | 79 | assert img1.dataobj.inter == img2.dataobj.inter
|
79 | 80 |
|
80 | 81 |
|
| 82 | +def test_lossless_slice_noscaling(tmp_path): |
| 83 | + fname = tmp_path / 'image.img' |
| 84 | + img = nb.AnalyzeImage(np.random.uniform(-20000, 20000, (5, 5, 5, 5)).astype("float32"), |
| 85 | + affine=np.eye(4)) |
| 86 | + img.header.set_data_dtype("float32") |
| 87 | + img.to_filename(fname) |
| 88 | + img1 = nb.load(fname) |
| 89 | + sliced_fname = tmp_path / 'sliced.img' |
| 90 | + lossless_slice(img1, (slice(None), slice(None), slice(2, 4))).to_filename(sliced_fname) |
| 91 | + img2 = nb.load(sliced_fname) |
| 92 | + |
| 93 | + assert np.array_equal(img1.get_fdata()[:, :, 2:4], img2.get_fdata()) |
| 94 | + assert np.array_equal(img1.dataobj.get_unscaled()[:, :, 2:4], img2.dataobj.get_unscaled()) |
| 95 | + assert img1.dataobj.slope == img2.dataobj.slope |
| 96 | + assert img1.dataobj.inter == img2.dataobj.inter |
| 97 | + |
| 98 | + |
81 | 99 | @pytest.mark.parametrize("inplace", (True, False))
|
82 | 100 | def test_nib_roi(tmp_path, inplace):
|
83 | 101 | in_file = os.path.join(data_path, 'functional.nii')
|
@@ -112,3 +130,24 @@ def test_nib_roi_bad_slices(capsys, args, errmsg):
|
112 | 130 | assert retval != 0
|
113 | 131 | captured = capsys.readouterr()
|
114 | 132 | assert errmsg in captured.out
|
| 133 | + |
| 134 | + |
| 135 | +def test_entrypoint(capsys): |
| 136 | + # Check that we handle missing args as expected |
| 137 | + with mock.patch("sys.argv", ["nib-roi", "--help"]): |
| 138 | + try: |
| 139 | + retval = main() |
| 140 | + except SystemExit: |
| 141 | + pass |
| 142 | + else: |
| 143 | + assert False, "argparse exits on --help. If changing to another parser, update test." |
| 144 | + captured = capsys.readouterr() |
| 145 | + assert captured.out.startswith("usage: nib-roi") |
| 146 | + |
| 147 | + |
| 148 | +def test_nib_roi_unknown_axes(capsys): |
| 149 | + in_file = os.path.join(data_path, 'minc1_4d.mnc') |
| 150 | + with pytest.raises(ValueError): |
| 151 | + main([in_file, os.devnull, "-i", ":"]) |
| 152 | + captured = capsys.readouterr() |
| 153 | + assert "Could not slice image." in captured.out |
0 commit comments