Improve Orientation transform to use the "space" (LPS vs RAS) of a metatensor by default
Fix for #8467
Description
As detailed in #8467, the Orientation transform currently always assumes a tensor's affine matrix is in RAS, regardless of the meta["space"] attribute, leading to incorrect performance for LPS metatensors unless the labels are explicitly defined by the user (and it is not at all clear that this needs to be done or how it should be done).
The code in this PR checks whether the input tensor is a metatensor with its affine defined in LPS space. If so, it adjusts the labels passed to nibabel.orientations.axcodes2ornt to give the expected behaviour for LPS tensors.
The default value of the labels parameter of the Orientation transform (and OrientationD) has changed from (('L', 'R'), ('P', 'A'), ('I', 'S')) to None. However, since the behaviour of nibabel.orientations.axcodes2ornt when passed labels=None is equivalent to when passing labels=(('L', 'R'), ('P', 'A'), ('I', 'S')), I would not consider this a breaking change.
Types of changes
- [x] 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.
- [x] Integration tests passed locally by running
./runtests.sh -f -u --net --coverage. - [x] Quick tests passed locally by running
./runtests.sh --quick --unittests --disttests. - [x] In-line docstrings updated.
- [x] Documentation updated, tested
make htmlcommand in thedocs/folder.
This is now ready for review
Thanks so much to @CPBridge for putting this PR together so quickly and to everyone who maintains this excellent library. I realize there is limited bandwidth but it would be tremendous if @KumoLiu, @ericspod, @Nic-Ma, or another could review this PR. I worry that this could easily introduce laterality errors as people go from research (nifti, RAS) to production (dicom, LPS), and it remains a blocking issue for my group. Thanks again for everyone's efforts.
Hi @sudomakeinstall, while I would obviously like to see this merged soon too, if this is blocking for you there is a (non-obvious) workaround that you can use on current releases.
If you know in advance that all the metatensors going through your pipeline will have LPS affine matrices, you can manually set labels=(("R", "L"), ("A", "P"), ("I", "S")) when constructing the Orientation transform. This tells the transform to assume the affine matrices of metatensors it receives have this definition of axes (corresponding to the LPS convention used in DICOM and elsewhere).
[!NOTE]
Other AI code review bot(s) detected
CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.
Walkthrough
The changes update the default behavior of the labels argument in the Orientation and Orientationd transforms, making it dynamic based on input metadata rather than a fixed default. Deprecation warnings are added for the previous default. The corresponding tests are enhanced to cover both RAS and LPS spatial conventions by adding a boolean parameter to toggle LPS metadata, adjusting test inputs, expected outputs, and validation logic accordingly.
Changes
| File(s) | Change Summary |
|---|---|
| monai/transforms/spatial/array.py | Changed Orientation's labels default to None, added deprecation warning, and made label selection metadata-driven. |
| monai/transforms/spatial/dictionary.py | Updated Orientationd's labels default to None, added deprecation warning, and improved docstring for new logic. |
| tests/transforms/test_orientation.py | Extended tests to cover both RAS and LPS conventions, updated test data, and validation logic accordingly. |
| tests/transforms/test_orientationd.py | Duplicated tests for RAS and LPS conventions, updated input construction and axis code validation. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant Orientation
participant MetaTensor
User->>Orientation: Call with input (optionally MetaTensor)
Orientation->>MetaTensor: Check for "space" metadata
alt "space" == "LPS"
Orientation->>Orientation: Set labels to (("R","L"),("A","P"),("I","S"))
else
Orientation->>Orientation: Set labels to (("L","R"),("P","A"),("I","S"))
end
Orientation->>Orientation: Apply transform using selected labels
Orientation-->>User: Return transformed tensor
Estimated code review effort
๐ฏ 3 (Moderate) | โฑ๏ธ ~20 minutes
Poem
A hop, a skip, a label switch,
Now axes flex, no longer fixed.
LPS or RAS, we test them all,
Metadata guides, transforms stand tall.
With warnings gentle, old ways fade,
The rabbit cheers each leap we've made! ๐โจ
๐ Recent review details
Configuration used: .coderabbit.yaml Review profile: CHILL Plan: Pro Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting
๐ฅ Commits
Reviewing files that changed from the base of the PR and between 6905382e1524c339ae71620332d1d3f47069b7ae and ed7d5af9724c4acfabdf0446a888a16aca9db911.
๐ Files selected for processing (2)
monai/transforms/spatial/array.py(7 hunks)monai/transforms/spatial/dictionary.py(3 hunks)
๐ง Files skipped from review as they are similar to previous changes (2)
- monai/transforms/spatial/array.py
- monai/transforms/spatial/dictionary.py
โฐ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: min-dep-pytorch (2.7.1)
- GitHub Check: min-dep-pytorch (2.8.0)
- GitHub Check: min-dep-py3 (3.12)
- GitHub Check: min-dep-py3 (3.11)
- GitHub Check: min-dep-py3 (3.10)
- GitHub Check: min-dep-os (ubuntu-latest)
- GitHub Check: min-dep-pytorch (2.6.0)
- GitHub Check: min-dep-os (windows-latest)
- GitHub Check: min-dep-pytorch (2.5.1)
- GitHub Check: min-dep-py3 (3.9)
- GitHub Check: min-dep-os (macOS-latest)
- GitHub Check: quick-py3 (ubuntu-latest)
- GitHub Check: quick-py3 (windows-latest)
- GitHub Check: packaging
- GitHub Check: build-docs
- GitHub Check: flake8-py3 (pytype)
- GitHub Check: flake8-py3 (codeformat)
- GitHub Check: flake8-py3 (mypy)
- GitHub Check: quick-py3 (macOS-latest)
โจ Finishing Touches
- [ ] ๐ Generate Docstrings
๐งช Generate unit tests
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
๐ชง Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai generate unit teststo generate unit tests for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
Thanks @ericspod I have addressed all comments from review
/build
Just wanted to nudge here a bit @KumoLiu @ericspod . We seem to be stuck
/build
/build
/build