Repository: Project-MONAI/MONAI
File: monai/handlers/ignite_metric.py (Python)

Code before:

import warnings
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, Any

import torch
from torch.nn.modules.loss import _Loss

from monai.config import IgniteInfo
from monai.metrics import CumulativeIterationMetric, LossMetric
from monai.utils import min_version, optional_import

idist, _ = optional_import("ignite", IgniteInfo.OPT_IMPORT_VERSION, min_version, "distributed")

if TYPE_CHECKING:
    from ignite.engine import Engine
    from ignite.metrics import Metric
    from ignite.metrics.metric import reinit__is_reduced
else:
    Engine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine")
    Metric, _ = optional_import("ignite.metrics", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Metric", as_type="base")
    reinit__is_reduced, _ = optional_import(
        "ignite.metrics.metric", IgniteInfo.OPT_IMPORT_VERSION, min_version, "reinit__is_reduced", as_type="decorator"
    )


class IgniteMetricHandler(Metric):
    """
    Base Metric class based on ignite event handler mechanism.
    The input `prediction` or `label` data can be a PyTorch Tensor or numpy array with batch dim and channel dim,
    or a list of PyTorch Tensor or numpy array without batch dim.

    Args:
        metric_fn: callable function or class to compute raw metric results after every iteration.
            expect to return a Tensor with shape (batch, channel, ...) or tuple (Tensor, not_nans).
        loss_fn: A torch _Loss function which is used to generate the LossMetric
        output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then
            construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or
            lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`.
            `engine.state` and `output_transform` inherit from the ignite concept:
            https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial:
            https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb.
        save_details: whether to save metric computation details per image, for example: mean_dice of every image.
            default to True, will save to `engine.state.metric_details` dict with the metric name as key.
        kwargs: keyword argument that will be passed into the LossMetric

    """

    def __init__(
        self,
        metric_fn: CumulativeIterationMetric = None,

Review comment (suggestion):
To provide a deprecation warning about the old name I would create a new subclass of `IgniteMetricHandler` called `IgniteMetric` and decorate this with [deprecated](https://github.com/Project-MONAI/MONAI/blob/17c1e3acff583e241066da08dae4b75554b52dda/monai/utils/deprecate_utils.py#L41). The `since` value should be "1.3" and `removed` should be "1.4". The `msg_suffix` should mention the new name. You can search the repo for examples of using this decorator with functions and classes.  

Code after:

import warnings
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, Any, cast

import torch
from torch.nn.modules.loss import _Loss

from monai.config import IgniteInfo
from monai.metrics import CumulativeIterationMetric, LossMetric
from monai.utils import MetricReduction, deprecated, min_version, optional_import

idist, _ = optional_import("ignite", IgniteInfo.OPT_IMPORT_VERSION, min_version, "distributed")

if TYPE_CHECKING:
    try:
        _, has_ignite = optional_import("ignite")
        from ignite.engine import Engine
        from ignite.metrics import Metric
        from ignite.metrics.metric import reinit__is_reduced
    except ImportError:
        has_ignite = False

else:
    Engine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine")
    Metric, _ = optional_import("ignite.metrics", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Metric", as_type="base")
    reinit__is_reduced, _ = optional_import(
        "ignite.metrics.metric", IgniteInfo.OPT_IMPORT_VERSION, min_version, "reinit__is_reduced", as_type="decorator"
    )


class IgniteMetricHandler(Metric):
    """
    Base Metric class based on ignite event handler mechanism.
    The input `prediction` or `label` data can be a PyTorch Tensor or numpy array with batch dim and channel dim,
    or a list of PyTorch Tensor or numpy array without batch dim.

    Args:
        metric_fn: callable function or class to compute raw metric results after every iteration.
            expect to return a Tensor with shape (batch, channel, ...) or tuple (Tensor, not_nans).
        loss_fn: A torch _Loss function which is used to generate the LossMetric
        output_transform: callable to extract `y_pred` and `y` from `ignite.engine.state.output` then
            construct `(y_pred, y)` pair, where `y_pred` and `y` can be `batch-first` Tensors or
            lists of `channel-first` Tensors. the form of `(y_pred, y)` is required by the `update()`.
            `engine.state` and `output_transform` inherit from the ignite concept:
            https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial:
            https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb.
        save_details: whether to save metric computation details per image, for example: mean_dice of every image.
            default to True, will save to `engine.state.metric_details` dict with the metric name as key.
        reduction: Argument for the LossMetric, look there for details
        get_not_nans: Argument for the LossMetric, look there for details