def ref_ali2d( ref_data ): from utilities import print_msg from filter import fit_tanh, filt_tanl from utilities import center_2D # Prepare the reference in 2D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FRC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 print_msg("ref_ali2d #%6d\n"%(ref_ali2d_counter)) fl, aa = fit_tanh(ref_data[3]) aa = min(aa, 0.2) fl = max(min(0.4,fl),0.12) msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) tavg = filt_tanl(ref_data[2], fl, aa) cs = [0.0]*2 if(ref_data[1] > 0): tavg, cs[0], cs[1] = center_2D(tavg, ref_data[1], self_defined_reference = ref_data[0]) msg = "Center x = %10.3f Center y = %10.3f\n"%(cs[0], cs[1]) print_msg(msg) return tavg, cs
def ref_7grp(ref_data): from utilities import print_msg from filter import fit_tanh, filt_tanl, filt_gaussinv from fundamentals import fshift from morphology import threshold from math import sqrt # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: #cs = [0.0]*3 stat = Util.infomask(ref_data[2], None, False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) volf = Util.muln_img(threshold(volf), ref_data[0]) fl, aa = fit_tanh(ref_data[3]) msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) if (ref_data[1] == 1): cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n" % ( cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) B_factor = 10.0 volf = filt_gaussinv(volf, 10.0) return volf, cs
def ref_ali2d(ref_data): from utilities import print_msg from filter import fit_tanh, filt_tanl from utilities import center_2D # Prepare the reference in 2D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FRC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 print_msg("ref_ali2d #%6d\n" % (ref_ali2d_counter)) fl, aa = fit_tanh(ref_data[3]) aa = min(aa, 0.2) fl = max(min(0.4, fl), 0.12) msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) tavg = filt_tanl(ref_data[2], fl, aa) cs = [0.0] * 2 if (ref_data[1] > 0): tavg, cs[0], cs[1] = center_2D(tavg, ref_data[1], self_defined_reference=ref_data[0]) msg = "Center x = %10.3f Center y = %10.3f\n" % ( cs[0], cs[1]) print_msg(msg) return tavg, cs
def ref_7grp( ref_data ): from utilities import print_msg from filter import fit_tanh, filt_tanl, filt_gaussinv from fundamentals import fshift from morphology import threshold from math import sqrt # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: #cs = [0.0]*3 stat = Util.infomask(ref_data[2], None, False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0/stat[1]) volf = Util.muln_img(threshold(volf), ref_data[0]) fl, aa = fit_tanh(ref_data[3]) msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) if(ref_data[1] == 1): cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n"%(cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) B_factor = 10.0 volf = filt_gaussinv( volf, 10.0 ) return volf,cs
def filt_vols( vols, fscs, mask3D ): from math import sqrt from filter import fit_tanh, filt_tanl, filt_table from fundamentals import rops_table from morphology import threshold flmin = 1.0 flmax = -1.0 nvol = len(vols) for i in xrange(nvol): fl, aa = fit_tanh( fscs[i] ) if (fl < flmin): flmin = fl aamin = aa if (fl > flmax): flmax = fl idmax = i print " Filter tanl, parameters: ",flmin-0.05, " ", aamin volmax = vols[idmax] volmax = filt_tanl( volmax, flmin-0.05, aamin ) pmax = rops_table( volmax ) for i in xrange(nvol): ptab = rops_table( vols[i] ) for j in xrange( len(ptab) ): ptab[j] = sqrt( pmax[j]/ptab[j] ) vols[i] = filt_table( vols[i], ptab ) #stat = Util.infomask( vols[i], mask3D, False ) #volf -= stat[0] Util.mul_img( vols[i], mask3D ) #volf = threshold( volf ) return vols
def ref_ali3d(ref_data): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 fl = ref_data[2].cmp("dot", ref_data[2], { "negative": 0, "mask": ref_data[0] }) print_msg("ref_ali3d Step = %5d GOAL = %10.3e\n" % (ref_ali2d_counter, fl)) cs = [0.0] * 3 #filt = filt_from_fsc(fscc, 0.05) #vol = filt_table(vol, filt) # here figure the filtration parameters and filter vol for the next iteration #fl, fh = filt_params(res) #vol = filt_btwl(vol, fl, fh) # store the filtered reference volume #lk = 0 #while(res[1][lk] >0.9 and res[0][lk]<0.25): # lk+=1 #fl = res[0][lk] #fh = min(fl+0.1,0.49) #vol = filt_btwl(vol, fl, fh) #fl, fh = filt_params(fscc) #print "fl, fh, iter",fl,fh,Iter #vol = filt_btwl(vol, fl, fh) stat = Util.infomask(ref_data[2], ref_data[0], False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) #volf = threshold(volf) Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) #fl = 0.4 #aa = 0.1 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) if ref_data[1] == 1: cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n" % ( cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) return volf, cs
def ref_ali3d( ref_data ): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: global ref_ali2d_counter ref_ali2d_counter += 1 fl = ref_data[2].cmp("dot",ref_data[2], {"negative":0, "mask":ref_data[0]} ) print_msg("ref_ali3d Step = %5d GOAL = %10.3e\n"%(ref_ali2d_counter,fl)) cs = [0.0]*3 #filt = filt_from_fsc(fscc, 0.05) #vol = filt_table(vol, filt) # here figure the filtration parameters and filter vol for the next iteration #fl, fh = filt_params(res) #vol = filt_btwl(vol, fl, fh) # store the filtered reference volume #lk = 0 #while(res[1][lk] >0.9 and res[0][lk]<0.25): # lk+=1 #fl = res[0][lk] #fh = min(fl+0.1,0.49) #vol = filt_btwl(vol, fl, fh) #fl, fh = filt_params(fscc) #print "fl, fh, iter",fl,fh,Iter #vol = filt_btwl(vol, fl, fh) stat = Util.infomask(ref_data[2], ref_data[0], False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0/stat[1]) #volf = threshold(volf) Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) #fl = 0.4 #aa = 0.1 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) if ref_data[1] == 1: cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n"%(cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) return volf, cs
def ref_aliB_cone(ref_data): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold from math import sqrt # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - reference PW # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: print_msg("ref_aliB_cone\n") #cs = [0.0]*3 stat = Util.infomask(ref_data[2], None, True) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) volf = threshold(volf) Util.mul_img(volf, ref_data[0]) from fundamentals import rops_table pwem = rops_table(volf) ftb = [] for idum in xrange(len(pwem)): ftb.append(sqrt(ref_data[1][idum] / pwem[idum])) from filter import filt_table volf = filt_table(volf, ftb) fl, aa = fit_tanh(ref_data[3]) #fl = 0.41 #aa = 0.15 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) stat = Util.infomask(volf, None, True) volf -= stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) """ if(ref_data[1] == 1): cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n"%(cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) """ return volf
def ref_aliB_cone( ref_data ): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold from math import sqrt # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - reference PW # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: print_msg("ref_aliB_cone\n") #cs = [0.0]*3 stat = Util.infomask(ref_data[2], None, True) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0/stat[1]) volf = threshold(volf) Util.mul_img(volf, ref_data[0]) from fundamentals import rops_table pwem = rops_table(volf) ftb = [] for idum in xrange(len(pwem)): ftb.append(sqrt(ref_data[1][idum]/pwem[idum])) from filter import filt_table volf = filt_table(volf, ftb) fl, aa = fit_tanh(ref_data[3]) #fl = 0.41 #aa = 0.15 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) stat = Util.infomask(volf, None, True) volf -= stat[0] Util.mul_scalar(volf, 1.0/stat[1]) """ if(ref_data[1] == 1): cs = volf.phase_cog() msg = "Center x = %10.3f Center y = %10.3f Center z = %10.3f\n"%(cs[0], cs[1], cs[2]) print_msg(msg) volf = fshift(volf, -cs[0], -cs[1], -cs[2]) """ return volf
def minfilt( fscc ): from filter import fit_tanh numref = len(fscc) flmin = 1.0 flmax = -1.0 for iref in xrange(numref): fl, aa = fit_tanh( fscc[iref] ) if (fl < flmin): flmin = fl aamin = aa idmin = iref if (fl > flmax): flmax = fl aamax = aa return flmin,aamin,idmin
def minfilt(fscc): from filter import fit_tanh numref = len(fscc) flmin = 1.0 flmax = -1.0 for iref in xrange(numref): fl, aa = fit_tanh(fscc[iref]) if (fl < flmin): flmin = fl aamin = aa idmin = iref if (fl > flmax): flmax = fl aamax = aa return flmin, aamin, idmin
def ref_ali3dm_ali_50S(refdata): from filter import fit_tanh, filt_tanl from utilities import get_im from fundamentals import rot_shift3D import os numref = refdata[0] outdir = refdata[1] fscc = refdata[2] total_iter = refdata[3] varf = refdata[4] #mask_50S = get_im( "mask-50S.spi" ) flmin = 1.0 flmax = -1.0 for iref in xrange(numref): fl, aa = fit_tanh(fscc[iref]) if (fl < flmin): flmin = fl aamin = aa if (fl > flmax): flmax = fl aamax = aa print 'iref,fl,aa: ', iref, fl, aa # filter to minimum resolution print 'flmin,aamin:', flmin, aamin for iref in xrange(numref): v = get_im(os.path.join(outdir, "vol%04d.hdf" % total_iter), iref) v = filt_tanl(v, flmin, aamin) if ali50s: from utilities import get_params3D, set_params3D, combine_params3 from applications import ali_vol_shift, ali_vol_rotate if iref == 0: v50S_ref = alivol_mask_getref(v, mask_50S) else: v = alivol_mask(v, v50S_ref, mask_50S) if not (varf is None): print 'filtering by fourier variance' v.filter_by_image(varf) v.write_image(os.path.join(outdir, "volf%04d.hdf" % total_iter), iref)
def ref_ali3dm_ali_50S( refdata ): from filter import fit_tanh, filt_tanl from utilities import get_im from fundamentals import rot_shift3D import os numref = refdata[0] outdir = refdata[1] fscc = refdata[2] total_iter = refdata[3] varf = refdata[4] #mask_50S = get_im( "mask-50S.spi" ) flmin = 1.0 flmax = -1.0 for iref in xrange(numref): fl, aa = fit_tanh( fscc[iref] ) if (fl < flmin): flmin = fl aamin = aa if (fl > flmax): flmax = fl aamax = aa print 'iref,fl,aa: ', iref, fl, aa # filter to minimum resolution print 'flmin,aamin:', flmin, aamin for iref in xrange(numref): v = get_im(os.path.join(outdir, "vol%04d.hdf"%total_iter), iref) v = filt_tanl(v, flmin, aamin) if ali50s: from utilities import get_params3D, set_params3D, combine_params3 from applications import ali_vol_shift, ali_vol_rotate if iref==0: v50S_ref = alivol_mask_getref( v, mask_50S ) else: v = alivol_mask( v, v50S_ref, mask_50S ) if not(varf is None): print 'filtering by fourier variance' v.filter_by_image( varf ) v.write_image(os.path.join(outdir, "volf%04d.hdf"%total_iter), iref)
def spruce_up(ref_data): from utilities import print_msg from filter import filt_tanl, fit_tanh from morphology import threshold # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: print_msg("Changed4 spruce_up\n") cs = [0.0] * 3 stat = Util.infomask(ref_data[2], None, True) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) volf = threshold(volf) # Apply B-factor from filter import filt_gaussinv from math import sqrt B = 1.0 / sqrt(2. * 14.0) volf = filt_gaussinv(volf, B, False) nx = volf.get_xsize() from utilities import model_circle stat = Util.infomask( volf, model_circle(nx // 2 - 2, nx, nx, nx) - model_circle(nx // 2 - 6, nx, nx, nx), True) volf -= stat[0] Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) #fl = 0.35 #aa = 0.1 aa /= 2 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n" % ( fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) return volf, cs
def ref_ali3d( ref_data ): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold fl = ref_data[2].cmp("dot",ref_data[2], {"negative":0, "mask":ref_data[0]} ) cs = [0.0]*3 stat = Util.infomask(ref_data[2], ref_data[0], False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0/stat[1]) Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) volf = filt_tanl(volf, fl, aa) volf.process_inplace("normalize") if ref_data[1] == 1: cs = volf.phase_cog() volf = fshift(volf, -cs[0], -cs[1], -cs[2]) return volf, cs, fl
def spruce_up( ref_data ): from utilities import print_msg from filter import filt_tanl, fit_tanh from morphology import threshold # Prepare the reference in 3D alignment, i.e., low-pass filter and center. # Input: list ref_data # 0 - mask # 1 - center flag # 2 - raw average # 3 - fsc result # Output: filtered, centered, and masked reference image # apply filtration (FSC) to reference image: print_msg("Changed4 spruce_up\n") cs = [0.0]*3 stat = Util.infomask(ref_data[2], None, True) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0/stat[1]) volf = threshold(volf) # Apply B-factor from filter import filt_gaussinv from math import sqrt B = 1.0/sqrt(2.*14.0) volf = filt_gaussinv(volf, B, False) nx = volf.get_xsize() from utilities import model_circle stat = Util.infomask(volf, model_circle(nx//2-2,nx,nx,nx)-model_circle(nx//2-6,nx,nx,nx), True) volf -= stat[0] Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) #fl = 0.35 #aa = 0.1 aa /= 2 msg = "Tangent filter: cut-off frequency = %10.3f fall-off = %10.3f\n"%(fl, aa) print_msg(msg) volf = filt_tanl(volf, fl, aa) return volf, cs
def ref_ali3d(ref_data): from utilities import print_msg from filter import fit_tanh, filt_tanl from fundamentals import fshift from morphology import threshold fl = ref_data[2].cmp("dot", ref_data[2], { "negative": 0, "mask": ref_data[0] }) cs = [0.0] * 3 stat = Util.infomask(ref_data[2], ref_data[0], False) volf = ref_data[2] - stat[0] Util.mul_scalar(volf, 1.0 / stat[1]) Util.mul_img(volf, ref_data[0]) fl, aa = fit_tanh(ref_data[3]) volf = filt_tanl(volf, fl, aa) volf.process_inplace("normalize") if ref_data[1] == 1: cs = volf.phase_cog() volf = fshift(volf, -cs[0], -cs[1], -cs[2]) return volf, cs, fl
def compare(compare_ref_free, outfile_repro, ref_free_output, yrng, xrng, rstep, nx, apix, ref_free_cutoff, nproc, myid, main_node): from alignment import Numrinit, ringwe, Applyws from random import seed, randint from utilities import get_params2D, set_params2D, model_circle, inverse_transform2, combine_params2 from fundamentals import rot_shift2D from mpi import MPI_COMM_WORLD, mpi_barrier, mpi_bcast, MPI_INT from statistics import fsc_mask from filter import fit_tanh from numpy import array fout = "%s.hdf" % ref_free_output frc_out = "%s_frc" % ref_free_output res_out = "%s_res" % ref_free_output nima = EMUtil.get_image_count(compare_ref_free) image_start, image_end = MPI_start_end(nima, nproc, myid) ima = EMData() ima.read_image(compare_ref_free, image_start) last_ring = nx / 2 - 2 first_ring = 1 mask = model_circle(last_ring, nx, nx) refi = [] numref = EMUtil.get_image_count(outfile_repro) cnx = nx / 2 + 1 cny = cnx mode = "F" numr = Numrinit(first_ring, last_ring, rstep, mode) wr = ringwe(numr, mode) ima.to_zero() for j in xrange(numref): temp = EMData() temp.read_image(outfile_repro, j) # even, odd, numer of even, number of images. After frc, totav refi.append(temp) # for each node read its share of data data = EMData.read_images(compare_ref_free, range(image_start, image_end)) for im in xrange(image_start, image_end): data[im - image_start].set_attr('ID', im) set_params2D(data[im - image_start], [0, 0, 0, 0, 1]) ringref = [] for j in xrange(numref): refi[j].process_inplace("normalize.mask", { "mask": mask, "no_sigma": 1 }) # normalize reference images to N(0,1) cimage = Util.Polar2Dm(refi[j], cnx, cny, numr, mode) Util.Frngs(cimage, numr) Applyws(cimage, numr, wr) ringref.append(cimage) if myid == main_node: seed(1000) data_shift = [] frc = [] res = [] for im in xrange(image_start, image_end): alpha, sx, sy, mirror, scale = get_params2D(data[im - image_start]) alphai, sxi, syi, scalei = inverse_transform2(alpha, sx, sy, 1.0) # normalize data[im - image_start].process_inplace("normalize.mask", { "mask": mask, "no_sigma": 1 }) # subtract average under the mask # align current image to the reference [angt, sxst, syst, mirrort, xiref, peakt] = Util.multiref_polar_ali_2d(data[im - image_start], ringref, xrng, yrng, 1, mode, numr, cnx + sxi, cny + syi) iref = int(xiref) [alphan, sxn, syn, mn] = combine_params2(0.0, -sxi, -syi, 0, angt, sxst, syst, (int)(mirrort)) set_params2D(data[im - image_start], [alphan, sxn, syn, int(mn), scale]) temp = rot_shift2D(data[im - image_start], alphan, sxn, syn, mn) temp.set_attr('assign', iref) tfrc = fsc_mask(temp, refi[iref], mask=mask) temp.set_attr('frc', tfrc[1]) res = fit_tanh(tfrc) temp.set_attr('res', res) data_shift.append(temp) for node in xrange(nproc): if myid == node: for image in data_shift: image.write_image(fout, -1) refindex = image.get_attr('assign') refi[refindex].write_image(fout, -1) mpi_barrier(MPI_COMM_WORLD) rejects = [] if myid == main_node: a = EMData() index = 0 frc = [] res = [] temp = [] classes = [] for im in xrange(nima): a.read_image(fout, index) frc.append(a.get_attr("frc")) if ref_free_cutoff != -1: classes.append(a.get_attr("class_ptcl_idxs")) tmp = a.get_attr("res") temp.append(tmp[0]) res.append("%12f" % (apix / tmp[0])) res.append("\n") index = index + 2 res_num = array(temp) mean_score = res_num.mean(axis=0) std_score = res_num.std(axis=0) std = std_score / 2 if ref_free_cutoff != -1: cutoff = mean_score - std * ref_free_cutoff reject = res_num < cutoff index = 0 for i in reject: if i: rejects.extend(classes[index]) index = index + 1 rejects.sort() length = mpi_bcast(len(rejects), 1, MPI_INT, main_node, MPI_COMM_WORLD) rejects = mpi_bcast(rejects, length, MPI_INT, main_node, MPI_COMM_WORLD) del a fout_frc = open(frc_out, 'w') fout_res = open(res_out, 'w') fout_res.write("".join(res)) temp = zip(*frc) datstrings = [] for i in temp: for j in i: datstrings.append(" %12f" % (j)) datstrings.append("\n") fout_frc.write("".join(datstrings)) fout_frc.close() del refi del ringref return rejects
def compare(compare_ref_free, outfile_repro,ref_free_output,yrng, xrng, rstep,nx,apix,ref_free_cutoff, nproc, myid, main_node): from alignment import Numrinit, ringwe, Applyws from random import seed, randint from utilities import get_params2D, set_params2D, model_circle, inverse_transform2, combine_params2 from fundamentals import rot_shift2D from mpi import MPI_COMM_WORLD, mpi_barrier, mpi_bcast, MPI_INT from statistics import fsc_mask from filter import fit_tanh from numpy import array fout = "%s.hdf" % ref_free_output frc_out = "%s_frc" % ref_free_output res_out = "%s_res" % ref_free_output nima = EMUtil.get_image_count(compare_ref_free) image_start, image_end = MPI_start_end(nima, nproc, myid) ima = EMData() ima.read_image(compare_ref_free, image_start) last_ring = nx/2-2 first_ring = 1 mask = model_circle(last_ring, nx, nx) refi = [] numref = EMUtil.get_image_count(outfile_repro) cnx = nx/2 +1 cny = cnx mode = "F" numr = Numrinit(first_ring, last_ring, rstep, mode) wr = ringwe(numr, mode) ima.to_zero() for j in xrange(numref): temp = EMData() temp.read_image(outfile_repro, j) # even, odd, numer of even, number of images. After frc, totav refi.append(temp) # for each node read its share of data data = EMData.read_images(compare_ref_free, range(image_start, image_end)) for im in xrange(image_start, image_end): data[im-image_start].set_attr('ID', im) set_params2D(data[im-image_start],[0,0,0,0,1]) ringref = [] for j in xrange(numref): refi[j].process_inplace("normalize.mask", {"mask":mask, "no_sigma":1}) # normalize reference images to N(0,1) cimage = Util.Polar2Dm(refi[j], cnx, cny, numr, mode) Util.Frngs(cimage, numr) Applyws(cimage, numr, wr) ringref.append(cimage) if myid == main_node: seed(1000) data_shift = [] frc = [] res = [] for im in xrange(image_start, image_end): alpha, sx, sy, mirror, scale = get_params2D(data[im-image_start]) alphai, sxi, syi, scalei = inverse_transform2(alpha, sx, sy, 1.0) # normalize data[im-image_start].process_inplace("normalize.mask", {"mask":mask, "no_sigma":1}) # subtract average under the mask # align current image to the reference [angt, sxst, syst, mirrort, xiref, peakt] = Util.multiref_polar_ali_2d(data[im-image_start], ringref, xrng, yrng, 1, mode, numr, cnx+sxi, cny+syi) iref = int(xiref) [alphan, sxn, syn, mn] = combine_params2(0.0, -sxi, -syi, 0, angt, sxst, syst, (int)(mirrort)) set_params2D(data[im-image_start], [alphan, sxn, syn, int(mn), scale]) temp = rot_shift2D(data[im-image_start], alphan, sxn, syn, mn) temp.set_attr('assign',iref) tfrc = fsc_mask(temp,refi[iref],mask = mask) temp.set_attr('frc',tfrc[1]) res = fit_tanh(tfrc) temp.set_attr('res',res) data_shift.append(temp) for node in xrange(nproc): if myid == node: for image in data_shift: image.write_image(fout,-1) refindex = image.get_attr('assign') refi[refindex].write_image(fout,-1) mpi_barrier(MPI_COMM_WORLD) rejects = [] if myid == main_node: a = EMData() index = 0 frc = [] res = [] temp = [] classes = [] for im in xrange(nima): a.read_image(fout, index) frc.append(a.get_attr("frc")) if ref_free_cutoff != -1: classes.append(a.get_attr("class_ptcl_idxs")) tmp = a.get_attr("res") temp.append(tmp[0]) res.append("%12f" %(apix/tmp[0])) res.append("\n") index = index + 2 res_num = array(temp) mean_score = res_num.mean(axis=0) std_score = res_num.std(axis=0) std = std_score / 2 if ref_free_cutoff !=-1: cutoff = mean_score - std * ref_free_cutoff reject = res_num < cutoff index = 0 for i in reject: if i: rejects.extend(classes[index]) index = index + 1 rejects.sort() length = mpi_bcast(len(rejects),1,MPI_INT,main_node, MPI_COMM_WORLD) rejects = mpi_bcast(rejects,length , MPI_INT, main_node, MPI_COMM_WORLD) del a fout_frc = open(frc_out,'w') fout_res = open(res_out,'w') fout_res.write("".join(res)) temp = zip(*frc) datstrings = [] for i in temp: for j in i: datstrings.append(" %12f" % (j)) datstrings.append("\n") fout_frc.write("".join(datstrings)) fout_frc.close() del refi del ringref return rejects