def __init__(self,particleList=None,angleObject=None,maskFile=None,scoreObject=None,startClassNumber=0,destinationDirectory='.',preprocessing = None): from pytom.tools.files import checkDirExists from pytom.angles.angleList import AngleList from pytom.basic.structures import ParticleList self._particleList = particleList or ParticleList('/') self._angleObject = angleObject or AngleList() self._startClassNumber = startClassNumber self._maskFile = maskFile or None if preprocessing: self._preprocessing = preprocessing else: from pytom.alignment.preprocessing import Preprocessing self._preprocessing = Preprocessing() if self._maskFile.__class__ == str: from pytom.basic.structures import Mask self._maskFile = Mask(self.maskFile) self._score = scoreObject if not checkDirExists(destinationDirectory): raise Exception('Destination directory ' + destinationDirectory + ' does not exist.') if not destinationDirectory[len(destinationDirectory)-1] == '/': destinationDirectory = destinationDirectory + '/' self._destinationDirectory = destinationDirectory
def fromXML(self,xmlObj): """ fromXML : Assigns values to job attributes from XML object @param xmlObj: A xml object @type xmlObj: L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml.etree import _Element if xmlObj.__class__ != _Element: raise ParameterError('You must provide a valid XML-CorrelationMatrixJob object.') from pytom.basic.structures import ParticleList,Mask particleListObject = xmlObj.xpath('ParticleList') self._particleList = ParticleList('/') self._particleList.fromXML(particleListObject[0]) maskObject = xmlObj.xpath('Mask')[0] self._mask = Mask() self._mask.fromXML(maskObject) self._resultMatrixName = xmlObj.get('ResultMatrixName') self._applyWedge = xmlObj.get('ApplyWedge') == 'True' self._binningFactor = float(xmlObj.get('Binning')) self._lowestFrequency = float(xmlObj.get('LowestFrequency')) self._highestFrequency = float(xmlObj.get('HighestFrequency'))
def fromXML(self, xmlObj): """ fromXML: @param xmlObj: A xml object @type xmlObj: L{lxml.etree._Element} @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 XMLobject.' ) if xmlObj.tag == 'GrowingAverageJob': job_element = xmlObj else: from pytom.basic.exceptions import ParameterError raise ParameterError( 'Is not a GrowingAverageJobXML! You must provide a valid GrowingAverageJobXML object.' ) from pytom.angles.angle import AngleObject from pytom.score.score import fromXML as scoreFromXML from pytom.basic.structures import ParticleList from pytom.alignment.preprocessing import Preprocessing self.startParticleNumber = int(job_element.get('StartParticleNumber')) mask = job_element.xpath('Mask')[0] from pytom.basic.structures import Mask self.maskFile = Mask('') self.maskFile.fromXML(mask) self.destinationDirectory = job_element.get('DestinationDirectory') particleXML = job_element.xpath('ParticleList')[0] self.particleList = ParticleList('/', []) self.particleList.fromXML(particleXML) angleXML = job_element.xpath('Angles')[0] ang = AngleObject() self.angleObject = ang.fromXML(angleXML) scoreXML = job_element.xpath('Score')[0] self.score = scoreFromXML(scoreXML) self.preprocessing = Preprocessing() preprocessingXML = job_element.xpath('Preprocessing')[0] self.preprocessing.fromXML(preprocessingXML)
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 == "CTFCorrectionJob": jobDescription = xmlObj else: jobDescription = xmlObj.xpath('CTFCorrectionJob') if len(jobDescription) == 0: raise Exception("This XML is not a CTFCorrectionJob.") jobDescription = jobDescription[0] from pytom.basic.structures import ParticleList, Reference, Mask, SampleInformation particleList_element = jobDescription.xpath('ParticleList')[0] pl = ParticleList('.') pl.fromXML(particleList_element) self.particleList = pl 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() self.ctf_conv_pl = jobDescription.get('CTFConvolutedParticleList') 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.sum_ctf_sqr = jobDescription.get('CTFSquared')
def PeakJobMsg_Test(self): from pytom.localization.peak_job_msg import PeakJobMsg from pytom.localization.peak_job import PeakJob from pytom.basic.structures import Mask, Reference, WedgeInfo from pytom.localization.structures import Volume from pytom.score.score import FLCFScore from pytom.angles.angleList import AngleList v = Volume(self.testfilename) ref = Reference(self.testfilename) m = Mask(self.testfilename) w = WedgeInfo(30) s = FLCFScore() rot = AngleList([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) j = PeakJob(v, ref, m, w, rot, s) a = PeakJobMsg(str(0), str(1)) a.setJob(j) xmlObj = a.toXML() b = PeakJobMsg() b.fromXML(xmlObj) self.assertTrue(b.getSender() == a.getSender(), msg='') self.assertTrue(b.getRecipient() == a.getRecipient(), msg='')
def PeakJob_Test(self): from pytom.localization.peak_job import PeakJob from pytom.basic.structures import Mask, Reference, WedgeInfo from pytom.localization.structures import Volume from pytom.score.score import FLCFScore from pytom.angles.angleList import AngleList v = Volume(self.testfilename) ref = Reference(self.testfilename) m = Mask(self.testfilename) w = WedgeInfo(30) s = FLCFScore() rot = AngleList([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) a = PeakJob(v, ref, m, w, rot, s, 1) xmlObj = a.toXML() b = PeakJob() b.fromXML(xmlObj) self.assertTrue(b.volume.getFilename() == a.volume.getFilename(), msg='') self.assertTrue(b.jobID == a.jobID, msg='') self.assertTrue(b.reference.getReferenceFilename() == a.reference.getReferenceFilename(), msg='') self.assertTrue(b.mask.getFilename() == a.mask.getFilename(), msg='') self.assertTrue(b.wedge.getWedgeAngle() == a.wedge.getWedgeAngle(), msg='') self.assertTrue(b.score.getScoreFunc() == a.score.getScoreFunc(), msg='')
def __init__(self,particle=None,particleList=None,mask=None,particleIndex = None,applyWedge = True,binningFactor=0,lowestFrequency=-1,highestFrequency=-1): """ __init__: @param particle: Particle @type particle: pytom.alignment.structures.Particle @param particleList: ParticleList of all particles will be correlated with self._particle @type particleList: pytom.alignment.structures.ParticleList @param mask: Mask used for correlation @type mask: str @param applyWedge: Apply wedge during correlation. True by default, disable for rotation classification. @type applyWedge: Bool @param binningFactor: Binning factor accroding to libtomc definition. 0 by default. @type binningFactor: unsigned int @param lowestFrequency: Lowest frequency for bandpass in nyquist @type lowestFrequency: float @param highestFrequency: Highest frequency for bandpass in nyquist @type highestFrequency: float """ from pytom.basic.structures import Particle,ParticleList,Mask if particle and particle.__class__ != Particle: raise ParameterError('You must provide a Particle object.') if particleList and particleList.__class__ != ParticleList: raise ParameterError('You must provide a ParticleList object.') self._particle = particle self._particleList = particleList if not mask: mask = Mask() elif mask.__class__ == str: mask = Mask(mask) elif mask.__class__ != Mask: from pytom.basic.exceptions import ParameterError raise ParameterError('Mask must be a string or Mask object!') self._mask = mask self._particleIndex = particleIndex self._applyWedge = applyWedge self._binningFactor = binningFactor self._lowestFrequency = lowestFrequency self._highestFrequency = highestFrequency
def __init__(self,particleList=None,mask='',resultMatrixName='',applyWedge = True,binningFactor=0,lowestFrequency=-1,highestFrequency=-1): """ __init__: @param particleList: ParticleList of all particles that will be correlated @type particleList: L{pytom.basic.structures.ParticleList} @param mask: Mask used for correlation @type mask: str or L{pytom.basic.structures.Mask} @param resultMatrixName: Result filename @type resultMatrixName: str @param applyWedge: Apply wedge weighting if available? @type applyWedge: boolean, False by default @param binningFactor: Binning factor accroding to libtomc definition. 0 by default. @type binningFactor: unsigned int @param lowestFrequency: Lowest frequency for bandpass in nyquist @type lowestFrequency: float @param highestFrequency: Highest frequency for bandpass in nyquist @type highestFrequency: float """ from pytom.basic.structures import ParticleList,Mask from pytom.tools.files import checkFileExists if not particleList: particleList = ParticleList('/') elif particleList.__class__ != ParticleList: raise ParameterError('You must provide a ParticleList object!') self._particleList = particleList if mask.__class__ == str: mask = Mask(mask) elif mask.__class__ != Mask: raise ParameterError('Mask must be a string or Mask object!') self._mask = mask self._resultMatrixName = resultMatrixName self._applyWedge = applyWedge self._binningFactor = binningFactor self._lowestFrequency = lowestFrequency self._highestFrequency = highestFrequency
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
class PeakJob(PyTomClass): """ PeakJob: stores all the infos needed for calculation of the peak score """ def __init__(self, volume='', reference='', mask='', wedge='', rotations='', score='', jobID=0, members=1, dstDir='./', bandpass=None): """ @param volume: target volume @type volume: L{pytom.localization.structures.Volume} @param reference: reference volume @type reference: L{pytom.basic.structures.Reference} @param mask: mask volume @type mask: L{pytom.basic.structures.Mask} @param wedge: wedge information @type wedge: L{pytom.basic.structures.WedgeInfo} @param rotations: rotation list @type rotations: L{pytom.angles.angle} @param score: score function @type score: L{pytom.score.score} @param jobID: job identification @type jobID: integer @param members: how many members are there available to accomplish this job (1 means only itself) @type members: integer @param dstDir: destination directory where the result is written to @type dstDir: string @param bandpass: bandpass object that will be applied to the reference @type bandpass: L{pytom.basic.structure.BandPassFilter} """ self.volume = volume self.reference = reference self.mask = mask self.wedge = wedge self.rotations = rotations self.score = score self.jobID = jobID self.members = members if dstDir[-1] == '/': self.dstDir = dstDir else: self.dstDir = dstDir + '/' self.bandpass = bandpass def copy(self, fromJob): self.volume = fromJob.volume self.reference = fromJob.reference self.mask = fromJob.mask self.wedge = fromJob.wedge self.rotations = fromJob.rotations self.score = fromJob.score self.jobID = fromJob.jobID self.members = fromJob.members self.dstDir = fromJob.dstDir self.bandpass = fromJob.bandpass def fromXML(self, xmlObj): """ fromXML : Assigns values to job attributes from XML object @param xmlObj: A xml object @author: chen """ from lxml.etree import _Element if xmlObj.__class__ != _Element: raise Exception('You must provide a valid XML object.') if xmlObj.tag == "JobDescription": jobDescription = xmlObj else: jobDescription = xmlObj.xpath('JobDescription') if len(jobDescription) == 0: raise Exception("This XML is not an JobDescription.") jobDescription = jobDescription[0] id = jobDescription.get('ID') if id != None and id != 'None': self.jobID = int(id) members = jobDescription.get('Members') if members != None and members != 'None': self.members = int(members) dstDir = jobDescription.get('Destination') if dstDir != None: if dstDir[-1] == '/': self.dstDir = dstDir else: self.dstDir = dstDir + '/' from pytom.score.score import fromXML as fromXMLScore from pytom.basic.structures import Mask, Reference, Wedge # from pytom.angles.angleList import AngleList from pytom.localization.structures import Volume e = jobDescription.xpath('Volume')[0] v = Volume() v.fromXML(e) self.volume = v ref = jobDescription.xpath('Reference')[0] self.reference = Reference('') self.reference.fromXML(ref) wedgeXML = jobDescription.xpath('Wedge') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('SingleTiltWedge') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('WedgeInfo') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('DoubleTiltWedge') assert len(wedgeXML) > 0 self.wedge = Wedge() self.wedge.fromXML(wedgeXML[0]) mask = jobDescription.xpath('Mask')[0] self.mask = Mask('') self.mask.fromXML(mask) score = jobDescription.xpath('Score') self.score = fromXMLScore(score[0]) rot = jobDescription.xpath('Angles')[0] from pytom.angles.angle import AngleObject ang = AngleObject() self.rotations = ang.fromXML(rot) # self.rotations = AngleList() # self.rotations.fromXML(rot) bp = jobDescription.xpath('BandPassFilter') if bp != []: bp = bp[0] from pytom.basic.structures import BandPassFilter self.bandpass = BandPassFilter(0, 0, 0) self.bandpass.fromXML(bp) else: self.bandpass = None def toXML(self): """ toXML : Compiles a XML file from job object @author: chen """ from lxml import etree jobElement = etree.Element("JobDescription", ID=str(self.jobID), Members=str(self.members), Destination=str(self.dstDir)) jobElement.append(self.volume.toXML()) jobElement.append(self.reference.toXML()) jobElement.append(self.mask.toXML()) jobElement.append(self.wedge.toXML()) jobElement.append(self.rotations.toXML()) jobElement.append(self.score.toXML()) if self.bandpass: jobElement.append(self.bandpass.toXML()) return jobElement def check(self): """ check: Performs check whether all settings are valid. Paths and Files exist @author: chen """ from pytom.tools.files import checkFileExists, checkDirExists returnValue = checkFileExists(self.volume.getFilename()) if not returnValue: raise IOError('File: ' + str(self.volume) + ' not found!') returnValue = checkFileExists(self.reference.getReferenceFilename()) if not returnValue: raise IOError('File: ' + str(self.reference) + ' not found!') returnValue = checkFileExists(self.mask.getFilename()) if not returnValue: raise IOError('File: ' + str(self.mask) + ' not found!') returnValue = checkDirExists(self.dstDir[:-1]) if not returnValue: raise IOError('Directory: ' + str(self.dstDir) + ' not found!') return returnValue def send(self, source, destination): """ send: Send the job-relevant message from source to destination @param source: source machine id gained from pytom_mpi @type source: int @param destination: destination machine id @type destination: int @author: chen """ from pytom.localization.peak_job_msg import PeakJobMsg # self.check() msg = PeakJobMsg(str(source), str(destination)) msg.setJob(self) import pytom_mpi print(f'destination: {destination}\ntype: {type(destination)}') pytom_mpi.send(str(msg), int(destination))
pl.fromXMLFile(particleList) if reference: if not checkFileExists(reference): raise RuntimeError('Reference file ' + reference + ' does not exist!') ref = Reference(referenceFile=reference) else: ref = Reference() if not checkFileExists(mask): raise RuntimeError('Mask file ' + mask + ' does not exist!') if isSphere: isSphere = True else: isSphere = False m = Mask(filename=mask, isSphere=isSphere) if not checkDirExists(destination): raise RuntimeError('Destination directory ' + destination + ' does not exist!') if not angShells: angShells = 3 if not angleInc: angleInc = 3. rot = LocalSampling(angShells,angleInc) if not pixelSize: pixelSize = 1. if not diameter: diameter = -1 sampleI = SampleInformation(pixelSize=float(pixelSize), particleDiameter=float(diameter))
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)
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)
def interpretRequestParameters(parameters): from pytom.localization.peak_job import PeakJob from pytom.localization.structures import Volume from pytom.basic.structures import Mask, Reference, WedgeInfo from pytom.angles.globalSampling import GlobalSampling from pytom.basic.structures import BandPassFilter from pytom.frontend.serverpages.serverMessages import FileMessage if 'tomo' in parameters: vol = Volume(parameters['tomo']) else: raise RuntimeError('Parameter missing in request!') if 'ref' in parameters: ref = Reference(parameters['ref']) else: raise RuntimeError('Parameter missing in request!') if 'mask' in parameters: mask = Mask(parameters['mask']) else: raise RuntimeError('Parameter missing in request!') if 'angle' in parameters: ang = GlobalSampling(parameters['angle']) else: raise RuntimeError('Parameter missing in request!') if 'dest' in parameters: dest = parameters['dest'] else: dest = './' if 'low' in parameters: low = float(parameters['low']) else: raise RuntimeError('Parameter missing in request!') if 'high' in parameters: high = float(parameters['high']) else: raise RuntimeError('Parameter missing in request!') if 'smooth' in parameters: smooth = float(parameters['smooth']) else: raise RuntimeError('Parameter missing in request!') if 'w1' in parameters: w1 = float(parameters['w1']) else: raise RuntimeError('Parameter missing in request!') if 'w2' in parameters: w2 = float(parameters['w2']) else: raise RuntimeError('Parameter missing in request!') if 'x' in parameters: x = float(parameters['x']) else: x = 0 if 'y' in parameters: y = float(parameters['y']) else: y = 0 if 'z' in parameters: z = float(parameters['z']) else: z = 0 bp = BandPassFilter(low, high, smooth) wedge = WedgeInfo([w1, w2]) from pytom.score.score import FLCFScore sc = FLCFScore() job = PeakJob(vol, ref, mask, wedge, ang, dstDir=dest, score=sc, bandpass=bp) jobXMLFile = '' if 'jobFile' in parameters: jobXMLFile = parameters['jobFile'] job.toXMLFile(jobXMLFile) if jobXMLFile[-4:] == '.xml': jobRunFile = jobXMLFile[0:-4] else: jobRunFile = jobXMLFile createRunscripts(jobRunFile + '.sh', jobXMLFile, x, y, z) return FileMessage('LocalizationJob', jobXMLFile, 'created')
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
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
@param splitType: split type of the job @type splitType: "Ang" or "Vol" """ def __init__(self, jobID=-1, originalJobID=-1, splitType=None): self.jobID = jobID self.originalJobID = originalJobID self.splitType = splitType if __name__ == '__main__': from pytom.basic.structures import Mask, Reference, WedgeInfo from pytom.localization.structures import Volume from pytom.score.score import FLCFScore, xcfScore from pytom.localization.peak_job import PeakJob v = Volume('/fs/pool/pool-foerster/apps/src/molmatch/test/testvol.em') ref = Reference('/fs/pool/pool-foerster/apps/src/molmatch/test/templ.em') m = Mask('/fs/pool/pool-foerster/apps/src/molmatch/test/mask_15.em', True) w = WedgeInfo(0) s = FLCFScore() from pytom.angles.globalSampling import GlobalSampling r = GlobalSampling( '/fs/home/ychen/develop/pytom/trunk/pytom/pytomc/libs/libtomc/data/common_data/angles_90_26.em' ) job = PeakJob(v, ref, m, w, r, s) job.toXMLFile('JobInfo.xml') job.toHTMLFile('JobInfo.html')
class CorrelationVectorJob(PyTomClass): """ CorrelationVectorJob: All settings needed for a correlation vector job. Explore class for more info. """ def __init__(self,particle=None,particleList=None,mask=None,particleIndex = None,applyWedge = True,binningFactor=0,lowestFrequency=-1,highestFrequency=-1): """ __init__: @param particle: Particle @type particle: pytom.alignment.structures.Particle @param particleList: ParticleList of all particles will be correlated with self._particle @type particleList: pytom.alignment.structures.ParticleList @param mask: Mask used for correlation @type mask: str @param applyWedge: Apply wedge during correlation. True by default, disable for rotation classification. @type applyWedge: Bool @param binningFactor: Binning factor accroding to libtomc definition. 0 by default. @type binningFactor: unsigned int @param lowestFrequency: Lowest frequency for bandpass in nyquist @type lowestFrequency: float @param highestFrequency: Highest frequency for bandpass in nyquist @type highestFrequency: float """ from pytom.basic.structures import Particle,ParticleList,Mask if particle and particle.__class__ != Particle: raise ParameterError('You must provide a Particle object.') if particleList and particleList.__class__ != ParticleList: raise ParameterError('You must provide a ParticleList object.') self._particle = particle self._particleList = particleList if not mask: mask = Mask() elif mask.__class__ == str: mask = Mask(mask) elif mask.__class__ != Mask: from pytom.basic.exceptions import ParameterError raise ParameterError('Mask must be a string or Mask object!') self._mask = mask self._particleIndex = particleIndex self._applyWedge = applyWedge self._binningFactor = binningFactor self._lowestFrequency = lowestFrequency self._highestFrequency = highestFrequency def getMask(self): return self._mask def getParticle(self): return self._particle def getParticleList(self): return self._particleList def getParticleIndex(self): return self._particleIndex def getApplyWedge(self): return self._applyWedge def getBinning(self): return self._binningFactor def getLowestFrequency(self): return self._lowestFrequency def getHighestFrequency(self): return self._highestFrequency def toXML(self): """ toXML : Compiles a XML object from result object rtype : L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml import etree jobElement = etree.Element('CorrelationVectorJob',ParticleIndex = self._particleIndex.__str__(),ApplyWedge=self._applyWedge.__str__(),Binning = self._binningFactor.__str__(),LowestFrequency = str(self._lowestFrequency),HighestFrequency = str(self._highestFrequency)) jobElement.append(self._particle.toXML()) jobElement.append(self._particleList.toXML()) jobElement.append(self._mask.toXML()) return jobElement def fromXML(self,xmlObj): """ fromXML : Assigns values to job attributes from XML object @param xmlObj: A xml object @type xmlObj: L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml.etree import _Element if xmlObj.__class__ != _Element: raise ParameterError('You must provide a valid XML-CorrelationVectorJob object.') from pytom.basic.structures import Particle,ParticleList,Mask particleObject = xmlObj.xpath('Particle') self._particle = Particle('.') self._particle.fromXML(particleObject[0]) particleListObject = xmlObj.xpath('ParticleList') self._particleList = ParticleList('/') self._particleList.fromXML(particleListObject[0]) maskObject = xmlObj.xpath('Mask')[0] self._mask = Mask() self._mask.fromXML(maskObject) self._particleIndex = xmlObj.get('ParticleIndex') self._applyWedge = xmlObj.get('ApplyWedge') == 'True' self._binningFactor = float(xmlObj.get('Binning')) self._lowestFrequency = float(xmlObj.get('LowestFrequency')) self._highestFrequency = float(xmlObj.get('HighestFrequency'))
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
class CorrelationMatrixJob(PyTomClass): """ CorrelationVectorJob: Represents all settings for a correlation matrix job. Explore the class for further information """ def __init__(self,particleList=None,mask='',resultMatrixName='',applyWedge = True,binningFactor=0,lowestFrequency=-1,highestFrequency=-1): """ __init__: @param particleList: ParticleList of all particles that will be correlated @type particleList: L{pytom.basic.structures.ParticleList} @param mask: Mask used for correlation @type mask: str or L{pytom.basic.structures.Mask} @param resultMatrixName: Result filename @type resultMatrixName: str @param applyWedge: Apply wedge weighting if available? @type applyWedge: boolean, False by default @param binningFactor: Binning factor accroding to libtomc definition. 0 by default. @type binningFactor: unsigned int @param lowestFrequency: Lowest frequency for bandpass in nyquist @type lowestFrequency: float @param highestFrequency: Highest frequency for bandpass in nyquist @type highestFrequency: float """ from pytom.basic.structures import ParticleList,Mask from pytom.tools.files import checkFileExists if not particleList: particleList = ParticleList('/') elif particleList.__class__ != ParticleList: raise ParameterError('You must provide a ParticleList object!') self._particleList = particleList if mask.__class__ == str: mask = Mask(mask) elif mask.__class__ != Mask: raise ParameterError('Mask must be a string or Mask object!') self._mask = mask self._resultMatrixName = resultMatrixName self._applyWedge = applyWedge self._binningFactor = binningFactor self._lowestFrequency = lowestFrequency self._highestFrequency = highestFrequency def getParticleList(self): return self._particleList def getMask(self): return self._mask def getResultMatrixName(self): return self._resultMatrixName def getApplyWedge(self): return self._applyWedge def getBinning(self): return self._binningFactor def getLowestFrequency(self): return self._lowestFrequency def getHighestFrequency(self): return self._highestFrequency def toXML(self): """ toXML : Compiles a XML object from result object rtype : L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml import etree jobElement = etree.Element('CorrelationMatrixJob',ResultMatrixName = self._resultMatrixName, ApplyWedge=self._applyWedge.__str__(),Binning=self._binningFactor.__str__(),LowestFrequency = str(self._lowestFrequency),HighestFrequency = str(self._highestFrequency)) jobElement.append(self._particleList.toXML()) jobElement.append(self._mask.toXML()) return jobElement def fromXML(self,xmlObj): """ fromXML : Assigns values to job attributes from XML object @param xmlObj: A xml object @type xmlObj: L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml.etree import _Element if xmlObj.__class__ != _Element: raise ParameterError('You must provide a valid XML-CorrelationMatrixJob object.') from pytom.basic.structures import ParticleList,Mask particleListObject = xmlObj.xpath('ParticleList') self._particleList = ParticleList('/') self._particleList.fromXML(particleListObject[0]) maskObject = xmlObj.xpath('Mask')[0] self._mask = Mask() self._mask.fromXML(maskObject) self._resultMatrixName = xmlObj.get('ResultMatrixName') self._applyWedge = xmlObj.get('ApplyWedge') == 'True' self._binningFactor = float(xmlObj.get('Binning')) self._lowestFrequency = float(xmlObj.get('LowestFrequency')) self._highestFrequency = float(xmlObj.get('HighestFrequency')) def toHTMLFile(self,filename): """ toHTMLFile: Overrides parent method and stores CorrelationMatrixJob to HMTL @param filename: HTML filename """ from lxml import etree import io from pytom.tools.files import getPytomPath,readStringFile pytomPath = getPytomPath() xsltString = readStringFile(pytomPath + '/xslt/CorrelationMatrix.xsl') xsltStringIO = io.StringIO(xsltString) selfHTML = self.xsltTransform(xsltStringIO) htmlString = etree.tostring(selfHTML,pretty_print=True) file = open(filename, "w") file.write(htmlString) file.close()
def interpretRequestParameters(parameters): """ interpretRequestParameters """ from pytom.basic.structures import ParticleList, SampleInformation, Reference, Mask, Wedge from pytom.alignment.preprocessing import Preprocessing from pytom.frontend.serverpages.serverMessages import FileMessage particleList = ParticleList('.') if 'plXML' in parameters: particleList.fromXMLFile(parameters['plXML']) elif 'plDIR' in parameters: particleList = ParticleList(parameters['plDIR']) particleList.loadDirectory() else: raise RuntimeError('ParticleList parameter missing in request!') sampleInfo = SampleInformation() if 'pixSize' in parameters: sampleInfo.setPixelSize(parameters['pixSize']) else: raise RuntimeError('Pixelsize parameter missing in request!') if 'partDia' in parameters: sampleInfo.setParticleDiameter(parameters['partDia']) else: raise RuntimeError('Particle diameter missing in request!') if 'wa1' in parameters: wedgeAngle1 = float(parameters['wa1']) else: raise RuntimeError('Wedge angle 1 parameter missing in request!') if 'wa2' in parameters: wedgeAngle2 = float(parameters['wa2']) else: raise RuntimeError('Wedge angle 2 parameter missing in request!') wedgeInfo = Wedge([wedgeAngle1, wedgeAngle2]) if 'mask' in parameters: mask = Mask(parameters['mask']) else: raise RuntimeError('Mask parameter missing in request!') if not 'lowestF' in parameters: raise RuntimeError('Lowest frequency parameter missing in request!') if not 'highestF' in parameters: raise RuntimeError('Highest frequency parameter missing in request!') if not 'filtSm' in parameters: raise RuntimeError('Filter smooth parameter missing in request!') preprocessing = Preprocessing(float(parameters['lowestF']), float(parameters['highestF']), float(parameters['filtSm'])) score = None if 'score' in parameters: if parameters['score'] == 'xcf': from pytom.score.score import xcfScore as scoreClass elif parameters['score'] == 'nxcf': from pytom.score.score import nxcfScore as scoreClass elif parameters['score'] == 'flcf': from pytom.score.score import FLCFScore as scoreClass score = scoreClass() else: raise RuntimeError('Score parameter missing in request!') if 'binning' in parameters: binning = int(parameters['binning']) else: raise RuntimeError('Scaling parameter missing in request!') if 'classes' in parameters: numberClasses = float(parameters['classes']) else: raise RuntimeError('Number classes parameter missing in request!') if 'conv' in parameters: convergence = float(parameters['conv']) else: raise RuntimeError('Convergence parameter missing in request!') if 'dest' in parameters: destination = parameters['dest'] else: raise RuntimeError('Destination parameter missing in request!') sampleInfo = SampleInformation() if 'pixSize' in parameters: sampleInfo.setPixelSize(float(parameters['pixSize'])) else: raise RuntimeError('Pixelsize parameter missing in request!') if 'partDia' in parameters: sampleInfo.setParticleDiameter(float(parameters['partDia'])) else: raise RuntimeError('Particle diameter missing in request!') temperature = None if 'temp' in parameters: temperature = parameters['temp'] if 'stemp' in parameters: startTemperature = float(parameters['stemp']) else: raise RuntimeError( 'Start temperature parameter missing in request!') if 'astep' in parameters: annealingStep = float(parameters['astep']) else: raise RuntimeError('Annealing step parameter missing in request!') from pytom.cluster.mcoACStructures import SigmaTemperature if temperature == 'sigma': temperature = SigmaTemperature(startTemperature, annealingStep) else: raise RuntimeError('Temperature missing in request!') criterion = None if 'crit' in parameters: from pytom.cluster.mcoACStructures import MetropolisCriterion, ThresholdAcceptance criterion = parameters['crit'] if criterion == 'metropolis': criterion = MetropolisCriterion() elif criterion == 'threshold': criterion = ThresholdAcceptance() else: raise RuntimeError('Criterion missing in request!') if 'refin' in parameters: localSearchIncrement = float(parameters['refin']) else: raise RuntimeError('Number of refinement rounds missing in request!') from pytom.cluster.mcoACStructures import MCOACJob job = MCOACJob(particleList, destination, mask, score, preprocessing, wedgeInfo, binning, sampleInfo, numberClasses, temperature, criterion, convergence, localSearchIncrement, symmetry=None) jobXMLFile = '' if 'jobFile' in parameters: jobXMLFile = parameters['jobFile'] job.toXMLFile(jobXMLFile) jobRunFile = jobXMLFile[0:-3] createRunscripts(jobRunFile + 'sh', jobXMLFile) return FileMessage('MCOACJob', jobXMLFile, 'created')
def fromXML(self, xmlObj): """ fromXML : Assigns values to job attributes from XML object @param xmlObj: A xml object @author: chen """ from lxml.etree import _Element if xmlObj.__class__ != _Element: raise Exception('You must provide a valid XML object.') if xmlObj.tag == "JobDescription": jobDescription = xmlObj else: jobDescription = xmlObj.xpath('JobDescription') if len(jobDescription) == 0: raise Exception("This XML is not an JobDescription.") jobDescription = jobDescription[0] id = jobDescription.get('ID') if id != None and id != 'None': self.jobID = int(id) members = jobDescription.get('Members') if members != None and members != 'None': self.members = int(members) dstDir = jobDescription.get('Destination') if dstDir != None: if dstDir[-1] == '/': self.dstDir = dstDir else: self.dstDir = dstDir + '/' from pytom.score.score import fromXML as fromXMLScore from pytom.basic.structures import Mask, Reference, Wedge # from pytom.angles.angleList import AngleList from pytom.localization.structures import Volume e = jobDescription.xpath('Volume')[0] v = Volume() v.fromXML(e) self.volume = v ref = jobDescription.xpath('Reference')[0] self.reference = Reference('') self.reference.fromXML(ref) wedgeXML = jobDescription.xpath('Wedge') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('SingleTiltWedge') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('WedgeInfo') if len(wedgeXML) == 0: wedgeXML = jobDescription.xpath('DoubleTiltWedge') assert len(wedgeXML) > 0 self.wedge = Wedge() self.wedge.fromXML(wedgeXML[0]) mask = jobDescription.xpath('Mask')[0] self.mask = Mask('') self.mask.fromXML(mask) score = jobDescription.xpath('Score') self.score = fromXMLScore(score[0]) rot = jobDescription.xpath('Angles')[0] from pytom.angles.angle import AngleObject ang = AngleObject() self.rotations = ang.fromXML(rot) # self.rotations = AngleList() # self.rotations.fromXML(rot) bp = jobDescription.xpath('BandPassFilter') if bp != []: bp = bp[0] from pytom.basic.structures import BandPassFilter self.bandpass = BandPassFilter(0, 0, 0) self.bandpass.fromXML(bp) else: self.bandpass = None
def interpretRequestParameters(parameters): """ interpretRequestParameters """ from pytom.basic.structures import ParticleList, SampleInformation, Reference, Mask, Wedge from pytom.alignment.preprocessing import Preprocessing from pytom.frontend.serverpages.serverMessages import FileMessage particleList = ParticleList() if 'plXML' in parameters: particleList.fromXMLFile(parameters['plXML']) elif 'plDIR' in parameters: particleList = ParticleList(parameters['plDIR']) particleList.loadDirectory() else: raise RuntimeError('ParticleList parameter missing in request!') sampleInfo = SampleInformation() if 'pixSize' in parameters: sampleInfo.setPixelSize(parameters['pixSize']) else: raise RuntimeError('Pixelsize parameter missing in request!') if 'partDia' in parameters: sampleInfo.setParticleDiameter(parameters['partDia']) else: raise RuntimeError('Particle diameter missing in request!') if 'wa1' in parameters: wedgeAngle1 = float(parameters['wa1']) else: raise RuntimeError('Wedge angle 1 parameter missing in request!') if 'wa2' in parameters: wedgeAngle2 = float(parameters['wa2']) else: raise RuntimeError('Wedge angle 2 parameter missing in request!') wedgeInfo = Wedge([wedgeAngle1, wedgeAngle2]) if 'mask' in parameters: mask = Mask(parameters['mask']) else: raise RuntimeError('Mask parameter missing in request!') if not 'lowestF' in parameters: raise RuntimeError('Lowest frequency parameter missing in request!') if not 'highestF' in parameters: raise RuntimeError('Highest frequency parameter missing in request!') if not 'filtSm' in parameters: raise RuntimeError('Filter smooth parameter missing in request!') preprocessing = Preprocessing(float(parameters['lowestF']), float(parameters['highestF']), float(parameters['filtSm'])) score = None if 'score' in parameters: if parameters['score'] == 'xcf': from pytom.score.score import xcfScore as scoreClass elif parameters['score'] == 'nxcf': from pytom.score.score import nxcfScore as scoreClass elif parameters['score'] == 'flcf': from pytom.score.score import FLCFScore as scoreClass score = scoreClass() else: raise RuntimeError('Score parameter missing in request!') if 'iter' in parameters: iterations = int(parameters['iter']) else: raise RuntimeError('Number of iterations missing in request!') if 'binning' in parameters: binning = int(parameters['binning']) else: raise RuntimeError('Scaling parameter missing in request!') if 'classes' in parameters: numberClasses = float(parameters['classes']) else: raise RuntimeError('Number classes parameter missing in request!') if 'conv' in parameters: convergence = float(parameters['conv']) else: raise RuntimeError('Convergence parameter missing in request!') if 'dest' in parameters: destination = parameters['dest'] else: raise RuntimeError('Destination parameter missing in request!') sampleInfo = SampleInformation() if 'pixSize' in parameters: sampleInfo.setPixelSize(float(parameters['pixSize'])) else: raise RuntimeError('Pixelsize parameter missing in request!') if 'partDia' in parameters: sampleInfo.setParticleDiameter(float(parameters['partDia'])) else: raise RuntimeError('Particle diameter missing in request!') from pytom.cluster.mcoEXMXStructures import MCOEXMXJob job = MCOEXMXJob(particleList, iterations, destination, mask, score, preprocessing, wedgeInfo, binning, sampleInfo, numberClasses, convergence, symmetry=None) jobXMLFile = '' if 'jobFile' in parameters: jobXMLFile = parameters['jobFile'] job.toXMLFile(jobXMLFile) jobRunFile = jobXMLFile[0:-3] createRunscripts(jobRunFile + 'sh', jobXMLFile) return FileMessage('MCOEXMXJob', jobXMLFile, 'created')
class GrowingAverageJob(PyTomClass): """ GrowingAverageJob: @ivar particleList: List of particles to be aligned @ivar angleObject: Angle object L{pytom.angles.AngleObject} @ivar startParticleNumber: Number of start particle (default 0) @ivar maskFile: Mask used for appedizing """ def __init__(self,particleList=None,angleObject=None,maskFile=None,scoreObject=None,startClassNumber=0,destinationDirectory='.',preprocessing = None): from pytom.tools.files import checkDirExists from pytom.angles.angleList import AngleList from pytom.basic.structures import ParticleList self._particleList = particleList or ParticleList('/') self._angleObject = angleObject or AngleList() self._startClassNumber = startClassNumber self._maskFile = maskFile or None if preprocessing: self._preprocessing = preprocessing else: from pytom.alignment.preprocessing import Preprocessing self._preprocessing = Preprocessing() if self._maskFile.__class__ == str: from pytom.basic.structures import Mask self._maskFile = Mask(self.maskFile) self._score = scoreObject if not checkDirExists(destinationDirectory): raise Exception('Destination directory ' + destinationDirectory + ' does not exist.') if not destinationDirectory[len(destinationDirectory)-1] == '/': destinationDirectory = destinationDirectory + '/' self._destinationDirectory = destinationDirectory def toXML(self): """ toXML : Compiles a XML file from result object rtype : L{lxml.etree._Element} @author: Thomas Hrabe """ from lxml import etree job_element = etree.Element('GrowingAverageJob',StartClassNumber = str(self._startClassNumber), DestinationDirectory = str(self._destinationDirectory)) job_element.append(self._maskFile.toXML()) job_element.append(self._particleList.toXML()) job_element.append(self._angleObject.toXML()) job_element.append(self._score.toXML()) job_element.append(self._preprocessing.toXML()) return job_element def fromXML(self,xmlObj): """ fromXML: @param xmlObj: A xml object @type xmlObj: L{lxml.etree._Element} @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 XMLobject.') if xmlObj.tag == 'GrowingAverageJob': job_element = xmlObj else: from pytom.basic.exceptions import ParameterError raise ParameterError('Is not a GrowingAverageJobXML! You must provide a valid GrowingAverageJobXML object.') from pytom.angles.angle import AngleObject from pytom.score.score import fromXML as scoreFromXML from pytom.basic.structures import ParticleList from pytom.alignment.preprocessing import Preprocessing self._startClassNumber = int(job_element.get('StartClassNumber')) mask = job_element.xpath('Mask')[0] from pytom.basic.structures import Mask self._maskFile = Mask('') self._maskFile.fromXML(mask) self._destinationDirectory = job_element.get('DestinationDirectory') particleXML = job_element.xpath('ParticleList')[0] self._particleList = ParticleList('/',[]) self._particleList.fromXML(particleXML) angleXML = job_element.xpath('Angles')[0] ang = AngleObject() self._angleObject = ang.fromXML(angleXML) scoreXML = job_element.xpath('Score')[0] self._score = scoreFromXML(scoreXML) self._preprocessing = Preprocessing() preprocessingXML = job_element.xpath('Preprocessing')[0] self._preprocessing.fromXML(preprocessingXML)
class CTFCorrectionJob(PyTomClass): """For the worker to actually run. """ def __init__(self, pl=None, ctf_conv_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, sum_ctf_sqr=None): self.particleList = pl self.ctf_conv_pl = ctf_conv_pl if ref is None: self.reference = [] else: 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.sum_ctf_sqr = sum_ctf_sqr 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 == "CTFCorrectionJob": jobDescription = xmlObj else: jobDescription = xmlObj.xpath('CTFCorrectionJob') if len(jobDescription) == 0: raise Exception("This XML is not a CTFCorrectionJob.") jobDescription = jobDescription[0] from pytom.basic.structures import ParticleList, Reference, Mask, SampleInformation particleList_element = jobDescription.xpath('ParticleList')[0] pl = ParticleList('.') pl.fromXML(particleList_element) self.particleList = pl 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() self.ctf_conv_pl = jobDescription.get('CTFConvolutedParticleList') 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.sum_ctf_sqr = jobDescription.get('CTFSquared') def toXML(self): from lxml import etree jobElement = etree.Element("CTFCorrectionJob") jobElement.append(self.particleList.toXML()) for ref in self.reference: jobElement.append(ref.toXML()) jobElement.append(self.mask.toXML()) jobElement.append(self.sampleInformation.toXML()) jobElement.set("CTFConvolutedParticleList", self.ctf_conv_pl) 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("CTFSquared", str(self.sum_ctf_sqr)) return jobElement
if not checkFileExists(mask): raise RuntimeError('Mask file ' + mask + ' does not exist!') if not checkDirExists(destination): raise RuntimeError('Destination directory ' + destination + ' does not exist!') from pytom.cluster.mcoEXMXStructures import MCOEXMXJob from pytom.basic.structures import ParticleList, Reference, Mask, Wedge, SampleInformation, PointSymmetry from pytom.score.score import FLCFScore from pytom.frontend.serverpages.createMCOEXMXJob import createRunscripts from pytom.alignment.preprocessing import Preprocessing p = ParticleList() p.fromXMLFile(particleList) m = Mask(mask) w = Wedge([float(wedge1), float(wedge2)]) pre = Preprocessing(lowestFrequency=float(lowestFrequency), highestFrequency=float(highestFrequency)) sample = SampleInformation(pixelSize=float(pixelSize), particleDiameter=float(diameter)) if symmetryN is None or symmetryAxisZ is None or symmetryAxisX is None: sym = None else: sym = PointSymmetry(nfold=int(symmetryN), z2=float(symmetryAxisZ), x=float(symmetryAxisX)) job = MCOEXMXJob(particleList=p,numberIterations=numberIterations,\ destinationDirectory=destination,mask=mask,score=FLCFScore(),preprocessing=pre,\
def interpretRequestParameters(parameters): """ interpretRequestParameters """ from pytom.basic.structures import ParticleList, SampleInformation, Reference, Mask, Wedge, PointSymmetry from pytom.alignment.preprocessing import Preprocessing from pytom.frontend.serverpages.serverMessages import FileMessage particleList = ParticleList('.') if 'plXML' in parameters: particleList.fromXMLFile(parameters['plXML']) elif 'plDIR' in parameters: particleList = ParticleList(parameters['plDIR']) particleList.loadDirectory() else: raise RuntimeError('ParticleList parameter missing in request!') sampleInfo = SampleInformation() if 'pixSize' in parameters: sampleInfo.setPixelSize(parameters['pixSize']) else: raise RuntimeError('Pixelsize parameter missing in request!') if 'partDia' in parameters: sampleInfo.setParticleDiameter(parameters['partDia']) else: raise RuntimeError('Particle diameter missing in request!') reference = '' if 'ref' in parameters: reference = Reference(parameters['ref']) else: raise RuntimeError('Reference parameter missing in request!') if 'mask' in parameters: mask = Mask(parameters['mask']) else: raise RuntimeError('Mask parameter missing in request!') angles = None if 'sampling' in parameters: if parameters['sampling'] == 'GLOBAL': from pytom.angles.globalSampling import GlobalSampling if 'angFile' in parameters: angles = GlobalSampling(parameters['angFile']) else: raise RuntimeError('Angle file missing in request!') else: from pytom.angles.localSampling import LocalSampling if 'angStart' in parameters: startIncrement = int(parameters['angStart']) else: raise RuntimeError('Angle start missing in request!') if 'angShells' in parameters: shells = int(parameters['angShells']) else: raise RuntimeError('Angle shells missing in request!') if 'angInc' in parameters: shellIncrement = int(parameters['angInc']) else: raise RuntimeError('Angle increment missing in request!') angles = LocalSampling(shells=shells, increment=startIncrement) else: raise RuntimeError('Sampling completely missing in request!') if not 'lowestF' in parameters: raise RuntimeError('Lowest frequency parameter missing in request!') if not 'highestF' in parameters: raise RuntimeError('Highest frequency parameter missing in request!') if not 'filtSm' in parameters: raise RuntimeError('Filter smooth parameter missing in request!') preprocessing = Preprocessing(float(parameters['lowestF']), float(parameters['highestF']), float(parameters['filtSm'])) adaptive = True if 'adapt' in parameters: fscCriterion = 0.5 resOffset = 0.1 angleFactor = 0.5 adaptive = parameters['adapt'] == 'ON' if parameters['adapt'] == 'ON': if 'adResC' in parameters: fscCriterion = float(parameters['adResC']) else: raise RuntimeError('Resolution criterion missing in request!') if 'adResOf' in parameters: resOffset = parameters['adResOf'] else: raise RuntimeError( 'Resolution offset parameter missing in request!') if 'angFac' in parameters: angleFactor = float(parameters['angFac']) else: raise RuntimeError( 'Angular factor parameter missing in request!') else: raise RuntimeError('Adaptive parameter missing in request!') score = None if 'score' in parameters: if parameters['score'] == 'xcf': from pytom.score.score import xcfScore as scoreClass elif parameters['score'] == 'nxcf': from pytom.score.score import nxcfScore as scoreClass elif parameters['score'] == 'flcf': from pytom.score.score import FLCFScore as scoreClass score = scoreClass() else: raise RuntimeError('Score parameter missing in request!') if 'pkPriRad' in parameters: radius = float(parameters['pkPriRad']) else: raise RuntimeError('Peak distance parameter missing in request!') if 'pkSmooth' in parameters: smooth = float(parameters['pkSmooth']) else: raise RuntimeError('Peak distance smooth missing in request!') score.setPeakPrior(radius=radius, smooth=smooth) if 'iter' in parameters: iterations = int(parameters['iter']) else: raise RuntimeError('Number of iterations missing in request!') if 'binning' in parameters: binning = int(parameters['binning']) else: raise RuntimeError('Scaling parameter missing in request!') if 'dest' in parameters: destination = parameters['dest'] else: raise RuntimeError('Destination parameter missing in request!') from pytom.alignment.ExMaxAlignment import ExMaxJob job = ExMaxJob(particleList, destination, reference, score, angles, mask, PointSymmetry(1), 1, iterations, preprocessing, -1, binning, sampleInfo, fscCriterion, adaptive, resOffset, angleFactor) #print job jobXMLFile = '' if 'jobFile' in parameters: jobXMLFile = parameters['jobFile'] job.toXMLFile(jobXMLFile) if jobXMLFile[-4:] == '.xml': jobRunFile = jobXMLFile[0:-4] else: jobRunFile = jobXMLFile createRunscripts(jobRunFile + '.sh', jobXMLFile) return FileMessage('AlignmentJob', jobXMLFile, 'created')