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: @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)
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,\ wedgeInfo=w,binning=int(binning),sampleInformation=sample,numberClasses=int(numberClasses),\ endThreshold=float(endThreshold),symmetry = sym,doAlignment = False,frmBandwidth = None)
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)
def growingAverageNew(particleList=None,angleObject=None,maskFile=None,scoreObject=None,startClassNumber=0,destinationDirectory='.',preprocessing = None,binning=1,verbose=False): """ """ from pytom.alignment.alignmentFunctions import bestAlignment from pytom.basic.structures import Reference,Particle,Rotation,ParticleList from pytom.alignment.preprocessing import Preprocessing if not preprocessing: preprocessing = Preprocessing() numberOfClasses = len(particleList.splitByClass()) if verbose: print('Processing ' + str(numberOfClasses) + ' classes.') print('Generating start average') startAverageList = particleList.particlesFromClass(float(startClassNumber)) startAverageList.average(destinationDirectory + '/GA_it0.em',progressBar=verbose) currentReference = Reference(destinationDirectory + '/GA_it0.em') growingAverageParticleList = ParticleList(particleList.getDirectory()) for p in startAverageList: p.setRotation(Rotation(0,0,0)) growingAverageParticleList.append(p) for i in range(2,numberOfClasses): currentParticleList = particleList.particlesFromClass(float(i)) if verbose: print('Generating ' + str(i) + '. class average') currentParticleList.average(destinationDirectory + '/CA_it'+str(i)+'.em',progressBar=verbose) currentParticle = Particle(destinationDirectory + '/CA_it'+str(i)+'.em',wedgeInfo=currentParticleList[0].getWedgeInfo()) if verbose: print('Running alignment iteration ' + str(i)) print(currentParticle) print(currentReference) currentPeak = bestAlignment(currentParticle.getVolume(),currentReference.getVolume(),currentReference.getWeighting(),currentParticle.getWedgeInfo(),angleObject,scoreObject,maskFile,preprocessing=preprocessing,binning=binning) if verbose: print('Parameters determined:') print(currentPeak) for p in currentParticleList: p.setRotation(currentPeak.getRotation()) p.setShift(currentPeak.getShift()) growingAverageParticleList.append(p) if verbose: print('Generating growing average ' + str(i)) growingAverageParticleList.average(destinationDirectory + '/GA_it'+ str(i) +'.em',progressBar=verbose) currentReference = Reference(destinationDirectory + '/GA_it'+ str(i) +'.em') angleObject.reset()
def bestAlignment(particle, reference, referenceWeighting, wedgeInfo, rotations, scoreObject=0, mask=None, preprocessing=None, progressBar=False, binning=1, bestPeak=None, verbose=False): """ bestAlignment: Determines best alignment of particle relative to the reference @param particle: A particle @type particle: L{pytom_volume.vol} @param reference: A reference @type reference: L{pytom_volume.vol} @param referenceWeighting: Fourier weighting of the reference (sum of wedges for instance) @type referenceWeighting: L{pytom.basic.structures.vol} @param wedgeInfo: What does the wedge look alike? @type wedgeInfo: L{pytom.basic.structures.Wedge} @param rotations: All rotations to be scanned @type rotations: L{pytom.angles.AngleObject} @param scoreObject: @type scoreObject: L{pytom.score.score.Score} @param mask: real-space mask for correlation function @type mask: L{pytom.basic.structures.Particle} @param preprocessing: Class storing preprocessing of particle and reference such as bandpass @type preprocessing: L{pytom.alignment.preprocessing.Preprocessing} @param progressBar: Display progress bar of alignment. False by default. @param binning: binning factor - e.g. binning=2 reduces size by FACTOR of 2 @type binning: int or float @param bestPeak: Initialise best peak with old values. @param verbose: Print out infos. Writes CC volume to disk!!! Default is False @return: Returns the best rotation for particle and the corresponding scoring result. @author: Thomas Hrabe """ from pytom.basic.correlation import subPixelPeak, subPixelPeakParabolic from pytom.alignment.structures import Peak from pytom_volume import peak, vol, vol_comp from pytom.basic.filter import filter,rotateWeighting from pytom.basic.structures import Rotation, Shift, Particle, Mask from pytom.angles.angle import AngleObject from pytom.alignment.preprocessing import Preprocessing from pytom.basic.transformations import resize, resizeFourier binningType = 'Fourier' # or 'Fourier' assert isinstance(rotations, AngleObject), "bestAlignment: rotations must be " \ "AngleObject!" currentRotation = rotations.nextRotation() if currentRotation == [None,None,None]: raise Exception('bestAlignment: No rotations are sampled! Something is wrong with input rotations') assert particle.__class__ == vol, "particle not of type vol" assert reference.__class__ == vol, "reference not of type vol" assert (referenceWeighting.__class__ == vol or referenceWeighting.__class__ == str), \ "referenceWeighting not volume or str" if mask: assert mask.__class__ == Mask, "Mask not of type Mask" m = mask.getVolume() if scoreObject == 0 or not scoreObject: from pytom.score.score import xcfScore scoreObject = xcfScore() # fix binning if binning == 0: binning = 1 if binning != 1: particleUnbinned = vol(particle.sizeX(), particle.sizeY(), particle.sizeZ()) particleUnbinned.copyVolume(particle) particle = resize(volume=particle, factor=1./binning, interpolation=binningType) if type(particle) == tuple: particle = particle[0] referenceUnbinned = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ()) referenceUnbinned.copyVolume(reference) reference = resize(volume=reference, factor=1./binning, interpolation=binningType) if type(reference) == tuple: reference = reference[0] if mask: m = resize(volume=m, factor=1./binning, interpolation='Spline') if not referenceWeighting.__class__ == str: referenceWeightingUnbinned = vol_comp(referenceWeighting.sizeX(), referenceWeighting.sizeY(), referenceWeighting.sizeZ()) referenceWeightingUnbinned.copyVolume(referenceWeighting) if binning != 1: referenceWeighting = resizeFourier(fvol=referenceWeighting, factor=1./binning) centerX, centerY, centerZ = int(particle.sizeX()/2), int(particle.sizeY()/2), int(particle.sizeZ()/2) # create buffer volume for transformed particle particleCopy = vol(particle.sizeX(),particle.sizeY(),particle.sizeZ()) particle = wedgeInfo.apply(particle) #apply wedge to itself if preprocessing is None: preprocessing = Preprocessing() preprocessing.setTaper( taper=particle.sizeX()/10.) particle = preprocessing.apply(volume=particle, bypassFlag=True) # filter particle to some resolution particleCopy.copyVolume(particle) # compute standard veviation volume really only if needed if mask and (scoreObject._type=='FLCFScore'): from pytom_volume import sum from pytom.basic.correlation import meanUnderMask, stdUnderMask p = sum(m) meanV = meanUnderMask(particle, m, p) stdV = stdUnderMask(particle, m, p, meanV) else: meanV = None stdV = None while currentRotation != [None,None,None]: if mask: m = mask.getVolume(currentRotation) if binning != 1: m = resize(volume=m, factor=1./binning, interpolation='Spline') #update stdV if mask is not a sphere # compute standard deviation volume really only if needed if not mask.isSphere() and (scoreObject._type=='FLCFScore'): meanV = meanUnderMask(particle, m, p) stdV = stdUnderMask(particle, m, p, meanV) else: m = None simulatedVol = _rotateWedgeReference(reference, currentRotation, wedgeInfo, m, [centerX, centerY, centerZ]) simulatedVol = preprocessing.apply(volume=simulatedVol, bypassFlag=True) #weight particle if not referenceWeighting.__class__ == str: from pytom_freqweight import weight weightingRotated = rotateWeighting(weighting=referenceWeighting, z1=currentRotation[0], z2=currentRotation[1], x=currentRotation[2], isReducedComplex=True, returnReducedComplex=True, binarize=False) particleCopy.copyVolume(particle) r = list(filter(particleCopy, weight(weightingRotated))) particleCopy = r[0] scoringResult = scoreObject.score(particleCopy, simulatedVol, m, stdV) pk = peak(scoringResult) #with subPixelPeak [peakValue,peakPosition] = subPixelPeak(scoreVolume=scoringResult, coordinates=pk, interpolation='Quadratic', verbose=False) #[peakValue,peakPosition] = subPixelPeakParabolic(scoreVolume=scoringResult, coordinates=pk, verbose=False) # determine shift relative to center shiftX = (peakPosition[0] - centerX) * binning shiftY = (peakPosition[1] - centerY) * binning shiftZ = (peakPosition[2] - centerZ) * binning #NANs would fail this test. assert peakValue == peakValue, "peakValue seems to be NaN" newPeak = Peak(peakValue, Rotation(currentRotation), Shift(shiftX, shiftY, shiftZ)) if verbose: print('Rotation: z1=%3.1f, z2=%3.1f, x=%3.1f; Dx=%2.2f, Dy=%2.2f, Dz=%2.2f, CC=%2.3f' % \ (currentRotation[0], currentRotation[1], currentRotation[2], shiftX, shiftY, shiftZ, peakValue)) if bestPeak is None: bestPeak = newPeak if verbose: scoringResult.write('BestScore.em') if bestPeak < newPeak: bestPeak = newPeak if verbose: scoringResult.write('BestScore.em') currentRotation = rotations.nextRotation() # repeat ccf for binned sampling to get translation accurately if binning != 1: m = mask.getVolume(bestPeak.getRotation()) centerX, centerY, centerZ = int(particleUnbinned.sizeX()/2), int(particleUnbinned.sizeY()/2), \ int(particleUnbinned.sizeZ()/2) simulatedVol = _rotateWedgeReference(referenceUnbinned, bestPeak.getRotation(), wedgeInfo, m, [centerX, centerY, centerZ]) simulatedVol = preprocessing.apply(volume=simulatedVol, bypassFlag=True) if mask and scoreObject._type=='FLCFScore': p = sum(m) meanV = meanUnderMask(volume=particleUnbinned, mask=m, p=p) stdV = stdUnderMask(volume=particleUnbinned, mask=m, p=p, meanV=meanV) scoreObject._peakPrior.reset_weight() scoringResult = scoreObject.score(particle=particleUnbinned, reference=simulatedVol, mask=m, stdV=stdV) pk = peak(scoringResult) [peakValue,peakPosition] = subPixelPeak(scoreVolume=scoringResult, coordinates=pk, interpolation='Quadratic', verbose=False) shiftX = (peakPosition[0] - centerX) shiftY = (peakPosition[1] - centerY) shiftZ = (peakPosition[2] - centerZ) bestPeak = Peak(peakValue, bestPeak.getRotation(), Shift(shiftX, shiftY, shiftZ)) if verbose: print('BestAlignment: z1=%3.1f, z2=%3.1f, x=%3.1f; Dx=%2.2f, Dy=%2.2f, Dz=%2.2f, CC=%2.3f' % \ (bestPeak.getRotation()[0], bestPeak.getRotation()[1], bestPeak.getRotation()[2], bestPeak.getShift()[0], bestPeak.getShift()[1], bestPeak.getShift()[2], bestPeak.getScoreValue())) rotations.reset() scoreObject._peakPrior.reset_weight() return bestPeak
weighting = False if compound: compound = True else: compound = False ################# fixed parameters ################# if (scoreObject == None or scoreObject.lower() == 'flcf'): score = FLCFScore() elif scoreObject.lower() == 'nxcf': score = nxcfScore() else: score = FLCFScore() score.setRemoveAutocorrelation(flag=False) #pre = Preprocessing(lowestFrequency = float(lowestFrequency),highestFrequency = float(highestFrequency)) pre = Preprocessing() adaptive_res = 0.1 fsc_criterion = 0.143 locJob = GLocalSamplingJob(pl=pl, ref=ref, mask=m, sample_info=sampleI, rotations=rot, preprocessing=pre, dest=destination, max_iter=int(numberIterations), score=score, binning=int(binning), weighting=weighting, compoundWedge=compound, symmetries=None, adaptive_res=adaptive_res, fsc_criterion=fsc_criterion) locJob.toXMLFile(jobName) # run script mainAlignmentLoop( alignmentJob=locJob, verbose=False)
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')
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: 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 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 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')