o  i@sddlmZddlmZddlmZmZddlZddl m Z m Z m Z ddl mZddlmZddlmZmZmZdd lmZmZerKdd lmZd Zn ed d ed \ZZesZdd ZGdddZdS)) annotations)Iterable) TYPE_CHECKINGAnyN)PathologyProbNMScompute_isolated_tumor_cellscompute_multi_instance_mask)NdarrayOrTensor) WSIReader)compute_fp_tp_probscompute_froc_curve_datacompute_froc_score) min_versionoptional_import)tqdmTrz4.47.0cCs|S)N)xrrj/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/pathology/metrics/lesion_froc.pyr"sc@sLeZdZdZ       d&d'ddZd(ddZdd Zd!d"Zd#d$Zd%S)) LesionFROCa Evaluate with Free Response Operating Characteristic (FROC) score. Args: data: either the list of dictionaries containing probability maps (inference result) and tumor mask (ground truth), as below, or the path to a json file containing such list. `{ "prob_map": "path/to/prob_map_1.npy", "tumor_mask": "path/to/ground_truth_1.tiff", "level": 6, "pixel_spacing": 0.243 }` grow_distance: Euclidean distance (in micrometer) by which to grow the label the ground truth's tumors. Defaults to 75, which is the equivalent size of 5 tumor cells. itc_diameter: the maximum diameter of a region (in micrometer) to be considered as an isolated tumor cell. Defaults to 200. 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. nms_sigma: the standard deviation for gaussian filter of non-maximal suppression. Defaults to 0.0. nms_prob_threshold: the probability threshold of non-maximal suppression. Defaults to 0.5. nms_box_size: the box size (in pixel) to be removed around the pixel for non-maximal suppression. image_reader_name: the name of library to be used for loading whole slide imaging, either CuCIM or OpenSlide. Defaults to CuCIM. Note: For more info on `nms_*` parameters look at monai.utils.prob_nms.ProbNMS`. Kg??r0cuCIMdata list[dict] grow_distanceint itc_diametereval_thresholdstuple nms_sigmafloatnms_prob_threshold nms_box_sizeimage_reader_namestrreturnNonec Cs6||_||_||_||_t||_t|||d|_dS)N)sigmaprob_thresholdbox_size)r r"r$r%r image_readerrnms) selfr r"r$r%r'r)r*r+rrr__init__Ds   zLesionFROC.__init__sampledict)tuple[np.ndarray, np.ndarray, np.ndarray]cCs`t|d}|j||dd}|rt|\}}}nggg}}}t|t|t|fS)zH Prepare the probability map for detection evaluation. prob_maplevel)Z probs_mapresolution_level)nploadr3ziparray)r4r6r9Z nms_outputsprobsx_coordy_coordrrrprepare_inference_resultVs z#LesionFROC.prepare_inference_resultcCs|j|d}|jj||dddd}|dtd|d}|j|d}t||d}|j|j|}t||d}||fS) zY Prepare the ground truth for evaluation based on the binary tumor mask tumor_maskr:)r:r pixel_spacingr)mask threshold)rDrG)r2readget_datapowr"rr$r)r4r6Zimg_objrDZmask_pixel_spacingZgrow_pixel_thresholdZ itc_threshold itc_labelsrrrprepare_ground_truthls  zLesionFROC.prepare_ground_truthc Csg}g}d}t|j}t|jD].}||\}}}||\} } t|||| | |dd\} } } || || || 7}qt|t|||fS)z Compute false positive and true positive probabilities for tumor detection, by comparing the model outputs with the prepared ground truths for all samples rr:)r@rBrAevaluation_masklabels_to_excluder;) lenr rrCrLr extendr<r?)r4Ztotal_fp_probsZtotal_tp_probsZtotal_num_targets num_imagesr6r@rBrAZ ground_truthrKfp_probstp_probs num_targetsrrr compute_fp_tps&    zLesionFROC.compute_fp_tpcCs8|\}}}}t||||d\}}t|||jd}|S)z Evaluate the detection performance of a model based on the model probability map output, the ground truth tumor mask, and their associated metadata (e.g., pixel_spacing, level) )rRrSrTrQ) fps_per_imagetotal_sensitivityr%)rUr r r%)r4rRrSrTrQrVrWZ froc_scorerrrevaluates zLesionFROC.evaluateN)rrrrrrr)r r!r"r#r$r#r%r&r'r(r)r(r*r#r+r,r-r.)r6r7r-r8) __name__ __module__ __qualname____doc__r5rCrLrUrXrrrrr&s   r) __future__rcollections.abcrtypingrrnumpyr<Zmonai.apps.pathology.utilsrrr monai.configr monai.data.wsi_readerr monai.metricsr r r monai.utilsrrrhas_tqdmrrrrrs