def readAll(self):
        """
        readAll: Read all the specified files from the disk (except the original volume)
        """
        from pytom.localization.structures import Volume, Orientation
        self.result = Volume(self.resultFilename).getVolume()
        self.orient = Orientation(self.orientFilename).getVolume()

        if self.angleListFilename != '':
            from pytom.angles.globalSampling import GlobalSampling
            self.angleList = GlobalSampling(
                self.angleListFilename).getRotations()
Beispiel #2
0
    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='')
Beispiel #3
0
    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='')
Beispiel #4
0
    def Volume_Test(self):
        from pytom.localization.structures import Volume

        filename = self.testfilename
        a = Volume(filename)

        self.assertTrue(a.getFilename() == filename,
                        msg='filename not stored in volume structure')

        xmlObj = a.toXML()
        b = Volume()
        b.fromXML(xmlObj)

        self.assertTrue(b.getFilename() == filename,
                        msg='filename not updated in volume structure')
Beispiel #5
0
    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:
            raise Exception(
                'Is not a lxml.etree._Element! You must provide a valid XML object.'
            )

        if xmlObj.tag == "Result":
            result = xmlObj
        else:
            result = xmlObj.xpath('Result')

            if len(result) == 0:
                raise Exception(
                    "This XML is not an PeakResult. No Result provided.")

            result = result[0]

        self.jobID = int(result.get('ID'))

        from pytom.localization.structures import Volume, Orientation
        e = result.xpath('Volume')[0]
        p = Volume()
        p.fromXML(e)
        self.result = p

        e = result.xpath('Orientation')[0]
        o = Orientation()
        o.fromXML(e)
        self.orient = o
Beispiel #6
0
    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='')
Beispiel #7
0
    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='')
Beispiel #8
0
    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
Beispiel #9
0
    def splitVolumes(self, job, splitX, splitY, splitZ, verbose=True):
        """
        splitVolumes: Split the job in the "volume" way (sequentially)
        @param job: job
        @type job: L{pytom.localization.peak_job.PeakJob}
        @param splitX: split part along the x dimension
        @type splitX: integer
        @param splitY: split part along the y dimension
        @type splitY: integer
        @param splitZ: split part along the z dimension
        @type splitZ: integer
        """
        # check if the split is feasible
        if job.volume.subregion == [0, 0, 0, 0, 0, 0]:
            v = job.volume.getVolume()
            origin = [0, 0, 0]
            vsizeX = v.sizeX()
            vsizeY = v.sizeY()
            vsizeZ = v.sizeZ()
        else:
            origin = job.volume.subregion[0:3]
            vsizeX = job.volume.subregion[3]
            vsizeY = job.volume.subregion[4]
            vsizeZ = job.volume.subregion[5]

        sizeX = vsizeX // splitX
        sizeY = vsizeY // splitY
        sizeZ = vsizeZ // splitZ
        r = job.reference.getVolume()
        rsizeX = r.sizeX()
        rsizeY = r.sizeY()
        rsizeZ = r.sizeZ()
        if rsizeX > sizeX or rsizeY > sizeY or rsizeZ > sizeZ:
            raise RuntimeError("Not big enough volume to split!")

#        # initialize the jobInfo structure
#        self.jobInfo["numDoneJobs"] = 0
#        self.jobInfo["numJobs"] = 0
#        self.jobInfo["originalJobID"] = job.jobID
#        self.jobInfo["splitType"] = "Vol"
#
#        self.jobInfo["originalSize"] = [vsizeX, vsizeY, vsizeZ]
#        self.jobInfo["splitSize"] = [sizeX, sizeY, sizeZ]
        originalJobID = job.jobID

        # read the target volume, calculate the respective subregion
        from pytom.localization.peak_job import PeakJob
        from pytom.localization.structures import Volume
        _start = [
            -rsizeX // 2 + origin[0], -rsizeY // 2 + origin[1],
            -rsizeZ // 2 + origin[2]
        ]
        _size = [sizeX + rsizeX, sizeY + rsizeY, sizeZ + rsizeZ]

        numPieces = splitX * splitY * splitZ
        totalMem = self.members
        numMemEach = totalMem // numPieces
        targetID = self.mpi_id

        for i in range(numPieces):
            strideZ = splitX * splitY
            strideY = splitX
            incZ = i // strideZ
            incY = (i % strideZ) // strideY
            incX = i % strideY
            _start = [
                -rsizeX // 2 + origin[0] + incX * sizeX,
                -rsizeY // 2 + origin[1] + incY * sizeY,
                -rsizeZ // 2 + origin[2] + incZ * sizeZ
            ]

            start = _start[:]
            end = [start[j] + _size[j] for j in range(len(start))]

            if start[0] < origin[0]:
                start[0] = origin[0]
            if start[1] < origin[1]:
                start[1] = origin[1]
            if start[2] < origin[2]:
                start[2] = origin[2]
            if end[0] > vsizeX + origin[0]:
                end[0] = vsizeX + origin[0]
            if end[1] > vsizeY + origin[1]:
                end[1] = vsizeY + origin[1]
            if end[2] > vsizeZ + origin[2]:
                end[2] = vsizeZ + origin[2]

            size = [end[j] - start[j] for j in range(len(start))]

            #            # make sure that the last dimension is not odd
            #            if size[2]%2 == 1:
            #                size[2] = size[2] - 1
            #                end[2] = end[2] - 1

            # for reassembling the result
            whole_start = start[:]
            sub_start = [0, 0, 0]
            if start[0] != origin[0]:
                whole_start[0] = start[0] + rsizeX // 2
                sub_start[0] = rsizeX // 2
            if start[1] != origin[1]:
                whole_start[1] = start[1] + rsizeY // 2
                sub_start[1] = rsizeY // 2
            if start[2] != origin[2]:
                whole_start[2] = start[2] + rsizeZ // 2
                sub_start[2] = rsizeZ // 2

#            self.jobInfo[subJobID] = [sub_start, whole_start]
            subJobID = job.jobID + i + 1
            subVol = Volume(
                job.volume.getFilename(),
                [start[0], start[1], start[2], size[0], size[1], size[2]])
            if i == 0:
                numMem = totalMem - (numPieces - 1) * numMemEach
            else:
                numMem = numMemEach
            subJob = PeakJob(subVol, job.reference, job.mask, job.wedge,
                             job.rotations, job.score, subJobID, numMem,
                             self.dstDir, job.bandpass)

            from pytom.localization.peak_job import JobInfo
            info = JobInfo(subJob.jobID, originalJobID, "Vol")
            info.sub_start = sub_start
            info.whole_start = whole_start
            info.originalSize = [vsizeX, vsizeY, vsizeZ]
            info.splitSize = [sizeX, sizeY, sizeZ]
            info.origin = origin
            self.jobInfoPool[subJobID] = info

            if targetID == self.mpi_id:
                self.setJob(subJob)
                if self.members > 1:
                    self.splitAngles(subJob, verbose)
            else:
                if verbose == True:
                    print(self.name + ' : send part of the volume to ' +
                          str(targetID))
                print(targetID, type(targetID))
                subJob.send(self.mpi_id, targetID)

            targetID = targetID + numMem
            self.jobInfoPool["numJobsV"] = self.jobInfoPool["numJobsV"] + 1
Beispiel #10
0
    def splitVolumes(self, job, splitX, splitY, splitZ, verbose=True):
        """
        splitVolumes: Split the target volume in job into smaller ones.
        """
        # check if the split is feasible
        v = job.volume.getVolume()
        r = job.reference.getVolume()

        print(r.sizeX(), r.sizeY())

        vsizeX = v.sizeX()
        vsizeY = v.sizeY()
        vsizeZ = v.sizeZ()
        sizeX = vsizeX // splitX
        sizeY = vsizeY // splitY
        sizeZ = vsizeZ // splitZ
        rsizeX = r.sizeX()
        rsizeY = r.sizeY()
        rsizeZ = r.sizeZ()
        if rsizeX > sizeX or rsizeY > sizeY or rsizeZ > sizeZ:
            raise RuntimeError("Not big enough volume to split!")

        self.jobInfo["originalSize"] = [vsizeX, vsizeY, vsizeZ]
        self.jobInfo["splitSize"] = [sizeX, sizeY, sizeZ]

        # read the target volume, calculate the respective subregion
        from pytom.localization.peak_job import PeakJob
        from pytom.localization.structures import Volume
        _start = [-rsizeX // 2, -rsizeY // 2, -rsizeZ // 2]
        _size = [sizeX + rsizeX, sizeY + rsizeY, sizeZ + rsizeZ]

        for i in range(splitX * splitY * splitZ):
            strideZ = splitX * splitY
            strideY = splitX
            incZ = i // strideZ
            incY = (i % strideZ) // strideY
            incX = i % strideY
            _start = [
                -rsizeX // 2 + incX * sizeX, -rsizeY // 2 + incY * sizeY,
                -rsizeZ // 2 + incZ * sizeZ
            ]

            start = _start[:]
            end = [start[j] + _size[j] for j in range(len(start))]

            if start[0] < 0:
                start[0] = 0
            if start[1] < 0:
                start[1] = 0
            if start[2] < 0:
                start[2] = 0
            if end[0] > vsizeX:
                end[0] = vsizeX
            if end[1] > vsizeY:
                end[1] = vsizeY
            if end[2] > vsizeZ:
                end[2] = vsizeZ

            size = [end[j] - start[j] for j in range(len(start))]
            #            print start[0], start[1], start[2], size[0], size[1], size[2]
            print(size)
            # for reassembling the result
            whole_start = start[:]
            sub_start = [0, 0, 0]
            if start[0] != 0:
                whole_start[0] = start[0] + rsizeX // 2
                sub_start[0] = rsizeX // 2
            if start[1] != 0:
                whole_start[1] = start[1] + rsizeY // 2
                sub_start[1] = rsizeY // 2
            if start[2] != 0:
                whole_start[2] = start[2] + rsizeZ // 2
                sub_start[2] = rsizeZ // 2
            self.jobInfo[i + 1] = [sub_start, whole_start]

            subVol = Volume(
                job.volume.getFilename(),
                [start[0], start[1], start[2], size[0], size[1], size[2]])
            subJob = PeakJob(subVol, job.reference, job.mask, job.wedge,
                             job.rotations, job.score, i + 1, job.dstDir,
                             job.bandpass)
            subJob.send(0, i % self.numWorkers + 1)
Beispiel #11
0
    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
Beispiel #12
0
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 ExPeakResult:
    """
    ExPeakResult: ExPeakResult of scoring function, used to retrieve score and orientation values.
    """
    def __init__(self,
                 volFilename='',
                 resultFilename='',
                 orientFilename='',
                 angleListFilename='',
                 score=None):
        """
        @param volFilename: filename of original volume
        @type volFilename: string
        @param resultFilename: filename of result volume
        @type resultFilename: string
        @param orientFilename: filename of orientation volume
        @type orientFilename: string
        @param angleListFilename: filename of used angle list
        @type angleListFilename: string
        @param score: score type of getting this result
        @type score: L{pytom.alignment.score}
        
        @author: chen
        """
        self.volFilename = volFilename
        self.resultFilename = resultFilename
        self.orientFilename = orientFilename
        self.angleListFilename = angleListFilename
        self.score = score

        self.result = None
        self.orient = None
        self.angleList = None

    def readAll(self):
        """
        readAll: Read all the specified files from the disk (except the original volume)
        """
        from pytom.localization.structures import Volume, Orientation
        self.result = Volume(self.resultFilename).getVolume()
        self.orient = Orientation(self.orientFilename).getVolume()

        if self.angleListFilename != '':
            from pytom.angles.globalSampling import GlobalSampling
            self.angleList = GlobalSampling(
                self.angleListFilename).getRotations()

    def get(self, x, y, z):
        """
        get : Get both the score and orientation values according to index
        
        @param x: x dimension
        @type x: integer
        @param y: y dimension
        @type y: integer
        @param z: z dimension
        @type z: integer
        @return: both score and orientation
        @rtype: [float, [float, float, float] ]
        
        @author: chen
        """
        if self.result == None or self.orient == None or self.angleList == None:
            self.readAll()
        score = self.result.getV(x, y, z)
        orientIdx = self.orient.getV(x, y, z)
        if int(orientIdx) < len(self.angleList):
            orientation = self.angleList[int(orientIdx)]
        else:
            orientation = self.angleList[-1]

        return [score, orientation]

    def maskOut(self, mask, center, size):
        """
        maskOut: Set part of mask volume to all zero. The region is specified by center and size.
        @param mask: volume that you handle with
        @type mask: L{pytom_volume.vol}
        @param center: center of the region
        @type center: [x,y,z]
        @param size: size of the region
        @type size: [sizeX, sizeY, sizeZ] or radius
        """

        from pytom_volume import vol, putSubVolume

        if size.__class__ == list:
            p_sizeX = size[0]
            p_sizeY = size[1]
            p_sizeZ = size[2]
        elif size.__class__ == vol:
            mm = size
            p_sizeX = mm.sizeX()
            p_sizeY = mm.sizeY()
            p_sizeZ = mm.sizeZ()
        else:
            radius = size
            p_sizeX = radius * 2
            p_sizeY = radius * 2
            p_sizeZ = radius * 2

        maskSize = [mask.sizeX(), mask.sizeY(), mask.sizeZ()]

        if maskSize < center:
            raise RuntimeError('Center out of range!')

        # [)
        # mask out double size. CHANGED!!!
        startX = int(center[0] - p_sizeX / 2)
        endX = int(center[0] + p_sizeX / 2)
        startY = int(center[1] - p_sizeY / 2)
        endY = int(center[1] + p_sizeY / 2)
        startZ = int(center[2] - p_sizeZ / 2)
        endZ = int(center[2] + p_sizeZ / 2)

        # only used for radius
        sub_startX = 0
        sub_startY = 0
        sub_startZ = 0

        if startX < 0:
            sub_startX = -startX
            startX = 0
        if endX > maskSize[0]:
            endX = maskSize[0]
        if startY < 0:
            sub_startY = -startY
            startY = 0
        if endY > maskSize[1]:
            endY = maskSize[1]
        if startZ < 0:
            sub_startZ = -startZ
            startZ = 0
        if endZ > maskSize[2]:
            endZ = maskSize[2]

        sizeX = endX - startX
        sizeY = endY - startY
        sizeZ = endZ - startZ

        if size.__class__ == list:
            subV = vol(sizeX, sizeY, sizeZ)
            subV.setAll(0)
        elif size.__class__ == vol:
            from pytom_volume import limit, subvolume
            subV = (mm - 1) / -1
            limit(subV, 0.999, 0, 0, 0, True, False)
            subV = subvolume(subV, sub_startX, sub_startY, sub_startZ, sizeX,
                             sizeY, sizeZ)
            tempV = subvolume(mask, startX, startY, startZ, sizeX, sizeY,
                              sizeZ)
            subV = subV * tempV  # AND operation
        else:
            from pytom_volume import initSphere, subvolume
            subV = vol(radius * 2, radius * 2, radius * 2)
            initSphere(subV, radius, 0, 0, radius, radius, radius)
            tempV = vol(radius * 2, radius * 2, radius * 2)
            tempV.setAll(1)
            subV = tempV - subV
            subV = subvolume(subV, sub_startX, sub_startY, sub_startZ, sizeX,
                             sizeY, sizeZ)
            tempV = subvolume(mask, startX, startY, startZ, sizeX, sizeY,
                              sizeZ)
            subV = subV * tempV  # AND operation

        putSubVolume(subV, mask, startX, startY, startZ)

    def findParticles(self,
                      sizeParticle,
                      maxNumParticle=0,
                      minScore=-1,
                      write2disk=0,
                      margin=None,
                      offset=[0, 0, 0]):
        """
        findParticles: Find particles in target volume according to the result volume.
        @param sizeParticle: size or radius of searched particle
        @type sizeParticle: [x,y,z] or integer
        @param maxNumParticle: maximal number of particles you want to pick
        @type maxNumParticle: integer
        @param minScore: minimal score as threshold
        @type minScore: float 
        @param write2disk: write the found particles to the disk or not (0: do not write, otherwise the length of each dimension)
        @type write2disk: integer
        @param margin: set the margin of the score volume
        @param margin: [x,y,z] or integer
        
        @return: list of found particles
        @rtype: L{pytom.localization.structures.FoundParticle}
        """
        from pytom_volume import vol, peak, putSubVolume, read
        from pytom.localization.structures import FoundParticle

        # prepare the mask
        x = self.result.sizeX()
        y = self.result.sizeY()
        z = self.result.sizeZ()

        if sizeParticle.__class__ == list:
            xP = sizeParticle[0]
            yP = sizeParticle[1]
            zP = sizeParticle[2]
        elif sizeParticle.__class__ == vol:
            xP = sizeParticle.sizeX()
            yP = sizeParticle.sizeY()
            zP = sizeParticle.sizeZ()
        else:
            radius = sizeParticle
            xP = 2 * sizeParticle
            yP = 2 * sizeParticle
            zP = 2 * sizeParticle

        if margin:
            if margin.__class__ == list:
                marginX, marginY, marginZ = margin
            else:
                marginX = marginY = marginZ = margin
        else:  # no margin given, set automatically
            marginX = int(xP / 2)
            marginY = int(yP / 2)
            marginZ = int(zP / 2)

        mask = vol(x, y, z)
        mask.setAll(0)

        maskIn = vol(x - 2 * marginX, y - 2 * marginY, z - 2 * marginZ)
        maskIn.setAll(1)
        putSubVolume(maskIn, mask, marginX, marginY, marginZ)

        # progress bar
        from pytom.tools.ProgressBar import FixedProgBar
        prog = FixedProgBar(0, maxNumParticle - 1, '')

        # find the particles
        resList = []
        for i in range(maxNumParticle):
            prog.update(i)

            try:
                posV = peak(self.result, mask)
            except:
                break  # the mask is all zero

            [scoreV, orientV] = self.get(posV[0], posV[1], posV[2])
            # test if the peak score is bigger than minimal threshold
            if scoreV > minScore:
                particleFilename = 'particle_' + str(i) + '.em'
                if write2disk:
                    # write the found particle to the disk
                    l = write2disk
                    v = read(self.volFilename, posV[0] - l / 2,
                             posV[1] - l / 2, posV[2] - l / 2, l, l, l, 0, 0,
                             0, 0, 0, 0)
                    v.write(particleFilename)

                score = self.score()
                score.setValue(scoreV)
                from pytom.basic.structures import PickPosition, Rotation
                pos = PickPosition(posV, originFilename=self.volFilename)
                pos + offset
                orientation = Rotation(orientV)
                p = FoundParticle(pos, orientation, score, particleFilename)

                resList.append(p)

                if sizeParticle.__class__ == list:
                    self.maskOut(mask, posV, [xP, yP, zP])
                elif sizeParticle.__class__ == vol:
                    self.maskOut(mask, posV, sizeParticle)
                else:
                    self.maskOut(mask, posV, radius)
            else:
                break

        return resList
Beispiel #14
0
    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.basic.structures import Mask, Reference, Wedge, BandPassFilter
    from pytom.localization.structures import Volume
    from pytom.angles.globalSampling import GlobalSampling
    from pytom.score.score import FLCFScore
    from pytom.localization.peak_job import PeakJob
    from pytom.frontend.serverpages.createLocalizationJob import createRunscripts

    v = Volume(volume)
    r = Reference(reference)
    m = Mask(mask)
    w = Wedge([float(wedge1), float(wedge2)])
    a = GlobalSampling(angles)

    job = PeakJob(volume=v,
                  reference=r,
                  mask=m,
                  wedge=w,
                  rotations=a,
                  score=FLCFScore(),
                  jobID=0,
                  members=1,
                  dstDir=destination,
                  bandpass=BandPassFilter(0, float(band), 0))
Beispiel #15
0
    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
Beispiel #16
0
    @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')