Esempio n. 1
0
 def invertStack(self, inputFn, outputFn):
     # get input dim
     (x, y, z, n) = xmippLib.getImageSize(inputFn)
     # Create empty output stack for efficiency
     xmippLib.createEmptyFile(outputFn, x, y, z, n)
     # handle image formats
     for i in range(1, n + 1):
         self.invert((i, inputFn), (i, outputFn))
Esempio n. 2
0
    def convertStack(self,
                     inputFn,
                     outputFn,
                     firstImg=None,
                     lastImg=None,
                     inFormat=None,
                     outFormat=None):
        """ Convert an input stack file into another.
        It is possible to only use a subset of frames to be written in the
            output stack.
        If outFormat/inFomat=None then there will be
        inferred from extension.If firstFrame/lastFrame are not None, the output
        stack will be a subset of input stack. If it are none, the conversion is
        over the whole stack. If the input format is ".dm4" or  ".img" only is
        allowed the conversion of the whole stack.
        """
        # inputLower = inputFn.lower()
        outputLower = outputFn.lower()
        if outputLower.endswith('.img'):
            if (firstImg and lastImg) is None:
                # FIXME Since now we can not read dm4 format in Scipion natively
                # or writing recent .img format
                # we are opening an Eman2 process to read the dm4 file
                convertImage = pwutils.importFromPlugin(
                    'eman2.convert', 'convertImage')
                convertImage(inputFn, outputFn)
            else:
                ext = os.path.splitext(outputFn)[1]
                raise Exception("if convert from %s, firstImg and lastImg "
                                "must be None" % ext)
        # elif inputLower.endswith('.tif'):
        #     # FIXME: It seems that we have some flip problem with compressed
        #     # tif files, we need to check that
        #     if outputLower.endswith('.mrc'):
        #         self.runJob('tif2mrc', '%s %s' % (inputFn, outputFn))
        #     else:
        #         raise Exception("Conversion from tif to %s is not "
        #                        "implemented yet. " % pwutils.getExt(outputFn))
        else:
            # get input dim
            (x, y, z, n) = xmippLib.getImageSize(inputFn)

            location = self._convertToLocation(inputFn)
            self._img.read(location, xmippLib.HEADER)
            dataType = self.getSupportedDataType(self._img.getDataType(),
                                                 outputLower)

            if (firstImg and lastImg) is None:
                n = max(z, n)
                firstImg = 1
                lastImg = n
            else:
                n = lastImg - firstImg + 1

            # Create empty output stack file to reserve desired space
            xmippLib.createEmptyFile(outputFn, x, y, 1, n, dataType)
            for i, j in izip(range(firstImg, lastImg + 1), range(1, n + 1)):
                self.convert((i, inputFn), (j, outputFn))
Esempio n. 3
0
 def createEmptyImage(cls,
                      fnOut,
                      xDim=1,
                      yDim=1,
                      zDim=1,
                      nDim=1,
                      dataType=None):
     dt = dataType or cls.DT_FLOAT
     xmippLib.createEmptyFile(fnOut, xDim, yDim, zDim, nDim, dt)
Esempio n. 4
0
 def createMaskFromGeometryStep(self):
     # Create empty volume file with desired dimensions
     size = self.size.get()
     xmippLib.createEmptyFile(self.maskFile, size, size, size)
     
     # Create the mask
     args = '-i %s ' % self.maskFile
     args += XmippGeometricalMask3D.argsForTransformMask(self,size)
     args += ' --create_mask %s' % self.maskFile
     self.runJob("xmipp_transform_mask", args)
     
     return [self.maskFile]
    def convertInputStep(self, volName):
        # Read volume and convert to DOUBLE
        self.vol = xmippLib.Image(volName)
        self.vol.convert2DataType(xmippLib.DT_DOUBLE)
        # Mask volume if needed
        if self.refMask.get() is not None:
            maskName = getImageLocation(self.refMask.get())
            self.mask = xmippLib.Image(maskName)
            self.mask.convert2DataType(xmippLib.DT_DOUBLE)
            self.vol.inplaceMultiply(self.mask)
        padding = 2
        maxFreq = 0.5
        splineDegree = 3
        ###
        self.fourierProjectVol = xmippLib.FourierProjector(self.vol, padding, maxFreq, splineDegree)
        ###
        partSet = self.inputParticles.get()
        nPart = len(partSet)
        numberOfTasks = min(nPart, max(self.numberOfThreads.get()-1, 1))
        taskSize = nPart / numberOfTasks
        md = xmippLib.MetaData()

        # Convert angles and shifts from volume system of coordinates to
        # projection system of coordinates
        mdCount = 0

        for index, part in enumerate(partSet):
            objId = md.addObject()
            imgRow = XmippMdRow()
            particleToRow(part, imgRow)
            shifts, angles = geometryFromMatrix(part.getTransform().getMatrix(), True)

            imgRow.setValue(xmippLib.MDL_SHIFT_X,-shifts[0])
            imgRow.setValue(xmippLib.MDL_SHIFT_Y,-shifts[1])
            imgRow.setValue(xmippLib.MDL_SHIFT_Z,0.)
            imgRow.setValue(xmippLib.MDL_ANGLE_ROT,angles[0])
            imgRow.setValue(xmippLib.MDL_ANGLE_TILT,angles[1])
            imgRow.setValue(xmippLib.MDL_ANGLE_PSI,angles[2])

            imgRow.writeToMd(md, objId)

            # Write a new metadata every taskSize number of elements
            # except in the last chunk where we want to add also the
            # remainder and the condition is the last element
            if ((index % taskSize == taskSize-1 and
                 mdCount < numberOfTasks-1) or index == nPart-1):
                md.write(self._getInputParticlesSubsetFn(mdCount))
                md.clear()
                mdCount += 1

        x, y, _ = partSet.getDim()
        xmippLib.createEmptyFile(self._getProjGalleryFn(), x, y, 1, nPart)
    def predictModel(self):
        from keras.models import load_model
        metadataPart = xmippLib.MetaData(
            self._getExtraPath('resizedParticles.xmd'))
        if self.model.get() == ITER_TRAIN:
            metadataProj = xmippLib.MetaData(
                self._getExtraPath('resizedProjections.xmd'))
        img = xmippLib.Image()
        dimMetadata = getMdSize(self._getExtraPath('resizedParticles.xmd'))
        xmippLib.createEmptyFile(self._getExtraPath('particlesDenoised.stk'),
                                 self.newXdim, self.newXdim, 1, dimMetadata)

        mdNewParticles = md.MetaData()

        self.groupParticles = 5000

        if self.model.get() == ITER_PREDICT:
            if self.modelPretrain:
                model = self.ownModel.get()._getPath('ModelTrained.h5')
                model = load_model(model)
            else:
                myModelfile = xmipp3.Plugin.getModel('deepDenoising',
                                                     'PretrainModel.h5')
                model = load_model(myModelfile)
        for num in range(1, dimMetadata, self.groupParticles):
            self.noisyParticles = self.gan.extractInfoMetadata(
                metadataPart, xmippLib.MDL_IMAGE, img, num,
                self.groupParticles, -1)

            if self.model.get() == ITER_TRAIN:
                self.predict = self.gan.predict(self.Generator,
                                                self.noisyParticles)
                self.projections = self.gan.extractInfoMetadata(
                    metadataProj, xmippLib.MDL_IMAGE, img, num,
                    self.groupParticles, 1)
            else:
                self.predict = self.gan.predict(model, self.noisyParticles)

            self.noisyParticles = self.gan.normalization(
                self.noisyParticles, 1)
            self.prepareMetadata(mdNewParticles, img, num)

        mdNewParticles.write(
            'particles@' + self._getExtraPath('particlesDenoised.xmd'),
            xmippLib.MD_APPEND)
        self.runJob(
            "xmipp_transform_normalize", "-i %s --method NewXmipp "
            "--background circle %d " %
            (self._getExtraPath('particlesDenoised.stk'), self.newXdim / 2))
 def convertInputStep(self):
     x, y, _ = self.inputParticles.get().getDimensions()
     n = self.inputParticles.get().getSize()
     createEmptyFile(self._getPath("images.mrc"), x, y, 1, n)
Esempio n. 8
0
    def predictDenoised(self,
                        model,
                        boxSize,
                        inputParticlesMdName,
                        dataPathProjections=None):
        from scipy.stats import pearsonr

        inputParticlesStackName = re.sub(r"\.xmd$", ".stk",
                                         inputParticlesMdName)
        outputParticlesMdName = self.getParam('-o')
        outputParticlesStackName = re.sub(r"\.xmd$", ".stk",
                                          outputParticlesMdName)

        useProjections = not dataPathProjections

        if useProjections:
            inputProjectionsStackName = re.sub(r"\.xmd$", ".stk",
                                               dataPathProjections)
            metadataProjections = xmippLib.MetaData(inputProjectionsStackName)
        else:
            metadataProjections = None

        dimMetadata = getMdSize(inputParticlesMdName)
        xmippLib.createEmptyFile(outputParticlesStackName, boxSize, boxSize, 1,
                                 dimMetadata)

        mdNewParticles = xmippLib.MetaData()

        I = xmippLib.Image()
        i = 0
        # yieldPredictions will compute the denoised particles from the particles contained in inputParticlesMdName and
        # it will yield a batch of denoisedParts, inputParts (and projectionParts if inputProjectionsStackName provided)
        for preds, particles, projections in model.yieldPredictions(
                inputParticlesMdName, metadataProjections):
            newRow = XmippMdRow()
            for pred, particle, projection in zip(
                    preds, particles,
                    projections):  # Here we will populate the output stacks
                i += 1
                outputImgpath = (
                    '%06d@' %
                    (i, )) + outputParticlesStackName  #denoised image path
                I.setData(np.squeeze(pred))
                I.write(outputImgpath)

                pathNoise = (
                    '%06d@' %
                    (i, )) + inputParticlesStackName  #input image path

                newRow.setValue(xmippLib.MDL_IMAGE, outputImgpath)
                newRow.setValue(xmippLib.MDL_IMAGE_ORIGINAL, pathNoise)

                correlations_input_vs_denoised, _ = pearsonr(
                    pred.ravel(), particle.ravel())
                newRow.setValue(xmippLib.MDL_CORR_DENOISED_NOISY,
                                correlations_input_vs_denoised)

                if useProjections:
                    pathProj = (
                        '%06d@' % (i, )
                    ) + inputProjectionsStackName  #projection image path
                    newRow.setValue(xmippLib.MDL_IMAGE_REF, pathProj)
                    correlations_proj_vs_denoised, _ = pearsonr(
                        pred.ravel(), projection.ravel())
                    newRow.setValue(xmippLib.MDL_CORR_DENOISED_PROJECTION,
                                    correlations_proj_vs_denoised)

                newRow.addToMd(mdNewParticles)

        mdNewParticles.write(
            'particles@' + outputParticlesMdName,
            xmippLib.MD_APPEND)  #save the particles in the metadata