U Ph @sddlmZddlZddlmZddlZddlmZddlm Z ddl m Z Gdd d e Z Gd d d e Z ddddddddddZdddddddddZdS)) annotationsN)Any)ignore_background)MetricReduction)Metriccs@eZdZdZdddddd d fd d Zd d dddZZS)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@?boolstrfloatNone)include_background spatial_mapscalar_reduction thresholdreturncs&t||_||_||_||_dSN)super__init__rrrr)selfrrrr __class__Z/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/active_learning_metrics.pyr)s  zVarianceMetric.__init__r)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 )rrrrr)compute_variancerrrr)rrrrr__call__6s zVarianceMetric.__call__)TFr r __name__ __module__ __qualname____doc__rr __classcell__rrrrrs rcs>eZdZdZdddddfdd Zd d d d d dZZS)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 r)rrrcst||_||_dSr)rrrr)rrrrrrrXs zLabelQualityScore.__init__rtorch.Tensor | None)ryrcCst|||j|jdS)r)rr(rr)label_quality_scorerr)rrr(rrrr]s zLabelQualityScore.__call__)Tr r rrrrr&Isr&TFmeanr z torch.Tensorr r r r')rrrrrrc Cs|}|s |}t||d\}}|||dk<t|j}|dkrP|rPtddS|j}|d|dg}td|D]} ||| qrt ||} tj | ddd } |r| S|t j krt | S|t jkrt| Std |d dS) 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.)r rlenshapewarningswarnrangeappendtorchreshapevarrMEANr*SUMr ValueError) rrrrrr(n_lenZn_shape new_shapeZ each_dim_idxZ y_reshapedvariancerrrrls,         r)rr(rrrcCs|}|}|s$t||d\}}t|j}|dkrL|dkrLtddSt||}|tj krh|S|tj krtj |t t d|dS|tjkrtj|t t d|dStd|d dS) 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.r0r1)r rr2r3r4r5r8absrNONEr;r*listr6r<r r=)rr(rrr>Z abs_diff_maprrrr)s      r))TFr*r )Tr*) __future__rr4typingrr8Zmonai.metrics.utilsr monai.utilsrmetricrrr&rr)rrrr s     0%: