-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix #8802: read nda_croppeds from input dict when pre-computed #8809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
williams145
wants to merge
7
commits into
Project-MONAI:dev
from
williams145:fix/issue-8802-image-stats-unbound-nda-croppeds
+97
−42
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2774923
Fix #8802: read nda_croppeds from input dict when pre-computed
williams145 e681985
DCO Remediation Commit for UGBOMEH OGOCHUKWU WILLIAMS <williamsugbome…
williams145 e80119c
Address review feedback on #8809: validate nda_croppeds and add test …
williams145 8a568c9
Address review feedback on #8809: fix docstring placement in ImageSta…
williams145 0e3f0c4
Address review feedback on #8809: add docstrings to TestDataAnalyzer …
williams145 89f47eb
Merge branch 'dev' into fix/issue-8802-image-stats-unbound-nda-croppeds
williams145 e19bdaf
Address review feedback on #8809: document ValueError in ImageStats._…
williams145 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -539,6 +539,36 @@ def test_seg_summarizer(self): | |
| assert str(DataStatsKeys.FG_IMAGE_STATS) in report | ||
| assert str(DataStatsKeys.LABEL_STATS) in report | ||
|
|
||
| def test_image_stats_precomputed_nda_croppeds(self): | ||
| # Verify that ImageStats does not crash when nda_croppeds is pre-populated in the dict. | ||
| # Previously this caused UnboundLocalError because the variable was only assigned in | ||
| # the else branch but used unconditionally. | ||
| analyzer = ImageStats(image_key="image") | ||
| image = torch.rand(1, 10, 10, 10) | ||
| precomputed = [np.random.rand(8, 8, 8)] # simulated pre-cropped foreground | ||
| data = {"image": MetaTensor(image), "nda_croppeds": precomputed} | ||
| result = analyzer(data) | ||
| assert "image_stats" in result | ||
| assert verify_report_format(result["image_stats"], analyzer.get_report_format()) | ||
|
|
||
| def test_analyzer_grad_state_restored_after_call(self): | ||
| # Verify that ImageStats.__call__ always restores the grad-enabled state it found | ||
| # on entry, regardless of which state that was. | ||
| analyzer = ImageStats(image_key="image") | ||
| image = torch.rand(1, 10, 10, 10) | ||
| data = {"image": MetaTensor(image)} | ||
|
|
||
| # grad enabled before call → must still be enabled after | ||
| torch.set_grad_enabled(True) | ||
| analyzer(data) | ||
| assert torch.is_grad_enabled(), "grad state was not restored after ImageStats call" | ||
|
|
||
| # grad disabled before call → must still be disabled after | ||
| torch.set_grad_enabled(False) | ||
| analyzer(data) | ||
| assert not torch.is_grad_enabled(), "grad state was not restored after ImageStats call" | ||
| torch.set_grad_enabled(True) # restore for subsequent tests | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make grad-state cleanup exception-safe in the test. The test mutates global grad mode; restore the original state in Proposed fix def test_analyzer_grad_state_restored_after_call(self):
# Verify that ImageStats.__call__ always restores the grad-enabled state it found
# on entry, regardless of which state that was.
analyzer = ImageStats(image_key="image")
image = torch.rand(1, 10, 10, 10)
data = {"image": MetaTensor(image)}
-
- # grad enabled before call → must still be enabled after
- torch.set_grad_enabled(True)
- analyzer(data)
- assert torch.is_grad_enabled(), "grad state was not restored after ImageStats call"
-
- # grad disabled before call → must still be disabled after
- torch.set_grad_enabled(False)
- analyzer(data)
- assert not torch.is_grad_enabled(), "grad state was not restored after ImageStats call"
- torch.set_grad_enabled(True) # restore for subsequent tests
+ original_grad_state = torch.is_grad_enabled()
+ try:
+ # grad enabled before call → must still be enabled after
+ torch.set_grad_enabled(True)
+ analyzer(data)
+ assert torch.is_grad_enabled(), "grad state was not restored after ImageStats call"
+
+ # grad disabled before call → must still be disabled after
+ torch.set_grad_enabled(False)
+ analyzer(data)
+ assert not torch.is_grad_enabled(), "grad state was not restored after ImageStats call"
+ finally:
+ torch.set_grad_enabled(original_grad_state)🤖 Prompt for AI Agents |
||
|
|
||
| def tearDown(self) -> None: | ||
| self.test_dir.cleanup() | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.