def test_create_feature_map(self):
        test_set = {"001": (2100, 3800, 2400, 4000),
                    "003": (2400, 4700, 2600, 4850)}
        id = "003"
        roi = test_set[id]
        x1 = roi[0]
        y1 = roi[1]
        x2 = roi[2]
        y2 = roi[3]

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

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

        # encoder = EncoderFactory(self._params, "idec", "AE_500_32", 16)
        encoder = EncoderFactory(c, "cae", "AE_500_32", 16)
        seg = Segmentation(c, imgCone, encoder)
        # global_seeds =  seg.get_seeds_for_seg(x1, y1, x2, y2, 1.25)
        # print(global_seeds)
        f_map = seg.create_feature_map(x1, y1, x2, y2, 1.25, 5)
        print(f_map.shape)
        np.save("feature_map", f_map)
    def test_create_superpixels(self):
        test_set = [("001", 2100, 3800, 2400, 4000),
                    ("003", 2400, 4700, 2600, 4850)]
        id = 1
        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)

        seg = Segmentation(c, imgCone)
        f_map = seg.create_feature_map(x1, y1, x2, y2, 1.25, 5)
        label_map = seg.create_superpixels(f_map, 0.4, iter_num = 3)

        print(label_map.shape)
        np.save("label_map", label_map)
Ejemplo n.º 3
0
    def test_extract_feature(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "cae"
        sample_name = "cifar10"

        ae = EncoderFactory(c, model_name, sample_name)
        ae.extract_feature(None)
Ejemplo n.º 4
0
    def test_train_model5(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "aae"
        sample_name = "AE_500_32"

        ae = EncoderFactory(c, model_name, sample_name, z_dim=64)
        ae.train_adversarial_ae(batch_size=64, epochs=50)
Ejemplo n.º 5
0
    def test_train_model3(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "ccae"
        sample_name = "cifar10"

        ae = EncoderFactory(c, model_name, sample_name)
        ae.train_ae(batch_size=64, epochs=50)
Ejemplo n.º 6
0
    def test_train_idec(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "idec"
        sample_name = "AE_500_32"

        idec = EncoderFactory(c, model_name, sample_name, 64)
        idec.train_idec(batch_size=64, epochs=100)
Ejemplo n.º 7
0
    def test_pack_samples_DSC_256(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        pack = PatchPack(c)
        pack.create_train_test_data_DSC("P0619", [
            "S2000_256_cancer", "S2000_256_normal", "S2000_256_normal2",
            "S2000_256_edgeinner", "S2000_256_edgeouter"
        ], "T2_P0619_4000_256", 40, 20)
Ejemplo n.º 8
0
    def test_calc_patch_cancer_ratio(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        pack = PatchPack(c)
        pack.calc_patch_cancer_ratio("P0619", [
            "S2000_256_edgeinner", "S2000_256_edgeouter", "S2000_256_cancer",
            "S2000_256_normal", "S4000_256_edgeinner", "S4000_256_edgeouter",
            "S4000_256_cancer", "S4000_256_normal"
        ])
Ejemplo n.º 9
0
    def test_eval_latent_weight(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "cae"
        sample_name = "AE_500_32"

        ae = EncoderFactory(c, model_name, sample_name, 64)
        result = ae.eval_latent_vector_loss(batch_size=64)
        np.save("latent_vector_loss_{}".format(model_name), result)
Ejemplo n.º 10
0
def test_no_plot_msg(params: Params) -> None:
    params.color_space = RGB  # type: ignore
    params.plot = True  # type: ignore
    captured_output = io.StringIO()
    sys.stdout = captured_output
    app.run(FDM, params)
    sys.stdout = sys.__stdout__
    expected = 'Plotting is not implemented for operation fdm!\n'
    assert captured_output.getvalue() == expected

    os.remove(params.result_path)
Ejemplo n.º 11
0
    def test_calc_latent_vector_weight(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "cae"
        sample_name = "AE_500_32"

        ae = EncoderFactory(c, model_name, sample_name, 16)
        loss = np.load("latent_vector_loss_{}.npy".format(model_name))

        print(loss)
Ejemplo n.º 12
0
    def test_02(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "cae"
        sample_name = "AE_500_32"

        ae = EncoderFactory(c, model_name, sample_name, 16)
        model = ae.create_initial_model()
        from torchsummary import summary
        summary(model, input_size=(3, 32, 32), device="cpu")
Ejemplo n.º 13
0
    def test_read_patch_db(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        patches_code = "P0619"
        root_path = c.PATCHS_ROOT_PATH[patches_code]
        data = np.load('{}/patch_mask_ratio.npy'.format(root_path),
                       allow_pickle=True)
        patch_db = data[()]
        print(len(patch_db.values()))
        plt.hist(patch_db.values(), bins=20, range=(0., 1), log=True)
        plt.show()
Ejemplo n.º 14
0
    def test_pack_samples_MSC(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        pack = PatchPack(c)
        pack.create_train_test_data_MSC({
            10: "S1000",
            20: "S2000",
            40: "S4000"
        }, {
            "256_cancer": (1, 3),
            "256_edgeinner": (1, 2),
            "256_edgeouter": (0, 1),
            "256_normal": (0, 0),
        }, 0, 1, "T_NC2_msc_256")
Ejemplo n.º 15
0
def test_params_len() -> None:
    mapping_1 = {'a': 1, 'b': 2, 'c': 3}
    mapping_2 = {'a': 1, 'b': 2, 'c': 3}

    params_1 = Params(mapping_1)
    params_2 = Params(mapping_2)

    assert len(params_1) == len(params_2)
    assert len(params_1) == len(mapping_1)
    assert len(params_2) == len(mapping_2)

    mapping_2 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    params_2 = Params(mapping_2)

    assert len(params_1) != len(params_2)
    assert len(params_2) == len(mapping_2)
def test_make_plot(color_space: str, image_channels: str, source_path: str,
                   reference_path: str) -> None:
    params = Params(
        {
            'color_space': color_space,
            'channels': image_channels,
            'match_proportion': 0.8,
            'src_path': source_path,
            'ref_path': reference_path
        }
    )
    converter = build_cs_converter(params.color_space)

    source = read_image(params.src_path)
    reference = read_image(params.ref_path)

    source = converter.convert(source)
    reference = converter.convert(reference)

    channels = tuple(int(c) for c in params.channels.split(','))
    hist_match = HistogramMatching(channels, params.match_proportion)
    result = hist_match(source, reference)

    file_name = os.path.join(TEST_DIR, 'histogram_matching_plot.png')
    images = hm_plot.Images(source, reference, result)
    hm_plot.make_plot(file_name, images, converter, params.color_space,
                      params.channels)

    assert os.path.exists(file_name) is True

    os.remove(file_name)
Ejemplo n.º 17
0
def test_params_str() -> None:
    mapping = {'a': 10, 'b': 'abc'}
    params = Params(mapping)

    # dict is a hash table, so there is no order guarantee
    params_str_1 = 'a : 10\nb : abc\n'
    params_str_2 = 'b : abc\na : 10\n'
    assert str(params) == params_str_1 or str(params) == params_str_2
Ejemplo n.º 18
0
    def test_extract_feature(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        model_name = "densenet121"

        imgCone = ImageCone(c, Open_Slide())

        # 读取数字全扫描切片图像
        tag = imgCone.open_slide("Tumor/Tumor_004.tif",
                                 None, "Tumor_004")
        seeds = [(34816, 48960), (35200, 48640), (12800, 56832)] # C, C, S,

        fe = Transfer(c, model_name, None)
        # result = fe.extract_feature(imgCone, 20, 256, seeds, 2)
        result = fe.svm_predict_on_batch(imgCone, 20, 256, seeds, 2)
        print(result)
Ejemplo n.º 19
0
    def test_pack_samples_2000_256(self):
        c = Params()
        c.load_config_file(JSON_PATH)

        pack = PatchPack(c)

        data_tag = pack.initialize_sample_tags_byMask("P0619", [
            "S2000_256_cancer", "S2000_256_normal", "S2000_256_normal2",
            "S2000_256_edgeinner", "S2000_256_edgeouter"
        ])

        # data_tag = pack.filtering(data_tag, filter_mask=["Tumor_033", "Tumor_034",
        #                                                  "Tumor_046","Tumor_054","Tumor_061"])
        pack.create_train_test_data(data_tag,
                                    0.95,
                                    0.05,
                                    "S1_P0619_2000_256",
                                    need_balance=True)
Ejemplo n.º 20
0
    def test_train_top_svm(self):
        MODEL_NAME = "densenet121"

        # SAMPLE_FIlENAME = "T_NC_500_128"
        # SAMPLE_FIlENAME = "T_NC_2000_256"
        SAMPLE_FIlENAME = "T_NC_4000_256"

        PATCH_TYPE = "4000_256"
        # PATCH_TYPE = "2000_256"
        # PATCH_TYPE = "500_128"

        train_file = "{}_{}_train_features.npz".format(MODEL_NAME, SAMPLE_FIlENAME)
        test_file = "{}_{}_test_features.npz".format(MODEL_NAME, SAMPLE_FIlENAME)

        c = Params()
        c.load_config_file(JSON_PATH)
        cnn = Transfer(c, MODEL_NAME, PATCH_TYPE)
        cnn.train_top_svm(train_file, test_file)
Ejemplo n.º 21
0
def test_params_dict(data_member_name: str) -> None:
    mapping_1 = {'a': 1, 'b': 2, 'c': 3}
    mapping_2 = {'a': 1, 'b': 2, 'c': 3}

    params_1 = Params(mapping_1)
    params_2 = Params(mapping_2)

    assert len(params_1.__dict__[data_member_name]) == \
        len(params_2.__dict__[data_member_name])
    assert params_1.__dict__[data_member_name] == \
        params_2.__dict__[data_member_name]

    mapping_2 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    params_2 = Params(mapping_2)

    assert len(params_1.__dict__[data_member_name]) != \
        len(params_2.__dict__[data_member_name])
    assert params_1.__dict__[data_member_name] != \
        params_2.__dict__[data_member_name]
Ejemplo n.º 22
0
def fixture_params() -> Params:
    return Params({
        'color_space': GRAY,
        'channels': '0,1,2',
        'match_proportion': MATCH_FULL,
        'verify_input': True,
        'plot': False,
        'source_path': MUNICH_1_PATH,
        'reference_path': MUNICH_2_PATH,
        'result_path': 'application_run_test.png'
    })
Ejemplo n.º 23
0
    def test_show_sample_txt(self):
        c = Params()
        c.load_config_file(
            "D:/CloudSpace/WorkSpace/PatholImage/config/justin2.json")
        sample_txt = "{}/{}".format(c.PATCHS_ROOT_PATH,
                                    "S500_128_False_normal.txt")
        patch_path = c.PATCHS_ROOT_PATH

        filenames_list, labels_list = read_csv_file(patch_path, sample_txt)

        fig = plt.figure(figsize=(8, 10), dpi=100)
        for index, filename in enumerate(filenames_list):
            img = imread(filename)
            pos = index % 20
            plt.subplot(4, 5, pos + 1)
            plt.imshow(img)
            plt.axis("off")

            if pos == 19:
                fig.tight_layout()  # 调整整体空白
                plt.subplots_adjust(wspace=0, hspace=0)  # 调整子图间距
                plt.show()
Ejemplo n.º 24
0
    def test_compare_save_file(self):
        c = Params()
        c.load_config_file(JSON_PATH)
        import numpy as np
        filename = "densenet121_T_NC_2000_256_test_features.npz"
        data_path = "{}/data/pytorch/{}".format(c.PROJECT_ROOT, filename)
        D = np.load(data_path)
        test_features = D['arr_0']
        test_label = D['arr_1']
        data_path = "{}/data/keras/{}".format(c.PROJECT_ROOT, filename)
        D = np.load(data_path)
        test_features2 = D['arr_0']
        test_label2 = D['arr_1']

        d = (test_features - test_features2)
        mean = np.mean(d)
        std = np.std(d)
        print(mean, std)

        mean = np.mean(test_features)
        std = np.std(test_features)
        print(mean, std)
def test_operation_context_builder() -> None:
    params = Params({'color_space': RGB,
                     'channels': '0,1,2',
                     'match_proportion': 1.0,
                     'verify_input': True})
    op_ctx = operation_context_builder.build_operation_context(HM, params)
    assert isinstance(op_ctx.operation, HistogramMatching)
    assert isinstance(op_ctx.converter, ColorSpaceConverter)

    op_ctx = operation_context_builder.build_operation_context(FDM, params)
    assert isinstance(op_ctx.operation, FeatureDistributionMatching)
    assert isinstance(op_ctx.converter, ColorSpaceConverter)

    with pytest.raises(ValueError):
        operation_context_builder.build_operation_context('1337', params)
Ejemplo n.º 26
0
def fixture_params() -> Params:
    mapping = {'param_1': 10, 'param_2': 'abc', 'param_3': 2.45}
    return Params(mapping)
Ejemplo n.º 27
0
def run(ctx: click.core.Context) -> None:
    """ Calls the main program with the parameters available in the context """
    application.run(ctx.command.name, Params(ctx.obj))
Ejemplo n.º 28
0
def test_params_python_keyword() -> None:
    mapping = {'a': 1, 'b': 2, 'import': 3}
    with pytest.raises(NameError):
        assert Params(mapping)
Ejemplo n.º 29
0
def test_params_wrong_identifier() -> None:
    mapping = {'a': 1, 'b': 2, '3.14x': 3}
    with pytest.raises(NameError):
        assert Params(mapping)
Ejemplo n.º 30
0
def test_params_wrong_name_type() -> None:
    mapping = {'a': 1, 'b': 2, 3: 'xyz'}
    with pytest.raises(TypeError):
        assert Params(mapping)  # type: ignore