o  i-@srddlmZddlmZmZddlZddlZddlm Z  d&d'd dZ  d(d)ddZ d*ddZ d+d,d$d%Z dS)-) annotations)AnycastN)NdarrayOrTensorprobsrcoordsevaluation_masklabels_to_exclude list | Nonereturn,tuple[NdarrayOrTensor, NdarrayOrTensor, int]c Cs`t|t|kstd|jd|jdt|jdkr&|jdt|jks3td|jd|jdt|tjrA|}t|tjrO|}t|tjr]|}|durcg}t |}t j |ft j d}|t |j}|t |d k}td|dD]}||vr||vr|t ||k ||d<q|t|} ||tt| fS) ar This function is modified from the official evaluation code of `CAMELYON 16 Challenge `_, and used to distinguish true positive and false positive predictions. A true positive prediction is defined when the detection point is within the annotated ground truth region. Args: probs: an array with shape (n,) that represents the probabilities of the detections. Where, n is the number of predicted detections. coords: an array with shape (n, n_dim) that represents the coordinates of the detections. The dimensions must be in the same order as in `evaluation_mask`. evaluation_mask: the ground truth mask for evaluation. labels_to_exclude: labels in this list will not be counted for metric calculation. Returns: fp_probs: an array that contains the probabilities of the false positive detections. tp_probs: an array that contains the probabilities of the True positive detections. num_targets: the total number of targets (excluding `labels_to_exclude`) for all images under evaluation. zthe length of probs z", should be the same as of coords .zcoords z9 need to represent the same number of dimensions as mask N)dtyper)len ValueErrorshape isinstancetorchTensordetachcpunumpynpmaxzerosfloat32tupleTwhererangerint) rrrr max_labeltp_probsZ hittedlabelfp_probsi num_targetsr'T/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/metrics/froc.pycompute_fp_tp_probs_nds0"     r)y_coordx_coordresolution_levelr!cCst|tjr|}t|tjr|}|td|t}|td|t}t j ||gdd}t ||||dS)a This function is modified from the official evaluation code of `CAMELYON 16 Challenge `_, and used to distinguish true positive and false positive predictions. A true positive prediction is defined when the detection point is within the annotated ground truth region. Args: probs: an array with shape (n,) that represents the probabilities of the detections. Where, n is the number of predicted detections. y_coord: an array with shape (n,) that represents the Y-coordinates of the detections. x_coord: an array with shape (n,) that represents the X-coordinates of the detections. evaluation_mask: the ground truth mask for evaluation. labels_to_exclude: labels in this list will not be counted for metric calculation. resolution_level: the level at which the evaluation mask is made. Returns: fp_probs: an array that contains the probabilities of the false positive detections. tp_probs: an array that contains the probabilities of the True positive detections. num_targets: the total number of targets (excluding `labels_to_exclude`) for all images under evaluation. r)axis)rrrr ) rrrrrrpowastyper!rstackr))rr*r+rr r,stackedr'r'r(compute_fp_tp_probsNs  r3r$np.ndarray | torch.Tensorr#r& num_imagestuple[np.ndarray, np.ndarray]c Cst|t|s tdt|tjr|}t|tjr'|}gg}}tt t |t |}|ddD]}| ||k | ||k q>| d| dt |t|}t |t|} || fS)a This function is modified from the official evaluation code of `CAMELYON 16 Challenge `_, and used to compute the required data for plotting the Free Response Operating Characteristic (FROC) curve. Args: fp_probs: an array that contains the probabilities of the false positive detections for all images under evaluation. tp_probs: an array that contains the probabilities of the True positive detections for all images under evaluation. num_targets: the total number of targets (excluding `labels_to_exclude`) for all images under evaluation. num_images: the number of images under evaluation. z&fp and tp probs should have same type.rNr)rtypeAssertionErrorrrrrrsortedsetlistappendsumrasarrayfloat) r$r#r&r5Z total_fpsZ total_tpsZ all_probsthresh fps_per_imagetotal_sensitivityr'r'r(compute_froc_curve_datazs      rCg?g?rr-rA np.ndarrayrBeval_thresholdsrrcCs,t||ddd|ddd}t|S)a This function is modified from the official evaluation code of `CAMELYON 16 Challenge `_, and used to compute the challenge's second evaluation metric, which is defined as the average sensitivity at the predefined false positive rates per whole slide image. Args: fps_per_image: the average number of false positives per image for different thresholds. total_sensitivity: sensitivities (true positive rates) for different thresholds. eval_thresholds: the false positive rates for calculating the average sensitivity. Defaults to (0.25, 0.5, 1, 2, 4, 8) which is the same as the CAMELYON 16 Challenge. N)rinterpmean)rArBrHZ interp_sensr'r'r(compute_froc_scores" rL)N) rrrrrrr r r r )Nr)rrr*rr+rrrr r r,r!r r ) r$r4r#r4r&r!r5r!r r6)rD)rArGrBrGrHrr r) __future__rtypingrrrrr monai.configrr)r3rCrLr'r'r'r(s   = ,%