Example #1
0
def shift_vis(model):
    N = model.shape[0]
    rad = 0.8
    kernel = 'lanczos'
    kernsize = 4

    xy, trunc_xy, truncmask = geometry.gencoords(N, 2, rad, True)
    N_T = trunc_xy.shape[0]
    premult = cryoops.compute_premultiplier(N,
                                            kernel=kernel,
                                            kernsize=kernsize)
    TtoF = sincint.gentrunctofull(N=N, rad=rad)

    fM = density.real_to_fspace(model)
    prefM = density.real_to_fspace(
        premult.reshape((1, 1, -1)) * premult.reshape(
            (1, -1, 1)) * premult.reshape((-1, 1, 1)) * model)

    pt = np.random.randn(3)
    pt /= np.linalg.norm(pt)
    psi = 2 * np.pi * np.random.rand()
    ea = geometry.genEA(pt)[0]
    ea[2] = psi
    print('project model for Euler angel: ({:.2f}, {:.2f}, {:.2f}) degree'.
          format(*np.rad2deg(ea)))

    rot_matrix = geometry.rotmat3D_EA(*ea)[:, 0:2]
    slop = cryoops.compute_projection_matrix([rot_matrix], N, kernel, kernsize,
                                             rad, 'rots')
    # trunc_slice = slop.dot(prefM.reshape((-1,)))
    trunc_slice = cryoem.getslices(prefM, slop)
    fourier_slice = TtoF.dot(trunc_slice).reshape(N, N)
    real_proj = density.fspace_to_real(fourier_slice)

    fig, axes = plt.subplots(4, 4, figsize=(12.8, 8))

    im_real = axes[0, 0].imshow(real_proj)
    im_fourier = axes[1, 0].imshow(np.log(np.abs(fourier_slice)))

    for i, ax in enumerate(axes[:, 1:].T):
        shift = np.random.randn(2) * (N / 4.0)
        S = cryoops.compute_shift_phases(shift.reshape(1, 2), N, rad)[0]

        shift_trunc_slice = S * trunc_slice
        shift_fourier_slice = TtoF.dot(shift_trunc_slice).reshape(N, N)
        shift_real_proj = density.fspace_to_real(shift_fourier_slice)

        ax[0].imshow(shift_real_proj)
        ax[1].imshow(np.log(np.abs(shift_fourier_slice)))
        ax[2].imshow(np.log(shift_fourier_slice.real))
        ax[3].imshow(np.log(shift_fourier_slice.imag))

    fig.tight_layout()
    plt.show()
def genphantomdata(N_D, phantompath, ctfparfile):
    mscope_params = {
        'akv': 200,
        'wgh': 0.07,
        'cs': 2.0,
        'psize': 2.8,
        'bfactor': 500.0
    }
    N = 128
    rad = 0.95
    shift_sigma = 3.0
    sigma_noise = 25.0
    M_totalmass = 80000
    kernel = 'lanczos'
    ksize = 6

    premult = cryoops.compute_premultiplier(N, kernel, ksize)

    tic = time.time()

    N_D = int(N_D)
    N = int(N)
    rad = float(rad)
    psize = mscope_params['psize']
    bfactor = mscope_params['bfactor']
    shift_sigma = float(shift_sigma)
    sigma_noise = float(sigma_noise)
    M_totalmass = float(M_totalmass)

    srcctf_stack = CTFStack(ctfparfile, mscope_params)
    genctf_stack = GeneratedCTFStack(
        mscope_params, parfields=['PHI', 'THETA', 'PSI', 'SHX', 'SHY'])

    TtoF = sincint.gentrunctofull(N=N, rad=rad)
    Cmap = n.sort(
        n.random.random_integers(0,
                                 srcctf_stack.get_num_ctfs() - 1, N_D))

    M = mrc.readMRC(phantompath)
    cryoem.window(M, 'circle')
    M[M < 0] = 0
    if M_totalmass is not None:
        M *= M_totalmass / M.sum()

    V = density.real_to_fspace(
        premult.reshape((1, 1, -1)) * premult.reshape(
            (1, -1, 1)) * premult.reshape((-1, 1, 1)) * M)

    print "Generating data..."
    sys.stdout.flush()
    imgdata = n.empty((N_D, N, N), dtype=density.real_t)

    pardata = {'R': [], 't': []}

    prevctfI = None
    for i, srcctfI in enumerate(Cmap):
        ellapse_time = time.time() - tic
        remain_time = float(N_D - i) * ellapse_time / max(i, 1)
        print "\r%.2f Percent.. (Elapsed: %s, Remaining: %s)      " % (
            i / float(N_D) * 100.0, format_timedelta(ellapse_time),
            format_timedelta(remain_time)),
        sys.stdout.flush()

        # Get the CTF for this image
        cCTF = srcctf_stack.get_ctf(srcctfI)
        if prevctfI != srcctfI:
            genctfI = genctf_stack.add_ctf(cCTF)
            C = cCTF.dense_ctf(N, psize, bfactor).reshape((N**2, ))
            prevctfI = srcctfI

        # Randomly generate the viewing direction/shift
        pt = n.random.randn(3)
        pt /= n.linalg.norm(pt)
        psi = 2 * n.pi * n.random.rand()
        EA = geom.genEA(pt)[0]
        EA[2] = psi
        shift = n.random.randn(2) * shift_sigma

        R = geom.rotmat3D_EA(*EA)[:, 0:2]
        slop = cryoops.compute_projection_matrix([R], N, kernel, ksize, rad,
                                                 'rots')
        S = cryoops.compute_shift_phases(shift.reshape((1, 2)), N, rad)[0]

        D = slop.dot(V.reshape((-1, )))
        D *= S

        imgdata[i] = density.fspace_to_real((C * TtoF.dot(D)).reshape(
            (N, N))) + n.require(n.random.randn(N, N) * sigma_noise,
                                 dtype=density.real_t)

        genctf_stack.add_img(genctfI,
                             PHI=EA[0] * 180.0 / n.pi,
                             THETA=EA[1] * 180.0 / n.pi,
                             PSI=EA[2] * 180.0 / n.pi,
                             SHX=shift[0],
                             SHY=shift[1])

        pardata['R'].append(R)
        pardata['t'].append(shift)

    print "\rDone in ", time.time() - tic, " seconds."
    return imgdata, genctf_stack, pardata, mscope_params
Example #3
0
 def compute_operator(self,interp_params,inds=None):
     pts = self.get_pts(inds)
     return cops.compute_shift_phases(pts,interp_params['N'],interp_params['rad'])
Example #4
0
 def compute_operator(self, interp_params, inds=None):
     pts = self.get_pts(inds)
     return cops.compute_shift_phases(pts, interp_params['N'],
                                      interp_params['rad'])
Example #5
0
EAs_grid = healpix.gen_EAs_grid(nside=2, psi_step=360)
Rs = [geometry.rotmat3D_EA(*EA)[:, 0:2] for EA in EAs_grid]
slice_ops = cryoops.compute_projection_matrix(Rs,
                                              N,
                                              kern='lanczos',
                                              kernsize=ksize,
                                              rad=rad,
                                              projdirtype='rots')

slices_sampled = cryoem.getslices(fM, slice_ops).reshape(
    (EAs_grid.shape[0], trunc_xy.shape[0]))

premult_slices_sampled = cryoem.getslices(prefM, slice_ops).reshape(
    (EAs_grid.shape[0], trunc_xy.shape[0]))

S = cryoops.compute_shift_phases(
    np.asarray([100, -20]).reshape((1, 2)), N, rad)[0]

trunc_slice = slices_sampled[0]
premult_trunc_slice = premult_slices_sampled[0]
premult_trunc_slice_shift = S * premult_slices_sampled[0]

fourier_slice = TtoF.dot(trunc_slice).reshape((N, N))
real_proj = density.fspace_to_real(fourier_slice)
premult_fourier_slice = TtoF.dot(premult_trunc_slice).reshape((N, N))
premult_fourier_slice_shift = TtoF.dot(premult_trunc_slice_shift).reshape(
    (N, N))
premult_real_proj = density.fspace_to_real(premult_fourier_slice)
premult_real_proj_shift = density.fspace_to_real(premult_fourier_slice_shift)

fig, axes = plt.subplots(2, 2)
ax = axes.flatten()
def genphantomdata(N_D, phantompath, ctfparfile):
    # mscope_params = {'akv': 200, 'wgh': 0.07,
    #                  'cs': 2.0, 'psize': 2.8, 'bfactor': 500.0}
    mscope_params = {'akv': 200, 'wgh': 0.07,
                     'cs': 2.0, 'psize': 3.0, 'bfactor': 500.0}
    
    M = mrc.readMRC(phantompath)

    N = M.shape[0]
    rad = 0.95
    shift_sigma = 3.0
    sigma_noise = 25.0
    M_totalmass = 80000
    kernel = 'lanczos'
    ksize = 6

    premult = cryoops.compute_premultiplier(N, kernel, ksize)

    tic = time.time()

    N_D = int(N_D)
    N = int(N)
    rad = float(rad)
    psize = mscope_params['psize']
    bfactor = mscope_params['bfactor']
    shift_sigma = float(shift_sigma)
    sigma_noise = float(sigma_noise)
    M_totalmass = float(M_totalmass)

    srcctf_stack = CTFStack(ctfparfile, mscope_params)
    genctf_stack = GeneratedCTFStack(mscope_params, parfields=[
                                     'PHI', 'THETA', 'PSI', 'SHX', 'SHY'])

    TtoF = sincint.gentrunctofull(N=N, rad=rad)
    Cmap = np.sort(np.random.random_integers(
        0, srcctf_stack.get_num_ctfs() - 1, N_D))

    cryoem.window(M, 'circle')
    M[M < 0] = 0
    if M_totalmass is not None:
        M *= M_totalmass / M.sum()

    V = density.real_to_fspace(
        premult.reshape((1, 1, -1)) * premult.reshape((1, -1, 1)) * premult.reshape((-1, 1, 1)) * M)

    print("Generating data...")
    sys.stdout.flush()
    imgdata = np.empty((N_D, N, N), dtype=density.real_t)

    pardata = {'R': [], 't': []}

    prevctfI = None
    for i, srcctfI in enumerate(Cmap):
        ellapse_time = time.time() - tic
        remain_time = float(N_D - i) * ellapse_time / max(i, 1)
        print("\r%.2f Percent.. (Elapsed: %s, Remaining: %s)" % (i / float(N_D)
                                                                 * 100.0, format_timedelta(ellapse_time), format_timedelta(remain_time)))
        sys.stdout.flush()

        # Get the CTF for this image
        cCTF = srcctf_stack.get_ctf(srcctfI)
        if prevctfI != srcctfI:
            genctfI = genctf_stack.add_ctf(cCTF)
            C = cCTF.dense_ctf(N, psize, bfactor).reshape((N**2,))
            prevctfI = srcctfI

        # Randomly generate the viewing direction/shift
        pt = np.random.randn(3)
        pt /= np.linalg.norm(pt)
        psi = 2 * np.pi * np.random.rand()
        EA = geometry.genEA(pt)[0]
        EA[2] = psi
        shift = np.random.randn(2) * shift_sigma

        R = geometry.rotmat3D_EA(*EA)[:, 0:2]
        slop = cryoops.compute_projection_matrix(
            [R], N, kernel, ksize, rad, 'rots')
        S = cryoops.compute_shift_phases(shift.reshape((1, 2)), N, rad)[0]

        D = slop.dot(V.reshape((-1,)))
        D *= S

        imgdata[i] = density.fspace_to_real((C * TtoF.dot(D)).reshape((N, N))) + np.require(
            np.random.randn(N, N) * sigma_noise, dtype=density.real_t)

        genctf_stack.add_img(genctfI,
                             PHI=EA[0] * 180.0 / np.pi, THETA=EA[1] * 180.0 / np.pi, PSI=EA[2] * 180.0 / np.pi,
                             SHX=shift[0], SHY=shift[1])

        pardata['R'].append(R)
        pardata['t'].append(shift)

    print("\rDone in ", time.time() - tic, " seconds.")
    return imgdata, genctf_stack, pardata, mscope_params
premult = cryoops.compute_premultiplier(N, kernel, ksize)
TtoF = sincint.gentrunctofull(N=N, rad=rad)

fM = density.real_to_fspace(M)
prefM = density.real_to_fspace(premult.reshape(
        (1, 1, -1)) * premult.reshape((1, -1, 1)) * premult.reshape((-1, 1, 1)) * M)

EAs_grid = healpix.gen_EAs_grid(nside=2, psi_step=360)
Rs = [geometry.rotmat3D_EA(*EA)[:, 0:2] for EA in EAs_grid]
slice_ops = cryoops.compute_projection_matrix(Rs, N, kern='lanczos', kernsize=ksize, rad=rad, projdirtype='rots')

slices_sampled = cryoem.getslices(fM, slice_ops).reshape((EAs_grid.shape[0], trunc_xy.shape[0]))

premult_slices_sampled = cryoem.getslices(prefM, slice_ops).reshape((EAs_grid.shape[0], trunc_xy.shape[0]))

S = cryoops.compute_shift_phases(np.asarray([100, -20]).reshape((1,2)), N, rad)[0]

trunc_slice = slices_sampled[0]
premult_trunc_slice = premult_slices_sampled[0]
premult_trunc_slice_shift = S * premult_slices_sampled[0]

fourier_slice = TtoF.dot(trunc_slice).reshape((N, N))
real_proj = density.fspace_to_real(fourier_slice)
premult_fourier_slice = TtoF.dot(premult_trunc_slice).reshape((N, N))
premult_fourier_slice_shift = TtoF.dot(premult_trunc_slice_shift).reshape((N, N))
premult_real_proj = density.fspace_to_real(premult_fourier_slice)
premult_real_proj_shift = density.fspace_to_real(premult_fourier_slice_shift)

fig, axes = plt.subplots(2, 2)
ax = axes.flatten()
ax[0].imshow(real_proj)