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,tostring if xmlObj.__class__ != _Element: raise ParameterError('You must provide a valid XML-CorrelationVector object.') from pytom.basic.structures import Particle,ParticleList self._particleIndex = int(xmlObj.get('ParticleIndex')) particleObject = xmlObj.xpath('Particle') self._particle = Particle('.') self._particle.fromXML(particleObject[0]) particleListObject = xmlObj.xpath('ParticleList') self._particleList = ParticleList('/') self._particleList.fromXML(particleListObject[0]) values = xmlObj.xpath('Correlation') self._correlations = [0 for _ in range(len(values))] for v in values: index = int(v.get('Index')) self._correlations[index] = float(v.get('Value'))
def determineClassSwaps(particleList1,particleList2): """ determineClassSwaps @param particleList1: The previous particleList @param particleList2: The current particleList @return: List of particles that swapped class and a corresponding list of [previous class, current class]. @rtype: [L{pytom.basic.structures.ParticleList},[previousClass,currentClass]] """ from pytom.basic.structures import ParticleList returnList = ParticleList(particleList1.getDirectory()) classSwaps = [] for particle in particleList1: particleName = particle.getFilename() particleClass = particle.getClassName() otherParticle = particleList2.getParticleByFilename(particleName) otherParticleClass = otherParticle.getClassName() if not particleClass == otherParticleClass: returnList.append(otherParticle) classSwaps.append([particleClass,otherParticleClass]) return [returnList,classSwaps]
def fromXML(self,xmlObj): from lxml.etree import _Element from pytom.basic.structures import ParticleList from pytom.reconstruction.reconstructionStructures import ProjectionList if xmlObj.__class__ != _Element : raise Exception('Is not a lxml.etree._Element! You must provide a valid XMLobject.') if xmlObj.tag == 'ReconstructionMessage': message_element = xmlObj else: Exception('Is not a ReconstructionMessage! You must provide a valid ReconstructionMessage object.') self._sender = message_element.get('Sender') self._recipient = message_element.get('Recipient') self._timestamp = message_element.get('Timestamp') self._cubeSize = int(message_element.get('CubeSize')) self._binning = float(message_element.get('Binning')) self._applyWeighting = bool(message_element.get('ApplyWeighting')) self._particleList = ParticleList('.') particleListXML = message_element.xpath('ParticleList')[0] self._particleList.fromXML(particleListXML) self._projectionList = ProjectionList() projectionListXML = message_element.xpath('ProjectionList')[0] self._projectionList.fromXML(projectionListXML)
def distribute_job(self, job, verbose=False): pl_filename = job["ParticleList"] from pytom.basic.structures import ParticleList pl = ParticleList('.') pl.fromXMLFile(pl_filename) nn = len(pl) all_pairs = [] for i in range(nn): for j in range(i+1, nn): all_pairs.append((i, j)) n = len(all_pairs) particlesPerNode = int(n/self.num_workers) residual = n-particlesPerNode*self.num_workers start_idx = 0 for i in range(1, self.num_workers+1): if i < residual+1: # since i starts from 1 l = particlesPerNode+1 else: l = particlesPerNode sub_pairs = all_pairs[start_idx : start_idx+l] start_idx += l # construct the job sub_job = {} sub_job["Pairs"] = sub_pairs sub_job["ParticleList"] = pl_filename sub_job["Mask"] = job["Mask"] sub_job["Frequency"] = job["Frequency"] sub_job["Binning"] = job["Binning"] self.send_job(sub_job, i) if verbose: print(self.node_name + ': distributed %d particles to node %d' % (len(sub_pairs), i))
def get_phase_flip_pl(self): if self.phase_flip_pl_obj is None: from pytom.basic.structures import ParticleList pl = ParticleList('.') pl.fromXMLFile(self.phase_flip_pl) self.phase_flip_pl_obj = pl return self.phase_flip_pl_obj
def start(self, job, outdir='./', verbose=False): outdir = job["outdir"] if self.mpi_id == 0: import numpy as np pl_filename = job["ParticleList"] from pytom.basic.structures import ParticleList pl = ParticleList('.') pl.fromXMLFile(pl_filename) n = len(pl) correlation_matrix = np.ones((n, n)) # distribute the job self.distribute_job(job, verbose) # fill in the diagnal line for i in range(n): correlation_matrix[i][i] = 1 # gather the results for i in range(self.num_workers): result = self.get_result() pairs = list(result.keys()) for pair in pairs: correlation_matrix[pair[0]][pair[1]] = result[pair] correlation_matrix[pair[1]][pair[0]] = result[pair] # write the correlation matrix to the disk np.savetxt(os.path.join(outdir, 'correlation_matrix.csv'), correlation_matrix, delimiter=',') # send end signal to other nodes and terminate itself self.end(verbose) else: # other nodes self.run(verbose)
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 writeSetOfVolumes(volSet, volXml, volDir): """ Convert a SetOfVolumes to a xml file used by PyTom. The volumes will be converted to .mrc format if not are '.em' or '.mrc' Params: volSet: input SetOfVolumes. volXml: filename where to write the xml file. volDir: where to create links or copies (converted to mrc). """ # Add to the path the root to pytom backupPath = list(sys.path) addPyTomPaths() from pytom.basic.structures import Particle, ParticleList, Wedge, SingleTiltWedge from pytom.score.score import Score, PeakPrior, xcfScore from pytom.frm.FRMAlignment import FRMScore w = SingleTiltWedge() #s = PeakPrior() pl = ParticleList() ih = em.convert.ImageHandler() for vol in volSet: index, fn = vol.getLocation() convert = True # by default, convert, which is the save way if index == em.NO_INDEX: # means single volumes volName = os.path.basename(fn) if fn.endswith('.em') or fn.endswith('.mrc'): convert = False # we can just create a link in this case else: volName = 'volume_%03d.mrc' % vol.getObjId() volFn = os.path.join(volDir, volName) if convert: ih.convert(vol, volFn) else: pwutils.createLink(fn, volFn) # Make the volumes names relative to the xml file # where the programs will be executed volRel = os.path.relpath(volFn, os.path.dirname(volXml)) p = Particle() s = xcfScore() s.setValue(1.0) pytomInfo = getattr(vol, 'pytomInfo', None) if pytomInfo is None: p.setWedge(w) else: p.fromXML(pytomInfo.get()) # Get stored XML format from PyTom p.setFilename(volRel) p.setScore(s) pl.append(p) pl.toXMLFile(volXml) #pl.setWedgeAllParticles(w) sys.path = backupPath
def writeCroppedParticles(particleListName, output, center, cubesize): """ @param particleListName: name of particle list @type particleListName: str @param output: Name of output particles @type output: str @param center: center of output particles in template orientation @type center: list @param cubesize: Size of output particles in pixel @type cubesize: int """ from pytom.basic.structures import ParticleList, Particle, Shift from pytom_volume import transformSpline as transform from pytom_volume import subvolume, vol pl = ParticleList() pl.fromXMLFile(filename=particleListName) #copy particle list for list of cropped particles pl_new = pl.copy() pvol = pl[0].getVolume() sizeX = pvol.sizeX() sizeY = pvol.sizeY() sizeZ = pvol.sizeZ() pvol_ali = vol(sizeX, sizeY, sizeZ) subV = vol(cubesize, cubesize, cubesize) sub_startX = center[0]-cubesize/2 sub_startY = center[1]-cubesize/2 sub_startZ = center[2]-cubesize/2 if (sub_startX < 0) or (sub_startY < 0) or (sub_startZ < 0): raise ValueError('cubesize too large :(') for (ipart, part) in enumerate(pl): pvol_ali.setAll(0) subV.setAll(0) pvol = part.getVolume() rot = part.getRotation() rotinvert = rot.invert() shiftV = part.getShift() transform(pvol, pvol_ali, rotinvert[0], rotinvert[1], rotinvert[2], sizeX/2, sizeY/2, sizeZ/2, -shiftV[0], -shiftV[1], -shiftV[2], 0, 0, 0) # box out subvolume subV = subvolume(pvol_ali, sub_startX, sub_startY, sub_startZ, cubesize, cubesize, cubesize) transform(subV, subV, rot[0], rot[1], rot[2], cubesize/2, cubesize/2, cubesize/2, 0, 0, 0, 0, 0, 0) fname = part.getFilename() idx = fname.split('_')[-1].split('.')[0] nfname = output+'_'+idx+'.em' print("write file " + nfname) subV.write(nfname) pl_new[ipart].setFilename(newFilename=nfname) pl_new[ipart].setShift(shift=Shift(0,0,0)) return pl_new
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 cleanUp_RandomParticleList(pl_filename='pl.xml', pdir='./testparticles'): """ remove directories """ from os import remove, rmdir from pytom.basic.structures import ParticleList pl = ParticleList() pl.fromXMLFile(filename=pl_filename) for part in pl: remove(part.getFilename()) rmdir(pdir) remove(pl_filename)
def saveForFSC(newReferenceName, particleList, verbose=False): """ saveForFSC: Split particles into even / odd and calculate the Fourier Shell Correlation out of these two sets. @param newReferenceName: Name of current alignment result @type newReferenceName: str @param particleList: @type particleList: L{pytom.basic.structures.ParticleList} """ if len(particleList) < 2: raise RuntimeError( 'ParticleList must have at least 2 elements to run saveForFSC!') from pytom.basic.structures import ParticleList from pytom.alignment.alignmentFunctions import average even = ParticleList('/') odd = ParticleList('/') for particleCounter in range(len(particleList)): if particleCounter % 2 == 0: even.append(particleList[particleCounter]) else: odd.append(particleList[particleCounter]) if verbose: print('averaging even:') average(even, newReferenceName + 'even.em', verbose) if verbose: print('averaging odd:') average(odd, newReferenceName + 'odd.em', verbose)
def simulationDescriptionToParticleList(directory,prefix = ''): """ simulationDescriptionToParticleList: """ lenDir = len(directory) if not directory[lenDir-1] == '/': directory = directory + '/' xmlFile = directory + 'desc.xml' from lxml import etree simulationXML = etree.parse(xmlFile) #print etree.tostring(simulationXML,pretty_print=True) particles = simulationXML.xpath('particle') parameters = simulationXML.xpath('Simulation_Parameters') wedge = int(parameters[0].get('wangleEnd'))/2 from pytom.basic.structures import Particle,ParticleList,WedgeInfo wi = WedgeInfo(wedge,[0.0,0.0,0.0]) pl = ParticleList(directory) for particle in particles: filename = prefix + particle.get('filename') rotation = particle.get('rotation') rotation = rotation.replace('[','') rotation = rotation.replace(']','') rotation = rotation.split(',') rotation = [float(rotation[0]),float(rotation[1]),float(rotation[2])] shift = particle.get('shift') shift = shift.replace('[','') shift = shift.replace(']','') shift = shift.split(',') shift = [int(round(float(shift[0]))),int(round(float(shift[1]))),int(round(float(shift[2])))] p = Particle(filename,rotation = rotation,shift = shift,wedge=wi) pl.append(p) #al.toXMLFile(directory+'AlignmentList.xml') return pl
def toParticleList(self): """ toParticleList: Converts this object to a Particle List @return: A particle list that can be used for further processing @rtype: L{pytom.basic.structures.ParticleList} """ from pytom.basic.structures import ParticleList pl = ParticleList('/') for result in self._alignmentList: p = result.toParticle() pl.append(p) return pl
def run(fname, outname, cores=6): even = ParticleList() even.fromXMLFile(fname) aa = averageParallel(particleList=even, averageName=outname, showProgressBar=True, verbose=False, createInfoVolumes=False, weighting=False, norm=False, setParticleNodesRatio=3, cores=cores)
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 interpretRequestParameters(parameters): """ interpretRequestParameters """ from pytom.reconstruction.reconstructionStructures import ProjectionList from pytom.basic.structures import ParticleList from pytom.frontend.serverpages.serverMessages import FileMessage particleList = ParticleList('.') shellCommand = '' if 'tomo' in parameters: tomogram = parameters['tomo'] shellCommand += '--tomogram ' + tomogram if 'plXML' in parameters: shellCommand += '--particleList ' + parameters['plXML'] if shellCommand == '': raise RuntimeError('Tomogram or ParticleList parameter missing in request!') if 'prlDIR' in parameters: shellCommand += ' --projectionDirectory ' + parameters['prlDIR'] elif 'prlXML' in parameters: shellCommand += ' --projectionList ' + parameters['prlXML'] else: raise RuntimeError('ProjectionList parameter missing in request!') if 'ts' in parameters: shellCommand += ' --coordinatesScale ' + parameters['ts'] if 'x' in parameters: shellCommand += ' --size ' + parameters['x'] if 'xc' in parameters and 'yc' in parameters and 'zc' in parameters: shellCommand += ' --recOffset ' + parameters['xc'] + ',' + parameters['yc'] + ','+ parameters['zc'] if 'sb' in parameters: shellCommand += ' --projBinning ' + parameters['sb'] # if 'sa' in parameters: # shellCommand += ' --postScale ' + parameters['sa'] if 'jobFile' in parameters: jobFile = parameters['jobFile'] else: raise RuntimeError('No job file specified!') #print shellCommand try: createRunscripts(jobFile,shellCommand + '\n') except Exception as ex: print(ex) return FileMessage('ReconstructionJob',jobFile,'created')
def split_topn_classes(pls, n): """ sort the particle list by the length @param pls: particle list @type pls: L{pytom.basic.structures.ParticleList} @param n: number of top-n classes @type n: C{int} @return: new_pl """ # sort the particle list by the length assert len(pls) >= n pls.sort(key=lambda a: len(a)) # find the maximal running label max_label = -1 for pp in pls: cl = int(pp[0].getClass()) if cl > max_label: max_label = cl # split according to the score new_pls = [] i = 0 for pp in pls: class_label = pp[0].getClass() if class_label == '-1': # we do nothing with the trash class new_pls.append(pp) continue if i < n: pp.sortByScore() l = len(pp) p1 = pp[:l // 2] p2 = pp[l // 2:] p1.setClassAllParticles(str(max_label + 1)) p2.setClassAllParticles(str(max_label + 2)) new_pls.append(p1) new_pls.append(p2) print("Split class %s to %s and %s" % (class_label, str(max_label + 1), str(max_label + 2))) max_label += 2 i += 1 else: new_pls.append(pp) # return the result new_pl = ParticleList() for pp in new_pls: new_pl += pp return new_pl
def __init__(self, particleList=None, newAverageName=''): """ @param particleList: particle list @type particleList: L{pytom.basic.structures.ParticleList} @param newAverageName: name of output average @type newAverageName: L{str} """ from pytom.basic.structures import ParticleList self._particleList = particleList or ParticleList('/', []) self._newAverageName = newAverageName
def get_size(particleList, directory): tempPL = ParticleList() tempPL.fromXMLFile(particleList) tomoName = tempPL[0].getPickPosition().getOriginFilename( ) if tempPL[0].getPickPosition().getOriginFilename( ) else tempPL[0].getSourceInfo().getTomoName() if not os.path.exists(tomoName): tomoName = os.path.join(directory, tomoName) if not os.path.exists(tomoName): return 'Failed' try: dimx, dimy, dimz = read_size(tomoName) except: print('Failed') return 'Failed' return [dimx, dimy, dimz]
def extractClassesFromPL(pl_name, class_names, output): from pytom.basic.structures import ParticleList pl = ParticleList() pl.fromXMLFile(pl_name) pls = pl.splitByClass() class_labels = class_names.split(',') res = ParticleList() for pp in pls: if pp[0].getClass() in class_labels: res += pp if output: res.toXMLFile(output) return res
def average_sub_pl(self, pl, name_prefix): """For worker node, this function has been rewritten. """ from pytom.basic.structures import ParticleList even = ParticleList('.') odd = ParticleList('.') for i in range(len(pl)): if i%2 == 0: even.append(pl[i]) else: odd.append(pl[i]) self.sum_sub_pl(even, name_prefix+'even') self.sum_sub_pl(odd, name_prefix+'odd')
def initialize(pl, settings): from pytom.basic.structures import Particle # from pytom.alignment.alignmentFunctions import average2 from pytom.basic.filter import lowpassFilter print("Initializing the class centroids ...") pl = pl.copy() pl.sortByScore() if settings["noise"]: pl = pl[:int((1-settings["noise"])*len(pl))] K = settings["ncluster"] freq = settings["frequency"] kn = len(pl)//K references = {} frequencies = {} # get the first class centroid pp = pl[:kn] # avg, fsc = average2(pp, norm=True, verbose=False) pp.setClassAllParticles('0') res, tmp, tmp2 = calculate_averages(pp, settings["binning"], None, outdir=settings["output_directory"]) avg = res['0'] avg = lowpassFilter(avg, freq, freq/10.)[0] avg.write(os.path.join(settings['output_directory'], 'initial_0.em') ) p = Particle(os.path.join(settings['output_directory'], 'initial_0.em')) p.setClass('0') references['0'] = p frequencies['0'] = freq for k in range(1, K): distances = [4]*len(pl) for c, ref in references.items(): args = list(zip(pl, [ref]*len(pl), [freq]*len(pl), [settings["fmask"]]*len(pl), [settings["binning"]]*len(pl))) dist = mpi.parfor(distance, args) for i in range(len(pl)): if distances[i] > dist[i]: distances[i] = dist[i] distances = np.asarray(distances) print('sum distances: ', distances.sum()) distances = distances/np.sum(distances) idx = np.random.choice(len(pl), kn, replace=False, p=distances) pp = ParticleList() for i in idx: pp.append(pl[int(i)]) # avg, fsc = average2(pp, norm=True, verbose=False) pp.setClassAllParticles('0') res, tmp, tmp2 = calculate_averages(pp, settings["binning"], None, outdir=settings["output_directory"]) avg = res['0'] avg = lowpassFilter(avg, freq, freq/10.)[0] kname = os.path.join(settings['output_directory'], 'initial_{}.em'.format(k)) avg.write(kname) p = Particle(kname) p.setClass(str(k)) references[str(k)] = p frequencies[str(k)] = freq return references, frequencies
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 == "FRMResult": result = xmlObj else: result = xmlObj.xpath('FRMResult') if len(result) == 0: raise Exception("This XML is not a FRMResult.") result = result[0] from pytom.basic.structures import ParticleList particleList_element = result.xpath('ParticleList')[0] pl = ParticleList('.') pl.fromXML(particleList_element) self.pl = pl self.name = result.get('Name') self.worker_id = int(result.get('WorkerID'))
def applySymmetryOnParticleList(particleList,symmetry): """ applySymmetryOnParticleList @deprecated: use L{pytom.basic.structures.Symmetry.apply} instead! """ if symmetry.isOneFold(): return particleList from pytom.basic.structures import ParticleList,Rotation newList = ParticleList(particleList.getDirectory()) for i in range(len(particleList)): particle = particleList[i] originalRotation = particle.getRotation() symmetry.setPsi(originalRotation.getPsi()) symmetry.setTheta(originalRotation.getTheta()) phi = originalRotation.getPhi() angleList = symmetry.getAngleList(phi) rotation = angleList.nextRotation() rotation = angleList.nextRotation() while not rotation == [None,None,None]: p2 = particle.copy() p2.setRotation(Rotation(rotation)) newList.append(p2) rotation = angleList.nextRotation() return newList + particleList
def readSetOfVolumes(volsXml, volSet, **kwargs): """ Populate a Scipion set of volumes from a given xml file from PyTom. """ # Add to the path the root to pytom backupPath = list(sys.path) addPyTomPaths() from pytom.basic.structures import Particle, ParticleList, Wedge from pytom.score.score import Score, PeakPrior from pytom.frm.FRMAlignment import FRMScore pl = ParticleList() pl.fromXMLFile(volsXml) xmlDir = os.path.dirname(volsXml) for particle in pl: volFn = os.path.join(xmlDir, particle.getFilename()) vol = em.Volume() vol.setFileName(volFn) vol.pytomInfo = readPyTomInfo(particle) volSet.append(vol) sys.path = backupPath
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 import etree from lxml.etree import _Element from pytom.basic.structures import ParticleList 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.' ) if not xmlObj.tag == 'ExpectationJob': jobElement = xmlObj.xpath('ExpectationJob') if len(jobElement) == 0: from pytom.basic.exceptions import ParameterError raise ParameterError( 'You must provide a valid XML-ExpectationJob object.') else: jobElement = jobElement[0] else: jobElement = xmlObj self._newAverageName = jobElement.get('AverageName').__str__() particleListXML = xmlObj.xpath('ParticleList') self._particleList = ParticleList('/') if len(particleListXML) > 0: self._particleList.fromXML(particleListXML[0])
def average_sub_pl(self, pl, name_prefix): """For worker node, this function has been rewritten. """ from pytom.basic.structures import ParticleList even = ParticleList('.') odd = ParticleList('.') for p in pl: i = int(p.getClass()) if i % 2 == 0: even.append(p) else: odd.append(p) assert len( even) > 0, "average_sub_pl: length even particle list == 0 :(" assert len(odd) > 0, "average_sub_pl: length odd particle list == 0 :(" # in some rare cases this would fail # that is: this worker only get one class, in this case, one of the pl will be null # just restart the job self.sum_sub_pl(even, name_prefix + 'even') self.sum_sub_pl(odd, name_prefix + 'odd')
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 averageClasses(particleListFilename, avName): """ write class averages of classified particles @param particleListFilename: particle list filename @type particleListFilename: str @param avName: Name for class averages (<avName>_iclass.em) @type avName: str @author: FF @date: Jan 2013 """ from pytom.basic.structures import ParticleList pl = ParticleList() pl.fromXMLFile(particleListFilename) pl.sortByClassLabel() pls = pl.splitByClass() for cl in pls: className = cl[0].getClassName() cl.average(avName + "_" + str(className) + '.em') print(className, ' contains ', len(cl), ' particles')
def subTomoClust(particleListFilename, classifiedParticleListFilename, cccName, neig, nclass, verbose=False): """ subtomogram clustering using CCC and kmeans @param particleListFilename: particle list filename @type particleListFilename: str @param classifiedParticleListFilename: output particle list filename @type classifiedParticleListFilename: str @param cccName: Name of (Constrained) Correlation Coefficient (CCC) matrix @type cccName: str @param neig: number of eigenvectors @type neig: int @param nclass: number of classes @type nclass: int @author: FF Jan 2013 """ from pytom.basic.structures import ParticleList pl = ParticleList('.') pl.fromXMLFile(particleListFilename) if verbose: print("Particle List read in") ccc = readCCC(cccName) if verbose: print("CCC read in") coeffs, eigvalues = SVD_analysis(ccc) if verbose: print("Eigen analysis done") labels = kmeansCluster(coeff=coeffs, neig=neig, nclass=nclass) if verbose: print("kmeans clustering done") for (ipart, part) in enumerate(pl): part.setClass(className=str(labels[ipart])) if verbose: print("Class labels assigned") pl.toXMLFile(classifiedParticleListFilename) if verbose: print("File written")