Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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