def homogeneizeStep(self):
     minClass = self.homogeneize.get()
     fnNeighbours = self._getExtraPath("neighbours.xmd")
     
     # Look for the block with the minimum number of images
     if minClass==0:
         minClass = 1e38
         for block in xmippLib.getBlocksInMetaDataFile(fnNeighbours):
             projNumber = int(block.split("_")[1])
             fnDir=self._getExtraPath("direction_%d"%projNumber,"level_00","class_classes.xmd")
             if exists(fnDir):
                 blockSize = md.getSize("class000001_images@"+fnDir)
                 if blockSize<minClass:
                     minClass=blockSize
     
     # Construct the homogeneized metadata
     mdAll = xmippLib.MetaData()
     mdSubset=xmippLib.MetaData()
     mdRandom=xmippLib.MetaData()
     for block in xmippLib.getBlocksInMetaDataFile(fnNeighbours):
         projNumber = int(block.split("_")[1])
         fnDir=self._getExtraPath("direction_%d"%projNumber,"level_00","class_classes.xmd")
         if exists(fnDir):
             mdDirection = xmippLib.MetaData("class000001_images@"+fnDir)
             mdRandom.randomize(mdDirection)
             mdSubset.selectPart(mdRandom,0L,min(mdRandom.size(),minClass))
             mdAll.unionAll(mdSubset)
     mdAll.removeDuplicates(md.MDL_ITEM_ID)
     mdAll.sort(md.MDL_ITEM_ID)
     mdAll.fillConstant(md.MDL_PARTICLE_ID,1)
     fnHomogeneous = self._getExtraPath("images_homogeneous.xmd")
     mdAll.write(fnHomogeneous)    
     self.runJob("xmipp_metadata_utilities",'-i %s --operate modify_values "particleId=itemId"'%fnHomogeneous,numberOfMpi=1)
Esempio n. 2
0
    def getFilePreview(self, objFile):
        self._imgPreview = None
        self._imgInfo = None
        filename = objFile.getPath()
        ext = getExt(filename)

        if ext == '.xmd' or ext == '.ctfparam' or ext == '.pos' or ext == '.doc':
            msg = "*Metadata File* "
            blocks = xmippLib.getBlocksInMetaDataFile(filename)
            nblocks = len(blocks)
            if nblocks <= 1:
                mdStr = self._getMdString(filename)
                msg += "  (single block)\n"
                if self._imgInfo:
                    msg += "\nFirst item: \n" + self._imgInfo
                msg += '\n' + mdStr
            else:
                mdStr = self._getMdString(filename, blocks[0])
                msg += "  (%d blocks) " % nblocks
                if self._imgInfo:
                    msg += "\nFirst item: \n" + self._imgInfo
                msg += "\nFirst block: \n" + mdStr
                msg += "\nAll blocks:" + ''.join(
                    ["\n  - %s" % b for b in blocks])
        elif ext == '.star':
            msg = "*Relion STAR file* \n"
            msg += self._getMdString(filename)

        return self._imgPreview, msg
    def _pickMicrograph(self, mic, *args):
        micPath = mic.getFileName()
        # Get particle picking boxsize from the previous run
        boxSize = self.particlePickingRun.outputCoordinates.getBoxSize()
        modelRoot = self._getExtraPath('model')

        micName = removeBaseExt(micPath)
        proceed = True
        if self.micsToPick == MICS_SAMEASPICKING:
            basePos = replaceBaseExt(micPath, "pos")
            fnPos = self.particlePickingRun._getExtraPath(basePos)
            if exists(fnPos):
                blocks = xmippLib.getBlocksInMetaDataFile(fnPos)
                copy = True
                if 'header' in blocks:
                    mdheader = xmippLib.MetaData("header@" + fnPos)
                    state = mdheader.getValue(
                        xmippLib.MDL_PICKING_MICROGRAPH_STATE,
                        mdheader.firstObject())
                    if state == "Available":
                        copy = False
                if copy:
                    # Copy manual .pos file of this micrograph
                    copyFile(fnPos, self._getExtraPath(basename(fnPos)))
                    proceed = False

        if proceed:
            args = "-i %s " % micPath
            args += "--particleSize %d " % boxSize
            args += "--model %s " % modelRoot
            args += "--outputRoot %s " % self._getExtraPath(micName)
            args += "--mode autoselect --thr %d" % self.numberOfThreads

            self.runJob("xmipp_micrograph_automatic_picking", args)
def runInitAngularReferenceFileStep(self):
    '''Create Initial angular file. Either fill it with zeros or copy input'''
    #NOTE: if using angles, self.selFileName file should contain angles info
    md = xmippLib.MetaData(self.selFileName)

    # Ensure this labels are always
    md.addLabel(xmippLib.MDL_ANGLE_ROT)
    md.addLabel(xmippLib.MDL_ANGLE_TILT)
    md.addLabel(xmippLib.MDL_ANGLE_PSI)

    expImages = self._getFileName('inputParticlesDoc')
    ctfImages = self._getFileName('imageCTFpairs')

    md.write(self._getExpImagesFileName(expImages))
    blocklist = xmippLib.getBlocksInMetaDataFile(ctfImages)

    mdCtf = xmippLib.MetaData()
    mdAux = xmippLib.MetaData()
    readLabels = [xmippLib.MDL_ITEM_ID, xmippLib.MDL_IMAGE]

    for block in blocklist:
        #read ctf block from ctf file
        mdCtf.read(block + '@' + ctfImages, readLabels)
        #add ctf columns to images file
        mdAux.joinNatural(md, mdCtf)
        # write block in images file with ctf info
        mdCtf.write(block + '@' + expImages, xmippLib.MD_APPEND)

    return [expImages]
    def classifyGroupsStep(self):
        # Create two metadatas, one for classes and another one for images
        mdClasses = xmippLib.MetaData()
        mdImages = xmippLib.MetaData()

        fnNeighbours = self._getExtraPath("neighbours.xmd")
        fnGallery = self._getExtraPath("gallery.stk")

        self.classCount = 0
        self.classImages = set()

        for block in xmippLib.getBlocksInMetaDataFile(fnNeighbours):
            # Figure out the projection number from the block name
            projNumber = int(block.split("_")[1])

            self.classifyOneGroup(projNumber,
                                  projMdBlock="%s@%s" % (block, fnNeighbours),
                                  projRef="%06d@%s" % (projNumber, fnGallery),
                                  mdClasses=mdClasses,
                                  mdImages=mdImages)

        galleryMd = xmippLib.MetaData(self._getExtraPath("gallery.doc"))
        # Increment the reference number to starts from 1
        galleryMd.operate("ref=ref+1")
        mdJoined = xmippLib.MetaData()
        # Add extra information from the gallery metadata
        mdJoined.join1(mdClasses, galleryMd, xmippLib.MDL_REF)
        # Remove unnecessary columns
        md.keepColumns(mdJoined, "ref", "ref2", "image", "image1",
                       "classCount", "angleRot", "angleTilt")

        # Write both classes and images
        fnDirectional = self._getDirectionalClassesFn()
        self.info("Writting classes info to: %s" % fnDirectional)
        mdJoined.write(fnDirectional)

        fnDirectionalImages = self._getDirectionalImagesFn()
        self.info("Writing images info to: %s" % fnDirectionalImages)
        mdImages.write(fnDirectionalImages)