def test_save_superpixels(self):
        test_list = range(50, 131)

        for id in test_list:

            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                     None, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            seg = Segmentation(c, imgCone)
            label_map = detector.genrating_superpixels(seg, x1, y1, x2, y2, 1.25, superpixel_area=1000)

            save_filename = "{}/data/Segmentations/{}_labelmap.npz".format(c.PROJECT_ROOT, imgCone.slice_id)

            np.savez_compressed(save_filename, x1=x1, y1=y1, x2=x2, y2=y2, scale=1.25, labelmap=label_map)
            print(">>> >>> ", save_filename, " saved!")
    def test_adaptive_detect_region(self):
        # # train set
        # test_set = {1: (1, 2100, 3800, 2400, 4000),
        #             2: (3, 2400, 4700, 2600, 4850),  # 小的局部150 x 200
        #             3: (3, 2000, 4300, 2800, 4900),  # 600 x 800
        #             4: (3, 721, 3244, 3044, 5851),  # 全切片范围
        #             5: (44, 410, 2895, 2813, 6019),  #
        #             6: (47, 391, 2402, 2891, 4280),  #
        #             }
        #
        # id = 3
        # roi = test_set[id]
        # id = roi[0]
        # x1 = roi[1]
        # y1 = roi[2]
        # x2 = roi[3]
        # y2 = roi[4]

        x1, y1, x2, y2 = 0, 0, 0, 0
        id = 2
        slice_id = "Tumor_{:0>3d}".format(id)

        c = Params()
        c.load_config_file(JSON_PATH)
        imgCone = ImageCone(c, Open_Slide())

        # 读取数字全扫描切片图像
        tag = imgCone.open_slide("Train_Tumor/%s.tif" % slice_id,
                                 'Train_Tumor/%s.xml' % slice_id, slice_id)

        detector = AdaptiveDetector(c, imgCone)

        if x2 * y2 == 0:
            eff_zone = imgCone.get_effective_zone(1.25)
            x1, y1, x2, y2 = imgCone.get_mask_min_rect(eff_zone)
            print("x1, y1, x2, y2: ", x1, y1, x2, y2)

        history = detector.process(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                   max_iter_nums=100, batch_size=100,
                                   limit_sampling_density=3, enhanced=True)
        detector.save_result_history(x1, y1, x2, y2, 1.25, history)
    def test_adaptive_detect_region_train_slice_normal_part(self):
        # test set
        test_list = range(1, 161)
        # test_list = [3,5,6,7,9,12,14]
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in test_list:

            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Train_Normal/%s.tif" % slice_id,
                                     None, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=20, batch_size=100,
                                               limit_sampling_density=1, enhanced=True,
                                               superpixel_area=1000, superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)
    def test_adaptive_detect_region_test_slice_normal_part(self):
        # test set
        # test_list = [3, 5, 6, 7, 9, 12, 14, 15, 17, 18, 19, 20, 22, 23, 24, 25, 28, 31, 32, 34, 35, 36, 37, 39, 41, 42, 43, 44, 45,
        #  47, 49, 50, 53, 54, 55, 56, 57, 58, 59, 60, 62, 63, 67, 70, 72, 76, 77, 78, 80, 81, 83, 85, 86, 87, 88, 89, 91,
        #  92, 93, 95, 96, 98, 100, 101, 103, 106, 107, 109, 111, 112, 114, 115, 118, 119, 120, 123, 124, 125, 126, 127,
        #  128, 129, 130]
        test_list = [111, 112, 114, 115, 118, 119, 120, 123, 124, 125, 126, 127,
                 128, 129, 130]
        # test_list = [3,5,6,7,9,12,14]
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in test_list:

            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                     None, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=10, batch_size=100,
                                               limit_sampling_density=1, enhanced=False,
                                               superpixel_area=1000, superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                if imgCone.ano_filename is None:
                    mask_img = np.zeros(src_img.shape, dtype=np.bool)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                                               save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
    def test_adaptive_detect_region_test_slice(self):
        # test set
        # test_list = [1,2,4,8,10,11,13,16,21,26,27,29,30,33,38,40,46,48,51,52,61,64,65,66,68,69,71,73,74,75,79,
        # 82,84,90,94,97,99,102,104,105,108,110,113,116,117,121,122]
        # test_list = [4,10,29,30,33,38,48,66,79,84,99,102,116,117,122]
        test_list = [117]
        # test_list = [16,21,26,27,29,30,33,38,40,46,48,51,52,61,64,65,66,68,69,71,73,74,75,79,
        #              82,84,90,94,97,99,102,104,105,108,110,113,116,117,121,122]
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = True

        for id in test_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Test_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                     'Testing/images/%s.xml' % slice_id, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.process(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                       max_iter_nums=20, batch_size=100,
                                       limit_sampling_density=2, enhanced=True)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                                               save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
    def test_adaptive_detect_region_test(self):
        # test test
        test_set = {1: ("001", 100, 100, 2600, 2700),  # 检测 dice =0.7811
                    1.1: ("001", 800, 1600, 1600, 2300),  # dice = 0.76084
                    16: ("016", 0, 200, 3250, 2900),  # dice = 0.92056
                    21: ("021", 0, 0, 0, 0),  # dice = 0.93743
                    26: ("026", 0, 0, 0, 0),  # 检测,dice c3= 0.7601
                    61: ("061", 0, 0, 0, 0),  # 检测, c3 = 0.75468
                    4: ("004", 0, 0, 0, 0),  # 检测,c3 = 2.5917e-05, 检测区域太小
                    8: ("008", 0, 0, 0, 0),  # 检测,c3 = 0.003159
                    10: ("010", 0, 0, 0, 0),  # 检测,c3 = 3.7647e-05
                    11: ("011", 0, 0, 0, 0),  # 检测,c3 = 0.0005543
                    13: ("013", 0, 0, 0, 0),  # 检测,c3 = 0.003278
                    27: ("027", 0, 0, 0, 0),  # 检测,c3 = 0.9601, 0.3540
                    29: ("029", 0, 0, 0, 0),  # 检测,c3 =
                    30: ("030", 0, 0, 0, 0),  # 检测,c3 = 0.02823
                    33: ("033", 0, 0, 0, 0),  # 检测,c3 =
                    38: ("038", 0, 0, 0, 0),  # 检测,c3 = 0.000214
                    40: ("040", 0, 0, 0, 0),  # 检测,c3 = 0.06608, 检测错误
                    46: ("046", 0, 0, 0, 0),  # 检测,c3 = 0.0005763
                    48: ("048", 0, 0, 0, 0),  # 检测,c3 =
                    51: ("051", 0, 0, 0, 0),  # 检测,c3 = 0.1478
                    52: ("052", 0, 0, 0, 0),  # 检测,c3 =
                    71: ("071", 0, 0, 0, 0),  # 检测,c3 =
                    64: ("064", 0, 0, 0, 0),  # 检测,c3 =
                    65: ("065", 0, 0, 0, 0),  # 检测,c3 =
                    66: ("066", 0, 0, 0, 0),  # 检测,c3 =
                    68: ("068", 0, 0, 0, 0),  # 检测,c3 =
                    69: ("069", 0, 0, 0, 0),  # 检测,c3 =
                    73: ("073", 0, 0, 0, 0),  # 检测,c3 =
                    }

        id = 39

        roi = test_set[id]
        slice_id = roi[0]
        x1 = roi[1]
        y1 = roi[2]
        x2 = roi[3]
        y2 = roi[4]

        c = Params()
        c.load_config_file(JSON_PATH)
        imgCone = ImageCone(c, Open_Slide())

        # 读取数字全扫描切片图像
        # tag = imgCone.open_slide("Tumor/Tumor_%s.tif" % slice_id,
        #                          'Tumor/tumor_%s.xml' % slice_id, "Tumor_%s" % slice_id)
        tag = imgCone.open_slide("Testing/images/test_%s.tif" % slice_id,
                                 'Testing/images/test_%s.xml' % slice_id, "test_%s" % slice_id)

        detector = AdaptiveDetector(c, imgCone)

        if x2 * y2 == 0:
            eff_zone = imgCone.get_effective_zone(1.25)
            x1, y1, x2, y2 = imgCone.get_mask_min_rect(eff_zone)
            print("x1, y1, x2, y2: ", x1, y1, x2, y2)

        cancer_map, history = detector.process(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=100, batch_size=100,
                                               limit_sampling_density=1, )

        src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
        mask_img = detector.get_true_mask_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)

        levels = [0.2, 0.3, 0.5, 0.6, 0.8]
        false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map, mask_img,
                                                                                               levels)

        detector.save_result_history(x1, y1, x2, y2, 1.25, history)

        enable_show = False
        # 存盘输出部分
        if enable_show:
            save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
            Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels, save_path)

            detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)
    def test_adaptive_detect_region_train_slice(self):
        # train set
        # train_list = [9, 11, 16, 26, 39, 47, 58, 68, 72, 76]
        # train_list = [11, 16, 26, 39, 47, 58, 68, 72, 76]
        train_list = range(1,112) # [29,33,61,89,95]range(27,39)
        result = {}
        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in train_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            slice_id = "Tumor_{:0>3d}".format(id)

            c = Params()
            c.load_config_file(JSON_PATH)
            imgCone = ImageCone(c, Open_Slide())

            # 读取数字全扫描切片图像
            tag = imgCone.open_slide("Train_Tumor/%s.tif" % slice_id,
                                     'Train_Tumor/%s.xml' % slice_id, slice_id)

            detector = AdaptiveDetector(c, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(x1, y1, x2, y2, 1.25, extract_scale=40, patch_size=256,
                                               max_iter_nums=20, batch_size=100,
                                               limit_sampling_density=1, enhanced=True,
                                               superpixel_area=1000, superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(c, extract_scale=40, patch_size=256)
                cancer_map = cmb.generating_probability_map(history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.6, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(cancer_map,
                                                                                                       mask_img,
                                                                                                       levels)
                result[slice_id] = (roc_auc, dice)

                # 存盘输出部分
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                # save_path = "{}/results/cancer_pic".format(c.PROJECT_ROOT)
                # Evaluation.save_result_picture(slice_id, src_img, mask_img, cancer_map, history, roc_auc, levels,
                #                                save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))
Ejemplo n.º 8
0
    def test_detect(self):

        slide_list = [1]
        result = {}
        Train_Tag = True

        # 如果输出癌变概率图,并进行评估
        enable_evaluate = False

        for id in slide_list:
            x1, y1, x2, y2 = 0, 0, 0, 0

            if Train_Tag:
                slice_id = "Tumor_{:0>3d}".format(id)
            else:
                slice_id = "Test_{:0>3d}".format(id)

            imgCone = ImageCone(self._params, Open_Slide())

            # 读取数字全扫描切片图像
            if Train_Tag:
                tag = imgCone.open_slide("Train_Tumor/%s.tif" % slice_id,
                                         'Train_Tumor/%s.xml' % slice_id,
                                         slice_id)
            else:
                tag = imgCone.open_slide("Testing/images/%s.tif" % slice_id,
                                         'Testing/images/%s.xml' % slice_id,
                                         slice_id)

            detector = AdaptiveDetector(self._params, imgCone)

            if x2 * y2 == 0:
                x1, y1, x2, y2 = detector.get_detection_rectangle()
                print("x1, y1, x2, y2: ", x1, y1, x2, y2)

            history = detector.adaptive_detect(
                x1,
                y1,
                x2,
                y2,
                1.25,
                extract_scale=40,
                patch_size=256,
                max_iter_nums=20,
                batch_size=10,
                limit_sampling_density=1,
                enhanced=True,
                superpixel_area=1000,
                superpixels_boundaries_spacing=120)

            detector.save_result_history(x1, y1, x2, y2, 1.25, history)

            if enable_evaluate:
                cmb = CancerMapBuilder(self._params,
                                       extract_scale=40,
                                       patch_size=256)
                cancer_map = cmb.generating_probability_map(
                    history, x1, y1, x2, y2, 1.25)

                src_img = detector.get_img_in_detect_area(
                    x1, y1, x2, y2, 1.25, 1.25)
                mask_img = detector.get_true_mask_in_detect_area(
                    x1, y1, x2, y2, 1.25, 1.25)

                levels = [0.3, 0.5, 0.8]
                false_positive_rate, true_positive_rate, roc_auc, dice = Evaluation.evaluate_slice_map(
                    cancer_map, mask_img, levels)
                result[slice_id] = (roc_auc, dice)

                # save results
                # self.show_results(cancer_map, dice, false_positive_rate, history, levels, mask_img, roc_auc, slice_id,
                #                   src_img, true_positive_rate)
                save_path = "{}/results/cancer_pic".format(
                    self._params.PROJECT_ROOT)
                Evaluation.save_result_picture(slice_id, src_img, mask_img,
                                               cancer_map, history, roc_auc,
                                               levels, save_path)
                # detector.save_result_xml(x1, y1, 1.25, cancer_map, levels)

        for slice, (auc, dices) in result.items():
            print("#################{}###################".format(slice))
            for t, value in dices:
                print("threshold = {:.3f}, dice coef = {:.6f}".format(
                    t, value))
            print("ROC auc: {:.6f}".format(auc))
            print("#################{}###################".format(slice))