本文整理汇总了Python中pyworkflow.em.convert.ImageHandler类的典型用法代码示例。如果您正苦于以下问题:Python ImageHandler类的具体用法?Python ImageHandler怎么用?Python ImageHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _buildDendrogram
def _buildDendrogram(self, leftIndex, rightIndex, index, writeAverages=False, level=0):
""" This function is recursively called to create the dendogram graph(binary tree)
and also to write the average image files.
Params:
leftIndex, rightIndex: the indinxes within the list where to search.
index: the index of the class average.
writeImages: flag to select when to write averages.
From self:
self.dendroValues: the list with the heights of each node
self.dendroImages: image stack filename to read particles
self.dendroAverages: stack name where to write averages
It will search for the max in values list (between minIndex and maxIndex).
Nodes to the left of the max are left childs and the other right childs.
"""
maxValue = self.dendroValues[leftIndex]
maxIndex = 0
for i, v in enumerate(self.dendroValues[leftIndex+1:rightIndex]):
if v > maxValue:
maxValue = v
maxIndex = i+1
m = maxIndex + leftIndex
node = DendroNode(index, maxValue)
ih = ImageHandler()
particleNumber = self.dendroIndexes[m+1]
node.imageList = [particleNumber]
if writeAverages:
node.image = ih.read((particleNumber, self.dendroImages))
def addChildNode(left, right, index):
if right > left:
child = self._buildDendrogram(left, right, index, writeAverages, level+1)
node.addChild(child)
node.length += child.length
node.imageList += child.imageList
if writeAverages:
node.image += child.image
del child.image # Allow to free child image memory
if rightIndex > leftIndex + 1 and level < self.dendroMaxLevel:
addChildNode(leftIndex, m, 2*index)
addChildNode(m+1, rightIndex, 2*index+1)
node.avgCount = self.dendroAverageCount + 1
self.dendroAverageCount += 1
node.path = '%[email protected]%s' % (node.avgCount, self.dendroAverages)
if writeAverages:
#TODO: node['image'] /= float(node['length'])
#node.image.inplaceDivide(float(node.length)) #FIXME: not working, noisy images
avgImage = node.image / float(node.length)
ih.write(avgImage, (node.avgCount, self.dendroAverages))
fn = self._getTmpPath('doc_class%03d.stk' % index)
doc = SpiderDocFile(fn, 'w+')
for i in node.imageList:
doc.writeValues(i)
doc.close()
return node
示例2: importVolumesStep
def importVolumesStep(self, pattern, samplingRate):
""" Copy images matching the filename pattern
Register other parameters.
"""
self.info("Using pattern: '%s'" % pattern)
# Create a Volume template object
vol = Volume()
vol.setSamplingRate(self.samplingRate.get())
copyOrLink = self.getCopyOrLink()
imgh = ImageHandler()
volSet = self._createSetOfVolumes()
volSet.setSamplingRate(self.samplingRate.get())
for fileName, fileId in self.iterFiles():
dst = self._getExtraPath(basename(fileName))
copyOrLink(fileName, dst)
x, y, z, n = imgh.getDimensions(dst)
# First case considers when reading mrc without volume flag
# Second one considers single volumes (not in stack)
if (z == 1 and n != 1) or (z !=1 and n == 1):
vol.setObjId(fileId)
vol.setLocation(dst)
volSet.append(vol)
else:
for index in range(1, n+1):
vol.cleanObjId()
vol.setLocation(index, dst)
volSet.append(vol)
if volSet.getSize() > 1:
self._defineOutputs(outputVolumes=volSet)
else:
self._defineOutputs(outputVolume=vol)
示例3: ImagePreviewDialog
class ImagePreviewDialog(PreviewDialog):
def _beforePreview(self):
self.dim = 256
self.previewLabel = ''
def _createPreview(self, frame):
""" Should be implemented by subclasses to
create the items preview.
"""
from pyworkflow.gui.matplotlib_image import ImagePreview
self.preview = ImagePreview(frame, self.dim, label=self.previewLabel)
self.preview.grid(row=0, column=0)
def _itemSelected(self, obj):
index = obj.getIndex()
filename = obj.getFileName()
if index:
filename = "%[email protected]%s" % (index, filename)
# self.image = xmipp.Image()
self.image = ImageHandler()._img
try:
self.image.readPreview(filename, self.dim)
if filename.endswith('.psd'):
self.image.convertPSD()
self.Z = self.image.getData()
except Exception, e:
from pyworkflow.gui.matplotlib_image import getPngData
self.Z = getPngData(findResource('no-image.png'))
dialog.showError("Input particles", "Error reading image <%s>" % filename, self)
self.preview.updateData(self.Z)
示例4: convertInputStep
def convertInputStep(self, particlesId, volId):
""" Write the input images as a Xmipp metadata file.
particlesId: is only need to detect changes in
input particles and cause restart from here.
"""
inputParticles = self.inputParticles.get()
inputVolume = self.inputVolume.get()
writeSetOfParticles(inputParticles, self._getExpParticlesFn())
img = ImageHandler()
img.convert(inputVolume, self._getInputVolFn())
if self._useSeveralClasses():
# Scale particles
Xdim = inputParticles.getXDim()
Ts = inputParticles.getSamplingRate()
newTs = self.targetResolution.get() * 0.4
newTs = max(Ts, newTs)
newXdim = Xdim * Ts / newTs
self.runJob("xmipp_image_resize",
"-i %s -o %s --save_metadata_stack %s --fourier %d" %
(self._getExpParticlesFn(),
self._getTmpPath('scaled_particles.stk'),
self._getTmpPath('scaled_particles.xmd'),
newXdim))
# Scale volume
Xdim = inputVolume.getXDim()
if Xdim != newXdim:
self.runJob("xmipp_image_resize", "-i %s --dim %d"
% (self._getInputVolFn(), newXdim), numberOfMpi=1)
示例5: test_readDM4
def test_readDM4(self):
""" Check we can read dm4 files (using EMAN)
"""
micFn = self.dsFormat.getFile('SuperRef_c3-adp-se-xyz-0228_001.dm4')
ih = ImageHandler()
# Check that we can read the dimensions of the dm4 file:
EXPECTED_SIZE = (7676, 7420, 1, 1)
self.assertEqual(ih.getDimensions(micFn), EXPECTED_SIZE)
# We could even convert to an mrc file:
outSuffix = pwutils.replaceBaseExt(micFn, 'mrc')
outFn = join('/tmp', outSuffix)
print "Converting: \n%s -> %s" % (micFn, outFn)
ih.convert(micFn, outFn)
self.assertTrue(os.path.exists(outFn))
self.assertTrue(pwutils.getFileSize(outFn) > 0)
# Check dimensions are still the same:
self.assertEqual(ih.getDimensions(outFn), EXPECTED_SIZE)
# Clean up tmp files
pwutils.cleanPath(outFn)
示例6: _validateImages
def _validateImages(self):
errors = []
ih = ImageHandler()
for imgFn, _ in self.iterFiles():
if isdir(imgFn):
errors.append("Folders can not be selected.")
errors.append(' %s' % imgFn)
else:
# Check if images are correct by reading the header of the
# imported files:
# Exceptions:
# - Compressed movies (bz2 or tbz extensions)
# - Importing in streaming, since files may be incomplete
# - Bad characters in path [':' ,'%', '#']
if (not self.dataStreaming and
not (imgFn.endswith('bz2') or
imgFn.endswith('tbz') or
ih.isImageFile(imgFn))):
if not errors: # if empty add the first line
errors.append("Error reading the following images:")
errors.append(' %s' % imgFn)
errors += ProtImportImages.validatePath(imgFn)
return errors
示例7: createOutputStep
def createOutputStep(self):
particles = self.inputParticles.get()
# Generate the SetOfAlignmet
alignedSet = self._createSetOfParticles()
alignedSet.copyInfo(particles)
inputMd = self._getPath('aligned_particles.xmd')
alignedSet.copyItems(particles,
updateItemCallback=self._updateItem,
itemDataIterator=iterMdRows(inputMd))
# Remove alignment 2D
alignedSet.setAlignment(ALIGN_NONE)
# Define the output average
avgFile = self._getExtraPath("average.xmp")
imgh = ImageHandler()
avgImage = imgh.computeAverage(alignedSet)
avgImage.write(avgFile)
avg = Particle()
avg.setLocation(1, avgFile)
avg.copyInfo(alignedSet)
self._defineOutputs(outputAverage=avg)
self._defineSourceRelation(self.inputParticles, avg)
self._defineOutputs(outputParticles=alignedSet)
self._defineSourceRelation(self.inputParticles, alignedSet)
示例8: prepareMask
def prepareMask(self,maskObject,fnMask,TsMaskOut,XdimOut):
img=ImageHandler()
img.convert(maskObject, fnMask)
self.runJob('xmipp_image_resize',"-i %s --factor %f"%(fnMask,maskObject.getSamplingRate()/TsMaskOut),numberOfMpi=1)
maskXdim, _, _, _ =img.getDimensions((1,fnMask))
if XdimOut!=maskXdim:
self.runJob('xmipp_transform_window',"-i %s --size %d"%(fnMask,XdimOut),numberOfMpi=1)
self.runJob('xmipp_transform_threshold',"-i %s --select below 0.5 --substitute binarize"%fnMask,numberOfMpi=1)
示例9: _runBeforePreWhitening
def _runBeforePreWhitening(self):
prot = self.form.protocol
# Convert input volumes
ih = ImageHandler()
ih.convert(prot.inputVolume.get(), join(self.workingDir, 'volume1.map'))
if prot.useSplitVolume:
ih.convert(prot.splitVolume.get(), join(self.workingDir, 'volume2.map'))
self.results = prot.runResmap(self.workingDir, wizardMode=True)
示例10: _particlesToEmx
def _particlesToEmx(emxData, partSet, micSet=None, **kwargs):
""" Write a SetOfMicrograph as expected in EMX format
Params:
micSet: input set of micrographs
filename: the EMX file where to store the micrographs information.
micSet: micrographs set associated with the particles
**kwargs: writeImages: if set to False, only coordinates are exported.
imagesStack: if passed all images will be output into a single
stack file.
imagesPrefix: used when not imagesStack is passed. A different
stack will be created per micrograph.
"""
writeImages = kwargs.get('writeImages', True)
imagesPrefix = kwargs.get('imagesPrefix', None)
micDict = {}
# Use singleMic for count all particles to be written to a single stack
imagesStack = kwargs.get('imagesStack', None)
singleMic = Micrograph()
singleMic.setFileName(imagesStack)
singleMic.counter = pwobj.Integer(0)
def _getMicKey(particle):
coord = particle.getCoordinate()
if coord is None or coord.getMicName() is None:
return '%05d' % particle.getMicId()
else:
return pwutils.removeExt(coord.getMicName())
def _getLocation(particle):
if imagesStack is not None:
mic = singleMic
else:
micKey = _getMicKey(particle)
if micKey not in micDict:
mic = Micrograph()
mic.setFileName(join(imagesPrefix, 'particles_%s.mrc' % micKey))
mic.counter = pwobj.Integer(0)
micDict[micKey] = mic
else:
mic = micDict[micKey]
# Count one more particle assigned to this micrograph
mic.counter.increment()
return (mic.counter.get(), mic.getFileName())
ih = ImageHandler()
partAlign = partSet.getAlignment()
for particle in partSet:
if writeImages:
newLoc = _getLocation(particle)
ih.convert(particle, newLoc)
localFn = basename(newLoc[1])
particle.setLocation(newLoc[0], localFn)
emxObj = _particleToEmx(emxData, particle, micSet, partAlign)
else:
emxObj = _coordinateToEmx(emxData, particle, micSet)
emxData.addObject(emxObj)
示例11: projectInitialVolume
def projectInitialVolume(self):
fnOutputInitVolume=self._getTmpPath("initialVolume.vol")
img = ImageHandler()
img.convert(self.initialVolume.get(), fnOutputInitVolume)
self.runJob("xmipp_image_resize","-i %s --dim %d %d"%(fnOutputInitVolume,self.Xdim2,self.Xdim2))
fnGallery=self._getTmpPath('gallery_InitialVolume.stk')
fnOutputReducedClass = self._getExtraPath("reducedClasses.xmd")
self.runJob("xmipp_angular_project_library", "-i %s -o %s --sampling_rate %f --sym %s --method fourier 1 0.25 bspline --compute_neighbors --angular_distance -1 --experimental_images %s"\
%(fnOutputInitVolume,fnGallery,self.angularSampling.get(),self.symmetryGroup.get(),fnOutputReducedClass))
示例12: importImagesStep
def importImagesStep(self, pattern, voltage, sphericalAberration, amplitudeContrast, magnification):
""" Copy images matching the filename pattern
Register other parameters.
"""
self.info("Using pattern: '%s'" % pattern)
createSetFunc = getattr(self, '_create' + self._outputClassName)
imgSet = createSetFunc()
imgSet.setIsPhaseFlipped(self.haveDataBeenPhaseFlipped.get())
acquisition = imgSet.getAcquisition()
self.fillAcquisition(acquisition)
# Call a function that should be implemented by each subclass
self.setSamplingRate(imgSet)
outFiles = [imgSet.getFileName()]
imgh = ImageHandler()
img = imgSet.ITEM_TYPE()
img.setAcquisition(acquisition)
n = 1
copyOrLink = self.getCopyOrLink()
for i, (fileName, fileId) in enumerate(self.iterFiles()):
dst = self._getExtraPath(basename(fileName))
copyOrLink(fileName, dst)
# Handle special case of Imagic images, copying also .img or .hed
self.handleImgHed(copyOrLink, fileName, dst)
if self._checkStacks:
_, _, _, n = imgh.getDimensions(dst)
if n > 1:
for index in range(1, n+1):
img.cleanObjId()
img.setMicId(fileId)
img.setFileName(dst)
img.setIndex(index)
imgSet.append(img)
else:
img.setObjId(fileId)
img.setFileName(dst)
self._fillMicName(img, fileName) # fill the micName if img is a Micrograph.
imgSet.append(img)
outFiles.append(dst)
sys.stdout.write("\rImported %d/%d" % (i+1, self.numberOfFiles))
sys.stdout.flush()
print "\n"
args = {}
outputSet = self._getOutputName()
args[outputSet] = imgSet
self._defineOutputs(**args)
return outFiles
示例13: convertInputStep
def convertInputStep(self, volLocation1, volLocation2=None):
""" Convert input volume to .mrc as expected by ResMap.
Params:
volLocation1: a tuple containing index and filename of the input volume.
volLocation2: if not None, a tuple like volLocation1 for the split volume.
"""
ih = ImageHandler()
ih.convert(volLocation1, self._getPath('volume1.map'))
if volLocation2 is not None:
ih.convert(volLocation2, self._getPath('volume2.map'))
示例14: _beforePreWhitening
def _beforePreWhitening(protocol, dir):
from pyworkflow.em.convert import ImageHandler
# Convert input volumes
ih = ImageHandler()
inputVolume = protocol.inputVolume.get()
path = join(dir, 'volume1.map')
print path
ih.convert(inputVolume, path)
if protocol.useSplitVolume:
ih.convert(protocol.splitVolume.get(), join(dir, 'volume2.map'))
return protocol.runResmap(dir, wizardMode=True)
示例15: createMaskStep
def createMaskStep(self):
""" Create a circular mask in Imagic format. """
inputParticles = self.inputParticles.get()
radius = self.radius.get()
if self.maskType.get() == 0:
if radius < 0: # usually -1
radiusMask = inputParticles.getDim()[0] / 2 # use half of input dim
else:
radiusMask = radius
outMask = self._getTmpPath('mask.img')
ih = ImageHandler()
ih.createCircularMask(radiusMask, inputParticles.getFirstItem(), outMask)