Skip to content

Add spatial shape constraint docs and test for SwinUNETR (#6771)#8817

Open
Cado87 wants to merge 2 commits intoProject-MONAI:devfrom
Cado87:docs/swin-unetr-shape-constraints
Open

Add spatial shape constraint docs and test for SwinUNETR (#6771)#8817
Cado87 wants to merge 2 commits intoProject-MONAI:devfrom
Cado87:docs/swin-unetr-shape-constraints

Conversation

@Cado87
Copy link
Copy Markdown

@Cado87 Cado87 commented Apr 11, 2026

Fixes #6771 .

Description

Adds documentation of spatial shape constraints for SwinUNETR. Each input
spatial dimension must be divisible by patch_size ** 5 (32 by default with
patch_size=2). The runtime validation logic already existed in _check_input_size()
but was undocumented. This PR adds a Spatial Shape Constraints section to the
class docstring, updates the patch_size arg description in __init__, and adds
a test to verify that forward() raises ValueError for invalid spatial shapes.

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.

…AI#6771)

Signed-off-by: Adrian Caderno <adriancaderno@gmail.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 11, 2026

📝 Walkthrough

Walkthrough

Updated SwinUNETR class docstrings to explicitly document that input spatial dimensions must be divisible by patch_size ** 5. Added a Raises: entry documenting the ValueError condition. Added a unit test validating that the constraint is enforced for both 2D and 3D configurations. No runtime logic was modified; validation already exists in _check_input_size().

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title directly describes the main change: adding spatial shape constraint documentation and tests for SwinUNETR.
Description check ✅ Passed Description includes all required sections with clear explanation of changes, proper types selection, and docstring updates acknowledged.
Linked Issues check ✅ Passed PR addresses issue #6771 by documenting spatial shape constraints (divisibility by patch_size**5) through docstrings and unit tests, fulfilling the request for code logic and documentation.
Out of Scope Changes check ✅ Passed All changes stay focused on documenting and testing existing spatial shape constraints; no unrelated modifications introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/networks/nets/test_swin_unetr.py (1)

93-103: Consider adding one non-default patch_size case.

This verifies the default (patch_size=2) path well. Add one invalid-shape assertion for patch_size=3 to directly cover the generalized patch_size ** 5 contract.

Proposed test extension
     def test_invalid_input_shape(self):
         # spatial dims not divisible by patch_size**5 (default patch_size=2, so must be divisible by 32)
         net = SwinUNETR(in_channels=1, out_channels=2, feature_size=24, spatial_dims=3)
         with self.assertRaises(ValueError):
             net(torch.randn(1, 1, 33, 64, 64))  # 33 is not divisible by 32

         net_2d = SwinUNETR(in_channels=1, out_channels=2, feature_size=24, spatial_dims=2)
         with self.assertRaises(ValueError):
             net_2d(torch.randn(1, 1, 48, 33))  # 33 is not divisible by 32
+
+        net_2d_patch3 = SwinUNETR(in_channels=1, out_channels=2, feature_size=24, spatial_dims=2, patch_size=3)
+        with self.assertRaises(ValueError):
+            net_2d_patch3(torch.randn(1, 1, 33, 33))  # 33 is not divisible by 3**5 (243)

As per coding guidelines, "Ensure new or modified definitions will be covered by existing or new unit tests."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/networks/nets/test_swin_unetr.py` around lines 93 - 103, Add a new
invalid-shape assertion in the same test (test_invalid_input_shape) that
instantiates SwinUNETR with a non-default patch_size (e.g., patch_size=3) and
calls the model with an input tensor whose spatial dimensions are not divisible
by that patch_size (e.g., use torch.randn(1, 1, 10, 48, 48) for spatial_dims=3)
wrapped in self.assertRaises(ValueError); this will exercise the SwinUNETR
constructor/check logic (SwinUNETR) for non-default patch sizes and ensure the
generalized patch_size validation path is covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/networks/nets/test_swin_unetr.py`:
- Around line 93-103: Add a new invalid-shape assertion in the same test
(test_invalid_input_shape) that instantiates SwinUNETR with a non-default
patch_size (e.g., patch_size=3) and calls the model with an input tensor whose
spatial dimensions are not divisible by that patch_size (e.g., use
torch.randn(1, 1, 10, 48, 48) for spatial_dims=3) wrapped in
self.assertRaises(ValueError); this will exercise the SwinUNETR
constructor/check logic (SwinUNETR) for non-default patch sizes and ensure the
generalized patch_size validation path is covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8e650413-a978-4d3a-999c-17bfa4eb13df

📥 Commits

Reviewing files that changed from the base of the PR and between cc92126 and cb21557.

📒 Files selected for processing (2)
  • monai/networks/nets/swin_unetr.py
  • tests/networks/nets/test_swin_unetr.py

Copy link
Copy Markdown
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Cado87 thanks for the network shape analysis!

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.

Documentation of spatial shape constraints for the networks

2 participants