Exemple #1
0
class ImageGenerator:
    def __init__(self, project_path, images_path,
                 bigThumb=None, smallThumb=None):
        self.project_path = project_path
        self.images_path = images_path
        self.ih = ImageHandler()
        self.img = self.ih.createImage()
        self.bigThumb = bigThumb
        self.smallThumb = smallThumb

    def generate_image(self, input_file, outputName=None):
        output_root = join(self.images_path, basename(outputName))
        output_file = output_root + '.jpg'

        print "Generating image: ", output_file

        if not exists(output_file):
            from PIL import Image
            self.img.read(join(self.project_path, input_file))
            pimg = getPILImage(self.img)

            pwutils.makeFilePath(output_file)
            if self.bigThumb:
                pimg.save(output_file, "JPEG")

            if self.smallThumb:
                pimg.thumbnail((self.smallThumb, self.smallThumb), Image.ANTIALIAS)
                pimg.save(output_root + 't.jpg', "JPEG")

        return output_file
class ImageGenerator:
    def __init__(self, project_path, images_path,
                 bigThumb=None, smallThumb=None):
        self.project_path = project_path
        self.images_path = images_path
        self.ih = ImageHandler()
        self.img = self.ih.createImage()
        self.bigThumb = bigThumb
        self.smallThumb = smallThumb

    def generate_image(self, input_file, outputName=None):
        output_root = join(self.images_path, basename(outputName))
        output_file = output_root + '.png'

        print "Generating image: ", output_file

        if not exists(output_file):
            from PIL import Image
            self.img.read(join(self.project_path, input_file))
            pimg = getPILImage(self.img)

            pwutils.makeFilePath(output_file)
            if self.bigThumb:
                pimg.save(output_file, "PNG")

            if self.smallThumb:
                pimg.thumbnail((self.smallThumb, self.smallThumb), Image.ANTIALIAS)
                pimg.save(output_root + 't.png', "PNG")

        return output_file
Exemple #3
0
    def _compareMovies(self, micSet1, micSet2):
        print "Comparing micrographs (binary images) from results. "

        ih = ImageHandler()
        img1 = ih.createImage()
        img2 = ih.createImage()

        for mic1, mic2 in izip(micSet1, micSet2):
            img1.read(mic1.getFileName())
            img2.read(mic2.getFileName())
            print("Comparing %s vs %s" %
                  (mic1.getFileName(), mic2.getFileName()))
            self.assertTrue(img1.equal(img2, 20))
Exemple #4
0
    def createOutputStep(self, particlesId, coordsId, boxSize):
        """ Create the input file in STAR format as expected by Relion.
        Params:
            particlesId: use this parameters just to force redo of convert if 
                the input particles are changed.
        """
        ih = ImageHandler()
        outputStack = self._getPath('particles.mrcs')
        outputImg = ih.createImage()

        inputParticles = self._getInputParticles()
        inputCoords = self.inputCoordinates.get()
        outputSet = self._createSetOfParticles()
        outputSet.copyInfo(inputParticles)

        boxSize = self.boxSize.get()
        b2 = int(round(boxSize / 2))
        center = np.zeros((boxSize, boxSize))

        ih = ImageHandler()

        i = 0
        outliers = 0
        partIdExcluded = []
        lastPartId = None

        for coord in inputCoords.iterItems(
                orderBy=['_subparticle._micId', '_micId', 'id']):
            # The original particle id is stored in the sub-particle as micId
            partId = coord._micId.get()

            # Load the particle if it has changed from the last sub-particle
            if partId != lastPartId:
                particle = inputParticles[partId]

                if particle is None:
                    partIdExcluded.append(partId)
                    self.info("WARNING: Missing particle with id %s from "
                              "input particles set" % partId)
                else:
                    # Now load the particle image to extract later sub-particles
                    img = ih.read(particle)
                    x, y, _, _ = img.getDimensions()
                    data = img.getData()

                lastPartId = partId

            # If particle is not in inputParticles, subparticles will not be
            # generated. Now, subtract from a subset of original particles is
            # supported.
            if not partId in partIdExcluded:
                xpos = coord.getX()
                ypos = coord.getY()

                # Check that the sub-particle will not lay out of the particle
                if (ypos - b2 < 0 or ypos + b2 > y or xpos - b2 < 0
                        or xpos + b2 > x):
                    outliers += 1
                    continue

                # Crop the sub-particle data from the whole particle image
                center[:, :] = data[ypos - b2:ypos + b2, xpos - b2:xpos + b2]
                outputImg.setData(center)
                i += 1
                outputImg.write((i, outputStack))
                subpart = coord._subparticle
                subpart.setLocation(
                    (i, outputStack))  # Change path to new stack
                subpart.setObjId(None)  # Force to insert as a new item
                outputSet.append(subpart)

        if outliers:
            self.info(
                "WARNING: Discarded %s particles because laid out of the "
                "particle (for a box size of %d" % (outliers, boxSize))

        self._defineOutputs(outputParticles=outputSet)
        self._defineSourceRelation(self.inputParticles, outputSet)
    def createOutputStep(self, particlesId, coordsId, boxSize):
        """ Create the input file in STAR format as expected by Relion.
        Params:
            particlesId: use this parameters just to force redo of convert if 
                the input particles are changed.
        """
        ih = ImageHandler()
        outputStack = self._getPath('particles.mrcs')
        outputImg = ih.createImage()

        inputParticles = self._getInputParticles()
        inputCoords = self.inputCoordinates.get()
        outputSet = self._createSetOfParticles()
        outputSet.copyInfo(inputParticles)

        boxSize = self.boxSize.get()
        b2 = int(round(boxSize / 2))
        center = np.zeros((boxSize, boxSize))

        ih = ImageHandler()

        i = 0
        outliers = 0
        partIdExcluded = []
        lastPartId = None

        for coord in inputCoords.iterItems(orderBy=['_subparticle._micId',
                                                    '_micId', 'id']):
            # The original particle id is stored in the sub-particle as micId
            partId = coord._micId.get()

            # Load the particle if it has changed from the last sub-particle
            if partId != lastPartId:
                particle = inputParticles[partId]

                if particle is None:
                    partIdExcluded.append(partId)
                    self.info("WARNING: Missing particle with id %s from "
                              "input particles set" % partId)
                else:
                    # Now load the particle image to extract later sub-particles
                    img = ih.read(particle)
                    x, y, _, _ = img.getDimensions()
                    data = img.getData()
                
                lastPartId = partId

            # If particle is not in inputParticles, subparticles will not be
            # generated. Now, subtract from a subset of original particles is
            # supported.
            if not partId in partIdExcluded:
                xpos = coord.getX()
                ypos = coord.getY()
    
                # Check that the sub-particle will not lay out of the particle
                if (ypos - b2 < 0 or ypos + b2 > y or
                    xpos - b2 < 0 or xpos + b2 > x):
                    outliers += 1
                    continue
    
                # Crop the sub-particle data from the whole particle image
                center[:, :] = data[ypos-b2:ypos+b2, xpos-b2:xpos+b2]
                outputImg.setData(center)
                i += 1
                outputImg.write((i, outputStack))
                subpart = coord._subparticle
                subpart.setLocation((i, outputStack)) # Change path to new stack
                subpart.setObjId(None) # Force to insert as a new item
                outputSet.append(subpart)

        if outliers:
            self.info("WARNING: Discarded %s particles because laid out of the "
                      "particle (for a box size of %d" % (outliers, boxSize))

        self._defineOutputs(outputParticles=outputSet)
        self._defineSourceRelation(self.inputParticles, outputSet)