U Phf@sddlmZddlZddlmZmZddlZer`_. The input `y_pred` and `y` can be a list of `channel-first` Tensor or a `batch-first` Tensor. Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`. Args: average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} Type of averaging performed if not binary classification. Defaults to ``"macro"``. - ``"macro"``: calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - ``"weighted"``: calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label). - ``"micro"``: calculate metrics globally by considering each element of the label indicator matrix as a label. - ``"none"``: the scores for each class are returned. Average | strNone)averagereturncst||_dSN)super__init__r )selfr  __class__I/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/rocauc.pyr5s zROCAUCMetric.__init__ torch.Tensorz!tuple[torch.Tensor, torch.Tensor]y_predyr cCs||fSrr)rrrrrr_compute_tensor9szROCAUCMetric._compute_tensorNzAverage | str | None"np.ndarray | float | npt.ArrayLikecCs@|\}}t|tjr$t|tjs,tdt|||p:|jdS)as Typically `y_pred` and `y` are stored in the cumulative buffers at each iteration, This function reads the buffers and computes the area under the ROC. Args: average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} Type of averaging performed if not binary classification. Defaults to `self.average`. z$y_pred and y must be PyTorch Tensor.)rrr ) get_buffer isinstancetorchTensor ValueErrorcompute_roc_aucr )rr rrrrr aggregate<s zROCAUCMetric.aggregate)N) __name__ __module__ __qualname____doc__rMACROrrr" __classcell__rrrrr sr rfloatrc Cs||krdkr0nnt|t|ks8td|}t|dkrjtd|dtdS|t j ddg|j |j dstd| dtdSt|}|}||}||}d }}}}t|D]} tt|| } | d|kr:|| || dkr:|| 7}|d| 7}q||dkr|| 7}|d| 7}||7}||||d 7}d}}q| dkr||7}q|d7}q||||S) Nrz7y and y_pred must be 1 dimension data with same length.zy values can not be all z(, skip AUC computation and return `Nan`.nanr)dtypedevicez y values must be 0 or 1, but in g) ndimensionlenAssertionErroruniquewarningswarnitemr)equalrtensorr+r,tolistargsortcpunumpyranger) rrZy_uniquenindicesnnegZaucZtmp_posZtmp_negiZy_irrr _calculateNs>0  $     r@r r)rrr r cCsz|}|}|dkr*td|jd|dkrDtd|jd|dkrj|jddkrj|jdd}d}|dkr|jddkr|jdd}|dkrt||S|j|jkrtd |jd |jdt|t}|tjkrt||S| d d| d d}}d d t ||D}|tj kr(|S|tj kr>t |S|tjkrfdd |D}t j||dStd|ddS)aFComputes Area Under the Receiver Operating Characteristic Curve (ROC AUC). Referring to: `sklearn.metrics.roc_auc_score `_. Args: y_pred: input data to compute, typical classification model output. the first dim must be batch, if multi-classes, it must be in One-Hot format. for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. y: ground truth to compute ROC AUC metric, the first dim must be batch. if multi-classes, it must be in One-Hot format. for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} Type of averaging performed if not binary classification. Defaults to ``"macro"``. - ``"macro"``: calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account. - ``"weighted"``: calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label). - ``"micro"``: calculate metrics globally by considering each element of the label indicator matrix as a label. - ``"none"``: the scores for each class are returned. Raises: ValueError: When ``y_pred`` dimension is not one of [1, 2]. ValueError: When ``y`` dimension is not one of [1, 2]. ValueError: When ``average`` is not one of ["macro", "weighted", "micro", "none"]. Note: ROCAUC expects y to be comprised of 0's and 1's. `y_pred` must be either prob. estimates or confidence values. )rr-zPPredictions should be of shape (batch_size, num_classes) or (batch_size, ), got .zLTargets should be of shape (batch_size, num_classes) or (batch_size, ), got r-r)dimz.data shapes of y_pred and y do not match, got z and rcSsg|]\}}t||qSr)r@).0Zy_pred_y_rrr sz#compute_roc_auc..cSsg|] }t|qSr)sum)rDrErrrrFs)weightszUnsupported average: z?, available options are ["macro", "weighted", "micro", "none"].N)r.r shapesqueezer@rrMICROflatten transposezipNONEr'npmeanWEIGHTEDr )rrr Z y_pred_ndimZy_ndimZ auc_valuesrHrrrr!ss<#           r!) __future__rr2typingrrr:rP numpy.typingnptr monai.utilsrrmetricrr r@r'r!rrrr s   1&