Esempio n. 1
0
class MaximisationJob(PyTomClass):
    """
    MaximisationJob : Stores all infos needed for a maximisation job
    """
    def __init__(self,
                 particle='',
                 reference='',
                 score='',
                 rotations='',
                 mask='',
                 numberRefinementRounds=1,
                 preprocessing='',
                 binning=1):
        """
        @param particle: particle to be aligned
        @type particle: L{pytom.basic.structures.Particle} or str
        @param reference: reference for particle alignment
        @param score: type of score used for alignment
        @param rotations: rotations scanned in search
        @param mask: mask on reference
        @param numberRefinementRounds: iteration number
        @param preprocessing: preprocessing parameters
        @param binning: Binning Factor
        @type binning: int
        """

        from lxml.etree import _Element

        if particle.__class__ == _Element:
            self.fromXML(particle)
        else:
            if particle.__class__ == str:
                from pytom.basic.structures import Particle
                self.particle = Particle(particle)
            else:
                self.particle = particle

            self.reference = reference
            self.score = score
            self.rotations = rotations
            self.mask = mask
            self.numberRefinementRounds = numberRefinementRounds
            self.preprocessing = preprocessing
            self.binning = binning

    def fromXML(self, xmlObj):
        """
        fromXML : Assigns values to job attributes from XML object
        @param xmlObj: A xml object  
        @author: Thomas Hrabe 
        """
        from lxml.etree import _Element

        if xmlObj.__class__ != _Element:
            from pytom.basic.exceptions import ParameterError
            raise ParameterError(
                'You must provide a valid XML-MaximisationJob object.')

        if xmlObj.tag == "JobDescription":
            jobDescription = xmlObj
        else:
            jobDescription = xmlObj.xpath('JobDescription')

            if len(jobDescription) == 0:
                from pytom.basic.structures import PyTomClassError
                raise PyTomClassError("This XML is not an JobDescription.")

            jobDescription = jobDescription[0]

        from pytom.angles.angle import AngleObject
        from pytom.score.score import fromXML as fromXMLScore
        from pytom.alignment.preprocessing import Preprocessing
        from pytom.basic.structures import Mask, Particle, Reference, ReferenceList

        self.binning = int(jobDescription.get('Binning'))

        particle_element = jobDescription.xpath('Particle')[0]
        p = Particle('')
        p.fromXML(particle_element)
        self.particle = p

        r = jobDescription.xpath('Reference')

        if len(r) > 0:
            ref = Reference('')
            ref.fromXML(r[0])
            self.reference = ref
        else:
            r = jobDescription.xpath('ReferenceList')
            ref = ReferenceList()
            ref.fromXML(r[0])
            self.reference = ref

        mask = jobDescription.xpath('Mask')[0]
        self.mask = Mask('')
        self.mask.fromXML(mask)

        self.numberRefinementRounds = jobDescription.get(
            'NumberRefinementRounds')
        self.numberRefinementRounds = int(self.numberRefinementRounds)

        score = jobDescription.xpath('Score')
        self.score = fromXMLScore(score[0])

        angles = jobDescription.xpath('Angles')
        ang = AngleObject()
        self.rotations = ang.fromXML(angles[0])

        preObj = xmlObj.xpath('Preprocessing')
        if len(preObj) == 0:
            self.preprocessing = Preprocessing()
        else:
            p = Preprocessing()
            p.fromXML(preObj[0])
            self.preprocessing = p

    def toXML(self):
        """
        toXML : Compiles a XML file from job object
        @author: Thomas Hrabe
        """

        from lxml import etree

        jobElement = etree.Element("JobDescription")

        jobElement.set("NumberRefinementRounds",
                       str(self.numberRefinementRounds))

        jobElement.set("Binning", str(self.binning))

        jobElement.append(self.particle.toXML())

        jobElement.append(self.reference.toXML())

        jobElement.append(self.mask.toXML())
        jobElement.append(self.rotations.toXML())

        if self.score.__class__ == str:
            jobElement.append(self.score)
        else:
            jobElement.append(self.score.toXML())

        preObj = self.preprocessing.toXML()
        jobElement.append(preObj)

        return jobElement

    def check(self):
        """
        check: Performs check on self whether all settings were sane. Paths and Files exists 
        """

        from pytom.tools.files import checkFileExists, checkDirExists

        returnValue = checkFileExists(self.particle.getFilename())
        if not returnValue:
            raise IOError(str(self.particle.getFilename()) + ' not found!')

        returnValue = returnValue and checkFileExists(
            self.reference.getFilename())

        if not returnValue:
            raise IOError(str(self.reference) + ' not found!')

        return returnValue
Esempio n. 2
0
class MaximisationResult(PyTomClass):
    """
    MaximisationResult : Stores results of one maximisation process
    """
    def __init__(self,
                 particle='',
                 reference=-1.0,
                 score=-1.0,
                 shift=-1.0,
                 rotation=-1.0,
                 angleObject=-1):

        from pytom.basic.structures import Particle, Reference, Shift, Rotation
        from numpy import long

        if particle.__class__ == str:
            self._particle = Particle(particle)
        elif particle.__class__ == Particle:
            self._particle = particle
        else:
            self._particle = Particle()

        if reference.__class__ == str:
            self._reference = Reference(reference)
        elif reference.__class__ == Reference:
            self._reference = reference
        else:
            self._reference = Reference()

        if shift.__class__ == list:
            self._shift = Shift(shift)
        elif shift.__class__ == float:
            self._shift = Shift()
        else:
            self._shift = shift

        if rotation.__class__ == list:
            self._rotation = Rotation(rotation)
        elif rotation.__class__ == Rotation:
            self._rotation = rotation
        else:
            self._rotation = Rotation()

        if score.__class__ == float:
            from pytom.score.score import xcfScore
            self._score = xcfScore()
        else:
            self._score = score

        if angleObject.__class__ == float or isinstance(
                angleObject, (int, long)):
            from pytom.angles.angleList import AngleList
            self._angleObject = AngleList()
        else:
            self._angleObject = angleObject

    def toParticle(self):
        """
        toParticle: Converts this object to a Particle object.
        @return:
        @rtype: L{pytom.basic.structures.Particle}
        """
        from pytom.basic.structures import Particle
        particle = self._particle
        particle.setRotation(self._rotation)
        particle.setShift(self._shift)
        particle.setScore(self._score)

        return particle

    def getParticle(self):
        return self._particle

    def getShift(self):
        from pytom.basic.structures import Shift

        if self._shift.__class__ == list:
            return Shift(self._shift)
        else:
            return self._shift

    def setShift(self, shift):
        from pytom.basic.structures import Shift

        assert shift.__class__ == Shift

        self._shift = shift

    def getAngleObject(self):
        return self._angleObject

    def getRotation(self):

        from pytom.basic.structures import Rotation

        if self._rotation.__class__ == list:
            return Rotation(self._rotation[0], self._rotation[1],
                            self._rotation[2])
        else:
            return self._rotation.copy()

    def getScore(self):
        """
        getScore: Returns score object
        """
        return self._score

    def setRotation(self, rotation):
        """
        setRotation:
        @param rotation: 
        """
        from pytom.basic.structures import Rotation

        if rotation.__class__ == list:
            rotation = Rotation(rotation)

        self._rotation = rotation

    def fromXML(self, xmlObj):
        """
        fromXML : Assigns values to result attributes from XML object
        @param xmlObj: A xml object  
        @author: Thomas Hrabe 
        """
        from lxml.etree import _Element

        if xmlObj.__class__ != _Element:
            from pytom.basic.exceptions import ParameterError
            raise ParameterError(
                'Is not a lxml.etree._Element! You must provide a valid XML object.'
            )

        from pytom.score.score import fromXML as fromXMLScore
        from pytom.angles.angle import AngleObject

        if xmlObj.tag == "Result":
            result = xmlObj
        else:
            result = xmlObj.xpath('Result')

            if len(result) == 0:
                raise PyTomClassError(
                    "This XML is not an MaximisationResult. No Result provided."
                )

            result = result[0]
        from pytom.basic.structures import Particle, Reference, Rotation, Shift
        particle_element = result.xpath('Particle')[0]
        p = Particle('')
        p.fromXML(particle_element)
        self._particle = p

        r = result.xpath('Reference')
        ref = Reference('')
        ref.fromXML(r[0])
        self._reference = ref

        scoreXML = result.xpath('Score')[0]
        self._score = fromXMLScore(scoreXML)

        shiftXML = result.xpath('Shift')[0]
        self._shift = Shift()
        self._shift.fromXML(shiftXML)

        rotationXML = result.xpath('Rotation')[0]
        self._rotation = Rotation()
        self._rotation.fromXML(rotationXML)

        angleElement = result.xpath('Angles')
        ang = AngleObject()
        self._angleObject = ang.fromXML(angleElement[0])

    def toXML(self):
        """
        toXML : Compiles a XML from result object
        @author: Thomas Hrabe
        """
        from lxml import etree

        resultElement = etree.Element("Result")

        resultElement.append(self._particle.toXML())

        if self._reference.hasGeneratedByInfo():
            from pytom.basic.structures import Reference
            newRef = Reference(self._reference.getReferenceFilename())
            resultElement.append(newRef.toXML())
        else:
            resultElement.append(self._reference.toXML())

        resultElement.append(self._shift.toXML())

        resultElement.append(self._rotation.toXML())

        resultElement.append(self._score.toXML())

        resultElement.append(self._angleObject.toXML())

        return resultElement

    def copy(self):
        return MaximisationResult(self._particle, self._reference, self._score,
                                  self._shift, self._rotation,
                                  self._angleObject)