def gen_rings_ctf( prjref, nx, ctf, numr): """ Convert set of ffts of projections to Fourier rings with additional multiplication by a ctf The command returns list of rings """ from math import sin, cos, pi from fundamentals import fft from alignment import ringwe from filter import filt_ctf mode = "F" wr_four = ringwe(numr, "F") cnx = nx//2 + 1 cny = nx//2 + 1 qv = pi/180.0 refrings = [] # list of (image objects) reference projections in Fourier representation for i in range( len(prjref) ): cimage = Util.Polar2Dm(filt_ctf(prjref[i], ctf, True) , cnx, cny, numr, mode) # currently set to quadratic.... Util.Normalize_ring(cimage, numr, 0 ) Util.Frngs(cimage, numr) Util.Applyws(cimage, numr, wr_four) refrings.append(cimage) phi = prjref[i].get_attr('phi') theta = prjref[i].get_attr('theta') psi = prjref[i].get_attr('psi') n1 = sin(theta*qv)*cos(phi*qv) n2 = sin(theta*qv)*sin(phi*qv) n3 = cos(theta*qv) refrings[i].set_attr_dict( {"n1":n1, "n2":n2, "n3":n3, "phi": phi, "theta": theta,"psi": psi} ) return refrings
def gen_rings_ctf( prjref, nx, ctf, numr): """ Convert set of ffts of projections to Fourier rings with additional multiplication by a ctf The command returns list of rings """ from math import sin, cos, pi from fundamentals import fft from alignment import ringwe from filter import filt_ctf mode = "F" wr_four = ringwe(numr, "F") cnx = nx//2 + 1 cny = nx//2 + 1 qv = pi/180.0 refrings = [] # list of (image objects) reference projections in Fourier representation for i in xrange( len(prjref) ): cimage = Util.Polar2Dm(filt_ctf(prjref[i], ctf, True) , cnx, cny, numr, mode) # currently set to quadratic.... Util.Normalize_ring(cimage, numr) Util.Frngs(cimage, numr) Util.Applyws(cimage, numr, wr_four) refrings.append(cimage) phi = prjref[i].get_attr('phi') theta = prjref[i].get_attr('theta') psi = prjref[i].get_attr('psi') n1 = sin(theta*qv)*cos(phi*qv) n2 = sin(theta*qv)*sin(phi*qv) n3 = cos(theta*qv) refrings[i].set_attr_dict( {"n1":n1, "n2":n2, "n3":n3, "phi": phi, "theta": theta,"psi": psi} ) return refrings
def prepare_refringsHelical( volft, kb, nx, delta, ref_a, oplane, sym, numr, MPI=False): """ prepare projections for helical processing rotation 180 degrees inplane & specified out-of-plane """ from alignment import ringwe, Applyws from projection import prgs from math import sin, cos, pi from applications import MPI_start_end from utilities import bcast_list_to_all, bcast_number_to_all, reduce_EMData_to_root, bcast_EMData_to_all import re # convert csym to integer: sym = int(re.sub("\D", "", sym)) # generate list of Eulerian angles for reference projections # phi, theta, psi mode = "F" ref_angles = [] inplane=int((179.99/sym)/delta) + 1 # first create 0 and positive out-of-plane tilts i = 0 while i < oplane: for j in xrange(inplane): t = j*delta ref_angles.append([t,90.0+i,90.0]) i+=delta # negative out of plane rotation i = -(delta) while i > -(oplane): for j in xrange(inplane): t = j*delta ref_angles.append([t,90.0+i,90.0]) i-=delta wr_four = ringwe(numr, mode) cnx = nx//2 + 1 cny = nx//2 + 1 qv = pi/180. num_ref = len(ref_angles) if MPI: from mpi import mpi_comm_rank, mpi_comm_size, MPI_COMM_WORLD myid = mpi_comm_rank( MPI_COMM_WORLD ) ncpu = mpi_comm_size( MPI_COMM_WORLD ) else: ncpu = 1 myid = 0 from applications import MPI_start_end ref_start,ref_end = MPI_start_end( num_ref, ncpu, myid ) refrings = [] # list of (image objects) reference projections in Fourier representation sizex = numr[ len(numr)-2 ] + numr[ len(numr)-1 ] - 1 for i in xrange(num_ref): prjref = EMData() prjref.set_size(sizex, 1, 1) refrings.append(prjref) for i in xrange(ref_start, ref_end): prjref = prgs(volft, kb, [ref_angles[i][0], ref_angles[i][1], ref_angles[i][2], 0.0, 0.0]) cimage = Util.Polar2Dm(prjref, cnx, cny, numr, mode) # currently set to quadratic.... Util.Normalize_ring(cimage, numr) Util.Frngs(cimage, numr) Applyws(cimage, numr, wr_four) refrings[i] = cimage if MPI: from utilities import bcast_EMData_to_all for i in xrange(num_ref): for j in xrange(ncpu): ref_start,ref_end = MPI_start_end(num_ref,ncpu,j) if i >= ref_start and i < ref_end: rootid = j bcast_EMData_to_all( refrings[i], myid, rootid ) for i in xrange(len(ref_angles)): n1 = sin(ref_angles[i][1]*qv)*cos(ref_angles[i][0]*qv) n2 = sin(ref_angles[i][1]*qv)*sin(ref_angles[i][0]*qv) n3 = cos(ref_angles[i][1]*qv) refrings[i].set_attr_dict( {"n1":n1, "n2":n2, "n3":n3} ) refrings[i].set_attr("phi", ref_angles[i][0]) refrings[i].set_attr("theta", ref_angles[i][1]) refrings[i].set_attr("psi", ref_angles[i][2]) return refrings
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