Example #1
0
def calc_full_ac(image, rad, outside=True, **ac_kwargs):
    import pyximport
    pyximport.install(setup_args={"include_dirs": np.get_include()},
                      reload_support=True)
    import sincint

    assert image.ndim == 2, "wrong dimension"
    assert image.shape[0] == image.shape[1]

    N = image.shape[0]
    FtoT = sincint.genfulltotrunc(N, rad)
    TtoF = FtoT.T
    trunc = FtoT.dot(image.flatten())
    corr_trunc = calc_angular_correlation(trunc, N, rad, **ac_kwargs)
    full_angular_correlation = TtoF.dot(corr_trunc)

    if outside:
        _, _, outside_mask = gencoords_outside(N, 2, rad, True)
        corr_trunc_outside = calc_angular_correlation(
            image[outside_mask.reshape(N, N)].flatten(),
            N,
            rad,
            outside=True,
            **ac_kwargs)
        full_angular_correlation[outside_mask] = corr_trunc_outside

    return full_angular_correlation.reshape(N, N)
Example #2
0
def correlation_trunc_example(M):
    N = M.shape[0]
    rad = 1
    proj = M.sum(axis=0)
    FtoT = sincint.genfulltotrunc(N=N, rad=rad)
    TtoF = sincint.gentrunctofull(N=N, rad=rad)
    trunc = FtoT.dot(proj.flatten())
    corr_trunc = correlation.calc_angular_correlation(trunc, N, 1)
    corr_proj = TtoF.dot(corr_trunc).reshape(N, N)
    fig, ax = plt.subplots(1,2)
    ax[0].imshow(proj)
    ax[1].imshow(corr_proj)
    plt.show()
Example #3
0
def full_to_trunc(full_samples, rad, beamstop_rad=None):
    """convert samples in full shape to samples in truncation"""
    dtype = full_samples.dtype
    if full_samples.ndim == 3:
        num_samples = full_samples.shape[0]
        N = full_samples.shape[1]
    elif full_samples.ndim == 2:
        N = full_samples.shape[0]
        num_samples = 1
        full_samples = (full_samples,)
    
    FtoT = sincint.genfulltotrunc(N=N, rad=rad, beamstop_rad=None)
    N_T = FtoT.shape[0]
    
    trunc_samples = np.zeros((num_samples, N_T), dtype=dtype)
    for i, sample in enumerate(full_samples):
        trunc_samples[i, :] = FtoT.dot(sample.flatten())

    return trunc_samples
Example #4
0
def ctf_correlation_benchmark(N=128):
    akv = 200
    wgh = 0.07
    cs = 2.0
    df1, df2, angast = 44722, 49349, 45.0 * (np.pi / 180.0)
    dscale = 1.0
    psz = 5.6
    rad = 0.99

    rots = np.arange(0, 2 * np.pi, 2*np.pi/360)

    ctf_map = ctf.compute_full_ctf(
        None, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None)
    ctf_rots = ctf.compute_full_ctf(
        rots, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None)
    
    ctf_map = ctf_map.reshape(N, N)
    ctf_rots = ctf_rots.reshape(N, N, -1).transpose((2, 0, 1))

    FtoT = sincint.genfulltotrunc(N=N, rad=rad)
    TtoF = FtoT.T

    corr_ctf_map = TtoF.dot(calc_angular_correlation(
        FtoT.dot(ctf_map.flatten()), N=N, rad=rad
        )).reshape(N, N)

    corr_ctf_rots = np.zeros_like(ctf_rots, dtype=ctf_rots.dtype)
    for i, curr_ctf in enumerate(ctf_rots):
        corr_ctf_rots[i] = TtoF.dot(calc_angular_correlation(
            FtoT.dot(curr_ctf.flatten()), N=N, rad=rad
            )).reshape(N, N)
    
    difference = calc_difference(ctf_map, ctf_rots, only_real=True)
    corr_difference = calc_difference(corr_ctf_map, corr_ctf_rots, only_real=True)

    vis_real_space_comparison(ctf_rots, corr_ctf_rots, difference, corr_difference,
                              original_img=ctf_map, original_corr_img=corr_ctf_map,
                              save_animation=True, animation_name='ctf_correlation_benchmark')
Example #5
0
with open(os.path.join(data_dir, 'ctf_gt.par')) as par:
    par.readline()
    # 'C                 PHI      THETA        PSI        SHX        SHY       FILM        DF1        DF2     ANGAST'
    while True:
        try:
            line = par.readline().split()
            euler_angles.append([float(line[1]), float(line[2]), float(line[3])])
        except Exception:
            break
euler_angles = np.deg2rad(np.asarray(euler_angles))
imgdata = mrc.readMRC(img_file)

num_data = imgdata.shape[2]

rad = 0.8
FtoT = sincint.genfulltotrunc(N=N, rad=rad)

N_T = FtoT.shape[0]

slices = np.zeros((num_data, N_T), dtype=np.float32)

for i in range(num_data):
    curr_img = imgdata[:, :, i]
    slices[i] = FtoT.dot(curr_img.reshape(-1, 1)).reshape(-1)

Rs = np.vstack([geometry.rotmat3D_EA(*ea)[:, 0:2].reshape((1, 3, 2)) for ea in euler_angles])

# cx, cy = int(N/2), int(N/2)
# nx, ny = N, N

# for i, ea in enumerate(euler_angles):