Skip to content

Add input validation to ImageStats class #8501

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

Merged
merged 9 commits into from
Aug 1, 2025
16 changes: 16 additions & 0 deletions monai/auto3dseg/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ def __init__(self, image_key: str, stats_name: str = DataStatsKeys.IMAGE_STATS)
self.update_ops(ImageStatsKeys.INTENSITY, SampleOperations())

def __call__(self, data):
# Input Validation Addition
if not isinstance(data, dict):
raise TypeError(f"Input data must be a dict, but got {type(data).__name__}.")
if self.image_key not in data:
raise KeyError(f"Key '{self.image_key}' not found in input data.")
image = data[self.image_key]
if not isinstance(image, (np.ndarray, torch.Tensor, MetaTensor)):
raise TypeError(
f"Value for '{self.image_key}' must be a numpy array, torch.Tensor, or MetaTensor, "
f"but got {type(image).__name__}."
)
if image.ndim < 3:
raise ValueError(
f"Image data under '{self.image_key}' must have at least 3 dimensions, but got shape {image.shape}."
)
# --- End of validation ---
"""
Callable to execute the pre-defined functions

Expand Down
Loading