o  i @sddlmZddlZddlmZddlZddlmZddlm Z ddl m Z Gdd d e Z Gd d d e Z  d d!ddZ d"d#ddZdS)$) annotationsN)Any)ignore_background)MetricReduction)Metriccs6eZdZdZ    ddfdd ZdddZZS)VarianceMetrica Compute the Variance of a given T-repeats N-dimensional array/tensor. The primary usage is as an uncertainty based metric for Active Learning. It can return the spatial variance/uncertainty map based on user choice or a single scalar value via mean/sum of the variance for scoring purposes Args: include_background: Whether to include the background of the spatial image or channel 0 of the 1-D vector spatial_map: Boolean, if set to True, spatial map of variance will be returned corresponding to i/p image dimensions scalar_reduction: reduction type of the metric, either 'sum' or 'mean' can be used threshold: To avoid NaN's a threshold is used to replace zero's TFsumMb@?include_backgroundbool spatial_mapscalar_reductionstr thresholdfloatreturnNonecs&t||_||_||_||_dSN)super__init__r r rr)selfr r rr __class__g/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/active_learning_metrics.pyr)s  zVarianceMetric.__init__y_predrcCst||j|j|j|jdS)( Args: y_pred: Predicted segmentation, typically segmentation model output. It must be N-repeats, repeat-first tensor [N,C,H,W,D]. Returns: Pytorch tensor of scalar value of variance as uncertainty or a spatial map of uncertainty )rr r rr)compute_variancer r rr)rrrrr__call__6s zVarianceMetric.__call__)TFr r ) r r r r rrrrrr)rrrr__name__ __module__ __qualname____doc__rr __classcell__rrrrrs rcs.eZdZdZddfd d ZdddZZS)LabelQualityScorea The assumption is that the DL model makes better predictions than the provided label quality, hence the difference can be treated as a label quality score It can be combined with variance/uncertainty for active learning frameworks to factor in the quality of label along with uncertainty Args: include_background: Whether to include the background of the spatial image or channel 0 of the 1-D vector spatial_map: Boolean, if set to True, spatial map of variance will be returned corresponding to i/p image dimensions scalar_reduction: reduction type of the metric, either 'sum' or 'mean' can be used Tr r r rrrrcst||_||_dSr)rrr r)rr rrrrrXs  zLabelQualityScore.__init__rrytorch.Tensor | NonecCst|||j|jdS)r)rr'r r)label_quality_scorer r)rrr'rrrr]s zLabelQualityScore.__call__)Tr )r r rrrr)rrr'rrr(r rrrrr&Isr&TFmeanr r torch.Tensorr r r rrrrrr(c Cs|}|s|}t||d\}}|||dk<t|j}|dkr(|r(tddS|j}|d|dg}td|D] } ||| q9t ||} tj | ddd } |rU| S|t j kr_t | S|t jkrit| Std |d ) a Args: y_pred: [N, C, H, W, D] or [N, C, H, W] or [N, C, H] where N is repeats, C is channels and H, W, D stand for Height, Width & Depth include_background: Whether to include the background of the spatial image or channel 0 of the 1-D vector spatial_map: Boolean, if set to True, spatial map of variance will be returned corresponding to i/p image dimensions scalar_reduction: reduction type of the metric, either 'sum' or 'mean' can be used threshold: To avoid NaN's a threshold is used to replace zero's Returns: A single scalar uncertainty/variance value or the spatial map of uncertainty/variance rr'rz@Spatial map requires a 2D/3D image with N-repeats and C-channelsNrF)dimunbiasedscalar_reduction= not supported.)rrlenshapewarningswarnrangeappendtorchreshapevarrMEANr*SUMr ValueError) rr r rrr'n_lenZn_shape new_shapeZ each_dim_idxZ y_reshapedvariancerrrrls,         rr'cCs|}|}|st||d\}}t|j}|dkr&|dkr&tddSt||}|tj kr4|S|tj krEtj |t t d|dS|tjkrVtj|t t d|dStd|d ) a The assumption is that the DL model makes better predictions than the provided label quality, hence the difference can be treated as a label quality score Args: y_pred: Input data of dimension [B, C, H, W, D] or [B, C, H, W] or [B, C, H] where B is Batch-size, C is channels and H, W, D stand for Height, Width & Depth y: Ground Truth of dimension [B, C, H, W, D] or [B, C, H, W] or [B, C, H] where B is Batch-size, C is channels and H, W, D stand for Height, Width & Depth include_background: Whether to include the background of the spatial image or channel 0 of the 1-D vector scalar_reduction: reduction type of the metric, either 'sum' or 'mean' can be used to retrieve a single scalar value, if set to 'none' a spatial map will be returned Returns: A single scalar absolute difference value as score with a reduction based on sum/mean or the spatial map of absolute difference r,r-nonez^Reduction set to None, Spatial map return requires a 2D/3D image of B-Batchsize and C-channelsNr)r/r1r2)rrr3r4r5r6r9absrNONEr<r*listr7r=r r>)rr'r rr?Z abs_diff_maprrrr)s      r))TFr*r ) rr+r r r r rrrrrr()Tr*) rr+r'r+r r rrrr() __future__rr5typingrr9Zmonai.metrics.utilsr monai.utilsrmetricrrr&rr)rrrrs     0% :