Beispiel #1
0
def boxerRotate(imgfile, parttree, outstack, boxsize, pixlimit=None):
    """
	boxes the particles with expanded size,
	applies a rotation to particle,
	reduces boxsize to requested size,
	and saves them to a imagic file
	"""
    # size needed is sqrt(2)*boxsize, using 1.5 to be extra safe
    bigboxsize = int(math.ceil(1.5 * boxsize))
    imgarray = mrc.read(imgfile)
    imgarray = imagefilter.pixelLimitFilter(imgarray, pixlimit)
    bigboxedparticles = boxerMemory(imgarray, parttree, bigboxsize)

    boxedparticles = []
    boxshape = (boxsize, boxsize)
    apDisplay.printMsg("Rotating particles...")
    for i in range(len(bigboxedparticles)):
        if i % 10 == 0:
            sys.stderr.write(".")
        bigboxpart = bigboxedparticles[i]
        partdict = parttree[i]
        ### add 90 degrees because database angle is from x-axis not y-axis
        angle = partdict['angle'] + 90.0
        rotatepart = ndimage.rotate(bigboxpart,
                                    angle=angle,
                                    reshape=False,
                                    order=1)
        boxpart = imagefilter.frame_cut(rotatepart, boxshape)
        boxedparticles.append(boxpart)
    sys.stderr.write("done\n")
    apImagicFile.writeImagic(boxedparticles, outstack)
    return True
def boxerRotate(imgfile, parttree, outstack, boxsize, pixlimit=None):
	"""
	boxes the particles with expanded size,
	applies a rotation to particle,
	reduces boxsize to requested size,
	and saves them to a imagic file
	"""
	# size needed is sqrt(2)*boxsize, using 1.5 to be extra safe
	bigboxsize = int(math.ceil(1.5*boxsize))
	imgarray = mrc.read(imgfile)
	imgarray = imagefilter.pixelLimitFilter(imgarray, pixlimit)
	bigboxedparticles = boxerMemory(imgarray, parttree, bigboxsize)
	
	boxedparticles = []
	boxshape = (boxsize,boxsize)
	apDisplay.printMsg("Rotating particles...")
	for i in range(len(bigboxedparticles)):
		if i % 10 == 0:
			sys.stderr.write(".")
		bigboxpart = bigboxedparticles[i]
		partdict = parttree[i]
		### add 90 degrees because database angle is from x-axis not y-axis
		angle = partdict['angle']+90.0
		rotatepart = ndimage.rotate(bigboxpart, angle=angle, reshape=False, order=1)
		boxpart = imagefilter.frame_cut(rotatepart, boxshape)
		boxedparticles.append(boxpart)
	sys.stderr.write("done\n")
	apImagicFile.writeImagic(boxedparticles, outstack)
	return True
Beispiel #3
0
def boxer(imgfile, parttree, outstack, boxsize, pixlimit=None):
    """
	boxes the particles and saves them to a imagic file
	"""
    imgarray = mrc.read(imgfile)
    imgarray = imagefilter.pixelLimitFilter(imgarray, pixlimit)
    boxedparticles = boxerMemory(imgarray, parttree, boxsize)
    apImagicFile.writeImagic(boxedparticles, outstack)
    return True
def boxer(imgfile, parttree, outstack, boxsize, pixlimit=None):
	"""
	boxes the particles and saves them to a imagic file
	"""
	imgarray = mrc.read(imgfile)
	imgarray = imagefilter.pixelLimitFilter(imgarray, pixlimit)
	boxedparticles = boxerMemory(imgarray, parttree, boxsize)
	apImagicFile.writeImagic(boxedparticles, outstack)
	return True
Beispiel #5
0
	def start(self):

		### Works
		# read from MRC image/stack
		# read from HED/IMG stack
		# write to MRC image
		# write to MRC stack		
		# write to HED/IMG stack
		# append to MRC stack
		# append to HED/IMG stack
		# filter images
		# implement binning
		# implement normalization

		### needs more testing
		# write pixelsize to new MRC file
		# get apix from MRC header
		# implement proc2d --average
		# implement proc2d --list feature

		### TODO
		# read from SPIDER stack
		# read from SPIDER image
		# read from EMAN2/HDF stack		
		# write to SPIDER image
		# write to SPIDER stack
		# write to EMAN2/HDF stack
		# get apix from HED/IMG header
		# implement proc2d --rotavg
		# implement proc2d --clip

		# determine numParticles to add
		if self.params['last'] is None:
			self.params['last'] = self.inputNumParticles - 1 #stacks start at 0
		elif self.params['last'] > self.inputNumParticles:
			apDisplay.printWarning("Last particle requested (%d) is larger than available particles (%d)"
				%(self.params['last'], self.inputNumParticles))
			self.params['last'] = self.inputNumParticles - 1 #stacks start at 0
		addNumParticles = self.params['last'] - self.params['first'] + 1

		### prepare for an existing file
		existNumParticles = self.outFileStatus()
		self.totalParticles = existNumParticles + addNumParticles

		indata = self.readFileData(self.params['infile'])

		#it more efficient to process X particles and write them to disk rather than
		# write each particle to disk each time.
		#particles are read using a memory map (numpy.memmap), so we can pretend to
		# continuously read all into memory
		particlesPerCycle = self.getParticlesPerCycle(self.params['infile'])

		if self.params['average'] is True:
			summedPartice = numpy.zeros((self.boxsize,self.boxsize))

		processedParticles = []
		if self.params['list']:
			partlist = self.readKeepList()
		else:
			partlist = range(self.params['first'], self.params['first']+addNumParticles)
		count = 0
		for partnum in partlist:
			count += 1
			particle = indata[partnum]
			if self.params['debug'] is True:
				print "---------"
				print "Particle Number: %d of %d"%(partnum, addNumParticles)
			if self.params['pixlimit']:
				self.message("pixlimit: %s"%(self.params['pixlimit']))
				particle = imagefilter.pixelLimitFilter(particle, self.params['pixlimit'])
			if self.params['lowpass']:
				self.message("lowpass: %s"%(self.params['lowpass']))
				particle = imagefilter.lowPassFilter(particle, apix=self.params['apix'], radius=self.params['lowpass'])
			if self.params['highpass']:
				self.message("highpass: %s"%(self.params['highpass']))
				particle = imagefilter.highPassFilter2(particle, self.params['highpass'], apix=self.params['apix'])
			### unless specified, invert the images
			if self.params['inverted'] is True:
				self.message("inverted: %s"%(self.params['inverted']))
				particle = -1.0 * particle
			### clipping
			"""
			if particle.shape != boxshape:
				if boxsize <= particle.shape[0] and boxsize <= particle.shape[1]:
					particle = imagefilter.frame_cut(particle, boxshape)
				else:
					apDisplay.printError("particle shape (%dx%d) is smaller than boxsize (%d)"
						%(particle.shape[0], particle.shape[1], boxsize))
			"""

			### step 3: normalize particles
			#self.normoptions = ('none', 'boxnorm', 'edgenorm', 'rampnorm', 'parabolic') #normalizemethod
			self.message("normalize method: %s"%(self.params['normalizemethod']))
			if self.params['normalizemethod'] == 'boxnorm':
				particle = imagenorm.normStdev(particle)
			elif self.params['normalizemethod'] == 'edgenorm':
				particle = imagenorm.edgeNorm(particle)
			elif self.params['normalizemethod'] == 'rampnorm':
				particle = imagenorm.rampNorm(particle)
			elif self.params['normalizemethod'] == 'parabolic':
				particle = imagenorm.parabolicNorm(particle)

			### step 4: decimate/bin particles if specified
			### binning is last, so we maintain most detail and do not have to deal with binned apix
			if self.params['bin'] > 1:
				particle = imagefun.bin2(particle, self.params['bin'])

			### working above this line
			if self.params['average'] is True:
				summedPartice += particle
			else:
				processedParticles.append(particle)

			if len(processedParticles) == particlesPerCycle:
				### step 5: merge particle list with larger stack
				self.appendParticleListToStackFile(processedParticles, self.params['outfile'])
				sys.stderr.write("%d of %d"%(count, len(partlist)))
				processedParticles = []
		if len(processedParticles) > 0:
			self.appendParticleListToStackFile(processedParticles, self.params['outfile'])
		if self.params['average'] is True:
			avgParticle = summedPartice/count
			self.appendParticleListToStackFile([avgParticle,], self.params['outfile'])
		print "Wrote %d particles to file "%(self.particlesWritten)