Skip to content

Commit d0a6425

Browse files
committed
TEST: Check MINC-2, Analyze, and main()
1 parent e969374 commit d0a6425

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

nibabel/cmdline/tests/test_roi.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from nibabel.testing import data_path
66

77
import unittest
8+
from unittest import mock
89
import pytest
910

1011

@@ -78,6 +79,23 @@ def test_lossless_slice_scaling(tmp_path):
7879
assert img1.dataobj.inter == img2.dataobj.inter
7980

8081

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+
8199
@pytest.mark.parametrize("inplace", (True, False))
82100
def test_nib_roi(tmp_path, inplace):
83101
in_file = os.path.join(data_path, 'functional.nii')
@@ -112,3 +130,24 @@ def test_nib_roi_bad_slices(capsys, args, errmsg):
112130
assert retval != 0
113131
captured = capsys.readouterr()
114132
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

Comments
 (0)