def compute_operator(self,interp_params,inds=None): if inds is None: theta = self.theta else: theta = self.theta[inds] N = interp_params['N'] kern = interp_params['kern'] kernsize = interp_params['kernsize'] rad = interp_params['rad'] zeropad = interp_params.get('zeropad',0) N_src = N if zeropad == 0 else N + 2*int(zeropad*(N/2)) return cops.compute_inplanerot_matrix(theta,N,kern,kernsize,rad,N_src, onlyRs = interp_params.get('onlyRs', False))
def compute_operator(self, interp_params, inds=None): if inds is None: theta = self.theta else: theta = self.theta[inds] N = interp_params['N'] kern = interp_params['kern'] kernsize = interp_params['kernsize'] rad = interp_params['rad'] zeropad = interp_params.get('zeropad', 0) N_src = N if zeropad == 0 else N + 2 * int(zeropad * (N / 2)) return cops.compute_inplanerot_matrix(theta, N, kern, kernsize, rad, N_src, onlyRs=interp_params.get( 'onlyRs', True))
v1 = compute_ctf(fcoords, rots, akv, cs, wgh, df1, df2, angast, dscale).reshape((-1,)) v2 = compute_ctf(rotfcoords, None, akv, cs, wgh, df1, df2, angast, dscale).reshape((-1,)) # This being small confirms that using the rots parameter is equivalent to # rotating the coordinates print(np.abs(v1 - v2).max()) N = 512 psz = 5.6 rad = 0.25 fcoords = geometry.gencoords(N, 2, rad) / (N * psz) ctf1_rot = compute_full_ctf( rots, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None) ctf2_full = compute_full_ctf( None, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None) P_rot = coops.compute_inplanerot_matrix(rots, N, 'lanczos', 10, rad) ctf2_rot = P_rot.dot(ctf2_full).reshape((-1,)) P_null = coops.compute_inplanerot_matrix(np.array([0]), N, 'linear', 2, rad) ctf1_rot = P_null.dot(ctf1_rot).reshape((-1,)) roterr = ctf1_rot - ctf2_rot relerr = np.abs(roterr) / np.maximum(np.abs(ctf1_rot), np.abs(ctf2_rot)) # This being small confirms that compute_inplane_rotmatrix and rots use # the same rotation convention print(relerr.max(), relerr.mean())
def ctf_vis(): import cryoops as coops fcoords = np.random.randn(10, 2) rots = np.array([np.pi / 3.0]) R = np.array([[np.cos(rots), -np.sin(rots)], [np.sin(rots), np.cos(rots)]]).reshape((2, 2)) rotfcoords = np.dot(fcoords, R.T) akv = 200 wgh = 0.07 cs = 2.0 df1, df2, angast = 44722, 49349, 45.0 * (np.pi / 180.0) dscale = 1.0 v1 = ctf.compute_ctf(fcoords, rots, akv, cs, wgh, df1, df2, angast, dscale).reshape((-1,)) v2 = ctf.compute_ctf(rotfcoords, None, akv, cs, wgh, df1, df2, angast, dscale).reshape((-1,)) # This being small confirms that using the rots parameter is equivalent to # rotating the coordinates print(np.abs(v1 - v2).max()) # N = 512 N = 128 psz = 5.6 rad = 0.25 fcoords = geometry.gencoords(N, 2, rad) / (N * psz) ctf1_rot = ctf.compute_full_ctf( rots, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None) ctf2_full = ctf.compute_full_ctf( None, N, psz, akv, cs, wgh, df1, df2, angast, dscale, None) print(ctf1_rot.shape) fig, ax = plt.subplots(1, 2) ax[0].imshow(ctf1_rot.reshape(N, N)) ax[1].imshow(ctf2_full.reshape(N, N)) ax[0].set_title('inplane-rotation: 0') ax[1].set_title('inplane-rotation: 60 degree') fig, ax = plt.subplots() x = np.arange(int(-N/2.0), int(N/2.0)) ax.plot(x, ctf1_rot.reshape(N, N)[int(N/2), :], label='0 degree') ax.plot(x, ctf2_full.reshape(N, N)[int(N/2), :], label='60 degree') ax.set_xlabel('pixel') ax.set_ylabel('CTF value') ax.legend() plt.show() P_rot = coops.compute_inplanerot_matrix(rots, N, 'lanczos', 10, rad) ctf2_rot = P_rot.dot(ctf2_full).reshape((-1,)) P_null = coops.compute_inplanerot_matrix(np.array([0]), N, 'linear', 2, rad) ctf1_rot = P_null.dot(ctf1_rot).reshape((-1,)) print(fcoords.shape) print(P_rot.shape) print(ctf2_rot.shape) roterr = ctf1_rot - ctf2_rot relerr = np.abs(roterr) / np.maximum(np.abs(ctf1_rot), np.abs(ctf2_rot)) # This being small confirms that compute_inplane_rotmatrix and rots use # the same rotation convention print(relerr.max(), relerr.mean())
def plot_figures(): oversampling_factor = 6 psize = 3 * oversampling_factor freq = 0.05 rad = 0.5 * 2.0 * psize beamstop_freq = 0.005 beamstop_rad = beamstop_freq * 2.0 * psize # M = mrc.readMRC('particle/EMD-6044-cropped.mrc') # M[M < 0] = 0 # M_totalmass = 2000000 # if M_totalmass is not None: # M *= M_totalmass / M.sum() # fM = density.real_to_fspace_with_oversampling(M, oversampling_factor=oversampling_factor) # fM = fM.real ** 2 + fM.imag ** 2 # mrc.writeMRC("particle/EMD-6044-cropped_fM_totalmass_%d_oversampling_%d.mrc" % (M_totalmass, oversampling_factor), fM, psz=psize) # exit() fM = mrc.readMRC( 'particle/EMD-6044-cropped_fM_totalmass_2000000_oversampling_6.mrc') N = fM.shape[0] TtoF = sincint.gentrunctofull(N=N, rad=rad, beamstop_rad=beamstop_rad) theta = np.arange(0, 2 * np.pi, 2 * np.pi / 60) degree_R, resolution_R = SK97Quadrature.compute_degree(N, 0.3, 1.0) dirs, weights = SK97Quadrature.get_quad_points(degree_R, None) Rs = np.vstack([ geometry.rotmat3D_dir(vec)[:, 0:2].reshape((1, 3, 2)) for vec in dirs ]) N_R = dirs.shape[0] N_I = theta.shape[0] N_T = TtoF.shape[1] # generate slicing operators dir_slice_interp = { 'projdirs': dirs, 'N': N, 'kern': 'lanczos', 'kernsize': 6, 'projdirtype': 'dirs', 'onlyRs': True, 'rad': rad, 'sym': None } # 'zeropad': 0, 'dopremult': True R_slice_interp = { 'projdirs': Rs, 'N': N, 'kern': 'lanczos', 'kernsize': 6, 'projdirtype': 'rots', 'onlyRs': True, 'rad': rad, 'sym': None } # 'zeropad': 0, 'dopremult': True inplane_interp = { 'thetas': theta, 'N': N, 'kern': 'lanczos', 'kernsize': 6, 'onlyRs': True, 'rad': rad } # 'zeropad': 1, 'dopremult': True inplane_interp['N_src'] = N # zp_N slice_ops = cryoops.compute_projection_matrix(**dir_slice_interp) inplane_ops = cryoops.compute_inplanerot_matrix(**inplane_interp) # generate slices and inplane-rotated slices slices_sampled = cryoem.getslices_interp( fM, slice_ops, rad, beamstop_rad=beamstop_rad).reshape((N_R, N_T)) curr_img = TtoF.dot(slices_sampled[0]).reshape(N, N) rotd_sampled = cryoem.getslices_interp(curr_img, inplane_ops, rad, beamstop_rad=beamstop_rad).reshape( (N_I, N_T)) ## plot figures fig, axes = plt.subplots(3, 4, figsize=(9.6, 7.2)) for i, ax in enumerate(axes.flatten()): img = TtoF.dot(slices_sampled[i]).reshape(N, N) ax.imshow(img, origin='lower') fig.suptitle('slices_sampled') fig, axes = plt.subplots(3, 4, figsize=(9.6, 7.2)) for i, ax in enumerate(axes.flatten()): img = TtoF.dot(slices_sampled[i]).reshape(N, N) ax.imshow(img, origin='lower') fig.suptitle('rotd_sampled') plt.show()
'onlyRs': True, 'rad': rad, 'sym': None } # 'zeropad': 0, 'dopremult': True inplane_interp = { 'thetas': theta, 'N': N, 'kern': 'lanczos', 'kernsize': 6, 'onlyRs': True, 'rad': rad } # 'zeropad': 1, 'dopremult': True inplane_interp['N_src'] = N # zp_N slice_ops = cryoops.compute_projection_matrix(**dir_slice_interp) inplane_ops = cryoops.compute_inplanerot_matrix(**inplane_interp) # generate slices and inplane-rotated slices slices_sampled = cryoem.getslices_interp( fM, slice_ops, rad, beamstop_rad=beamstop_rad).reshape((N_R, N_T)) curr_img = TtoF.dot(slices_sampled[0]).reshape(N, N) rotd_sampled = cryoem.getslices_interp(curr_img, inplane_ops, rad, beamstop_rad=beamstop_rad).reshape( (N_I, N_T)) ## plot figures fig, axes = plt.subplots(3, 4, figsize=(9.6, 7.2)) for i, ax in enumerate(axes.flatten()): img = TtoF.dot(slices_sampled[i]).reshape(N, N)