Skip to content

Commit 0e9fc83

Browse files
px12811ericspod
andauthored
test(nifti): cover 2D writer without target affine (#9173)
## Summary Add a regression test for the public `NibabelWriter` path reported in #9166. The reported `TypeError` is already resolved on `dev` by the general `spatial_resample` fix in #9080. This PR adds direct coverage for the original writer-level scenario so the regression cannot return without failing this test. The new test writes a 2D channel-less array with `spatial_ndim=None`, `original_affine=None`, and no `spatial_shape`, then verifies the saved data and affine. ## Validation - On `e28a2c07^`, the new test fails with the reported `TypeError: '>=' not supported between instances of 'NoneType' and 'int'`. - On current `dev`, the new test passes. - `python -m pytest tests/data/test_nifti_rw.py -q`: 42 passed - `python -m pytest tests/transforms/test_spatial_resample.py -q`: 76 passed - `python -m pytest tests/data/test_image_rw.py -q`: 2 passed, 28 skipped in the local minimal environment - `ruff`, `black --check`, `isort --check-only`, and `git diff --check`: passed ## AI disclosure This contribution was prepared with Codex assistance and reviewed by the submitter, who will respond to review comments personally. Signed-off-by: peng xiang <78131130+px12811@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 41df4d9 commit 0e9fc83

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎tests/data/test_nifti_rw.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ def test_write_2d(self):
172172
np.testing.assert_allclose(out.get_fdata(), [[0, 2, 4]], atol=1e-4, rtol=1e-4)
173173
np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 1, 1]), atol=1e-4, rtol=1e-4)
174174

175+
def test_write_2d_without_target_affine(self):
176+
"""2D channel-less data should be written unchanged when no target affine is provided."""
177+
with tempfile.TemporaryDirectory() as out_dir:
178+
image_name = os.path.join(out_dir, "test.nii.gz")
179+
for p in TEST_NDARRAYS:
180+
img = p(np.arange(6).reshape((2, 3)))
181+
writer_obj = NibabelWriter()
182+
writer_obj.set_data_array(img, channel_dim=None, spatial_ndim=None)
183+
writer_obj.set_metadata({"affine": np.eye(4), "original_affine": None})
184+
writer_obj.write(image_name)
185+
out = nib.load(image_name)
186+
np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [3, 4, 5]], atol=1e-4, rtol=1e-4)
187+
np.testing.assert_allclose(out.affine, np.eye(4), atol=1e-4, rtol=1e-4)
188+
175189
def test_write_3d(self):
176190
with tempfile.TemporaryDirectory() as out_dir:
177191
image_name = os.path.join(out_dir, "test.nii.gz")

0 commit comments

Comments
 (0)