def softMask(infile, outfile=None, mask=0.8, falloff=0.1):
        """
        applies a soft mask to images in a stack
        """
        fname = pymagic.fileFilter(infile)
        if outfile:
                outname = pymagic.fileFilter(outname)
        else:
                outname = fname+"_mask"

        ## output temporary file to same directory as original file
        outfile = fname+"_mask"
        myIm = pymagic.ImagicSession("stand/arithm.e")
        myIm.toImagicQuiet("%s"%fname) # input
        myIm.toImagicQuiet("%s"%outname) # output
        myIm.toImagicQuiet("SOFT") # soft mask
        myIm.toImagicQuiet("%.2f"%mask) # mask as a fraction
        myIm.toImagicQuiet("%.2f"%falloff) # falloff as a fraction
        myIm.close()

        ### check that it ran correctly
        if not os.path.exists(outname+".hed"):
                apDisplay.printError("arithm.e did not execute properly")
                return None

        return outname
def normalize(infile,
              outfile=None,
              sigma=10.0,
              path=os.path.abspath('.'),
              keepfiles=False):
    """
	normalize images in a stack
	"""

    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outfile)
    else:
        outname = fname + "_norm"

    myIm = pymagic.ImagicSession("stand/pretreat.e")
    imagicv = myIm.version()
    myIm.toImagicQuiet(fname)  # input
    myIm.toImagicQuiet(outname)  # output
    myIm.toImagicQuiet("NORM_VARIANCE")  # mode
    myIm.toImagicQuiet("WHOLE")  # mask to be used
    myIm.toImagicQuiet("%.2f" % sigma)  # desired sigma
    myIm.toImagicQuiet("NO")  # remove dust outliers
    if imagicv >= 110119:
        myIm.toImagicQuiet("NO")
    myIm.close()

    # check proper execution
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("normalization did not execute properly")

    return outname
def normalize(infile, outfile=None, sigma=10.0, path=os.path.abspath('.'), keepfiles=False):
        """
        normalize images in a stack
        """

        fname = pymagic.fileFilter(infile)
        if outfile:
                outname = pymagic.fileFilter(outfile)
        else:
                outname=fname+"_norm"

        myIm = pymagic.ImagicSession("stand/pretreat.e")
        imagicv = myIm.version()
        myIm.toImagicQuiet(fname) # input
        myIm.toImagicQuiet(outname) # output
        myIm.toImagicQuiet("NORM_VARIANCE") # mode
        myIm.toImagicQuiet("WHOLE") # mask to be used
        myIm.toImagicQuiet("%.2f"%sigma) # desired sigma
        myIm.toImagicQuiet("NO") # remove dust outliers
        if imagicv >= 110119:
                myIm.toImagicQuiet("NO")
        myIm.close()

        # check proper execution
        if not os.path.exists(outname+".hed"):
                apDisplay.printError("normalization did not execute properly")

        return outname
Beispiel #4
0
def alimass(infile, outfile=None, maxshift=0.2, ceniter=10, nproc=1):
    """
        uses a rotationally averaged total sum to center particles
        default max shift is 20% of box size
        default # of centering iterations is 10
        """
    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outname)
    else:
        outname = fname + "_cen"

    if nproc > 1:
        myIm = pymagic.ImagicSession("align/alimass.e_mpi", nproc)
        myIm.toImagicQuiet("YES")
        myIm.toImagicQuiet(nproc)
    else:
        myIm = pymagic.ImagicSession("align/alimass.e")
        myIm.toImagicQuiet("NO")
    myIm.toImagicQuiet(fname)
    myIm.toImagicQuiet(outname)
    myIm.toImagicQuiet("TOTSUM")
    myIm.toImagicQuiet("CCF")
    myIm.toImagicQuiet("%.3f" % maxshift)
    myIm.toImagicQuiet("%i" % ceniter)
    myIm.toImagicQuiet("NO_FILTER")
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("alimass.e did not execute properly")
        return None

    return outname
def softMask(infile, outfile=None, mask=0.8, falloff=0.1):
    """
	applies a soft mask to images in a stack
	"""
    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outname)
    else:
        outname = fname + "_mask"

    ## output temporary file to same directory as original file
    outfile = fname + "_mask"
    myIm = pymagic.ImagicSession("stand/arithm.e")
    myIm.toImagicQuiet("%s" % fname)  # input
    myIm.toImagicQuiet("%s" % outname)  # output
    myIm.toImagicQuiet("SOFT")  # soft mask
    myIm.toImagicQuiet("%.2f" % mask)  # mask as a fraction
    myIm.toImagicQuiet("%.2f" % falloff)  # falloff as a fraction
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("arithm.e did not execute properly")
        return None

    return outname
def alimass(infile, outfile=None, maxshift=0.2, ceniter=10, nproc=1):
    """
	uses a rotationally averaged total sum to center particles
	default max shift is 20% of box size
	default # of centering iterations is 10
	"""
    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outname)
    else:
        outname = fname + "_cen"

    if nproc > 1:
        myIm = pymagic.ImagicSession("align/alimass.e_mpi", nproc)
        myIm.toImagicQuiet("YES")
        myIm.toImagicQuiet(nproc)
    else:
        myIm = pymagic.ImagicSession("align/alimass.e")
        myIm.toImagicQuiet("NO")
    myIm.toImagicQuiet(fname)
    myIm.toImagicQuiet(outname)
    myIm.toImagicQuiet("TOTSUM")
    myIm.toImagicQuiet("CCF")
    myIm.toImagicQuiet("%.3f" % maxshift)
    myIm.toImagicQuiet("%i" % ceniter)
    myIm.toImagicQuiet("NO_FILTER")
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("alimass.e did not execute properly")
        return None

    return outname
Beispiel #7
0
def headersToFile(infile, outfile=None):
    """
        writes out shift values to a file
        """

    fname = pymagic.fileFilter(infile)
    if not outfile:
        outfile = "outparams.plt"

    myIm = pymagic.ImagicSession("stand/headers.e")
    if myIm.version() < 91120:
        myIm.toImagicQuiet(fname)
        myIm.toImagicQuiet("PLT")
        myIm.toImagicQuiet("SHIFT")
        myIm.toImagicQuiet(outfile)
        myIm.toImagicQuiet("*")
    else:
        myIm.toImagicQuiet("PLT")
        myIm.toImagicQuiet("SHIFT")
        myIm.toImagicQuiet(fname)
        myIm.toImagicQuiet(outfile)
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outfile):
        apDisplay.printError("headers.e did not execute properly")
        return None

    return outfile
def headersToFile(infile, outfile=None):
    """
	writes out shift values to a file
	"""

    fname = pymagic.fileFilter(infile)
    if not outfile:
        outfile = "outparams.plt"

    myIm = pymagic.ImagicSession("stand/headers.e")
    if myIm.version() < 91120:
        myIm.toImagicQuiet(fname)
        myIm.toImagicQuiet("PLT")
        myIm.toImagicQuiet("SHIFT")
        myIm.toImagicQuiet(outfile)
        myIm.toImagicQuiet("*")
    else:
        myIm.toImagicQuiet("PLT")
        myIm.toImagicQuiet("SHIFT")
        myIm.toImagicQuiet(fname)
        myIm.toImagicQuiet(outfile)
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outfile):
        apDisplay.printError("headers.e did not execute properly")
        return None

    return outfile
Beispiel #9
0
def alirefs(infile,
            outfile=None,
            mask=0.99,
            maxshift=0.3,
            minrot=-180.0,
            maxrot=180.0,
            iter=5,
            minrad=0.0,
            maxrad=0.9):
    """
        align a stack of references to each other
        """

    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outfile, exists=False)
    else:
        outname = fname + "_ali"

    myIm = pymagic.ImagicSession("align/alirefs.e")
    imagicv = myIm.version()
    myIm.toImagicQuiet("ALL")  # translation & rotation
    if imagicv >= 101013:
        myIm.toImagicQuiet("ROTATION_FIRST")
    myIm.toImagicQuiet("CCF")  # CCF or MCF
    myIm.toImagicQuiet("%s" % fname)  # input
    myIm.toImagicQuiet("NO")  # no contours on reference imgs
    myIm.toImagicQuiet("%.2f" % mask)  # mask as fraction
    myIm.toImagicQuiet("%s" % outname)  # output
    myIm.toImagicQuiet("-999.")  # density for thresholding
    myIm.toImagicQuiet("%.2f" % maxshift)  # max shift
    myIm.toImagicQuiet("%.2f,%.2f" % (minrot, maxrot))  # min max rot angle
    if imagicv >= 101013:
        myIm.toImagicQuiet("MEDIUM")  # Precision for rotational alignment
        myIm.toImagicQuiet("%.2f,%.2f" %
                           (minrad, maxrad))  # min, max radius for rot align
    myIm.toImagicQuiet("NO")  # create mirrors
    myIm.toImagicQuiet("%i" % iter)  # alignment iterations
    myIm.toImagicQuiet("NO")  # full output of all parameters
    myIm.close()

    # check proper execution
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("alirefs.e did not execute properly")

    return outname
def alirefs(infile, outfile=None, mask=0.99, maxshift=0.3, minrot=-180.0, maxrot=180.0, iter=5, minrad=0.0, maxrad=0.9):
    """
	align a stack of references to each other
	"""

    fname = pymagic.fileFilter(infile)
    if outfile:
        outname = pymagic.fileFilter(outfile, exists=False)
    else:
        outname = fname + "_ali"

    myIm = pymagic.ImagicSession("align/alirefs.e")
    imagicv = myIm.version()
    myIm.toImagicQuiet("ALL")  # translation & rotation
    if imagicv >= 101013:
        myIm.toImagicQuiet("ROTATION_FIRST")
    myIm.toImagicQuiet("CCF")  # CCF or MCF
    myIm.toImagicQuiet("%s" % fname)  # input
    myIm.toImagicQuiet("NO")  # no contours on reference imgs
    myIm.toImagicQuiet("%.2f" % mask)  # mask as fraction
    myIm.toImagicQuiet("%s" % outname)  # output
    myIm.toImagicQuiet("-999.")  # density for thresholding
    myIm.toImagicQuiet("%.2f" % maxshift)  # max shift
    myIm.toImagicQuiet("%.2f,%.2f" % (minrot, maxrot))  # min max rot angle
    if imagicv >= 101013:
        myIm.toImagicQuiet("MEDIUM")  # Precision for rotational alignment
        myIm.toImagicQuiet("%.2f,%.2f" % (minrad, maxrad))  # min, max radius for rot align
    myIm.toImagicQuiet("NO")  # create mirrors
    myIm.toImagicQuiet("%i" % iter)  # alignment iterations
    myIm.toImagicQuiet("NO")  # full output of all parameters
    myIm.close()

    # check proper execution
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("alirefs.e did not execute properly")

    return outname
def mask2D(boxsz, mask, infile=False, maskfile="mask2Dimgfile"):
        """
        creates a 2d circular mask
        if infile is specified, mask is applied to stack & then mask is deleted
        boxsz is the box size in pixels
        mask is the size of the mask to apply as a fraction
        """

        ### generate a 2D mask
#       f=open(batchfile,"w")
#       f.write("#!/bin/csh -f\n")
#       f.write("setenv IMAGIC_BATCH 1\n")
        apDisplay.printMsg("creating 2D mask")
        myIm = pymagic.ImagicSession("stand/testim.e")
        myIm.toImagicQuiet("%s"%maskfile)
        myIm.toImagicQuiet("%i,%i"%(boxsz,boxsz))
        myIm.toImagicQuiet("real")
        myIm.toImagicQuiet("disc")
        myIm.toImagicQuiet("%.3f"%mask)
        myIm.close()

        if not infile:
                # check proper execution
                if not os.path.exists(maskfile+".hed"):
                        apDisplay.printError("mask generation did not execute properly")
                return maskfile

        ### if infile is specified, apply mask to images
        fname = pymagic.fileFilter(infile)
        file_ma=fname+"_ma"

        apDisplay.printMsg("applying 2D mask")
        myIm = pymagic.ImagicSession("stand/twoimag.e")
        myIm.toImagicQuiet("mul")
        myIm.toImagicQuiet("%s"%fname)
        myIm.toImagicQuiet("%s"%maskfile)
        myIm.toImagicQuiet("%s"%file_ma)
        myIm.close()

        # check proper execution
        if not os.path.exists(file_ma+".hed"):
                apDisplay.printError("masking did not execute properly")

        return file_ma
def mask2D(boxsz, mask, infile=False, maskfile="mask2Dimgfile"):
    """
	creates a 2d circular mask
	if infile is specified, mask is applied to stack & then mask is deleted
	boxsz is the box size in pixels
	mask is the size of the mask to apply as a fraction
	"""

    ### generate a 2D mask
    #	f=open(batchfile,"w")
    #	f.write("#!/bin/csh -f\n")
    #	f.write("setenv IMAGIC_BATCH 1\n")
    apDisplay.printMsg("creating 2D mask")
    myIm = pymagic.ImagicSession("stand/testim.e")
    myIm.toImagicQuiet("%s" % maskfile)
    myIm.toImagicQuiet("%i,%i" % (boxsz, boxsz))
    myIm.toImagicQuiet("real")
    myIm.toImagicQuiet("disc")
    myIm.toImagicQuiet("%.3f" % mask)
    myIm.close()

    if not infile:
        # check proper execution
        if not os.path.exists(maskfile + ".hed"):
            apDisplay.printError("mask generation did not execute properly")
        return maskfile

    ### if infile is specified, apply mask to images
    fname = pymagic.fileFilter(infile)
    file_ma = fname + "_ma"

    apDisplay.printMsg("applying 2D mask")
    myIm = pymagic.ImagicSession("stand/twoimag.e")
    myIm.toImagicQuiet("mul")
    myIm.toImagicQuiet("%s" % fname)
    myIm.toImagicQuiet("%s" % maskfile)
    myIm.toImagicQuiet("%s" % file_ma)
    myIm.close()

    # check proper execution
    if not os.path.exists(file_ma + ".hed"):
        apDisplay.printError("masking did not execute properly")

    return file_ma
def takeoverHeaders(filename, numpart, boxsize):
        ### better workaround than copyFile ... still a workaround though

        fname = pymagic.fileFilter(filename)
        
        ## output temporary file to same directory as original file
        outpath = os.path.split(fname)[0]
        outfile = os.path.join(outpath,"tempimg")
        myIm = pymagic.ImagicSession("stand/testim.e")
        myIm.toImagicQuiet("%s,1,%d" % (outfile,numpart))
        myIm.toImagicQuiet("%d,%d" % (boxsize, boxsize))
        myIm.toImagicQuiet("REAL")
        myIm.toImagicQuiet("BLOBS")
        myIm.close()
        
        if not os.path.exists(outfile+".hed"):
                apDisplay.printError("header rewrite failed")
        apDisplay.printMsg("replacing '%s' with '%s'"%(fname+".hed",outfile+".hed"))
        shutil.move(outfile+".hed", fname+".hed")
        os.remove(outfile+".img")
def takeoverHeaders(filename, numpart, boxsize):
    ### better workaround than copyFile ... still a workaround though

    fname = pymagic.fileFilter(filename)

    ## output temporary file to same directory as original file
    outpath = os.path.split(fname)[0]
    outfile = os.path.join(outpath, "tempimg")
    myIm = pymagic.ImagicSession("stand/testim.e")
    myIm.toImagicQuiet("%s,1,%d" % (outfile, numpart))
    myIm.toImagicQuiet("%d,%d" % (boxsize, boxsize))
    myIm.toImagicQuiet("REAL")
    myIm.toImagicQuiet("BLOBS")
    myIm.close()

    if not os.path.exists(outfile + ".hed"):
        apDisplay.printError("header rewrite failed")
    apDisplay.printMsg("replacing '%s' with '%s'" %
                       (fname + ".hed", outfile + ".hed"))
    shutil.move(outfile + ".hed", fname + ".hed")
    os.remove(outfile + ".img")
def readIndexFromHeader(headerfilename, indexnum, numparts=100):
        """
        returns the header values contained at the index
        as an array of float values
        Limited to the numparts if specified
        """
        fname = pymagic.fileFilter(headerfilename)              
        headfile=open(fname+".hed",'rb')

        # get number or particles in stack
        nump = int('%d' % (os.stat(fname+".hed")[6]/1024))
        if numparts > nump:
                numparts = nump

        # create array of values
        headervals=[]
        for particle in range(numparts+1):
                headerbytes = headfile.read(1024)
                headervals.append(numpy.fromstring(headerbytes,dtype=numpy.float32)[indexnum])
        headfile.close()
        return headervals
def readIndexFromHeader(headerfilename, indexnum, numparts=100):
    """
	returns the header values contained at the index
	as an array of float values
	Limited to the numparts if specified
	"""
    fname = pymagic.fileFilter(headerfilename)
    headfile = open(fname + ".hed", 'rb')

    # get number or particles in stack
    nump = int('%d' % (os.stat(fname + ".hed")[6] / 1024))
    if numparts > nump:
        numparts = nump

    # create array of values
    headervals = []
    for particle in range(numparts + 1):
        headerbytes = headfile.read(1024)
        headervals.append(
            numpy.fromstring(headerbytes, dtype=numpy.float32)[indexnum])
    headfile.close()
    return headervals
def mralign(alistack, origstack, refs, outfile=None, mask=0.8, imask=0, nproc=1):
    """
	perform a multi-reference alignment
	"""

    aliname = pymagic.fileFilter(alistack)
    stackname = pymagic.fileFilter(origstack)
    refname = pymagic.fileFilter(refs)
    if outfile:
        outname = pymagic.fileFilter(outfile)
    else:
        outname = "mrastack"

    if nproc > 1:
        myIm = pymagic.ImagicSession("align/mralign.e_mpi", nproc)
        myIm.toImagicQuiet("YES")
        myIm.toImagicQuiet(nproc)
    else:
        myIm = pymagic.ImagicSession("align/mralign.e")
        myIm.toImagicQuiet("NO")

        ## get imagic version
    imagicv = myIm.version()

    ## rest of the params
    myIm.toImagicQuiet("FRESH")
    myIm.toImagicQuiet("ALL")
    # 091120 or higher version 4D options:
    if imagicv >= 91120:
        myIm.toImagicQuiet("ALIGNMENT")
        myIm.toImagicQuiet("ALL")
    myIm.toImagicQuiet("ROTATION_FIRST")
    myIm.toImagicQuiet("CCF")
    myIm.toImagicQuiet(aliname)
    myIm.toImagicQuiet(outname)
    myIm.toImagicQuiet(stackname)
    myIm.toImagicQuiet(refname)
    myIm.toImagicQuiet("NO_FILTER")
    # lower than 091120 version of imagic asks for mirrors:
    if imagicv < 91120:
        myIm.toImagicQuiet("NO")
    myIm.toImagicQuiet("0.31")
    # check if there are any rotations stored in the header
    equivRots = apImagicFile.readIndexFromHeader(aliname, 116)
    hasRots = False
    for value in equivRots:
        if value != 0:
            hasRots = True
            break

            # don't ask Max shift (during this alignment) for first iteration:
    if hasRots is True:
        myIm.toImagicQuiet("0.2")
    myIm.toImagicQuiet("-180,180")
    # don't ask rotation (during this alignment) for first iteration:
    if hasRots is True:
        myIm.toImagicQuiet("-180,180")
    myIm.toImagicQuiet("MEDIUM")
    myIm.toImagicQuiet("%.2f,%.2f" % (imask, mask))
    myIm.toImagicQuiet("2")
    myIm.toImagicQuiet("NO")
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("mralign.e did not execute properly")
        return None

    return outname
Beispiel #18
0
def mralign(alistack,
            origstack,
            refs,
            outfile=None,
            mask=0.8,
            imask=0,
            nproc=1):
    """
        perform a multi-reference alignment
        """

    aliname = pymagic.fileFilter(alistack)
    stackname = pymagic.fileFilter(origstack)
    refname = pymagic.fileFilter(refs)
    if outfile:
        outname = pymagic.fileFilter(outfile)
    else:
        outname = "mrastack"

    if nproc > 1:
        myIm = pymagic.ImagicSession("align/mralign.e_mpi", nproc)
        myIm.toImagicQuiet("YES")
        myIm.toImagicQuiet(nproc)
    else:
        myIm = pymagic.ImagicSession("align/mralign.e")
        myIm.toImagicQuiet("NO")

    ## get imagic version
    imagicv = myIm.version()

    ## rest of the params
    myIm.toImagicQuiet("FRESH")
    myIm.toImagicQuiet("ALL")
    # 091120 or higher version 4D options:
    if imagicv >= 91120:
        myIm.toImagicQuiet("ALIGNMENT")
        myIm.toImagicQuiet("ALL")
    myIm.toImagicQuiet("ROTATION_FIRST")
    myIm.toImagicQuiet("CCF")
    myIm.toImagicQuiet(aliname)
    myIm.toImagicQuiet(outname)
    myIm.toImagicQuiet(stackname)
    myIm.toImagicQuiet(refname)
    myIm.toImagicQuiet("NO_FILTER")
    # lower than 091120 version of imagic asks for mirrors:
    if imagicv < 91120:
        myIm.toImagicQuiet("NO")
    myIm.toImagicQuiet("0.31")
    # check if there are any rotations stored in the header
    equivRots = apImagicFile.readIndexFromHeader(aliname, 116)
    hasRots = False
    for value in equivRots:
        if value != 0:
            hasRots = True
            break

    # don't ask Max shift (during this alignment) for first iteration:
    if hasRots is True:
        myIm.toImagicQuiet("0.2")
    myIm.toImagicQuiet("-180,180")
    # don't ask rotation (during this alignment) for first iteration:
    if hasRots is True:
        myIm.toImagicQuiet("-180,180")
    myIm.toImagicQuiet("MEDIUM")
    myIm.toImagicQuiet("%.2f,%.2f" % (imask, mask))
    myIm.toImagicQuiet("2")
    myIm.toImagicQuiet("NO")
    myIm.close()

    ### check that it ran correctly
    if not os.path.exists(outname + ".hed"):
        apDisplay.printError("mralign.e did not execute properly")
        return None

    return outname