Esempio n. 1
0
def do_ali(ptcls, projs):
	boxsize=ptcls[0]["nx"]
	bslst=[]
	quals=[]
	bss=.0
	pjs=[(p, None) for p in projs]
	for i in range(len(ptcls)):
		#sim=cmponetomany(pjs,ptcls[i],align=("rotate_translate_flip",{"maxshift":boxsize/5}),alicmp=("ccc",{}),ralign=("refine",{}),cmp=("frc",{"minres":80,"maxres":20}))
		sim=cmponetomany(pjs,ptcls[i],align=("rotate_translate_tree",{"maxshift":old_div(boxsize,5), "maxres":20}),alicmp=("ccc",{}),ralign=("refine",{}),cmp=("frc",{"minres":160,"maxres":20}))
		bs=min(sim)
		
		bss+=bs[0]
		bslst.append((bs[0],i))
		n=sim.index(bs)
#		 print n
		ptcls[i]["match_n"]=n
		ptcls[i]["match_qual"]=-bs[0]
		ptcls[i]["xform.projection"]=projs[n]["xform.projection"]# best orientation set in the original particle

	bslst.sort()# sorted list of all particle qualities
	bslst.reverse()
	aptcls=[]
	#for i in range(len(ptcls)*3/4):# We used to include 3/4 of the particles
	for i in range(len(ptcls)):
		n=ptcls[bslst[i][1]]["match_n"]
		quals.append(ptcls[bslst[i][1]]["match_qual"])
		aptcls.append(ptcls[bslst[i][1]].align("rotate_translate_tree",projs[n],{"maxshift":old_div(boxsize,5), "maxres":20},"frc",{}))
		aptcls[-1].process_inplace("normalize.toimage",{"to":projs[n]})
		#aptcls[-1].process_inplace("normalize")
		aptcls[-1].add(-aptcls[-1]["mean"])
	return aptcls
Esempio n. 2
0
def do_ali_fullcov(ptcls, projs):
    boxsize = ptcls[0]["nx"]
    quals = []
    bss = .0
    pts = [(p, None) for p in ptcls]
    for i in range(len(projs)):
        #sim=cmponetomany(pjs,ptcls[i],align=("rotate_translate_flip",{"maxshift":boxsize/5}),alicmp=("ccc",{}),ralign=("refine",{}),cmp=("frc",{"minres":80,"maxres":20}))
        sim = cmponetomany(pts,
                           projs[i],
                           align=("rotate_translate_tree", {
                               "maxshift": boxsize / 5,
                               "maxres": 20
                           }),
                           alicmp=("ccc", {}),
                           ralign=("refine", {}),
                           cmp=("frc", {
                               "minres": 80,
                               "maxres": 20
                           }))
        bs = min(sim)

        bss += bs[0]
        n = sim.index(bs)
        #		 print n
        projs[i]["match_n"] = n
        projs[i]["match_qual"] = -bs[0]

    aptcls = []
    #for i in range(len(ptcls)*3/4):# We used to include 3/4 of the particles
    for i in range(len(projs)):
        n = projs[i]["match_n"]
        quals.append(projs[i]["match_qual"])
        aptcls.append(ptcls[n].align("rotate_translate_tree", projs[i], {
            "maxshift": boxsize / 5,
            "maxres": 20
        }, "frc", {}))
        aptcls[-1].process_inplace("normalize.toimage", {"to": projs[i]})
        #aptcls[-1].process_inplace("normalize")
        aptcls[-1]["match_n"] = i
        aptcls[-1].add(-aptcls[-1]["mean"])
        aptcls[-1]["xform.projection"] = projs[i][
            "xform.projection"]  # best orientation set in the original
    return aptcls
Esempio n. 3
0
def main():
    progname = os.path.basename(sys.argv[0])
    usage = """prog [options] <c input> <r input> <output>
	Computes a similarity matrix between c-input (col - projections) and r-input (row - particles) stacks of 2-D images. Images may
	optionally be aligned before comparison. Output is a matrix stored as an image with similarity value
	pairs. When used for classification, c input is the references and r input are the particles. More information on
	the output file can be found in the Wiki."""

    parser = EMArgumentParser(usage=usage, version=EMANVERSION)

    #parser.add_argument("--apix", "-A", type=float, help="A/voxel", default=1.0)
    #parser.add_argument("--box", "-B", type=str, help="Box size in pixels, <xyz> or <x>,<y>,<z>")
    #parser.add_argument("--het", action="store_true", help="Include HET atoms in the map", default=False)
    parser.add_argument(
        "--align",
        type=str,
        help="The name of an 'aligner' to use prior to comparing the images",
        default=None)
    parser.add_argument(
        "--aligncmp",
        type=str,
        help="Name of the aligner along with its construction arguments",
        default="dot")
    parser.add_argument(
        "--ralign",
        type=str,
        help=
        "The name and parameters of the second stage aligner which refines the results of the first alignment",
        default=None)
    parser.add_argument(
        "--raligncmp",
        type=str,
        help=
        "The name and parameters of the comparitor used by the second stage aligner. Default is dot.",
        default="dot")
    parser.add_argument(
        "--cmp",
        type=str,
        help="The name of a 'cmp' to be used in comparing the aligned images",
        default="dot:normalize=1")
    parser.add_argument(
        "--prefilt",
        action="store_true",
        help=
        "Filter each reference (c) to match the power spectrum of each particle (r) before alignment and comparison",
        default=False)
    parser.add_argument("--prectf",
                        action="store_true",
                        help="Apply CTF to each projection before comparison",
                        default=False)
    parser.add_argument(
        "--mask",
        type=str,
        help=
        "File containing a single mask image to apply after alignment, but before similarity comparison",
        default=None)
    parser.add_argument(
        "--colmasks",
        type=str,
        help=
        "File containing one mask for each column (projection) image, to be used when refining row (particle) image alignments.",
        default=None)
    parser.add_argument(
        "--range",
        type=str,
        help=
        "Range of images to process (c0,r0,c1,r1) c0,r0 inclusive c1,r1 exclusive",
        default=None)
    parser.add_argument(
        "--saveali",
        action="store_true",
        help=
        "Save alignment values, output is 5, c x r images instead of 1. Images are (score,dx,dy,da,flip). ",
        default=False)
    parser.add_argument(
        "--verbose",
        "-v",
        dest="verbose",
        action="store",
        metavar="n",
        type=int,
        default=0,
        help=
        "verbose level [0-9], higher number means higher level of verboseness")
    #	parser.add_argument("--lowmem",action="store_true",help="prevent the bulk reading of the reference images - this will save memory but potentially increase CPU time",default=False)
    parser.add_argument(
        "--init",
        action="store_true",
        help=
        "Initialize the output matrix file before performing 'range' calculations",
        default=False)
    parser.add_argument(
        "--fillzero",
        action="store_true",
        help=
        "Checks the existing output file, and fills only matrix elements which are exactly zero.",
        default=False)
    #	parser.add_argument("--force", "-f",dest="force",default=False, action="store_true",help="Force overwrite the output file if it exists")
    parser.add_argument(
        "--exclude",
        type=str,
        default=None,
        help=
        "The named file should contain a set of integers, each representing an image from the input file to exclude. Matrix elements will still be created, but will be zeroed."
    )
    parser.add_argument(
        "--shrink",
        type=float,
        default=None,
        help=
        "Optionally shrink the input particles by an integer amount prior to computing similarity scores. This will speed the process up."
    )
    parser.add_argument(
        "--nofilecheck",
        action="store_true",
        help=
        "Turns file checking off in the check functionality - used by e2refine.py.",
        default=False)
    parser.add_argument("--check",
                        "-c",
                        action="store_true",
                        help="Performs a command line argument check only.",
                        default=False)
    parser.add_argument(
        "--ppid",
        type=int,
        help="Set the PID of the parent process, used for cross platform PPID",
        default=-1)
    parser.add_argument("--parallel",
                        type=str,
                        help="Parallelism string",
                        default=None)

    (options, args) = parser.parse_args()

    if len(args) < 3: parser.error("Input and output files required")

    if (options.check):
        options.verbose = 1  # turn verbose on if the user is only checking...

    options.reffile = args[0]
    options.datafile = args[1]
    options.outfile = args[2]

    error = check(options, True)

    if options.verbose > 0:
        if (error):
            print("e2simmx.py command line arguments test.... FAILED")
        else:
            print("e2simmx.py command line arguments test.... PASSED")

    if error: exit(1)
    if options.check: exit(0)

    if options.prectf:
        if options.prefilt:
            print("ERROR: may only specify one of prefilt or prectf")
            sys.exit(1)
        options.prefilt = 2
    elif options.prefilt:
        options.prefilt = 1
    else:
        options.prefilt = 0

    E2n = E2init(sys.argv, options.ppid)

    if options.parallel:
        parsimmx = EMParallelSimMX(options, args, E2n)
        parsimmx.execute()
        # Region writing is used extensively, so we do the compression as a post-processing operation
        #		compress_hdf(options.outfile,0)
        E2end(E2n)
        sys.exit(0)

    # just remove the file - if the user didn't specify force then the error should have been found in the check function
    if file_exists(options.outfile):
        #		if (options.force):
        remove_file(options.outfile)

    options.align = parsemodopt(options.align)
    options.aligncmp = parsemodopt(options.aligncmp)
    options.ralign = parsemodopt(options.ralign)
    options.raligncmp = parsemodopt(options.raligncmp)
    options.cmp = parsemodopt(options.cmp)

    if options.exclude:
        try:
            excl = open(options.exclude, "r").readlines()
            excl = [int(i) for i in excl]
            excl = set(excl)
        except:
            print("Warning: exclude file failed")  # it's ok if this fails

    clen = EMUtil.get_image_count(args[0])
    rlen = EMUtil.get_image_count(args[1])

    if options.init:
        a = EMData()
        a.set_size(clen, rlen, 1)
        a.to_zero()
        a.write_image(args[2], 0)
        if options.saveali:
            for i in range(1, 6):
                a.write_image(args[2], i)
        E2end(E2n)
        sys.exit(0)

    # Compute range in c and r
    if options.range:
        crange = options.range.split(",")[0:4:2]
        rrange = options.range.split(",")[1:4:2]
        crange[0] = int(crange[0])
        crange[1] = int(crange[1])
        rrange[0] = int(rrange[0])
        rrange[1] = int(rrange[1])
    else:
        crange = [0, clen]
        rrange = [0, rlen]

    # initialize output array
    mxout = [EMData()]
    mxout[0].set_attr(PROJ_FILE_ATTR, args[0])
    mxout[0].set_attr(PART_FILE_ATTR, args[1])
    mxout[0].set_size(crange[1] - crange[0], rrange[1] - rrange[0], 1)
    mxout[0].to_zero()
    if options.saveali:
        mxout.append(mxout[0].copy())  # dx
        mxout.append(mxout[0].copy())  # dy
        mxout.append(mxout[0].copy())  # alpha (angle)
        mxout.append(mxout[0].copy())  # mirror
        mxout.append(mxout[0].copy())  # scale
    if options.verbose > 0: print("Computing Similarities")

    # Read all c images, then read and compare one r image at a time
    cimgs = EMData.read_images(args[0], list(range(*crange)))
    if options.colmasks:
        cmimgs = EMData.read_images(options.colmasks, list(range(*crange)))
        cimgs = list(zip(cimgs, cmimgs))
    else:
        for i in range(len(cimgs)):
            cimgs[i] = (cimgs[i], None)

    if options.shrink != None:  # the check function guarantees that shrink is an integer greater than 1
        #d = [ image.process("math.meanshrink",{"n":options.shrink}) for image in cimgs]
        #cimgs = d
        for image, imagem in cimgs:
            image.process_inplace("math.fft.resample", {"n": options.shrink})
            if imagem != None:
                imagem.process_inplace("math.fft.resample",
                                       {"n": options.shrink})

#	if (options.lowmem):
    rimg = EMData()
    #	else:
    #		rimages = EMData.read_images(args[1],range(*rrange))
    #		if options.shrink != None: # the check function guarantees that shrink is an integer greater than 1
    #			#d = [ image.process("math.meanshrink",{"n":options.shrink}) for image in rimages]
    #			#rimages = d
    #			# I chose this way in the end for memory efficiency. There's probably a better way to do it
    #			for image in rimages:
    #				image.process_inplace("math.meanshrink",{"n":options.shrink})

    #dimages =  EMData.read_images(args[1],range(*rrange))
    #d = [ image.process_inplace("math.meanshrink",{"n":options.shrink}) for image in dimages]

    if options.mask == None: mask = None
    else: mask = EMData(options.mask, 0)

    for r in range(*rrange):
        if options.exclude and r in excl: continue

        if options.verbose > 0:
            print("%d/%d\r" % (r, rrange[1]), end=' ')
            sys.stdout.flush()

        # With the fillzero option, we only compute values where there is a zero in the existing matrix
        if options.fillzero:
            ss = EMData()
            ss.read_image(args[2], 0, False,
                          Region(crange[0], r, crange[1] - crange[0] + 1, 1))
            subset = [i for i in range(ss["nx"]) if ss[i, 0] == 0]
        else:
            subset = None

        #		if ( options.lowmem ):
        rimg.read_image(args[1], r)
        if options.shrink != None:  # the check function guarantees that shrink is an integer greater than
            rimg.process_inplace("math.fft.resample", {"n": options.shrink})
            if mask != None:
                mask.process_inplace("math.fft.resample",
                                     {"n": options.shrink})

        if mask != None: rimg.mult(mask)  # cimgs are masked in cmponetomany
        #		else:
        #			rimg = rimages[r]

        E2progress(E2n, old_div(float(r - rrange[0]), (rrange[1] - rrange[0])))
        shrink = options.shrink
        if options.verbose > 1: print("%d. " % r, end=' ')
        row = cmponetomany(cimgs, rimg, options.align, options.aligncmp,
                           options.cmp, options.ralign, options.raligncmp,
                           options.shrink, mask, subset, options.prefilt,
                           options.verbose)
        for c, v in enumerate(row):
            if v == None: mxout[0].set_value_at(c, r, 0, -1.0e38)
            else: mxout[0].set_value_at(c, r, 0, v[0])

        # This is to catch any NaNs - yes this is a problem but this is a temporary work around
        mxout[0].process_inplace("math.finite", {"to": 1e24})
        if options.saveali:
            for c, v in enumerate(row):
                if v == None:
                    mxout[1].set_value_at(c, r, 0, 0)
                    mxout[2].set_value_at(c, r, 0, 0)
                    mxout[3].set_value_at(c, r, 0, 0)
                    mxout[4].set_value_at(c, r, 0, 0)
                    mxout[5].set_value_at(c, r, 0, 0)
                else:
                    mxout[1].set_value_at(c, r, 0, v[1])
                    mxout[2].set_value_at(c, r, 0, v[2])
                    mxout[3].set_value_at(c, r, 0, v[3])
                    mxout[4].set_value_at(c, r, 0, v[4])
                    mxout[5].set_value_at(c, r, 0, v[5])

    if options.verbose > 0: print("\nSimilarity computation complete")

    # write the results into the full-sized matrix
    if crange == [0, clen] and rrange == [0, rlen]:
        for i, j in enumerate(mxout):
            j.write_image(args[2], i)


#		compress_hdf(args[2],0)
    else:
        for i, j in enumerate(mxout):
            j.write_image(
                args[2], i, IMAGE_UNKNOWN, 0,
                Region(crange[0], rrange[0], 0, crange[1] - crange[0],
                       rrange[1] - rrange[0], 1))

    E2end(E2n)