U Ph- @sddlmZddlmZmZddlZddlZddlm Z dddddddd d Z ddddddd dd d dZ ddd d ddddZ ddddddddZ dS)) annotations)AnycastN)NdarrayOrTensorrz list | Nonez,tuple[NdarrayOrTensor, NdarrayOrTensor, int])probscoordsevaluation_masklabels_to_excludereturnc Csft|t|ks*td|jd|jdt|jdkrL|jdt|jksftd|jd|jdt|tjr|}t|tjr|}t|tjr|}|dkrg}t |}t j |ft j d}|t |j}|t |d k}td|dD]8}||kr||kr|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%G/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/froc.pycompute_fp_tp_probs_nds."    " r'r)ry_coordx_coordrr resolution_levelr cCst|tjr|}t|tjr8|}|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 ) rrrrrrpowastyperrstackr')rr(r)rr r*stackedr%r%r&compute_fp_tp_probsNs  r1znp.ndarray | torch.Tensorztuple[np.ndarray, np.ndarray])r"r!r$ num_imagesr c Cst|t|stdt|tjr2|}t|tjrN|}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.r Nr)rtypeAssertionErrorrrrrrsortedsetlistappendsumrasarrayfloat) r"r!r$r2Z total_fpsZ total_tpsZ all_probsthresh fps_per_imagetotal_sensitivityr%r%r&compute_froc_curve_datazs      r?g?g?r r+z np.ndarrayrr)r=r>eval_thresholdsr cCs,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)r=r>rCZ interp_sensr%r%r&compute_froc_scores"rG)N)Nr)r@) __future__rtypingrrrrr monai.configrr'r1r?rGr%r%r%r& s  =,%