Example #1
0
    def longAxisOrthDist(self):
        from PYME.Analysis.binAvg import binAvg

        xvs = np.linspace(-1, 1, self.nsteps)
        bms = []

        mc = self.chans[self.masterChan]

        for c in self.chans:
            bn, bm, bs = binAvg(mc.A0 / mc.geom_length, c.data, xvs)
            bss = bm * bn
            bn, bm, bs = binAvg(mc.A0 / mc.geom_length, c.data * mc.SA, xvs)
            bb = bm * bn
            bms.append(bb / bss)

        return xvs, bms
Example #2
0
def imageDensityAtDistance(A, mask, voxelsize = None, bins=100):
    '''Calculates the distribution of a label at varying distances from a mask.
    Negative distances are on the inside of the mask.

    Parameters:
    A - intensity image
    mask - binary mask
    voxelsize - size of the pixels/voxels - should be either a constant, or an iterable
                with a length equal to the number of dimensions in the data
    bins - either a number of bins, or an array of bin edges


    Returns:
    bn - integrated intensity in distance bin
    bm - mean intensity in distance bin
    bins - the bin edges
    '''
    
    if voxelsize == None:
        voxelsize = numpy.ones(len(A.shape))

    dt = -ndimage.distance_transform_edt(mask, sampling=voxelsize)

    dt = dt + ndimage.distance_transform_edt(1- ndimage.binary_dilation(mask), sampling=voxelsize)

    if numpy.isscalar(bins):
        bins = numpy.linspace(dt.min(), dt.max(), bins+1)
        
    #print bins

    bn, bm, bs = binAvg.binAvg(dt, A, bins)

    return bn, bm, bins
    def OnColoc(self, event):
        from PYME.Analysis import binAvg        
        voxelsize = self.image.voxelsize

        #assume we have exactly 2 channels #FIXME - add a selector
        #grab image data
        imA = self.image.data[:,:,:,0].squeeze()
        imB = self.image.data[:,:,:,1].squeeze()
        
        X, Y = np.mgrid[0:float(imA.shape[0]), 0:float(imA.shape[1])]
        X = X/X.shape[0]
        Y = Y/X.shape[1]
        X = X - .5
        Y = Y - .5
        R = np.sqrt(X**2 + Y**2)

        H1 = pylab.fftn(imA)
        H2 = pylab.fftn(imB)
        
        rB = np.linspace(0,R.max())
        
        bn, bm, bs = binAvg.binAvg(R, pylab.fftshift(abs(H1*H2)), rB)
        
        bn1, bm1, bs1 = binAvg.binAvg(R, pylab.fftshift(abs(H1*H1)), rB)
        bn2, bm2, bs2 = binAvg.binAvg(R, pylab.fftshift(abs(H2*H2)), rB)
       
       
        

        pylab.figure()
        
        ax = pylab.gca()
        
        ax.plot(rB[:-1], bm/np.sqrt(bm1*bm2))
        ax.plot(rB[:-1], 2./np.sqrt(bn/2))
        
        xt = np.array([10., 15, 20, 30, 50, 80, 100, 150])
        rt = voxelsize[0]/xt
        
        pylab.xticks(rt[::-1],['%d' % xi for xi in xt[::-1]])
        
        
        pylab.show()
Example #4
0
    def angularDist(self):
        from PYME.Analysis.binAvg import binAvg

        xvs = np.linspace(-np.pi, np.pi, self.nsteps)
        bms = []

        mc = self.chans[self.masterChan]

        for c in self.chans:
            bn, bm, bs = binAvg(mc.Theta, c.data, xvs)
            bb = bm * bn
            bms.append(bb / bb.sum())

        return xvs, bms
Example #5
0
    def shortAxisDist(self):
        from PYME.Analysis.binAvg import binAvg

        xvs = np.linspace(-500, 500, self.nsteps)
        bms = []

        mc = self.chans[self.masterChan]

        for c in self.chans:
            bn, bm, bs = binAvg(mc.SA, c.data, xvs)
            bb = bm * bn
            bms.append(bb / bb.sum())

        return xvs, bms
Example #6
0
    def RadialDistN(self):
        from PYME.Analysis.binAvg import binAvg

        xvs = np.linspace(0, 1, self.nsteps)
        bms = []

        mc = self.chans[self.masterChan]
        R = mc.R / (mc.geom_length / 2)

        for c in self.chans:
            bn, bm, bs = binAvg(R, c.data, xvs)
            bb = bm * bn
            bms.append(bb / bb.sum())

        return xvs, bms
Example #7
0
    def radialDistN(self):
        from PYME.Analysis.binAvg import binAvg

        xvs = np.linspace(0, 2, self.nsteps)
        bms = []

        mc = self.chans[self.masterChan]
        r = mc.r / (mc.geom_length / 2)

        for c in self.chans:
            bn, bm, bs = binAvg(r, c.data, xvs)
            bb = bm * bn
            bms.append(bb / bb.sum())

        return xvs, bms
    def OnColoc(self, event):
        import matplotlib.pyplot as plt
        from PYME.Analysis import binAvg        
        voxelsize = self.image.voxelsize

        #assume we have exactly 2 channels #FIXME - add a selector
        #grab image data
        imA = self.image.data[:,:,:,0].squeeze()
        imB = self.image.data[:,:,:,1].squeeze()
        
        X, Y = np.mgrid[0:float(imA.shape[0]), 0:float(imA.shape[1])]
        X = X/X.shape[0]
        Y = Y/X.shape[1]
        X = (X - .5)
        Y = Y - .5
        R = np.sqrt(X**2 + Y**2)

        H1 = np.fft.fftn(imA)
        H2 = np.fft.fftn(imB)
        
        #rB = np.linspace(0,R.max())
        rB = np.linspace(0, 0.5, 100)
    
        bn, bm, bs = binAvg.binAvg(R, np.fft.fftshift(H1*H2.conjugate()).real, rB)
        
        bn1, bm1, bs1 = binAvg.binAvg(R, np.fft.fftshift((H1*H1.conjugate()).real), rB)
        bn2, bm2, bs2 = binAvg.binAvg(R, np.fft.fftshift((H2*H2.conjugate()).real), rB)
       

        plt.figure()
        
        ax = plt.gca()
        
        #FRC
        FRC = bm/np.sqrt(bm1*bm2)
        ax.plot(rB[:-1], FRC)
        
        #noise envelope???????????
        ax.plot(rB[:-1], 2./np.sqrt(bn/2), ':')
        
        dfrc = np.diff(FRC)
        monotone = np.where(dfrc > 0)[0][0] + 1
        
        #print FRC[:monotone], FRC[:(monotone+1)]
        
        intercept = np.interp(1.0 - 1/7.0, 1-FRC[:monotone], rB[:monotone])
        
        print('Intercept= %3.2f (%3.2f nm)' % (intercept, voxelsize[0]/intercept))
        
        xt = np.array([10.,  15, 20, 30, 40, 50, 70, 90, 120, 150, 200, 300, 500])
        rt = voxelsize[0]/xt
        
        plt.xticks(rt[::-1],['%d' % xi for xi in xt[::-1]], rotation='vertical')

        ax.plot([0, rt[0]], np.ones(2)/7.0)
        
        plt.grid()
        plt.xlabel('Resolution [nm]')
        
        plt.ylabel('FRC')
        
        plt.figtext(0.5, 0.5, 'FRC intercept at %3.1f nm' % (voxelsize[0]/intercept))
        
        
        plt.show()
def fourier_ring_correlation(imA, imB, voxelsize=[1.0, 1.0], window=False):
    import matplotlib.pyplot as plt
    from PYME.Analysis import binAvg

    imA = imA.squeeze()
    imB = imB.squeeze()

    X, Y = np.mgrid[0:float(imA.shape[0]), 0:float(imA.shape[1])]
    X = X / X.shape[0]
    Y = Y / X.shape[1]
    X = (X - .5)
    Y = Y - .5
    R = np.sqrt(X**2 + Y**2)

    if window:
        W = np.hanning(X.shape[0])[:, None] * np.hanning(X.shape[1])[None, :]
        imA = imA * W
        imB = imB * W

    H1 = np.fft.fftn(imA)
    H2 = np.fft.fftn(imB)

    #rB = np.linspace(0,R.max())
    rB = np.linspace(0, 0.5, 100)

    bn, bm, bs = binAvg.binAvg(R,
                               np.fft.fftshift(H1 * H2.conjugate()).real, rB)

    bn1, bm1, bs1 = binAvg.binAvg(R, np.fft.fftshift(
        (H1 * H1.conjugate()).real), rB)
    bn2, bm2, bs2 = binAvg.binAvg(R, np.fft.fftshift(
        (H2 * H2.conjugate()).real), rB)

    plt.figure()

    ax = plt.gca()

    #FRC
    FRC = bm / np.sqrt(bm1 * bm2)
    ax.plot(rB[:-1], FRC)

    #noise envelope???????????
    ax.plot(rB[:-1], 2. / np.sqrt(bn / 2), ':')

    dfrc = np.diff(FRC)
    #print FRC
    monotone = np.where(dfrc > 0)[0][0] + 1

    #print FRC[:monotone], FRC[:(monotone+1)]

    intercept_m = np.interp(1.0 - 1 / 7.0, 1 - FRC[:monotone], rB[:monotone])

    print('Intercept_m= %3.2f (%3.2f nm)' %
          (intercept_m, voxelsize[0] / intercept_m))

    from scipy import ndimage
    f_s = np.sign(FRC - 1. / 7.)

    fss = ndimage.gaussian_filter(f_s, 10, mode='nearest')

    intercept = np.interp(0.0, -fss, rB[:-1])

    print('Intercept= %3.2f (%3.2f nm)' %
          (intercept, voxelsize[0] / intercept))

    xt = np.array([10., 15, 20, 30, 40, 50, 70, 90, 120, 150, 200, 300, 500])
    rt = voxelsize[0] / xt

    plt.xticks(rt[::-1], ['%d' % xi for xi in xt[::-1]], rotation='vertical')

    ax.plot([0, rt[0]], np.ones(2) / 7.0)

    plt.grid()
    plt.xlabel('Resolution [nm]')

    plt.ylabel('FRC')
    plt.ylim(0, 1.1)

    plt.plot([intercept, intercept], [0, 1], '--')

    plt.figtext(0.5, 0.5,
                'FRC intercept at %3.1f nm' % (voxelsize[0] / intercept))

    plt.figure()
    plt.plot(rB[:-1], f_s)
    plt.plot(rB[:-1], fss)
    plt.show()