MONAI icon indicating copy to clipboard operation
MONAI copied to clipboard

Improve Orientation transform to use the "space" (LPS vs RAS) of a metatensor by default

Open CPBridge opened this issue 7 months ago โ€ข 1 comments

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 html command in the docs/ folder.

CPBridge avatar Jun 04 '25 14:06 CPBridge

This is now ready for review

CPBridge avatar Jun 16 '25 22:06 CPBridge

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.

sudomakeinstall avatar Jul 07 '25 23:07 sudomakeinstall

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).

CPBridge avatar Jul 08 '25 12:07 CPBridge

[!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.

โค๏ธ Share
๐Ÿชง 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 @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere 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.

coderabbitai[bot] avatar Jul 25 '25 16:07 coderabbitai[bot]

Thanks @ericspod I have addressed all comments from review

CPBridge avatar Jul 25 '25 16:07 CPBridge

/build

KumoLiu avatar Aug 10 '25 14:08 KumoLiu

Just wanted to nudge here a bit @KumoLiu @ericspod . We seem to be stuck

CPBridge avatar Aug 27 '25 15:08 CPBridge

/build

KumoLiu avatar Aug 28 '25 07:08 KumoLiu

/build

KumoLiu avatar Aug 28 '25 10:08 KumoLiu

/build

KumoLiu avatar Sep 01 '25 11:09 KumoLiu