From 87133d89a8036d53edc7d10972e64c2135fb163d Mon Sep 17 00:00:00 2001 From: peng xiang <78131130+px12811@users.noreply.github.com> Date: Wed, 14 Oct 2026 14:56:22 +0800 Subject: [PATCH] test(nifti): cover 2D writer without target affine Add a regression test for the NibabelWriter path reported in #9166. The underlying fix landed in #9080, but the writer-level behavior was not covered directly. Assisted-by: Codex Signed-off-by: peng xiang <78131130+px12811@users.noreply.github.com> --- tests/data/test_nifti_rw.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/data/test_nifti_rw.py b/tests/data/test_nifti_rw.py index cd75bef93d7..e07d0a73f23 100644 --- a/tests/data/test_nifti_rw.py +++ b/tests/data/test_nifti_rw.py @@ -172,6 +172,20 @@ def test_write_2d(self): np.testing.assert_allclose(out.get_fdata(), [[0, 2, 4]], atol=1e-4, rtol=1e-4) np.testing.assert_allclose(out.affine, np.diag([1.4, 2, 1, 1]), atol=1e-4, rtol=1e-4) + def test_write_2d_without_target_affine(self): + """2D channel-less data should be written unchanged when no target affine is provided.""" + with tempfile.TemporaryDirectory() as out_dir: + image_name = os.path.join(out_dir, "test.nii.gz") + for p in TEST_NDARRAYS: + img = p(np.arange(6).reshape((2, 3))) + writer_obj = NibabelWriter() + writer_obj.set_data_array(img, channel_dim=None, spatial_ndim=None) + writer_obj.set_metadata({"affine": np.eye(4), "original_affine": None}) + writer_obj.write(image_name) + out = nib.load(image_name) + np.testing.assert_allclose(out.get_fdata(), [[0, 1, 2], [3, 4, 5]], atol=1e-4, rtol=1e-4) + np.testing.assert_allclose(out.affine, np.eye(4), atol=1e-4, rtol=1e-4) + def test_write_3d(self): with tempfile.TemporaryDirectory() as out_dir: image_name = os.path.join(out_dir, "test.nii.gz")