o  i.@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)nameMultiClassPairwiseMeasuresMetricsReloadedBinaryMetricsReloadedCategoricalcsBeZdZdZdejdfdfd d Z ddddZddZZ 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. TF metric_namestrinclude_backgroundbool reductionMetricReduction | str get_not_nansreturnNonecs&t||_||_||_||_dSN)super__init__rrrrselfrrrr __class__W/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/wrapper.pyr/s  zMetricsReloadedWrapper.__init__NMetricReduction | str | None0torch.Tensor | tuple[torch.Tensor, torch.Tensor]cCsB|}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|}|}|jst||d\}}|||jfS)z.Prepares onehot encoded input for metric call.)y_predy)floatrrdevice)rr/r0r"r"r#prepare_onehotFs  z%MetricsReloadedWrapper.prepare_onehot rrrrrrrrrrr)rr$rr%) __name__ __module__ __qualname____doc__rMEANrr.r3 __classcell__r"r"r r#rs  rcs6eZdZdZdejdfdfd d ZdddZZS)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() TFrrrrrrrrrcstj||||ddSN)rrrr)rrrr r"r#r}s  zMetricsReloadedBinary.__init__r/ torch.Tensorr0cCs|||\}}}|}|dkrtd|d|jddks'|jddkr8td|jdd|jddt|}t|}t||ttd|d d }|j|j vr[td |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: r2) r3 ndimensionr*shaperr tuplerangermetricsr)rr/r0r2dimsbpmmetricr"r"r#_compute_tensors"  z%MetricsReloadedBinary._compute_tensorr4r/r<r0r<rr< r5r6r7r8rr9rrNr:r"r"r r#rOs0cs8eZdZdZdejddfdfdd ZdddZZS)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() TFrArrrrrrrrCr1rrcstj||||d||_dSr;)rrrC)rrrrrrCr r"r#rs z#MetricsReloadedCategorical.__init__r/r<r0cCs |||\}}}|}|dkrtd|d|jd}||jd|jdd}|d}||jd|jdd}|d}|}t|}t|}t||tt d||j t t |dd }|j |j vrptd |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)rBrC list_values is_onehotrD).NrE)r3rFr*rGreshapepermuterrrHrIrClistrrJrr(r)r)rr/r0r2rK num_classesrLrMr"r"r#rNs2      z*MetricsReloadedCategorical._compute_tensor) rrrrrrrrrCr1rrrOrPr"r"r r#rs2) __future__rtypingrr(monai.metrics.utilsrr monai.utilsrrrr rMr r _r__all__rrrr"r"r"r#s   0g