-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(NrrdReader): handle non-spatial axes in 4-D NRRD files #8895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,23 @@ | |
| "space origin": [1.0, 5.0, 20.0], | ||
| }, | ||
| ] | ||
| # 4-D NRRD with an explicit 'list' channel axis (kinds: list domain domain domain). | ||
| # pynrrd stores the 'none' space direction for the channel axis as a row of NaN values. | ||
| TEST_CASE_4D_CHANNEL = [ | ||
| (3, 4, 5, 6), # (channel, H, W, D) | ||
| "test_4d_channel.nrrd", | ||
| np.float32, | ||
| { | ||
| "dimension": 4, | ||
| "space": "left-posterior-superior", | ||
| "kinds": ["list", "domain", "domain", "domain"], | ||
| "sizes": [3, 4, 5, 6], | ||
| "space directions": np.array( | ||
| [[np.nan, np.nan, np.nan], [1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]] | ||
| ), | ||
| "space origin": np.array([10.0, 20.0, 30.0]), | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| @skipUnless(has_nrrd, "nrrd required") | ||
|
|
@@ -128,6 +145,32 @@ def test_read_with_header_index_order_c(self, data_shape, filename, expected_sha | |
| self.assertTupleEqual(image_array.shape, expected_shape[::-1]) | ||
| self.assertTupleEqual(image_array.shape, tuple(image_header["spatial_shape"])) | ||
|
|
||
| @parameterized.expand([TEST_CASE_4D_CHANNEL]) | ||
| def test_read_4d_channel(self, data_shape, filename, dtype, reference_header): | ||
| """4-D NRRD with a 'list' channel axis must not crash in _get_affine and must | ||
| set ORIGINAL_CHANNEL_DIM / spatial_shape correctly.""" | ||
| test_image = np.random.rand(*data_shape).astype(dtype) | ||
| with tempfile.TemporaryDirectory() as tempdir: | ||
| filepath = os.path.join(tempdir, filename) | ||
| nrrd.write(filepath, test_image, header=reference_header) | ||
| reader = NrrdReader() | ||
| image_array, image_header = reader.get_data(reader.read(filepath)) | ||
| self.assertIsInstance(image_array, np.ndarray) | ||
| self.assertEqual(image_array.dtype, dtype) | ||
| self.assertTupleEqual(image_array.shape, data_shape) | ||
| # spatial_shape must exclude the channel axis | ||
| self.assertTupleEqual(tuple(image_header["spatial_shape"]), data_shape[1:]) | ||
| # channel dim 0 must be identified | ||
| self.assertEqual(image_header["original_channel_dim"], 0) | ||
| # affine must be a valid 4×4 matrix (3 spatial dims → 4×4) | ||
| self.assertTupleEqual(image_header["affine"].shape, (4, 4)) | ||
| np.testing.assert_allclose( | ||
| image_header["affine"], | ||
| np.array( | ||
| [[-1.0, 0.0, 0.0, -10.0], [0.0, -2.0, 0.0, -20.0], [0.0, 0.0, 3.0, 30.0], [0.0, 0.0, 0.0, 1.0]] | ||
| ), | ||
| ) | ||
|
Comment on lines
+148
to
+172
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Add the C-order variant of this regression test. This only covers the default F-order path, but the PR also changes 🧰 Tools🪛 Ruff (0.15.15)[warning] 165-165: Comment contains ambiguous (RUF003) [warning] 165-165: Comment contains ambiguous (RUF003) 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle the C-order NaN axis too.
After
_convert_f_to_c_order, a 4x3space directionsarray becomes 3x4, so the non-spatial axis is an all-NaN column, not a row. Line 1592 only filters rows, which meansindex_order="C"still reaches a shape mismatch at Line 1598 for the new 4-D channel case.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents