U Ph.@sddlmZddlmZddlZddlmZmZddlm Z m Z m Z m Z ddl mZe dd d \ZZe dd d \ZZd d gZGdddeZGdd d eZGdd d eZdS)) annotations)castN)do_metric_reductionignore_background)MetricReductionconvert_to_numpyconvert_to_tensoroptional_import)CumulativeIterationMetricz)MetricsReloaded.metrics.pairwise_measuresBinaryPairwiseMeasures)nameMultiClassPairwiseMeasuresMetricsReloadedBinaryMetricsReloadedCategoricalcsReZdZdZdejdfddddddfd d Zdd d dddZddZZ S)MetricsReloadedWrapperaoBase class for defining MetricsReloaded metrics as a CumulativeIterationMetric. Args: metric_name: Name of a metric from the MetricsReloaded package. include_background: whether to include computation on the first channel of the predicted output. Defaults to ``True``. reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction. get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans). Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric. TFstrboolMetricReduction | strNone metric_nameinclude_background reduction get_not_nansreturncs&t||_||_||_||_dS)N)super__init__rrrrselfrrrr __class__J/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/wrapper.pyr/s  zMetricsReloadedWrapper.__init__NzMetricReduction | str | Nonez0torch.Tensor | tuple[torch.Tensor, torch.Tensor])rrcCsB|}t|tjstdt||p(|j\}}|jr>||fS|S)Nz-the data to aggregate must be PyTorch Tensor.) get_buffer isinstancetorchTensor ValueErrorrrr)rrdatafnot_nansr"r"r# aggregate<s  z MetricsReloadedWrapper.aggregatecCs2|}|}|js&t||d\}}|||jfS)z.Prepares onehot encoded input for metric call.)y_predy)floatrrdevice)rr-r.r"r"r#prepare_onehotFs z%MetricsReloadedWrapper.prepare_onehot)N) __name__ __module__ __qualname____doc__rMEANrr,r1 __classcell__r"r"r r#rs rcsJeZdZdZdejdfddddddfd d Zd d d d d dZZS)ra Wraps the binary pairwise metrics of MetricsReloaded. Args: metric_name: Name of a binary metric from the MetricsReloaded package. include_background: whether to include computation on the first channel of the predicted output. Defaults to ``True``. reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction. get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans). Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric. Example: .. code-block:: python import torch from monai.metrics import MetricsReloadedBinary metric_name = "Cohens Kappa" metric = MetricsReloadedBinary(metric_name=metric_name) # first iteration # shape [batch=1, channel=1, 2, 2] y_pred = torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]]) y = torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]) print(metric(y_pred, y)) # second iteration # shape [batch=1, channel=1, 2, 2] y_pred = torch.tensor([[[[1.0, 0.0], [0.0, 0.0]]]]) y = torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]) print(metric(y_pred, y)) # aggregate # shape ([batch=2, channel=1]) print(metric.aggregate(reduction="none")) # tensor([[0.5], [0.2]]) # reset metric.reset() TFrrrrrcstj||||ddSN)rrrr)rrrr r"r#r}s zMetricsReloadedBinary.__init__ torch.Tensorr-r.rcCs|||\}}}|}|dkr2td|d|jddksN|jddkrptd|jdd|jddt|}t|}t||ttd|d d }|j|j krtd |j|j |j}t ||d S) a#Computes a binary (single-class) MetricsReloaded metric from a batch of predictions and references. Args: y_pred: Prediction with dimensions (batch, channel, *spatial), where channel=1. The values should be binarized. y: Ground-truth with dimensions (batch, channel, *spatial), where channel=1. The values should be binarized. Raises: ValueError: when `y_pred` has less than three dimensions. ValueError: when second dimension ~= 1 Hy_pred should have at least 3 dimensions (batch, channel, spatial), got .r zy_pred.shape[1]=z and y.shape[1]=z should be one.h㈵>)axis smooth_drUnsupported metric: r0) r1 ndimensionr(shaperr tuplerangermetricsr)rr-r.r0dimsbpmmetricr"r"r#_compute_tensors" z%MetricsReloadedBinary._compute_tensor r2r3r4r5rr6rrLr7r"r"r r#rOs 0csNeZdZdZdejddfdddddd d fd d Zd d d dddZZS)ra Wraps the categorical pairwise metrics of MetricsReloaded. Args: metric_name: Name of a categorical metric from the MetricsReloaded package. include_background: whether to include computation on the first channel of the predicted output. Defaults to ``True``. reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values, available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``, ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction. get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans). Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric. smooth_dr: a small constant added to the denominator to avoid nan. OBS: should be greater than zero. Example: .. code-block:: python import torch from monai.metrics import MetricsReloadedCategorical metric_name = "Weighted Cohens Kappa" metric = MetricsReloadedCategorical(metric_name=metric_name) # first iteration # shape [bach=1, channel=3, 2, 2] y_pred = torch.tensor([[[[0, 0], [0, 1]], [[0, 0], [0, 0]], [[1, 1], [1, 0]]]]) y = torch.tensor([[[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]]) print(metric(y_pred, y)) # second iteration # shape [batch=1, channel=3, 2, 2] y_pred = torch.tensor([[[[1, 0], [0, 1]], [[0, 1], [1, 0]], [[0, 0], [0, 0]]]]) y = torch.tensor([[[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]]) print(metric(y_pred, y)) # aggregate # shape ([batch=2, channel=1]) print(metric.aggregate(reduction="none")) # tensor([[0.2727], [0.6000]]) # reset metric.reset() TFr?rrrr/r)rrrrrArcstj||||d||_dSr8)rrrA)rrrrrrAr r"r#rsz#MetricsReloadedCategorical.__init__r9r:cCs |||\}}}|}|dkr2td|d|jd}||jd|jdd}|d}||jd|jdd}|d}|}t|}t|}t||tt d||j t t |dd }|j |j krtd |j |j |j }|d }ttjt||d S) aComputes a categorical (multi-class) MetricsReloaded metric from a batch of predictions and references. Args: y_pred: Prediction with dimensions (batch, channel, *spatial). The values should be one-hot encoded and binarized. y: Ground-truth with dimensions (batch, channel, *spatial). The values should be 1 one-hot encoded and binarized. Raises: ValueError: when `y_pred` has less than three dimensions. r;r<r=r r)rr>r T)r@rA list_values is_onehotrB).NrC)r1rDr(rEreshapepermuterrrFrGrAlistrrHrr&r'r)rr-r.r0rI num_classesrJrKr"r"r#rLs2      z*MetricsReloadedCategorical._compute_tensorrMr"r"r r#rs2) __future__rtypingrr&monai.metrics.utilsrr monai.utilsrrrr rKr r _r__all__rrrr"r"r"r# s    0g