Add randomize_per_key option to random dict transforms (#7561)#8959
Add randomize_per_key option to random dict transforms (#7561)#8959aymuos15 wants to merge 2 commits into
Conversation
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 winUpdate the stale
RandGaussianNoisedclass 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
📒 Files selected for processing (3)
monai/transforms/intensity/dictionary.pymonai/transforms/spatial/dictionary.pytests/transforms/test_rand_per_key.py
…al dict transforms Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
001c353 to
377d3ea
Compare
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_keyflag 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 theRandomizablebase 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