Skip to content

Commit 2a12c4b

Browse files
EmdyaKumoLiuericspod
authored
Add input validation to ImageStats class (#8501)
Fixes # . ### Description This pull request adds input validation to the ImageStats class in monai/auto3dseg/analyzer.py. Specifically, it introduces type and value checks at the start of the __call__ method to ensure: The input is a dictionary. The specified image_key exists in the input data. The value for image_key is a NumPy array, torch.Tensor, or MetaTensor. The image array has at least 3 dimensions. If any of these checks fail, an informative error is raised.This enhancement improves robustness and user experience by providing clearer feedback when invalid data is passed to the analyzer. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved input validation for image statistics analysis to provide clearer error messages when inputs are invalid. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Emdya Permuy <67919243+Emdya@users.noreply.github.com> Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent ede6ace commit 2a12c4b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

monai/auto3dseg/analyzer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ def __init__(self, image_key: str, stats_name: str = DataStatsKeys.IMAGE_STATS)
217217
self.update_ops(ImageStatsKeys.INTENSITY, SampleOperations())
218218

219219
def __call__(self, data):
220+
# Input Validation Addition
221+
if not isinstance(data, dict):
222+
raise TypeError(f"Input data must be a dict, but got {type(data).__name__}.")
223+
if self.image_key not in data:
224+
raise KeyError(f"Key '{self.image_key}' not found in input data.")
225+
image = data[self.image_key]
226+
if not isinstance(image, (np.ndarray, torch.Tensor, MetaTensor)):
227+
raise TypeError(
228+
f"Value for '{self.image_key}' must be a numpy array, torch.Tensor, or MetaTensor, "
229+
f"but got {type(image).__name__}."
230+
)
231+
if image.ndim < 3:
232+
raise ValueError(
233+
f"Image data under '{self.image_key}' must have at least 3 dimensions, but got shape {image.shape}."
234+
)
235+
# --- End of validation ---
220236
"""
221237
Callable to execute the pre-defined functions
222238

0 commit comments

Comments
 (0)