def test_evaluate_model_with_sampling(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "simple_cnn"
        sample_name = "4000_256"

        imgCone = ImageCone(c, Open_Slide())

        # 读取数字全扫描切片图像
        tag = imgCone.open_slide("Testing/images/test_001.tif", None,
                                 "test_001")
        images = imgCone.get_uniform_sampling_blocks(40, 256)
        print("sample count = ", len(images))

        normal = ACDNormalization("acd",
                                  dc_txt="dc.txt",
                                  w_txt="w.txt",
                                  template_path="template_normal")
        normal.prepare(images)

        source_samples = ("P0404", "T_NC_Simple0404_4000_256_test.txt")
        cnn = Simple_Classifier(c,
                                model_name,
                                sample_name,
                                normalization=normal,
                                special_norm=0)
        cnn.evaluate_model(samples_name=source_samples,
                           model_file=None,
                           batch_size=20,
                           max_count=None)
    def test_evaluate_model3(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        # model_name = "se_densenet_40"
        # model_name = "simple_cnn"
        # model_name = "resnet_18"
        model_name = "densenet_22"
        sample_name = "4000_256"

        cnn = Simple_Classifier(c,
                                model_name,
                                sample_name,
                                normalization=None,
                                special_norm=-1)

        result = cnn.evaluate_models(
            samples_name=("P0404", "T_NC_Simple0404_4000_256_test.txt"),
            model_directory="{}/models/pytorch/{}_{}".format(
                c.PROJECT_ROOT, model_name, sample_name),
            batch_size=60,
            max_count=None)

        pd.set_option('display.max_colwidth', 300)
        pd.set_option('display.max_rows', None)
        pd.set_option('display.max_columns', None)
        pd.set_option('display.width', 2000)
        print(result)
    def test_evaluate_model2(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        # model_name = "se_densenet_40"
        # sample_name = "x_256"
        # model_name = "simple_cnn"
        model_name = "e_densenet_22"
        sample_name = "4000_256"

        normal = None

        cnn = Simple_Classifier(c,
                                model_name,
                                sample_name,
                                normalization=normal,
                                special_norm=-1)

        # Check_P0430_4000_256_test,
        # T1_P0430_4000_256_train,
        # T1_P0430_4000_256_test.txt
        Xtest, Ytest, predicted_tags, features = cnn.evaluate_model(
            samples_name=("P0430", "T1_P0430_4000_256_train.txt"),
            model_file=None,
            batch_size=100,
            max_count=None)
        cnn.evaluate_accuracy_based_slice(Xtest, predicted_tags, Ytest)
    def test_export_ONNX_model(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        # model_name = "se_densenet_22"
        model_name = "e_densenet_22"
        sample_name = "4000_256"

        cnn = Simple_Classifier(c, model_name, sample_name)

        cnn.export_ONNX_model()
    def test_train_model_cifar(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        # model_name = "simple_cnn"
        model_name = "e_densenet_40"
        sample_name = "cifar10"

        cnn = Simple_Classifier(c, model_name, sample_name)
        cnn.train_model(samples_name=None,
                        augment_func=None,
                        batch_size=32,
                        epochs=200)
    def test_train_model_patholImg(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        mode = 41

        if mode == 1:
            model_name = "simple_cnn"
            sample_name = "4000_256"
        elif mode == 2:
            model_name = "densenet_22"
            sample_name = "4000_256"
        elif mode == 3:
            model_name = "e_densenet_22"
            sample_name = "4000_256"
        elif mode == 4:
            model_name = "e_densenet_40"
            sample_name = "4000_256"
        elif mode == 41:
            model_name = "e_densenet_40"
            sample_name = "2000_256"
        elif mode == 5:
            model_name = "se_densenet_40"
            sample_name = "4000_256"
        elif mode == 6:
            model_name = "resnet_18"
            sample_name = "4000_256"
        elif mode == 7:
            pass

        cnn = Simple_Classifier(c, model_name, sample_name)
        samples = [
            ("P0619", "T1_P0619_4000_256"),  # 平衡样本集
            ("P0619", "S1_P0619_2000_256"),  # 平衡样本集
            ("P0619", "A1_P0619_4k2k_256"),  # 平衡样本集
        ]

        # cnn.train_model(samples_name=("P0430","T1_P0430_4000_256"), class_weight=[0.2391, 1.0],
        #                 augment_func = None, batch_size=30, epochs = 10)
        # cnn.train_model(samples_name=samples[1], cl ass_weight=None,
        #                 augment_func = None, batch_size=30, epochs = 10)
        cnn.train_model_A2(samples_name=samples[1],
                           augment_func=None,
                           class_weight=None,
                           batch_size=30,
                           loss_weight=0.001,
                           epochs=10)
    def test_predict_on_batch(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "simple_cnn"
        sample_name = "4000_256"

        cnn = Simple_Classifier(c, model_name, sample_name)

        imgCone = ImageCone(c, Open_Slide())

        # 读取数字全扫描切片图像
        tag = imgCone.open_slide("Train_Tumor/Tumor_004.tif", None,
                                 "Tumor_004")
        seeds = [(69400, 98400), (70800, 98400), (27200, 113600)]  # C, C, S,
        result = cnn.predict_on_batch(imgCone, 40, 256, seeds, 2)
        print(result)
    def test_evaluate_model(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        # model_name = "se_densenet_40"
        # sample_name = "x_256"
        # model_name = "simple_cnn"
        model_name = "e_densenet_22"
        sample_name = "4000_256"

        # normal = HistNormalization("match_hist", hist_target ="hist_templates_2048.npy",
        #                            hist_source = "hist_source_P0404.npy")
        # normal = HistNormalization("match_hist", hist_target = "hist_soures_P0327.npy",
        #                             hist_source = "hist_soures_P0330.npy")
        # normal = HSDNormalization("hsd_norm", target_mean=( -0.2574, 0.2353, 0.3893),
        #                           target_std=(0.1860, 0.1884, 0.2482),
        #                           source_mean=(-0.0676, 0.4088, 0.3710),
        #                           source_std=(0.1254, 0.1247, 0.1988))
        # normal = ReinhardNormalization("reinhard", target_mean=(72.66, 16.89, -9.979),
        #                             target_std=(13.42, 5.767, 4.891),
        #                             source_mean=(72.45, 17.63, -17.77),
        #                             source_std=(10.77, 7.064, 6.50))

        normal = None
        # normal = ImageAugmentation(l_range = (0.95, 1.05), a_range = (0.95, 1.05),
        #                            b_range = (0.95, 1.05), constant_range = (-10, 10))

        # normal = ACDNormalization_tf("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="template_normal")

        # normal = ACDNormalization("acd", dc_txt="dc.txt", w_txt="w.txt", template_path="Tumor_025")
        # source_samples = ("P0404", "T_NC_Simple0404_4000_256_test.txt")
        # patch_root = c.PATCHS_ROOT_PATH[source_samples[0]]
        # sample_filename = source_samples[1]
        # train_list = "{}/{}".format(patch_root, sample_filename)
        #
        # Xtrain, Ytrain = util.read_csv_file(patch_root, train_list)
        #
        # # prepare
        # images = []
        # for patch_file in Xtrain:
        #     img = io.imread(patch_file, as_gray=False)
        #     images.append(img)
        #
        # normal.prepare(images)

        cnn = Simple_Classifier(c,
                                model_name,
                                sample_name,
                                normalization=normal,
                                special_norm=-1)
        # cnn.evaluate_model(samples_name=("P0330", "T_NC_Simple0330_4000_256_test.txt"),
        #                    model_file=None, batch_size=20, max_count=None)

        # cnn.evaluate_model(samples_name=("P0404", "T_NC_Y0404_4000_256_test.txt"),
        #                    model_file=None, batch_size=20, max_count=None)

        # cnn.evaluate_model(samples_name=("P0404", "T_NC_W0404_4000_256_test.txt"),
        #                    model_file=None, batch_size=20, max_count=None)

        # cnn.evaluate_model(samples_name=("P0330", "T_NC_Simple0330_4000_256_test.txt"),
        #                    model_file=None, batch_size=60, max_count=None)

        cnn.evaluate_model(
            samples_name=("P0404", "T_NC_Simple0404_4000_256_test.txt"
                          ),  # T_NC_Simple0404_4000_256_test
            model_file=None,
            batch_size=20,
            max_count=None)