Skip to content

Add spatial shape constraint docs and validation for DiNTS (#6771)#8827

Open
Cado87 wants to merge 4 commits intoProject-MONAI:devfrom
Cado87:docs/dints-shape-constraints
Open

Add spatial shape constraint docs and validation for DiNTS (#6771)#8827
Cado87 wants to merge 4 commits intoProject-MONAI:devfrom
Cado87:docs/dints-shape-constraints

Conversation

@Cado87
Copy link
Copy Markdown
Contributor

@Cado87 Cado87 commented Apr 13, 2026

Fixes #6771 .

Description

Adds spatial shape constraint documentation and runtime validation for DiNTS.
Each input spatial dimension must be divisible by 2 ** (num_depths + int(use_downsample)).
Previously only a print() warning existed in TopologyConstruction.__init__ — no error was raised.
This PR adds _check_input_size() to DiNTS, calls it in forward(), updates the class docstring
with a Spatial Shape Constraints section, and adds tests for both 3D and 2D invalid inputs.

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.

…ONAI#6771)

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

coderabbitai bot commented Apr 13, 2026

📝 Walkthrough

Walkthrough

Added runtime input-size validation to DiNTS: the forward path now calls a new private @torch.jit.unused helper _check_input_size(spatial_shape) (when not scripting/tracing) that computes a required divisibility factor 2 ** (num_depths + int(use_downsample)) and raises ValueError listing offending dimensions if the input spatial shape is not divisible. Updated docstrings/comments to state that use_downsample affects the divisibility factor. Tests were updated and new tests added to assert that invalid 2D/3D spatial sizes raise ValueError.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% 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 The PR title clearly and specifically describes the main change: adding spatial shape constraint documentation and validation for DiNTS.
Description check ✅ Passed The description covers the main change, references the linked issue, specifies new tests and docstring updates, and follows the template structure.
Linked Issues check ✅ Passed The PR fulfills issue #6771 by adding code logic to validate input spatial shapes against model configuration, preventing runtime issues for DiNTS.
Out of Scope Changes check ✅ Passed All changes are scoped to DiNTS spatial constraint validation: updated divisibility rules, runtime checks, docstrings, and corresponding tests.

✏️ 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
monai/networks/nets/dints.py (1)

493-504: Add Returns sections in modified docstrings for guideline compliance.

Both modified methods document args/raises but omit return-value sections.

As per coding guidelines, "Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings."

Also applies to: 520-523

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

In `@monai/networks/nets/dints.py` around lines 493 - 504, The docstring for
_check_input_size (and the other modified method immediately following it) is
missing a Returns section; update both docstrings to include a Returns entry
describing the return value (e.g., "None: if validation succeeds" or the actual
return type) and a brief description, following Google-style docstring format so
each function documents args, returns, and raises; ensure you edit the
_check_input_size docstring and the subsequent modified method's docstring to
add the Returns section accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@monai/networks/nets/dints.py`:
- Line 505: The validation uses self.dints_space.use_downsample while the
constructor uses the use_downsample argument, allowing divergence; ensure both
use the same source by assigning the constructor argument into the config before
any validation or stem construction (e.g., set self.dints_space.use_downsample =
use_downsample early in DiNTS.__init__), or alternatively change
_check_input_size() to read the use_downsample argument passed into __init__;
update the places computing factor (currently using
self.dints_space.use_downsample) and stem construction to reference the unified
source so _check_input_size, DiNTS.__init__, and factor calculation are
consistent.

---

Nitpick comments:
In `@monai/networks/nets/dints.py`:
- Around line 493-504: The docstring for _check_input_size (and the other
modified method immediately following it) is missing a Returns section; update
both docstrings to include a Returns entry describing the return value (e.g.,
"None: if validation succeeds" or the actual return type) and a brief
description, following Google-style docstring format so each function documents
args, returns, and raises; ensure you edit the _check_input_size docstring and
the subsequent modified method's docstring to add the Returns section
accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 648f35b7-1f1e-4150-8195-53b174ddf38a

📥 Commits

Reviewing files that changed from the base of the PR and between 5a2d0a7 and 88ce791.

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

Raises:
ValueError: if any spatial dimension is not divisible by the required factor.
"""
factor = 2 ** (self.num_depths + int(self.dints_space.use_downsample))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Unify use_downsample source to avoid inconsistent validation.

_check_input_size() uses self.dints_space.use_downsample, but stem construction in DiNTS.__init__ uses the use_downsample argument. If these differ, validation and actual network path can diverge.

Proposed fix
 class DiNTS(nn.Module):
@@
     def __init__(
@@
         use_downsample: bool = True,
@@
     ):
         super().__init__()
+        if hasattr(dints_space, "use_downsample") and dints_space.use_downsample != use_downsample:
+            raise ValueError(
+                f"DiNTS.use_downsample ({use_downsample}) must match dints_space.use_downsample "
+                f"({dints_space.use_downsample})."
+            )
+        self.use_downsample = use_downsample
@@
     def _check_input_size(self, spatial_shape):
@@
-        factor = 2 ** (self.num_depths + int(self.dints_space.use_downsample))
+        factor = 2 ** (self.num_depths + int(self.use_downsample))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/nets/dints.py` at line 505, The validation uses
self.dints_space.use_downsample while the constructor uses the use_downsample
argument, allowing divergence; ensure both use the same source by assigning the
constructor argument into the config before any validation or stem construction
(e.g., set self.dints_space.use_downsample = use_downsample early in
DiNTS.__init__), or alternatively change _check_input_size() to read the
use_downsample argument passed into __init__; update the places computing factor
(currently using self.dints_space.use_downsample) and stem construction to
reference the unified source so _check_input_size, DiNTS.__init__, and factor
calculation are consistent.

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.

♻️ Duplicate comments (1)
monai/networks/nets/dints.py (1)

506-507: ⚠️ Potential issue | 🟠 Major

Unify use_downsample source for validation and stem construction.

At Line 506, _check_input_size() uses self.dints_space.use_downsample, but stem construction uses the constructor use_downsample (Lines 414 and 457). If those diverge, validation can check the wrong factor.

Suggested fix
 class DiNTS(nn.Module):
@@
     def __init__(
@@
         use_downsample: bool = True,
@@
     ):
         super().__init__()
+        if hasattr(dints_space, "use_downsample") and dints_space.use_downsample != use_downsample:
+            raise ValueError(
+                f"DiNTS.use_downsample ({use_downsample}) must match "
+                f"dints_space.use_downsample ({dints_space.use_downsample})."
+            )
+        self.use_downsample = use_downsample
@@
     def _check_input_size(self, spatial_shape):
@@
-        factor = 2 ** (self.num_depths + int(self.dints_space.use_downsample))
+        factor = 2 ** (self.num_depths + int(self.use_downsample))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/networks/nets/dints.py` around lines 506 - 507, The validation in
_check_input_size currently reads self.dints_space.use_downsample but stem
construction and the constructor use the parameter/attribute use_downsample;
change the validation to reference the same source (use self.use_downsample) so
the downsample flag is consistent. Locate _check_input_size and replace any use
of self.dints_space.use_downsample with the class attribute/constructor param
(self.use_downsample), ensuring factor computation and wrong_dims calculation
use that unified flag; also ensure the constructor sets self.use_downsample if
not already.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@monai/networks/nets/dints.py`:
- Around line 506-507: The validation in _check_input_size currently reads
self.dints_space.use_downsample but stem construction and the constructor use
the parameter/attribute use_downsample; change the validation to reference the
same source (use self.use_downsample) so the downsample flag is consistent.
Locate _check_input_size and replace any use of self.dints_space.use_downsample
with the class attribute/constructor param (self.use_downsample), ensuring
factor computation and wrong_dims calculation use that unified flag; also ensure
the constructor sets self.use_downsample if not already.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f620ceee-2b1f-448c-a509-7ec30c75518e

📥 Commits

Reviewing files that changed from the base of the PR and between 88ce791 and 3420a85.

📒 Files selected for processing (2)
  • monai/networks/nets/dints.py
  • tests/networks/nets/test_dints_network.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/networks/nets/test_dints_network.py

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

1 participant