Ejemplo n.º 1
0
    def test_FRM(self):
        import swig_frm
        from sh_alignment.frm import frm_align
        from pytom_volume import vol, rotate, shift
        from pytom.basic.structures import Rotation, Shift
        from pytom.tools.maths import rotation_distance

        v = vol(32,32,32)
        v.setAll(0)
        vMod = vol(32,32,32)
        vRot = vol(32,32,32)
        v.setV(1,10,10,10)
        v.setV(1,20,20,20)
        v.setV(1,15,15,15)
        v.setV(1,7,21,7)
        
        rotation = Rotation(10,20,30)
        shiftO = Shift(1,-3,5)
        
        rotate(v,vRot,rotation.getPhi(),rotation.getPsi(),rotation.getTheta())
        shift(vRot,vMod,shiftO.getX(),shiftO.getY(),shiftO.getZ())
        
        pos, ang, score = frm_align(vMod, None, v, None, [4, 64], 10)
        rotdist = rotation_distance(ang1=rotation, ang2=ang)
        diffx = shiftO[0] - (pos[0] - 16)
        diffy = shiftO[1] - (pos[1] - 16)
        diffz = shiftO[2] - (pos[2] - 16)

        self.assertTrue( rotdist < 5., msg='Rotations are different')
        self.assertTrue( diffx < .5, msg='x-difference > .5')
        self.assertTrue( diffy < .5, msg='y-difference > .5')
        self.assertTrue( diffz < .5, msg='z-difference > .5')
Ejemplo n.º 2
0
def theta_vol_projection(vol_src, theta):

    from pytom_volume import vol
    from pytom_volume import rotate
    from pytom_volume import subvolume
    from pytom_volume import sum

    vol_src_dim_x = vol_src.sizeX()
    vol_src_dim_y = vol_src.sizeY()
    vol_src_dim_z = vol_src.sizeZ()

    vol_dst = vol(vol_src_dim_x, vol_src_dim_y, vol_src_dim_z)
    vol_dst.setAll(0.0)

    rotate(vol_src, vol_dst, 270, 90, theta)

    vol_img = vol(vol_src_dim_x, vol_src_dim_y, 1)
    vol_img.setAll(0.0)

    for i in range(vol_src_dim_x):
        for j in range(vol_src_dim_y):

            vol_img.setV(sum(subvolume(vol_dst, i, j, 0, 1, 1, vol_src_dim_z)),
                         i, j, 0)

    return vol_img
Ejemplo n.º 3
0
def applySymmetryToVolume(volume,symmetryObject,wedgeInfo):
    """
    applySymmetryToVolume
    @deprecated: use L{pytom.basic.structures.Symmetry.apply} instead!
    """
    from pytom_volume import read,rotate,shift,vol,initSphere,complexDiv
    from pytom_freqweight import weight
    from pytom.basic.fourier import fft,ifft,ftshift
    from pytom.basic.filter import filter
    from pytom.alignment.structures import ExpectationResult
    from pytom.basic.structures import Reference,Symmetry
    from pytom.tools.maths import epsilon
    
    
    if not volume.__class__ == vol:
        raise Exception('You must provide a volume as first parameter to applySymmetryToObject')
    
    if not symmetryObject.__class__ == Symmetry:
        raise Exception('You must provide a Symmetry object as second parameter to applySymmetryToObject')
    
    result = vol(volume.sizeX(),volume.sizeY(),volume.sizeZ())
    result.copyVolume(volume)
    
    sizeX = volume.sizeX()
    sizeY = volume.sizeY()
    sizeZ = volume.sizeZ()
    
    rot = vol(sizeX,sizeY,sizeZ)
    
    wedgeSum = vol(sizeX,sizeY,sizeZ/2+1)
    wedgeSum.setAll(0.0)
    
    angleList = symmetryObject.getAngleList()
    
    rotation = angleList.nextRotation() 
    
    while not rotation == [None,None,None]:
        
          rotate(volume,rot,rotation[0],rotation[1],rotation[2])          
          result = result + rot
          
          rotation = angleList.nextRotation()
    

    result.shiftscale(0.0,1/float(angleList.numberRotations()))
    return result
Ejemplo n.º 4
0
def particleVolume(particleList,
                   templateVolume,
                   dimX,
                   dimY,
                   dimZ,
                   volume=None):

    from pytom_volume import vol, paste, rotate, subvolume

    if volume == None:
        volume = vol(dimX, dimY, dimZ)
        volume.setAll(0.0)

    for p in particleList:

        x = p.getPickPosition().getX()
        y = p.getPickPosition().getY()
        z = p.getPickPosition().getZ()

        z1 = p.getRotation().getZ1()
        z2 = p.getRotation().getZ2()
        x1 = p.getRotation().getX()

        tempTemplate = vol(templateVolume.sizeX(), templateVolume.sizeY(),
                           templateVolume.sizeZ())
        tempTemplate.setAll(0.0)
        rotate(templateVolume, tempTemplate, z1, z2, x1)
        tempTemplate = tempTemplate + subvolume(
            volume, int(x - templateVolume.sizeX() / 2),
            int(y - templateVolume.sizeY() / 2),
            int(z - templateVolume.sizeZ() / 2), templateVolume.sizeX(),
            templateVolume.sizeY(), templateVolume.sizeZ())
        paste(tempTemplate, volume, int(x - templateVolume.sizeX() / 2),
              int(y - templateVolume.sizeY() / 2),
              int(z - templateVolume.sizeZ() / 2))

    return volume
Ejemplo n.º 5
0
def extractPeaks(volume,
                 reference,
                 rotations,
                 scoreFnc=None,
                 mask=None,
                 maskIsSphere=False,
                 wedgeInfo=None,
                 **kwargs):
    '''
    Created on May 17, 2010
    @param volume: target volume
    @type volume: L{pytom_volume.vol}
    @param reference: reference
    @type reference: L{pytom_volume.vol}
    @param rotations: rotation angle list
    @type rotations: L{pytom.angles.globalSampling.GlobalSampling}
    @param scoreFnc: score function that is used
    @type scoreFnc: L{pytom.basic.correlation}
    @param mask: mask volume
    @type mask: L{pytom_volume.vol}
    @param maskIsSphere: flag to indicate whether the mask is sphere or not
    @type maskIsSphere: boolean
    @param wedgeInfo: wedge information
    @type wedgeInfo: L{pytom.basic.structures.WedgeInfo}
    @return: both the score volume and the corresponding rotation index volume
    @rtype: L{pytom_volume.vol}
    @author: chen
    '''
    #    from pytom.tools.timing import timing
    #    t = timing(); t.start()

    # parse the parameters
    nodeName = kwargs.get('nodeName', '')
    verbose = kwargs.get('verboseMode', True)
    if verbose not in [True, False]:
        verbose = True
    moreInfo = kwargs.get('moreInfo', False)
    if moreInfo not in [True, False]:
        moreInfo = False

    from pytom.basic.correlation import FLCF
    from pytom.basic.structures import WedgeInfo, Wedge
    from pytom_volume import vol, pasteCenter
    from pytom_volume import rotateSpline as rotate  # for more accuracy
    from pytom_volume import updateResFromIdx
    from pytom.basic.files import write_em

    if scoreFnc == None:
        scoreFnc = FLCF

    # only FLCF needs mask
    if scoreFnc == FLCF:
        if mask.__class__ != vol:  # construct a sphere mask by default
            from pytom_volume import initSphere
            mask = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ())
            mask.setAll(0)
            initSphere(mask,
                       reference.sizeX() / 2, 0, 0,
                       reference.sizeX() / 2,
                       reference.sizeX() / 2,
                       reference.sizeX() / 2)
            maskIsSphere = True

    # result volume which stores the score
    result = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
    result.setAll(-1)

    # result orientation of the peak value (index)
    orientation = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
    orientation.setAll(0)

    currentRotation = rotations.nextRotation()
    index = 0

    if verbose == True:
        from pytom.tools.ProgressBar import FixedProgBar
        max = rotations.numberRotations() - 1
        prog = FixedProgBar(0, max, nodeName)
    if moreInfo:
        sumV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        sumV.setAll(0)
        sqrV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        sqrV.setAll(0)
    else:
        sumV = None
        sqrV = None

    if wedgeInfo.__class__ == WedgeInfo or wedgeInfo.__class__ == Wedge:
        print('Applied wedge to volume')
        volume = wedgeInfo.apply(volume)

    while currentRotation != [None, None, None]:
        if verbose == True:
            prog.update(index)

        # rotate the reference
        ref = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ())
        rotate(reference, ref, currentRotation[0], currentRotation[1],
               currentRotation[2])

        # apply wedge
        if wedgeInfo.__class__ == WedgeInfo or wedgeInfo.__class__ == Wedge:
            ref = wedgeInfo.apply(ref)

        # rotate the mask if it is asymmetric
        if scoreFnc == FLCF:
            if maskIsSphere == False:  # if mask is not a sphere, then rotate it
                m = vol(mask.sizeX(), mask.sizeY(), mask.sizeZ())
                rotate(mask, m, currentRotation[0], currentRotation[1],
                       currentRotation[2])
            else:
                m = mask

        # compute the score
        # if mask is sphere and it is the first run, compute the standard deviation of the volume under mask for late use
        if scoreFnc == FLCF and index == 0 and maskIsSphere == True:
            # compute standard deviation of the volume under mask
            maskV = m
            if volume.sizeX() != m.sizeX() or volume.sizeY() != m.sizeY(
            ) or volume.sizeZ() != m.sizeZ():
                maskV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
                maskV.setAll(0)
                pasteCenter(m, maskV)
            from pytom_volume import sum
            p = sum(m)
            from pytom.basic.correlation import meanUnderMask, stdUnderMask
            meanV = meanUnderMask(volume, maskV, p)
            stdV = stdUnderMask(volume, maskV, p, meanV)

        # ref.write('template_cpu.em')

        if scoreFnc == FLCF:
            if maskIsSphere == True:
                score = scoreFnc(volume, ref, m, stdV, wedge=1)
            else:
                score = scoreFnc(volume, ref, m)
        else:  # not FLCF, so doesn't need mask as parameter and perhaps the reference should have the same size
            _ref = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
            _ref.setAll(0)
            pasteCenter(ref, _ref)

            score = scoreFnc(volume, _ref)

        # update the result volume and the orientation volume
        updateResFromIdx(result, score, orientation, index)

        if moreInfo:
            sumV = sumV + score
            sqrV = sqrV + score * score

        currentRotation = rotations.nextRotation()
        index = index + 1


#    if moreInfo:
#        sumV = sumV/rotations.numberRotations()
#        sqrV = sqrV/rotations.numberRotations()

#    time = t.end(); print 'The overall execution time: %f' % time

    return [result, orientation, sumV, sqrV]
Ejemplo n.º 6
0
def simpleSimulation(volume,
                     rotation,
                     shiftV,
                     wedgeInfo=None,
                     SNR=0.1,
                     mask=None):
    """
    simpleSimulation: Simulates an ET by applying rotation,shift,wedge and noise to an volume
    
    @param volume: the volume used for simulations
    @param rotation: the rotation applied to volume
    @param shiftV: shift vector applied to volume
    @param wedgeInfo: wedge applied to volume
    @param SNR: noise level applied to volume
    @param mask: Apodisation mask 
    @return: a simple cryo em simulation of volume 
    """
    from pytom_volume import vol, rotate, shift, initSphere
    from pytom.simulation import whiteNoise

    if not rotation == [0, 0, 0]:
        #print '---ROTATE---'
        #print 'EMSimulation simpleSimulation: in rotation 1 ' + str(rotation)
        rotatedCopy = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        rotate(volume, rotatedCopy, rotation[0], rotation[1], rotation[2])
    else:
        rotatedCopy = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        rotatedCopy.copyVolume(volume)

    #print 'EMSimulation simpleSimulation: after rotation '

    if not mask:
        #print 'EMSimulation simpleSimulation: in mask 1'
        mask = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        initSphere(mask,
                   volume.sizeX() // 2 - 1, 0, 0,
                   volume.sizeX() // 2,
                   volume.sizeX() // 2,
                   volume.sizeX() // 2)
        maskedCopy = rotatedCopy * mask
    if not mask.__class__ == vol:
        #print 'EMSimulation simpleSimulation: in mask 2'

        mask = mask.getVolume(rotation)
        maskedCopy = rotatedCopy * mask

    else:
        #print 'EMSimulation simpleSimulation: in mask 3'
        maskedCopy = rotatedCopy * mask

    #print 'EMSimulation simpleSimulation:  after mask'

    if not shiftV == [0, 0, 0]:
        #print '--SHIFT---'
        shiftedCopy = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        shift(maskedCopy, shiftedCopy, shiftV[0], shiftV[1], shiftV[2])
    else:
        shiftedCopy = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
        shiftedCopy.copyVolume(maskedCopy)

    if (shiftV == [0, 0, 0]) and (rotation == [0, 0, 0]):
        #no shift and no rotation -> simply take the original volume
        c = vol(maskedCopy.sizeX(), volume.sizeY(), volume.sizeZ())
        c.copyVolume(maskedCopy)
        noisyCopy = whiteNoise.add(c, SNR)
    else:
        noisyCopy = whiteNoise.add(shiftedCopy, SNR)

    if wedgeInfo:
        #print '---WEDGE---'
        result = wedgeInfo.apply(noisyCopy)
    else:
        result = noisyCopy

    #print 'EMSimulation simpleSimulation: end function'

    if result.__class__ == list:
        return result[0]
    else:
        return result
Ejemplo n.º 7
0
def calculateCorrelationVector(particle,
                               particleList,
                               mask,
                               particleIndex,
                               applyWedge=True,
                               binningFactor=0,
                               lowestFrequency=0,
                               highestFrequency=1):
    """
    calculateCorrelationVector: 
    @param particle: the current particle
    @param particleList: all the other particles
    @param mask: corellation under a mask
    @param applyWedge: Apply constrained correlation? True by default. Set to false for rotation classification for instance.
    @param binningFactor: Binning of data when read. Default is 0 (larger values according to libtomc notation)   
    @param lowestFrequency: Lowest frequency for bandpass
    @param highestFrequency: Highest frequency for bandpass
    """
    from pytom.basic.structures import Particle, ParticleList, Mask
    from pytom.cluster.correlationMatrixStructures import CorrelationVector
    from pytom_volume import vol, rotate, shift
    from pytom.basic.filter import bandpassFilter, filter

    assert particle.__class__ == Particle
    assert particleList.__class__ == ParticleList
    assert mask.__class__ == Mask or mask.__class__ == str

    from pytom.score.score import FLCFScore
    from pytom.tools.memory import read as readBuffer
    from pytom_volume import read

    if mask.__class__ == str:
        mask = read(mask, 0, 0, 0, 0, 0, 0, 0, 0, 0, int(binningFactor),
                    int(binningFactor), int(binningFactor))
    else:
        mask = read(mask.getFilename(), 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    int(binningFactor), int(binningFactor), int(binningFactor))

    #sc = nxcfScore()
    sc = FLCFScore()

    correlationVector = CorrelationVector(particle, particleList,
                                          particleIndex)
    particleRotation = particle.getRotation()
    particleShift = particle.getShift()

    particleShifted = None
    otherParticleShifted = None

    particleRotated = None
    otherParticleRotated = None

    bandpassFilterObject = None

    for particleIndex in range(len(particleList)):

        otherParticle = particleList[particleIndex]

        #read from disk

        particleVolume = read(particle.getFilename(), 0, 0, 0, 0, 0, 0, 0, 0,
                              0, int(binningFactor), int(binningFactor),
                              int(binningFactor))

        #otherParticleVolume = read(otherParticle.getFilename(),binningX=binningFactor,binningY=binningFactor,binningZ=binningFactor)
        otherParticleVolume = read(otherParticle.getFilename(), 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, int(binningFactor),
                                   int(binningFactor), int(binningFactor))

        #initialise memory for buffer volumes
        if not particleRotated:
            particleRotated = vol(particleVolume.sizeX(),
                                  particleVolume.sizeY(),
                                  particleVolume.sizeZ())
            otherParticleRotated = vol(particleVolume.sizeX(),
                                       particleVolume.sizeY(),
                                       particleVolume.sizeZ())
            particleShifted = vol(particleVolume.sizeX(),
                                  particleVolume.sizeY(),
                                  particleVolume.sizeZ())
            otherParticleShifted = vol(particleVolume.sizeX(),
                                       particleVolume.sizeY(),
                                       particleVolume.sizeZ())

        #get settings
        rotationOtherParticle = otherParticle.getRotation()
        otherParticleShift = otherParticle.getShift()

        #apply shift determined
        if particleShift.getX() != 0 or particleShift.getY(
        ) != 0 or particleShift.getZ() != 0:
            shift(particleVolume, particleShifted, -particleShift[0],
                  -particleShift[1], -particleShift[2])
        else:
            particleShifted = particleVolume

        if otherParticleShift.getX() != 0 or otherParticleShift.getY(
        ) != 0 or otherParticleShift.getZ() != 0:
            shift(otherParticleVolume, otherParticleShifted,
                  -otherParticleShift[0], -otherParticleShift[1],
                  -otherParticleShift[2])
        else:
            otherParticleShifted = otherParticleVolume

        #apply rotation determined
        if particleRotation.getZ1() != 0 or particleRotation.getX(
        ) != 0 or particleRotation.getZ2() != 0:
            rotate(otherParticleShifted, otherParticleRotated,
                   -particleRotation.getZ2(), -particleRotation.getZ1(),
                   -particleRotation.getX())
            otherParticleRotated = otherParticleRotated * mask
        else:
            otherParticleRotated = otherParticleVolume

        if rotationOtherParticle.getZ1() != 0 or rotationOtherParticle.getX(
        ) != 0 or rotationOtherParticle.getZ2() != 0:
            rotate(particleShifted, particleRotated,
                   -rotationOtherParticle.getZ2(),
                   -rotationOtherParticle.getZ1(),
                   -rotationOtherParticle.getX())
            particleRotated = particleRotated * mask
        else:
            particleRotated = particleVolume

        applyWedge = particleRotation.getZ1() != 0 or particleRotation.getX(
        ) != 0 or particleRotation.getZ2() != 0
        applyWedge = applyWedge or rotationOtherParticle.getZ1(
        ) != 0 or rotationOtherParticle.getX(
        ) != 0 or rotationOtherParticle.getZ2() != 0

        #apply cross wedge
        if applyWedge:

            particleWedge = particle.getWedge()
            otherParticleWedge = otherParticle.getWedge()

            particleWedge.setRotation(particleRotation)
            otherParticleWedge.setRotation(rotationOtherParticle)

            particleVolume = otherParticleWedge.apply(particleRotated)
            otherParticleVolume = particleWedge.apply(otherParticleRotated)

        #apply bandpass
        if 0 <= lowestFrequency < 1 and 0 < highestFrequency <= 1:
            if not bandpassFilterObject:
                bandpassString = str(lowestFrequency) + ':' + str(
                    highestFrequency) + ';'

                r = bandpassFilter(particleVolume, bandpassString)

                particleVolume = r[0]
                bandpassFilterObject = r[1]
                #print 'calculateCorrelationVector : Init bandpass'
            else:
                r = list(filter(particleVolume, bandpassFilterObject))
                particleVolume = r[0]

                #print 'calculateCorrelationVector : existing bandpass'

            r = list(filter(otherParticleVolume, bandpassFilterObject))
            otherParticleVolume = r[0]

        #particleVolume.write('p.em')
        #otherParticleVolume.write('op.em')
        #assert False

        #apply scoring here
        value = sc.scoringCoefficient(otherParticleVolume, particleVolume,
                                      mask)

        if value != value:
            print(
                'Error during calculation of correlationMatrix! Check files written to disk (error_part.em,error_otherPart.em). ABORTING!'
            )
            print(particle.getFilename(), otherParticle.getFilename())
            particleVolume.write('error_part.em')
            otherParticleVolume.write('error_otherPart.em')

            assert False

        correlationVector.append(value)

    return correlationVector
Ejemplo n.º 8
0
    def apply(self, volume, bypassFlag=False, downscale=1, particle=None):
        """
        apply: Performs preprocessing of volume and reference
        @param volume: volume to be pre-processed
        @type volume: L{pytom_volume.vol}
        @param bypassFlag: Set if only bandpassFilter needed. False otherwise and all routines will be processed.
        @param downscale: not used anymore
        @param particle: particle Volume to be subtracted from input volume
        @type particle: L{pytom_volume.vol}
        @return: Returns modified volume
        @author: Thomas Hrabe  
        """

        from pytom_volume import vol

        if self._bandpassOn:

            from pytom.basic.filter import bandpassFilter

            # if frequencies specified in Nyquist, 0.5 being highest
            # fixed wrong adjustment of frequencies upon binning - FF
            if self._highestFrequency < 1:
                highestFrequency = self._highestFrequency * volume.sizeX()
                lowestFrequency = self._lowestFrequency * volume.sizeX()
            else:
                highestFrequency = self._highestFrequency
                lowestFrequency = self._lowestFrequency

            v = bandpassFilter(volume=volume,
                               lowestFrequency=lowestFrequency,
                               highestFrequency=highestFrequency,
                               bpf=0,
                               smooth=self._bandpassSmooth)
            volume = v[0]

        if self._prerotateOn and (not bypassFlag):

            from pytom_volume import rotate

            rot = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())
            rotation = self.prerotate
            rotate(volume, rot, rotation[0], rotation[1], rotation[2])
            volume = rot

        if self._weightingOn and (not bypassFlag):

            from pytom_volume import read
            from pytom_freqweight import weight
            from pytom.basic.fourier import fft, ifft

            wedgeSum = read(self._weightingFile)

            fVolume = fft(volume)
            weighting = weight(wedgeSum)
            weighting.apply(fVolume)
            volume = ifft(fVolume)

        if self._substractParticle and particle.__class__ == vol:
            volume -= particle

        if self._taper > 0:
            from pytom.tools.macros import volumesSameSize
            if self._taperMask is None or not volumesSameSize(
                    volume, self._taperMask):
                from pytom.basic.functions import taper_edges
                volume, self._taperMask = taper_edges(volume, self._taper)
            else:
                volume = volume * self._taperMask

        return volume