Skip to content

Commit

Permalink
BUG: fixed wrong space directions interpretation in nrrd reader (#8091)
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

### 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 <christianherz1985@gmail.com>
  • Loading branch information
che85 authored Sep 18, 2024
1 parent 25589c3 commit 5bff478
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 5bff478

Please sign in to comment.