def setModel(modName, md):
    global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

    if not modName == interpModelName:
        mf = open(getFullExistingFilename(modName), 'rb')
        mod, voxelsize = cPickle.load(mf)
        mf.close()
        
        interpModelName = modName

        if not voxelsize.x == md.voxelsize.x:
            raise RuntimeError("PSF and Image voxel sizes don't match")

        IntXVals = 1e3*voxelsize.x*scipy.mgrid[-(mod.shape[0]/2.):(mod.shape[0]/2.)]
        IntYVals = 1e3*voxelsize.y*scipy.mgrid[-(mod.shape[1]/2.):(mod.shape[1]/2.)]
        IntZVals = 1e3*voxelsize.z*scipy.mgrid[-(mod.shape[2]/2.):(mod.shape[2]/2.)]

        dx = voxelsize.x*1e3
        dy = voxelsize.y*1e3
        dz = voxelsize.z*1e3

        interpModel = mod

        interpModel = interpModel/interpModel.max() #normalise to 1

        twist.twistCal(interpModel, IntXVals, IntYVals, IntZVals)
Beispiel #2
0
def preparePSF(md, PSSize):
    global PSFFileName, cachedPSF, cachedOTF2, cachedOTFH, autocorr
    
    PSFFilename = md.PSFFile
                
    if (not (PSFFileName == PSFFilename)) or (not (cachedPSF.shape == PSSize)):
        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()
            
        ps = ps.max(2)
        ps = ps - ps.min()
        #ps = ps*(ps > 0)
        ps = ps*scipy.signal.hanning(ps.shape[0])[:,None]*scipy.signal.hanning(ps.shape[1])[None,:]
        ps = ps/ps.sum()
        PSFFileName = PSFFilename
        pw = (numpy.array(PSSize) - ps.shape)/2.
        pw1 = numpy.floor(pw)
        pw2 = numpy.ceil(pw)
        cachedPSF = pad.with_constant(ps, ((pw2[0], pw1[0]), (pw2[1], pw1[1])), (0,))
        cachedOTFH = ifftn(cachedPSF)*cachedPSF.size
        cachedOTF2 = cachedOTFH*fftn(cachedPSF)
Beispiel #3
0
def setModel(modName, md):
    global IntXVals, IntYVals, IntZVals, interpModel, dx, dy, dz

    
    mf = open(getFullExistingFilename(modName), 'rb')
    mod, voxelsize = pickle.load(mf)
    mf.close()
    
    mod = resizePSF(mod, interpModel.shape)

    #if not voxelsize.x == md.voxelsize.x:
    #    raise RuntimeError("PSF and Image voxel sizes don't match")

    IntXVals = 1e3*voxelsize.x*mgrid[-(mod.shape[0]/2.):(mod.shape[0]/2.)]
    IntYVals = 1e3*voxelsize.y*mgrid[-(mod.shape[1]/2.):(mod.shape[1]/2.)]
    IntZVals = 1e3*voxelsize.z*mgrid[-(mod.shape[2]/2.):(mod.shape[2]/2.)]

    dx = voxelsize.x*1e3
    dy = voxelsize.y*1e3
    dz = voxelsize.z*1e3

    #interpModel = mod

    #interpModel = np.maximum(mod/mod.max(), 0) #normalise to 1
    interpModel = np.maximum(mod/mod[:,:,len(IntZVals)/2].sum(), 0) #normalise to 1 and clip
def setModel(modName, md):
    global IntXVals, IntYVals, IntZVals, interpModel, interpModelF, interpModelName, dx, dy, dz, kx, ky, kz

    if not modName == interpModelName:
        mf = open(getFullExistingFilename(modName), 'rb')
        mod, voxelsize = cPickle.load(mf)
        mf.close()

        interpModelName = modName

        if not voxelsize.x == md.voxelsize.x:
            raise RuntimeError("PSF and Image voxel sizes don't match")

        IntXVals = 1e3*voxelsize.x*mgrid[-(mod.shape[0]/2.):(mod.shape[0]/2.)]
        IntYVals = 1e3*voxelsize.y*mgrid[-(mod.shape[1]/2.):(mod.shape[1]/2.)]
        IntZVals = 1e3*voxelsize.z*mgrid[-(mod.shape[2]/2.):(mod.shape[2]/2.)]

        dx = voxelsize.x*1e3
        dy = voxelsize.y*1e3
        dz = voxelsize.z*1e3

        interpModel = mod

        interpModel = interpModel/interpModel.max() #normalise to 1

        interpModelF = numpy.fft.fftn(interpModel)

        kx,ky,kz = numpy.mgrid[:interpModel.shape[0],:interpModel.shape[1],:interpModel.shape[2]]

        kx = numpy.fft.fftshift(kx - interpModel.shape[0]/2.)/interpModel.shape[0]
        ky = numpy.fft.fftshift(ky - interpModel.shape[1]/2.)/interpModel.shape[1]
        kz = numpy.fft.fftshift(kz - interpModel.shape[2]/2.)/interpModel.shape[2]
Beispiel #5
0
def preparePSF(md, PSSize):
    global PSFFileName, cachedPSF, cachedOTF2, cachedOTFH, autocorr

    PSFFilename = md.PSFFile

    if (not (PSFFileName == PSFFilename)) or (not (cachedPSF.shape == PSSize)):
        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()

        ps = ps.max(2)
        ps = ps - ps.min()
        #ps = ps*(ps > 0)
        ps = ps * scipy.signal.hanning(
            ps.shape[0])[:, None] * scipy.signal.hanning(ps.shape[1])[None, :]
        ps = ps / ps.sum()
        PSFFileName = PSFFilename
        pw = (numpy.array(PSSize) - ps.shape) / 2.
        pw1 = numpy.floor(pw)
        pw2 = numpy.ceil(pw)
        cachedPSF = pad.with_constant(ps, ((pw2[0], pw1[0]), (pw2[1], pw1[1])),
                                      (0, ))
        cachedOTFH = ifftn(cachedPSF) * cachedPSF.size
        cachedOTF2 = cachedOTFH * fftn(cachedPSF)
Beispiel #6
0
def setModel(modName, md):
    global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

    if not modName == interpModelName:
        mf = open(getFullExistingFilename(modName), 'rb')
        mod, voxelsize = cPickle.load(mf)
        mf.close()

        interpModelName = modName

        #if not voxelsize.x == md.voxelsize.x:
        #    raise RuntimeError("PSF and Image voxel sizes don't match")

        IntXVals = 1e3 * voxelsize.x * mgrid[-(mod.shape[0] / 2.):
                                             (mod.shape[0] / 2.)]
        IntYVals = 1e3 * voxelsize.y * mgrid[-(mod.shape[1] / 2.):
                                             (mod.shape[1] / 2.)]
        IntZVals = 1e3 * voxelsize.z * mgrid[-(mod.shape[2] / 2.):
                                             (mod.shape[2] / 2.)]

        dx = voxelsize.x * 1e3
        dy = voxelsize.y * 1e3
        dz = voxelsize.z * 1e3

        interpModel = mod

        interpModel = interpModel / interpModel.max()  #normalise to 1

        #do the spline filtering here rather than in interpolation
        interpModel = ndimage.spline_filter(interpModel)

        twist.twistCal(interpModel, IntXVals, IntYVals, IntZVals)
Beispiel #7
0
    def setModelFromMetadata(self, md):
        '''load the model from file - returns True if the model changed, False if
        an existing model was reused'''
        #global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

        modName = md.PSFFile

        if not modName == self.interpModelName:
            #special case for theoretical models
            if modName.startswith('ZMODEL:'):
                params = eval(modName[7:])
                mod, voxelsize = self.genTheoreticalModelZernike(md, **params)
            else:
                #try and get psf from task queue
                #if not md.taskQueue == None:
                try:
                    mod, voxelsize = md.taskQueue.getQueueData(
                        md.dataSourceID, 'PSF')
                except:
                    mf = open(getFullExistingFilename(modName), 'rb')
                    mod, voxelsize = load(mf)
                    mf.close()

            self.setModel(modName, mod, voxelsize)

            #print 'model changed'
            return True  #model changed
        else:
            return False  #model not changed
Beispiel #8
0
def preparePSF(md, axis):
    global PSFFileName, lobeSeparation

    PSFFilename = md.PSFFile

    if (not (PSFFileName == PSFFilename)):

        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()

        ps = ps.max(2)
        ps = ps - ps.min()
        ps = ps / ps.max()

        PSFFileName = PSFFilename

        if axis == 'x':
            I = ps.max(1)
        else:
            I = ps.max(0)

        sepr = I[(I.size / 2):].argmax()
        sepl = I[(I.size / 2)::-1].argmax()

        print((sepr, sepl))

        lobeSeparation = sepr + sepl
Beispiel #9
0
def preparePSF(md, axis):
    global PSFFileName, lobeSeparation
    
    PSFFilename = md.PSFFile
                
    if (not (PSFFileName == PSFFilename)):
        
        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()
            
        ps = ps.max(2)
        ps = ps - ps.min()
        ps = ps /ps.max()

        PSFFileName = PSFFilename
        
        if axis == 'x':
            I = ps.max(1)
        else:
            I = ps.max(0)
            
        sepr = I[(I.size/2):].argmax()
        sepl = I[(I.size/2)::-1].argmax()
        
        print((sepr, sepl))
        
        lobeSeparation = sepr + sepl
Beispiel #10
0
    def setModelFromMetadata(self, md):
        '''load the model from file - returns True if the model changed, False if
        an existing model was reused'''
        #global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

        modName = md.PSFFile

        if not modName == self.interpModelName:
            #special case for theoretical models
            if modName.startswith('ZMODEL:'):
                params = eval(modName[7:])
                mod, voxelsize = self.genTheoreticalModelZernike(md, **params)
            else: 
                #try and get psf from task queue
                #if not md.taskQueue == None:
                try:
                    mod, voxelsize = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
                except:
                    mf = open(getFullExistingFilename(modName), 'rb')
                    mod, voxelsize = load(mf)
                    mf.close()

            self.setModel(modName, mod, voxelsize)

            #print 'model changed'
            return True #model changed
        else:
            return False #model not changed
Beispiel #11
0
    def _fetchMap(self, md, mapName):
        '''retrive a map, with a given name. First try and get it from the Queue,
        then try finding it locally'''
        try:
            varmap = md.taskQueue.getQueueData(md.dataSourceID, 'MAP',  mapName)
        except:
            fn = getFullExistingFilename(mapName)
            varmap = ImageStack(filename=fn).data[:,:,0].squeeze() #this should handle .tif, .h5, and a few others

        return varmap
Beispiel #12
0
    def _fetchMap(self, md, mapName):
        '''retrive a map, with a given name. First try and get it from the Queue,
        then try finding it locally'''
        try:
            varmap = md.taskQueue.getQueueData(md.dataSourceID, 'MAP',  mapName)
        except:
            fn = getFullExistingFilename(mapName)
            varmap = ImageStack(filename=fn).data[:,:,0].squeeze() #this should handle .tif, .h5, and a few others

        return varmap
Beispiel #13
0
def preparePSF(md, PSSize):
    global PSFFileName, PSFSize, FTW

    PSFFilename = md.PSFFile

    if (not (PSFFileName == PSFFilename)) or (not (PSFSize == PSSize)):
        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()

        FTW = fftwWeiner(ps, vox, PSSize)

        PSFFileName = PSFFilename
        PSFSize = PSSize
Beispiel #14
0
def preparePSF(md, PSSize):
    global PSFFileName, PSFSize, FTW

    PSFFilename = md.PSFFile    
    
    if (not (PSFFileName == PSFFilename)) or (not (PSFSize == PSSize)):
        try:
            ps, vox = md.taskQueue.getQueueData(md.dataSourceID, 'PSF')
        except:
            fid = open(getFullExistingFilename(PSFFilename), 'rb')
            ps, vox = pickle.load(fid)
            fid.close()     
        
        FTW = fftwWeiner(ps,vox, PSSize)

        PSFFileName = PSFFilename
        PSFSize = PSSize
Beispiel #15
0
    def getQueueData(self, fieldName, *args):
        '''Get data, defined by fieldName and potntially additional arguments,  ascociated with queue'''
        if fieldName == 'ImageShape':
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.ImageData.shape[1:]
            
            return res
        elif fieldName == 'ImageData':
            sliceNum, = args
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.ImageData[sliceNum, :,:]
            
            return res
        elif fieldName == 'NumSlices':
            #self.dataFileLock.acquire()
            #res = self.h5DataFile.root.ImageData.shape[0]
            #self.dataFileLock.release()
            #print res, self.numSlices
            #return res
            return self.numSlices
        elif fieldName == 'Events':
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.Events[:]
            
            return res
        elif fieldName == 'PSF':
            from PYME.ParallelTasks.relativeFiles import getFullExistingFilename
            res = None
            #self.dataFileLock.acquire()
            #try:
                #res = self.h5DataFile.root.PSFData[:]
            #finally:
            #    self.dataFileLock.release()
            #try:
            modName = self.resultsMDH.getEntry('PSFFile')
            mf = open(getFullExistingFilename(modName), 'rb')
            res = np.load(mf)
            mf.close()
            #except:
                #pass

            return res
        else:
            return HDFResultsTaskQueue.getQueueData(self, fieldName, *args)
Beispiel #16
0
    def getQueueData(self, fieldName, *args):
        '''Get data, defined by fieldName and potntially additional arguments,  ascociated with queue'''
        if fieldName == 'ImageShape':
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.ImageData.shape[1:]

            return res
        elif fieldName == 'ImageData':
            sliceNum, = args
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.ImageData[sliceNum, :, :]

            return res
        elif fieldName == 'NumSlices':
            #self.dataFileLock.acquire()
            #res = self.h5DataFile.root.ImageData.shape[0]
            #self.dataFileLock.release()
            #print res, self.numSlices
            #return res
            return self.numSlices
        elif fieldName == 'Events':
            with self.dataFileLock.rlock:
                res = self.h5DataFile.root.Events[:]

            return res
        elif fieldName == 'PSF':
            from PYME.ParallelTasks.relativeFiles import getFullExistingFilename
            res = None
            #self.dataFileLock.acquire()
            #try:
            #res = self.h5DataFile.root.PSFData[:]
            #finally:
            #    self.dataFileLock.release()
            #try:
            modName = self.resultsMDH.getEntry('PSFFile')
            mf = open(getFullExistingFilename(modName), 'rb')
            res = np.load(mf)
            mf.close()
            #except:
            #pass

            return res
        else:
            return HDFResultsTaskQueue.getQueueData(self, fieldName, *args)
Beispiel #17
0
    def setModelFromFile(self, modName, md=None):
        '''load the model from file - returns True if the model changed, False if
        an existing model was reused'''
        #global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

        if not modName == self.interpModelName:
            #special case for theoretical models
            if modName.startswith('ZMODEL:'):
                #print modName[7:]
                params = eval(modName[7:])
                mod, voxelsize = self.genTheoreticalModelZernike(md, **params)
            else:
                mf = open(getFullExistingFilename(modName), 'rb')
                mod, voxelsize = load(mf)
                mf.close()

            self.setModel(modName, mod, voxelsize)

            #print 'model changed'
            return True  #model changed
        else:
            return False  #model not changed
Beispiel #18
0
    def getQueueData(self, fieldName, *args):
        '''Get data, defined by fieldName and potntially additional arguments,  ascociated with queue'''
        if fieldName == 'FitResults':
            startingAt, = args
            with self.fileResultsLock.rlock:
                if self.h5ResultsFile.__contains__('/FitResults'):
                    res = self.h5ResultsFile.root.FitResults[startingAt:]
                else:
                    res = []
            
            return res
        elif fieldName == 'MAP':
            mapName, = args
            from PYME.ParallelTasks.relativeFiles import getFullExistingFilename
            from PYME.DSView.image import ImageStack
            
            fn = getFullExistingFilename(mapName)
            varmap = ImageStack(filename=fn).data[:,:,0].squeeze() #this should handle .tif, .h5, and a few others

            return varmap
        else:
            return None
Beispiel #19
0
    def setModelFromFile(self, modName, md=None):
        '''load the model from file - returns True if the model changed, False if
        an existing model was reused'''
        #global IntXVals, IntYVals, IntZVals, interpModel, interpModelName, dx, dy, dz

        if not modName == self.interpModelName:
            #special case for theoretical models
            if modName.startswith('ZMODEL:'):
                #print modName[7:]
                params = eval(modName[7:])
                mod, voxelsize = self.genTheoreticalModelZernike(md, **params)
            else: 
                mf = open(getFullExistingFilename(modName), 'rb')
                mod, voxelsize = load(mf)
                mf.close()

            self.setModel(modName, mod, voxelsize)

            #print 'model changed'
            return True #model changed
        else:
            return False #model not changed
Beispiel #20
0
    def getQueueData(self, fieldName, *args):
        '''Get data, defined by fieldName and potntially additional arguments,  ascociated with queue'''
        if fieldName == 'FitResults':
            startingAt, = args
            with self.fileResultsLock.rlock:
                if self.h5ResultsFile.__contains__('/FitResults'):
                    res = self.h5ResultsFile.root.FitResults[startingAt:]
                else:
                    res = []

            return res
        elif fieldName == 'MAP':
            mapName, = args
            from PYME.ParallelTasks.relativeFiles import getFullExistingFilename
            from PYME.DSView.image import ImageStack

            fn = getFullExistingFilename(mapName)
            varmap = ImageStack(filename=fn).data[:, :, 0].squeeze(
            )  #this should handle .tif, .h5, and a few others

            return varmap
        else:
            return None