def fromXML(self, xmlObj): """ fromXML : Assigns values to result attributes from XML object @param xmlObj: A xml object @author: chen """ 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-PeakResultMsg object.' ) from pytom.localization.peak_job import PeakResult if xmlObj.tag == "PeakResultMsg": main = xmlObj else: main = xmlObj.xpath('PeakResultMsg') if len(main) == 0: from pytom.parallel.messages import MessageError raise MessageError("This message is not a PeakResultMsg") main = main[0] message = main.xpath('Message') message = message[0] self._sender = message.get('Sender') self._recipient = message.get('Recipient') self._timestamp = message.get('Timestamp') res = main.xpath('Result')[0] self.result = PeakResult() self.result.fromXML(res)
def PeakResultMsg_Test(self): from pytom.localization.peak_job_msg import PeakResultMsg from pytom.localization.peak_job import PeakResult from pytom.localization.structures import Volume, Orientation v = Volume(self.testfilename) o = Orientation(self.testfilename) r = PeakResult(v, o) a = PeakResultMsg(str(1), str(0)) a.setResult(r) xmlObj = a.toXML() b = PeakResultMsg() b.fromXML(xmlObj) self.assertTrue(b.getSender() == a.getSender(), msg='') self.assertTrue(b.getRecipient() == a.getRecipient(), msg='')
def PeakResult_Test(self): from pytom.localization.peak_job import PeakResult from pytom.localization.structures import Volume, Orientation v = Volume(self.testfilename) o = Orientation(self.testfilename) a = PeakResult(v, o, 2) xmlObj = a.toXML() b = PeakResult() b.fromXML(xmlObj) self.assertTrue(b.jobID == a.jobID, msg='') self.assertTrue(b.result.getFilename() == a.result.getFilename(), msg='') self.assertTrue(b.orient.getFilename() == a.orient.getFilename(), msg='')
def writeRes(self, resV, orientV, jobID=None): """ writeRes: Write the result back to the disk, and return the PeakJobResult. @param resV: result volume @type resV: L{pytom_volume.vol} @param orientV: orientation volume @type orientV: L{pytom_volume.vol} @param jobID: ID of job @type jobID: integer @rtype: L{pytom.localization.peak_job.PeakResult} """ from pytom.tompy.io import read, write if jobID != None: resFilename = self.dstDir + self.name + '_job' + str( jobID) + '_res.em' orientFilename = self.dstDir + self.name + '_job' + str( jobID) + '_orient.em' else: resFilename = self.dstDir + self.name + '_res.em' orientFilename = self.dstDir + self.name + '_orient.em' try: resV.write(resFilename) orientV.write(orientFilename) except: print(resFilename, orientFilename) write(resFilename, resV) write(orientFilename, orientV) from pytom.localization.structures import Volume, Orientation res = Volume(resFilename) orient = Orientation(orientFilename) # construct the result from pytom.localization.peak_job import PeakResult result = PeakResult(res, orient, jobID) return result
def parallelRun(self, verbose=True, **kwargs): ''' parallelRun: Run the worker in parallel status and send the result message back @param verbose: verbose mode @type verbose: boolean ''' from pytom.parallel.messages import StatusMessage, MessageError from pytom.basic.exceptions import ParameterError from pytom.basic.structures import PyTomClassError end = False while not end: # get the message string mpi_msgString = getMsgStr() try: msg = self.getJobMsg(mpi_msgString) self.jobFromMsg(msg) if verbose == True: print(self.name + ': running...') [resV, orientV, sumV, sqrV] = self.run(verbose, moreInfo=True) # write the result volume back to the disk resFilename = self.name + '_job' + str(self.jobID) + '_res.em' orientFilename = self.name + '_job' + str( self.jobID) + '_orient.em' resV.write(resFilename) orientV.write(orientFilename) if sumV and sqrV: sumFilename = self.name + '_job' + str( self.jobID) + '_sum.em' sqrFilename = self.name + '_job' + str( self.jobID) + '_sqr.em' sumV.write(sumFilename) sqrV.write(sqrFilename) from pytom.localization.structures import Volume, Orientation res = Volume(resFilename, self.volume.subregion) orient = Orientation(orientFilename) # construct the result from pytom.localization.peak_job import PeakResult result = PeakResult(res, orient, self.jobID) # sent the result back if verbose == True: print(self.name + ': sending back result') result.send(self.mpi_id, self.backTo) except (MessageError, PyTomClassError, ParameterError): try: # get the message as StatusMessage and finish msg = StatusMessage('', '') msg.fromStr(mpi_msgString) if msg.getStatus() == 'End': end = True if verbose == True: print(self.name + ': ending...') except (MessageError, PyTomClassError, ParameterError): print( 'Error parsing message. Message either unknown or invalid.' ) assert False
class PeakResultMsg(Message): result = -1 def setResult(self, result): """ @param result: result @type result: L{pytom.localization.peak_job.PeakResult} """ self.result = result def getResult(self): return self.result def fromXML(self, xmlObj): """ fromXML : Assigns values to result attributes from XML object @param xmlObj: A xml object @author: chen """ 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-PeakResultMsg object.' ) from pytom.localization.peak_job import PeakResult if xmlObj.tag == "PeakResultMsg": main = xmlObj else: main = xmlObj.xpath('PeakResultMsg') if len(main) == 0: from pytom.parallel.messages import MessageError raise MessageError("This message is not a PeakResultMsg") main = main[0] message = main.xpath('Message') message = message[0] self._sender = message.get('Sender') self._recipient = message.get('Recipient') self._timestamp = message.get('Timestamp') res = main.xpath('Result')[0] self.result = PeakResult() self.result.fromXML(res) def toXML(self): from lxml import etree resMsg = etree.Element("PeakResultMsg") messageElement = etree.Element("Message", Sender=self._sender, Recipient=self._recipient, Timestamp=self._timestamp.__str__()) resMsg.append(messageElement) resultElement = self.result.toXML() resMsg.append(resultElement) return resMsg