def splitAngles(self, job, verbose=True): """ splitAngles: Split the job in the "angle" way (binary split) @param job: job @type job: L{pytom.localization.peak_job.PeakJob} """ from pytom.localization.peak_job import PeakJob totalNum = job.rotations.numberRotations() # # initialize the jobInfo structure # self.jobInfo["numDoneJobs"] = 0 # self.jobInfo["numJobs"] = 0 # self.jobInfo["originalJobID"] = job.jobID # self.jobInfo["splitType"] = "Ang" originalJobID = job.jobID while self.members > 1 and totalNum > 1: numEach = totalNum // self.members subMem1 = self.members // 2 subMem2 = self.members - self.members // 2 from pytom.angles.angleList import AngleList subRot1 = AngleList(job.rotations[:numEach * subMem1]) subRot2 = AngleList(job.rotations[numEach * subMem1:]) # avoid the collision of the job id subJob1 = PeakJob(job.volume, job.reference, job.mask, job.wedge, subRot1, job.score, job.jobID * 10 + 1, subMem1, self.dstDir, job.bandpass) subJob2 = PeakJob(job.volume, job.reference, job.mask, job.wedge, subRot2, job.score, job.jobID * 10 + 2, subMem2, self.dstDir, job.bandpass) if verbose == True: print(self.name + ': send number of %d rotations to node %d' % (subJob2.rotations.numberRotations(), self.mpi_id + subMem1)) subJob2.send(self.mpi_id, self.mpi_id + subMem1) # self.jobInfo["numJobs"] = self.jobInfo["numJobs"] + 1 # # set offset # self.jobInfo[subJob1.jobID] = 0 # self.jobInfo[subJob2.jobID] = numEach*subMem1 self.jobInfoPool["numJobsA"] = self.jobInfoPool["numJobsA"] + 1 from pytom.localization.peak_job import JobInfo info = JobInfo(subJob1.jobID, originalJobID, "Ang") info.angleOffset = 0 self.jobInfoPool[subJob1.jobID] = info info = JobInfo(subJob2.jobID, originalJobID, "Ang") info.angleOffset = numEach * subMem1 self.jobInfoPool[subJob2.jobID] = info job = subJob1 totalNum = job.rotations.numberRotations() self.setJob(job) # self.jobInfo["numJobs"] = self.jobInfo["numJobs"] + 1 self.jobInfoPool["numJobsA"] = self.jobInfoPool["numJobsA"] + 1
def determineRotationsByClassAssignment(classifiedParticleList, rotationsParticleList): """ determineRotationsByClassAssignment: Will determine rotations (stored in rotationsParticleList) \ to clusters determined in classifiedParticleList @param classifiedParticleList: @param rotationsParticleList: @return: [rotationsParticleList clustered according to classifiedParticleList, \ list of L{pytom.angles.angleList.AngleList} storing the only cluster rotations] @author: Thomas Hrabe """ from pytom.angles.angleList import AngleList rotationsCopy = rotationsParticleList.copy() rotationsCopy.setClassesFromList(classifiedParticleList) classLists = rotationsCopy.splitByClass() clusteredRotations = [] for classMembers in classLists: angleList = AngleList() for particle in classMembers: angleList.append(particle.getRotation()) clusteredRotations.append(angleList) return [classLists, clusteredRotations]
def extractClusterRotationsFromParticleList(particleList): """ extractClusterRotationsFromParticleList: Splits a particleList according to the clusters stored @param particleList: The particleList @type particleList: either str or L{pytom.alignment.structures.ParticleList}. @return: Returns a list of L{pytom.angles.angleList.AngleList} storing cluster rotations """ from pytom.angles.angleList import AngleList particleClasses = particleList.splitByClass() classes = [] for i in range(len(particleClasses)): className = particleClasses[i][0].getClassName() classes.append(className) classRotations = [] for className in classes: particles = particleList.particleFromClass(className) angleList = AngleList() for particle in particles: angleList.append(particle.getRotation()) classRotations.append(angleList) return classRotations
def __init__(self, particle='', reference=-1.0, score=-1.0, shift=-1.0, rotation=-1.0, angleObject=-1): from pytom.basic.structures import Particle, Reference, Shift, Rotation from numpy import long if particle.__class__ == str: self._particle = Particle(particle) elif particle.__class__ == Particle: self._particle = particle else: self._particle = Particle() if reference.__class__ == str: self._reference = Reference(reference) elif reference.__class__ == Reference: self._reference = reference else: self._reference = Reference() if shift.__class__ == list: self._shift = Shift(shift) elif shift.__class__ == float: self._shift = Shift() else: self._shift = shift if rotation.__class__ == list: self._rotation = Rotation(rotation) elif rotation.__class__ == Rotation: self._rotation = rotation else: self._rotation = Rotation() if score.__class__ == float: from pytom.score.score import xcfScore self._score = xcfScore() else: self._score = score if angleObject.__class__ == float or isinstance( angleObject, (int, long)): from pytom.angles.angleList import AngleList self._angleObject = AngleList() else: self._angleObject = angleObject
def __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 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 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 test_AngleList_sliceAccess(self): from pytom.angles.angleList import AngleList rl = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]] angleObject = AngleList(rl) rotations = [[1, 1, 1], [2, 2, 2]] res = angleObject[1:3] self.assertTrue(res == rotations)
def indexAccess_T(self, angleObject=None, rotation=None, key=None): if not angleObject: from pytom.angles.angleList import AngleList rl = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]] angleObject = AngleList(rl) if not rotation: rotation = [1, 1, 1] if not key: key = 1 r = angleObject[key] self.assertTrue(r == rotation)
def splitAngles(self, job, verbose=True): ''' splitAngles: Distribute the job to the workers by splitting angles @param job: job to be done @type job: L{pytom.localization.peak_job.PeakJob} @param verbose: verbose mode @type verbose: boolean ''' import pytom_mpi mpi_myid = pytom_mpi.rank() if not mpi_myid == 0: raise RuntimeError( 'This function can only be processed by mpi_id = 0! ID == ' + str(mpi_myid) + ' Aborting!') # split the job into smaller ones rotationsPerWorker = job.rotations.numberRotations() // self.numWorkers if rotationsPerWorker == 0: raise RuntimeError("Not enough angles to split!") if verbose == True: print( '\n\nManager: distribute number of %d rotations to %d workers' % (job.rotations.numberRotations(), self.numWorkers)) for i in range(1, self.numWorkers + 1): # split the rotations if i != self.numWorkers: subRot = job.rotations[(i - 1) * rotationsPerWorker:i * rotationsPerWorker] else: # the last node will take all the rest of rotations subRot = job.rotations[(i - 1) * rotationsPerWorker:] self.jobInfo[i] = (i - 1) * rotationsPerWorker from pytom.angles.angleList import AngleList rot = AngleList(subRot) from pytom.localization.peak_job import PeakJob subJob = PeakJob(volume=job.volume, reference=job.reference, mask=job.mask, wedge=job.wedge, rotations=rot, score=job.score, jobID=i, dstDir=job.dstDir, bandpass=job.bandpass) subJob.send(0, i)
def fromXML(self,xmlObj, verbose=False): """ fromXML: Creates any AngleObject child depending xmlObj @param xmlObj: a XML DOM object @param verbose: verbose mode? @type verbose: L{bool} @return: an Angle List as defined by xmlObj @author: Thomas Hrabe G{callgraph} """ angType = xmlObj.get('Type') if angType == 'Eulerian': from pytom.angles.angleList import EulerAngleList ang = EulerAngleList() ang.fromXML(xmlObj) elif angType == 'Equidistant': from pytom.angles.localSampling import LocalSampling ang = LocalSampling() ang.fromXML(xmlObj) elif angType == 'AV3Sampling': from pytom.angles.localSampling import AV3Sampling ang = AV3Sampling() ang.fromXML(xmlObj) elif angType == 'ExtendedInplaneSampling': from pytom.angles.localSampling import ExtendedInplaneSampling ang = ExtendedInplaneSampling() ang.fromXML(xmlObj) elif angType == 'GlobalSampling': from pytom.angles.globalSampling import GlobalSampling ang = GlobalSampling() ang.fromXML(xmlObj) elif angType == 'FromEMFile': from pytom.angles.globalSampling import GlobalSampling ang = GlobalSampling() ang.fromXML(xmlObj) elif angType == 'AngleList': from pytom.angles.angleList import AngleList ang = AngleList() ang.fromXML(xmlObj) elif angType == 'OneAngleList': from pytom.angles.angleList import OneAngleList ang = OneAngleList() ang.fromXML(xmlObj) elif angType == 'LocalSampling': from pytom.angles.localSampling import LocalSampling ang = LocalSampling() ang.fromXML(xmlObj) #elif angType == 'RestrictedInplaneLocalSampling': # from pytom.angles.localSampling import InplaneLocalSampling # ang = RestrictedInplaneEuidistantList() # ang.fromXML(xmlObj) elif angType == 'EquidistantList': from pytom.angles.localSampling import LocalSampling ang = LocalSampling() ang.fromXML(xmlObj) #elif angType == 'RestrictedInplaneEuidistantList': # from pytom.angles.localSampling import RestrictedInplaneLocalSampling # ang = RestrictedInplaneEuidistantList() # ang.fromXML(xmlObj) elif angType == 'Combined': from pytom.angles.combined import GlobalLocalCombined ang = GlobalLocalCombined() ang.fromXML(xmlObj) else: raise TypeError('Type ' +angType+' not available in Angles.') if verbose: print("AngleObject.fromXML: Returned AngleObject: "+str(ang)) self.ang = ang return ang
class MaximisationResult(PyTomClass): """ MaximisationResult : Stores results of one maximisation process """ def __init__(self, particle='', reference=-1.0, score=-1.0, shift=-1.0, rotation=-1.0, angleObject=-1): from pytom.basic.structures import Particle, Reference, Shift, Rotation from numpy import long if particle.__class__ == str: self._particle = Particle(particle) elif particle.__class__ == Particle: self._particle = particle else: self._particle = Particle() if reference.__class__ == str: self._reference = Reference(reference) elif reference.__class__ == Reference: self._reference = reference else: self._reference = Reference() if shift.__class__ == list: self._shift = Shift(shift) elif shift.__class__ == float: self._shift = Shift() else: self._shift = shift if rotation.__class__ == list: self._rotation = Rotation(rotation) elif rotation.__class__ == Rotation: self._rotation = rotation else: self._rotation = Rotation() if score.__class__ == float: from pytom.score.score import xcfScore self._score = xcfScore() else: self._score = score if angleObject.__class__ == float or isinstance( angleObject, (int, long)): from pytom.angles.angleList import AngleList self._angleObject = AngleList() else: self._angleObject = angleObject def toParticle(self): """ toParticle: Converts this object to a Particle object. @return: @rtype: L{pytom.basic.structures.Particle} """ from pytom.basic.structures import Particle particle = self._particle particle.setRotation(self._rotation) particle.setShift(self._shift) particle.setScore(self._score) return particle def getParticle(self): return self._particle def getShift(self): from pytom.basic.structures import Shift if self._shift.__class__ == list: return Shift(self._shift) else: return self._shift def setShift(self, shift): from pytom.basic.structures import Shift assert shift.__class__ == Shift self._shift = shift def getAngleObject(self): return self._angleObject def getRotation(self): from pytom.basic.structures import Rotation if self._rotation.__class__ == list: return Rotation(self._rotation[0], self._rotation[1], self._rotation[2]) else: return self._rotation.copy() def getScore(self): """ getScore: Returns score object """ return self._score def setRotation(self, rotation): """ setRotation: @param rotation: """ from pytom.basic.structures import Rotation if rotation.__class__ == list: rotation = Rotation(rotation) self._rotation = rotation def fromXML(self, xmlObj): """ fromXML : Assigns values to result attributes from XML object @param xmlObj: A xml object @author: Thomas Hrabe """ from lxml.etree import _Element if xmlObj.__class__ != _Element: from pytom.basic.exceptions import ParameterError raise ParameterError( 'Is not a lxml.etree._Element! You must provide a valid XML object.' ) from pytom.score.score import fromXML as fromXMLScore from pytom.angles.angle import AngleObject if xmlObj.tag == "Result": result = xmlObj else: result = xmlObj.xpath('Result') if len(result) == 0: raise PyTomClassError( "This XML is not an MaximisationResult. No Result provided." ) result = result[0] from pytom.basic.structures import Particle, Reference, Rotation, Shift particle_element = result.xpath('Particle')[0] p = Particle('') p.fromXML(particle_element) self._particle = p r = result.xpath('Reference') ref = Reference('') ref.fromXML(r[0]) self._reference = ref scoreXML = result.xpath('Score')[0] self._score = fromXMLScore(scoreXML) shiftXML = result.xpath('Shift')[0] self._shift = Shift() self._shift.fromXML(shiftXML) rotationXML = result.xpath('Rotation')[0] self._rotation = Rotation() self._rotation.fromXML(rotationXML) angleElement = result.xpath('Angles') ang = AngleObject() self._angleObject = ang.fromXML(angleElement[0]) def toXML(self): """ toXML : Compiles a XML from result object @author: Thomas Hrabe """ from lxml import etree resultElement = etree.Element("Result") resultElement.append(self._particle.toXML()) if self._reference.hasGeneratedByInfo(): from pytom.basic.structures import Reference newRef = Reference(self._reference.getReferenceFilename()) resultElement.append(newRef.toXML()) else: resultElement.append(self._reference.toXML()) resultElement.append(self._shift.toXML()) resultElement.append(self._rotation.toXML()) resultElement.append(self._score.toXML()) resultElement.append(self._angleObject.toXML()) return resultElement def copy(self): return MaximisationResult(self._particle, self._reference, self._score, self._shift, self._rotation, self._angleObject)