def max_3D_pixel_error(t1, t2, r=1.0): """ Compute maximum pixel error between two sets of orientation parameters assuming object has radius r, t1 is the projection transformation of the first projection and t2 of the second one, respectively: t = Transform({"type":"spider","phi":phi,"theta":theta,"psi":psi}) t.set_trans(Vec2f(-tx, -ty)) Note the function is symmetric in t1, t2. """ pass #IMPORTIMPORTIMPORT from EMAN2 import Vec2f pass #IMPORTIMPORTIMPORT import types dummy = EMAN2_cppwrap.Transform() if (type(dummy) != type(t1)): t = EMAN2_cppwrap.Transform({ "type": "spider", "phi": t1[0], "theta": t1[1], "psi": t1[2] }) t.set_trans(EMAN2_cppwrap.Vec2f(-t1[3], -t1[4])) else: t = t1 if (type(dummy) != type(t2)): u = EMAN2_cppwrap.Transform({ "type": "spider", "phi": t2[0], "theta": t2[1], "psi": t2[2] }) u.set_trans(EMAN2_cppwrap.Vec2f(-t2[3], -t2[4])) else: u = t2 return EMAN2_cppwrap.EMData().max_3D_pixel_error(t, u, r)
def project(volume, params, radius=-1): """ Name project - calculate 2-D projection of a 3-D volume using trilinear interpolation Input vol: input volume, all dimensions have to be the same params: input parameters given as a list [phi, theta, psi, s2x, s2y], projection in calculated using the three Eulerian angles and then shifted by s2x,s2y radius: radius of a sphere within which the projection of the volume will be calculated Output proj: generated 2-D projection """ # angles phi, theta, psi pass #IMPORTIMPORTIMPORT from sp_fundamentals import rot_shift2D pass #IMPORTIMPORTIMPORT from sp_utilities import set_params_proj pass #IMPORTIMPORTIMPORT from EMAN2 import Processor if (radius > 0): myparams = { "transform": EMAN2_cppwrap.Transform({ "type": "spider", "phi": params[0], "theta": params[1], "psi": params[2] }), "radius": radius } else: myparams = { "transform": EMAN2_cppwrap.Transform({ "type": "spider", "phi": params[0], "theta": params[1], "psi": params[2] }) } proj = volume.project("pawel", myparams) if (params[3] != 0. or params[4] != 0.): params2 = { "filter_type": EMAN2_cppwrap.Processor.fourier_filter_types.SHIFT, "x_shift": params[3], "y_shift": params[4], "z_shift": 0.0 } proj = EMAN2_cppwrap.Processor.EMFourierFilter(proj, params2) #proj = rot_shift2D(proj, sx = params[3], sy = params[4], interpolation_method = "linear") sp_utilities.set_params_proj( proj, [params[0], params[1], params[2], -params[3], -params[4]]) proj.set_attr_dict({'ctf_applied': 0}) return proj
def prgl(volft, params, interpolation_method=0, return_real=True): """ Name prgl - calculate 2-D projection of a 3-D volume using either NN Fourier or or trilinear Fourier Input vol: input volume, the volume has to be cubic params: 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 interpolation_method = 0 NN interpolation_method = 1 trilinear return_real: True - return real; False - return FT of a projection. Output proj: generated 2-D projection """ # params: phi, theta, psi, sx, sy pass #IMPORTIMPORTIMPORT from sp_fundamentals import fft pass #IMPORTIMPORTIMPORT from sp_utilities import set_params_proj, info pass #IMPORTIMPORTIMPORT from EMAN2 import Processor if (interpolation_method < 0 or interpolation_method > 1): sp_global_def.ERROR('Unsupported interpolation method', "interpolation_method", 1, 0) npad = volft.get_attr_default("npad", 1) R = EMAN2_cppwrap.Transform({ "type": "spider", "phi": params[0], "theta": params[1], "psi": params[2] }) if (npad == 1): temp = volft.extract_section(R, interpolation_method) elif (npad == 2): temp = volft.extract_section2(R, interpolation_method) temp.fft_shuffle() temp.center_origin_fft() if (params[3] != 0. or params[4] != 0.): filt_params = { "filter_type": EMAN2_cppwrap.Processor.fourier_filter_types.SHIFT, "x_shift": params[3], "y_shift": params[4], "z_shift": 0.0 } temp = EMAN2_cppwrap.Processor.EMFourierFilter(temp, filt_params) if return_real: temp.do_ift_inplace() temp.set_attr_dict({'ctf_applied': 0, 'npad': 1}) temp.depad() else: temp.set_attr_dict({'ctf_applied': 0, 'npad': 1}) sp_utilities.set_params_proj( temp, [params[0], params[1], params[2], -params[3], -params[4]]) return temp
def prgs(volft, kb, params, kbx=None, kby=None): """ Name prg - calculate 2-D projection of a 3-D volume Input vol: input volume, the volume can be either cubic or rectangular kb: interpolants generated using prep_vol (tabulated Kaiser-Bessel function). If the volume is cubic, kb is the only interpolant. Otherwise, kb is the for caculating weigthing along z direction. kbx,kby: interpolants generated using prep_vol used to calculae weighting aling x and y directin. Default is none when the volume is cubic. If the volume is rectangular, kbx and kby must be given. params: 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 Output proj: generated 2-D projection """ # params: phi, theta, psi, sx, sy pass #IMPORTIMPORTIMPORT from sp_fundamentals import fft pass #IMPORTIMPORTIMPORT from sp_utilities import set_params_proj pass #IMPORTIMPORTIMPORT from EMAN2 import Processor R = EMAN2_cppwrap.Transform({ "type": "spider", "phi": params[0], "theta": params[1], "psi": params[2] }) if kbx is None: temp = volft.extract_plane(R, kb) else: temp = volft.extract_plane_rect(R, kbx, kby, kb) temp.fft_shuffle() temp.center_origin_fft() if (params[3] != 0. or params[4] != 0.): filt_params = { "filter_type": EMAN2_cppwrap.Processor.fourier_filter_types.SHIFT, "x_shift": params[3], "y_shift": params[4], "z_shift": 0.0 } temp = EMAN2_cppwrap.Processor.EMFourierFilter(temp, filt_params) temp.do_ift_inplace() sp_utilities.set_params_proj( temp, [params[0], params[1], params[2], -params[3], -params[4]]) temp.set_attr_dict({'ctf_applied': 0, 'npad': 2}) temp.depad() return temp
def func(args, data, return_avg_pixel_error=True): pass #IMPORTIMPORTIMPORT from math import pi, sin, cos, radians, degrees ali_params = data[0] d = data[1] L = len(ali_params) N = len(ali_params[0]) / 4 args_list = [0.0] * (L * 3) for i in range(L * 3 - 3): args_list[i] = args[i] cosa = [0.0] * L sina = [0.0] * L for i in range(L): cosa[i] = numpy.cos(numpy.radians(args_list[i * 3])) sina[i] = numpy.sin(numpy.radians(args_list[i * 3])) sqr_pixel_error = [0.0] * N ave_params = [] for i in range(N): sum_cosa = 0.0 sum_sina = 0.0 sx = [0.0] * L sy = [0.0] * L for j in range(L): if int(ali_params[j][i * 4 + 3]) == 0: sum_cosa += numpy.cos( numpy.radians(args_list[j * 3] + ali_params[j][i * 4])) sum_sina += numpy.sin( numpy.radians(args_list[j * 3] + ali_params[j][i * 4])) sx[j] = args_list[j * 3 + 1] + ali_params[j][ i * 4 + 1] * cosa[j] + ali_params[j][i * 4 + 2] * sina[j] sy[j] = args_list[j * 3 + 2] - ali_params[j][ i * 4 + 1] * sina[j] + ali_params[j][i * 4 + 2] * cosa[j] else: sum_cosa += numpy.cos( numpy.radians(-args_list[j * 3] + ali_params[j][i * 4])) sum_sina += numpy.sin( numpy.radians(-args_list[j * 3] + ali_params[j][i * 4])) sx[j] = -args_list[j * 3 + 1] + ali_params[j][ i * 4 + 1] * cosa[j] - ali_params[j][i * 4 + 2] * sina[j] sy[j] = args_list[j * 3 + 2] + ali_params[j][ i * 4 + 1] * sina[j] + ali_params[j][i * 4 + 2] * cosa[j] sqrtP = numpy.sqrt(sum_cosa**2 + sum_sina**2) sqr_pixel_error[i] = max( 0.0, d * d / 4. * (1 - sqrtP / L) + sqerr(sx) + sqerr(sy)) # Get ave transform params H = EMAN2_cppwrap.Transform({"type": "2D"}) H.set_matrix([ sum_cosa / sqrtP, sum_sina / sqrtP, 0.0, sum(sx) / L, -sum_sina / sqrtP, sum_cosa / sqrtP, 0.0, sum(sy) / L, 0.0, 0.0, 1.0, 0.0 ]) dd = H.get_params("2D") # We are using here mirror of the LAST SET. H = EMAN2_cppwrap.Transform({ "type": "2D", "alpha": dd["alpha"], "tx": dd["tx"], "ty": dd["ty"], "mirror": int(ali_params[L - 1][i * 4 + 3]), "scale": 1.0 }) dd = H.get_params("2D") ave_params.append([dd["alpha"], dd["tx"], dd["ty"], dd["mirror"]]) # Warning: Whatever I return here is squared pixel error, this is for the easy expression of derivative # Don't forget to square root it after getting the value if return_avg_pixel_error: return sum(sqr_pixel_error) / N else: return sqr_pixel_error, ave_params
def func(args, data, return_avg_pixel_error=True): ali_params = data[0] d = data[1] L = len(ali_params) N = old_div(len(ali_params[0]), 4) args_list = [0.0] * (L * 3) for i in range(L * 3 - 3): args_list[i] = args[i] cosa = [0.0] * L sina = [0.0] * L for i in range(L): cosa[i] = numpy.cos(numpy.radians(args_list[i * 3])) sina[i] = numpy.sin(numpy.radians(args_list[i * 3])) sqr_pixel_error = [0.0] * N ave_params = [] for i in range(N): sum_cosa = 0.0 sum_sina = 0.0 sx = [0.0] * L sy = [0.0] * L for j in range(L): if int(ali_params[j][i * 4 + 3]) == 0: sum_cosa += numpy.cos( numpy.radians(args_list[j * 3] + ali_params[j][i * 4])) sum_sina += numpy.sin( numpy.radians(args_list[j * 3] + ali_params[j][i * 4])) sx[j] = (args_list[j * 3 + 1] + ali_params[j][i * 4 + 1] * cosa[j] + ali_params[j][i * 4 + 2] * sina[j]) sy[j] = (args_list[j * 3 + 2] - ali_params[j][i * 4 + 1] * sina[j] + ali_params[j][i * 4 + 2] * cosa[j]) else: sum_cosa += numpy.cos( numpy.radians(-args_list[j * 3] + ali_params[j][i * 4])) sum_sina += numpy.sin( numpy.radians(-args_list[j * 3] + ali_params[j][i * 4])) sx[j] = (-args_list[j * 3 + 1] + ali_params[j][i * 4 + 1] * cosa[j] - ali_params[j][i * 4 + 2] * sina[j]) sy[j] = (args_list[j * 3 + 2] + ali_params[j][i * 4 + 1] * sina[j] + ali_params[j][i * 4 + 2] * cosa[j]) sqrtP = numpy.sqrt(sum_cosa**2 + sum_sina**2) sqr_pixel_error[i] = max( 0.0, old_div(d * d, 4.0) * (1 - old_div(sqrtP, L)) + sqerr(sx) + sqerr(sy), ) # Get ave transform params H = EMAN2_cppwrap.Transform({"type": "2D"}) H.set_matrix([ old_div(sum_cosa, sqrtP), old_div(sum_sina, sqrtP), 0.0, old_div(sum(sx), L), -old_div(sum_sina, sqrtP), old_div(sum_cosa, sqrtP), 0.0, old_div(sum(sy), L), 0.0, 0.0, 1.0, 0.0, ]) dd = H.get_params("2D") # We are using here mirror of the LAST SET. H = EMAN2_cppwrap.Transform({ "type": "2D", "alpha": dd["alpha"], "tx": dd["tx"], "ty": dd["ty"], "mirror": int(ali_params[L - 1][i * 4 + 3]), "scale": 1.0, }) dd = H.get_params("2D") ave_params.append([dd["alpha"], dd["tx"], dd["ty"], dd["mirror"]]) # Warning: Whatever I return here is squared pixel error, this is for the easy expression of derivative # Don't forget to square root it after getting the value if return_avg_pixel_error: return old_div(sum(sqr_pixel_error), N) else: return sqr_pixel_error, ave_params
def shc( data, refrings, list_of_reference_angles, numr, xrng, yrng, step, an=-1.0, sym="c1", finfo=None, ): number_of_checked_refs = 0 mode = "F" nx = data.get_xsize() ny = data.get_ysize() # center is in SPIDER convention cnx = old_div(nx, 2) + 1 cny = old_div(ny, 2) + 1 if an >= 0.0: ant = numpy.cos(numpy.radians(an)) else: ant = -1.0 # phi, theta, psi, sxo, syo = get_params_proj(data) t1 = data.get_attr("xform.projection") dp = t1.get_params("spider") ou = numr[-3] sxi = round(-dp["tx"], 2) syi = round(-dp["ty"], 2) txrng = search_range(nx, ou, sxi, xrng) tyrng = search_range(ny, ou, syi, yrng) if finfo: finfo.write( "Old parameters: %9.4f %9.4f %9.4f %9.4f %9.4f\n" % (dp["phi"], dp["theta"], dp["psi"], -dp["tx"], -dp["ty"]) ) finfo.flush() z1, z2, z3, z4, z5 = sp_utilities.get_params_proj(data, "xform.anchor") finfo.write( "Anc parameters: %9.4f %9.4f %9.4f %9.4f %9.4f\n" % (z1, z2, z3, -z4, -z5) ) finfo.flush() previousmax = data.get_attr("previousmax") [ang, sxs, sys, mirror, iref, peak, checked_refs] = EMAN2_cppwrap.Util.shc( data, refrings, list_of_reference_angles, txrng, tyrng, step, ant, mode, numr, cnx - sxi, cny - syi, sym, ) iref = int(iref) number_of_checked_refs += int(checked_refs) if peak <= previousmax: return -1.0e23, 0.0, number_of_checked_refs, -1 """Multiline Comment50""" else: # The ormqip returns parameters such that the transformation is applied first, the mirror operation second. # What that means is that one has to change the the Eulerian angles so they point into mirrored direction: phi+180, 180-theta, 180-psi if mirror: phi = (refrings[iref].get_attr("phi") + 540.0) % 360.0 theta = 180.0 - refrings[iref].get_attr("theta") psi = (540.0 - refrings[iref].get_attr("psi") - ang) % 360.0 else: phi = refrings[iref].get_attr("phi") theta = refrings[iref].get_attr("theta") psi = (360.0 + refrings[iref].get_attr("psi") - ang) % 360.0 s2x = sxs + sxi s2y = sys + syi # set_params_proj(data, [phi, theta, psi, s2x, s2y]) t2 = EMAN2_cppwrap.Transform( {"type": "spider", "phi": phi, "theta": theta, "psi": psi} ) t2.set_trans(EMAN2_cppwrap.Vec2f(-s2x, -s2y)) data.set_attr("xform.projection", t2) data.set_attr("previousmax", peak) # Find the pixel error that is minimum over symmetry transformations if sym == "nomirror" or sym == "c1": pixel_error = sp_pixel_error.max_3D_pixel_error(t1, t2, numr[-3]) else: ts = t2.get_sym_proj(sym) # only do it if it is not c1 pixel_error = +1.0e23 for ut in ts: # we do not care which position minimizes the error pixel_error = min( sp_pixel_error.max_3D_pixel_error(t1, ut, numr[-3]), pixel_error ) if finfo: finfo.write( "New parameters: %9.4f %9.4f %9.4f %9.4f %9.4f %10.5f %11.3e\n\n" % (phi, theta, psi, s2x, s2y, peak, pixel_error) ) finfo.flush() return peak, pixel_error, number_of_checked_refs, iref
def recons3d_trl_struct_MPI( myid, main_node, prjlist, paramstructure, refang, rshifts_shrank, delta, upweighted=True, mpi_comm=None, CTF=True, target_size=-1, avgnorm=1.0, norm_per_particle=None, ): """ recons3d_4nn_ctf - calculate CTF-corrected 3-D reconstruction from a set of projections using three Eulerian angles, two shifts, and CTF settings for each projeciton image Input list_of_prjlist: list of lists of projections to be included in the reconstruction """ if mpi_comm == None: mpi_comm = mpi.MPI_COMM_WORLD refvol = sp_utilities.model_blank(target_size) refvol.set_attr("fudge", 1.0) if CTF: do_ctf = 1 else: do_ctf = 0 fftvol = EMAN2_cppwrap.EMData() weight = EMAN2_cppwrap.EMData() params = { "size": target_size, "npad": 2, "snr": 1.0, "sign": 1, "symmetry": "c1", "refvol": refvol, "fftvol": fftvol, "weight": weight, "do_ctf": do_ctf, } r = EMAN2_cppwrap.Reconstructors.get("nn4_ctfw", params) r.setup() if prjlist: if norm_per_particle == None: norm_per_particle = len(prjlist) * [1.0] nnx = prjlist[0].get_xsize() nny = prjlist[0].get_ysize() nshifts = len(rshifts_shrank) for im in range(len(prjlist)): # parse projection structure, generate three lists: # [ipsi+iang], [ishift], [probability] # Number of orientations for a given image numbor = len(paramstructure[im][2]) ipsiandiang = [ old_div(paramstructure[im][2][i][0], 1000) for i in range(numbor) ] allshifts = [ paramstructure[im][2][i][0] % 1000 for i in range(numbor) ] probs = [paramstructure[im][2][i][1] for i in range(numbor)] # Find unique projection directions tdir = list(set(ipsiandiang)) bckgn = prjlist[im].get_attr("bckgnoise") ct = prjlist[im].get_attr("ctf") # For each unique projection direction: data = [None] * nshifts for ii in range(len(tdir)): # Find the number of times given projection direction appears on the list, it is the number of different shifts associated with it. lshifts = sp_utilities.findall(tdir[ii], ipsiandiang) toprab = 0.0 for ki in range(len(lshifts)): toprab += probs[lshifts[ki]] recdata = EMAN2_cppwrap.EMData(nny, nny, 1, False) recdata.set_attr("is_complex", 0) for ki in range(len(lshifts)): lpt = allshifts[lshifts[ki]] if data[lpt] == None: data[lpt] = sp_fundamentals.fshift( prjlist[im], rshifts_shrank[lpt][0], rshifts_shrank[lpt][1]) data[lpt].set_attr("is_complex", 0) EMAN2_cppwrap.Util.add_img( recdata, EMAN2_cppwrap.Util.mult_scalar( data[lpt], old_div(probs[lshifts[ki]], toprab)), ) recdata.set_attr_dict({ "padffted": 1, "is_fftpad": 1, "is_fftodd": 0, "is_complex_ri": 1, "is_complex": 1, }) if not upweighted: recdata = sp_filter.filt_table(recdata, bckgn) recdata.set_attr_dict({"bckgnoise": bckgn, "ctf": ct}) ipsi = tdir[ii] % 100000 iang = old_div(tdir[ii], 100000) r.insert_slice( recdata, EMAN2_cppwrap.Transform({ "type": "spider", "phi": refang[iang][0], "theta": refang[iang][1], "psi": refang[iang][2] + ipsi * delta, }), old_div(toprab * avgnorm, norm_per_particle[im]), ) # clean stuff del bckgn, recdata, tdir, ipsiandiang, allshifts, probs sp_utilities.reduce_EMData_to_root(fftvol, myid, main_node, comm=mpi_comm) sp_utilities.reduce_EMData_to_root(weight, myid, main_node, comm=mpi_comm) if myid == main_node: dummy = r.finish(True) mpi.mpi_barrier(mpi_comm) if myid == main_node: return fftvol, weight, refvol else: return None, None, None
def main(): def params_3D_2D_NEW(phi, theta, psi, s2x, s2y, mirror): # the final ali2d parameters already combine shifts operation first and rotation operation second for parameters converted from 3D if mirror: m = 1 alpha, sx, sy, scalen = sp_utilities.compose_transform2( 0, s2x, s2y, 1.0, 540.0 - psi, 0, 0, 1.0) else: m = 0 alpha, sx, sy, scalen = sp_utilities.compose_transform2( 0, s2x, s2y, 1.0, 360.0 - psi, 0, 0, 1.0) return alpha, sx, sy, m progname = optparse.os.path.basename(sys.argv[0]) usage = ( progname + " prj_stack --ave2D= --var2D= --ave3D= --var3D= --img_per_grp= --fl= --aa= --sym=symmetry --CTF" ) parser = optparse.OptionParser(usage, version=sp_global_def.SPARXVERSION) parser.add_option("--output_dir", type="string", default="./", help="Output directory") parser.add_option( "--ave2D", type="string", default=False, help="Write to the disk a stack of 2D averages", ) parser.add_option( "--var2D", type="string", default=False, help="Write to the disk a stack of 2D variances", ) parser.add_option( "--ave3D", type="string", default=False, help="Write to the disk reconstructed 3D average", ) parser.add_option( "--var3D", type="string", default=False, help="Compute 3D variability (time consuming!)", ) parser.add_option( "--img_per_grp", type="int", default=100, help="Number of neighbouring projections.(Default is 100)", ) parser.add_option( "--no_norm", action="store_true", default=False, help="Do not use normalization.(Default is to apply normalization)", ) # parser.add_option("--radius", type="int" , default=-1 , help="radius for 3D variability" ) parser.add_option( "--npad", type="int", default=2, help= "Number of time to pad the original images.(Default is 2 times padding)", ) parser.add_option("--sym", type="string", default="c1", help="Symmetry. (Default is no symmetry)") parser.add_option( "--fl", type="float", default=0.0, help= "Low pass filter cutoff in absolute frequency (0.0 - 0.5) and is applied to decimated images. (Default - no filtration)", ) parser.add_option( "--aa", type="float", default=0.02, help= "Fall off of the filter. Use default value if user has no clue about falloff (Default value is 0.02)", ) parser.add_option( "--CTF", action="store_true", default=False, help="Use CFT correction.(Default is no CTF correction)", ) # parser.add_option("--MPI" , action="store_true", default=False, help="use MPI version") # parser.add_option("--radiuspca", type="int" , default=-1 , help="radius for PCA" ) # parser.add_option("--iter", type="int" , default=40 , help="maximum number of iterations (stop criterion of reconstruction process)" ) # parser.add_option("--abs", type="float" , default=0.0 , help="minimum average absolute change of voxels' values (stop criterion of reconstruction process)" ) # parser.add_option("--squ", type="float" , default=0.0 , help="minimum average squared change of voxels' values (stop criterion of reconstruction process)" ) parser.add_option( "--VAR", action="store_true", default=False, help="Stack of input consists of 2D variances (Default False)", ) parser.add_option( "--decimate", type="float", default=0.25, help="Image decimate rate, a number less than 1. (Default is 0.25)", ) parser.add_option( "--window", type="int", default=0, help= "Target image size relative to original image size. (Default value is zero.)", ) # parser.add_option("--SND", action="store_true", default=False, help="compute squared normalized differences (Default False)") # parser.add_option("--nvec", type="int" , default=0 , help="Number of eigenvectors, (Default = 0 meaning no PCA calculated)") parser.add_option( "--symmetrize", action="store_true", default=False, help="Prepare input stack for handling symmetry (Default False)", ) parser.add_option("--overhead", type="float", default=0.5, help="python overhead per CPU.") (options, args) = parser.parse_args() ##### # from mpi import * # This is code for handling symmetries by the above program. To be incorporated. PAP 01/27/2015 # Set up global variables related to bdb cache if sp_global_def.CACHE_DISABLE: sp_utilities.disable_bdb_cache() # Set up global variables related to ERROR function sp_global_def.BATCH = True # detect if program is running under MPI RUNNING_UNDER_MPI = "OMPI_COMM_WORLD_SIZE" in optparse.os.environ if RUNNING_UNDER_MPI: sp_global_def.MPI = True if options.output_dir == "./": current_output_dir = optparse.os.path.abspath(options.output_dir) else: current_output_dir = options.output_dir if options.symmetrize: if mpi.mpi_comm_size(mpi.MPI_COMM_WORLD) > 1: sp_global_def.ERROR( "Cannot use more than one CPU for symmetry preparation") if not optparse.os.path.exists(current_output_dir): optparse.os.makedirs(current_output_dir) sp_global_def.write_command(current_output_dir) if optparse.os.path.exists( optparse.os.path.join(current_output_dir, "log.txt")): optparse.os.remove( optparse.os.path.join(current_output_dir, "log.txt")) log_main = sp_logger.Logger(sp_logger.BaseLogger_Files()) log_main.prefix = optparse.os.path.join(current_output_dir, "./") instack = args[0] sym = options.sym.lower() if sym == "c1": sp_global_def.ERROR( "There is no need to symmetrize stack for C1 symmetry") line = "" for a in sys.argv: line += " " + a log_main.add(line) if instack[:4] != "bdb:": # if output_dir =="./": stack = "bdb:data" stack = "bdb:" + current_output_dir + "/data" sp_utilities.delete_bdb(stack) junk = sp_utilities.cmdexecute("sp_cpy.py " + instack + " " + stack) else: stack = instack qt = EMAN2_cppwrap.EMUtil.get_all_attributes(stack, "xform.projection") na = len(qt) ts = sp_utilities.get_symt(sym) ks = len(ts) angsa = [None] * na for k in range(ks): # Qfile = "Q%1d"%k # if options.output_dir!="./": Qfile = os.path.join(options.output_dir,"Q%1d"%k) Qfile = optparse.os.path.join(current_output_dir, "Q%1d" % k) # delete_bdb("bdb:Q%1d"%k) sp_utilities.delete_bdb("bdb:" + Qfile) # junk = cmdexecute("e2bdb.py "+stack+" --makevstack=bdb:Q%1d"%k) junk = sp_utilities.cmdexecute("e2bdb.py " + stack + " --makevstack=bdb:" + Qfile) # DB = db_open_dict("bdb:Q%1d"%k) DB = EMAN2db.db_open_dict("bdb:" + Qfile) for i in range(na): ut = qt[i] * ts[k] DB.set_attr(i, "xform.projection", ut) # bt = ut.get_params("spider") # angsa[i] = [round(bt["phi"],3)%360.0, round(bt["theta"],3)%360.0, bt["psi"], -bt["tx"], -bt["ty"]] # write_text_row(angsa, 'ptsma%1d.txt'%k) # junk = cmdexecute("e2bdb.py "+stack+" --makevstack=bdb:Q%1d"%k) # junk = cmdexecute("sxheader.py bdb:Q%1d --params=xform.projection --import=ptsma%1d.txt"%(k,k)) DB.close() # if options.output_dir =="./": delete_bdb("bdb:sdata") sp_utilities.delete_bdb("bdb:" + current_output_dir + "/" + "sdata") # junk = cmdexecute("e2bdb.py . --makevstack=bdb:sdata --filt=Q") sdata = "bdb:" + current_output_dir + "/" + "sdata" sp_global_def.sxprint(sdata) junk = sp_utilities.cmdexecute("e2bdb.py " + current_output_dir + " --makevstack=" + sdata + " --filt=Q") # junk = cmdexecute("ls EMAN2DB/sdata*") # a = get_im("bdb:sdata") a = sp_utilities.get_im(sdata) a.set_attr("variabilitysymmetry", sym) # a.write_image("bdb:sdata") a.write_image(sdata) else: myid = mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD) number_of_proc = mpi.mpi_comm_size(mpi.MPI_COMM_WORLD) main_node = 0 shared_comm = mpi.mpi_comm_split_type(mpi.MPI_COMM_WORLD, mpi.MPI_COMM_TYPE_SHARED, 0, mpi.MPI_INFO_NULL) myid_on_node = mpi.mpi_comm_rank(shared_comm) no_of_processes_per_group = mpi.mpi_comm_size(shared_comm) masters_from_groups_vs_everything_else_comm = mpi.mpi_comm_split( mpi.MPI_COMM_WORLD, main_node == myid_on_node, myid_on_node) color, no_of_groups, balanced_processor_load_on_nodes = sp_utilities.get_colors_and_subsets( main_node, mpi.MPI_COMM_WORLD, myid, shared_comm, myid_on_node, masters_from_groups_vs_everything_else_comm, ) overhead_loading = options.overhead * number_of_proc # memory_per_node = options.memory_per_node # if memory_per_node == -1.: memory_per_node = 2.*no_of_processes_per_group keepgoing = 1 current_window = options.window current_decimate = options.decimate if len(args) == 1: stack = args[0] else: sp_global_def.sxprint("Usage: " + usage) sp_global_def.sxprint("Please run '" + progname + " -h' for detailed options") sp_global_def.ERROR( "Invalid number of parameters used. Please see usage information above." ) return t0 = time.time() # obsolete flags options.MPI = True # options.nvec = 0 options.radiuspca = -1 options.iter = 40 options.abs = 0.0 options.squ = 0.0 if options.fl > 0.0 and options.aa == 0.0: sp_global_def.ERROR( "Fall off has to be given for the low-pass filter", myid=myid) # if options.VAR and options.SND: # ERROR( "Only one of var and SND can be set!",myid=myid ) if options.VAR and (options.ave2D or options.ave3D or options.var2D): sp_global_def.ERROR( "When VAR is set, the program cannot output ave2D, ave3D or var2D", myid=myid, ) # if options.SND and (options.ave2D or options.ave3D): # ERROR( "When SND is set, the program cannot output ave2D or ave3D", myid=myid ) # if options.nvec > 0 : # ERROR( "PCA option not implemented", myid=myid ) # if options.nvec > 0 and options.ave3D == None: # ERROR( "When doing PCA analysis, one must set ave3D", myid=myid ) if current_decimate > 1.0 or current_decimate < 0.0: sp_global_def.ERROR( "Decimate rate should be a value between 0.0 and 1.0", myid=myid) if current_window < 0.0: sp_global_def.ERROR( "Target window size should be always larger than zero", myid=myid) if myid == main_node: img = sp_utilities.get_image(stack, 0) nx = img.get_xsize() ny = img.get_ysize() if min(nx, ny) < current_window: keepgoing = 0 keepgoing = sp_utilities.bcast_number_to_all(keepgoing, main_node, mpi.MPI_COMM_WORLD) if keepgoing == 0: sp_global_def.ERROR( "The target window size cannot be larger than the size of decimated image", myid=myid, ) options.sym = options.sym.lower() # if global_def.CACHE_DISABLE: # from utilities import disable_bdb_cache # disable_bdb_cache() # global_def.BATCH = True if myid == main_node: if not optparse.os.path.exists(current_output_dir): optparse.os.makedirs( current_output_dir ) # Never delete output_dir in the program! img_per_grp = options.img_per_grp # nvec = options.nvec radiuspca = options.radiuspca # if os.path.exists(os.path.join(options.output_dir, "log.txt")): os.remove(os.path.join(options.output_dir, "log.txt")) log_main = sp_logger.Logger(sp_logger.BaseLogger_Files()) log_main.prefix = optparse.os.path.join(current_output_dir, "./") if myid == main_node: line = "" for a in sys.argv: line += " " + a log_main.add(line) log_main.add("-------->>>Settings given by all options<<<-------") log_main.add("Symmetry : %s" % options.sym) log_main.add("Input stack : %s" % stack) log_main.add("Output_dir : %s" % current_output_dir) if options.ave3D: log_main.add("Ave3d : %s" % options.ave3D) if options.var3D: log_main.add("Var3d : %s" % options.var3D) if options.ave2D: log_main.add("Ave2D : %s" % options.ave2D) if options.var2D: log_main.add("Var2D : %s" % options.var2D) if options.VAR: log_main.add("VAR : True") else: log_main.add("VAR : False") if options.CTF: log_main.add("CTF correction : True ") else: log_main.add("CTF correction : False ") log_main.add("Image per group : %5d" % options.img_per_grp) log_main.add("Image decimate rate : %4.3f" % current_decimate) log_main.add("Low pass filter : %4.3f" % options.fl) current_fl = options.fl if current_fl == 0.0: current_fl = 0.5 log_main.add( "Current low pass filter is equivalent to cutoff frequency %4.3f for original image size" % round((current_fl * current_decimate), 3)) log_main.add("Window size : %5d " % current_window) log_main.add("sx3dvariability begins") symbaselen = 0 if myid == main_node: nima = EMAN2_cppwrap.EMUtil.get_image_count(stack) img = sp_utilities.get_image(stack) nx = img.get_xsize() ny = img.get_ysize() nnxo = nx nnyo = ny if options.sym != "c1": imgdata = sp_utilities.get_im(stack) try: i = imgdata.get_attr("variabilitysymmetry").lower() if i != options.sym: sp_global_def.ERROR( "The symmetry provided does not agree with the symmetry of the input stack", myid=myid, ) except: sp_global_def.ERROR( "Input stack is not prepared for symmetry, please follow instructions", myid=myid, ) i = len(sp_utilities.get_symt(options.sym)) if (old_div(nima, i)) * i != nima: sp_global_def.ERROR( "The length of the input stack is incorrect for symmetry processing", myid=myid, ) symbaselen = old_div(nima, i) else: symbaselen = nima else: nima = 0 nx = 0 ny = 0 nnxo = 0 nnyo = 0 nima = sp_utilities.bcast_number_to_all(nima) nx = sp_utilities.bcast_number_to_all(nx) ny = sp_utilities.bcast_number_to_all(ny) nnxo = sp_utilities.bcast_number_to_all(nnxo) nnyo = sp_utilities.bcast_number_to_all(nnyo) if current_window > max(nx, ny): sp_global_def.ERROR( "Window size is larger than the original image size") if current_decimate == 1.0: if current_window != 0: nx = current_window ny = current_window else: if current_window == 0: nx = int(nx * current_decimate + 0.5) ny = int(ny * current_decimate + 0.5) else: nx = int(current_window * current_decimate + 0.5) ny = nx symbaselen = sp_utilities.bcast_number_to_all(symbaselen) # check FFT prime number is_fft_friendly = nx == sp_fundamentals.smallprime(nx) if not is_fft_friendly: if myid == main_node: log_main.add( "The target image size is not a product of small prime numbers" ) log_main.add("Program adjusts the input settings!") ### two cases if current_decimate == 1.0: nx = sp_fundamentals.smallprime(nx) ny = nx current_window = nx # update if myid == main_node: log_main.add("The window size is updated to %d." % current_window) else: if current_window == 0: nx = sp_fundamentals.smallprime( int(nx * current_decimate + 0.5)) current_decimate = old_div(float(nx), nnxo) ny = nx if myid == main_node: log_main.add("The decimate rate is updated to %f." % current_decimate) else: nx = sp_fundamentals.smallprime( int(current_window * current_decimate + 0.5)) ny = nx current_window = int(old_div(nx, current_decimate) + 0.5) if myid == main_node: log_main.add("The window size is updated to %d." % current_window) if myid == main_node: log_main.add("The target image size is %d" % nx) if radiuspca == -1: radiuspca = old_div(nx, 2) - 2 if myid == main_node: log_main.add("%-70s: %d\n" % ("Number of projection", nima)) img_begin, img_end = sp_applications.MPI_start_end( nima, number_of_proc, myid) """Multiline Comment0""" """ Comments from adnan, replace index_of_proj to index_of_particle, index_of_proj was not defined also varList is not defined not made an empty list there """ if options.VAR: # 2D variance images have no shifts varList = [] # varList = EMData.read_images(stack, range(img_begin, img_end)) for index_of_particle in range(img_begin, img_end): image = sp_utilities.get_im(stack, index_of_particle) if current_window > 0: varList.append( sp_fundamentals.fdecimate( sp_fundamentals.window2d(image, current_window, current_window), nx, ny, )) else: varList.append(sp_fundamentals.fdecimate(image, nx, ny)) else: if myid == main_node: t1 = time.time() proj_angles = [] aveList = [] tab = EMAN2_cppwrap.EMUtil.get_all_attributes( stack, "xform.projection") for i in range(nima): t = tab[i].get_params("spider") phi = t["phi"] theta = t["theta"] psi = t["psi"] x = theta if x > 90.0: x = 180.0 - x x = x * 10000 + psi proj_angles.append([x, t["phi"], t["theta"], t["psi"], i]) t2 = time.time() log_main.add( "%-70s: %d\n" % ("Number of neighboring projections", img_per_grp)) log_main.add("...... Finding neighboring projections\n") log_main.add("Number of images per group: %d" % img_per_grp) log_main.add("Now grouping projections") proj_angles.sort() proj_angles_list = numpy.full((nima, 4), 0.0, dtype=numpy.float32) for i in range(nima): proj_angles_list[i][0] = proj_angles[i][1] proj_angles_list[i][1] = proj_angles[i][2] proj_angles_list[i][2] = proj_angles[i][3] proj_angles_list[i][3] = proj_angles[i][4] else: proj_angles_list = 0 proj_angles_list = sp_utilities.wrap_mpi_bcast( proj_angles_list, main_node, mpi.MPI_COMM_WORLD) proj_angles = [] for i in range(nima): proj_angles.append([ proj_angles_list[i][0], proj_angles_list[i][1], proj_angles_list[i][2], int(proj_angles_list[i][3]), ]) del proj_angles_list proj_list, mirror_list = sp_utilities.nearest_proj( proj_angles, img_per_grp, range(img_begin, img_end)) all_proj = [] for im in proj_list: for jm in im: all_proj.append(proj_angles[jm][3]) all_proj = list(set(all_proj)) index = {} for i in range(len(all_proj)): index[all_proj[i]] = i mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == main_node: log_main.add("%-70s: %.2f\n" % ("Finding neighboring projections lasted [s]", time.time() - t2)) log_main.add("%-70s: %d\n" % ("Number of groups processed on the main node", len(proj_list))) log_main.add("Grouping projections took: %12.1f [m]" % (old_div((time.time() - t2), 60.0))) log_main.add("Number of groups on main node: ", len(proj_list)) mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == main_node: log_main.add("...... Calculating the stack of 2D variances \n") # Memory estimation. There are two memory consumption peaks # peak 1. Compute ave, var; # peak 2. Var volume reconstruction; # proj_params = [0.0]*(nima*5) aveList = [] varList = [] # if nvec > 0: eigList = [[] for i in range(nvec)] dnumber = len( all_proj) # all neighborhood set for assigned to myid pnumber = len(proj_list) * 2.0 + img_per_grp # aveList and varList tnumber = dnumber + pnumber vol_size2 = old_div(nx**3 * 4.0 * 8, 1.0e9) vol_size1 = old_div(2.0 * nnxo**3 * 4.0 * 8, 1.0e9) proj_size = old_div(nnxo * nnyo * len(proj_list) * 4.0 * 2.0, 1.0e9) # both aveList and varList orig_data_size = old_div(nnxo * nnyo * 4.0 * tnumber, 1.0e9) reduced_data_size = old_div(nx * nx * 4.0 * tnumber, 1.0e9) full_data = numpy.full((number_of_proc, 2), -1.0, dtype=numpy.float16) full_data[myid] = orig_data_size, reduced_data_size if myid != main_node: sp_utilities.wrap_mpi_send(full_data, main_node, mpi.MPI_COMM_WORLD) if myid == main_node: for iproc in range(number_of_proc): if iproc != main_node: dummy = sp_utilities.wrap_mpi_recv( iproc, mpi.MPI_COMM_WORLD) full_data[numpy.where(dummy > -1)] = dummy[numpy.where( dummy > -1)] del dummy mpi.mpi_barrier(mpi.MPI_COMM_WORLD) full_data = sp_utilities.wrap_mpi_bcast(full_data, main_node, mpi.MPI_COMM_WORLD) # find the CPU with heaviest load minindx = numpy.argsort(full_data, 0) heavy_load_myid = minindx[-1][1] total_mem = sum(full_data) if myid == main_node: if current_window == 0: log_main.add( "Nx: current image size = %d. Decimated by %f from %d" % (nx, current_decimate, nnxo)) else: log_main.add( "Nx: current image size = %d. Windowed to %d, and decimated by %f from %d" % (nx, current_window, current_decimate, nnxo)) log_main.add("Nproj: number of particle images.") log_main.add("Navg: number of 2D average images.") log_main.add("Nvar: number of 2D variance images.") log_main.add( "Img_per_grp: user defined image per group for averaging = %d" % img_per_grp) log_main.add( "Overhead: total python overhead memory consumption = %f" % overhead_loading) log_main.add( "Total memory) = 4.0*nx^2*(nproj + navg +nvar+ img_per_grp)/1.0e9 + overhead: %12.3f [GB]" % (total_mem[1] + overhead_loading)) del full_data mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == heavy_load_myid: log_main.add( "Begin reading and preprocessing images on processor. Wait... " ) ttt = time.time() # imgdata = EMData.read_images(stack, all_proj) imgdata = [None for im in range(len(all_proj))] for index_of_proj in range(len(all_proj)): # image = get_im(stack, all_proj[index_of_proj]) if current_window > 0: imgdata[index_of_proj] = sp_fundamentals.fdecimate( sp_fundamentals.window2d( sp_utilities.get_im(stack, all_proj[index_of_proj]), current_window, current_window, ), nx, ny, ) else: imgdata[index_of_proj] = sp_fundamentals.fdecimate( sp_utilities.get_im(stack, all_proj[index_of_proj]), nx, ny) if current_decimate > 0.0 and options.CTF: ctf = imgdata[index_of_proj].get_attr("ctf") ctf.apix = old_div(ctf.apix, current_decimate) imgdata[index_of_proj].set_attr("ctf", ctf) if myid == heavy_load_myid and index_of_proj % 100 == 0: log_main.add( " ...... %6.2f%% " % (old_div(index_of_proj, float(len(all_proj))) * 100.0)) mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == heavy_load_myid: log_main.add("All_proj preprocessing cost %7.2f m" % (old_div( (time.time() - ttt), 60.0))) log_main.add("Wait untill reading on all CPUs done...") """Multiline Comment1""" if not options.no_norm: mask = sp_utilities.model_circle(old_div(nx, 2) - 2, nx, nx) if myid == heavy_load_myid: log_main.add("Start computing 2D aveList and varList. Wait...") ttt = time.time() inner = old_div(nx, 2) - 4 outer = inner + 2 xform_proj_for_2D = [None for i in range(len(proj_list))] for i in range(len(proj_list)): ki = proj_angles[proj_list[i][0]][3] if ki >= symbaselen: continue mi = index[ki] dpar = EMAN2_cppwrap.Util.get_transform_params( imgdata[mi], "xform.projection", "spider") phiM, thetaM, psiM, s2xM, s2yM = ( dpar["phi"], dpar["theta"], dpar["psi"], -dpar["tx"] * current_decimate, -dpar["ty"] * current_decimate, ) grp_imgdata = [] for j in range(img_per_grp): mj = index[proj_angles[proj_list[i][j]][3]] cpar = EMAN2_cppwrap.Util.get_transform_params( imgdata[mj], "xform.projection", "spider") alpha, sx, sy, mirror = params_3D_2D_NEW( cpar["phi"], cpar["theta"], cpar["psi"], -cpar["tx"] * current_decimate, -cpar["ty"] * current_decimate, mirror_list[i][j], ) if thetaM <= 90: if mirror == 0: alpha, sx, sy, scale = sp_utilities.compose_transform2( alpha, sx, sy, 1.0, phiM - cpar["phi"], 0.0, 0.0, 1.0) else: alpha, sx, sy, scale = sp_utilities.compose_transform2( alpha, sx, sy, 1.0, 180 - (phiM - cpar["phi"]), 0.0, 0.0, 1.0, ) else: if mirror == 0: alpha, sx, sy, scale = sp_utilities.compose_transform2( alpha, sx, sy, 1.0, -(phiM - cpar["phi"]), 0.0, 0.0, 1.0) else: alpha, sx, sy, scale = sp_utilities.compose_transform2( alpha, sx, sy, 1.0, -(180 - (phiM - cpar["phi"])), 0.0, 0.0, 1.0, ) imgdata[mj].set_attr( "xform.align2d", EMAN2_cppwrap.Transform({ "type": "2D", "alpha": alpha, "tx": sx, "ty": sy, "mirror": mirror, "scale": 1.0, }), ) grp_imgdata.append(imgdata[mj]) if not options.no_norm: for k in range(img_per_grp): ave, std, minn, maxx = EMAN2_cppwrap.Util.infomask( grp_imgdata[k], mask, False) grp_imgdata[k] -= ave grp_imgdata[k] = old_div(grp_imgdata[k], std) if options.fl > 0.0: for k in range(img_per_grp): grp_imgdata[k] = sp_filter.filt_tanl( grp_imgdata[k], options.fl, options.aa) # Because of background issues, only linear option works. if options.CTF: ave, var = sp_statistics.aves_wiener( grp_imgdata, SNR=1.0e5, interpolation_method="linear") else: ave, var = sp_statistics.ave_var(grp_imgdata) # Switch to std dev # threshold is not really needed,it is just in case due to numerical accuracy something turns out negative. var = sp_morphology.square_root(sp_morphology.threshold(var)) sp_utilities.set_params_proj(ave, [phiM, thetaM, 0.0, 0.0, 0.0]) sp_utilities.set_params_proj(var, [phiM, thetaM, 0.0, 0.0, 0.0]) aveList.append(ave) varList.append(var) xform_proj_for_2D[i] = [phiM, thetaM, 0.0, 0.0, 0.0] """Multiline Comment2""" if (myid == heavy_load_myid) and (i % 100 == 0): log_main.add(" ......%6.2f%% " % (old_div(i, float(len(proj_list))) * 100.0)) del imgdata, grp_imgdata, cpar, dpar, all_proj, proj_angles, index if not options.no_norm: del mask if myid == main_node: del tab # At this point, all averages and variances are computed mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == heavy_load_myid: log_main.add("Computing aveList and varList took %12.1f [m]" % (old_div((time.time() - ttt), 60.0))) xform_proj_for_2D = sp_utilities.wrap_mpi_gatherv( xform_proj_for_2D, main_node, mpi.MPI_COMM_WORLD) if myid == main_node: sp_utilities.write_text_row( [str(entry) for entry in xform_proj_for_2D], optparse.os.path.join(current_output_dir, "params.txt"), ) del xform_proj_for_2D mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if options.ave2D: if myid == main_node: log_main.add("Compute ave2D ... ") km = 0 for i in range(number_of_proc): if i == main_node: for im in range(len(aveList)): aveList[im].write_image( optparse.os.path.join( current_output_dir, options.ave2D), km, ) km += 1 else: nl = mpi.mpi_recv( 1, mpi.MPI_INT, i, sp_global_def.SPARX_MPI_TAG_UNIVERSAL, mpi.MPI_COMM_WORLD, ) nl = int(nl[0]) for im in range(nl): ave = sp_utilities.recv_EMData( i, im + i + 70000) """Multiline Comment3""" tmpvol = sp_fundamentals.fpol(ave, nx, nx, 1) tmpvol.write_image( optparse.os.path.join( current_output_dir, options.ave2D), km, ) km += 1 else: mpi.mpi_send( len(aveList), 1, mpi.MPI_INT, main_node, sp_global_def.SPARX_MPI_TAG_UNIVERSAL, mpi.MPI_COMM_WORLD, ) for im in range(len(aveList)): sp_utilities.send_EMData(aveList[im], main_node, im + myid + 70000) """Multiline Comment4""" if myid == main_node: sp_applications.header( optparse.os.path.join(current_output_dir, options.ave2D), params="xform.projection", fimport=optparse.os.path.join(current_output_dir, "params.txt"), ) mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if options.ave3D: t5 = time.time() if myid == main_node: log_main.add("Reconstruct ave3D ... ") ave3D = sp_reconstruction.recons3d_4nn_MPI( myid, aveList, symmetry=options.sym, npad=options.npad) sp_utilities.bcast_EMData_to_all(ave3D, myid) if myid == main_node: if current_decimate != 1.0: ave3D = sp_fundamentals.resample( ave3D, old_div(1.0, current_decimate)) ave3D = sp_fundamentals.fpol( ave3D, nnxo, nnxo, nnxo) # always to the orignal image size sp_utilities.set_pixel_size(ave3D, 1.0) ave3D.write_image( optparse.os.path.join(current_output_dir, options.ave3D)) log_main.add("Ave3D reconstruction took %12.1f [m]" % (old_div((time.time() - t5), 60.0))) log_main.add("%-70s: %s\n" % ("The reconstructed ave3D is saved as ", options.ave3D)) mpi.mpi_barrier(mpi.MPI_COMM_WORLD) del ave, var, proj_list, stack, alpha, sx, sy, mirror, aveList """Multiline Comment5""" if options.ave3D: del ave3D if options.var2D: if myid == main_node: log_main.add("Compute var2D...") km = 0 for i in range(number_of_proc): if i == main_node: for im in range(len(varList)): tmpvol = sp_fundamentals.fpol( varList[im], nx, nx, 1) tmpvol.write_image( optparse.os.path.join( current_output_dir, options.var2D), km, ) km += 1 else: nl = mpi.mpi_recv( 1, mpi.MPI_INT, i, sp_global_def.SPARX_MPI_TAG_UNIVERSAL, mpi.MPI_COMM_WORLD, ) nl = int(nl[0]) for im in range(nl): ave = sp_utilities.recv_EMData( i, im + i + 70000) tmpvol = sp_fundamentals.fpol(ave, nx, nx, 1) tmpvol.write_image( optparse.os.path.join( current_output_dir, options.var2D), km, ) km += 1 else: mpi.mpi_send( len(varList), 1, mpi.MPI_INT, main_node, sp_global_def.SPARX_MPI_TAG_UNIVERSAL, mpi.MPI_COMM_WORLD, ) for im in range(len(varList)): sp_utilities.send_EMData( varList[im], main_node, im + myid + 70000) # What with the attributes?? mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if myid == main_node: sp_applications.header( optparse.os.path.join(current_output_dir, options.var2D), params="xform.projection", fimport=optparse.os.path.join(current_output_dir, "params.txt"), ) mpi.mpi_barrier(mpi.MPI_COMM_WORLD) if options.var3D: if myid == main_node: log_main.add("Reconstruct var3D ...") t6 = time.time() # radiusvar = options.radius # if( radiusvar < 0 ): radiusvar = nx//2 -3 res = sp_reconstruction.recons3d_4nn_MPI(myid, varList, symmetry=options.sym, npad=options.npad) # res = recons3d_em_MPI(varList, vol_stack, options.iter, radiusvar, options.abs, True, options.sym, options.squ) if myid == main_node: if current_decimate != 1.0: res = sp_fundamentals.resample( res, old_div(1.0, current_decimate)) res = sp_fundamentals.fpol(res, nnxo, nnxo, nnxo) sp_utilities.set_pixel_size(res, 1.0) res.write_image(os.path.join(current_output_dir, options.var3D)) log_main.add( "%-70s: %s\n" % ("The reconstructed var3D is saved as ", options.var3D)) log_main.add("Var3D reconstruction took %f12.1 [m]" % (old_div( (time.time() - t6), 60.0))) log_main.add("Total computation time %f12.1 [m]" % (old_div( (time.time() - t0), 60.0))) log_main.add("sx3dvariability finishes") if RUNNING_UNDER_MPI: sp_global_def.MPI = False sp_global_def.BATCH = False
def run(): progname = optparse.os.path.basename(sys.argv[0]) usage = """%prog [options] input.pdb output.hdf Converts a pdb file into an electron density map. 0,0,0 in PDB space will map to the center of the volume.""" parser = optparse.OptionParser(usage=usage, version=EMAN2_meta.EMANVERSION) parser.add_option("--apix", "-A", type="float", help="Angstrom/voxel", default=1.0) parser.add_option("--box", "-B", type="string", help="Box size in pixels, <xyz> or <x,y,z>") parser.add_option("--het", action="store_true", help="Include HET atoms in the map", default=False) parser.add_option( "--chains", type="string", help= "String list of chain identifiers to include, e.g. 'ABEFG'; default: include all chains", default="", ) parser.add_option( "--center", type="string", default="a", help= "center: c - coordinates; a (default) - center of gravity; <x,y,z> - vector (in Angstrom) to subtract from all coordinates; n - none", ) parser.add_option("--O", action="store_true", default=False, help="use O system of coordinates") parser.add_option("--quiet", action="store_true", default=False, help="Verbose is the default") parser.add_option( "--tr0", type="string", default="none", help="Filename of initial 3x4 transformation matrix", ) parser.add_option( "--set_apix_value", action="store_true", help="Set apix value in header of the ouput map", default=False, ) (options, args) = parser.parse_args() # if len(args) < 2: sp_global_def.ERROR("Input and output files required") return if sp_global_def.CACHE_DISABLE: sp_utilities.disable_bdb_cache() chains = options.chains if chains == "": chains = None try: infile = open(args[0], "r") except: sp_global_def.ERROR("Cannot open input file") return aavg = [0, 0, 0] # calculate atomic center asig = [0, 0, 0] # to compute radius of gyration natm = 0 atoms = [] # we store a list of atoms to process to avoid multiple passes nelec = 0 mass = 0 # read in initial-transformation file: if options.tr0 != "none": cols = sp_utilities.read_text_file(options.tr0, -1) txlist = [] for i in range(3): txlist.append(cols[0][i]) txlist.append(cols[1][i]) txlist.append(cols[2][i]) txlist.append(cols[3][i]) tr0 = EMAN2_cppwrap.Transform(txlist) # parse the pdb file and pull out relevant atoms for line in infile: if line[:4] == "ATOM" or (line[:6] == "HETATM" and options.het): if chains and (line[21] not in chains): continue try: a = line[12:14].strip() aseq = int(line[6:11].strip()) res = int(line[22:26].strip()) if options.O: x = float(line[38:46]) y = float(line[30:38]) z = -float(line[46:54]) else: x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) except: sp_global_def.sxprint( "PDB Parse error:\n%s\n'%s','%s','%s' '%s','%s','%s'\n" % ( line, line[12:14], line[6:11], line[22:26], line[30:38], line[38:46], line[46:54], )) sp_global_def.sxprint(a, aseq, res, x, y, z) try: nelec += atomdefs[a.upper()][0] mass += atomdefs[a.upper()][1] except: sp_global_def.sxprint(( "Unknown atom %s ignored at %d. You can modify the atomdefs info at the top of this file with the atomic number and atomic weight." % (a, aseq))) continue atoms.append([a, x, y, z]) natm += 1 if options.center == "a": aavg[0] += x * atomdefs[a.upper()][1] aavg[1] += y * atomdefs[a.upper()][1] aavg[2] += z * atomdefs[a.upper()][1] asig[0] += x**2 * atomdefs[a.upper()][1] asig[1] += y**2 * atomdefs[a.upper()][1] asig[2] += z**2 * atomdefs[a.upper()][1] else: aavg[0] += x aavg[1] += y aavg[2] += z asig[0] += x**2 asig[1] += y**2 asig[2] += z**2 infile.close() if options.center == "a": rad_gyr = math.sqrt( old_div((asig[0] + asig[1] + asig[2]), mass) - (old_div(aavg[0], mass))**2 - (old_div(aavg[1], mass))**2 - (old_div(aavg[2], mass))**2) else: rad_gyr = math.sqrt( old_div((asig[0] + asig[1] + asig[2]), natm) - (old_div(aavg[0], natm))**2 - (old_div(aavg[1], natm))**2 - (old_div(aavg[2], natm))**2) if not options.quiet: sp_global_def.sxprint( "%d atoms; total charge = %d e-; mol mass = %.2f kDa; radius of gyration = %.2f A" % (natm, nelec, old_div(mass, 1000.0), rad_gyr)) # center PDB according to option: if options.center == "a": if not options.quiet: sp_global_def.sxprint( "center of gravity at %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % ( old_div(aavg[0], mass), old_div(aavg[1], mass), old_div(aavg[2], mass), )) for i in range(len(atoms)): atoms[i][1] -= old_div(aavg[0], mass) atoms[i][2] -= old_div(aavg[1], mass) atoms[i][3] -= old_div(aavg[2], mass) if options.center == "c": if not options.quiet: sp_global_def.sxprint( "atomic center at %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % ( old_div(aavg[0], natm), old_div(aavg[1], natm), old_div(aavg[2], natm), )) for i in range(len(atoms)): atoms[i][1] -= old_div(aavg[0], natm) atoms[i][2] -= old_div(aavg[1], natm) atoms[i][3] -= old_div(aavg[2], natm) spl = options.center.split(",") if len(spl) == 3: # substract the given vector from all coordinates if not options.quiet: sp_global_def.sxprint( "vector to substract: %1.1f,%1.1f,%1.1f (center of volume at 0,0,0)" % (float(spl[0]), float(spl[1]), float(spl[2]))) for i in range(len(atoms)): atoms[i][1] -= float(spl[0]) atoms[i][2] -= float(spl[1]) atoms[i][3] -= float(spl[2]) # apply given initial transformation (this used to be done before centering, # thereby loosing the translation. This is the right place to apply tr0): if options.tr0 != "none": if not options.quiet: sp_global_def.sxprint( "Applying initial transformation to PDB coordinates... ") for i in range(len(atoms)): atom_coords = EMAN2_cppwrap.Vec3f(atoms[i][1], atoms[i][2], atoms[i][3]) new_atom_coords = tr0 * atom_coords atoms[i][1] = new_atom_coords[0] atoms[i][2] = new_atom_coords[1] atoms[i][3] = new_atom_coords[2] if not options.quiet: sp_global_def.sxprint("done.\n") # bounding box: amin = [atoms[0][1], atoms[0][2], atoms[0][3]] amax = [atoms[0][1], atoms[0][2], atoms[0][3]] for i in range(1, len(atoms)): for k in range(3): amin[k] = min(atoms[i][k + 1], amin[k]) amax[k] = max(atoms[i][k + 1], amax[k]) if not options.quiet: sp_global_def.sxprint("Range of coordinates [A]: x: %7.2f - %7.2f" % (amin[0], amax[0])) sp_global_def.sxprint(" y: %7.2f - %7.2f" % (amin[1], amax[1])) sp_global_def.sxprint(" z: %7.2f - %7.2f" % (amin[2], amax[2])) # find the output box size, either user specified or from bounding box box = [0, 0, 0] try: spl = options.box.split(",") if len(spl) == 1: box[0] = box[1] = box[2] = int(spl[0]) else: box[0] = int(spl[0]) box[1] = int(spl[1]) box[2] = int(spl[2]) except: for i in range(3): box[i] = int( old_div(2 * max(math.fabs(amax[i]), math.fabs(amin[i])), options.apix)) # Increase the box size by 1/4. box[i] += old_div(box[i], 4) if not options.quiet: sp_global_def.sxprint("Bounding box [pixels]: x: %5d " % box[0]) sp_global_def.sxprint(" y: %5d " % box[1]) sp_global_def.sxprint(" z: %5d " % box[2]) # figure oversampled box size # bigb = max(box[0],box[1],box[2]) fcbig = 1 """Multiline Comment0""" if not options.quiet: sp_global_def.sxprint( "Box size: %d x %d x %d" % (box[0], box[1], box[2]), ", oversampling ", fcbig, ) # Calculate working dimensions pixelbig = old_div(options.apix, fcbig) bigbox = [] for i in range(3): bigbox.append(box[i] * fcbig) # initialize the final output volume outmap = EMAN2_cppwrap.EMData(bigbox[0], bigbox[1], bigbox[2], True) nc = [] for i in range(3): nc.append(old_div(bigbox[i], 2)) # fill in the atoms for i in range(len(atoms)): # print "Adding %d '%s'"%(i,atoms[i][0]) if not options.quiet and i % 1000 == 0: sp_global_def.sxprint("\r %d" % i, end=" ") sys.stdout.flush() try: elec = atomdefs[atoms[i][0].upper()][0] # outmap[int(atoms[i][1]/pixelbig+bigbox[0]//2),int(atoms[i][2]/pixelbig+bigbox[1]//2),int(atoms[i][3]/pixelbig+bigbox[2]//2)] += elec for k in range(2): pz = old_div(atoms[i][3], pixelbig) + nc[2] dz = pz - int(pz) uz = ((1 - k) + (2 * k - 1) * dz) * elec for l in range(2): py = old_div(atoms[i][2], pixelbig) + nc[1] dy = py - int(py) uy = ((1 - l) + (2 * l - 1) * dy) * uz for m in range(2): px = old_div(atoms[i][1], pixelbig) + nc[0] dx = px - int(px) outmap[int(px) + m, int(py) + l, int(pz) + k] += ((1 - m) + (2 * m - 1) * dx) * uy except: sp_global_def.sxprint("Skipping %d '%s'" % (i, atoms[i][0])) if not options.quiet: sp_global_def.sxprint("\r %d\nConversion complete." % len(atoms)) # ," Now shape atoms." """Multiline Comment1""" (filename_path, filextension) = optparse.os.path.splitext(args[1]) if filextension == ".hdf": if options.set_apix_value: outmap.set_attr("apix_x", options.apix) outmap.set_attr("apix_y", options.apix) outmap.set_attr("apix_z", options.apix) outmap.set_attr("pixel_size", options.apix) else: sp_global_def.sxprint("Pixel_size is not set in the header!") outmap.write_image(args[1], 0, EMAN2_cppwrap.EMUtil.ImageType.IMAGE_HDF) elif filextension == ".spi": outmap.write_image(args[1], 0, EMAN2_cppwrap.EMUtil.ImageType.IMAGE_SINGLE_SPIDER) else: sp_global_def.ERROR("Unknown image type") return