Fix ClipIntensityPercentiles metadata accumulation#9003
Conversation
Signed-off-by: Mohamed Abdeltawab <mohamed.abdeltawab@integrant.com>
📝 WalkthroughWalkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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
🤖 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 `@monai/transforms/intensity/array.py`:
- Around line 1177-1178: Update the clipping_values assignment in the transform
logic to verify that img has a meta attribute before accessing it, while
preserving the existing clipping_values non-None condition and metadata
assignment when available.
🪄 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: 172b6c36-87cd-48a5-82ae-6a2627769c24
📒 Files selected for processing (2)
monai/transforms/intensity/array.pytests/transforms/test_clip_intensity_percentiles.py
| if clipping_values is not None: | ||
| img.meta["clipping_values"] = clipping_values # type: ignore |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Guard against missing meta attribute.
If metadata tracking is disabled globally (get_track_meta() == False), img will not be a MetaTensor and accessing img.meta raises an AttributeError. Check for existence before assignment.
🛡️ Proposed fix
- if clipping_values is not None:
+ if clipping_values is not None and hasattr(img, "meta"):
img.meta["clipping_values"] = clipping_values # type: ignore📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if clipping_values is not None: | |
| img.meta["clipping_values"] = clipping_values # type: ignore | |
| if clipping_values is not None and hasattr(img, "meta"): | |
| img.meta["clipping_values"] = clipping_values # type: ignore |
🤖 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/array.py` around lines 1177 - 1178, Update the
clipping_values assignment in the transform logic to verify that img has a meta
attribute before accessing it, while preserving the existing clipping_values
non-None condition and metadata assignment when available.
Description
ClipIntensityPercentilesstored returned clipping values on the transform instance. Reusing the same transform therefore accumulated values from earlier calls and also mutated the metadata of earlier outputs.This change keeps the clipping-value accumulator local to each
__call__. Each result now receives only its own clipping values, and later calls cannot change metadata already returned to a caller.Regression tests cover repeated channel-wise and non-channel-wise calls.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.Testing
python -m unittest tests.transforms.test_clip_intensity_percentiles tests.transforms.test_clip_intensity_percentilesd— 96 tests passedpython -m ruff check monai/transforms/intensity/array.py tests/transforms/test_clip_intensity_percentiles.pypython -m black --check monai/transforms/intensity/array.py tests/transforms/test_clip_intensity_percentiles.py