def classifyGroupsStep(self):
        fnNeighbours = self._getExtraPath("neighbours.xmd")
        gallery = self._getExtraPath("gallery.stk")

        for block in md.getBlocksInMetaDataFile(fnNeighbours):

            #creating a folder to each direction
            imgNo = int(block.split("_")[1])
            fnDir = self._getExtraPath("direction_%05d" % imgNo)
            fnBlock = "%s@%s" % (block, fnNeighbours)

            if not exists(fnDir):
                pwpath.makePath(fnDir)

            if self.classMethod.get() == cmcons.CL2D:
                dirClasses = self.directionalClasses.get()
                nLevs = self._getCl2dLevels()
                fnOut = join(fnDir, "level_%02d/class_classes.stk" % nLevs)
                self._runClassifSteps(fnOut, fnBlock, fnDir, imgNo, gallery,
                                      callbackMethod=self._runCl2dStep)
            else:
                relPart = self._createSetOfParticles()
                relPart.copyInfo(self.inputParticles.get())
                fnOut = join(fnDir, "class_")
                self._runClassifSteps(fnOut, fnBlock, fnDir, imgNo, gallery,
                                      callbackMethod=self._runRelionStep)
Example #2
0
    def kmeansClassifyStep(self, fnInputMd):
        iteration = 0
        args = "-i %s -k %d -m %d" % (fnInputMd, self.numberOfClasses.get(),
                                      self.maxObjects.get())
        self.runJob("xmipp_classify_kmeans_2d", args)
        cleanPath(self._getExtraPath("level_00"))
        blocks = md.getBlocksInMetaDataFile(self._getExtraPath("output.xmd"))
        fnDir = self._getExtraPath()
        # Gather all images in block
        for b in blocks:
            if b.startswith('class0'):
                args = "-i %s@%s --iter 5 --distance correlation " \
                       "--classicalMultiref --nref 1 --odir %s --oroot %s" % \
                       (b, self._getExtraPath("output.xmd"), fnDir, b)
                if iteration == 0:
                    args += " --nref0 1"
                else:
                    args += " --ref0 %s" % \
                            self._getExtraPath("level_00/%s_classes.stk" % b)
                self.runJob("xmipp_classify_CL2D",
                            args,
                            numberOfMpi=max(2, self.numberOfMpi.get()))
                cleanPath(self._getExtraPath("level_00/%s_classes.xmd" % b))

        streamMode = Set.STREAM_CLOSED if self.finished else Set.STREAM_OPEN
        outSet = self._loadOutputSet(SetOfClasses2D, 'classes2D.sqlite')
        self._updateOutputSet('outputParticles', outSet, streamMode)
    def _fillClasses(self, outputClasses):
        """ Create the SetOfClasses2D """
        inputSet = self.inputClasses.get().getImages()
        myRep = md.MetaData('classes@' + self._getExtraPath(
            'final_classes.xmd'))

        for row in md.iterRows(myRep):
            fn = row.getValue(md.MDL_IMAGE)
            rep = Particle()
            rep.setLocation(xmippToLocation(fn))
            repId = row.getObjId()
            newClass = Class2D(objId=repId)
            newClass.setAlignment2D()
            newClass.copyInfo(inputSet)
            newClass.setAcquisition(inputSet.getAcquisition())
            newClass.setRepresentative(rep)
            outputClasses.append(newClass)

        i=1
        mdBlocks = md.getBlocksInMetaDataFile(self._getExtraPath(
            'final_classes.xmd'))
        for block in mdBlocks:
            if block.startswith('class00'):
                mdClass = md.MetaData(block + "@" + self._getExtraPath(
                                      'final_classes.xmd'))
                imgClassId = i
                newClass = outputClasses[imgClassId]
                newClass.enableAppend()
                for row in md.iterRows(mdClass):
                    part = rowToParticle(row)
                    newClass.append(part)
                i+=1
                newClass.setAlignment2D()
                outputClasses.update(newClass)
    def _runCl2dStep(self, fnOut, fnBlock, fnDir, imgNo, fnGallery):
        fnAlignRoot = join(fnDir, "classes")
        args = {'-i': fnBlock,
                '--odir': fnDir,
                '--ref0': str(imgNo) + '@' + fnGallery,
                '--iter': self.cl2dIterations.get(),
                '--nref': self.directionalClasses.get(),
                '--distance': 'correlation',
                '--classicalMultiref': '',
                '--maxShift': self.maxShift.get()
                }

        argsAlign = {'-i': fnOut,
                     '--oroot': fnAlignRoot,
                     '--ref': str(imgNo) + '@' + fnGallery,
                     '--iter': 1
                     }

        params = self._getParams(args)
        self.runJob("xmipp_classify_CL2D", params)

        parALign = self._getParams(argsAlign)
        self.runJob("xmipp_image_align", parALign, numberOfMpi=1)

        paramGeo = "-i %s_alignment.xmd --apply_transform" % fnAlignRoot
        self.runJob("xmipp_transform_geometry", paramGeo , numberOfMpi=1)

        fnOutput = self._getPath('output_particles.xmd')
        mdOutput = self._getMetadata(fnOutput)

        cLevels = self._getCl2dLevels()
        mdClassesFn = fnDir + '/level_%02d/class_classes.xmd' % cLevels

        CC = []
        mdCount = self._getMetadata(mdClassesFn)
        for row in md.iterRows(mdCount):
            CC.append(row.getValue(md.MDL_CLASS_COUNT))

        n = sum(CC)
        out = np.true_divide(CC, n)
        highest = out >= self.thresholdValue.get()
        xmippBlocks = md.getBlocksInMetaDataFile(mdClassesFn)[2:]

        for indx, block in enumerate(xmippBlocks):
            fnBlock = '%s@%s' % (block, mdClassesFn)
            mdClassCount = self._getMetadata(fnBlock)
            for row in md.iterRows(mdClassCount):
                objId = mdOutput.addObject()
                if not highest[indx]:
                    row.setValue(md.MDL_ENABLED, -1)
                row.writeToMd(mdOutput, objId)
        mdOutput.write(fnOutput)
Example #5
0
 def _fillClassesFromLevel(self, clsSet):
     """ Create the SetOfClasses2D from a given iteration. """
     blocks = md.getBlocksInMetaDataFile(self._getExtraPath('output.xmd'))
     for bId, b in enumerate(blocks):
         if b.startswith('class0'):
             self._loadClassesInfo(
                 self._getExtraPath("level_00/%s_classes.stk" % b), bId)
             xmpMd = b + "@" + self._getExtraPath('output.xmd')
             iterator = md.SetMdIterator(
                 xmpMd,
                 sortByLabel=md.MDL_ITEM_ID,
                 updateItemCallback=self._updateParticle,
                 skipDisabled=True)
             clsSet.classifyItems(updateItemCallback=iterator.updateItem,
                                  updateClassCallback=self._updateClass)
    def _runRelionStep(self, fnOut, fnBlock, fnDir, imgNo, fnGallery):
        relPart = SetOfParticles(filename=":memory:")
        convXmp.readSetOfParticles(fnBlock, relPart)

        if self.copyAlignment.get():
            alignType = relPart.getAlignment()
            alignType != ALIGN_NONE
        else:
            alignType = ALIGN_NONE

        alignToPrior = getattr(self, 'alignmentAsPriors', False)
        fillRandomSubset = getattr(self, 'fillRandomSubset', False)
        fnRelion = self._getExtraPath('relion_%s.star' % imgNo)

        writeSetOfParticles(relPart, fnRelion, self._getExtraPath(),
                            alignType=alignType,
                            postprocessImageRow=self._postprocessParticleRow,
                            fillRandomSubset=fillRandomSubset)
        if alignToPrior:
            mdParts = md.MetaData(fnRelion)
            self._copyAlignAsPriors(mdParts, alignType)
            mdParts.write(fnRelion)
        if self.doCtfManualGroups:
            self._splitInCTFGroups(fnRelion)

        args = {}
        self._setNormalArgs(args)
        args['--i'] = fnRelion
        args['--o'] = fnOut
        if self.referenceClassification.get():
            fnRef = "%s@%s" % (imgNo, fnGallery)
            args['--ref'] = fnRef
        self._setComputeArgs(args)

        params = ' '.join(
            ['%s %s' % (k, str(v)) for k, v in args.items()])
        print('Vamos a correr relion', params)
        self.runJob(self._getRelionProgram(), params)

        clsDistList = []
        it = self.numberOfIterations.get()
        model = '_it%03d_' % it
        fnModel = (fnOut + model + 'model.star')

        block = md.getBlocksInMetaDataFile(fnModel)[1]
        fnBlock = "%s@%s" % (block, fnModel)

        mdBlocks = md.MetaData(fnBlock)
        fnData = (fnOut + model + 'data.star')

        for objId in mdBlocks:
            clsDist = mdBlocks.getValue(md.RLN_MLMODEL_PDF_CLASS, objId)
            clsDistList.append(clsDist)

        fnOutput = self._getPath('output_particles.xmd')
        mdOutput = self._getMetadata(fnOutput)

        mdParticles = md.MetaData(fnData)
        for row in md.iterRows(mdParticles):
            objId = mdOutput.addObject()
            clsNum = row.getValue('rlnClassNumber')
            clsDist = clsDistList[clsNum-1]

            if clsDist >= self.thresholdValue.get():
                row.setValue(md.MDL_ENABLED, 1)
            else:
                row.setValue(md.MDL_ENABLED, -1)
            row.writeToMd(mdOutput, objId)
        mdOutput.write(fnOutput)
    def realignStep(self):

        inputMdName = self._getExtraPath('inputClasses.xmd')
        writeSetOfClasses2D(self.inputClasses.get(), inputMdName,
                            writeParticles=True)

        centeredStackName = self._getExtraPath('centeredStack.stk')
        self._params = {'input': inputMdName,
                        'output': centeredStackName}
        args = ('-i %(input)s -o %(output)s --save_metadata_transform')
        self.runJob("xmipp_transform_center_image", args % self._params,
                    numberOfMpi=1)

        centeredMdName = centeredStackName.replace('stk', 'xmd')
        centeredMd = md.MetaData(centeredMdName)
        centeredStack = md.MetaData(centeredStackName)

        listName = []
        listTransform=[]
        for rowStk in md.iterRows(centeredStack):
            listName.append(rowStk.getValue(md.MDL_IMAGE))
        for rowMd in md.iterRows(centeredMd):
            listTransform.append(rowToAlignment(rowMd, ALIGN_2D))

        mdNewClasses = md.MetaData()
        for i, row in enumerate(md.iterRows(inputMdName)):
            newRow = md.Row()
            newRow.setValue(md.MDL_IMAGE, listName[i])
            refNum = row.getValue(md.MDL_REF)
            newRow.setValue(md.MDL_REF, refNum)
            classCount = row.getValue(md.MDL_CLASS_COUNT)
            newRow.setValue(md.MDL_CLASS_COUNT, classCount)
            newRow.addToMd(mdNewClasses)
        mdNewClasses.write('classes@' + self._getExtraPath('final_classes.xmd'),
                           MD_APPEND)

        mdImages = md.MetaData()
        i=0
        mdBlocks = md.getBlocksInMetaDataFile(inputMdName)
        resultMat = Transform()
        for block in mdBlocks:
            if block.startswith('class00'):
                newMat = listTransform[i]
                newMatrix = newMat.getMatrix()
                mdClass = md.MetaData(block + "@" + inputMdName)
                mdNewClass = md.MetaData()
                i+=1
                for rowIn in md.iterRows(mdClass):
                    #To create the transformation matrix (and its parameters)
                    #  for the realigned particles
                    if rowIn.getValue(md.MDL_ANGLE_PSI)!=0:
                        flag_psi=True
                    if rowIn.getValue(md.MDL_ANGLE_ROT)!=0:
                        flag_psi=False
                    inMat = rowToAlignment(rowIn, ALIGN_2D)
                    inMatrix = inMat.getMatrix()
                    resultMatrix = np.dot(newMatrix,inMatrix)
                    resultMat.setMatrix(resultMatrix)
                    rowOut=md.Row()
                    rowOut.copyFromRow(rowIn)
                    alignmentToRow(resultMat, rowOut, ALIGN_2D)
                    if flag_psi==False:
                        newAngle = rowOut.getValue(md.MDL_ANGLE_PSI)
                        rowOut.setValue(md.MDL_ANGLE_PSI, 0.)
                        rowOut.setValue(md.MDL_ANGLE_ROT, newAngle)

                    #To create the new coordinates for the realigned particles
                    inPoint = np.array([[0.],[0.],[0.],[1.]])
                    invResultMat = np.linalg.inv(resultMatrix)
                    centerPoint = np.dot(invResultMat,inPoint)
                    rowOut.setValue(md.MDL_XCOOR, rowOut.getValue(
                        md.MDL_XCOOR)+int(centerPoint[0]))
                    rowOut.setValue(md.MDL_YCOOR, rowOut.getValue(
                        md.MDL_YCOOR)+int(centerPoint[1]))
                    rowOut.addToMd(mdNewClass)
                mdNewClass.write(block + "@" + self._getExtraPath(
                    'final_classes.xmd'), MD_APPEND)
                mdImages.unionAll(mdNewClass)
        mdImages.write(self._getExtraPath('final_images.xmd'))