def calc_2d_projection( particle_volume, projection_angle_and_shift, interpolation=1, return_real=True ): """ :param particle_volume: input volume :param projection_angle_and_shift: input parameters given as a list [phi, theta, psi, s2x, s2y], projection in calculated using the three Eulerian angles and then shifted by sx,sy :param interpolation: interpolation_method,1 = triliniear :param return_real: True - return real; False - return FT of a projection. :return: 2D Projection """ projection = prgl( particle_volume, projection_angle_and_shift, interpolation, return_real ) return projection
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