def ali_nvol(v, mask): pass # IMPORTIMPORTIMPORT from sp_alignment import alivol_mask_getref, alivol_mask pass # IMPORTIMPORTIMPORT from sp_statistics import ave_var pass # IMPORTIMPORTIMPORT from sp_utilities import set_params3D, get_params3D ,compose_transform3 pass # IMPORTIMPORTIMPORT from sp_fundamentals import rot_shift3D ocrit = 1.0e20 gogo = True niter = 0 for l in range(len(v)): sp_utilities.set_params3D(v[l], (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 1.0)) while gogo: ave, var = sp_statistics.ave_var(v) p = EMAN2_cppwrap.Util.infomask(var, mask, True) crit = p[1] if (old_div(old_div((crit - ocrit), (crit + ocrit)), 2.0) > -1.0e-2 or niter > 10): gogo = False niter += 1 ocrit = crit ref = alivol_mask_getref(ave, mask) for l in range(len(v)): ophi, otht, opsi, os3x, os3y, os3z, dum, dum = sp_utilities.get_params3D( v[l]) vor = sp_fundamentals.rot_shift3D(v[l], ophi, otht, opsi, os3x, os3y, os3z) phi, tht, psi, s3x, s3y, s3z = alivol_mask(vor, ref, mask) phi, tht, psi, s3x, s3y, s3z, scale = sp_utilities.compose_transform3( phi, tht, psi, s3x, s3y, s3z, 1.0, ophi, otht, opsi, os3x, os3y, os3z, 1.0, ) sp_utilities.set_params3D(v[l], (phi, tht, psi, s3x, s3y, s3z, 0, 1.0)) # print "final align3d params: %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f" % (phi,tht,psi,s3x,s3y,s3z) for l in range(len(v)): ophi, otht, opsi, os3x, os3y, os3z, dum, dum = sp_utilities.get_params3D( v[l]) sp_global_def.sxprint(l, ophi, otht, opsi, os3x, os3y, os3z) v[l] = sp_fundamentals.rot_shift3D(v[l], ophi, otht, opsi, os3x, os3y, os3z) v[l].del_attr("xform.align3d") return v
def alivol_mask(v, vref, mask): pass # IMPORTIMPORTIMPORT from sp_utilities import set_params3D, get_params3D,compose_transform3 pass # IMPORTIMPORTIMPORT from sp_applications import ali_vol_shift, ali_vol_rotate v50S_i = v.copy() v50S_i *= mask cnt = v50S_i.phase_cog() sp_utilities.set_params3D( v50S_i, (0.0, 0.0, 0.0, -cnt[0], -cnt[1], -cnt[2], 0, 1.0)) v50S_i = sp_applications.ali_vol_shift(v50S_i, vref, 1.0) v50S_i = sp_applications.ali_vol_rotate(v50S_i, vref, 5.0) v50S_i = sp_applications.ali_vol_shift(v50S_i, vref, 0.5) v50S_i = sp_applications.ali_vol_rotate(v50S_i, vref, 1.0) phi, tht, psi, s3x, s3y, s3z, mirror, scale = sp_utilities.get_params3D( v50S_i) dun, dum, dum, cnx, cny, cnz, mirror, scale = sp_utilities.get_params3D( vref) phi, tht, psi, s3x, s3y, s3z, scale = sp_utilities.compose_transform3( phi, tht, psi, s3x, s3y, s3z, 1.0, 0.0, 0.0, 0.0, -cnx, -cny, -cnz, 1.0) return phi, tht, psi, s3x, s3y, s3z
def ali_vol_func_shift(params, data): pass # IMPORTIMPORTIMPORT from sp_utilities import compose_transform3 pass # IMPORTIMPORTIMPORT from sp_fundamentals import rot_shift3D cphi, ctheta, cpsi, cs2x, cs2y, cs2z, cscale = sp_utilities.compose_transform3( data[3][0], data[3][1], data[3][2], data[3][3], data[3][4], data[3][5], data[3][7], 0.0, 0.0, 0.0, params[0], params[1], params[2], 1.0, ) x = sp_fundamentals.rot_shift3D(data[0], cphi, ctheta, cpsi, cs2x, cs2y, cs2z, cscale) res = -x.cmp(data[4], data[1], {"mask": data[2]}) # print " %9.3f %9.3f %9.3f %12.3e" %(params[0], params[1], params[2], -res) return res
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 apsh(refimgs, imgs2align, outangles=None, refanglesdoc=None, outaligndoc=None, outerradius=-1, maxshift=0, ringstep=1, mode="F", log=None, verbose=False): """ Generates polar representations of a series of images to be used as alignment references. Arguments: refimgs : Input reference image stack (filename or EMData object) imgs2align : Image stack to be aligned (filename or EMData object) outangles : Output Euler angles doc file refanglesdoc : Input Euler angles for reference projections outaligndoc : Output 2D alignment doc file outerradius : Outer alignment radius maxshift : Maximum shift allowed ringstep : Alignment radius step size mode : Mode, full circle ("F") vs. half circle ("H") log : Logger object verbose : (boolean) Whether to write additional information to screen """ # Generate polar representation(s) of reference(s) alignrings, polarrefs = mref2polar(refimgs, outerradius=outerradius, ringstep=ringstep, log=log, verbose=verbose) # Read image stack (as a filename or already an EMDataobject) if isinstance(imgs2align, str): imagestacklist = EMData.read_images(imgs2align) else: imagestacklist = [imgs2align] # Get number of images numimg = len(imagestacklist) # Get image dimensions (assuming square, and that images and references have the same dimension) idim = imagestacklist[0]['nx'] # Calculate image center halfdim = idim / 2 + 1 # Set constants currshift = 0 scale = 1 # Initialize output angles outangleslist = [] outalignlist = [] if outerradius <= 0: outerradius = halfdim - 3 # Set search range txrng = tyrng = search_range(idim, outerradius, currshift, maxshift) print_log_msg( 'Running multireference alignment allowing a maximum shift of %s\n' % maxshift, log, verbose) # Loop through images for imgindex in range(numimg): currimg = imagestacklist[imgindex] # Perform multi-reference alignment (adapted from alignment.mref_ali2d) best2dparamslist = [ angt, sxst, syst, mirrorfloat, bestreffloat, peakt ] = Util.multiref_polar_ali_2d(currimg, polarrefs, txrng, tyrng, ringstep, mode, alignrings, halfdim, halfdim) bestref = int(bestreffloat) mirrorflag = int(mirrorfloat) # Store parameters params2dlist = [angt, sxst, syst, mirrorflag, scale] outalignlist.append(params2dlist) if refanglesdoc: refangleslist = read_text_row(refanglesdoc) besteulers = refangleslist[bestref] else: besteulers = [0] * 5 # Check for mirroring if mirrorflag == 1: tempeulers = list( compose_transform3(besteulers[0], besteulers[1], besteulers[2], besteulers[3], besteulers[4], 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, -angt, 0, 0, 0, 1)) else: combinedparams = list( compose_transform3(besteulers[0], besteulers[1], besteulers[2], besteulers[3], besteulers[4], 0, 1, 0, 0, -angt, 0, 0, 0, 1)) # compose_transform3: returns phi,theta,psi, tx,ty,tz, scale outangleslist.append(combinedparams) # Set transformations as image attribute set_params2D(currimg, params2dlist, xform="xform.align2d" ) # sometimes I get a vector error with sxheader set_params_proj(currimg, besteulers, xform="xform.projection") # use shifts if outangles or outaligndoc: msg = '' if outangles: write_text_row(outangleslist, outangles) msg += 'Wrote alignment angles to %s\n' % outangles print_log_msg(msg, log, verbose) if outaligndoc: write_text_row(outalignlist, outaligndoc) msg += 'Wrote 2D alignment parameters to %s\n' % outaligndoc print_log_msg(msg, log, verbose) return outalignlist