Пример #1
0
def main():
    """
	Main function.

	Arguments:
	None

	Returns:
	None
	"""

    command_args = parse_command_line()

    # Import volume
    sp_global_def.sxprint("Import volume.")
    input_vol = sp_utilities.get_im(command_args.input_volume)

    # Sanity checks
    sanity_checks(command_args, input_vol)

    try:
        os.makedirs(command_args.output_dir)
    except OSError:
        sp_global_def.sxprint(
            "Output directory already exists. No need to create it.")
    else:
        sp_global_def.sxprint("Created output directory.")
    sp_global_def.write_command(command_args.output_dir)
    output_prefix = os.path.join(command_args.output_dir, command_args.prefix)

    # Filter volume if specified
    if command_args.low_pass_filter_resolution is not None:
        sp_global_def.sxprint("Filter volume to {0}A.".format(
            command_args.low_pass_filter_resolution))
        input_vol = sp_filter.filt_tanl(
            input_vol,
            old_div(command_args.pixel_size,
                    command_args.low_pass_filter_resolution),
            command_args.low_pass_filter_falloff,
        )
        input_vol.write_image(output_prefix + "_filtered_volume.hdf")
    else:
        sp_global_def.sxprint("Skip filter volume.")

    # Create a mask based on the filtered volume
    sp_global_def.sxprint("Create mask")
    density_threshold = -9999.0
    nsigma = 1.0
    if command_args.mol_mass:
        density_threshold = input_vol.find_3d_threshold(
            command_args.mol_mass, command_args.pixel_size)
        sp_global_def.sxprint(
            "Mask molecular mass translated into binary threshold: ",
            density_threshold)
    elif command_args.threshold:
        density_threshold = command_args.threshold
    elif command_args.nsigma:
        nsigma = command_args.nsigma
    else:
        assert False

    if command_args.edge_type == "cosine":
        mode = "C"
    elif command_args.edge_type == "gaussian":
        mode = "G"
    else:
        assert False

    mask_first = sp_morphology.adaptive_mask_scipy(
        input_vol,
        nsigma=nsigma,
        threshold=density_threshold,
        ndilation=command_args.ndilation,
        nerosion=command_args.nerosion,
        edge_width=command_args.edge_width,
        allow_disconnected=command_args.allow_disconnected,
        mode=mode,
        do_approx=command_args.do_old,
        do_fill=command_args.fill_mask,
        do_print=True,
    )

    # Create a second mask based on the filtered volume
    s_mask = None
    s_density_threshold = 1
    s_nsigma = 1.0
    if command_args.second_mask is not None:
        sp_global_def.sxprint("Prepare second mask")
        s_mask = sp_utilities.get_im(command_args.second_mask)
        s_density_threshold = -9999.0
        s_nsigma = 1.0
        if command_args.s_mol_mass:
            s_density_threshold = input_vol.find_3d_threshold(
                command_args.s_mol_mass, command_args.s_pixel_size)
            sp_global_def.sxprint(
                "Second mask molecular mass translated into binary threshold: ",
                s_density_threshold,
            )
        elif command_args.s_threshold:
            s_density_threshold = command_args.s_threshold
        elif command_args.s_nsigma:
            s_nsigma = command_args.s_nsigma
        else:
            assert False
    elif command_args.second_mask_shape is not None:
        sp_global_def.sxprint("Prepare second mask")
        nx = mask_first.get_xsize()
        ny = mask_first.get_ysize()
        nz = mask_first.get_zsize()
        if command_args.second_mask_shape == "cube":
            s_nx = command_args.s_nx
            s_ny = command_args.s_ny
            s_nz = command_args.s_nz
            s_mask = sp_utilities.model_blank(s_nx, s_ny, s_nz, 1)
        elif command_args.second_mask_shape == "cylinder":
            s_radius = command_args.s_radius
            s_nx = command_args.s_nx
            s_ny = command_args.s_ny
            s_nz = command_args.s_nz
            try:
                s_mask = sp_utilities.model_cylinder(s_radius, s_nx, s_ny,
                                                     s_nz)
            except RuntimeError as e:
                sp_global_def.sxprint(
                    "An error occured! Please check the error log")
                raise
        elif command_args.second_mask_shape == "sphere":
            s_radius = command_args.s_radius
            s_nx = command_args.s_nx
            s_ny = command_args.s_ny
            s_nz = command_args.s_nz
            try:
                s_mask = sp_utilities.model_circle(s_radius, s_nx, s_ny, s_nz)
            except RuntimeError as e:
                sp_global_def.sxprint(
                    "An error occured! Please check the error log")
                raise
        else:
            assert False
        s_mask = sp_utilities.pad(s_mask, nx, ny, nz, 0)

    if s_mask is not None:
        sp_global_def.sxprint("Create second mask")

        if command_args.s_edge_type == "cosine":
            mode = "C"
        elif command_args.s_edge_type == "gaussian":
            mode = "G"
        else:
            assert False

        s_mask = sp_morphology.adaptive_mask_scipy(
            s_mask,
            nsigma=s_nsigma,
            threshold=s_density_threshold,
            ndilation=command_args.s_ndilation,
            nerosion=command_args.s_nerosion,
            edge_width=command_args.s_edge_width,
            allow_disconnected=command_args.s_allow_disconnected,
            mode=mode,
            do_approx=command_args.s_do_old,
            do_fill=command_args.s_fill_mask,
            do_print=True,
        )
        if command_args.s_invert:
            s_mask = 1 - s_mask
        sp_global_def.sxprint("Write outputs.")
        mask_first.write_image(output_prefix + "_mask_first.hdf")
        s_mask.write_image(output_prefix + "_mask_second.hdf")
        masked_combined = mask_first * s_mask
        masked_combined.write_image(output_prefix + "_mask.hdf")
    else:
        sp_global_def.sxprint("Write outputs.")
        mask_first.write_image(output_prefix + "_mask.hdf")
Пример #2
0
def helicalshiftali_MPI(stack,
                        maskfile=None,
                        maxit=100,
                        CTF=False,
                        snr=1.0,
                        Fourvar=False,
                        search_rng=-1):

    nproc = mpi.mpi_comm_size(mpi.MPI_COMM_WORLD)
    myid = mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD)
    main_node = 0

    ftp = file_type(stack)

    if myid == main_node:
        print_begin_msg("helical-shiftali_MPI")

    max_iter = int(maxit)
    if (myid == main_node):
        infils = EMUtil.get_all_attributes(stack, "filament")
        ptlcoords = EMUtil.get_all_attributes(stack, 'ptcl_source_coord')
        filaments = ordersegments(infils, ptlcoords)
        total_nfils = len(filaments)
        inidl = [0] * total_nfils
        for i in range(total_nfils):
            inidl[i] = len(filaments[i])
        linidl = sum(inidl)
        nima = linidl
        tfilaments = []
        for i in range(total_nfils):
            tfilaments += filaments[i]
        del filaments
    else:
        total_nfils = 0
        linidl = 0
    total_nfils = bcast_number_to_all(total_nfils, source_node=main_node)
    if myid != main_node:
        inidl = [-1] * total_nfils
    inidl = bcast_list_to_all(inidl, myid, source_node=main_node)
    linidl = bcast_number_to_all(linidl, source_node=main_node)
    if myid != main_node:
        tfilaments = [-1] * linidl
    tfilaments = bcast_list_to_all(tfilaments, myid, source_node=main_node)
    filaments = []
    iendi = 0
    for i in range(total_nfils):
        isti = iendi
        iendi = isti + inidl[i]
        filaments.append(tfilaments[isti:iendi])
    del tfilaments, inidl

    if myid == main_node:
        print_msg("total number of filaments: %d" % total_nfils)
    if total_nfils < nproc:
        ERROR(
            'number of CPUs (%i) is larger than the number of filaments (%i), please reduce the number of CPUs used'
            % (nproc, total_nfils),
            myid=myid)

    #  balanced load
    temp = chunks_distribution([[len(filaments[i]), i]
                                for i in range(len(filaments))],
                               nproc)[myid:myid + 1][0]
    filaments = [filaments[temp[i][1]] for i in range(len(temp))]
    nfils = len(filaments)

    #filaments = [[0,1]]
    #print "filaments",filaments
    list_of_particles = []
    indcs = []
    k = 0
    for i in range(nfils):
        list_of_particles += filaments[i]
        k1 = k + len(filaments[i])
        indcs.append([k, k1])
        k = k1
    data = EMData.read_images(stack, list_of_particles)
    ldata = len(data)
    sxprint("ldata=", ldata)
    nx = data[0].get_xsize()
    ny = data[0].get_ysize()
    if maskfile == None:
        mrad = min(nx, ny) // 2 - 2
        mask = pad(model_blank(2 * mrad + 1, ny, 1, 1.0), nx, ny, 1, 0.0)
    else:
        mask = get_im(maskfile)

    # apply initial xform.align2d parameters stored in header
    init_params = []
    for im in range(ldata):
        t = data[im].get_attr('xform.align2d')
        init_params.append(t)
        p = t.get_params("2d")
        data[im] = rot_shift2D(data[im], p['alpha'], p['tx'], p['ty'],
                               p['mirror'], p['scale'])

    if CTF:
        from sp_filter import filt_ctf
        from sp_morphology import ctf_img
        ctf_abs_sum = EMData(nx, ny, 1, False)
        ctf_2_sum = EMData(nx, ny, 1, False)
    else:
        ctf_2_sum = None
        ctf_abs_sum = None

    from sp_utilities import info

    for im in range(ldata):
        data[im].set_attr('ID', list_of_particles[im])
        st = Util.infomask(data[im], mask, False)
        data[im] -= st[0]
        if CTF:
            ctf_params = data[im].get_attr("ctf")
            qctf = data[im].get_attr("ctf_applied")
            if qctf == 0:
                data[im] = filt_ctf(fft(data[im]), ctf_params)
                data[im].set_attr('ctf_applied', 1)
            elif qctf != 1:
                ERROR('Incorrectly set qctf flag', myid=myid)
            ctfimg = ctf_img(nx, ctf_params, ny=ny)
            Util.add_img2(ctf_2_sum, ctfimg)
            Util.add_img_abs(ctf_abs_sum, ctfimg)
        else:
            data[im] = fft(data[im])

    del list_of_particles

    if CTF:
        reduce_EMData_to_root(ctf_2_sum, myid, main_node)
        reduce_EMData_to_root(ctf_abs_sum, myid, main_node)
    if CTF:
        if myid != main_node:
            del ctf_2_sum
            del ctf_abs_sum
        else:
            temp = EMData(nx, ny, 1, False)
            tsnr = 1. / snr
            for i in range(0, nx + 2, 2):
                for j in range(ny):
                    temp.set_value_at(i, j, tsnr)
                    temp.set_value_at(i + 1, j, 0.0)
            #info(ctf_2_sum)
            Util.add_img(ctf_2_sum, temp)
            #info(ctf_2_sum)
            del temp

    total_iter = 0
    shift_x = [0.0] * ldata

    for Iter in range(max_iter):
        if myid == main_node:
            start_time = time()
            print_msg("Iteration #%4d\n" % (total_iter))
        total_iter += 1
        avg = EMData(nx, ny, 1, False)
        for im in range(ldata):
            Util.add_img(avg, fshift(data[im], shift_x[im]))

        reduce_EMData_to_root(avg, myid, main_node)

        if myid == main_node:
            if CTF: tavg = Util.divn_filter(avg, ctf_2_sum)
            else: tavg = Util.mult_scalar(avg, 1.0 / float(nima))
        else:
            tavg = model_blank(nx, ny)

        if Fourvar:
            bcast_EMData_to_all(tavg, myid, main_node)
            vav, rvar = varf2d_MPI(myid, data, tavg, mask, "a", CTF)

        if myid == main_node:
            if Fourvar:
                tavg = fft(Util.divn_img(fft(tavg), vav))
                vav_r = Util.pack_complex_to_real(vav)
            # normalize and mask tavg in real space
            tavg = fft(tavg)
            stat = Util.infomask(tavg, mask, False)
            tavg -= stat[0]
            Util.mul_img(tavg, mask)
            tavg.write_image("tavg.hdf", Iter)
            # For testing purposes: shift tavg to some random place and see if the centering is still correct
            #tavg = rot_shift3D(tavg,sx=3,sy=-4)

        if Fourvar: del vav
        bcast_EMData_to_all(tavg, myid, main_node)
        tavg = fft(tavg)

        sx_sum = 0.0
        nxc = nx // 2

        for ifil in range(nfils):
            """
			# Calculate filament average
			avg = EMData(nx, ny, 1, False)
			filnima = 0
			for im in xrange(indcs[ifil][0], indcs[ifil][1]):
				Util.add_img(avg, data[im])
				filnima += 1
			tavg = Util.mult_scalar(avg, 1.0/float(filnima))
			"""
            # Calculate 1D ccf between each segment and filament average
            nsegms = indcs[ifil][1] - indcs[ifil][0]
            ctx = [None] * nsegms
            pcoords = [None] * nsegms
            for im in range(indcs[ifil][0], indcs[ifil][1]):
                ctx[im - indcs[ifil][0]] = Util.window(ccf(tavg, data[im]), nx,
                                                       1)
                pcoords[im - indcs[ifil][0]] = data[im].get_attr(
                    'ptcl_source_coord')
                #ctx[im-indcs[ifil][0]].write_image("ctx.hdf",im-indcs[ifil][0])
                #print "  CTX  ",myid,im,Util.infomask(ctx[im-indcs[ifil][0]], None, True)
            # search for best x-shift
            cents = nsegms // 2

            dst = sqrt(
                max((pcoords[cents][0] - pcoords[0][0])**2 +
                    (pcoords[cents][1] - pcoords[0][1])**2,
                    (pcoords[cents][0] - pcoords[-1][0])**2 +
                    (pcoords[cents][1] - pcoords[-1][1])**2))
            maxincline = atan2(ny // 2 - 2 - float(search_rng), dst)
            kang = int(dst * tan(maxincline) + 0.5)
            #print  "  settings ",nsegms,cents,dst,search_rng,maxincline,kang

            # ## C code for alignment. @ming
            results = [0.0] * 3
            results = Util.helixshiftali(ctx, pcoords, nsegms, maxincline,
                                         kang, search_rng, nxc)
            sib = int(results[0])
            bang = results[1]
            qm = results[2]
            #print qm, sib, bang

            # qm = -1.e23
            #
            # 			for six in xrange(-search_rng, search_rng+1,1):
            # 				q0 = ctx[cents].get_value_at(six+nxc)
            # 				for incline in xrange(kang+1):
            # 					qt = q0
            # 					qu = q0
            # 					if(kang>0):  tang = tan(maxincline/kang*incline)
            # 					else:        tang = 0.0
            # 					for kim in xrange(cents+1,nsegms):
            # 						dst = sqrt((pcoords[cents][0] - pcoords[kim][0])**2 + (pcoords[cents][1] - pcoords[kim][1])**2)
            # 						xl = dst*tang+six+nxc
            # 						ixl = int(xl)
            # 						dxl = xl - ixl
            # 						#print "  A  ", ifil,six,incline,kim,xl,ixl,dxl
            # 						qt += (1.0-dxl)*ctx[kim].get_value_at(ixl) + dxl*ctx[kim].get_value_at(ixl+1)
            # 						xl = -dst*tang+six+nxc
            # 						ixl = int(xl)
            # 						dxl = xl - ixl
            # 						qu += (1.0-dxl)*ctx[kim].get_value_at(ixl) + dxl*ctx[kim].get_value_at(ixl+1)
            # 					for kim in xrange(cents):
            # 						dst = sqrt((pcoords[cents][0] - pcoords[kim][0])**2 + (pcoords[cents][1] - pcoords[kim][1])**2)
            # 						xl = -dst*tang+six+nxc
            # 						ixl = int(xl)
            # 						dxl = xl - ixl
            # 						qt += (1.0-dxl)*ctx[kim].get_value_at(ixl) + dxl*ctx[kim].get_value_at(ixl+1)
            # 						xl =  dst*tang+six+nxc
            # 						ixl = int(xl)
            # 						dxl = xl - ixl
            # 						qu += (1.0-dxl)*ctx[kim].get_value_at(ixl) + dxl*ctx[kim].get_value_at(ixl+1)
            # 					if( qt > qm ):
            # 						qm = qt
            # 						sib = six
            # 						bang = tang
            # 					if( qu > qm ):
            # 						qm = qu
            # 						sib = six
            # 						bang = -tang
            #if incline == 0:  print  "incline = 0  ",six,tang,qt,qu
            #print qm,six,sib,bang
            #print " got results   ",indcs[ifil][0], indcs[ifil][1], ifil,myid,qm,sib,tang,bang,len(ctx),Util.infomask(ctx[0], None, True)
            for im in range(indcs[ifil][0], indcs[ifil][1]):
                kim = im - indcs[ifil][0]
                dst = sqrt((pcoords[cents][0] - pcoords[kim][0])**2 +
                           (pcoords[cents][1] - pcoords[kim][1])**2)
                if (kim < cents): xl = -dst * bang + sib
                else: xl = dst * bang + sib
                shift_x[im] = xl

            # Average shift
            sx_sum += shift_x[indcs[ifil][0] + cents]

        # #print myid,sx_sum,total_nfils
        sx_sum = mpi.mpi_reduce(sx_sum, 1, mpi.MPI_FLOAT, mpi.MPI_SUM,
                                main_node, mpi.MPI_COMM_WORLD)
        if myid == main_node:
            sx_sum = float(sx_sum[0]) / total_nfils
            print_msg("Average shift  %6.2f\n" % (sx_sum))
        else:
            sx_sum = 0.0
        sx_sum = 0.0
        sx_sum = bcast_number_to_all(sx_sum, source_node=main_node)
        for im in range(ldata):
            shift_x[im] -= sx_sum
            #print  "   %3d  %6.3f"%(im,shift_x[im])
        #exit()

    # combine shifts found with the original parameters
    for im in range(ldata):
        t1 = Transform()
        ##import random
        ##shix=random.randint(-10, 10)
        ##t1.set_params({"type":"2D","tx":shix})
        t1.set_params({"type": "2D", "tx": shift_x[im]})
        # combine t0 and t1
        tt = t1 * init_params[im]
        data[im].set_attr("xform.align2d", tt)
    # write out headers and STOP, under MPI writing has to be done sequentially
    mpi.mpi_barrier(mpi.MPI_COMM_WORLD)
    par_str = ["xform.align2d", "ID"]
    if myid == main_node:
        from sp_utilities import file_type
        if (file_type(stack) == "bdb"):
            from sp_utilities import recv_attr_dict_bdb
            recv_attr_dict_bdb(main_node, stack, data, par_str, 0, ldata,
                               nproc)
        else:
            from sp_utilities import recv_attr_dict
            recv_attr_dict(main_node, stack, data, par_str, 0, ldata, nproc)
    else:
        send_attr_dict(main_node, data, par_str, 0, ldata)
    if myid == main_node: print_end_msg("helical-shiftali_MPI")
Пример #3
0
def generate_helimic(refvol,
                     outdir,
                     pixel,
                     CTF=False,
                     Cs=2.0,
                     voltage=200.0,
                     ampcont=10.0,
                     nonoise=False,
                     rand_seed=14567):

    from sp_utilities import model_blank, model_gauss, model_gauss_noise, pad, get_im
    from random import random
    from sp_projection import prgs, prep_vol
    from sp_filter import filt_gaussl, filt_ctf
    from EMAN2 import EMAN2Ctf

    if os.path.exists(outdir):
        ERROR(
            "Output directory exists, please change the name and restart the program"
        )
        return

    os.mkdir(outdir)
    seed(rand_seed)
    Util.set_randnum_seed(rand_seed)
    angles = []
    for i in range(3):
        angles.append([0.0 + 60.0 * i, 90.0 - i * 5, 0.0, 0.0, 0.0])

    nangle = len(angles)

    volfts = get_im(refvol)
    nx = volfts.get_xsize()
    ny = volfts.get_ysize()
    nz = volfts.get_zsize()
    volfts, kbx, kby, kbz = prep_vol(volfts)
    iprj = 0
    width = 500
    xstart = 0
    ystart = 0

    for idef in range(3, 6):
        mic = model_blank(2048, 2048)
        #defocus = idef*0.2
        defocus = idef * 0.6  ##@ming
        if CTF:
            #ctf = EMAN2Ctf()
            #ctf.from_dict( {"defocus":defocus, "cs":Cs, "voltage":voltage, "apix":pixel, "ampcont":ampcont, "bfactor":0.0} )
            from sp_utilities import generate_ctf
            ctf = generate_ctf(
                [defocus, 2, 200, 1.84, 0.0, ampcont, defocus * 0.2, 80]
            )  ##@ming   the range of astigmatism amplitude is between 10 percent and 22 percent. 20 percent is a good choice.
        i = idef - 4
        for k in range(1):
            psi = 90 + 10 * i
            proj = prgs(
                volfts, kbz,
                [angles[idef - 3][0], angles[idef - 3][1], psi, 0.0, 0.0], kbx,
                kby)
            proj = Util.window(proj, 320, nz)
            mic += pad(proj, 2048, 2048, 1, 0.0, 750 * i, 20 * i, 0)

        if not nonoise: mic += model_gauss_noise(30.0, 2048, 2048)
        if CTF:
            #apply CTF
            mic = filt_ctf(mic, ctf)

        if not nonoise:
            mic += filt_gaussl(model_gauss_noise(17.5, 2048, 2048), 0.3)

        mic.write_image("%s/mic%1d.hdf" % (outdir, idef - 3), 0)
Пример #4
0
def main():
    progname = os.path.basename(sys.argv[0])
    usage = progname + """ Input Output [options]
	
	Generate three micrographs, each micrograph contains one projection of a long filament.
	Input: Reference Volume, output directory 
	Output: Three micrographs stored in output directory		
				 
		sxhelical_demo.py tmp.hdf  mic --generate_micrograph --CTF --apix=1.84	
	
	Generate noisy cylinder ini.hdf with radius 35 pixels and box size 100 by 100 by 200
	
		sxhelical_demo.py ini.hdf --generate_noisycyl --boxsize="100,100,200" --rad=35
	
	Generate rectangular 2D mask mask2d.hdf with width 60 pixels and image size 200 by 200 pixels
	
		sxhelical_demo.py mask2d.hdf --generate_mask --masksize="200,200" --maskwidth=60
	
	Apply the centering parameters to bdb:adata, normalize using average and standard deviation outside the mask, and output the new images to bdb:data
		
		sxhelical_demo.py bdb:adata bdb:data mask2d.hdf --applyparams
	
	Generate run through example script for helicon
	
		sxhelical_demo.py --generate_script --filename=run --seg_ny=180 --ptcl_dist=15 --fract=0.35
	"""
    parser = OptionParser(usage, version=SPARXVERSION)

    # helicise the Atom coordinates

    # generate micrographs of helical filament
    parser.add_option(
        "--generate_micrograph",
        action="store_true",
        default=False,
        help=
        "Generate three micrographs where each micrograph contains one projection of a long filament. \n Input: Reference Volume, output directory \n Output: Three micrographs containing helical filament projections stored in output directory"
    )
    parser.add_option("--CTF",
                      action="store_true",
                      default=False,
                      help="Use CTF correction")
    parser.add_option("--apix",
                      type="float",
                      default=-1,
                      help="pixel size in Angstroms")
    parser.add_option(
        "--rand_seed",
        type="int",
        default=14567,
        help=
        "the seed used for generating random numbers (default 14567) for adding noise to the generated micrographs."
    )
    parser.add_option("--Cs",
                      type="float",
                      default=2.0,
                      help="Microscope Cs (spherical aberation)")
    parser.add_option("--voltage",
                      type="float",
                      default=200.0,
                      help="Microscope voltage in KV")
    parser.add_option("--ac",
                      type="float",
                      default=10.0,
                      help="Amplitude contrast (percentage, default=10)")
    parser.add_option("--nonoise",
                      action="store_true",
                      default=False,
                      help="Do not add noise to the micrograph.")

    # generate initial volume
    parser.add_option("--generate_noisycyl",
                      action="store_true",
                      default=False,
                      help="Generate initial volume of noisy cylinder.")
    parser.add_option(
        "--boxsize",
        type="string",
        default="100,100,200",
        help=
        "String containing x , y, z dimensions (separated by comma) in pixels")
    parser.add_option("--rad",
                      type="int",
                      default=35,
                      help="Radius of initial volume in pixels")

    # generate 2D mask
    parser.add_option("--generate_mask",
                      action="store_true",
                      default=False,
                      help="Generate 2D rectangular mask.")
    parser.add_option(
        "--masksize",
        type="string",
        default="200,200",
        help=
        "String containing x and y dimensions (separated by comma) in pixels")
    parser.add_option("--maskwidth",
                      type="int",
                      default=60,
                      help="Width of rectangular mask")

    # Apply 2D alignment parameters to input stack and output new images to output stack
    parser.add_option(
        "--applyparams",
        action="store_true",
        default=False,
        help=
        "Apply the centering parameters to input stack, normalize using average and standard deviation outside the mask, and output the new images to output stack"
    )

    # Generate run script
    parser.add_option("--generate_script",
                      action="store_true",
                      default=False,
                      help="Generate script for helicon run through example")
    parser.add_option("--filename",
                      type="string",
                      default="runhelicon",
                      help="Name of run script to generate")
    parser.add_option("--seg_ny",
                      type="int",
                      default=180,
                      help="y-dimension of segment used for refinement")
    parser.add_option(
        "--ptcl_dist",
        type="int",
        default=15,
        help=
        "Distance in pixels between adjacent segments windowed from same filament"
    )
    parser.add_option(
        "--fract",
        type="float",
        default=0.35,
        help="Fraction of the volume used for applying helical symmetry.")

    (options, args) = parser.parse_args()
    if len(args) > 3:
        sxprint("usage: " + usage)
        sxprint("Please run '" + progname + " -h' for detailed options")
        ERROR(
            "Invalid number of parameters. Please see usage information above."
        )
        return

    else:
        if options.generate_script:
            generate_runscript(options.filename, options.seg_ny,
                               options.ptcl_dist, options.fract)

        if options.generate_micrograph:
            if options.apix <= 0:
                ERROR("Please enter pixel size.")
                return

            generate_helimic(args[0], args[1], options.apix, options.CTF,
                             options.Cs, options.voltage, options.ac,
                             options.nonoise, options.rand_seed)

        if options.generate_noisycyl:

            from sp_utilities import model_cylinder, model_gauss_noise
            outvol = args[0]
            boxdims = options.boxsize.split(',')

            if len(boxdims) < 1 or len(boxdims) > 3:
                ERROR(
                    "Enter box size as string containing x , y, z dimensions (separated by comma) in pixels. E.g.: --boxsize=\'100,100,200\'"
                )
                return

            nx = int(boxdims[0])

            if len(boxdims) == 1:
                ny = nx
                nz = nx
            else:
                ny = int(boxdims[1])
                if len(boxdims) == 3:
                    nz = int(boxdims[2])

            (model_cylinder(options.rad, nx, ny, nz) *
             model_gauss_noise(1.0, nx, ny, nz)).write_image(outvol)

        if options.generate_mask:
            from sp_utilities import model_blank, pad
            outvol = args[0]
            maskdims = options.masksize.split(',')

            if len(maskdims) < 1 or len(maskdims) > 2:
                ERROR(
                    "Enter box size as string containing x , y dimensions (separated by comma) in pixels. E.g.: --boxsize=\'200,200\'"
                )
                return

            nx = int(maskdims[0])

            if len(maskdims) == 1:
                ny = nx
            else:
                ny = int(maskdims[1])

            mask = pad(model_blank(options.maskwidth, ny, 1, 1.0), nx, ny, 1,
                       0.0)
            mask.write_image(outvol)

        if options.applyparams:
            from sp_utilities import get_im, get_params2D, set_params2D
            from sp_fundamentals import cyclic_shift
            stack = args[0]
            newstack = args[1]
            mask = get_im(args[2])
            nima = EMUtil.get_image_count(stack)
            for im in range(nima):
                prj = get_im(stack, im)
                alpha, sx, sy, mirror, scale = get_params2D(prj)
                prj = cyclic_shift(prj, int(sx))
                set_params2D(prj, [0.0, 0., 0.0, 0, 1])
                stat = Util.infomask(prj, mask, False)
                prj = (prj - stat[0]) / stat[1]
                ctf_params = prj.get_attr("ctf")
                prj.set_attr('ctf_applied', 0)
                prj.write_image(newstack, im)
Пример #5
0
def prep_vol(vol, npad=2, interpolation_method=-1):
    """
		Name
			prep_vol - prepare the volume for calculation of gridding projections and generate the interpolants.
		Input
			vol: input volume for which projections will be calculated using prgs (interpolation_method=-1) or prgl (interpolation_method>0)
			interpolation_method = -1  gridding
			interpolation_method =  0  NN
			interpolation_method =  1  trilinear
		Output
			volft: volume prepared for gridding projections using prgs
			kb: interpolants (tabulated Kaiser-Bessel function) when the volume is cubic.
			kbx,kby: interpolants along x, y and z direction (tabulated Kaiser-Bessel function) when the volume is rectangular 
	"""
    # prepare the volume
    Mx = vol.get_xsize()
    My = vol.get_ysize()
    Mz = vol.get_zsize()
    #  gridding
    if interpolation_method == -1:
        K = 6
        alpha = 1.75
        assert npad == 2
        if (Mx == Mz & My == Mz):
            M = vol.get_xsize()
            # padd two times
            N = M * npad
            # support of the window
            kb = Util.KaiserBessel(alpha, K, M / 2, K / (2. * N), N)
            volft = vol.copy()
            volft.divkbsinh(kb)
            volft = volft.norm_pad(False, npad)
            volft.do_fft_inplace()
            volft.center_origin_fft()
            volft.fft_shuffle()
            return volft, kb
        else:
            Nx = Mx * npad
            Ny = My * npad
            Nz = Mz * npad
            # support of the window
            kbx = Util.KaiserBessel(alpha, K, Mx / 2, K / (2. * Nx), Nx)
            kby = Util.KaiserBessel(alpha, K, My / 2, K / (2. * Ny), Ny)
            kbz = Util.KaiserBessel(alpha, K, Mz / 2, K / (2. * Nz), Nz)
            volft = vol.copy()
            volft.divkbsinh_rect(kbx, kby, kbz)
            volft = volft.norm_pad(False, npad)
            volft.do_fft_inplace()
            volft.center_origin_fft()
            volft.fft_shuffle()
            return volft, kbx, kby, kbz
    else:
        # NN and trilinear
        assert interpolation_method >= 0
        from sp_utilities import pad
        volft = pad(vol, Mx * npad, My * npad, My * npad, 0.0)
        volft.set_attr("npad", npad)
        volft.div_sinc(interpolation_method)
        volft = volft.norm_pad(False, 1)
        volft.do_fft_inplace()
        volft.center_origin_fft()
        volft.fft_shuffle()
        volft.set_attr("npad", npad)
        return volft
Пример #6
0
def recons3d_4nn_ctf_MPI(
    myid,
    prjlist,
    snr=1.0,
    sign=1,
    symmetry="c1",
    finfo=None,
    npad=2,
    xysize=-1,
    zsize=-1,
    mpi_comm=None,
    smearstep=0.0,
):
    """
		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
			stack: name of the stack file containing projection data, projections have to be squares
			list_proj: list of projections to be included in the reconstruction or image iterator
			snr: Signal-to-Noise Ratio of the data
			sign: sign of the CTF
			symmetry: point-group symmetry to be enforced, each projection will enter the reconstruction in all symmetry-related directions.
	"""

    if mpi_comm == None:
        mpi_comm = mpi.MPI_COMM_WORLD

    if type(prjlist) == list:
        prjlist = sp_utilities.iterImagesList(prjlist)
    if not prjlist.goToNext():
        sp_global_def.ERROR("empty input list", "recons3d_4nn_ctf_MPI", 1)
    imgsize = prjlist.image().get_xsize()
    if prjlist.image().get_ysize() != imgsize:
        imgsize = max(imgsize, prjlist.image().get_ysize())
        dopad = True
    else:
        dopad = False
    prjlist.goToPrev()

    fftvol = EMAN2_cppwrap.EMData()

    if smearstep > 0.0:
        # if myid == 0:  print "  Setting smear in prepare_recons_ctf"
        ns = 1
        smear = []
        for j in range(-ns, ns + 1):
            if j != 0:
                for i in range(-ns, ns + 1):
                    for k in range(-ns, ns + 1):
                        smear += [
                            i * smearstep, j * smearstep, k * smearstep, 1.0
                        ]
        # Deal with theta = 0.0 cases
        prj = []
        for i in range(-ns, ns + 1):
            for k in range(-ns, ns + 1):
                prj.append(i + k)
        for i in range(-2 * ns, 2 * ns + 1, 1):
            smear += [i * smearstep, 0.0, 0.0, float(prj.count(i))]
        # if myid == 0:  print "  Smear  ",smear
        fftvol.set_attr("smear", smear)

    weight = EMAN2_cppwrap.EMData()
    if xysize == -1 and zsize == -1:
        params = {
            "size": imgsize,
            "npad": npad,
            "snr": snr,
            "sign": sign,
            "symmetry": symmetry,
            "fftvol": fftvol,
            "weight": weight,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf", params)
    else:
        if xysize != -1 and zsize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = old_div(float(zsize), imgsize)
        elif xysize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = 1.0
        else:
            rx = 1.0
            ry = 1.0
            rz = old_div(float(zsize), imgsize)
        #  There is an error here with sizeprojection  PAP 10/22/2014
        params = {
            "size": sizeprojection,
            "npad": npad,
            "snr": snr,
            "sign": sign,
            "symmetry": symmetry,
            "fftvol": fftvol,
            "weight": weight,
            "xratio": rx,
            "yratio": ry,
            "zratio": rz,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params)
    r.setup()

    # if not (finfo is None):
    nimg = 0
    while prjlist.goToNext():
        prj = prjlist.image()
        if dopad:
            prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference")
        # if params:
        insert_slices(r, prj)
        if not (finfo is None):
            nimg += 1
            finfo.write(" %4d inserted\n" % (nimg))
            finfo.flush()
    del sp_utilities.pad
    if not (finfo is None):
        finfo.write("begin reduce\n")
        finfo.flush()

    sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm)
    sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm)

    if not (finfo is None):
        finfo.write("after reduce\n")
        finfo.flush()

    if myid == 0:
        dummy = r.finish(True)
    else:
        if xysize == -1 and zsize == -1:
            fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize)
        else:
            if zsize == -1:
                fftvol = sp_utilities.model_blank(xysize, xysize, imgsize)
            elif xysize == -1:
                fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize)
            else:
                fftvol = sp_utilities.model_blank(xysize, xysize, zsize)
    return fftvol
Пример #7
0
def recons3d_4nn_ctf(
    stack_name,
    list_proj=[],
    snr=1.0,
    sign=1,
    symmetry="c1",
    verbose=0,
    npad=2,
    xysize=-1,
    zsize=-1,
):
    """Perform a 3-D reconstruction using Pawel's FFT Back Projection algoritm.

	   Input:
	    stack_name - name of the stack file on a disk,
	                 each image has to have the following attributes set:
			 psi, theta, phi, sx, sy, defocus,
	    list_proj - list of images from stack_name to be included in the reconstruction
	    symmetry	 -- Point group of the target molecule (defaults to "C1")

	   Return:  3d reconstructed volume image

	   Usage:

	     anglelist = getAngles("myangles.txt") # not yet written
	     vol = do_reconstruction(filepattern, start, end, anglelist, symmetry)
	"""

    # read first image to determine the size to use
    if list_proj == []:
        if type(stack_name) == bytes:
            nima = EMAN2_cppwrap.EMUtil.get_image_count(stack_name)
        else:
            nima = len(stack_name)
        list_proj = list(range(nima))
    # read first image to determine the size to use
    if type(stack_name) == bytes:
        proj = EMAN2_cppwrap.EMData()
        proj.read_image(stack_name, list_proj[0])
    else:
        proj = stack_name[list_proj[0]].copy()

    # convert angles to transform (rotation) objects
    # horatio active_refactoring Jy51i1EwmLD4tWZ9_00000_1
    # active = proj.get_attr_default('active', 1)
    size = proj.get_xsize()
    if proj.get_ysize() != size:
        size = max(size, proj.get_ysize())
        dopad = True
    else:
        dopad = False

    # reconstructor
    fftvol = EMAN2_cppwrap.EMData()
    weight = EMAN2_cppwrap.EMData()
    params = {
        "npad": npad,
        "symmetry": symmetry,
        "snr": snr,
        "sign": sign,
        "fftvol": fftvol,
        "weight": weight,
    }
    if xysize == -1 and zsize == -1:
        params["size"] = size
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf", params)
    else:
        if xysize != -1 and zsize != -1:
            rx = old_div(float(xysize), size)
            ry = old_div(float(xysize), size)
            rz = old_div(float(zsize), size)
        elif xysize != -1:
            rx = old_div(float(xysize), size)
            ry = old_div(float(xysize), size)
            rz = 1.0
        else:
            rx = 1.0
            ry = 1.0
            rz = old_div(float(zsize), size)

        params["sizeprojection"] = size
        params["xratio"] = rx
        params["yratio"] = ry
        params["zratio"] = rz
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params)
    r.setup()

    if type(stack_name) == bytes:
        for i in range(len(list_proj)):
            proj.read_image(stack_name, list_proj[i])
            if dopad:
                proj = sp_utilities.pad(proj, size, size, 1, "circumference")
            insert_slices(r, proj)
    else:
        for i in range(len(list_proj)):
            insert_slices(r, stack_name[list_proj[i]])
    dummy = r.finish(True)
    return fftvol
Пример #8
0
def recons3d_4nnw_MPI(
    myid,
    prjlist,
    bckgdata,
    snr=1.0,
    sign=1,
    symmetry="c1",
    finfo=None,
    npad=2,
    xysize=-1,
    zsize=-1,
    mpi_comm=None,
    smearstep=0.0,
    fsc=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
			stack: name of the stack file containing projection data, projections have to be squares
			prjlist: list of projections to be included in the reconstruction or image iterator
			bckgdata = [get_im("tsd.hdf"),read_text_file("data_stamp.txt")]
			snr: Signal-to-Noise Ratio of the data
			sign: sign of the CTF
			symmetry: point-group symmetry to be enforced, each projection will enter the reconstruction in all symmetry-related directions.
	"""
    pass  # IMPORTIMPORTIMPORT from sp_utilities  import reduce_EMData_to_root, pad
    pass  # IMPORTIMPORTIMPORT from EMAN2      import Reconstructors
    pass  # IMPORTIMPORTIMPORT from sp_utilities  import iterImagesList, set_params_proj, model_blank
    pass  # IMPORTIMPORTIMPORT from mpi        import MPI_COMM_WORLD
    pass  # IMPORTIMPORTIMPORT import types

    if mpi_comm == None:
        mpi_comm = mpi.MPI_COMM_WORLD

    if type(prjlist) == list:
        prjlist = sp_utilities.iterImagesList(prjlist)
    if not prjlist.goToNext():
        sp_global_def.ERROR("empty input list", "recons3d_4nnw_MPI", 1)
    imgsize = prjlist.image().get_xsize()
    if prjlist.image().get_ysize() != imgsize:
        imgsize = max(imgsize, prjlist.image().get_ysize())
        dopad = True
    else:
        dopad = False
    prjlist.goToPrev()

    #  Do the FSC shtick.
    bnx = old_div(imgsize * npad, 2) + 1
    if fsc:
        pass  # IMPORTIMPORTIMPORT from math import sqrt
        pass  # IMPORTIMPORTIMPORT from sp_utilities import reshape_1d
        t = [0.0] * len(fsc)
        for i in range(len(fsc)):
            t[i] = min(max(fsc[i], 0.0), 0.999)
        t = sp_utilities.reshape_1d(t, len(t), npad * len(t))
        refvol = sp_utilities.model_blank(bnx, 1, 1, 0.0)
        for i in range(len(fsc)):
            refvol.set_value_at(i, t[i])
    else:
        refvol = sp_utilities.model_blank(bnx, 1, 1, 1.0)
    refvol.set_attr("fudge", 1.0)

    fftvol = EMAN2_cppwrap.EMData()
    weight = EMAN2_cppwrap.EMData()

    if smearstep > 0.0:
        # if myid == 0:  print "  Setting smear in prepare_recons_ctf"
        ns = 1
        smear = []
        for j in range(-ns, ns + 1):
            if j != 0:
                for i in range(-ns, ns + 1):
                    for k in range(-ns, ns + 1):
                        smear += [
                            i * smearstep, j * smearstep, k * smearstep, 1.0
                        ]
        # Deal with theta = 0.0 cases
        prj = []
        for i in range(-ns, ns + 1):
            for k in range(-ns, ns + 1):
                prj.append(i + k)
        for i in range(-2 * ns, 2 * ns + 1, 1):
            smear += [i * smearstep, 0.0, 0.0, float(prj.count(i))]
        # if myid == 0:  print "  Smear  ",smear
        fftvol.set_attr("smear", smear)

    if xysize == -1 and zsize == -1:
        params = {
            "size": imgsize,
            "npad": npad,
            "snr": snr,
            "sign": sign,
            "symmetry": symmetry,
            "refvol": refvol,
            "fftvol": fftvol,
            "weight": weight,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctfw", params)
    else:
        if xysize != -1 and zsize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = old_div(float(zsize), imgsize)
        elif xysize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = 1.0
        else:
            rx = 1.0
            ry = 1.0
            rz = old_div(float(zsize), imgsize)
        #  There is an error here with sizeprojection  PAP 10/22/2014
        params = {
            "size": sizeprojection,
            "npad": npad,
            "snr": snr,
            "sign": sign,
            "symmetry": symmetry,
            "fftvol": fftvol,
            "weight": weight,
            "xratio": rx,
            "yratio": ry,
            "zratio": rz,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4_ctf_rect", params)
    r.setup()

    # from utilities import model_blank, get_im, read_text_file
    # bckgdata = [get_im("tsd.hdf"),read_text_file("data_stamp.txt")]

    nnx = bckgdata[0].get_xsize()
    nny = bckgdata[0].get_ysize()
    bckgnoise = []
    for i in range(nny):
        prj = sp_utilities.model_blank(nnx)
        for k in range(nnx):
            prj[k] = bckgdata[0].get_value_at(k, i)
        bckgnoise.append(prj)

    datastamp = bckgdata[1]
    if not (finfo is None):
        nimg = 0
    while prjlist.goToNext():
        prj = prjlist.image()
        try:
            stmp = old_div(nnx, 0)
            stmp = prj.get_attr("ptcl_source_image")
        except:
            try:
                stmp = prj.get_attr("ctf")
                stmp = round(stmp.defocus, 4)
            except:
                sp_global_def.ERROR(
                    "Either ptcl_source_image or ctf has to be present in the header.",
                    "recons3d_4nnw_MPI",
                    1,
                    myid,
                )
        try:
            indx = datastamp.index(stmp)
        except:
            sp_global_def.ERROR("Problem with indexing ptcl_source_image.",
                                "recons3d_4nnw_MPI", 1, myid)

        if dopad:
            prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference")

        prj.set_attr("bckgnoise", bckgnoise[indx])
        insert_slices(r, prj)
        if not (finfo is None):
            nimg += 1
            finfo.write(" %4d inserted\n" % (nimg))
            finfo.flush()
    del sp_utilities.pad
    if not (finfo is None):
        finfo.write("begin reduce\n")
        finfo.flush()

    sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm)
    sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm)

    if not (finfo is None):
        finfo.write("after reduce\n")
        finfo.flush()

    if myid == 0:
        dummy = r.finish(True)
    else:
        pass  # IMPORTIMPORTIMPORT from sp_utilities import model_blank
        if xysize == -1 and zsize == -1:
            fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize)
        else:
            if zsize == -1:
                fftvol = sp_utilities.model_blank(xysize, xysize, imgsize)
            elif xysize == -1:
                fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize)
            else:
                fftvol = sp_utilities.model_blank(xysize, xysize, zsize)
    return fftvol
Пример #9
0
def recons3d_4nn_MPI(
    myid,
    prjlist,
    symmetry="c1",
    finfo=None,
    snr=1.0,
    npad=2,
    xysize=-1,
    zsize=-1,
    mpi_comm=None,
):
    if mpi_comm == None:
        mpi_comm = mpi.MPI_COMM_WORLD

    if type(prjlist) == list:
        prjlist = sp_utilities.iterImagesList(prjlist)

    if not prjlist.goToNext():
        sp_global_def.ERROR("empty input list", "recons3d_4nn_MPI", 1)

    imgsize = prjlist.image().get_xsize()
    if prjlist.image().get_ysize() != imgsize:
        imgsize = max(imgsize, prjlist.image().get_ysize())
        dopad = True
    else:
        dopad = False
    prjlist.goToPrev()

    fftvol = EMAN2_cppwrap.EMData()
    weight = EMAN2_cppwrap.EMData()
    if xysize == -1 and zsize == -1:
        params = {
            "size": imgsize,
            "npad": npad,
            "symmetry": symmetry,
            "fftvol": fftvol,
            "weight": weight,
            "snr": snr,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4", params)
    else:
        if xysize != -1 and zsize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = old_div(float(zsize), imgsize)
        elif xysize != -1:
            rx = old_div(float(xysize), imgsize)
            ry = old_div(float(xysize), imgsize)
            rz = 1.0
        else:
            rx = 1.0
            ry = 1.0
            rz = old_div(float(zsize), imgsize)
        params = {
            "sizeprojection": imgsize,
            "npad": npad,
            "symmetry": symmetry,
            "fftvol": fftvol,
            "weight": weight,
            "xratio": rx,
            "yratio": ry,
            "zratio": rz,
        }
        r = EMAN2_cppwrap.Reconstructors.get("nn4_rect", params)
    r.setup()

    if not (finfo is None):
        nimg = 0
    while prjlist.goToNext():
        prj = prjlist.image()
        if dopad:
            prj = sp_utilities.pad(prj, imgsize, imgsize, 1, "circumference")
        insert_slices(r, prj)
        if not (finfo is None):
            nimg += 1
            finfo.write("Image %4d inserted.\n" % (nimg))
            finfo.flush()

    if not (finfo is None):
        finfo.write("Begin reducing ...\n")
        finfo.flush()

    sp_utilities.reduce_EMData_to_root(fftvol, myid, comm=mpi_comm)
    sp_utilities.reduce_EMData_to_root(weight, myid, comm=mpi_comm)

    if myid == 0:
        dummy = r.finish(True)
    else:
        if xysize == -1 and zsize == -1:
            fftvol = sp_utilities.model_blank(imgsize, imgsize, imgsize)
        else:
            if zsize == -1:
                fftvol = sp_utilities.model_blank(xysize, xysize, imgsize)
            elif xysize == -1:
                fftvol = sp_utilities.model_blank(imgsize, imgsize, zsize)
            else:
                fftvol = sp_utilities.model_blank(xysize, xysize, zsize)
    return fftvol