-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(data): skip empty foreground when computing DatasetSummary statistics #9043
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| import nibabel as nib | ||||||||||||||||||
| import numpy as np | ||||||||||||||||||
| import torch | ||||||||||||||||||
|
|
||||||||||||||||||
| from monai.data import Dataset, DatasetSummary, create_test_image_3d | ||||||||||||||||||
| from monai.transforms import LoadImaged | ||||||||||||||||||
|
|
@@ -99,6 +100,35 @@ def test_anisotropic_spacing(self): | |||||||||||||||||
| target_spacing = calculator.get_target_spacing(anisotropic_threshold=4.0, percentile=20.0) | ||||||||||||||||||
| np.testing.assert_allclose(target_spacing, (1.0, 1.0, 1.8)) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_mixed_foreground_and_background(self): | ||||||||||||||||||
| data = [ | ||||||||||||||||||
| {"image": torch.rand(1, 4, 4), "label": torch.ones(1, 4, 4)}, | ||||||||||||||||||
| {"image": torch.rand(1, 4, 4), "label": torch.zeros(1, 4, 4)}, | ||||||||||||||||||
| ] | ||||||||||||||||||
| image = torch.cat([d["image"] for d in data]) | ||||||||||||||||||
| label = torch.cat([d["label"] for d in data]) | ||||||||||||||||||
| expected = image[torch.where(label > 0)] | ||||||||||||||||||
|
|
||||||||||||||||||
| calculator = DatasetSummary(data, num_workers=0) | ||||||||||||||||||
| calculator.calculate_statistics() | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_mean, expected.mean().item(), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_std, expected.std(correction=0).item(), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_max, expected.max().item(), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_min, expected.min().item(), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
|
|
||||||||||||||||||
| calculator.calculate_percentiles(sampling_flag=False) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_min_percentile, np.percentile(expected, 0.5), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_max_percentile, np.percentile(expected, 99.5), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
| np.testing.assert_allclose(calculator.data_median, np.median(expected), rtol=1e-5, atol=1e-5) | ||||||||||||||||||
|
|
||||||||||||||||||
| def test_all_background(self): | ||||||||||||||||||
| data = [{"image": torch.rand(1, 4, 4), "label": torch.zeros(1, 4, 4)}] | ||||||||||||||||||
| calculator = DatasetSummary(data, num_workers=0) | ||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "foreground_threshold"): | ||||||||||||||||||
| calculator.calculate_statistics() | ||||||||||||||||||
| with self.assertRaisesRegex(ValueError, "foreground_threshold"): | ||||||||||||||||||
| calculator.calculate_percentiles() | ||||||||||||||||||
|
Comment on lines
+127
to
+130
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Assert the full remediation text. The test must verify the recommendation Proposed assertion- with self.assertRaisesRegex(ValueError, "foreground_threshold"):
+ with self.assertRaisesRegex(ValueError, r"set foreground_threshold=-1"):Apply this to both assertions. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||
| unittest.main() | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Complete Google-style docstrings for the changed definitions.
monai/data/dataset_summary.py#L171-L175: documentRaises:andReturns: Noneforcalculate_statistics.monai/data/dataset_summary.py#L221-L224: documentRaises:andReturns: Noneforcalculate_percentiles.tests/data/test_dataset_summary.py#L103-L103: document the mixed foreground/background test.tests/data/test_dataset_summary.py#L124-L124: document the all-background error test.As per path instructions, Python definitions must document variables, return values, and raised exceptions in Google-style docstrings.
📍 Affects 2 files
monai/data/dataset_summary.py#L171-L175(this comment)monai/data/dataset_summary.py#L221-L224tests/data/test_dataset_summary.py#L103-L103tests/data/test_dataset_summary.py#L124-L124🤖 Prompt for AI Agents
Source: Path instructions