コード例 #1
0
ファイル: FRMAlignment.py プロジェクト: mvanevic/PyTom
class FRMJob(PyTomClass):  # i need to rename the class, but for now it works
    def __init__(self,
                 pl=None,
                 ref=None,
                 mask=None,
                 peak_offset=0,
                 sample_info=None,
                 bw_range=None,
                 freq=None,
                 dest='.',
                 max_iter=10,
                 r_score=False,
                 weighting=False,
                 bfactor=None,
                 symmetries=None,
                 adaptive_res=0.1,
                 fsc_criterion=0.5,
                 constraint=None):
        self.particleList = pl
        self.reference = ref
        self.mask = mask
        self.peak_offset = peak_offset
        self.sampleInformation = sample_info
        self.bw_range = bw_range
        self.freq = freq
        self.destination = dest
        self.max_iter = max_iter
        self.r_score = r_score
        self.weighting = weighting
        self.bfactor = bfactor
        self.symmetries = symmetries
        self.adaptive_res = adaptive_res
        self.fsc_criterion = fsc_criterion
        self.constraint = constraint

    def fromXML(self, xmlObj):
        from lxml.etree import _Element

        if xmlObj.__class__ != _Element:
            raise Exception('You must provide a valid XML object.')

        if xmlObj.tag == "FRMJob":
            jobDescription = xmlObj
        else:
            jobDescription = xmlObj.xpath('FRMJob')
            if len(jobDescription) == 0:
                raise Exception("This XML is not a FRMJob.")
            jobDescription = jobDescription[0]

        from pytom.basic.structures import ParticleList, Reference, Mask, SampleInformation, MultiSymmetries

        pl = ParticleList('.')
        particleList_element = jobDescription.xpath('ParticleList')
        if len(particleList_element) > 0:
            pl.fromXML(particleList_element[0])
        else:
            list_elements = jobDescription.xpath('ParticleListLocation')
            for e in list_elements:
                sub_pl = ParticleList()
                sub_pl.fromXMLFile(e.get('Path'))
                pl += sub_pl
        self.particleList = pl

        r = jobDescription.xpath('Reference')[0]
        self.reference = Reference('')
        self.reference.fromXML(r)

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

        try:
            si = jobDescription.xpath('SampleInformation')[0]
            self.sampleInformation = SampleInformation()
            self.sampleInformation.fromXML(si)
        except:
            self.sampleInformation = SampleInformation()

        try:
            syms = jobDescription.xpath('MultiSymmetries')[0]
            self.symmetries = MultiSymmetries()
            self.symmetries.fromXML(syms)
        except:
            self.symmetries = MultiSymmetries()

        self.peak_offset = int(jobDescription.get('PeakOffset'))
        self.bw_range = [
            int(i)
            for i in jobDescription.get('BandwidthRange')[1:-1].split(',')
        ]
        self.freq = int(jobDescription.get('Frequency'))
        self.destination = jobDescription.get('Destination')
        self.max_iter = int(jobDescription.get('MaxIterations'))
        self.r_score = jobDescription.get('RScore') == 'True'
        self.weighting = jobDescription.get('WeightedAverage') == 'True'
        self.bfactor = jobDescription.get('BFactor')
        if jobDescription.get('AdaptiveResolution'):
            adaptive_resolution = jobDescription.get('AdaptiveResolution')
            if adaptive_resolution == '+1':
                self.adaptive_res = False  # always increase by 1
            else:
                self.adaptive_res = float(adaptive_resolution)
        else:
            self.adaptive_res = 0.0  # default, if not specified
        if jobDescription.get('FSC'):
            self.fsc_criterion = float(jobDescription.get('FSC'))
        else:
            self.fsc_criterion = 0.5  # default value

        # for the constraint
        try:
            from sh_alignment.constrained_frm import AngularConstraint
            con = jobDescription.xpath('AngularConstraint')
            if len(con) != 0:
                ac = AngularConstraint()
                c = ac.fromXML(con[0])
                self.constraint = c
            else:
                self.constraint = None
        except:
            self.constraint = None

    def toXML(self):
        from lxml import etree

        jobElement = etree.Element("FRMJob")
        jobElement.append(self.particleList.toXML())
        jobElement.append(self.reference.toXML())
        jobElement.append(self.mask.toXML())
        jobElement.append(self.sampleInformation.toXML())
        if self.symmetries is not None:
            jobElement.append(self.symmetries.toXML())
        jobElement.set("PeakOffset", str(self.peak_offset))
        jobElement.set("BandwidthRange", str(self.bw_range))
        jobElement.set("Frequency", str(self.freq))
        jobElement.set("Destination", self.destination)
        jobElement.set("MaxIterations", str(self.max_iter))
        jobElement.set("RScore", str(self.r_score))
        jobElement.set("WeightedAverage", str(self.weighting))
        jobElement.set("BFactor", str(self.bfactor))
        if self.adaptive_res is False:
            jobElement.set("AdaptiveResolution", '+1')
        else:
            jobElement.set("AdaptiveResolution", str(self.adaptive_res))
        jobElement.set("FSC", str(self.fsc_criterion))
        if self.constraint:
            jobElement.append(self.constraint.toXML())

        return jobElement

    def check(self):
        from pytom.tools.files import checkDirExists
        self.particleList.check()
        self.reference.check()
        self.mask.check()
        if not checkDirExists(self.destination):
            raise RuntimeError('Destination path not found! ' +
                               self.destination)
コード例 #2
0
ファイル: FRMAlignment.py プロジェクト: mvanevic/PyTom
class FRMJob(PyTomClass):  # i need to rename the class, but for now it works
    def __init__(self,
                 pl=None,
                 ref=None,
                 mask=None,
                 peak_offset=0,
                 sample_info=None,
                 bw_range=None,
                 freq=None,
                 dest='.',
                 max_iter=10,
                 r_score=False,
                 weighting=False,
                 bfactor=None,
                 symmetries=None,
                 adaptive_res=0.1,
                 fsc_criterion=0.5,
                 constraint=None,
                 binning=1):
        """
        initiate FRM job
        @param pl: particle list
        @type ps: L{pytom.basic.structures.ParticleList}
        @param ref: reference density
        @type ref: L{pytom.basic.structures.Reference}
        @param mask: mask 
        @type ref: L{pytom.basic.structures.Mask}
        @param peak_offset: peak offset in voxel
        @type peak_offset: C{int}
        @param sample_info: ?? (Default: None)
        @type sample_info: ??
        @param bw_range: bandwidth range in pixel (2-dim vector)
        @type bw_range: C{list}
        @param freq: frequency (default: None)
        @type: C{int}
        @param dest: distination directory (default: '.')
        @type: C{str}
        @param max_iter: maximum number of iterations
        @type max_iter: C{int}
        @param r_score: use r_score (??) (default: False)
        @type r_score: C{bool}
        @param weighting: weighting (default: False)
        @type weighting: C{bool}
        @param bfactor: B-factor (default: None)
        @type bfactor: C{float}?
        @param symmetries: symmetry (default: None)
        @type L{pytom.basic.structures.Symmetries}
        @param adaptive_res: adaptive resolution - add to resolution for filtering
        @type adaptive_res: C{float}
        @param fsc_criterion: FSC criterion (default: 0.5)
        @type fsc_criterion: C{float}
        @param constraint: Constraint on orientations (deafult: None)
        @type constraint: ??
        @param binning: Perform binning (downscale) of subvolumes by factor. Default=1.
        @type binning C{float}
        """
        self.particleList = pl
        self.reference = ref
        self.mask = mask
        self.peak_offset = peak_offset
        self.sampleInformation = sample_info
        self.bw_range = bw_range
        self.freq = freq
        self.destination = dest
        self.max_iter = max_iter
        self.r_score = r_score
        self.weighting = weighting
        self.bfactor = bfactor
        self.symmetries = symmetries
        self.adaptive_res = adaptive_res
        self.fsc_criterion = fsc_criterion
        self.constraint = constraint
        self.binning = binning

    def fromXML(self, xmlObj):
        """
        read from xml file
        @param xmlObj: xml object
        @type xmlObj: L{lxml.etree.Element}
        """
        from lxml.etree import _Element

        if xmlObj.__class__ != _Element:
            raise Exception('You must provide a valid XML object.')

        if xmlObj.tag == "FRMJob":
            jobDescription = xmlObj
        else:
            jobDescription = xmlObj.xpath('FRMJob')
            if len(jobDescription) == 0:
                raise Exception("This XML is not a FRMJob.")
            jobDescription = jobDescription[0]

        from pytom.basic.structures import ParticleList, Reference, Mask, SampleInformation, MultiSymmetries

        pl = ParticleList('.')
        particleList_element = jobDescription.xpath('ParticleList')
        if len(particleList_element) > 0:
            pl.fromXML(particleList_element[0])
        else:
            list_elements = jobDescription.xpath('ParticleListLocation')
            for e in list_elements:
                sub_pl = ParticleList()
                sub_pl.fromXMLFile(e.get('Path'))
                pl += sub_pl
        self.particleList = pl

        r = jobDescription.xpath('Reference')[0]
        self.reference = Reference('')
        self.reference.fromXML(r)

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

        try:
            si = jobDescription.xpath('SampleInformation')[0]
            self.sampleInformation = SampleInformation()
            self.sampleInformation.fromXML(si)
        except:
            self.sampleInformation = SampleInformation()

        try:
            syms = jobDescription.xpath('MultiSymmetries')[0]
            self.symmetries = MultiSymmetries()
            self.symmetries.fromXML(syms)
        except:
            self.symmetries = MultiSymmetries()

        self.peak_offset = int(jobDescription.get('PeakOffset'))
        self.bw_range = [
            int(i)
            for i in jobDescription.get('BandwidthRange')[1:-1].split(',')
        ]
        self.freq = int(jobDescription.get('Frequency'))
        self.destination = jobDescription.get('Destination')
        self.max_iter = int(jobDescription.get('MaxIterations'))
        self.r_score = jobDescription.get('RScore') == 'True'
        self.weighting = jobDescription.get('WeightedAverage') == 'True'
        self.bfactor = jobDescription.get('BFactor')
        self.binning = int(jobDescription.get('binning'))
        if jobDescription.get('AdaptiveResolution'):
            adaptive_resolution = jobDescription.get('AdaptiveResolution')
            if adaptive_resolution == '+1':
                self.adaptive_res = False  # always increase by 1
            else:
                self.adaptive_res = float(adaptive_resolution)
        else:
            self.adaptive_res = 0.0  # default, if not specified
        if jobDescription.get('FSC'):
            self.fsc_criterion = float(jobDescription.get('FSC'))
        else:
            self.fsc_criterion = 0.5  # default value

        # for the constraint
        try:
            from sh_alignment.constrained_frm import AngularConstraint
            con = jobDescription.xpath('AngularConstraint')
            if len(con) != 0:
                ac = AngularConstraint()
                c = ac.fromXML(con[0])
                self.constraint = c
            else:
                self.constraint = None
        except:
            self.constraint = None

    def toXML(self):
        """
        copy to xml structure
        @return: xml object for job
        @rtype L{lxml.etree.Element}
        """
        from lxml import etree

        jobElement = etree.Element("FRMJob")
        jobElement.append(self.particleList.toXML())
        jobElement.append(self.reference.toXML())
        jobElement.append(self.mask.toXML())
        jobElement.append(self.sampleInformation.toXML())
        if self.symmetries is not None:
            jobElement.append(self.symmetries.toXML())
        jobElement.set("PeakOffset", str(self.peak_offset))
        jobElement.set("BandwidthRange", str(self.bw_range))
        jobElement.set("Frequency", str(self.freq))
        jobElement.set("Destination", self.destination)
        jobElement.set("MaxIterations", str(self.max_iter))
        jobElement.set("RScore", str(self.r_score))
        jobElement.set("WeightedAverage", str(self.weighting))
        jobElement.set("BFactor", str(self.bfactor))
        jobElement.set("binning", str(self.binning))
        if self.adaptive_res is False:
            jobElement.set("AdaptiveResolution", '+1')
        else:
            jobElement.set("AdaptiveResolution", str(self.adaptive_res))
        jobElement.set("FSC", str(self.fsc_criterion))
        if self.constraint:
            jobElement.append(self.constraint.toXML())

        return jobElement

    def check(self):
        from pytom.tools.files import checkDirExists
        self.particleList.check()
        self.reference.check()
        self.mask.check()
        if not checkDirExists(self.destination):
            raise RuntimeError('Destination path not found! ' +
                               self.destination)
コード例 #3
0
class MultiDefocusJob(FRMJob):
    """For the entry of the whole procedure.
    """
    def fromXML(self, xmlObj):  # only rewrite this function
        from lxml.etree import _Element

        if xmlObj.__class__ != _Element:
            raise Exception('You must provide a valid XML object.')

        if xmlObj.tag == "FRMJob":  # the name is not changed here!
            jobDescription = xmlObj
        else:
            jobDescription = xmlObj.xpath('FRMJob')
            if len(jobDescription) == 0:
                raise Exception("This XML is not a FRMJob.")
            jobDescription = jobDescription[0]

        from pytom.basic.structures import Reference, Mask, SampleInformation, MultiSymmetries

        particleList_element = jobDescription.xpath('ParticleListSet')[0]
        pl = ParticleListSet()
        pl.fromXML(particleList_element)
        self.particleList = pl  # here i still use the original name!

        self.reference = []
        r = jobDescription.xpath('Reference')
        for ref_obj in r:
            ref = Reference('')
            ref.fromXML(ref_obj)
            self.reference.append(ref)

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

        try:
            si = jobDescription.xpath('SampleInformation')[0]
            self.sampleInformation = SampleInformation()
            self.sampleInformation.fromXML(si)
        except:
            self.sampleInformation = SampleInformation()

        try:
            syms = jobDescription.xpath('MultiSymmetries')[0]
            self.symmetries = MultiSymmetries()
            self.symmetries.fromXML(syms)
        except:
            self.symmetries = MultiSymmetries()

        self.peak_offset = int(jobDescription.get('PeakOffset'))
        self.bw_range = [
            int(i)
            for i in jobDescription.get('BandwidthRange')[1:-1].split(',')
        ]
        self.freq = int(jobDescription.get('Frequency'))
        self.destination = jobDescription.get('Destination')
        self.max_iter = int(jobDescription.get('MaxIterations'))
        self.r_score = jobDescription.get('RScore') == 'True'
        self.weighting = jobDescription.get('WeightedAverage') == 'True'
        self.bfactor = jobDescription.get('BFactor')
        if jobDescription.get('AdaptiveResolution'):
            adaptive_resolution = jobDescription.get('AdaptiveResolution')
            if adaptive_resolution == '+1':
                self.adaptive_res = False  # always increase by 1
            else:
                self.adaptive_res = float(adaptive_resolution)
        else:
            self.adaptive_res = 0.0  # default, if not specified
        if jobDescription.get('FSC'):
            self.fsc_criterion = float(jobDescription.get('FSC'))
        else:
            self.fsc_criterion = 0.5  # default value

    def check(self):
        for p in self.particleList.pairs:
            p.check()