Skip to content

Commit 5bff478

Browse files
authored
BUG: fixed wrong space directions interpretation in nrrd reader (#8091)
the vectors presented for space directions are column vectors ref: https://teem.sourceforge.net/nrrd/format.html#spacedirections ### Description A few sentences describing the changes proposed in this pull request. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [ ] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Christian Herz <[email protected]>
1 parent 25589c3 commit 5bff478

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

monai/data/image_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ def _get_affine(self, header: dict) -> np.ndarray:
13591359
x, y = direction.shape
13601360
affine_diam = min(x, y) + 1
13611361
affine: np.ndarray = np.eye(affine_diam)
1362-
affine[:x, :y] = direction
1362+
affine[:x, :y] = direction.T
13631363
affine[: (affine_diam - 1), -1] = origin # len origin is always affine_diam - 1
13641364
return affine
13651365

tests/test_nrrd_reader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
"dimension": 4,
4141
"space": "left-posterior-superior",
4242
"sizes": [3, 4, 4, 1],
43-
"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]],
44-
"space origin": [0.0, 0.0, 0.0],
43+
"space directions": [[0.7, 0.0, 0.0], [0.0, 0.0, -0.8], [0.0, 0.9, 0.0]],
44+
"space origin": [1.0, 5.0, 20.0],
4545
},
4646
]
4747

@@ -110,6 +110,10 @@ def test_read_with_header(self, data_shape, filename, expected_shape, dtype, ref
110110
np.testing.assert_allclose(image_array, test_image)
111111
self.assertIsInstance(image_header, dict)
112112
self.assertTupleEqual(tuple(image_header["spatial_shape"]), expected_shape)
113+
np.testing.assert_allclose(
114+
image_header["affine"],
115+
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]]),
116+
)
113117

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

0 commit comments

Comments
 (0)