def prg(volume, params): """Given a volume, a set of projection angles, and Kaiser-Bessel window parameters, use gridding to generate projection """ Mx = volume.get_xsize() My = volume.get_ysize() Mz = volume.get_zsize() if (Mx == Mz & My == Mz): volft, kb = prep_vol(volume) return prgs(volft, kb, params) else: volft, kbx, kby, kbz = prep_vol(volume) return prgs(volft, kbz, params, kbx, kby)
def prj(vol, params, stack=None): """ Name prj - calculate a set of 2-D projection of a 3-D volume using gridding Input vol: input volume, all dimensions have to be the same (nx=ny=nz) params: a list of input parameters given as a list [i][phi, theta, psi, sx, sy], projection in calculated using the three Eulerian angles and then shifted by sx,sy Output proj either: an in-core stack of generated 2-D projections stack """ from sp_utilities import set_params_proj from sp_projection import prep_vol volft, kb = prep_vol(vol) for i in range(len(params)): proj = prgs(volft, kb, params[i]) set_params_proj(proj, [ params[i][0], params[i][1], params[i][2], -params[i][3], -params[i][4] ]) proj.set_attr_dict({'ctf_applied': 0}) if (stack): proj.write_image(stack, i) else: if (i == 0): out = [] out.append(proj) if (stack): return else: return out
def read_volume(path_vol_1, path_vol_2, path_mask=None, resolution=None, pixel_size=None): """ Reads the 3D density map :param path_vol_1: Path to the density map :param path_mask: If provided, the volume is masked. :return: Volume for density map """ print("Read volumes, masking them and prepare them for reprojections") mask_vol = None vol2 = None if path_mask is not None: mask_vol = sp_utilities.get_im(path_mask) vol1 = prepare_volume(path_vol_1, mask_vol, resolution, pixel_size) if path_vol_2: vol2 = prepare_volume(path_vol_2, mask_vol, resolution, pixel_size) if mask_vol: mask_vol = sp_projection.prep_vol(mask_vol, interpolation_method=1) return vol1, vol2, mask_vol
def prepare_volume(volume_path, mask=None, resolution=None, pixel_size=None): """ Prepares the volume for reprojections :param volume_path: Path to volume file :param mask: Particle mask :param resolution: Resolution of the current reconstruction. :return: """ vol = sp_utilities.get_im(volume_path) if resolution: vol = vol.process( "filter.lowpass.gauss", { "cutoff_freq": old_div(1.0, resolution), "apix": pixel_size }, ) if mask: vol = vol * mask vol = sp_projection.prep_vol(vol, interpolation_method=1) return vol
def generate_helimic(refvol, outdir, pixel, CTF=False, Cs=2.0, voltage=200.0, ampcont=10.0, nonoise=False, rand_seed=14567): from sp_utilities import model_blank, model_gauss, model_gauss_noise, pad, get_im from random import random from sp_projection import prgs, prep_vol from sp_filter import filt_gaussl, filt_ctf from EMAN2 import EMAN2Ctf if os.path.exists(outdir): ERROR( "Output directory exists, please change the name and restart the program" ) return os.mkdir(outdir) seed(rand_seed) Util.set_randnum_seed(rand_seed) angles = [] for i in range(3): angles.append([0.0 + 60.0 * i, 90.0 - i * 5, 0.0, 0.0, 0.0]) nangle = len(angles) volfts = get_im(refvol) nx = volfts.get_xsize() ny = volfts.get_ysize() nz = volfts.get_zsize() volfts, kbx, kby, kbz = prep_vol(volfts) iprj = 0 width = 500 xstart = 0 ystart = 0 for idef in range(3, 6): mic = model_blank(2048, 2048) #defocus = idef*0.2 defocus = idef * 0.6 ##@ming if CTF: #ctf = EMAN2Ctf() #ctf.from_dict( {"defocus":defocus, "cs":Cs, "voltage":voltage, "apix":pixel, "ampcont":ampcont, "bfactor":0.0} ) from sp_utilities import generate_ctf ctf = generate_ctf( [defocus, 2, 200, 1.84, 0.0, ampcont, defocus * 0.2, 80] ) ##@ming the range of astigmatism amplitude is between 10 percent and 22 percent. 20 percent is a good choice. i = idef - 4 for k in range(1): psi = 90 + 10 * i proj = prgs( volfts, kbz, [angles[idef - 3][0], angles[idef - 3][1], psi, 0.0, 0.0], kbx, kby) proj = Util.window(proj, 320, nz) mic += pad(proj, 2048, 2048, 1, 0.0, 750 * i, 20 * i, 0) if not nonoise: mic += model_gauss_noise(30.0, 2048, 2048) if CTF: #apply CTF mic = filt_ctf(mic, ctf) if not nonoise: mic += filt_gaussl(model_gauss_noise(17.5, 2048, 2048), 0.3) mic.write_image("%s/mic%1d.hdf" % (outdir, idef - 3), 0)
def mode_meridien(reconfile, classavgstack, classdocs, partangles, selectdoc, maxshift, outerrad, outanglesdoc, outaligndoc, interpolation_method=1, outliers=None, goodclassparttemplate=None, alignopt='apsh', ringstep=1, log=None, verbose=False): # Resample reference recondata = EMAN2.EMData(reconfile) idim = recondata['nx'] reconprep = prep_vol(recondata, npad=2, interpolation_method=interpolation_method) # Initialize output angles outangleslist = [] outalignlist = [] # Read class lists classdoclist = glob.glob(classdocs) partangleslist = read_text_row(partangles) # Loop through class lists for classdoc in classdoclist: # [classdoclist[32]]: # # Strip out three-digit filenumber classexample = os.path.splitext(classdoc) classnum = int(classexample[0][-3:]) # Initial average [avg_phi_init, avg_theta_init] = average_angles(partangleslist, classdoc, selectdoc=selectdoc) # Look for outliers if outliers: [avg_phi_final, avg_theta_final] = average_angles( partangleslist, classdoc, selectdoc=selectdoc, init_angles=[avg_phi_init, avg_theta_init], threshold=outliers, goodpartdoc=goodclassparttemplate.format(classnum), log=log, verbose=verbose) else: [avg_phi_final, avg_theta_final] = [avg_phi_init, avg_theta_init] # Compute re-projection refprjreal = prgl(reconprep, [avg_phi_final, avg_theta_final, 0, 0, 0], interpolation_method=1, return_real=True) # Align to class average classavg = get_im(classavgstack, classnum) # Alignment using self-correlation function if alignopt == 'scf': ang_align2d, sxs, sys, mirrorflag, peak = align2d_scf(classavg, refprjreal, maxshift, maxshift, ou=outerrad) # Weird results elif alignopt == 'align2d': # Set search range currshift = 0 txrng = tyrng = search_range(idim, outerrad, currshift, maxshift) # Perform alignment ang_align2d, sxs, sys, mirrorflag, peak = align2d( classavg, refprjreal, txrng, tyrng, last_ring=outerrad) # Direct3 (angles seemed to be quantized) elif alignopt == 'direct3': [[ang_align2d, sxs, sys, mirrorflag, peak]] = align2d_direct3([classavg], refprjreal, maxshift, maxshift, ou=outerrad) # APSH-like alignment (default) else: [[ang_align2d, sxs, sys, mirrorflag, scale]] = apsh(refprjreal, classavg, outerradius=outerrad, maxshift=maxshift, ringstep=ringstep) outalignlist.append([ang_align2d, sxs, sys, mirrorflag, 1]) msg = "Particle list %s: ang_align2d=%s sx=%s sy=%s mirror=%s\n" % ( classdoc, ang_align2d, sxs, sys, mirrorflag) print_log_msg(msg, log, verbose) # Check for mirroring if mirrorflag == 1: tempeulers = list( compose_transform3(avg_phi_final, avg_theta_final, 0, 0, 0, 0, 1, 0, 180, 0, 0, 0, 0, 1)) combinedparams = list( compose_transform3(tempeulers[0], tempeulers[1], tempeulers[2], tempeulers[3], tempeulers[4], 0, 1, 0, 0, -ang_align2d, 0, 0, 0, 1)) else: combinedparams = list( compose_transform3(avg_phi_final, avg_theta_final, 0, 0, 0, 0, 1, 0, 0, -ang_align2d, 0, 0, 0, 1)) # compose_transform3: returns phi,theta,psi, tx,ty,tz, scale outangleslist.append(combinedparams) # End class-loop write_text_row(outangleslist, outanglesdoc) write_text_row(outalignlist, outaligndoc) print_log_msg( 'Wrote alignment parameters to %s and %s\n' % (outanglesdoc, outaligndoc), log, verbose) del recondata # Clean up
def compare_projs(reconfile, classavgstack, inputanglesdoc, outdir, interpolation_method=1, log=None, verbose=False): """ Make comparison stack between class averages (even-numbered (starts from 0)) and re-projections (odd-numbered). Arguments: reconfile : Input volume from which to generate re-projections classavgstack ; Input image stack inputanglesdoc : Input Euler angles doc outdir ; Output directory interpolation_method : Interpolation method: nearest neighbor (nn, 0), trilinear (1, default), gridding (-1) log : Logger object verbose : (boolean) Whether to write additional information to screen Returns: compstack : Stack of comparisons between input image stack (even-numbered (starts from 0)) and input volume (odd-numbered) """ recondata = EMAN2.EMData(reconfile) nx = recondata.get_xsize() # Resample reference reconprep = prep_vol(recondata, npad=2, interpolation_method=interpolation_method) ccclist = [] # Here you need actual radius to compute proper ccc's, but if you do, you have to deal with translations, PAP mask = model_circle(nx // 2 - 2, nx, nx) mask.write_image(os.path.join(outdir, 'maskalign.hdf')) compstack = os.path.join(outdir, 'comp-proj-reproj.hdf') # Number of images may have changed nimg1 = EMAN2.EMUtil.get_image_count(classavgstack) angleslist = read_text_row(inputanglesdoc) for imgnum in range(nimg1): # Get class average classimg = get_im(classavgstack, imgnum) # Compute re-projection prjimg = prgl(reconprep, angleslist[imgnum], interpolation_method=1, return_real=False) # Calculate 1D power spectra rops_dst = rops_table(classimg * mask) rops_src = rops_table(prjimg) # Set power spectrum of reprojection to the data. # Since data has an envelope, it would make more sense to set data to reconstruction, # but to do it one would have to know the actual resolution of the data. # you can check sxprocess.py --adjpw to see how this is done properly PAP table = [0.0] * len(rops_dst) # initialize table for j in range(len(rops_dst)): table[j] = sqrt(rops_dst[j] / rops_src[j]) prjimg = fft(filt_table( prjimg, table)) # match FFT amplitudes of re-projection and class average cccoeff = ccc(prjimg, classimg, mask) #print imgnum, cccoeff classimg.set_attr_dict({'cross-corr': cccoeff}) prjimg.set_attr_dict({'cross-corr': cccoeff}) montagestack = [] montagestack.append(prjimg) montagestack.append(classimg) comparison_pair = montage2(montagestack, ncol=2, marginwidth=1) comparison_pair.write_image(compstack, imgnum) ccclist.append(cccoeff) del angleslist meanccc = sum(ccclist) / nimg1 print_log_msg("Average CCC is %s\n" % meanccc, log, verbose) nimg2 = EMAN2.EMUtil.get_image_count(compstack) for imgnum in range(nimg2): # xrange will be deprecated in Python3 prjimg = get_im(compstack, imgnum) meanccc1 = prjimg.get_attr_default('mean-cross-corr', -1.0) prjimg.set_attr_dict({'mean-cross-corr': meanccc}) write_header(compstack, prjimg, imgnum) return compstack