Beispiel #1
0
def run_vem(data, mask, laplace_init=False):

    mu, sigma = moment_matching(data[mask])

    S = Segmentation(data, mask=mask, mu=mu, sigma=sigma, beta=0)
    S.run(niters=PRE_NITERS)
    S = Segmentation(data, mask=mask, mu=S.mu, sigma=S.sigma,
                     ngb_size=NGB_SIZE, beta=BETA)

    e, f, v, vf = [], [], [], []

    # Initialize ppm with RW optionally
    if laplace_init:
        S = run_rw(S)
        S.ppm[S.mask] = binarize_ppm(S.ppm[S.mask])

    for s in range(len(NITERS)):

        # Run VEM for a few iterations
        S, es, fs, vs, vfs = _run_vem(S, niters=NITERS[s])

        e += es
        f += fs
        v += vs
        vf += vfs

    return S, e, f, v, vf
def run_rw(img, mask, mu, sigma):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                 ngb_size=NGB_SIZE, beta=BETA)
    prior, gamma = config_random_walker(S.ext_field(), BETA)
    q = random_walker(mask, prior, gamma)
    S.ppm[mask] = q
    return S
Beispiel #3
0
def init_laplace(img, mask, mu, sigma):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                 ngb_size=NGB_SIZE, beta=BETA)
    prior, gamma = config_random_walker(S.ext_field(), BETA)
    q = random_walker(mask, prior, gamma)
    if BINARIZE:
        S.ppm[mask] = binarize_ppm(q)
    else:
        S.ppm[mask] = q
    return S
Beispiel #4
0
def init_classical(img, mask, niters=0):
    """
    moment matching following by indep EM
    """
    mu, sigma = moment_matching(img.get_data()[mask])
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                     beta=0.0)
    if niters > 0:
        S.run(niters=niters)
    S.set_markov_prior(BETA)
    return S
Beispiel #5
0
def compare_models(filepath):

    path, fname = split(filepath)
    fid, _ = splitext(fname)

    # Input image
    img = nb.load(join(path, fid + '.nii'))

    # Input mask
    mask = img.get_data() > 0

    # 4k-model
    mu, sigma = moment_matching(img.get_data()[mask], MU4, SIGMA4)
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                     ngb_size=NGB_SIZE, beta=BETA)
    S.run(niters=NITERS)
    save_map(img, S.ppm, mask, fid, stamp='4K')
    save_map(img, S.ppm, mask, fid, stamp='4K_conv', conv_mat=CONV_MAT4)

    # 5k-model
    mu, sigma = moment_matching(img.get_data()[mask], MU5, SIGMA5)
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                     ngb_size=NGB_SIZE, beta=BETA)
    S.run(niters=NITERS)
    print S.mu
    print S.sigma
    save_map(img, S.ppm, mask, fid, stamp='5K')
    save_map(img, S.ppm, mask, fid, stamp='5K_conv', conv_mat=CONV_MAT5)
Beispiel #6
0
def run_naive(img, mask, mu, sigma, niters=1):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma, beta=0)
    S.ve_step()
    return S
def run_naive(img, mask, mu, sigma):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                 ngb_size=NGB_SIZE, beta=BETA)
    field = S.ext_field()
    S.ppm[mask] = (field.T / field.sum(1)).T
    return S
def run_vem(img, mask, mu, sigma):
    S = Segmentation(img.get_data(), mask=mask, mu=mu, sigma=sigma,
                 ngb_size=NGB_SIZE, beta=BETA)
    for it in range(NITERS):
        S.ve_step()
    return S