Repository: Project-MONAI/MONAI
File: monai/auto3dseg/analyzer.py (Python)

Code before:
            ImageStatsKeys.CROPPED_SHAPE: None,
            ImageStatsKeys.SPACING: None,
            ImageStatsKeys.SIZEMM: None,
            ImageStatsKeys.INTENSITY: None,
        }

        super().__init__(stats_name, report_format)
        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"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, " f"but got shape {image.shape}."
            )
            # --- End of validation ---
        """
        Callable to execute the pre-defined functions

        Returns:
            A dictionary. The dict has the key in self.report_format. The value of
            ImageStatsKeys.INTENSITY is in a list format. Each element of the value list
            has stats pre-defined by SampleOperations (max, min, ....).

        Raises:
            RuntimeError if the stats report generated is not consistent with the pre-
                defined report_format.

        Note:
            The stats operation uses numpy and torch to compute max, min, and other
            functions. If the input has nan/inf, the stats results will be nan/inf.

        """
        d = dict(data)
        start = time.time()
        restore_grad_state = torch.is_grad_enabled()

Review comment (refactor):
```suggestion
```
Hi @Emdya thanks for the contribution. I think this block is duplicate so can be removed, but the whole section proposed here should be after the docstring. Please move it down and remove this unneeded check and we should be good to go. 

Code after:
            ImageStatsKeys.CROPPED_SHAPE: None,
            ImageStatsKeys.SPACING: None,
            ImageStatsKeys.SIZEMM: None,
            ImageStatsKeys.INTENSITY: None,
        }

        super().__init__(stats_name, report_format)
        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

        Returns:
            A dictionary. The dict has the key in self.report_format. The value of
            ImageStatsKeys.INTENSITY is in a list format. Each element of the value list
            has stats pre-defined by SampleOperations (max, min, ....).

        Raises:
            RuntimeError if the stats report generated is not consistent with the pre-
                defined report_format.

        Note:
            The stats operation uses numpy and torch to compute max, min, and other
            functions. If the input has nan/inf, the stats results will be nan/inf.

        """
        d = dict(data)
        start = time.time()
        restore_grad_state = torch.is_grad_enabled()
        torch.set_grad_enabled(False)

        ndas = [d[self.image_key][i] for i in range(d[self.image_key].shape[0])]
        if "nda_croppeds" not in d:
            nda_croppeds = [get_foreground_image(nda) for nda in ndas]