Ejemplo n.º 1
0
def bandCF(volume, reference, band=[0, 100]):
    """
    bandCF:
    @param volume: The volume
    @param reference: The reference
    @param band: [a,b] - specify the lower and upper end of band. [0,1] if not set.
    @return: First parameter - The correlation of the two volumes in the specified ring. 
             Second parameter - The bandpass filter used.
    @rtype: List - [L{pytom_volume.vol},L{pytom_freqweight.weight}]
    @author: Thomas Hrabe   
    @todo: does not work yet -> test is disabled
    """

    if gpu:
        import cupy as xp
    else:
        import numpy as xp

    import pytom_volume
    from math import sqrt
    from pytom.basic import fourier
    from pytom.basic.filter import bandpassFilter
    from pytom.basic.correlation import nXcf

    vf = bandpassFilter(volume, band[0], band[1], fourierOnly=True)
    rf = bandpassFilter(reference, band[0], band[1], vf[1], fourierOnly=True)

    v = pytom_volume.reducedToFull(vf[0])
    r = pytom_volume.reducedToFull(rf[0])

    absV = pytom_volume.abs(v)
    absR = pytom_volume.abs(r)

    pytom_volume.power(absV, 2)
    pytom_volume.power(absR, 2)

    sumV = abs(pytom_volume.sum(absV))
    sumR = abs(pytom_volume.sum(absR))

    if sumV == 0:
        sumV = 1

    if sumR == 0:
        sumR = 1

    pytom_volume.conjugate(rf[0])

    fresult = vf[0] * rf[0]

    #transform back to real space
    result = fourier.ifft(fresult)

    fourier.iftshift(result)

    result.shiftscale(0, 1 / float(sqrt(sumV * sumR)))

    return [result, vf[1]]
Ejemplo n.º 2
0
    def bandpass(self, lowfreq, hifreq, smooth=0., bpf=None):
        """
        bandpass filter image

        @param lowfreq: lowest frequency of filter (=hi-pass)
        @type lowfreq: L{float}
        @param hifreq: highest frequency of filter (=low-pass)
        @type hifreq: L{float}
        @param smooth: smoothing
        @type smooth: L{float}
        @param bpf: bandpass filter object

        @return: bandpass filter object
        @rtype: L{pytom.basic.structures.BandPassFilter}
        """
        from pytom.basic.filter import bandpassFilter

        res = bandpassFilter(volume=self.data,
                             lowestFrequency=lowfreq,
                             highestFrequency=hifreq,
                             bpf=None,
                             smooth=smooth,
                             fourierOnly=False)
        self.data = res[0]
        bpf = res[1]
        return bpf
Ejemplo n.º 3
0
    def bandpass_in_nyquist(self, lowfreq, hifreq, smooth=0., bpf=None):
        """
        bandpass filter image with frequencies specified in Nyquist

        @param lowfreq: lowest frequency of filter in Nyquist (=hi-pass)
        @param hifreq: highest frequency of filter in Nyquist (=low-pass)
        @param smooth: smoothing in Nyquist
        @param bpf: bandpass filter object

        @return: bandpass filter object
        @rtype: L{pytom.basic.structures.BandPassFilter}
        """
        from pytom.basic.filter import bandpassFilter

        res = bandpassFilter(volume=self.data,
                             lowestFrequency=lowfreq * self.dims[0] / 2.,
                             highestFrequency=hifreq * self.dims[0] / 2.,
                             bpf=None,
                             smooth=smooth * self.dims[0] / 2.,
                             fourierOnly=False)
        self.data = res[0]
        bpf = res[1]
        return bpf
Ejemplo n.º 4
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.º 5
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
Ejemplo n.º 6
0
def bandCC(volume,reference,band,verbose = False):
    """
    bandCC: Determines the normalised correlation coefficient within a band
    @param volume: The volume
    @type volume: L{pytom_volume.vol}
    @param reference: The reference
    @type reference: L{pytom_volume.vol}
    @param band: [a,b] - specify the lower and upper end of band.
    @return: First parameter - The correlation of the two volumes in the specified band. 
             Second parameter - The bandpass filter used.
    @rtype: List - [float,L{pytom_freqweight.weight}]
    @author: Thomas Hrabe    
    """
    import pytom_volume
    from pytom.basic.filter import bandpassFilter
    from pytom.basic.correlation import xcf
    from math import sqrt
    
    if verbose:
        print('lowest freq : ', band[0],' highest freq' , band[1])
        
    vf = bandpassFilter(volume,band[0],band[1],fourierOnly=True)
    rf = bandpassFilter(reference,band[0],band[1],vf[1],fourierOnly=True)
    
    ccVolume = pytom_volume.vol_comp(rf[0].sizeX(),rf[0].sizeY(),rf[0].sizeZ())
    ccVolume.copyVolume(rf[0])
    
    pytom_volume.conj_mult(ccVolume,vf[0])
    
    cc = pytom_volume.sum(ccVolume)

    cc = cc.real
    
    v = vf[0]
    r = rf[0]
    
    absV = pytom_volume.abs(v)
    absR = pytom_volume.abs(r)
    
    pytom_volume.power(absV,2)
    pytom_volume.power(absR,2)
    
    sumV = pytom_volume.sum(absV)
    sumR = pytom_volume.sum(absR)
    
    sumV = abs(sumV)
    sumR = abs(sumR)
    
    if sumV == 0:
        sumV =1
        
    if sumR == 0:
        sumR =1
        
    cc = cc / (sqrt(sumV*sumR)) 
    
    #numerical errors will be punished with nan
    if abs(cc) > 1.1 :
        cc = float('nan')
    
    return [cc,vf[1]];
Ejemplo n.º 7
0
    if help is True:
        print(helper)
        sys.exit()

    if not filename or not target:
        print(helper)
        sys.exit()

    v = read(filename)

    if lowestFrequency:
        lowestFrequency = int(lowestFrequency)
    else:
        lowestFrequency = 0

    if highestFrequency:
        highestFrequency = int(highestFrequency)
    else:
        highestFrequency = v.sizeX() / 2

    if smooth:
        smooth = int(smooth)
    else:
        smooth = 0

    r = bandpassFilter(
        v,
        str(lowestFrequency) + ':' + str(highestFrequency) + ';', None, smooth)

    r[0].write(target)