Exemple #1
0
def add(volume, SNR=1):
    """
    add Adds white noise to volume
    @param volume: A volume
    @param SNR: Signal to Noise ratio of result
    @type SNR: int or float > 0 
    @return: Volume containing noise with SNR == SNR
    @author: Thomas Hrabe  
    """

    if (SNR < 0):
        return volume

    from math import sqrt
    from pytom_volume import vol, mean, variance, gaussianNoise

    m = mean(volume)
    s = sqrt(variance(volume, False) / SNR)  # SNR = Var(signal) / Var(noise)

    #    s = sqrt(variance(volume,False)/SNR)-variance(volume,False)
    #
    #    if s<0:
    #        return volume
    #    elif s==0:
    #        s =1

    noise = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ())

    gaussianNoise(noise, m, s)  # s is actually the std

    result = volume + noise

    return result
    def test_resize2D(self):
        """
        test re-sizing in Fourier space
        """
        from pytom.basic.transformations import resize
        from pytom_volume import vol
        from pytom.basic.fourier import fft

        dim = 32
        px = 11
        py = 19
        scf = dim * dim
        myVol = vol(dim, dim, 1)
        myVol.setAll(0.)
        myVol.setV(1., px, py, 0)
        #fmyVol = fft(myVol)
        (resizeVol, resizefVol) = resize(volume=myVol,
                                         factor=2.,
                                         interpolation='Fourier')
        resizeVol.write('test1.em')
        ftresizeVol = fft(data=resizeVol)
        for ix in range(resizefVol.sizeX()):
            for iy in range(resizefVol.sizeY()):
                diff = ftresizeVol.getV(
                    ix, iy, 0) - scf * 4 * resizefVol.getV(ix, iy, 0)
                self.assertTrue(expr=abs(diff) < .05,
                                msg="inconsistency FFT/IFFT for magnification")
        (resizeVol, resizefVol) = resize(volume=resizeVol,
                                         factor=.5,
                                         interpolation='Fourier')
        from pytom_volume import variance
        diff = myVol - resizeVol
        self.assertTrue(expr=variance(diff, False) < .0000001,
                        msg="2D image before and after rescales differs")
Exemple #3
0
def mean0std1(volume, copyFlag=False):
    """
    mean0std1: normalises input volume to mean 0 and std 1. Procedure is performed inplace if copyFlag is unspecified!!!
    @param volume: Data containing either an image or a volume
    @param copyFlag: If True a copy of volume will be returned. False unless specified otherwise. 
    @return: If copyFlag == True, then return a normalised copy.
    @author: Thomas Hrabe
    """
    import pytom_volume
    from math import sqrt
    from pytom.tools.maths import epsilon

    if volume.__class__ == pytom_volume.vol_comp:
        from pytom.basic.fourier import ifft, iftshift
        volume = iftshift(ifft(volume))

    if not copyFlag:
        volumeMean = pytom_volume.mean(volume)
        volume.shiftscale(-volumeMean, 1)
        volumeStd = sqrt(pytom_volume.variance(volume, False))

        if volumeStd < epsilon:
            raise ValueError(
                'pytom_normalise.mean0std1 : The standard deviation is too low for division! '
                + str(volumeStd))

        volume.shiftscale(0, 1. / float(volumeStd))
    else:
        volumeCopy = pytom_volume.vol(volume.sizeX(), volume.sizeY(),
                                      volume.sizeZ())
        volumeCopy.copyVolume(volume)
        volumeMean = pytom_volume.mean(volumeCopy)
        volumeCopy.shiftscale(-1. * volumeMean, 1)
        volumeStd = sqrt(pytom_volume.variance(volumeCopy, False))

        if volumeStd < epsilon:
            raise ValueError(
                'pytom_normalise.mean0std1 : The standard deviation is too low for division! '
                + str(volumeStd))

        #volumeCopy.shiftscale(-1.*volumeMean,1)
        volumeCopy.shiftscale(0, 1 / float(volumeStd))

        return volumeCopy
Exemple #4
0
    def mean0std1_Test(self):

        import pytom_volume
        from pytom.basic.normalise import mean0std1
        v = pytom_volume.read('./testData/ribo.em')

        m = mean0std1(v, True)

        me = pytom_volume.mean(m)
        var = pytom_volume.variance(m, False)

        assert epsilon >= me >= -epsilon  #mean value test
        assert 1 + epsilon >= var >= 1 - epsilon
Exemple #5
0
def rescale_to_avg_std(volume1, nuavg, nustd):
    avg = mean(volume1)
    std = np.sqrt(variance(volume1, False))

    if (std > 1e-30):
        scale = nustd/std
    else: 
        scale = 1
    shift = nuavg - avg*scale
    
    x = volume1.sizeX()
    y = volume1.sizeY()
    z = volume1.sizeZ()
    
    for zz in xrange(0,z):
        for yy in xrange(0,y):
            for xx in xrange(0,x):
                val = volume1(xx,yy,zz)*scale + shift
                volume1.setV(round(val,6),xx,yy,zz) 
    
    return volume1
    def test_resize3D(self):
        """
        test 3D re-sizing in Fourier space
        """
        from pytom.basic.transformations import resize
        from pytom_volume import vol

        dim = 32
        px = 11
        py = 19
        pz = 21
        myVol = vol(dim, dim, dim)
        myVol.setAll(0.)
        myVol.setV(1., px, py, pz)
        (resizeVol, resizefVol) = resize(volume=myVol,
                                         factor=2.,
                                         interpolation='Fourier')
        (resizeVol, resizefVol) = resize(volume=resizeVol,
                                         factor=.5,
                                         interpolation='Fourier')
        from pytom_volume import variance
        diff = myVol - resizeVol
        self.assertTrue(expr=variance(diff, False) < .0000001,
                        msg="2D image before and after rescales differs")
Exemple #7
0
    window_size = L
    increment= 4 
    step = increment 
    numberBands = int(window_size/8)  
    sizeX = sizeY = sizeZ = window_size


    conf = SparkConf().setAppName("parallel local FSC").setMaster("local[*]") 
    sc = SparkContext(conf=conf)
    vol1File = "./emd_3802_half_map_1.map"
    vol2File = "./emd_3802_half_map_2.map"
    hm1 = read(vol1File) 
    hm2 = read(vol2File) 

    avg1 = mean(hm1)
    std1 = np.sqrt(variance(hm1, False))
    hm20 = rescale_to_avg_std(hm2, avg1, std1)

    
    nz = hm1.sizeZ()
    ny = hm1.sizeY()
    nx = hm1.sizeX()

    mask_level = -1
    vz = vy = vx = window_size/2 
    pmask = vol(nx,ny,nz)
    pmask.setAll(1)
    
    # set up the mask for which voxels to calculate
    total_num = 0 
    for zz in xrange(0,nz):
Exemple #8
0
def _dspcub(volume, sigma=None, projectionAxis='z'):
    """
    _dspcub: Creates a dspcub image
    dspcub2PNG: display z-slices in 2D image
    @param volume: The input volume
    @type volume: L{pytom_volume.vol}
    @parameter sigma: thresholding as multiples of standard deviation sigma
    @type sigma: float
    @param projectionAxis: Defines the axis. Default is z, means you look on the xy plane. (x equals to 'yz' plane, y to 'xz') 
    @return: Image
    @rtype: Image
    @@author: Pia Unverdorben 
    """

    if volume.sizeZ() == 1:
        raise Exception('You can use ')

    import Image
    from pytom_volume import min, max, mean, variance, limit, subvolume
    from math import sqrt, ceil, floor

    sizeX = volume.sizeX()
    sizeY = volume.sizeY()
    sizeZ = volume.sizeZ()

    if projectionAxis == 'x':
        imagesPerRow = int(floor(sqrt(sizeX)))
        size1 = float(sizeX)
        sizeI = sizeY
        sizeJ = sizeZ
    elif projectionAxis == 'y':
        imagesPerRow = int(floor(sqrt(sizeY)))
        size1 = float(sizeY)
        sizeI = sizeX
        sizeJ = sizeZ
    elif projectionAxis == 'z':
        imagesPerRow = int(floor(sqrt(sizeZ)))
        size1 = float(sizeZ)
        sizeI = sizeX
        sizeJ = sizeY

    numberIterations = imagesPerRow * imagesPerRow

    if size1 < numberIterations:
        iterationSteps = float(numberIterations / size1)
    else:
        iterationSteps = float(size1 / numberIterations)

    iterationSteps = int(iterationSteps)

    if projectionAxis == 'x':
        img = Image.new('L', (sizeY * imagesPerRow, sizeZ * imagesPerRow))
    elif projectionAxis == 'y':
        img = Image.new('L', (sizeX * imagesPerRow, sizeZ * imagesPerRow))
    elif projectionAxis == 'z':
        img = Image.new('L', (sizeX * imagesPerRow, sizeY * imagesPerRow))

    # normalize according to standard deviation if sigma is specified
    if sigma:
        meanv = mean(volume)
        stdv = sqrt(variance(volume, False))
        minValue = meanv - float(sigma) * stdv
        maxValue = meanv + float(sigma) * stdv
    else:
        minValue = min(volume)
        maxValue = max(volume)

    for sliceNumber in range(0, numberIterations, iterationSteps):

        if projectionAxis == 'x':
            png = Image.new('L', (sizeY, sizeZ))
        elif projectionAxis == 'y':
            png = Image.new('L', (sizeX, sizeZ))
        elif projectionAxis == 'z':
            png = Image.new('L', (sizeX, sizeY))

        (yvalue, xvalue) = divmod(sliceNumber, imagesPerRow)

        if projectionAxis == 'x':
            vol = subvolume(volume, sliceNumber, 0, 0, 1, sizeY, sizeZ)
        elif projectionAxis == 'y':
            vol = subvolume(volume, 0, sliceNumber, 0, sizeX, 1, sizeZ)
        elif projectionAxis == 'z':
            vol = subvolume(volume, 0, 0, sliceNumber, sizeX, sizeY, 1)

        vol.shiftscale(-minValue, 1)
        vol.shiftscale(0, 255 / maxValue)

        for i in range(sizeI):
            for j in range(sizeJ):

                if projectionAxis == 'x':
                    value = vol(0, i, j)
                elif projectionAxis == 'y':
                    value = vol(i, 0, j)
                elif projectionAxis == 'z':
                    value = vol(i, j, 0)

                png.putpixel((i, j), int(value))

        img.paste(png, (xvalue * sizeI, yvalue * sizeJ))

    return img