Exemple #1
0
    def _findFiducials(self, sfunc, debounce, fiducalSize):
        ####################################################
        # Find Fiducials

        self.mIm = numpy.ones(self.data.shape, 'f')

        mdnm = 1. / np.median((self.data / self.sigma).ravel())

        for dri in self.driftEstInd:
            bs = cameraMaps.correctImage(
                self.md,
                bufferManager.dBuffer.getSlice(dri).squeeze())
            bs = bs.reshape(self.data.shape) / self.sigma
            bs = bs * mdnm
            #multiply images together, thus favouring images which are on over multiple frames
            self.mIm = self.mIm * bs

        #self.mIm = numpy.absolute(self.mIm)
        #if not 'PSFFile' in self.md.getEntryNames():
        fid_scale = max((fiducalSize / 100.) * 0.5, 0)
        self.ofdDr = ofind.ObjectIdentifier(
            self.mIm,
            filterRadiusLowpass=(1 + fid_scale),
            filterRadiusHighpass=(3 + fid_scale))
        #else:
        #    self.ofdDr = ofind_xcorr.ObjectIdentifier(self.mIm, self.md.getEntry('PSFFile'), 7, 3e-2)

        thres = self.calObjThresh**len(self.driftEstInd)
        self.ofdDr.FindObjects(thres,
                               0,
                               splitter=sfunc,
                               debounceRadius=debounce)
Exemple #2
0
def fitDep(g, r, ofindThresh, dx, dy):
    rg = r + g  #detect objects in sum image

    ofd = ofind.ObjectIdentifier(rg)

    ofd.FindObjects(ofindThresh, blurRadius=2)

    res_d = np.empty(len(ofd), FitResultsDType)

    class foo:
        pass

    md = MetaData.TIRFDefault

    md.chroma = foo()
    md.chroma.dx = dx
    md.chroma.dy = dy

    ff = FitFactory(
        np.concatenate((g.reshape(512, -1, 1), r.reshape(512, -1, 1)), 2), md)

    for i in range(len(ofd)):
        p = ofd[i]
        res_d[i] = ff.FromPoint(round(p.x), round(p.y))

    return res_d
Exemple #3
0
def fitIndep(g,r,ofindThresh):
    rg = r + g #detect objects in sum image
    
    ofd = ofind.ObjectIdentifier(rg)

    ofd.FindObjects(ofindThresh, blurRadius=2)

    res_g = np.empty(len(ofd), FitResultsDType)
    res_r = np.empty(len(ofd), FitResultsDType)

    ff_g = FitFactory(g.reshape(512,256,1), MetaData.TIRFDefault)
    ff_r = FitFactory(r.reshape(512,256,1), MetaData.TIRFDefault)
    
    for i in range(len(ofd)):    
        p = ofd[i]
        res_g[i] = ff_g.FromPoint(round(p.x), round(p.y))
        res_r[i] = ff_r.FromPoint(round(p.x), round(p.y))

    return(res_g, res_r)
Exemple #4
0
    def __call__(self, gui=False, taskQueue=None):
        global dBuffer, bBuffer, dataSourceID

        # short-circuit if a task is generated for a frame pre-StartAt
        # FIXME - we should never generate tasks for these frames.
        if self.index < self.md.get('Analysis.StartAt', 0):
            logger.error(
                "Frame index is less than 'Analysis.StartAt', frame should not have been released for analysis. Skipping to avoid potential buffer errors."
            )
            return fitResult(self, [], [])

        #create a local copy of the metadata
        md = copy.copy(self.md)
        md.tIndex = self.index
        md.taskQueue = taskQueue
        md.dataSourceID = self.dataSourceID

        self.roi_offset = [0, 0]

        #logging.debug('dataSourceID: %s, cachedDSID: %s', md.dataSourceID, bufferManager.dataSourceID)

        #make sure we're buffering the right data stream
        bufferManager.updateBuffers(md, self.dataSourceModule, self.bufferLen)

        self.data = bufferManager.dBuffer.getSlice(self.index)
        #if logger.isEnabledFor(logging.DEBUG):
        #    logger.debug('data: min - %3.2f, max - %3.2f, mean - %3.2f' % (self.data.min(), self.data.max(), self.data.mean()))
        #print self.index

        #when camera buffer overflows, empty pictures are produced - deal with these here
        if self.data.max() == 0:
            return fitResult(self, [], [])

        # default background to zero
        self.bg = 0

        if 'GPU_PREFIT' in dir(self.fitMod):
            if len(self.bgindices) != 0:
                if md.get('Analysis.GPUPCTBackground', False):
                    # asynchronous background calc on the GPU
                    bufferManager.bBuffer.calc_background(self.bgindices)
                    self.bg = bufferManager.bBuffer
                else:
                    # calculate now on the CPU
                    self.bg = cameraMaps.correctImage(
                        md, bufferManager.bBuffer.getBackground(
                            self.bgindices)).reshape(self.data.shape)
            # fit module does its own prefit steps on the GPU
            self.data = self.data.squeeze()
            ff = self.fitMod.FitFactory(self.data,
                                        md,
                                        background=self.bg,
                                        noiseSigma=None)
            self.res = ff.FindAndFit(self.threshold,
                                     cameraMaps=cameraMaps,
                                     gui=gui)
            return fitResult(self, self.res, [])

        # squash 4th dimension
        # NOTE: correctImage now subtracts ADOffset
        self.data = cameraMaps.correctImage(md, self.data.squeeze()).reshape(
            (self.data.shape[0], self.data.shape[1], 1))
        # calculate noise
        self.sigma = self.calcSigma(md, self.data)
        if len(self.bgindices) != 0:
            # calculate the background for this frame and correct this for camera characteristics
            self.bg = cameraMaps.correctImage(
                md, bufferManager.bBuffer.getBackground(
                    self.bgindices)).reshape(self.data.shape)

        #if logger.isEnabledFor(logging.DEBUG):
        #    logger.debug('data_mean: %3.2f, bg: %3.2f, sigma: %3.2f' % (self.data.mean(), self.sigma.mean(), self.bg.mean()))

        #############################################
        # Special cases - defer object finding to fit module

        if self.fitModule == 'ConfocCOIR':  #special case - no object finding
            self.res = self.fitMod.ConfocCOI(self.data, md, background=self.bg)
            return fitResult(self, self.res, [])

        if 'MULTIFIT' in dir(self.fitMod):
            #fit module does it's own object finding
            ff = self.fitMod.FitFactory(self.data,
                                        md,
                                        background=self.bg,
                                        noiseSigma=self.sigma)
            self.res = ff.FindAndFit(self.threshold,
                                     gui=gui,
                                     cameraMaps=cameraMaps)
            return fitResult(self, self.res, [])

        ##############################################
        # Find candidate molecule positions
        bgd = (self.data.astype('f') - self.bg)

        #print bgd.shape, self.calcThreshold().shape

        if 'Splitter.TransmittedChannel' in self.md.getEntryNames():
            #don't find points in transmitted light channel
            transChan = md.getEntry('Splitter.TransmitedChannel')
            if transChan == 'Top':
                bgd[:, :(self.data.shape[1] /
                         2)] = 0  #set upper half of image to zero

        #define splitter mapping function (if appropriate) for use in object finding
        sfunc = None
        if self.is_splitter_fit:
            sfunc = self.__mapSplitterCoords

        #Choose which version of ofind to use
        if 'PRI.Axis' in self.md.getEntryNames(
        ) and not self.md['PRI.Axis'] == 'none':
            self.ofd = ofind_pri.ObjectIdentifier(bgd * (bgd > 0),
                                                  md,
                                                  axis=self.md['PRI.Axis'])
        else:  # not 'PSFFile' in self.md.getEntryNames():
            filtRadiusLowpass = md.getOrDefault(
                'Analysis.DetectionRadiusLowpass', 1.0)
            filtRadiusHighpass = md.getOrDefault(
                'Analysis.DetectionRadiusHighpass', 3.0)
            self.ofd = ofind.ObjectIdentifier(
                bgd * (bgd > 0),
                filterRadiusLowpass=filtRadiusLowpass,
                filterRadiusHighpass=filtRadiusHighpass)
        #else: #if we've got a PSF then use cross-correlation object identificatio
        #    self.ofd = ofind_xcorr.ObjectIdentifier(bgd * (bgd > 0), md, 7, 5e-2)

        debounce = self.md.getOrDefault('Analysis.DebounceRadius', 5)
        discardClumpRadius = self.md.getOrDefault('Analysis.ClumpRejectRadius',
                                                  0)

        self.ofd.FindObjects(self.calcThreshold(),
                             0,
                             splitter=sfunc,
                             debounceRadius=debounce,
                             discardClumpRadius=discardClumpRadius)

        ####################################################
        # Find Fiducials
        if self.driftEst:
            self.driftEstInd = self.index + self.md.getOrDefault(
                'Analysis.DriftIndices', np.array([-10, 0, 10]))
            self.calObjThresh = self.md.getOrDefault(
                'Analysis.FiducialThreshold', 6)
            fiducialROISize = self.md.getOrDefault('Analysis.FiducialROISize',
                                                   11)
            fiducialSize = self.md.getOrDefault('Analysis.FiducalSize', 1000.)
            self._findFiducials(sfunc, debounce, fiducialSize)

        #####################################################################
        #If we are using a splitter, chop the largest common ROI out of the two channels

        if self.is_splitter_fit:
            self.data = self._splitImage(self.data)
            self.sigma = self._splitImage(self.sigma)

            if not len(self.bgindices) == 0:
                self.bg = self._splitImage(self.bg)

        #If we're running under a gui - display found objects
        if gui:
            self._displayFoundObjects()

        #########################################################
        #Create a fit 'factory'
        fitFac = self.fitMod.FitFactory(self.data,
                                        md,
                                        background=self.bg,
                                        noiseSigma=self.sigma,
                                        roi_offset=self.roi_offset)

        if 'FitResultsDType' in dir(self.fitMod):
            self.res = numpy.empty(len(self.ofd), self.fitMod.FitResultsDType)
            if 'Analysis.ROISize' in md.getEntryNames():
                rs = md.getEntry('Analysis.ROISize')
                for i in range(len(self.ofd)):
                    p = self.ofd[i]
                    self.res[i] = fitFac.FromPoint(p.x, p.y, roiHalfSize=rs)
            else:
                for i in range(len(self.ofd)):
                    p = self.ofd[i]
                    self.res[i] = fitFac.FromPoint(p.x, p.y)
        else:
            #legacy fit modules
            self.res = [fitFac.FromPoint(p.x, p.y) for p in self.ofd]

        #Fit Fiducials NOTE: This is potentially broken
        self.drRes = []
        if self.driftEst:
            fitFac = self.fitMod.FitFactory(self.data,
                                            md,
                                            noiseSigma=self.sigma,
                                            roi_offset=self.roi_offset)
            nToFit = min(10, len(
                self.ofdDr))  #don't bother fitting lots of calibration objects
            if 'FitResultsDType' in dir(self.fitMod):
                self.drRes = numpy.empty(nToFit, self.fitMod.FitResultsDType)
                for i in range(nToFit):
                    p = self.ofdDr[i]
                    self.drRes[i] = fitFac.FromPoint(
                        p.x, p.y, roiHalfSize=fiducialROISize)
            else:
                self.drRes = [
                    fitFac.FromPoint(p.x, p.y) for p in self.ofd[:nToFit]
                ]

            #print 'Fitted %d fiducials' % nToFit, len(self.drRes)

        return fitResult(self, self.res, self.drRes)