Skip to content

Add randomize_per_key option to random dict transforms (#7561)#8959

Open
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:feat/7561-per-key-randomize
Open

Add randomize_per_key option to random dict transforms (#7561)#8959
aymuos15 wants to merge 2 commits into
Project-MONAI:devfrom
aymuos15:feat/7561-per-key-randomize

Conversation

@aymuos15

Copy link
Copy Markdown
Contributor

Fixes #7561

Description

Random dictionary transforms draw their parameters once and apply that same draw to every key. This adds an opt-in randomize_per_key flag so each key can draw independently instead, which is useful when keys hold unrelated images such as different views in self-supervised learning. This mirrors TorchIO's per-transform randomization, but as an opt-in flag rather than a change to the Randomizable base class.

Default is False, so existing behaviour is unchanged. The flag is added to the eleven random intensity transforms and to four spatial transforms (RandAxisFlipd, RandRotated, RandZoomd, RandGridDistortiond). Other transforms are left out for now because they precompute a shared sampling grid, emit multiple correlated samples, or mix across keys, so independent per-key randomization there needs more than a flag.

Types of changes

  • Non-breaking change.
  • New tests added to cover the changes.
  • In-line docstrings updated.

…rms (Project-MONAI#7561)

Random dictionary transforms draw their parameters once and apply that same draw to every key. This adds an opt-in randomize_per_key flag so each key can draw independently instead, which is useful when keys hold unrelated images such as different views in self-supervised learning. This mirrors TorchIO's per-transform randomization, but as an opt-in flag rather than a change to the Randomizable base class. Default is False, so existing behaviour is unchanged. The flag is added to the eleven random intensity transforms and to four spatial transforms (RandAxisFlipd, RandRotated, RandZoomd, RandGridDistortiond); other transforms are left out for now because they precompute a shared sampling grid, emit multiple correlated samples, or mix across keys, so per-key randomization there needs more than a flag.

Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7cb211ae-d04a-4a62-9e2a-8a179663d92f

📥 Commits

Reviewing files that changed from the base of the PR and between 001c353 and 377d3ea.

📒 Files selected for processing (1)
  • tests/transforms/test_rand_per_key.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/transforms/test_rand_per_key.py

📝 Walkthrough

Walkthrough

Adds randomize_per_key: bool = False to 11 intensity dictionary transforms and 4 spatial dictionary transforms. With the default setting, keys share one randomized parameter set; with True, each key draws parameters independently via the underlying transform’s randomize path. A new test module checks both shared and independent key behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.03% 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 title clearly matches the main change: adding an opt-in randomize_per_key option to random dict transforms.
Description check ✅ Passed The description follows the template well and includes the issue reference, summary, and required change-type items.
Linked Issues check ✅ Passed The PR implements the requested per-key randomization option for the targeted dictionary transforms and adds tests.
Out of Scope Changes check ✅ Passed The changes stay focused on per-key randomization, with accompanying tests and docstring updates.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
monai/transforms/intensity/dictionary.py (1)

179-192: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the stale RandGaussianNoised class summary.

Line 179 still says users must instantiate the transform separately for different noise per field, which now conflicts with randomize_per_key=True.

Proposed doc fix
-    Add Gaussian noise to image. This transform assumes all the expected fields have same shape, if you want to add
-    different noise for every field, please use this transform separately.
+    Add Gaussian noise to image. By default, all keys share one randomized noise sample and are expected to
+    have the same shape; set ``randomize_per_key=True`` to draw noise independently per key.

As per path instructions, “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.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/transforms/intensity/dictionary.py` around lines 179 - 192, Update the
RandGaussianNoised docstring summary in the dictionary transform to remove the
stale claim that separate instantiation is required for different noise per
field, since randomize_per_key already supports independent noise per key. Keep
the doc text aligned with the class behavior by updating the opening description
near RandGaussianNoised and its argument notes so the summary reflects both
shared and per-key randomization options.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/transforms/test_rand_per_key.py`:
- Around line 66-71: The test in test_independent_per_key is too strict because
independent random draws can still совпadать for a single seed, especially for
discrete choices in RandAxisFlipd. Update the assertion strategy so it does not
require img1 and img2 to differ for one fixed seed; instead, verify independence
by checking that at least one deterministic seed produces divergent outputs
across the per-key branches, using test_independent_per_key and the
randomize_per_key path.

---

Outside diff comments:
In `@monai/transforms/intensity/dictionary.py`:
- Around line 179-192: Update the RandGaussianNoised docstring summary in the
dictionary transform to remove the stale claim that separate instantiation is
required for different noise per field, since randomize_per_key already supports
independent noise per key. Keep the doc text aligned with the class behavior by
updating the opening description near RandGaussianNoised and its argument notes
so the summary reflects both shared and per-key randomization options.
🪄 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: a56722b4-a1ba-47e0-9030-dfd0db609e32

📥 Commits

Reviewing files that changed from the base of the PR and between b7d14c8 and 001c353.

📒 Files selected for processing (3)
  • monai/transforms/intensity/dictionary.py
  • monai/transforms/spatial/dictionary.py
  • tests/transforms/test_rand_per_key.py

Comment thread tests/transforms/test_rand_per_key.py Outdated
…al dict transforms

Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
@aymuos15 aymuos15 force-pushed the feat/7561-per-key-randomize branch from 001c353 to 377d3ea Compare June 29, 2026 17:07
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.

Make dictionary transforms support different random parameters across keys

1 participant