コード例 #1
0
    def __init__(self, feature_function=None, modelparams={}):
        from pysegbase import pycut
        self.working_voxelsize_mm = [1.5, 1.5, 1.5]
        self.data = None
        self.target = None
        #         self.cl = sklearn.naive_bayes.GaussianNB()
        #         self.cl = sklearn.mixture.GMM()
        #self.cl = sklearn.tree.DecisionTreeClassifier()
        if feature_function is None:
            feature_function = intensity_localization_fv


#         self.feature_function = feature_function

        modelparams_working = {
            'fv_type': "fv_extern",
            'fv_extern': feature_function,
            'type': 'gmmsame',
            'params': {
                'cvtype': 'full',
                'n_components': 15
            },
            'adaptation': 'original_data',
        }
        modelparams = add_fv_extern_into_modelparams(modelparams)
        modelparams_working.update(modelparams)
        self.cl = pycut.Model(modelparams=modelparams_working)
コード例 #2
0
def automatic_liver_seeds(
        data3d,
        seeds,
        voxelsize_mm,
        fn_mdl='~/lisa_data/liver_intensity.Model.p',
        return_likelihood_difference=True,
        gaussian_sigma_mm=[20,20,20]):
    from pysegbase import pycut
    # fn_mdl = op.expanduser(fn_mdl)
    mdl = pycut.Model({'mdl_stored_file':fn_mdl, 'fv_extern': organ_model.intensity_localization_fv})
    working_voxelsize_mm = np.asarray([1.5, 1.5, 1.5])
    gaussian_sigma_mm = np.asarray(gaussian_sigma_mm)

    data3dr = imtools.resize_to_mm(data3d, voxelsize_mm, working_voxelsize_mm)

    lik1 = mdl.likelihood_from_image(data3dr, working_voxelsize_mm, 0)
    lik2 = mdl.likelihood_from_image(data3dr, working_voxelsize_mm, 1)

    dl = lik2 - lik1

    seeds = add_negative_notrain_seeds(seeds,lik1, lik2)


    # Liver seed center
    import scipy

    # seed tam, kde je to nejpravděpodovnější - moc nefunguje při blbém natrénování
    dst = scipy.ndimage.filters.gaussian_filter(dl, sigma=gaussian_sigma_mm/working_voxelsize_mm)
    # seed1 = np.unravel_index(np.argmax(dl), dl.shape)

    # escte jinak
    # dáme seed doprostřed oblasti
    # dst = scipy.ndimage.morphology.distance_transform_edt(dl>0)

    seed1 = np.unravel_index(np.argmax(dst), dst.shape)
    # alternativa -
    seed1_mm = seed1 * working_voxelsize_mm
    print 'seed1 ', seed1, ' shape ', dst.shape

    seed1z = seed1[0]
    seed1z_mm = seed1_mm[0]
    print seed1z_mm


    add_negative_train_seeds_blobs(
        dl < 0,
        seeds,
        working_voxelsize_mm,
        voxelsize_mm,
        seed1z_mm, n_seed_blob=3)

    seeds = data_manipulation.add_seeds_mm(
        seeds, voxelsize_mm,
        [seed1_mm[0]],
        [seed1_mm[1]],
        [seed1_mm[2]],
        label=1,
        radius=25,
        width=1
    )
    # import sed3
    # sed3.show_slices(data3dr, dl > 40.0, slice_step=10)
    if return_likelihood_difference:
        return seeds, dl
    else:
        return seeds
コード例 #3
0
def model_score_from_sliver_data(
    #         output_file="~/lisa_data/liver_intensity.Model.p",
    sliver_reference_dir='~/data/medical/orig/sliver07/training/',
    orig_pattern="*orig*[1-9].mhd",
    ref_pattern="*seg*[1-9].mhd",
    modelparams={},
    likelihood_ratio=0.5,
    savefig=False,
    savefig_fn_prefix='../graphics/bn-symmetry-',
    show=False,
    label='',
):
    """

    :param label: text label added to all records in output table
    :param sliver_reference_dir:
    :param orig_pattern:
    :param ref_pattern:
    :param modelparams:
    :param likelihood_ratio: float number between 0 and 1, scalar or list. Set the segmentation threshodl
    :param savefig:
    :param savefig_fn_prefix:
    :param show: show images
    :return:
    """
    import pandas as pd
    from pysegbase import pycut
    import sed3
    import matplotlib.pyplot as plt

    import volumetry_evaluation
    sliver_reference_dir = op.expanduser(sliver_reference_dir)

    orig_fnames = glob.glob(sliver_reference_dir + orig_pattern)
    ref_fnames = glob.glob(sliver_reference_dir + ref_pattern)

    orig_fnames.sort()
    ref_fnames.sort()

    evaluation_all = []

    for oname, rname in zip(orig_fnames, ref_fnames):
        print(oname)
        data3d_orig, metadata = io3d.datareader.read(oname)
        vs_mm1 = metadata['voxelsize_mm']
        data3d_seg, metadata = io3d.datareader.read(rname)
        vs_mm = metadata['voxelsize_mm']

        mdl = pycut.Model(modelparams=modelparams)
        #     m0 = mdl.mdl[2]
        #     len(m0.means_)

        vs_mmr = [1.5, 1.5, 1.5]
        data3dr = io3d.misc.resize_to_mm(data3d_orig, vs_mm1, vs_mmr)
        lik1 = mdl.likelihood_from_image(data3dr, vs_mmr, 0)
        lik2 = mdl.likelihood_from_image(data3dr, vs_mmr, 1)

        if np.isscalar(likelihood_ratio):
            likelihood_ratio = [likelihood_ratio]

        for likelihood_ratio_i in likelihood_ratio:
            if (likelihood_ratio_i <= 0) or (likelihood_ratio_i >= 1.0):
                logger.error("likelihood ratio should be between 0 and 1")

            seg = ((likelihood_ratio_i * lik1) >
                   ((1.0 - likelihood_ratio_i) * lik2)).astype(np.uint8)
            #     seg = (lik1).astype(np.uint8)

            seg_orig = io3d.misc.resize_to_shape(seg, data3d_orig.shape)
            #       seg_orig = io3d.misc.resize_to_shape(seg, data3d_orig.shape)
            if show:
                plt.figure(figsize=(15, 15))
                sed3.show_slices(data3d_orig,
                                 seg_orig,
                                 show=False,
                                 slice_step=20)
                # likres = io3d.misc.resize_to_shape(lik1, data3d_orig.shape)
                # sed3.show_slices(likres , seg_orig, show=False, slice_step=20)

            import re
            numeric_label = re.search(".*g(\d+)", oname).group(1)

            #     plt.figure(figsize = (5,5))
            if savefig:
                plt.axis('off')
                #     plt.imshow(symmetry_img)
                filename = savefig_fn_prefix + numeric_label + '-lr-' + str(
                    likelihood_ratio_i) + ".png"
                # if np.isscalar(likelihood_ratio):
                #     filename = filename + ''+str
                plt.savefig(filename, bbox_inches='tight')

            evaluation = volumetry_evaluation.compare_volumes_sliver(
                seg_orig, data3d_seg, vs_mm)
            evaluation['likelihood_ratio'] = likelihood_ratio_i
            evaluation['numeric_label'] = numeric_label
            evaluation['label'] = label

            evaluation_all.append(evaluation)


#         print evaluation

    ev = pd.DataFrame(evaluation_all)
    return ev