Example #1
0
    def runMaxlike(self):
        stackdata = apStack.getOnlyStackData(self.params['stackid'])
        apix = apStack.getStackPixelSizeFromStackId(self.params['stackid'])
        stackfile = os.path.join(stackdata['path']['path'], stackdata['name'])

        ### process stack to local file
        self.params['localstack'] = os.path.join(self.params['rundir'],
                                                 self.timestamp + ".hed")
        proccmd = "proc2d " + stackfile + " " + self.params[
            'localstack'] + " apix=" + str(apix)
        if self.params['highpass'] > 1:
            proccmd += " hp=" + str(self.params['highpass'])
        if self.params['lowpass'] > 1:
            proccmd += " lp=" + str(self.params['lowpass'])
        apEMAN.executeEmanCmd(proccmd, verbose=True)

        ### convert stack into single spider files
        self.partlistdocfile = apXmipp.breakupStackIntoSingleFiles(
            self.params['localstack'])

        ### setup Xmipp command
        aligntime = time.time()
        xmippopts = (
            " " + " -i " +
            os.path.join(self.params['rundir'], self.partlistdocfile) +
            " -nref 1 " + " -iter 10 " + " -o " +
            os.path.join(self.params['rundir'], "part" + self.timestamp) +
            " -fast -C 1e-18 ")
        ### angle step
        if self.params['rotate'] is True:
            xmippopts += " -psi_step 90 "
        else:
            xmippopts += " -psi_step 360 "
        ### convergence criteria
        if self.params['converge'] == "fast":
            xmippopts += " -eps 5e-3 "
        elif self.params['converge'] == "slow":
            xmippopts += " -eps 5e-8 "
        else:
            xmippopts += " -eps 5e-5 "
        ### mirrors
        if self.params['mirror'] is True:
            xmippopts += " -mirror "
        if self.params['maxshift'] is not None:
            xmippopts += " -max_shift %d " % (self.params['maxshift'])

        ### use single processor
        xmippexe = apParam.getExecPath("xmipp_ml_align2d", die=True)
        xmippcmd = xmippexe + " " + xmippopts
        self.writeXmippLog(xmippcmd)
        apEMAN.executeEmanCmd(xmippcmd, verbose=True, showcmd=True)
        aligntime = time.time() - aligntime
        apDisplay.printMsg("Alignment time: " +
                           apDisplay.timeString(aligntime))

        ### create a quick mrc
        emancmd = "proc2d part" + self.timestamp + "_ref000001.xmp average.mrc"
        apEMAN.executeEmanCmd(emancmd, verbose=True)
        apFile.removeStack(self.params['localstack'])
        apFile.removeFilePattern("partfiles/*")
Example #2
0
	def start(self):
		### load parameters
		runparams = self.readRunParameters()

		### align references
		self.alignReferences(runparams)

		### create an aligned stack
		self.createAlignedReferenceStack()

		### read particles
		self.lastiter = self.findLastIterNumber()
		if self.params['sort'] is True:
			self.sortFolder()
		reflist = self.readRefDocFile()
		partlist = self.readPartDocFile(reflist)
		self.writePartDocFile(partlist)

		### create aligned stacks
		alignimagicfile = self.createAlignedStacks(partlist, runparams['localstack'])
		apStack.averageStack(alignimagicfile)

		### calculate resolution for each reference
		apix = apStack.getStackPixelSizeFromStackId(runparams['stackid'])*runparams['bin']
		self.calcResolution(partlist, alignimagicfile, apix)

		### insert into databse
		self.insertRunIntoDatabase(alignimagicfile, runparams)
		self.insertParticlesIntoDatabase(runparams['stackid'], partlist)

		apFile.removeStack(runparams['localstack'], warn=False)
		rmcmd = "/bin/rm -fr partfiles/*"
		apEMAN.executeEmanCmd(rmcmd, verbose=False, showcmd=False)
		apFile.removeFilePattern("partfiles/*")
    def runMaxlike(self):
        stackdata = apStack.getOnlyStackData(self.params["stackid"])
        apix = apStack.getStackPixelSizeFromStackId(self.params["stackid"])
        stackfile = os.path.join(stackdata["path"]["path"], stackdata["name"])

        ### process stack to local file
        self.params["localstack"] = os.path.join(self.params["rundir"], self.timestamp + ".hed")
        proccmd = "proc2d " + stackfile + " " + self.params["localstack"] + " apix=" + str(apix)
        if self.params["highpass"] > 1:
            proccmd += " hp=" + str(self.params["highpass"])
        if self.params["lowpass"] > 1:
            proccmd += " lp=" + str(self.params["lowpass"])
        apEMAN.executeEmanCmd(proccmd, verbose=True)

        ### convert stack into single spider files
        self.partlistdocfile = apXmipp.breakupStackIntoSingleFiles(self.params["localstack"])

        ### setup Xmipp command
        aligntime = time.time()
        xmippopts = (
            " "
            + " -i "
            + os.path.join(self.params["rundir"], self.partlistdocfile)
            + " -nref 1 "
            + " -iter 10 "
            + " -o "
            + os.path.join(self.params["rundir"], "part" + self.timestamp)
            + " -fast -C 1e-18 "
        )
        ### angle step
        if self.params["rotate"] is True:
            xmippopts += " -psi_step 90 "
        else:
            xmippopts += " -psi_step 360 "
        ### convergence criteria
        if self.params["converge"] == "fast":
            xmippopts += " -eps 5e-3 "
        elif self.params["converge"] == "slow":
            xmippopts += " -eps 5e-8 "
        else:
            xmippopts += " -eps 5e-5 "
        ### mirrors
        if self.params["mirror"] is True:
            xmippopts += " -mirror "
        if self.params["maxshift"] is not None:
            xmippopts += " -max_shift %d " % (self.params["maxshift"])

        ### use single processor
        xmippexe = apParam.getExecPath("xmipp_ml_align2d", die=True)
        xmippcmd = xmippexe + " " + xmippopts
        self.writeXmippLog(xmippcmd)
        apEMAN.executeEmanCmd(xmippcmd, verbose=True, showcmd=True)
        aligntime = time.time() - aligntime
        apDisplay.printMsg("Alignment time: " + apDisplay.timeString(aligntime))

        ### create a quick mrc
        emancmd = "proc2d part" + self.timestamp + "_ref000001.xmp average.mrc"
        apEMAN.executeEmanCmd(emancmd, verbose=True)
        apFile.removeStack(self.params["localstack"])
        apFile.removeFilePattern("partfiles/*")
	def start(self):
		aligndata = appiondata.ApAlignStackData.direct_query(self.params['alignstackid'])
		boxsize = aligndata['boxsize']
		apix = aligndata['pixelsize']
		maskpixrad = self.params['maskrad']/apix
		if maskpixrad*2 > boxsize-2:
			apDisplay.printError("Mask radius is too big for boxsize: %d > %d"%(maskpixrad*2,boxsize-2))
		apDisplay.printMsg("Mask radius and boxsize: %.1f < %d"%(maskpixrad*2,boxsize-2))
		self.instack = os.path.join(aligndata['path']['path'], aligndata['imagicfile'])
		outdata = "stack.data"

		apXmipp.convertStackToXmippData(self.instack, outdata, maskpixrad,
			boxsize, numpart=self.params['numpart']-1)

		self.runKerdenSOM(outdata)
		if apFile.stackSize(self.instack) > 3.0*(1024**3):
			# Big stacks use eman
			self.createMontageByEMAN()
			binned = None
		else:
			binned = self.createMontageInMemory(apix)
		self.insertKerDenSOM(binned=binned)

		apFile.removeFile(outdata)
		apFile.removeFilePattern("*.cod")
def convertStackToXmippData(instack, outdata, maskpixrad, boxsize, numpart=None):
	"""
	From http://xmipp.cnb.csic.es/twiki/bin/view/Xmipp/Img2Data

	This program applies a mask to a set of images. 
	This set is given by a selfile. 
	After applying the mask the result is storaged as a vector in the following format:
		The first line indicates the dimension of the vectors and the number of vectors.
		The rest of the lines are the feature vectors. 
		Each line is a vector and each column is a vectors' component (pixels values inside the mask). 
	"""
	apDisplay.printMsg("Convert stack file to Xmipp data file")
	maskfile = "circlemask.spi"
	operations.createMask(maskfile, maskpixrad, boxsize)
	partlistdocfile = breakupStackIntoSingleFiles(instack, numpart=numpart)
	convertcmd = "xmipp_convert_img2data -i %s -mask %s -o %s"%(partlistdocfile, maskfile, outdata)
	proc = subprocess.Popen(convertcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
	proc.wait()
	outfilesize = apFile.fileSize(outdata)
	partfilesize = apFile.fileSize(partlistdocfile)
	if outfilesize < 2*partfilesize:
		apDisplay.printError("Outdata conversion did not work, data file smaller than docfile, %s < %s"
			%(apDisplay.bytes(outfilesize), apDisplay.bytes(partfilesize)))
	apFile.removeFilePattern("partfiles/*")

	return outdata
	def close(self):
		"""
		hack to override appionScript close
		"""
		#apDisplay.printMsg("Waiting 10 seconds for threads to complete")
		#time.sleep(10)
		if self.params['keepall'] is False and self.params['limit'] is None:
			pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*.dwn.mrc')
			apFile.removeFilePattern(pattern)
		appionScript.AppionScript.close(self)
def renderAnimation(density, contour=None, zoom=1.0, sym=None, color=None,
                silhouette=False, xvfb=False, name=None):
        """
        create several snapshots and merge into animated GIF
        """
        if isValidVolume(density) is False:
                apDisplay.printError("Volume file is not valid")
        ### setup chimera params
        if name is None:
                os.environ['CHIMVOL'] = density
        else:
                ### set chimera to use temp volume
                os.environ['CHIMTEMPVOL'] = density
                os.environ['CHIMVOL'] = name
        os.environ['CHIMTYPE'] = 'animate'
        if silhouette is True:
                os.environ['CHIMSILHOUETTE'] = 'true'
        else:
                os.unsetenv('CHIMSILHOUETTE')
        if sym is not None:
                os.environ['CHIMSYM'] = sym
        if contour is not None:
                os.environ['CHIMCONTOUR'] = str(contour)
        if color is not None:
                colorstr = colorToString(color)
                os.environ['CHIMCOLORS'] = colorstr
        else:
                colorstr = getColorString()
                os.environ['CHIMCOLORS'] = colorstr
        if zoom is not None:
                os.environ['CHIMZOOM'] = str(zoom)
        os.environ['CHIMIMGSIZE'] = "512"
        ### unused
        #'CHIMBACK',  'CHIMIMGSIZE', 'CHIMIMGFORMAT', 'CHIMFILEFORMAT',
        chimsnappath = getSnapPath()
        apDisplay.printColor("running Chimera Animation for sym "+str(sym), "cyan")
        runChimeraScript(chimsnappath, xvfb=xvfb)
        image1 = os.environ['CHIMVOL']+".001.png"

        if os.path.isfile(image1):
                ### merge into animated GIF
                finalgif = density+".animate.gif"
                imagemagickcmd1 = "convert -delay 10 -loop 15 "
                images = glob.glob(density+".*[0-9][0-9].png")
                images.sort()
                imagestr = ""
                for image in images:
                        imagestr += image+" "
                imagemagickcmd1 += imagestr+finalgif
                apFile.removeFile(finalgif)
                proc = subprocess.Popen(imagemagickcmd1, shell=True)
                proc.wait()
                #if os.path.isfile(finalgif):
                apFile.removeFilePattern(density+".*[0-9][0-9].png")
        return
    def createMontageByEMAN(self):
        self.cluster_resolution = []
        apDisplay.printMsg("Converting files")

        ### create crappy files
        emancmd = ("proc2d " + self.instack +
                   " crap.mrc first=0 last=0 mask=1")
        apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
        emancmd = ("proc2d crap.mrc crap.png")
        apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)

        files = glob.glob(self.timestamp + ".[0-9]*")
        files.sort(self.sortFile)
        montagecmd = ("montage -geometry +4+4 -tile %dx%d " %
                      (self.params['xdim'], self.params['ydim']))
        stackname = "kerdenstack" + self.timestamp + ".hed"
        count = 0
        numclass = self.params['xdim'] * self.params['ydim']
        i = 0
        for listname in files:
            i += 1
            apDisplay.printMsg("%d of %d classes" % (i, len(files)))
            #listname = self.timestamp+str(i)
            if not os.path.isfile(listname) or apFile.fileSize(listname) < 1:
                ### create a ghost particle
                emancmd = ("proc2d crap.mrc " + stackname + " ")
                sys.stderr.write("skipping " + listname + "\n")
                apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
                ### create png
                shutil.copy("crap.png", listname + ".png")
            else:
                ### average particles
                emancmd = ("proc2d %s %s list=%s average" %
                           (self.instack, stackname, listname))
                apEMAN.executeEmanCmd(emancmd, showcmd=True, verbose=False)
                ### create mrc
                emancmd = ("proc2d %s %s first=%d last=%d" %
                           (stackname, listname + ".mrc", count, count))
                apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
                ### create png
                emancmd = ("proc2d %s %s" %
                           (listname + ".mrc", listname + ".png"))
                apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
            ### FIX ME: for now fill self.clsuter_sersolution with None, although it
            ### should be possible to  calculate it if particle list exists like in createMontageInMemory
            self.cluster_resolution.append(None)

            montagecmd += listname + ".png "
            count += 1
        montagecmd += "montage.png"
        apEMAN.executeEmanCmd(montagecmd, showcmd=True, verbose=False)
        time.sleep(1)
        apFile.removeFile("crap.mrc")
        apFile.removeFile("crap.png")
        apFile.removeFilePattern(self.timestamp + ".*.png")
 def close(self):
     """
             hack to override appionScript close
             """
     #apDisplay.printMsg("Waiting 10 seconds for threads to complete")
     #time.sleep(10)
     if self.params['keepall'] is False and self.params['limit'] is None:
         pattern = os.path.join(self.params['rundir'],
                                self.params['sessionname'] + '*.dwn.mrc')
         apFile.removeFilePattern(pattern)
     appionScript.AppionScript.close(self)
	def createMontageByEMAN(self):
		self.cluster_resolution = []
		apDisplay.printMsg("Converting files")

		### create crappy files
		emancmd = ( "proc2d "+self.instack+" crap.mrc first=0 last=0 mask=1" )
		apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
		emancmd = ( "proc2d crap.mrc crap.png" )
		apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)

		files = glob.glob(self.timestamp+".[0-9]*")
		files.sort(self.sortFile)
		montagecmd = ("montage -geometry +4+4 -tile %dx%d "%(self.params['xdim'], self.params['ydim']))
		stackname = "kerdenstack"+self.timestamp+".hed"
		count = 0
		numclass = self.params['xdim']*self.params['ydim']
		i = 0
		for listname in files:
			i += 1
			apDisplay.printMsg("%d of %d classes"%(i,len(files)))
			#listname = self.timestamp+str(i)
			if not os.path.isfile(listname) or apFile.fileSize(listname) < 1:
				### create a ghost particle
				emancmd = ( "proc2d crap.mrc "+stackname+" " )
				sys.stderr.write("skipping "+listname+"\n")
				apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
				### create png
				shutil.copy("crap.png", listname+".png")
			else:
				### average particles
				emancmd = ("proc2d %s %s list=%s average"%
					(self.instack, stackname, listname))
				apEMAN.executeEmanCmd(emancmd, showcmd=True, verbose=False)
				### create mrc
				emancmd = ("proc2d %s %s first=%d last=%d"%
					(stackname, listname+".mrc", count, count))
				apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
				### create png
				emancmd = ("proc2d %s %s"%
					(listname+".mrc", listname+".png"))
				apEMAN.executeEmanCmd(emancmd, showcmd=False, verbose=False)
			### FIX ME: for now fill self.clsuter_sersolution with None, although it 
			### should be possible to  calculate it if particle list exists like in createMontageInMemory
			self.cluster_resolution.append(None)

			montagecmd += listname+".png "
			count +=1
		montagecmd += "montage.png"
		apEMAN.executeEmanCmd(montagecmd, showcmd=True, verbose=False)
		time.sleep(1)
		apFile.removeFile("crap.mrc")
		apFile.removeFile("crap.png")
		apFile.removeFilePattern(self.timestamp+".*.png")
	def postLoopFunctions(self):
		### Delete CTF corrected images
		if self.params['keepall'] is False:
			pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*.dwn.mrc')
			apFile.removeFilePattern(pattern)
			### remove Ace2 images
			pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*mrc.corrected.mrc')
			apFile.removeFilePattern(pattern)
			### remove Spider images
			if self.params['fliptype'] == 'spiderimage':
				pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*_out.spi')
				apFile.removeFilePattern(pattern)
				pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*_tf.spi')
				apFile.removeFilePattern(pattern)
		if self.noimages is True:
			return

		stackpath = os.path.join(self.params['rundir'], self.params['single'])
		### delete this after testing
		apStack.averageStack(stack = stackpath)
		### Create Stack Mean Plot
		if self.params['commit'] is True and self.params['meanplot'] is True:
			stackid = apStack.getStackIdFromPath(stackpath)
			if stackid is not None:
				apStackMeanPlot.makeStackMeanPlot(stackid)

		apDisplay.printColor("Timing stats", "blue")
		self.printTimeStats("Batch Boxer", self.batchboxertimes)
		self.printTimeStats("Ctf Correction", self.ctftimes)
		self.printTimeStats("Stack Merging", self.mergestacktimes)
		self.printTimeStats("Mean/Std Read", self.meanreadtimes)
		self.printTimeStats("DB Insertion", self.insertdbtimes)
Example #12
0
def convertStackToXmippData(instack,
                            outdata,
                            maskpixrad,
                            boxsize,
                            numpart=None):
    """
	From http://xmipp.cnb.csic.es/twiki/bin/view/Xmipp/Img2Data

	This program applies a mask to a set of images. 
	This set is given by a selfile. 
	After applying the mask the result is storaged as a vector in the following format:
		The first line indicates the dimension of the vectors and the number of vectors.
		The rest of the lines are the feature vectors. 
		Each line is a vector and each column is a vectors' component (pixels values inside the mask). 
	"""
    apDisplay.printMsg("Convert stack file to Xmipp data file")
    maskfile = "circlemask.spi"
    operations.createMask(maskfile, maskpixrad, boxsize)
    partlistdocfile = breakupStackIntoSingleFiles(instack, numpart=numpart)
    convertcmd = "xmipp_convert_img2data -i %s -mask %s -o %s" % (
        partlistdocfile, maskfile, outdata)
    proc = subprocess.Popen(
        convertcmd,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    proc.wait()
    outfilesize = apFile.fileSize(outdata)
    partfilesize = apFile.fileSize(partlistdocfile)
    if outfilesize < 2 * partfilesize:
        apDisplay.printError(
            "Outdata conversion did not work, data file smaller than docfile, %s < %s"
            % (apDisplay.bytes(outfilesize), apDisplay.bytes(partfilesize)))
    apFile.removeFilePattern("partfiles/*")

    return outdata
	def postLoopFunctions(self):
		pattern = os.path.join(self.params['rundir'], self.params['sessionname']+'*.corrected.mrc')
		apFile.removeFilePattern(pattern)
		ctfdb.printCtfSummary(self.params, self.imgtree)
Example #14
0
def renderAnimation(density,
                    contour=None,
                    zoom=1.0,
                    sym=None,
                    color=None,
                    silhouette=False,
                    xvfb=False,
                    name=None):
    """
        create several snapshots and merge into animated GIF
        """
    if isValidVolume(density) is False:
        apDisplay.printError("Volume file is not valid")
    ### setup chimera params
    if name is None:
        os.environ['CHIMVOL'] = density
    else:
        ### set chimera to use temp volume
        os.environ['CHIMTEMPVOL'] = density
        os.environ['CHIMVOL'] = name
    os.environ['CHIMTYPE'] = 'animate'
    if silhouette is True:
        os.environ['CHIMSILHOUETTE'] = 'true'
    else:
        os.unsetenv('CHIMSILHOUETTE')
    if sym is not None:
        os.environ['CHIMSYM'] = sym
    if contour is not None:
        os.environ['CHIMCONTOUR'] = str(contour)
    if color is not None:
        colorstr = colorToString(color)
        os.environ['CHIMCOLORS'] = colorstr
    else:
        colorstr = getColorString()
        os.environ['CHIMCOLORS'] = colorstr
    if zoom is not None:
        os.environ['CHIMZOOM'] = str(zoom)
    os.environ['CHIMIMGSIZE'] = "512"
    ### unused
    #'CHIMBACK',  'CHIMIMGSIZE', 'CHIMIMGFORMAT', 'CHIMFILEFORMAT',
    chimsnappath = getSnapPath()
    apDisplay.printColor("running Chimera Animation for sym " + str(sym),
                         "cyan")
    runChimeraScript(chimsnappath, xvfb=xvfb)
    image1 = os.environ['CHIMVOL'] + ".001.png"

    if os.path.isfile(image1):
        ### merge into animated GIF
        finalgif = density + ".animate.gif"
        imagemagickcmd1 = "convert -delay 10 -loop 15 "
        images = glob.glob(density + ".*[0-9][0-9].png")
        images.sort()
        imagestr = ""
        for image in images:
            imagestr += image + " "
        imagemagickcmd1 += imagestr + finalgif
        apFile.removeFile(finalgif)
        proc = subprocess.Popen(imagemagickcmd1, shell=True)
        proc.wait()
        #if os.path.isfile(finalgif):
        apFile.removeFilePattern(density + ".*[0-9][0-9].png")
    return
	def start(self):
		self.stack = {}
		self.stack['data'] = apStack.getOnlyStackData(self.params['stackid'])
		self.stack['apix'] = apStack.getStackPixelSizeFromStackId(self.params['stackid'])
		self.stack['part'] = apStack.getOneParticleFromStackId(self.params['stackid'])
		self.stack['boxsize'] = apStack.getStackBoxsize(self.params['stackid'])
		self.stack['file'] = os.path.join(self.stack['data']['path']['path'], self.stack['data']['name'])

		### test insert to make sure data is not overwritten
		self.params['runtime'] = 0
		#self.checkDuplicateRefBasedRun()

		### set box size
		self.boxsize = int(math.floor(self.stack['boxsize']/self.params['bin']/2.0))*2

		### convert stack to spider
		spiderstack = self.createSpiderFile()

		### create template stack
		templatestack = self.createTemplateStack()

		### run the alignment
		aligntime = time.time()
		usestack = spiderstack
		oldpartlist = None
		for i in range(self.params['numiter']):
			iternum = i+1
			apDisplay.printColor("\n\nITERATION "+str(iternum), "green")
			alignedstack, partlist = self.runAlignmentGPU(
				usestack, templatestack, spiderstack,
				self.params['xysearch'], self.params['xystep'],
				self.params['firstring'], self.params['lastring'],
				iternum=iternum, oldpartlist=oldpartlist)
			oldpartlist = partlist
			usestack = alignedstack
			templatestack = self.updateTemplateStack(alignedstack, partlist, iternum)
		aligntime = time.time() - aligntime
		apDisplay.printMsg("Alignment time: "+apDisplay.timeString(aligntime))

		### remove large, worthless stack
		spiderstack = os.path.join(self.params['rundir'], "start.spi")
		apDisplay.printMsg("Removing un-aligned stack: "+spiderstack)
		apFile.removeFile(spiderstack, warn=True)

		### convert aligned stack to imagic
		finalspistack = "aligned.spi"
		shutil.move(alignedstack, finalspistack)
		imagicstack = "aligned.hed"
		apFile.removeStack(imagicstack)
		emancmd = "proc2d "+finalspistack+" "+imagicstack
		apEMAN.executeEmanCmd(emancmd, verbose=True)

		### average stack
		apStack.averageStack(imagicstack)

		### calculate resolution for each reference
		apix = self.stack['apix']*self.params['bin']
		self.calcResolution(partlist, imagicstack, apix)

		if self.params['commit'] is True:
			apDisplay.printMsg("committing results to DB")
			self.params['runtime'] = aligntime
			self.insertRefBasedRun(partlist, imagicstack, insert=True)
		else:
			apDisplay.printWarning("not committing results to DB")

		### remove temporary files
		apFile.removeFilePattern("alignments/alignedstack*.spi")
		apFile.removeFile(finalspistack)
Example #16
0
    def createKerdenSOMPlots(self):
        apFile.removeFilePattern(
            os.path.join(self.params['rundir'],
                         self.spectraTemporalFilesMask + ".png"))

        #logging.debug('Inside createKerdenSOMPlots')
        apDisplay.printMsg("Create Plots")
        codeVectorFileName = os.path.join(self.params['rundir'],
                                          self.timestamp + '.cod')
        f1 = open(codeVectorFileName, 'r')
        #Read first line, I need number of harmonic plus size
        line = f1.readline()
        splitline = line.split()
        numberHarmonic = int(splitline[0])
        xx = int(splitline[2])
        yy = int(splitline[3])
        numberCodevectors = xx * yy

        xmin = int(self.params['spectralowharmonic'])
        xmax = int(self.params['spectrahighharmonic'])
        #array with x and y values
        xvalues = []
        #fill x array with harmonic number
        for colNo in pylab.arange(xmin, xmax + 1):
            xvalues.append(colNo)

        #figure size in inches
        pylab.rcParams['figure.figsize'] = 1, 1
        pylab.rc("lines", linewidth=1.5)
        pylab.rc(('xtick', 'ytick', 'axes'), labelsize=4.0)  #fontsize

        #read code vector
        #compute y maximum
        ymax = 0.
        ymin = 150.
        for rowNo in range(numberCodevectors):
            line = f1.readline()
            splitLine = line.split()
            for colNo in pylab.arange(numberHarmonic):
                yval = float(splitLine[colNo])
                if ymax < yval:
                    ymax = yval
                if ymin > yval:
                    ymin = yval
        f1.close()
        ymax = math.ceil(ymax) + 1
        print "ymax ", ymax
        ymin = max(math.floor(ymin) - 1, 0)
        print "ymin ", ymin

        f1 = open(codeVectorFileName, 'r')
        #skip first line
        line = f1.readline()
        for rowNo in range(numberCodevectors):
            line = f1.readline()
            splitLine = line.split()
            #print line
            data = []
            for colNo in pylab.arange(numberHarmonic):
                data.append(float(splitLine[colNo]))
            print xvalues
            print data
            #clear previous plot
            pylab.clf()
            lines = pylab.plot(xvalues, data)
            pylab.ylim(ymin, ymax)
            pylab.xlim(xmin, xmax)
            pylab.xlabel('fold symmetry')
            pylab.ylabel('likelihood')
            pylab.xticks(xvalues)
            basefilename = os.path.join(
                self.params['rundir'],
                self.spectraTemporalFiles % (int(rowNo / yy), rowNo % xx))
            pylab.savefig(basefilename + ".png", dpi=256, format='png')
        def start(self):
                #get aligned stack id
                aligndata = appiondata.ApAlignStackData.direct_query(self.params['alignstackid'])
                xSizeVoxel = aligndata['boxsize']
                #get averaged image
                avgmrc = os.path.join(aligndata['path']['path'], aligndata["avgmrcfile"])
                avg = mrc.read(avgmrc)
                tmpSpiderFile="average.xmp"
                spider.write(avg, tmpSpiderFile)
                self.runFindCenter(tmpSpiderFile,xSizeVoxel)
                #get aligned stack
                alignStack = os.path.join(aligndata['path']['path'], aligndata["imagicfile"])
                tempFileNameforSpectra=self.runMakeSpectra(alignStack)

                #kerdensom will work with spectra output
                self.runKerdenSOM(tempFileNameforSpectra)
                self.createMontageInMemory()
                self.insertRotKerDenSOM()

                #apFile.removeFile(outdata)
                apFile.removeFilePattern("*.cod")
                apFile.removeFilePattern("*.err")
                apFile.removeFilePattern("*.his")
                apFile.removeFilePattern("*.inf")
                apFile.removeFilePattern("*.vs")
                apFile.removeFilePattern("*.xmp")
                apFile.removeFile(tempFileNameforSpectra)
Example #18
0
    def start(self):
        #get aligned stack id
        aligndata = appiondata.ApAlignStackData.direct_query(
            self.params['alignstackid'])
        xSizeVoxel = aligndata['boxsize']
        #get averaged image
        avgmrc = os.path.join(aligndata['path']['path'],
                              aligndata["avgmrcfile"])
        avg = mrc.read(avgmrc)
        tmpSpiderFile = "average.xmp"
        spider.write(avg, tmpSpiderFile)
        self.runFindCenter(tmpSpiderFile, xSizeVoxel)
        #get aligned stack
        alignStack = os.path.join(aligndata['path']['path'],
                                  aligndata["imagicfile"])
        tempFileNameforSpectra = self.runMakeSpectra(alignStack)

        #kerdensom will work with spectra output
        self.runKerdenSOM(tempFileNameforSpectra)
        self.createMontageInMemory()
        self.insertRotKerDenSOM()

        #apFile.removeFile(outdata)
        apFile.removeFilePattern("*.cod")
        apFile.removeFilePattern("*.err")
        apFile.removeFilePattern("*.his")
        apFile.removeFilePattern("*.inf")
        apFile.removeFilePattern("*.vs")
        apFile.removeFilePattern("*.xmp")
        apFile.removeFile(tempFileNameforSpectra)
	def createMontageInMemory(self, apix):
		self.cluster_resolution = []
		apDisplay.printMsg("Converting files")

		### Set binning of images
		boxsize = apImagicFile.getBoxsize(self.instack)
		bin = 1
		while boxsize/bin > 200:
			bin+=1
		binboxsize = boxsize/bin

		### create averages
		files = glob.glob(self.timestamp+".[0-9]*")
		files.sort(self.sortFile)
		montage = []
		montagepngs = []
		i = 0
		for listname in files:
			i += 1
			apDisplay.printMsg("%d of %d classes"%(i,len(files)))
			pngfile = listname+".png"
			if not os.path.isfile(listname) or apFile.fileSize(listname) < 1:
				### create a ghost particle
				sys.stderr.write("skipping "+listname+"\n")
				blank = numpy.ones((binboxsize, binboxsize), dtype=numpy.float32)

				### add to montage stack
				montage.append(blank)
				self.cluster_resolution.append(None)

				### create png
				apImage.arrayToPng(blank, pngfile)

			else:
				### read particle list
				partlist = self.readListFile(listname)

				### average particles
				partdatalist = apImagicFile.readParticleListFromStack(self.instack, partlist, boxsize, msg=False)
				partdataarray = numpy.asarray(partdatalist)
				finaldata = partdataarray.mean(0)
				if bin > 1:
					finaldata = apImage.binImg(finaldata, bin)

				### add to montage stack
				montage.append(finaldata)
				res = apFourier.spectralSNR(partdatalist, apix)
				self.cluster_resolution.append(res)

				### create png
				apImage.arrayToPng(finaldata, pngfile)

			### check for png file
			if os.path.isfile(pngfile):
				montagepngs.append(pngfile)
			else:
				apDisplay.printError("failed to create montage")

		stackname = "kerdenstack"+self.timestamp+".hed"
		apImagicFile.writeImagic(montage, stackname)
		### create montage
		montagecmd = ("montage -geometry +4+4 -tile %dx%d "%(self.params['xdim'], self.params['ydim']))
		for monpng in montagepngs:
			montagecmd += monpng+" "
		montagecmd += "montage.png"
		apEMAN.executeEmanCmd(montagecmd, showcmd=True, verbose=False)
		time.sleep(1)
		apFile.removeFilePattern(self.timestamp+".*.png")
		return bin
        def createKerdenSOMPlots(self):
                apFile.removeFilePattern(os.path.join(self.params['rundir'],self.spectraTemporalFilesMask + ".png"))

                #logging.debug('Inside createKerdenSOMPlots')
                apDisplay.printMsg("Create Plots")
                codeVectorFileName = os.path.join(self.params['rundir'], self.timestamp+'.cod')
                f1=open(codeVectorFileName,'r')
                #Read first line, I need number of harmonic plus size
                line=f1.readline()
                splitline = line.split()
                numberHarmonic = int(splitline[0])
                xx = int(splitline[2])
                yy = int(splitline[3])
                numberCodevectors = xx * yy

                xmin = int(self.params['spectralowharmonic'])
                xmax = int(self.params['spectrahighharmonic'])
                #array with x and y values
                xvalues=[]
                #fill x array with harmonic number
                for colNo in pylab.arange(xmin,xmax+1):
                        xvalues.append(colNo)

                #figure size in inches
                pylab.rcParams['figure.figsize'] = 1, 1
                pylab.rc("lines", linewidth=1.5)
                pylab.rc(('xtick','ytick','axes'), labelsize=4.0)#fontsize

                #read code vector
                #compute y maximum
                ymax = 0.
                ymin = 150.
                for rowNo in range(numberCodevectors):
                        line=f1.readline()
                        splitLine=line.split()
                        for colNo in pylab.arange(numberHarmonic):
                                yval = float(splitLine[colNo])
                                if ymax < yval:
                                        ymax = yval
                                if ymin > yval:
                                        ymin = yval
                f1.close()
                ymax = math.ceil(ymax)+1
                print "ymax ", ymax
                ymin = max(math.floor(ymin)-1,0)
                print "ymin ", ymin

                f1=open(codeVectorFileName,'r')
                #skip first line
                line=f1.readline()
                for rowNo in range(numberCodevectors):
                        line=f1.readline()
                        splitLine=line.split()
                        #print line
                        data = []
                        for colNo in pylab.arange(numberHarmonic):
                                data.append(float(splitLine[colNo]))
                        print xvalues
                        print data
                        #clear previous plot
                        pylab.clf()
                        lines = pylab.plot(xvalues, data)
                        pylab.ylim(ymin, ymax)
                        pylab.xlim(xmin, xmax)
                        pylab.xlabel('fold symmetry')
                        pylab.ylabel('likelihood')
                        pylab.xticks(xvalues)
                        basefilename = os.path.join(self.params['rundir'], self.spectraTemporalFiles%(int(rowNo/yy),rowNo%xx))
                        pylab.savefig(basefilename+".png", dpi=256, format='png')
	def start(self):
		self.stack = {}
		self.stack['data'] = apStack.getOnlyStackData(self.params['stackid'])
		self.stack['apix'] = apStack.getStackPixelSizeFromStackId(self.params['stackid'])
		self.stack['part'] = apStack.getOneParticleFromStackId(self.params['stackid'])
		self.stack['boxsize'] = apStack.getStackBoxsize(self.params['stackid'])
		self.stack['file'] = os.path.join(self.stack['data']['path']['path'], self.stack['data']['name'])

		### test insert to make sure data is not overwritten
		self.params['runtime'] = 0
		#self.checkDuplicateRefBasedRun()

		### set box size
		self.boxsize = int(math.floor(self.stack['boxsize']/self.params['bin']/2.0))*2

		### convert stack to spider
		spiderstack = self.createSpiderFile()

		### create template stack
		templatestack = self.createTemplateStack()

		### run the alignment
		aligntime = time.time()
		usestack = spiderstack
		oldpartlist = None
		for i in range(self.params['numiter']):
			iternum = i+1
			apDisplay.printColor("\n\nITERATION "+str(iternum), "green")
			alignedstack, partlist = self.runAlignmentGPU(
				usestack, templatestack, spiderstack,
				self.params['xysearch'], self.params['xystep'],
				self.params['firstring'], self.params['lastring'],
				iternum=iternum, oldpartlist=oldpartlist)
			oldpartlist = partlist
			usestack = alignedstack
			templatestack = self.updateTemplateStack(alignedstack, partlist, iternum)
		aligntime = time.time() - aligntime
		apDisplay.printMsg("Alignment time: "+apDisplay.timeString(aligntime))

		### remove large, worthless stack
		spiderstack = os.path.join(self.params['rundir'], "start.spi")
		apDisplay.printMsg("Removing un-aligned stack: "+spiderstack)
		apFile.removeFile(spiderstack, warn=True)

		### convert aligned stack to imagic
		finalspistack = "aligned.spi"
		shutil.move(alignedstack, finalspistack)
		imagicstack = "aligned.hed"
		apFile.removeStack(imagicstack)
		emancmd = "proc2d "+finalspistack+" "+imagicstack
		apEMAN.executeEmanCmd(emancmd, verbose=True)

		### average stack
		apStack.averageStack(imagicstack)

		### calculate resolution for each reference
		apix = self.stack['apix']*self.params['bin']
		self.calcResolution(partlist, imagicstack, apix)

		if self.params['commit'] is True:
			apDisplay.printMsg("committing results to DB")
			self.params['runtime'] = aligntime
			self.insertRefBasedRun(partlist, imagicstack, insert=True)
		else:
			apDisplay.printWarning("not committing results to DB")

		### remove temporary files
		apFile.removeFilePattern("alignments/alignedstack*.spi")
		apFile.removeFile(finalspistack)
Example #22
0
 def postLoopFunctions(self):
     pattern = os.path.join(self.params['rundir'],
                            self.params['sessionname'] + '*.corrected.mrc')
     apFile.removeFilePattern(pattern)
     ctfdb.printCtfSummary(self.params, self.imgtree)