예제 #1
0
    def _createCluster(self):
        """ Create the cluster with the selected particles
        from the cluster. This method will be called when
        the button 'Create Cluster' is pressed.
        """
        # Write the particles
        prot = self.protocol
        project = prot.getProject()
        inputSet = prot.getInputParticles()
        fnSqlite = prot._getTmpPath('cluster_particles.sqlite')
        cleanPath(fnSqlite)
        partSet = SetOfParticles(filename=fnSqlite)
        partSet.copyInfo(inputSet)
        for point in self.getData():
            if point.getState() == Point.SELECTED:
                particle = inputSet[point.getId()]
                partSet.append(particle)
        partSet.write()
        partSet.close()

        from protocol_batch_cluster import BatchProtNMACluster
        newProt = project.newProtocol(BatchProtNMACluster)
        clusterName = self.clusterWindow.getClusterName()
        if clusterName:
            newProt.setObjLabel(clusterName)
        newProt.inputNmaDimred.set(prot)
        newProt.sqliteFile.set(fnSqlite)

        project.launchProtocol(newProt)
예제 #2
0
    def _createSubSetFromMicrographsTiltPair(self, micrographsTiltPair):
        """ Create a subset of Micrographs Tilt Pair. """
        output = MicrographsTiltPair(filename=self._getPath('micrographs_pairs.sqlite'))
        print "self._dbName=%s" % self._dbName
        modifiedSet = MicrographsTiltPair(filename=self._dbName, prefix=self._dbPrefix)
        inputU = micrographsTiltPair.getUntilted()
        inputT = micrographsTiltPair.getTilted()
        outputU = SetOfMicrographs(filename=self._getPath('mics_untilted.sqlite'))
        outputT = SetOfParticles(filename=self._getPath('mics_tilted.sqlite'))
        outputU.copyInfo(inputU)
        outputT.copyInfo(inputT)
        for micPairI in modifiedSet:
            untilted = micPairI.getUntilted()
            tilted = micPairI.getTilted()
            if micPairI.isEnabled():

                micPairO = TiltPair()
                micPairO.setUntilted(untilted)
                micPairO.setTilted(tilted)
                output.append(micPairO)
                outputU.append(untilted)
                outputT.append(tilted)
        output.setUntilted(outputU)
        output.setTilted(outputT)
        # Register outputs
        outputDict = {'outputMicrographsTiltPair': output}
        self._defineOutputs(**outputDict)
        self._defineTransformRelation(micrographsTiltPair, output)
        return output
예제 #3
0
    def _createSubSetFromParticlesTiltPair(self, particlesTiltPair):
        print 'create subset from particles tilt pair'
        """ Create a subset of Micrographs Tilt Pair. """
        output = ParticlesTiltPair(filename=self._getPath('particles_pairs.sqlite'))
        
        inputU = particlesTiltPair.getUntilted()
        inputT = particlesTiltPair.getTilted()
        outputU = SetOfParticles(filename=self._getPath('particles_untilted.sqlite'))
        outputT = SetOfParticles(filename=self._getPath('particles_tilted.sqlite'))
        outputU.copyInfo(inputU)
        outputT.copyInfo(inputT)
        
        modifiedSet = ParticlesTiltPair(filename=self._dbName, prefix=self._dbPrefix)

        for pair, u, t in izip(modifiedSet, inputU, inputT):
            if pair.isEnabled():
                output.append(pair)
                outputU.append(u)
                outputT.append(t)
        # Register outputs
        output.setUntilted(outputU)
        output.setTilted(outputT)
        
        outputDict = {'outputParticlesTiltPair': output}
        self._defineOutputs(**outputDict)
        self._defineTransformRelation(particlesTiltPair, output)
        return output
예제 #4
0
    def _createSubSetFromParticlesTiltPair(self, particlesTiltPair):
        """ Create a subset of Micrographs Tilt Pair. """
        output = ParticlesTiltPair(
            filename=self._getPath('particles_pairs.sqlite'))

        inputU = particlesTiltPair.getUntilted()
        inputT = particlesTiltPair.getTilted()
        outputU = SetOfParticles(
            filename=self._getPath('particles_untilted.sqlite'))
        outputT = SetOfParticles(
            filename=self._getPath('particles_tilted.sqlite'))
        outputU.copyInfo(inputU)
        outputT.copyInfo(inputT)

        modifiedSet = ParticlesTiltPair(filename=self._dbName,
                                        prefix=self._dbPrefix)

        for pair, u, t in izip(modifiedSet, inputU, inputT):
            if pair.isEnabled():
                output.append(pair)
                outputU.append(u)
                outputT.append(t)
        # Register outputs
        output.setUntilted(outputU)
        output.setTilted(outputT)

        outputDict = {'outputParticlesTiltPair': output}
        self._defineOutputs(**outputDict)
        self._defineTransformRelation(particlesTiltPair, output)
        return output
예제 #5
0
 def _createCluster(self):
     """ Create the cluster with the selected particles
     from the cluster. This method will be called when
     the button 'Create Cluster' is pressed.
     """
     # Write the particles
     prot = self.protocol
     project = prot.getProject()
     inputSet = prot.getInputParticles()
     fnSqlite = prot._getTmpPath('cluster_particles.sqlite')
     cleanPath(fnSqlite)
     partSet = SetOfParticles(filename=fnSqlite)
     partSet.copyInfo(inputSet)
     for point in self.getData():
         if point.getState() == Point.SELECTED:
             particle = inputSet[point.getId()]
             partSet.append(particle)
     partSet.write()
     partSet.close()
             
     from protocol_batch_cluster import BatchProtNMACluster
     newProt = project.newProtocol(BatchProtNMACluster)
     clusterName = self.clusterWindow.getClusterName()
     if clusterName:
         newProt.setObjLabel(clusterName)
     newProt.inputNmaDimred.set(prot)
     newProt.sqliteFile.set(fnSqlite)
     
     project.launchProtocol(newProt)
예제 #6
0
    def _createSubSetFromMicrographsTiltPair(self, micrographsTiltPair):
        """ Create a subset of Micrographs Tilt Pair. """
        output = MicrographsTiltPair(
            filename=self._getPath('micrographs_pairs.sqlite'))
        modifiedSet = MicrographsTiltPair(filename=self._dbName,
                                          prefix=self._dbPrefix)
        inputU = micrographsTiltPair.getUntilted()
        inputT = micrographsTiltPair.getTilted()
        outputU = SetOfMicrographs(
            filename=self._getPath('mics_untilted.sqlite'))
        outputT = SetOfParticles(filename=self._getPath('mics_tilted.sqlite'))
        outputU.copyInfo(inputU)
        outputT.copyInfo(inputT)

        for micPairI in modifiedSet:
            untilted = micPairI.getUntilted()
            tilted = micPairI.getTilted()

            if micPairI.isEnabled():
                micPairO = TiltPair()
                micPairO.setUntilted(untilted)
                micPairO.setTilted(tilted)
                output.append(micPairO)
                outputU.append(untilted)
                outputT.append(tilted)
        output.setUntilted(outputU)
        output.setTilted(outputT)
        # Register outputs
        outputDict = {'outputMicrographsTiltPair': output}
        self._defineOutputs(**outputDict)
        self._defineTransformRelation(micrographsTiltPair, output)
        return output
    def getParticles(self, protImport, classid):
        dbPartSet = protImport._getPath("particles_class-%d.sqlite" % classid)
        class3D = protImport.outputClasses[classid]
        if os.path.exists(dbPartSet):
            os.remove(dbPartSet)
        partSet = SetOfParticles(filename=dbPartSet)
        partSet.copyInfo(class3D)

        for part in class3D:
            partSet.append(part)
        partSet.write()
        partSet.close()

        protImportCls1 = self.newProtocol(
            ProtImportParticles,
            objLabel='particles class-%d' % classid,
            importFrom=ProtImportParticles.IMPORT_FROM_SCIPION,
            sqliteFile=dbPartSet,
            magnification=10000,
            samplingRate=7.08,
            haveDataBeenPhaseFlipped=True)
        self.launchProtocol(protImportCls1)
        self.assertIsNotNone(protImportCls1.outputParticles.getFileName(),
                             "There was a problem with the import")
        return protImportCls1
예제 #8
0
    def createOutputStep(self):
        # Create the SetOfImages object on the database
        #imgSet = XmippSetOfParticles(self._getPath('images.xmd'))

        fnImages = self._getOutputImgMd()
        # Create output SetOfParticles
        imgSet = self._createSetOfParticles()
        imgSet.copyInfo(self.inputMics)
        if self.doFlip:
            # Check if self.inputMics are phase flipped.
            if self.inputMics.isPhaseFlipped():
                imgSet.setIsPhaseFlipped(False)
            else:
                imgSet.setIsPhaseFlipped(True)

        #imgSet.setHasCTF(self.fnCTF is not None)
        if self.downsampleType == OTHER:
            imgSet.setSamplingRate(self.inputMics.getSamplingRate() *
                                   self.downFactor.get())
        imgSet.setCoordinates(self.inputCoords)

        # Create a temporary set to read from the metadata file
        # and later create the good one with the coordinates
        # properly set. We need this because the .update is not
        # working in the mapper when new attributes are added.
        imgSet.setHasCTF(self.ctfRelations.hasValue())
        auxSet = SetOfParticles(filename=':memory:')
        auxSet.copyInfo(imgSet)
        readSetOfParticles(fnImages, auxSet)

        if self.downsampleType != SAME_AS_PICKING:
            factor = self.samplingInput / self.samplingFinal
        # For each particle retrieve micId from SetOFCoordinates and set it on the CTFModel
        for img in auxSet:
            #FIXME: This can be slow to make a query to grab the coord, maybe use zip(imgSet, coordSet)???
            coord = self.inputCoords[img.getObjId()]
            if self.downsampleType != SAME_AS_PICKING:
                x, y = coord.getPosition()
                coord.setPosition(x * factor, y * factor)
            ctfModel = img.getCTF()
            if ctfModel is not None:
                ctfModel.setObjId(coord.getMicId())
                ##img.setCTF(ctfModel)####JM
            img.setMicId(coord.getMicId())
            img.setCoordinate(coord)
            imgSet.append(img)

        self._storeMethodsInfo(fnImages)
        self._defineOutputs(outputParticles=imgSet)
        self._defineSourceRelation(self.inputCoordinates, imgSet)
        if self.ctfRelations.hasValue():
            self._defineSourceRelation(self.ctfRelations.get(), imgSet)
 def createOutputStep(self):
     # Create the SetOfImages object on the database
     #imgSet = XmippSetOfParticles(self._getPath('images.xmd'))
     
     fnImages = self._getOutputImgMd()
     # Create output SetOfParticles
     imgSet = self._createSetOfParticles()
     imgSet.copyInfo(self.inputMics)
     if self.doFlip:
         # Check if self.inputMics are phase flipped.
         if self.inputMics.isPhaseFlipped():
             imgSet.setIsPhaseFlipped(False)
         else:
             imgSet.setIsPhaseFlipped(True)
     
     #imgSet.setHasCTF(self.fnCTF is not None)
     if self.downsampleType == OTHER:
         imgSet.setSamplingRate(self.inputMics.getSamplingRate()*self.downFactor.get())
     imgSet.setCoordinates(self.inputCoords)
     
     # Create a temporary set to read from the metadata file
     # and later create the good one with the coordinates 
     # properly set. We need this because the .update is not
     # working in the mapper when new attributes are added.
     imgSet.setHasCTF(self.ctfRelations.hasValue())
     auxSet = SetOfParticles(filename=':memory:')
     auxSet.copyInfo(imgSet)
     readSetOfParticles(fnImages, auxSet)
     
     if self.downsampleType != SAME_AS_PICKING:
         factor = self.samplingInput / self.samplingFinal
     # For each particle retrieve micId from SetOFCoordinates and set it on the CTFModel
     for img in auxSet:
         #FIXME: This can be slow to make a query to grab the coord, maybe use zip(imgSet, coordSet)???
         coord = self.inputCoords[img.getObjId()]
         if self.downsampleType != SAME_AS_PICKING:
             x, y = coord.getPosition()
             coord.setPosition(x*factor, y*factor)           
         ctfModel = img.getCTF()
         if ctfModel is not None:
             ctfModel.setObjId(coord.getMicId())
             ##img.setCTF(ctfModel)####JM
         img.setMicId(coord.getMicId())
         img.setCoordinate(coord)
         imgSet.append(img)
         
     self._storeMethodsInfo(fnImages)
     self._defineOutputs(outputParticles=imgSet)
     self._defineSourceRelation(self.inputCoordinates, imgSet)
     if self.ctfRelations.hasValue():
         self._defineSourceRelation(self.ctfRelations.get(), imgSet)
예제 #10
0
    def _allParticles(self, iterate=False):
        # A handler function to iterate over the particles
        inputSet = self.inputSet.get()

        if self.isInputClasses():
            iterParticles = inputSet.iterClassItems()
            if iterate:
                return iterParticles
            else:
                particles = SetOfParticles(filename=":memory:")
                particles.copyInfo(inputSet.getFirstItem())
                particles.copyItems(iterParticles)
                return particles
        else:
            if iterate:
                return inputSet.iterItems()
            else:
                return inputSet
예제 #11
0
    def createOutputStep(self):
        fnTilted = self._getExtraPath("images_tilted.xmd")
        fnUntilted = self._getExtraPath("images_untilted.xmd")

        # Create outputs SetOfParticles both for tilted and untilted
        imgSetU = self._createSetOfParticles(suffix="Untilted")
        imgSetU.copyInfo(self.uMics)
        imgSetT = self._createSetOfParticles(suffix="Tilted")
        imgSetT.copyInfo(self.tMics)

        sampling = self.getMicSampling() if self._micsOther(
        ) else self.getCoordSampling()
        if self._doDownsample():
            sampling *= self.downFactor.get()
        imgSetU.setSamplingRate(sampling)
        imgSetT.setSamplingRate(sampling)

        # set coords from the input, will update later if needed
        imgSetU.setCoordinates(
            self.inputCoordinatesTiltedPairs.get().getUntilted())
        imgSetT.setCoordinates(
            self.inputCoordinatesTiltedPairs.get().getTilted())

        # Read untilted and tilted particles on a temporary object (also disabled particles)
        imgSetAuxU = SetOfParticles(filename=':memory:')
        imgSetAuxU.copyInfo(imgSetU)
        readSetOfParticles(fnUntilted, imgSetAuxU, removeDisabled=False)

        imgSetAuxT = SetOfParticles(filename=':memory:')
        imgSetAuxT.copyInfo(imgSetT)
        readSetOfParticles(fnTilted, imgSetAuxT, removeDisabled=False)

        # calculate factor for coords scaling
        factor = 1 / self.samplingFactor
        if self._doDownsample():
            factor /= self.downFactor.get()

        coordsT = self.getCoords().getTilted()
        # For each untilted particle retrieve micId from SetOfCoordinates untilted
        for imgU, coordU in izip(imgSetAuxU, self.getCoords().getUntilted()):
            # FIXME: Remove this check when sure that objIds are equal
            id = imgU.getObjId()
            if id != coordU.getObjId():
                raise Exception(
                    'ObjIds in untilted img and coord are not the same!!!!')
            imgT = imgSetAuxT[id]
            coordT = coordsT[id]

            # If both particles are enabled append them
            if imgU.isEnabled() and imgT.isEnabled():
                if self._micsOther() or self._doDownsample():
                    coordU.scale(factor)
                    coordT.scale(factor)
                imgU.setCoordinate(coordU)
                imgSetU.append(imgU)
                imgT.setCoordinate(coordT)
                imgSetT.append(imgT)

        if self.doFlip:
            imgSetU.setIsPhaseFlipped(self.ctfUntilt.hasValue())
            imgSetU.setHasCTF(self.ctfUntilt.hasValue())
            imgSetT.setIsPhaseFlipped(self.ctfTilt.hasValue())
            imgSetT.setHasCTF(self.ctfTilt.hasValue())
        imgSetU.write()
        imgSetT.write()

        # Define output ParticlesTiltPair
        outputset = ParticlesTiltPair(
            filename=self._getPath('particles_pairs.sqlite'))
        outputset.setTilted(imgSetT)
        outputset.setUntilted(imgSetU)
        for imgU, imgT in izip(imgSetU, imgSetT):
            outputset.append(TiltPair(imgU, imgT))

        outputset.setCoordsPair(self.inputCoordinatesTiltedPairs.get())
        self._defineOutputs(outputParticlesTiltPair=outputset)
        self._defineSourceRelation(self.inputCoordinatesTiltedPairs, outputset)
    def createOutputStep(self):
        # Create the SetOfImages object on the database
        #imgSet = XmippSetOfParticles(self._getPath('images.xmd'))
                  
        #Create images.xmd metadata
        fnImages = self._getPath('images.xmd')
        imgsXmd = xmipp.MetaData() 
        posFiles = glob(self._getExtraPath('*.pos')) 
        for posFn in posFiles:
            xmdFn = self._getExtraPath(replaceBaseExt(posFn, "xmd"))
            if exists(xmdFn):
                md = xmipp.MetaData(xmdFn)
                mdPos = xmipp.MetaData('particles@%s' % posFn)
                mdPos.merge(md) 
                #imgSet.appendFromMd(mdPos)
                imgsXmd.unionAll(mdPos)
            else:
                self.warning("The coord file %s wasn't used to extract! Maybe you are extracting over a subset of micrographs" % basename(posFn))
        imgsXmd.write(fnImages)

        # IF selected run xmipp_image_sort_by_statistics to add zscore info to images.xmd
        if self.doSort:
            args="-i %(fnImages)s --addToInput"
            if self.rejectionMethod == REJECT_MAXZSCORE:
                maxZscore = self.maxZscore.get()
                args += " --zcut " + str(maxZscore)
            elif self.rejectionMethod == REJECT_PERCENTAGE:
                percentage = self.percentage.get()
                args += " --percent " + str(percentage)

            self.runJob("xmipp_image_sort_by_statistics", args % locals())
        # Create output SetOfParticles
        imgSet = self._createSetOfParticles()
        imgSet.copyInfo(self.inputMics)
        if self.doFlip:
            # Check if self.inputMics are phase flipped.
            if self.inputMics.isPhaseFlipped():
                imgSet.setIsPhaseFlipped(False)
            else:
                imgSet.setIsPhaseFlipped(True)
        
        #imgSet.setHasCTF(self.fnCTF is not None)
        if self.downsampleType == OTHER:
            imgSet.setSamplingRate(self.inputMics.getSamplingRate()*self.downFactor.get())
        imgSet.setCoordinates(self.inputCoords)
        
        # Create a temporary set to read from the metadata file
        # and later create the good one with the coordinates 
        # properly set. We need this because the .update is not
        # working in the mapper when new attributes are added.
        imgSet.setHasCTF(self.ctfRelations.hasValue())
        auxSet = SetOfParticles(filename=':memory:')
        auxSet.copyInfo(imgSet)
        readSetOfParticles(fnImages, auxSet)
        # For each particle retrieve micId from SetOFCoordinates and set it on the CTFModel
        for img in auxSet:
            #FIXME: This can be slow to make a query to grab the coord, maybe use zip(imgSet, coordSet)???
            coord = self.inputCoords[img.getObjId()]
            ctfModel = img.getCTF()
            if ctfModel is not None:
                ctfModel.setObjId(coord.getMicId())
                ##img.setCTF(ctfModel)####JM
            img.setMicId(coord.getMicId())
            img.setCoordinate(coord)
            imgSet.append(img)
            
        self._storeMethodsInfo(fnImages)
        self._defineOutputs(outputParticles=imgSet)
        self._defineSourceRelation(self.inputCoords, imgSet)
예제 #13
0
    def launchTest(self, fileKey, mList, alignType=None, **kwargs):
        """ Helper function to launch similar alignment tests
        give the EMX transformation matrix.
        Params:
            fileKey: the file where to grab the input stack images.
            mList: the matrix list of transformations
                (should be the same length of the stack of images)
        """
        print "\n"
        print "*" * 80
        print "* Launching test: ", fileKey
        print "*" * 80

        is2D = alignType == ALIGN_2D

        stackFn = self.dataset.getFile(fileKey)
        partFn1 = self.getOutputPath(fileKey + "_particles1.sqlite")
        mdFn = self.getOutputPath(fileKey + "_particles.star")
        partFn2 = self.getOutputPath(fileKey + "_particles2.sqlite")

        if self.IS_ALIGNMENT:
            outputFn = self.getOutputPath(fileKey + "_output.mrcs")
            outputFnRelion = self.getOutputPath(fileKey + "_output")
            goldFn = self.dataset.getFile(fileKey + '_Gold_output_relion.mrcs')
        else:
            outputFn = self.getOutputPath(fileKey + "_output.vol")
            goldFn = self.dataset.getFile(fileKey + '_Gold_output.vol')

        if PRINT_FILES:
            print "BINARY DATA: ", stackFn
            print "SET1:        ", partFn1
            print "  MD:        ", mdFn
            print "SET2:        ", partFn2
            print "OUTPUT:      ", outputFn
            print "GOLD:        ", goldFn

        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            partSet = SetOfParticles(filename=partFn1)
        else:
            partSet = SetOfVolumes(filename=partFn1)
        partSet.setAlignment(alignType)
        partSet.setAcquisition(
            Acquisition(voltage=300,
                        sphericalAberration=2,
                        amplitudeContrast=0.1,
                        magnification=60000))
        # Populate the SetOfParticles with  images
        # taken from images.mrc file
        # and setting the previous alignment parameters
        aList = [numpy.array(m) for m in mList]
        for i, a in enumerate(aList):
            p = Particle()
            p.setLocation(i + 1, stackFn)
            p.setTransform(Transform(a))
            partSet.append(p)
        # Write out the .sqlite file and check that are correctly aligned
        print "Parset", partFn1
        partSet.printAll()
        partSet.write()
        # Convert to a Xmipp metadata and also check that the images are
        # aligned correctly
        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            relion.writeSetOfParticles(partSet,
                                       mdFn,
                                       "/tmp",
                                       alignType=alignType)
            partSet2 = SetOfParticles(filename=partFn2)
        else:
            relion.writeSetOfVolumes(partSet, mdFn, alignType=alignType)
            partSet2 = SetOfVolumes(filename=partFn2)
        # Let's create now another SetOfImages reading back the written
        # Xmipp metadata and check one more time.
        partSet2.copyInfo(partSet)
        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            relion.readSetOfParticles(mdFn, partSet2, alignType=alignType)
        else:
            relion.readSetOfVolumes(mdFn, partSet2, alignType=alignType)

        partSet2.write()

        if PRINT_MATRIX:
            for i, img in enumerate(partSet2):
                m1 = aList[i]
                m2 = img.getTransform().getMatrix()
                print "-" * 5
                print img.getFileName(), img.getIndex()
                print 'm1:\n', m1, relion.geometryFromMatrix(m1, False)

                print 'm2:\n', m2, relion.geometryFromMatrix(m2, False)
                # self.assertTrue(numpy.allclose(m1, m2, rtol=1e-2))

        # Launch apply transformation and check result images
        runRelionProgram(self.CMD % locals())

        if SHOW_IMAGES:
            runRelionProgram('scipion show %(outputFn)s' % locals())

        if os.path.exists(goldFn):
            self.assertTrue(
                ImageHandler().compareData(goldFn, outputFn, tolerance=0.001),
                "Different data files:\n>%s\n<%s" % (goldFn, outputFn))
    def createOutputStep(self):
        fnTilted = self._getExtraPath("images_tilted.xmd")
        fnUntilted = self._getExtraPath("images_untilted.xmd")

        # Create outputs SetOfParticles both for tilted and untilted
        imgSetU = self._createSetOfParticles(suffix="Untilted")
        imgSetU.copyInfo(self.uMics)
        imgSetT = self._createSetOfParticles(suffix="Tilted")
        imgSetT.copyInfo(self.tMics)

        sampling = self.samplingMics if self._micsOther() else self.samplingInput
        if self._doDownsample():
            sampling *= self.downFactor.get()
        imgSetU.setSamplingRate(sampling)
        imgSetT.setSamplingRate(sampling)

        # set coords from the input, will update later if needed
        imgSetU.setCoordinates(self.inputCoordinatesTiltedPairs.get().getUntilted())
        imgSetT.setCoordinates(self.inputCoordinatesTiltedPairs.get().getTilted())

        # Read untilted and tilted particles on a temporary object (also disabled particles)
        imgSetAuxU = SetOfParticles(filename=':memory:')
        imgSetAuxU.copyInfo(imgSetU)
        readSetOfParticles(fnUntilted, imgSetAuxU, removeDisabled=False)

        imgSetAuxT = SetOfParticles(filename=':memory:')
        imgSetAuxT.copyInfo(imgSetT)
        readSetOfParticles(fnTilted, imgSetAuxT, removeDisabled=False)

        # calculate factor for coords scaling
        factor = 1 / self.samplingFactor
        if self._doDownsample():
            factor /= self.downFactor.get()

        coordsT = self.inputCoordinatesTiltedPairs.get().getTilted()
        # For each untilted particle retrieve micId from SetOfCoordinates untilted
        for imgU, coordU in izip(imgSetAuxU, self.inputCoordinatesTiltedPairs.get().getUntilted()):
            # FIXME: Remove this check when sure that objIds are equal
            id = imgU.getObjId()
            if id != coordU.getObjId():
                raise Exception('ObjIds in untilted img and coord are not the same!!!!')
            imgT = imgSetAuxT[id]
            coordT = coordsT[id]

            # If both particles are enabled append them
            if imgU.isEnabled() and imgT.isEnabled():
                if self._micsOther() or self._doDownsample():
                    coordU.scale(factor)
                    coordT.scale(factor)
                imgU.setCoordinate(coordU)
                imgSetU.append(imgU)
                imgT.setCoordinate(coordT)
                imgSetT.append(imgT)

        imgSetU.write()
        imgSetT.write()

        # Define output ParticlesTiltPair
        outputset = ParticlesTiltPair(filename=self._getPath('particles_pairs.sqlite'))
        outputset.setTilted(imgSetT)
        outputset.setUntilted(imgSetU)
        for imgU, imgT in izip(imgSetU, imgSetT):
            outputset.append(TiltPair(imgU, imgT))

        outputset.setCoordsPair(self.inputCoordinatesTiltedPairs.get())
        self._defineOutputs(outputParticlesTiltPair=outputset)
        self._defineSourceRelation(self.inputCoordinatesTiltedPairs, outputset)
예제 #15
0
    def launchTest(self, fileKey, mList, alignType=None, **kwargs):
        """ Helper function to launch similar alignment tests
        give the EMX transformation matrix.
        Params:
            fileKey: the file where to grab the input stack images.
            mList: the matrix list of transformations
                (should be the same length of the stack of images)
        """
        print ("\n")
        print ("*" * 80)
        print ("* Launching test: ", fileKey)
        print ("*" * 80)

        is2D = alignType == ALIGN_2D

        stackFn = self.dataset.getFile(fileKey)
        partFn1 = self.getOutputPath(fileKey + "_particles1.sqlite")
        mdFn = self.getOutputPath(fileKey + "_particles.star")
        partFn2 = self.getOutputPath(fileKey + "_particles2.sqlite")

        if self.IS_ALIGNMENT:
            outputFn = self.getOutputPath(fileKey + "_output.mrcs")
            outputFnRelion = self.getOutputPath(fileKey + "_output")
            goldFn = self.dataset.getFile(fileKey + '_Gold_output_relion.mrcs')
        else:
            outputFn = self.getOutputPath(fileKey + "_output.vol")
            goldFn = self.dataset.getFile(fileKey + '_Gold_output.vol')

        if PRINT_FILES:
            print("BINARY DATA: ", stackFn)
            print("SET1:        ", partFn1)
            print("  MD:        ", mdFn)
            print("SET2:        ", partFn2)
            print("OUTPUT:      ", outputFn)
            print("GOLD:        ", goldFn)

        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            partSet = SetOfParticles(filename=partFn1)
        else:
            partSet = SetOfVolumes(filename=partFn1)
        partSet.setAlignment(alignType)
        partSet.setAcquisition(Acquisition(voltage=300,
                                           sphericalAberration=2,
                                           amplitudeContrast=0.1,
                                           magnification=60000))
        # Populate the SetOfParticles with  images
        # taken from images.mrc file
        # and setting the previous alignment parameters
        aList = [numpy.array(m) for m in mList]
        for i, a in enumerate(aList):
            p = Particle()
            p.setLocation(i + 1, stackFn)
            p.setTransform(Transform(a))
            partSet.append(p)
        # Write out the .sqlite file and check that are correctly aligned
        print ("Parset", partFn1)
        partSet.printAll()
        partSet.write()
        # Convert to a Xmipp metadata and also check that the images are
        # aligned correctly
        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            relion.writeSetOfParticles(partSet, mdFn,"/tmp", alignType=alignType)
            partSet2 = SetOfParticles(filename=partFn2)
        else:
            relion.writeSetOfVolumes(partSet, mdFn, alignType=alignType)
            partSet2 = SetOfVolumes(filename=partFn2)
        # Let's create now another SetOfImages reading back the written
        # Xmipp metadata and check one more time.
        partSet2.copyInfo(partSet)
        if alignType == ALIGN_2D or alignType == ALIGN_PROJ:
            relion.readSetOfParticles(mdFn, partSet2, alignType=alignType)
        else:
            relion.readSetOfVolumes(mdFn, partSet2, alignType=alignType)

        partSet2.write()

        if PRINT_MATRIX:
            for i, img in enumerate(partSet2):
                m1 = aList[i]
                m2 = img.getTransform().getMatrix()
                print ("-" * 5)
                print (img.getFileName(), img.getIndex())
                print ('m1:\n', m1, relion.geometryFromMatrix(m1, False))

                print ('m2:\n', m2, relion.geometryFromMatrix(m2, False))
                # self.assertTrue(numpy.allclose(m1, m2, rtol=1e-2))

        # Launch apply transformation and check result images
        runRelionProgram(self.CMD % locals())

        if SHOW_IMAGES:
            runRelionProgram('scipion show %(outputFn)s' % locals())

        if os.path.exists(goldFn):
            self.assertTrue(
                ImageHandler().compareData(goldFn, outputFn, tolerance=0.001),
                "Different data files:\n>%s\n<%s" % (goldFn, outputFn))