Esempio n. 1
0
    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)
    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)
Esempio n. 3
0
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 = "%03d@%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)
Esempio n. 4
0
    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)
    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 = '%d@%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
Esempio n. 6
0
    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)
Esempio n. 7
0
    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
 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)
Esempio n. 9
0
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)
Esempio n. 10
0
 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)
Esempio n. 11
0
 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))
Esempio n. 12
0
 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
Esempio n. 13
0
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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    def testExistLocation(self):
        volFn = self.dataset.getFile('volumes/volume_1_iter_002.mrc')

        ih = ImageHandler()
        # Test the volume filename exists
        self.assertTrue(ih.existsLocation(volFn))
        # Test missing filename
        self.assertFalse(ih.existsLocation(volFn.replace('.mrc', '_fake.mrc')))
        # Test the :mrc is append when used as volume
        newFn = ih.getVolFileName(volFn)
        self.assertEqual(newFn, volFn + ":mrc")
        # Test that the new filename still exists even with the :mrc suffix
        self.assertTrue(ih.existsLocation(newFn))
 def convertStep(self, imgsFn):
     from convert import writeSetOfClasses2D, writeSetOfParticles
     imgSet = self.inputSet.get()
     if isinstance(imgSet, SetOfClasses2D):
         writeSetOfClasses2D(imgSet, self.imgsFn, writeParticles=True)
     else:
         writeSetOfParticles(imgSet, self.imgsFn)
     from pyworkflow.em.convert import ImageHandler
     img = ImageHandler()
     fnVol = self._getTmpPath("volume.vol")
     img.convert(self.inputVolume.get(), fnVol)
     xdim=self.inputVolume.get().getDim()[0]
     if xdim!=self._getDimensions():
         self.runJob("xmipp_image_resize","-i %s --dim %d"%(fnVol,self._getDimensions()))
    def convertInputStep(self, particlesId):
        """ Write the input images as a Xmipp metadata file. 
        particlesId: is only need to detect changes in
        input particles and cause restart from here.
        """

        writeSetOfParticles(self.inputParticles.get(), 
                            self._getPath('input_particles.xmd'))
        
        if self.doWiener.get():
            params  =  '  -i %s' % self._getPath('input_particles.xmd')
            params +=  '  -o %s' % self._getExtraPath('corrected_ctf_particles.stk')
            params +=  '  --save_metadata_stack %s' % self._getExtraPath('corrected_ctf_particles.xmd')
            params +=  '  --pad %s' % self.padding_factor.get()
            params +=  '  --wc %s' % self.wiener_constant.get()
            params +=  '  --sampling_rate %s' % self.inputParticles.get().getSamplingRate()

            if self.inputParticles.get().isPhaseFlipped():
                params +=  '  --phase_flipped '
            
            if self.correctEnvelope:
                params +=  '  --correct_envelope '
                
            nproc = self.numberOfMpi.get()
            nT=self.numberOfThreads.get()
    
            self.runJob('xmipp_ctf_correct_wiener2d',
                        params)
        
        newTs, newXdim = self._getModifiedSizeAndSampling()
                
        if self.doWiener.get():
            params =  '  -i %s' % self._getExtraPath('corrected_ctf_particles.xmd')
        else :
            params =  '  -i %s' % self._getPath('input_particles.xmd')
            
        params +=  '  -o %s' % self._getExtraPath('scaled_particles.stk')
        params +=  '  --save_metadata_stack %s' % self._getExtraPath('scaled_particles.xmd')
        params +=  '  --fourier %d' % newXdim
        
        self.runJob('xmipp_image_resize',params)
        
        from pyworkflow.em.convert import ImageHandler
        img = ImageHandler()
        img.convert(self.inputVolumes.get(), self._getExtraPath("volume.vol"))
        Xdim = self.inputVolumes.get().getDim()[0]
        if Xdim!=newXdim:
            self.runJob("xmipp_image_resize","-i %s --dim %d"%\
                        (self._getExtraPath("volume.vol"),
                        newXdim), numberOfMpi=1)
Esempio n. 18
0
 def _beforePreview(self):
     ImagePreviewDialog._beforePreview(self)
     self.lastObj = None
     self.rightPreviewLabel = "Filtered"
     self.message = "Computing filtered image..."
     self.previewLabel = "Image"
     self.rightImage = ImageHandler()._img
 def buildDendrogram(self, writeAverages=False):
     """ Parse Spider docfile with the information to build the dendogram.
     Params:
         dendroFile: docfile with a row per image. 
              Each row contains the image id and the height.
     """ 
     dendroFile = self._getFileName('dendroDoc')
     # Dendrofile is a docfile with at least 3 data colums (class, height, id)
     doc = SpiderDocFile(dendroFile)
     values = []
     indexes = []
     for c, h, _ in doc.iterValues(): 
         indexes.append(c)
         values.append(h)
     doc.close()
     
     self.dendroValues = values
     self.dendroIndexes = indexes
     self.dendroImages = self._getFileName('particles')
     self.dendroAverages = self._getFileName('averages')
     self.dendroAverageCount = 0 # Write only the number of needed averages
     self.dendroMaxLevel = 10 # FIXME: remove hard coding if working the levels
     self.ih = ImageHandler()
     
     return self._buildDendrogram(0, len(values)-1, 1, writeAverages)
Esempio n. 20
0
 def _beforePreview(self):
     ImagePreviewDialog._beforePreview(self)
     self.lastObj = None
     self.rightPreviewLabel = "PSD"
     self.message = "Computing PSD..."
     self.previewLabel = "Micrograph"
     self.rightImage = ImageHandler()._img
Esempio n. 21
0
 def _beforePreview(self):
     ImagePreviewDialog._beforePreview(self)
     self.lastObj = None
     self.rightPreviewLabel = "Filtered particle"
     self.message = "Filtering particle..."
     self.previewLabel = "Particle"
     self.rightImage = ImageHandler()._img
Esempio n. 22
0
    def _insertAllSteps(self):

        self._params = {}
        # diameter must be passed in Armstrongs and therefore should be converted
        self._params['diam'] = self.diameter.get() * self.getInputMicrographs().getSamplingRate()
        # self._params['num-slices'] = self.numberSizes.get()
        # self._params['size-range'] = self.sizeRange.get()
        self._params['apix'] = self.inputMicrographs.get().getSamplingRate()
        self._params['thresh'] = self.threshold.get()
        # self._params['max-thresh'] = self.maxThreshold.get()
        # self._params['max-area'] = self.maxArea.get()
        # self._params['max-peaks'] = self.maxPeaks.get()

        args = ""
        for par, val in self._params.iteritems():
            args += " --%s=%s" % (par, str(val))

        if self.invert:
            args += " --invert"

        args += " " + self.extraParams.get('')

        deps = [] # Store all steps ids, final step createOutput depends on all of them

        ih = ImageHandler()

        for mic in self.inputMicrographs.get():
            # Create micrograph folder
            micName = mic.getFileName()
            micDir = self._getTmpPath(removeBaseExt(micName))
            makePath(micDir)

            # If needed convert micrograph to mrc format, otherwise link it
            if getExt(micName) != ".mrc":
                fnMicBase = replaceBaseExt(micName, 'mrc')
                inputMic = join(micDir, fnMicBase)
                ih.convert(mic.getLocation(), inputMic)
            else:
                inputMic = join(micDir, basename(micName))
                createLink(micName, inputMic)

            # Insert step to execute program
            stepId = self._insertFunctionStep('executeDogpickerStep', inputMic, args)
            deps.append(stepId)


        self._insertFinalSteps(deps)
Esempio n. 23
0
    def prepareReferences(self,fnDir,TsCurrent,targetResolution):
        print "Preparing references to sampling rate=",TsCurrent
        fnMask=''
        newXdim=self.readInfoField(fnDir,"size",xmipp.MDL_XSIZE)
        if self.nextMask.hasValue():
            fnMask=join(fnDir,"mask.vol")
            self.prepareMask(self.nextMask.get(), fnMask, TsCurrent, newXdim)

        fnReferenceVol=join(fnDir,"volumeRef.vol")
        img=ImageHandler()
        img.convert(self.inputVolume.get(), fnReferenceVol)
        Xdim=self.inputVolume.get().getDim()[0]
        if Xdim!=newXdim:
            self.runJob("xmipp_image_resize","-i %s --fourier %d"%(fnReferenceVol,newXdim),numberOfMpi=1)
        
        if fnMask!='':
            self.runJob('xmipp_image_operate','-i %s --mult %s'%(fnReferenceVol,fnMask),numberOfMpi=1)
Esempio n. 24
0
 def _beforePreview(self):
     imgLocation = self.protocolParent.inputImage.get().getLocation()
     self.dim = ImageHandler().getDimensions(imgLocation)[0]
     self.lastObj = None
     self.rightPreviewLabel = "Final mask"
     self.message = "Generating mask..."
     self.ih = ImageHandler()
     self.rightImage = self.ih.createImage()
Esempio n. 25
0
def writeSetOfImages(imgSet, stackFn, selFn):
    """ This function will write a SetOfMicrographs as a Spider stack and selfile.
    Params:
        imgSet: the SetOfMicrograph instance.
        stackFn: the filename where to write the stack.
        selFn: the filename of the Spider selection file.
    """
    ih = ImageHandler()
    doc = SpiderDocFile(selFn, 'w+')
    
    for i, img in enumerate(imgSet):
        ih.convert(img, (i+1, stackFn))
        doc.writeValues(i+1)
        
    doc.close()
    
    convertEndian(stackFn, imgSet.getSize())
Esempio n. 26
0
    def generateSplittedVolumes(self):
        inputParticles = self.directionalClasses.get()
        Xdim = inputParticles.getDimensions()[0]
        fnMask = ""
        if self.mask.hasValue():
            fnMask = self._getExtraPath("mask.vol")
            img=ImageHandler()
            img.convert(self.mask.get(), fnMask)
            self.runJob('xmipp_image_resize',"-i %s --dim %d"%(fnMask,Xdim),numberOfMpi=1)
            self.runJob('xmipp_transform_threshold',"-i %s --select below 0.5 --substitute binarize"%fnMask,numberOfMpi=1)

        args="-i %s --oroot %s --Nrec %d --Nsamples %d --sym %s --alpha %f"%\
             (self._getExtraPath("directionalClasses.xmd"),self._getExtraPath("split"),self.Nrec.get(),self.Nsamples.get(),
              self.symmetryGroup.get(), self.alpha.get())
        if fnMask!="":
            args+=" --mask binary_file %s"%fnMask
        self.runJob("xmipp_classify_first_split",args)
Esempio n. 27
0
    def test_convertMovie(self):
        """Check movie conversion"""
        movFn = self.dsFormat.getFile('qbeta/qbeta.mrc') + ":mrcs"
        
        ih = ImageHandler()
        # Check that we can read the dimensions of the dm4 file:
        EXPECTED_SIZE = (4096, 4096, 1, 7)
        EXPECTED_DT = ImageHandler.DT_USHORT

        self.assertEqual(ih.getDimensions(movFn), EXPECTED_SIZE)
        self.assertEqual(ih.getDataType(movFn), EXPECTED_DT)
        
        outFn = join('/tmp/qbeta_converted.mrcs')

        ih.convertStack(movFn, outFn, 2, 6)
        
        self.assertTrue(os.path.exists(outFn))
        self.assertTrue(pwutils.getFileSize(outFn) > 0)
        self.assertEqual(ih.getDimensions(outFn), (4096, 4096, 1, 5))
        self.assertEqual(ih.getDataType(outFn), EXPECTED_DT)

        if pwutils.envVarOn('SCIPION_DEBUG_NOCLEAN'):
            print "Not cleaning output movie: ", outFn
        else:
            pwutils.cleanPath(outFn)
Esempio n. 28
0
 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:
             # try to read the header of the imported images
             # except for the special case of compressed movies (bz2 extension)
             if 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)
     
     return errors
Esempio n. 29
0
    def test_convertMicrographs(self):
        """ Convert micrograhs to different formats.
         EMAN2 required for .img
        """
        micFn = self.dataset.getFile('micrographs/BPV_1386.mrc')
        outSuffix = pwutils.replaceBaseExt(micFn, 'img')
        ih = ImageHandler()

        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)

        pwutils.cleanPath(outFn)
        pwutils.cleanPath(outFn.replace('.img', '.hed'))
Esempio n. 30
0
    def test_coordinate1(self):
        """
        Download a micrograph and a set of coordinates in the EMX interchange standard.
        Convert both files to your package format.
        Extract the particles from the micrograph with a box size of 128 pixels. (Remember that coordinates refer to particle centers.)
        Upload the extracted images to the web as a 2D CCP4 stack (standard exchange format).
        Three galleries of images will be displayed: the gold standard, the one just uploaded and the differences between them. The test has been sucessful if the gold standard and the images updated are identical.
        As extra check, the Web Site will make a pixel by pixel comparison between images belonging to both galleries. A green tick will appear if both images are identical and a red cross if any pair of pixels differ more than 10**-2.
        """
        #download data
        self.url = "Coordinates/Test1/"
        micFn = self.downloadFile("micrograph.mrc")
        emxFn = self.downloadFile("coordinates.emx")
        particle_even = self.downloadFile("particle_even.mrcs")

        protEmxImport = self.newProtocol(
            ProtImportParticles,
            objLabel='from emx (coordinatesTest1)',
            importFrom=ProtImportParticles.IMPORT_FROM_EMX,
            emxFile=emxFn,
            alignType=3,
            voltage=100,
            magnification=10000,
            samplingRate=2.0)
        self.launchProtocol(protEmxImport)

        outputMics = getattr(protEmxImport, 'outputMicrographs', None)
        outputCoords = getattr(protEmxImport, 'outputCoordinates', None)

        self.assertIsNotNone(outputMics)
        self.assertIsNotNone(outputCoords)
        coordsGolds = [(539, 414), (509, 192), (711, 158), (634, 349),
                       (403, 157), (347, 437), (728, 389)]
        for coord, coordGold in izip(outputCoords, coordsGolds):
            self.assertEquals(coord.getPosition(), coordGold)

        protExtract = self.newProtocol(XmippProtExtractParticles,
                                       boxSize=128,
                                       downsampleType=0,
                                       doRemoveDust=False,
                                       doNormalize=False,
                                       doFlip=False,
                                       downFactor=1)
        protExtract.inputCoordinates.set(protEmxImport.outputCoordinates)
        protExtract.inputMicrographs.set(protEmxImport.outputMicrographs)

        self.launchProtocol(protExtract)
        #export as emx
        protEmxExport = self.newProtocol(ProtEmxExport)
        protEmxExport.inputSet.set(protExtract.outputParticles)
        self.launchProtocol(protEmxExport)
        stackFn = os.path.join(protEmxExport._getPath('emxData'), "data.mrc")
        self.assertTrue(ImageHandler().compareData(particle_even,
                                                   stackFn,
                                                   tolerance=0.01))
    def _processMovie(self, movie):
        allFramesSum = self._getPath('all_frames_sum.mrc')
        ih = ImageHandler()
        sumImg = ih.createImage()
        img = ih.createImage()

        n = movie.getNumberOfFrames()
        fn = movie.getFileName()

        sumImg.read((1, fn))

        for frame in range(2, n + 1):
            img.read((frame, fn))
            sumImg.inplaceAdd(img)

        if os.path.exists(allFramesSum):
            img.read(allFramesSum)
            sumImg.inplaceAdd(img)

        sumImg.write(allFramesSum)
Esempio n. 32
0
    def test_coordinate2(self):
        """
        as test_coordinate1 but with a 129 box
        """
        #download data
        self.url = "Coordinates/Test2/"
        micFn = self.downloadFile("micrograph.mrc")
        emxFn = self.downloadFile("coordinates.emx")
        particle_odd = self.downloadFile("particle_odd.mrcs")

        protEmxImport = self.newProtocol(
            ProtImportParticles,
            objLabel='from emx (coordinatesTest2)',
            importFrom=ProtImportParticles.IMPORT_FROM_EMX,
            emxFile=emxFn,
            alignType=3,
            voltage=100,
            magnification=10000,
            samplingRate=2.0)
        self.launchProtocol(protEmxImport)

        outputMics = getattr(protEmxImport, 'outputMicrographs', None)
        outputCoords = getattr(protEmxImport, 'outputCoordinates', None)

        self.assertIsNotNone(outputMics)
        self.assertIsNotNone(outputCoords)
        coordsGolds = [(539, 414), (509, 192), (711, 158), (634, 349),
                       (403, 157), (347, 437), (728, 389)]
        for coord, coordGold in izip(outputCoords, coordsGolds):
            self.assertEquals(coord.getPosition(), coordGold)

        protExtract = self.newProtocol(XmippProtExtractParticles,
                                       boxSize=129,
                                       downsampleType=0,
                                       doRemoveDust=False,
                                       doNormalize=False,
                                       doInvert=False,
                                       doFlip=False,
                                       doSort=False)
        protExtract.inputCoordinates.set(protEmxImport.outputCoordinates)
        protExtract.inputMicrographs.set(protEmxImport.outputMicrographs)

        self.launchProtocol(protExtract)
        #export as emx
        protEmxExport = self.newProtocol(ProtEmxExport)
        protEmxExport.inputSet.set(protExtract.outputParticles)
        self.launchProtocol(protEmxExport)
        stackFn = os.path.join(protEmxExport._getPath('emxData'), "data.mrc")
        # this assert does not work is a compare the stack as a block
        for num in range(1, 8):
            self.assertTrue(ImageHandler().compareData("%d@" % num +
                                                       particle_odd,
                                                       "%d@" % num + stackFn,
                                                       tolerance=0.01))
Esempio n. 33
0
    def test_orientation1(self):
        """
        This test requires:
            Download a stack of 2D images (particles) and a set of transformation matrices in the EMX interchange standard.
            Convert both files to your package format.
            Apply the transformation matrix to the stack of images.
            Compute an average image of the whole stack
            Upload the average image as a 2D CCP4 image (standard exchange format).
            Two images will be displayed: the gold standard and the one just uploaded. The test has been successful if the gold standard and the image updated are identical.
            Note: test data created using a subunit of GroEL (PDB id 1SS8)
        """
        #download data
        self.url = "Orientation/Test1/"
        imgFn = self.downloadFile("images.mrc")
        emxFn = self.downloadFile("images.emx")
        average = self.downloadFile("average.mrc")

        protEmxImport = self.newProtocol(
            ProtImportParticles,
            objLabel='from emx (orientation1)',
            importFrom=ProtImportParticles.IMPORT_FROM_EMX,
            emxFile=emxFn,
            alignType=0,  #2D align
            voltage=100,
            magnification=10000,
            samplingRate=2.46)
        self.launchProtocol(protEmxImport)
        outputParticles = getattr(protEmxImport, 'outputParticles', None)
        #apply alignment
        protApply = self.newProtocol(XmippProtApplyAlignment)
        protApply.inputParticles.set(protEmxImport.outputParticles)
        self.launchProtocol(protApply)
        # We check that protocol generates output
        self.assertIsNotNone(
            protApply.outputParticles,
            "There was a problem generating output particles")
        # Check that output particles do not have alignment information
        self.assertFalse(
            protApply.outputParticles.hasAlignment(),
            "Output particles should not have alignment information")

        average = getattr(protApply, 'outputAverage', None)
        outputParticles = getattr(protApply, 'outputParticles', None)

        #export as emx
        protEmxExport = self.newProtocol(ProtEmxExport)
        protEmxExport.inputSet.set(outputParticles)
        self.launchProtocol(protEmxExport)
        #TODO: upload result to emx web site. Now it is down
        stackFn = os.path.join(protEmxExport._getPath('emxData'), "data.mrc")
        firstImg = outputParticles.getFirstItem()
        self.assertTrue(ImageHandler().compareData(firstImg.getFileName(),
                                                   stackFn,
                                                   tolerance=0.01))
Esempio n. 34
0
class BsoftFilterDialog(DownsampleDialog):
    
    def _beforePreview(self):
        ImagePreviewDialog._beforePreview(self)
        self.lastObj = None
        self.rightPreviewLabel = "Filtered particle"
        self.message = "Filtering particle..."
        self.previewLabel = "Particle"
        self.rightImage = ImageHandler()._img
        
    def _createControls(self, frame):
        pass #FIXME
#         self.freqFrame = ttk.LabelFrame(frame, text="Frequencies", padding="5 5 5 5")
#         self.freqFrame.grid(row=0, column=0)
#         if self.protocolParent.filterType <= FILTER_SPACE_REAL:
#             self.radiusSlider = self.addFreqSlider('Radius', self.protocolParent.filterRadius.get(), col=0)
#         else:
#             self.lfSlider = self.addFreqSlider('Low freq', self.protocolParent.lowFreq.get(), col=0)
#             self.hfSlider = self.addFreqSlider('High freq', self.protocolParent.highFreq.get(), col=1)        
#             if self.protocolParent.filterType == FILTER_FERMI:
#                 self.tempSlider = self.addFreqSlider('Temperature', self.protocolParent.temperature.get(), col=2)
#         radiusButton = tk.Button(self.freqFrame, text='Preview', command=self._doPreview)
#         radiusButton.grid(row=0, column=3, padx=5, pady=5)
        
    def _doPreview(self, e=None):
        if self.lastObj is None:
            dialog.showError("Empty selection", "Select an item first before preview", self)
        else:
            self._computeRightPreview()
    
    def updateFilteredImage(self):
        self.rightPreview.updateData(self.rightImage.getData())
        
    def _computeRightPreview(self):
        """ This function should compute the right preview
        using the self.lastObj that was selected
        """
        from pyworkflow.em.packages.xmipp3 import locationToXmipp
        
        # Copy image to filter to Tmp project folder
        inputPath = os.path.join("Tmp", "bsoft_filter_input.spi")
        outputPath = os.path.join("Tmp", "bsoft_filter_output.spi")
        cleanPath(inputPath, outputPath)

        ih = ImageHandler()
        ih.convert(self.lastObj.getLocation(), inputPath) 
                
        self.protocolParent.runFilter(inputPath, outputPath)
        
        # Get output image and update filtered image
        img = ih._img
        img.read(outputPath)
        self.rightImage = img
        self.updateFilteredImage()
Esempio n. 35
0
    def importMicrographs(self, pattern, suffix, voltage, sphericalAberration,
                          amplitudeContrast):
        """ Copy images matching the filename pattern
        Register other parameters.
        """
        filePaths = glob(expandPattern(pattern))

        #imgSet = SetOfMicrographs(filename=self.micsPairsSqlite, prefix=suffix)
        imgSet = self._createSetOfMicrographs(suffix=suffix)
        acquisition = imgSet.getAcquisition()
        # Setting Acquisition properties
        acquisition.setVoltage(voltage)
        acquisition.setSphericalAberration(sphericalAberration)
        acquisition.setAmplitudeContrast(amplitudeContrast)

        # Call a function that should be implemented by each subclass
        self._setOtherPars(imgSet)

        outFiles = [imgSet.getFileName()]
        imgh = ImageHandler()
        img = imgSet.ITEM_TYPE()
        n = 1
        size = len(filePaths)

        filePaths.sort()

        for i, fn in enumerate(filePaths):
            #             ext = os.path.splitext(basename(f))[1]
            dst = self._getExtraPath(basename(fn))
            if self.copyToProj:
                copyFile(fn, dst)
            else:
                createLink(fn, dst)

            if n > 1:
                for index in range(1, n + 1):
                    img.cleanObjId()
                    img.setFileName(dst)
                    img.setIndex(index)
                    imgSet.append(img)
            else:
                img.cleanObjId()
                img.setFileName(dst)
                imgSet.append(img)
            outFiles.append(dst)

            sys.stdout.write("\rImported %d/%d" % (i + 1, size))
            sys.stdout.flush()

        print "\n"

        imgSet.write()

        return imgSet
Esempio n. 36
0
    def _itemSelected(self, obj):

        index = obj.getIndex()
        filename = obj.getFileName()
        if index:
            filename = "%03d@%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)
    def convertInputStep(self, micsId, refsId, runType):
        # runType is passed as parameter to force a re-execute of this step
        # if there is a change in the type

        self._ih = ImageHandler()  # used to convert micrographs
        # Match ctf information against the micrographs
        self.ctfDict = {}
        if self.ctfRelations.get() is not None:
            for ctf in self.ctfRelations.get():
                self.ctfDict[ctf.getMicrograph().getMicName()] = ctf.clone()

        micStar = self._getPath('input_micrographs.star')
        writeSetOfMicrographs(self.getMicrographList(),
                              micStar,
                              alignType=ALIGN_NONE,
                              preprocessImageRow=self._preprocessMicrographRow)

        if self.useInputReferences():
            writeReferences(self.getInputReferences(),
                            self._getPath('input_references'),
                            useBasename=True)
Esempio n. 38
0
    def _computeRightPreview(self):
        """ This function should compute the right preview
        using the self.lastObj that was selected
        """
        from pyworkflow.em.packages.xmipp3 import locationToXmipp
        
        # Copy image to filter to Tmp project folder
        inputPath = os.path.join("Tmp", "bsoft_filter_input.spi")
        outputPath = os.path.join("Tmp", "bsoft_filter_output.spi")
        cleanPath(inputPath, outputPath)

        ih = ImageHandler()
        ih.convert(self.lastObj.getLocation(), inputPath) 
                
        self.protocolParent.runFilter(inputPath, outputPath)
        
        # Get output image and update filtered image
        img = ih._img
        img.read(outputPath)
        self.rightImage = img
        self.updateFilteredImage()
Esempio n. 39
0
    def prepareReferences(self, fnDir, TsCurrent, targetResolution):
        print "Preparing references to sampling rate=", TsCurrent
        fnMask = ''
        newXdim = self.readInfoField(fnDir, "size", xmipp.MDL_XSIZE)
        if self.nextMask.hasValue():
            fnMask = join(fnDir, "mask.vol")
            self.prepareMask(self.nextMask.get(), fnMask, TsCurrent, newXdim)

        fnReferenceVol = join(fnDir, "volumeRef.vol")
        img = ImageHandler()
        img.convert(self.inputVolume.get(), fnReferenceVol)
        Xdim = self.inputVolume.get().getDim()[0]
        if Xdim != newXdim:
            self.runJob("xmipp_image_resize",
                        "-i %s --fourier %d" % (fnReferenceVol, newXdim),
                        numberOfMpi=1)

        if fnMask != '':
            self.runJob('xmipp_image_operate',
                        '-i %s --mult %s' % (fnReferenceVol, fnMask),
                        numberOfMpi=1)
Esempio n. 40
0
    def _runBeforePreWhitening(self):
        prot = self.form.protocol
        # Convert input volumes
        ih = ImageHandler()
        if prot.useSplitVolume:
            ih.convert(prot.volumeHalf1.get(),
                       join(self.workingDir, 'volume1.map'))
            ih.convert(prot.volumeHalf2.get(),
                       join(self.workingDir, 'volume2.map'))
        else:
            ih.convert(prot.inputVolume.get(),
                       join(self.workingDir, 'volume1.map'))

        self.results = prot.runResmap(self.workingDir, wizardMode=True)
Esempio n. 41
0
    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)
Esempio n. 42
0
    def _pickMicrograph(self, mic, radius):
        micFn = mic.getFileName()
        micDir = self._getMicDir(micFn)
        fnMicBase = pwutils.replaceBaseExt(micFn, 'mrc')
        fnMicCfg = pwutils.replaceBaseExt(micFn, 'cfg')
        fnMicFull = os.path.join(micDir, fnMicBase)
        fnPosBase = self._getMicPosFn(micFn)

        # Convert micrographs to mrc (uint8) as required by ETHAN program
        ih = ImageHandler()
        ih.convert(micFn, fnMicFull, md.DT_UCHAR)

        # Create a configuration file to be used by ETHAN with the parameters
        # selected by the user
        self.writeConfigFile(os.path.join(micDir, fnMicCfg))
        # Run ethan program with the required arguments
        program = self.getProgram()
        args = "%s %s %s %s" % (radius, fnMicBase, fnPosBase, fnMicCfg)
        self.runJob(program, args, cwd=micDir)

        # Clean temporary micrograph
        pwutils.cleanPath(fnMicFull)
    def generateSplittedVolumes(self):
        inputParticles = self.directionalClasses.get()
        Xdim = inputParticles.getDimensions()[0]
        fnMask = ""
        if self.mask.hasValue():
            fnMask = self._getExtraPath("mask.vol")
            img = ImageHandler()
            img.convert(self.mask.get(), fnMask)
            self.runJob('xmipp_image_resize',
                        "-i %s --dim %d" % (fnMask, Xdim),
                        numberOfMpi=1)
            self.runJob('xmipp_transform_threshold',
                        "-i %s --select below 0.5 --substitute binarize" %
                        fnMask,
                        numberOfMpi=1)

        args="-i %s --oroot %s --Nrec %d --Nsamples %d --sym %s --alpha %f"%\
             (self._getExtraPath("directionalClasses.xmd"),self._getExtraPath("split"),self.Nrec.get(),self.Nsamples.get(),
              self.symmetryGroup.get(), self.alpha.get())
        if fnMask != "":
            args += " --mask binary_file %s" % fnMask
        self.runJob("xmipp_classify_first_split", args)
Esempio n. 44
0
    def _validate(self):
        from pyworkflow.em.convert import ImageHandler
        ci = self.getImportClass()
        if ci is None:
            errors = ProtImportMicBase._validate(self)
            for micFn, _ in self.iterFiles():
                imgh = ImageHandler()
                if imgh.isImageFile(micFn):
                    _, _, z, n = imgh.getDimensions(micFn)
                    if n > 1 or z > 1:
                        errors.append(
                            "The protocol not support micrographs stored in stacks. "
                            "If you want to obtain your micrographs individually, "
                            "you can run the following command:\n"
                            "scipion run scipion_directory/scripts/split_stacks.py --files *your files* --ext *extension*"
                        )
                # JMRT: only check the first image, for large dataset
                # even reading the header can take a while
                break
            return errors

        else:
            return ci.validateMicrographs()
Esempio n. 45
0
 def test_convert(self):
     """ Run an Import particles protocol. """
     protImport = self.newProtocol(ProtImportParticles, filesPath=self.particlesFn, samplingRate=3.5)
     self.launchProtocol(protImport)
     # check that input images have been imported (a better way to do this?)
     if getattr(protImport, 'outputParticles', None) is None:
         raise Exception('Import of images: %s, failed. outputParticles is None.' % self.particlesFn)
     
     ih = ImageHandler()
     stackFn = self.getOutputPath('stack.stk')
     selFn = self.getOutputPath('stack_sel.stk')
     print "stackFn: ", stackFn
     
     writeSetOfImages(protImport.outputParticles, stackFn, selFn)
Esempio n. 46
0
    def createParticlesStep(self):
        self.name = "particle"
        time.sleep(self.creationInterval.get())

        for idx, p in enumerate(self.inputParticles.get()):
            if ((idx > self.counter - 1) and (idx < self.nDims)
                    and (idx <= self.counter - 1 + self.group)):
                newP = p.clone()
                ProtCreateStreamData.object = \
                    ImageHandler().read(newP.getLocation())
                destFn = self._getExtraPath("%s_%05d" % (self.name, idx))
                ProtCreateStreamData.object.write(destFn)
                self.dictObj[destFn] = True
        self._checkProcessedData()
Esempio n. 47
0
 def convertInputStep(self, micsId, refsId):
     self._ih = ImageHandler() # used to convert micrographs
     # Match ctf information against the micrographs
     self.ctfDict = {}
     if self.ctfRelations.get() is not None:
         for ctf in self.ctfRelations.get():
             self.ctfDict[ctf.getMicrograph().getMicName()] = ctf.clone()
     
     micStar = self._getPath('input_micrographs.star')
     writeSetOfMicrographs(self.getInputMicrographs(), micStar, 
                           preprocessImageRow=self._preprocessMicrographRow,
                           postprocessImageRow=self._postprocessMicrographRow)
     
     writeReferences(self.getInputReferences(), self._getPath('input_references'))  
Esempio n. 48
0
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 = ImageHandler.fixXmippVolumeFileName(obj)
        if index:
            filename = "%03d@%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)
Esempio n. 49
0
    def importMaskStep(self, path, samplingRate):
        """ Copy mask from maskPath.
        """
        self.info("Using mask path: '%s'" % path)

        # Copy the image file into the project
        dst = self._getExtraPath(basename(path))
        pwutils.copyFile(path, dst)

        # Retrive image dimensions
        imgh = ImageHandler()
        _, _, z, n = imgh.getDimensions(dst)

        # Create a 2D or 3D Mask, consider the case of n>1
        # as the case of some volume maps in mrc format
        if z > 1 or n > 1:
            mask = VolumeMask()
        else:
            mask = Mask()

        mask.setFileName(dst)
        mask.setSamplingRate(samplingRate)

        self._defineOutputs(outputMask=mask)
    def buildDendrogram(self, writeAverages=False):
        """ Parse Spider docfile with the information to build the dendrogram.
        Params:
            writeAverages: whether to write class averages or not.
        """
        dendroFile = self._getFileName('dendroDoc')
        # Dendrofile is a docfile with at least 3 data colums (class, height, id)
        doc = SpiderDocFile(dendroFile)
        values = []
        indexes = []
        for _, h, i in doc.iterValues():
            indexes.append(i)
            values.append(h)
        doc.close()
        
        self.dendroValues = values
        self.dendroIndexes = indexes
        self.dendroImages = self._getFileName('particles')
        self.dendroAverages = self._getFileName('averages')
        self.dendroAverageCount = 0 # Write only the number of needed averages
        self.dendroMaxLevel = 10 # FIXME: remove hard coding if working the levels
        self.ih = ImageHandler()

        return self._buildDendrogram(0, len(values)-1, 1, writeAverages)
Esempio n. 51
0
    def _pickMicrograph(self, mic, args):
        # Prepare mic folder and convert if needed
        micName = mic.getFileName()
        micDir = self._getTmpPath(pwutils.removeBaseExt(micName))
        pwutils.makePath(micDir)

        ih = ImageHandler()
        # If needed convert micrograph to mrc format, otherwise link it
        if pwutils.getExt(micName) != ".mrc":
            fnMicBase = pwutils.replaceBaseExt(micName, 'mrc')
            inputMic = os.path.join(micDir, fnMicBase)
            ih.convert(mic.getLocation(), inputMic)
        else:
            inputMic = os.path.join(micDir, os.path.basename(micName))
            pwutils.createLink(micName, inputMic)

        # Program to execute and it arguments
        program = "ApDogPicker.py"
        outputFile = self._getExtraPath(pwutils.replaceBaseExt(
            inputMic, "txt"))

        args += " --image=%s --outfile=%s" % (inputMic, outputFile)

        self.runJob(program, args)
    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())

        Xdim = inputParticles.getXDim()
        Ts = inputParticles.getSamplingRate()
        if self._useSeveralClasses():
            # Scale particles
            newTs = self.targetResolution.get() * 0.4
            newTs = max(Ts, newTs)
            newXdim = long(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)
            
            Xdim=newXdim
            Ts=newTs
        self.writeInfoField(self._getExtraPath(),"sampling",xmippLib.MDL_SAMPLINGRATE,Ts)
        self.writeInfoField(self._getExtraPath(),"size",xmippLib.MDL_XSIZE,long(Xdim))
Esempio n. 53
0
 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')) 
Esempio n. 54
0
def _beforePreWhitening(protocol, workingDir):
    from pyworkflow.em.convert import ImageHandler
    # Convert input volumes
    ih = ImageHandler()
    projPath = protocol.getProject().getPath()

    def convertVol(inputVol, outputFn):
        index, path = inputVol.get().getLocation()
        ih.convert((index, join(projPath, path)), join(workingDir, outputFn))

    if protocol.useSplitVolume:
        convertVol(protocol.volumeHalf1, 'volume1.map')
        convertVol(protocol.volumeHalf2, 'volume2.map')
    else:
        convertVol(protocol.inputVolume, 'volume1.map')

    return protocol.runResmap(workingDir, wizardMode=True)
Esempio n. 55
0
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)
Esempio n. 56
0
def writeSetOfImages(imgSet, stackFn, selFn):
    """ This function will write a SetOfMicrographs as a Spider stack and selfile.
    Params:
        imgSet: the SetOfMicrograph instance.
        stackFn: the filename where to write the stack.
        selFn: the filename of the Spider selection file.
    """
    ih = ImageHandler()
    doc = SpiderDocFile(selFn, 'w+')

    for i in range(imgSet.getSize()):
        doc.writeValues(i + 1)

    imgSet.writeStack(stackFn, applyTransform=True)

    doc.close()

    convertEndian(stackFn, imgSet.getSize())
Esempio n. 57
0
    def testExistLocation(self):
        volFn = self.dataset.getFile('volumes/volume_1_iter_002.mrc')

        ih = ImageHandler()
        # Test the volume filename exists
        self.assertTrue(ih.existsLocation(volFn))
        # Test missing filename
        self.assertFalse(ih.existsLocation(volFn.replace('.mrc', '_fake.mrc')))
        # Test the :mrc is append when used as volume
        newFn = ih.getVolFileName(volFn)
        self.assertEqual(newFn, volFn + ":mrc")
        # Test that the new filename still exists even with the :mrc suffix
        self.assertTrue(ih.existsLocation(newFn))
Esempio n. 58
0
    def convertInputStep(self, inputParticlesId):
        fnDir=self._getExtraPath()
        writeSetOfParticles(self.inputParticles.get(),self.imgsFn)

        # Choose the target sampling rate        
        TsOrig=self.inputParticles.get().getSamplingRate()
        TsCurrent=max(TsOrig,self.targetResolution.get()/3)
        Xdim=self.inputParticles.get().getDimensions()[0]
        newXdim=long(round(Xdim*TsOrig/TsCurrent))
        if newXdim<40:
            newXdim=long(40)
            TsCurrent=Xdim*(TsOrig/newXdim)
        print "Preparing images to sampling rate=",TsCurrent
        self.writeInfoField(fnDir,"size",xmippLib.MDL_XSIZE,newXdim)
        self.writeInfoField(fnDir,"sampling",xmippLib.MDL_SAMPLINGRATE,TsCurrent)
        
        # Prepare particles
        fnNewParticles=join(fnDir,"images.stk")
        if newXdim!=Xdim:
            self.runJob("xmipp_image_resize","-i %s -o %s --fourier %d"%(self.imgsFn,fnNewParticles,newXdim),
                        numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get())
        else:
            self.runJob("xmipp_image_convert","-i %s -o %s --save_metadata_stack %s"%(self.imgsFn,fnNewParticles,join(fnDir,"images.xmd")),
                        numberOfMpi=1)
        R=self.particleRadius.get()
        if R<=0:
            R=self.inputParticles.get().getDimensions()[0]/2
        R=min(round(R*TsOrig/TsCurrent*1.1),newXdim/2)
        self.runJob("xmipp_transform_mask","-i %s --mask circular -%d"%(fnNewParticles,R),numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get())
        
        # Prepare mask
        imgHandler=ImageHandler()
        if self.nextMask.hasValue():
            self.convertInputVolume(imgHandler, self.nextMask.get(), getImageLocation(self.nextMask.get()), join(fnDir,"mask.vol"), TsCurrent, newXdim)
        
        # Prepare references
        i=0
        for vol in self.inputVolumes.get():
            fnVol=join(fnDir,"volume%03d.vol"%i)
            self.convertInputVolume(imgHandler, vol, getImageLocation(vol), fnVol, TsCurrent, newXdim)
            self.runJob("xmipp_image_operate","-i %s --mult 0 -o %s"%(fnVol,join(fnDir,"volume%03d_speed.vol"%i)),numberOfMpi=1)
            i+=1
        xmippLib.MetaData().write("best@"+self._getExtraPath("swarm.xmd")) # Empty write to guarantee this block is the first one
        xmippLib.MetaData().write("bestByVolume@"+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND) # Empty write to guarantee this block is the second one
Esempio n. 59
0
    def _loadPath(self, path):
        self._tableName = None

        # If path is a tuple, we will convert to the filename format
        # as expected by Showj
        if isinstance(path, tuple):
            self._path = ImageHandler.locationToXmipp(path)
        # Check if there is a table name with @ in path
        # in that case split table name and path
        # table names can never starts with a number
        # this is considering an image inside an stack
        elif isinstance(path, basestring):
            if '@' in path and path[0] not in '0123456789':
                self._tableName, self._path = path.split('@')
            else:
                self._path = path
        else:
            raise Exception("Invalid input path, "
                            "should be 'string' or 'tuple'")