Ejemplo n.º 1
0
def arrayToJpegPlusPeak(numer, outfile, peak=None, normalize=True):
    """
	takes a numpy and writes a JPEG
	best for micrographs and photographs
	"""
    if normalize:
        numer = imagenorm.maxNormalizeImage(numer)
    else:
        numer = numer * 255
    image = _arrayToImage(numer)
    image = image.convert("RGB")

    if peak != None:
        draw = ImageDraw.Draw(image)
        peak2 = numpy.asarray(peak)
        for i in range(2):
            if peak[i] < 0:
                peak2[i] = (numer.shape)[i] + peak[i]
            elif peak[i] > (numer.shape)[i]:
                peak2[i] = peak[i] - (numer.shape)[i]
        drawPeak(peak2, draw, numer.shape)

    print " ... writing JPEG: ", outfile
    image.save(outfile, "JPEG", quality=85)

    return
def arrayToJpegPlusPeak(numer, outfile, peak=None, normalize=True):
        """
        takes a numpy and writes a JPEG
        best for micrographs and photographs
        """
        if normalize:
                numer = imagenorm.maxNormalizeImage(numer)
        else:
                numer = numer*255
        image = _arrayToImage(numer)
        image = image.convert("RGB")

        if peak != None:
                draw = ImageDraw.Draw(image)
                peak2 = numpy.asarray(peak)
                for i in range(2):
                        if peak[i] < 0:
                                peak2[i] = (numer.shape)[i] + peak[i]
                        elif peak[i] > (numer.shape)[i]:
                                peak2[i] = peak[i] - (numer.shape)[i]
                drawPeak(peak2, draw, numer.shape)

        print " ... writing JPEG: ",outfile
        image.save(outfile, "JPEG", quality=85)

        return
Ejemplo n.º 3
0
def arrayToImage(numer, normalize=True, stdevLimit=3.0):
    """
	takes a numpy and writes a JPEG
	best for micrographs and photographs
	"""
    if normalize:
        numer = imagenorm.maxNormalizeImage(numer, stdevLimit)
    else:
        numer = numer * 255
    image = _arrayToImage(numer)
    return image
def arrayToImage(numer, normalize=True, stdevLimit=3.0):
        """
        takes a numpy and writes a JPEG
        best for micrographs and photographs
        """
        if normalize:
                numer = imagenorm.maxNormalizeImage(numer, stdevLimit)
        else:
                numer = numer*255
        image = _arrayToImage(numer)
        return image
Ejemplo n.º 5
0
def arrayToPng(numer, filename, normalize=True, msg=True):
    """
	takes a numpy and writes a PNG
	best for masks and line art
	"""
    if normalize:
        numer = imagenorm.maxNormalizeImage(numer)
    else:
        numer = numer * 255
    image = _arrayToImage(numer)
    if msg is True:
        apDisplay.printMsg("writing PNG: " + apDisplay.short(filename))
    image.save(filename, "PNG")
    return
Ejemplo n.º 6
0
def arrayToJpeg(numer, filename, normalize=True, msg=True, quality=85):
    """
	takes a numpy and writes a JPEG
	best for micrographs and photographs
	"""
    if normalize:
        numer = imagenorm.maxNormalizeImage(numer)
    else:
        numer = numer * 255
    image = _arrayToImage(numer)
    if msg is True:
        apDisplay.printMsg("writing JPEG: " + apDisplay.short(filename))
    image.save(filename, "JPEG", quality=quality)
    return
def arrayToPng(numer, filename, normalize=True, msg=True):
        """
        takes a numpy and writes a PNG
        best for masks and line art
        """
        if normalize:
                numer = imagenorm.maxNormalizeImage(numer)
        else:
                numer = numer*255
        image = _arrayToImage(numer)
        if msg is True:
                apDisplay.printMsg("writing PNG: "+apDisplay.short(filename))
        image.save(filename, "PNG")
        return
def arrayToJpeg(numer, filename, normalize=True, msg=True, quality=85):
        """
        takes a numpy and writes a JPEG
        best for micrographs and photographs
        """
        if normalize:
                numer = imagenorm.maxNormalizeImage(numer)
        else:
                numer = numer*255
        image = _arrayToImage(numer)
        if msg is True:
                apDisplay.printMsg("writing JPEG: "+apDisplay.short(filename))
        image.save(filename, "JPEG", quality=quality)
        return
	def updateTemplateStack(self, alignedstack, partlist, iternum):
		"""
		Function to Average particles that match template to create new templates
		"""

		#templatestr = os.path.join(self.params['rundir'], "templates/filt*.mrc")
		#oldfilelist = glob.glob(templatestr)

		### clear old stacks
		templatestack = os.path.join(self.params['rundir'], ("templatestack%02d.spi" % iternum))
		apFile.removeFile(templatestack, warn=True)

		### calculate correlation stats
		statlist = []
		for partdict in partlist:
			statlist.append(partdict['score'])
		statlist.sort()
		cutoff = statlist[int(0.1*len(partlist))]*0.999
		apDisplay.printMsg("using a 10% correlation cutoff of: "+str(round(cutoff)))

		### init list of files
		keeplists = []
		for templatenum in range(1, self.params['numtemplate']+1):
			f = open(("templates/keeplist%02d-%02d.list" % (iternum, templatenum)), "w")
			keeplists.append(f)
		junk = open(("templates/rejectlist%02d.list" % (iternum)), "w")

		### allocate particles to keep lists
		numjunk = 0
		for partdict in partlist:
			#EMAN lists start at zero
			if partdict['score'] > cutoff:
				keeplists[partdict['template']-1].write(str(partdict['num']-1)+"\n")
			else:
				numjunk+=1
				junk.write(str(partdict['num']-1)+"\n")
		for f in keeplists:
			f.close()
		junk.close()

		### average junk for fun
		apDisplay.printMsg(str(numjunk)+" particles were marked as junk")
		if numjunk == 0:
			junk = open(("templates/rejectlist%02d.list" % (iternum)), "w")
			randpart = random.random()*(len(partlist)-1)
			junk.write(str(randpart)+"\n")
			junk.close()
		junklist = "templates/rejectlist%02d.list" % (iternum)
		junkmrcfile = "templates/junkavg%02d.mrc" % (iternum)
		emancmd  = ("proc2d "+alignedstack+" "+junkmrcfile
			+" list="+junklist
			+" edgenorm average norm=0,1 ")
		apEMAN.executeEmanCmd(emancmd, showcmd=False)

		### create averaged templates
		filelist = []
		for templatenum in range(1, self.params['numtemplate']+1):
			keeplist = "templates/keeplist%02d-%02d.list" % (iternum, templatenum)
			mrcfile = "templates/templateavg%02d-%02d.mrc" % (iternum, templatenum)
			if os.path.isfile(keeplist) and os.stat(keeplist)[6] > 1:
				emancmd  = ("proc2d "+alignedstack+" "+mrcfile
					+" list="+keeplist
					+" edgenorm average norm=0,1 ")
				if self.params['csym'] is not None:
					emancmd += "sym=c%d"%(self.params['csym'])
				apEMAN.executeEmanCmd(emancmd, showcmd=False)
			else:
				apDisplay.printWarning("No particles aligned to template "+str(templatenum))
				if numjunk == 0:
					apDisplay.printWarning("Using random particle as new template")
				else:
					apDisplay.printWarning("Using worst 10% of particles as new template")
				lasttemplate = "templates/templateavg%02d-%02d.mrc" % (iternum-1, templatenum)
				if not os.path.isfile(lasttemplate):
					lasttemplate = "templates/scaledTemplate%d.mrc" % (templatenum)
				lastdata = imagenorm.maxNormalizeImage(imagefile.mrcToArray(lasttemplate))
				junkdata = imagenorm.maxNormalizeImage(imagefile.mrcToArray(junkmrcfile))
				imagefile.arrayToMrc((lastdata+3.0*junkdata), "temp.mrc")
				emancmd  = ("proc2d temp.mrc "+mrcfile
					+" addnoise=1.5 "
					+" edgenorm norm=0,1 ")
				apEMAN.executeEmanCmd(emancmd, showcmd=False)
			filelist.append(mrcfile)

		### create new template stack
		for mrcfile in filelist:
			emancmd  = ("proc2d "+mrcfile+" "+templatestack
				+" clip="+str(self.boxsize)+","+str(self.boxsize)
				+" edgenorm norm=0,1 spider ")
			apEMAN.executeEmanCmd(emancmd, showcmd=False)

		return templatestack
	def updateTemplateStack(self, alignedstack, partlist, iternum):
		"""
		Function to Average particles that match template to create new templates
		"""

		#templatestr = os.path.join(self.params['rundir'], "templates/filt*.mrc")
		#oldfilelist = glob.glob(templatestr)

		### clear old stacks
		templatestack = os.path.join(self.params['rundir'], ("templatestack%02d.spi" % iternum))
		apFile.removeFile(templatestack, warn=True)

		### calculate correlation stats
		statlist = []
		for partdict in partlist:
			statlist.append(partdict['score'])
		statlist.sort()
		cutoff = statlist[int(0.1*len(partlist))]*0.999
		apDisplay.printMsg("using a 10% correlation cutoff of: "+str(round(cutoff)))

		### init list of files
		keeplists = []
		for templatenum in range(1, self.params['numtemplate']+1):
			f = open(("templates/keeplist%02d-%02d.list" % (iternum, templatenum)), "w")
			keeplists.append(f)
		junk = open(("templates/rejectlist%02d.list" % (iternum)), "w")

		### allocate particles to keep lists
		numjunk = 0
		for partdict in partlist:
			#EMAN lists start at zero
			if partdict['score'] > cutoff:
				keeplists[partdict['template']-1].write(str(partdict['num']-1)+"\n")
			else:
				numjunk+=1
				junk.write(str(partdict['num']-1)+"\n")
		for f in keeplists:
			f.close()
		junk.close()

		### average junk for fun
		apDisplay.printMsg(str(numjunk)+" particles were marked as junk")
		if numjunk == 0:
			junk = open(("templates/rejectlist%02d.list" % (iternum)), "w")
			randpart = random.random()*(len(partlist)-1)
			junk.write(str(randpart)+"\n")
			junk.close()
		junklist = "templates/rejectlist%02d.list" % (iternum)
		junkmrcfile = "templates/junkavg%02d.mrc" % (iternum)
		emancmd  = ("proc2d "+alignedstack+" "+junkmrcfile
			+" list="+junklist
			+" edgenorm average norm=0,1 ")
		apEMAN.executeEmanCmd(emancmd, showcmd=False)

		### create averaged templates
		filelist = []
		for templatenum in range(1, self.params['numtemplate']+1):
			keeplist = "templates/keeplist%02d-%02d.list" % (iternum, templatenum)
			mrcfile = "templates/templateavg%02d-%02d.mrc" % (iternum, templatenum)
			if os.path.isfile(keeplist) and os.stat(keeplist)[6] > 1:
				emancmd  = ("proc2d "+alignedstack+" "+mrcfile
					+" list="+keeplist
					+" edgenorm average norm=0,1 ")
				if self.params['csym'] is not None:
					emancmd += "sym=c%d"%(self.params['csym'])
				apEMAN.executeEmanCmd(emancmd, showcmd=False)
			else:
				apDisplay.printWarning("No particles aligned to template "+str(templatenum))
				if numjunk == 0:
					apDisplay.printWarning("Using random particle as new template")
				else:
					apDisplay.printWarning("Using worst 10% of particles as new template")
				lasttemplate = "templates/templateavg%02d-%02d.mrc" % (iternum-1, templatenum)
				if not os.path.isfile(lasttemplate):
					lasttemplate = "templates/scaledTemplate%d.mrc" % (templatenum)
				lastdata = imagenorm.maxNormalizeImage(imagefile.mrcToArray(lasttemplate))
				junkdata = imagenorm.maxNormalizeImage(imagefile.mrcToArray(junkmrcfile))
				imagefile.arrayToMrc((lastdata+3.0*junkdata), "temp.mrc")
				emancmd  = ("proc2d temp.mrc "+mrcfile
					+" addnoise=1.5 "
					+" edgenorm norm=0,1 ")
				apEMAN.executeEmanCmd(emancmd, showcmd=False)
			filelist.append(mrcfile)

		### create new template stack
		for mrcfile in filelist:
			emancmd  = ("proc2d "+mrcfile+" "+templatestack
				+" clip="+str(self.boxsize)+","+str(self.boxsize)
				+" edgenorm norm=0,1 spider ")
			apEMAN.executeEmanCmd(emancmd, showcmd=False)

		return templatestack