Skip to content

Fix spatial_ndim after EnsureChannelFirst adds a channel dim (fixes #… - #9192

Open
sillyfellow21 wants to merge 2 commits into
Project-MONAI:mainfrom
sillyfellow21:main
Open

sillyfellow21 wants to merge 2 commits into
Project-MONAI:mainfrom
sillyfellow21:main

Conversation

@sillyfellow21

@sillyfellow21 sillyfellow21 commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #9184

Description

EnsureChannelFirst(channel_dim="no_channel") adds a channel dim to a (H, W, D) tensor, but the cached spatial_ndim stays at ndim - 1 = 2 because the constructor assumes dim 0 is already the channel. Any later spatial crop then shifts only the first two axes and silently drops the third axis' affine offset — e.g. a crop origin lands at [15, 25, 30] instead of [15, 25, 35]. The same bug affects 2D input (spatial_ndim was 1 instead of 2).

This patch recomputes spatial_ndim from the affine the moment EnsureChannelFirst adds the channel dim, so downstream transforms (e.g. SpatialCrop) build the full N-axis translation. It only touches the no-channel case and non-batched affines, so nothing else is affected.

Types of changes

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

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f373dd22-a89a-4994-ad23-68f5b241a626

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant