Skip to content

Commit

Permalink
BUG: fixed wrong space directions interpretation in nrrd reader
Browse files Browse the repository at this point in the history
the vectors presented for space directions are column vectors

ref: https://teem.sourceforge.net/nrrd/format.html#spacedirections
Signed-off-by: Christian Herz <[email protected]>
  • Loading branch information
che85 committed Sep 17, 2024
1 parent 25589c3 commit 9a727a6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def _get_affine(self, header: dict) -> np.ndarray:
x, y = direction.shape
affine_diam = min(x, y) + 1
affine: np.ndarray = np.eye(affine_diam)
affine[:x, :y] = direction
affine[:x, :y] = direction.T
affine[: (affine_diam - 1), -1] = origin # len origin is always affine_diam - 1
return affine

Expand Down
8 changes: 6 additions & 2 deletions tests/test_nrrd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"dimension": 4,
"space": "left-posterior-superior",
"sizes": [3, 4, 4, 1],
"space directions": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
"space origin": [0.0, 0.0, 0.0],
"space directions": [[0.7, 0.0, 0.0], [0.0, 0.0, -0.8], [0.0, 0.9, 0.0]],
"space origin": [1.0, 5.0, 20.0],
},
]

Expand Down Expand Up @@ -110,6 +110,10 @@ def test_read_with_header(self, data_shape, filename, expected_shape, dtype, ref
np.testing.assert_allclose(image_array, test_image)
self.assertIsInstance(image_header, dict)
self.assertTupleEqual(tuple(image_header["spatial_shape"]), expected_shape)
np.testing.assert_allclose(
image_header["affine"],
np.array([[-0.7, 0.0, 0.0, -1.0], [0.0, 0.0, -0.9, -5.0], [0.0, -0.8, 0.0, 20.0], [0.0, 0.0, 0.0, 1.0]]),
)

@parameterized.expand([TEST_CASE_8])
def test_read_with_header_index_order_c(self, data_shape, filename, expected_shape, dtype, reference_header):
Expand Down

0 comments on commit 9a727a6

Please sign in to comment.