コード例 #1
0
ファイル: corr_lock_c2dg.py プロジェクト: BoettigerLab/Hal2
    def findROI(self, image):
        """
        Finds the ROI.
        """
        assert (image.flags['C_CONTIGUOUS']), "Image is not C contiguous!"
        assert (
            image.dtype == numpy.float64), "Images is not numpy.float64 type."

        self.mxf.resetTaken()

        # Create convolution object, if we have not already done this.
        if self.fg_filter is None:
            fg_psf = fitting.gaussianPSF(image.shape, self.sigma)
            self.fg_filter = matchedFilterC.MatchedFilter(fg_psf)

        # Find peaks.
        smoothed_image = self.fg_filter.convolve(image)
        [x, y, z, h] = self.mxf.findMaxima([smoothed_image], want_height=True)

        # No peaks found check
        if (x.size == 0):
            return [0, 0, False]

        # Slice out ROI.
        max_index = numpy.argmax(h)
        mx = int(round(x[max_index])) + 1
        my = int(round(y[max_index])) + 1
        rs = self.roi_size
        roi = image[my - rs:my + rs, mx - rs:mx + rs]
        roi -= numpy.min(roi)

        return [mx, my, roi]
コード例 #2
0
    def findFitPeak(self, image):
        """
        Returns the 2D gaussian fit to the brightest peak in the image.
        """
        # Convert image and add offset, this is to keep the MLE fitter from
        # overfitting the background and/or taking logs of zero.
        #
        image = numpy.ascontiguousarray(image, dtype = numpy.float64)
        image += self.offset
        
        self.mxf.resetTaken()

        # Create the peak fitter object, if we have not already done this.
        #
        if self.mfit is None:
            background = numpy.zeros(image.shape)

            # Create convolution filter.
            fg_psf = fitting.gaussianPSF(image.shape, self.sigma)
            self.fg_filter = matchedFilterC.MatchedFilter(fg_psf)

            # Create fitter.
            self.mfit = daoFitC.MultiFitter2DFixed(roi_size = self.roi_size)
            #self.mfit = daoFitC.MultiFitter2D()
            #self.mfit = daoFitC.MultiFitter3D()
            #self.mfit.default_tol = 1.0e-3
            self.mfit.initializeC(image)
            self.mfit.newImage(image)
            self.mfit.newBackground(background)
        else:
            self.mfit.newImage(image)

        # Find peaks.
        smoothed_image = self.fg_filter.convolve(image)
        [x, y, z, h] = self.mxf.findMaxima([smoothed_image], want_height = True)

        # No peaks found check
        if(x.size == 0):
            return [0, 0, False]

        max_index = numpy.argmax(h)

        peaks = {"x" : numpy.array([x[max_index]]),
                 "y" : numpy.array([y[max_index]]),
                 "z" : numpy.array([z[max_index]]),
                 "sigma" : numpy.array([self.sigma])}

        # Pass peaks to fitter & fit.
        self.mfit.newPeaks(peaks, "finder")
        self.mfit.doFit(max_iterations = 50)

        # Check for fit convergence.
        if (self.mfit.getUnconverged() == 0):
            # Return peak location.
            x = self.mfit.getPeakProperty("x")
            y = self.mfit.getPeakProperty("y")
            return [y[0], x[0], True]
        else:
            return [0, 0, False]
コード例 #3
0
    def findFitPeak(self, image):
        """
        Returns the 2D gaussian fit to the brightest peak in the image.
        """
        # Convert image and add offset, this is to keep the MLE fitter from
        # overfitting the background and/or taking logs of zero.
        #
        image = numpy.ascontiguousarray(image, dtype = numpy.float64)
        image += self.offset
        
        self.mxf.resetTaken()

        # Create the peak fitter object, if we have not already done this.
        #
        if self.mfit is None:
            background = numpy.zeros(image.shape)

            # Create convolution filter.
            fg_psf = fitting.gaussianPSF(image.shape, self.sigma)
            self.fg_filter = matchedFilterC.MatchedFilter(fg_psf)

            # Create fitter.
            self.mfit = daoFitC.MultiFitter2DFixed(roi_size = self.roi_size)
            #self.mfit = daoFitC.MultiFitter2D()
            #self.mfit = daoFitC.MultiFitter3D()
            #self.mfit.default_tol = 1.0e-3
            self.mfit.initializeC(image)
            self.mfit.newImage(image)
            self.mfit.newBackground(background)
        else:
            self.mfit.newImage(image)

        # Find peaks.
        smoothed_image = self.fg_filter.convolve(image)
        [x, y, z, h] = self.mxf.findMaxima([smoothed_image], want_height = True)

        # No peaks found check
        if(x.size == 0):
            return [0, 0, False]

        max_index = numpy.argmax(h)

        peaks = {"x" : numpy.array([x[max_index]]),
                 "y" : numpy.array([y[max_index]]),
                 "z" : numpy.array([z[max_index]]),
                 "sigma" : numpy.array([self.sigma])}

        # Pass peaks to fitter & fit.
        self.mfit.newPeaks(peaks, "finder")
        self.mfit.doFit(max_iterations = 50)

        # Check for fit convergence.
        if (self.mfit.getUnconverged() == 0):
            # Return peak location.
            x = self.mfit.getPeakProperty("x")
            y = self.mfit.getPeakProperty("y")
            return [y[0], x[0], True]
        else:
            return [0, 0, False]
コード例 #4
0
    def setVariances(self, variances):
        """
        setVariances() customized for gaussian PSFs.
        """

        # Make sure that the number of (sCMOS) variance arrays
        # matches the number of image planes.
        #
        assert (len(variances) == self.n_channels)

        # Pad variances to correct size.
        #
        temp = []
        for variance in variances:
            temp.append(fitting.padArray(variance, self.margin))
        variances = temp

        # Create "foreground" and "variance" filters. There is
        # only one z value here.
        #
        # These are stored in a list indexed by z value, then by
        # channel / plane. So self.mfilters[1][2] is the filter
        # for z value 1, plane 2.
        #
        self.mfilters.append([])
        self.vfilters.append([])

        psf_norm = fitting.gaussianPSF(
            variances[0].shape, self.parameters.getAttr("foreground_sigma"))
        var_norm = psf_norm * psf_norm

        for i in range(self.n_channels):
            self.mfilters[0].append(
                matchedFilterC.MatchedFilter(
                    psf_norm,
                    fftw_estimate=self.parameters.getAttr("fftw_estimate"),
                    memoize=True,
                    max_diff=1.0e-3))
            self.vfilters[0].append(
                matchedFilterC.MatchedFilter(
                    var_norm,
                    fftw_estimate=self.parameters.getAttr("fftw_estimate"),
                    memoize=True,
                    max_diff=1.0e-3))

            # Save a pictures of the PSFs for debugging purposes.
            if self.check_mode:
                print("psf max", numpy.max(psf))
                filename = "psf_z0.0_c{1:d}.tif".format(j)
                tifffile.imsave(filename, psf.astype(numpy.float32))

        # This handles the rest of the initialization.
        #
        super(MPPeakFinderDao, self).setVariances(variances)

        return variances
コード例 #5
0
    def setVariances(self, variances):
        """
        setVariances() customized for gaussian PSFs.
        """

        # Make sure that the number of (sCMOS) variance arrays
        # matches the number of image planes.
        #
        assert(len(variances) == self.n_channels)

        # Pad variances to correct size.
        #
        temp = []
        for variance in variances:
            temp.append(fitting.padArray(variance, self.margin))
        variances = temp

        # Create "foreground" and "variance" filters. There is
        # only one z value here.
        #
        # These are stored in a list indexed by z value, then by
        # channel / plane. So self.mfilters[1][2] is the filter
        # for z value 1, plane 2.
        #
        self.mfilters.append([])
        self.vfilters.append([])

        psf_norm = fitting.gaussianPSF(variances[0].shape, self.parameters.getAttr("foreground_sigma"))
        var_norm = psf_norm * psf_norm

        for i in range(self.n_channels):
            self.mfilters[0].append(matchedFilterC.MatchedFilter(psf_norm,
                                                                 memoize = True,
                                                                 max_diff = 1.0e-3))
            self.vfilters[0].append(matchedFilterC.MatchedFilter(var_norm,
                                                                 memoize = True,
                                                                 max_diff = 1.0e-3))

            # Save a pictures of the PSFs for debugging purposes.
            if self.check_mode:
                print("psf max", numpy.max(psf))
                filename = "psf_z0.0_c{1:d}.tif".format(j)
                tifffile.imsave(filename, psf.astype(numpy.float32))

        # This handles the rest of the initialization.
        #
        super(MPPeakFinderDao, self).setVariances(variances)

        return variances
コード例 #6
0
    def findFitPeak(self, image):
        """
        Returns the optimal alignment (based on the correlation score) between
        a Gaussian and the brightest peak in the image.
        """
        image = numpy.ascontiguousarray(image, dtype=numpy.float64)

        self.mxf.resetTaken()

        # Create convolution object, if we have not already done this.
        if self.fg_filter is None:
            fg_psf = fitting.gaussianPSF(image.shape, self.sigma)
            self.fg_filter = matchedFilterC.MatchedFilter(fg_psf)

        # Find peaks.
        smoothed_image = self.fg_filter.convolve(image)
        [x, y, z, h] = self.mxf.findMaxima([smoothed_image], want_height=True)

        # No peaks found check
        if (x.size == 0):
            return [0, 0, False]

        # Slice out ROI.
        max_index = numpy.argmax(h)
        mx = int(round(x[max_index])) + 1
        my = int(round(y[max_index])) + 1
        rs = self.roi_size
        roi = image[my - rs:my + rs, mx - rs:mx + rs]
        roi -= numpy.min(roi)

        # Pass to aligner and find optimal offset.
        self.c2dg.setImage(roi)
        [disp, success, fun, status] = self.c2dg.maximize()
        if (success) or (status == 2):
            return [my + disp[0] - 0.5, mx + disp[1] - 0.5, True]
        else:
            return [0, 0, False]
コード例 #7
0
    def setVariances(self, variances):
        """
        We initialize the following here because at __init__ we
        don't know how big the images are. This is called after
        the analysis specific version pads the variances and
        creates the mfilters[] and the vfilters[] class members.
        
        Note the assumption that every frame in all the movies
        is the same size.
        """

        # Create mask to limit peak finding to a user defined sub-region of the image.
        #
        self.peak_mask = fitting.peakMask(variances[0].shape, self.parameters,
                                          self.margin)

        # Create matched filter for background. There is one of these for
        # each imaging plane for the benefit of memoization.
        #
        for i in range(self.n_channels):
            bg_psf = fitting.gaussianPSF(
                variances[0].shape,
                self.parameters.getAttr("background_sigma"))
            self.bg_filters.append(
                matchedFilterC.MatchedFilter(bg_psf,
                                             memoize=True,
                                             max_diff=1.0e-3))

        # Process variance arrays now as they don't change from frame
        # to frame.
        #
        # This initializes the self.variances array with a list
        # of lists with the same organization as foreground and
        # psf / variance filters.
        #
        # Use variance filter. I now think this is correct as this is
        # also what we are doing with the image background term. In
        # the case of the image background we are estimating the
        # variance under the assumption that it is Poisson so the
        # mean of the background is the variance. With the cameras
        # we know what the variance is because we measured it. Now
        # we need to weight it properly given the PSF filter that
        # we are applying to the foreground.
        #

        # Iterate over z values.
        #
        for i in range(len(self.mfilters)):
            variance = numpy.zeros(variances[0].shape)

            # Iterate over channels / planes.
            for j in range(len(self.mfilters[i])):

                # Convolve variance with the appropriate variance filter.
                conv_var = self.vfilters[i][j].convolve(variances[j])

                # Transform variance to the channel 0 frame.
                if self.atrans[j] is None:
                    variance += conv_var
                else:
                    variance += self.atrans[j].transform(conv_var)

            self.variances.append(variance)

        # Save results if needed for debugging purposes.
        if self.check_mode:
            with tifffile.TiffWriter("camera_variances.tif") as tf:
                for var in self.variances:
                    tf.save(var.astype(numpy.float32))
コード例 #8
0
    def setVariances(self, variances):
        """
        We initialize the following here because at __init__ we
        don't know how big the images are. This is called after
        the analysis specific version pads the variances and
        creates the mfilters[] and the vfilters[] class members.
        
        Note the assumption that every frame in all the movies
        is the same size.
        """
        
        # Create mask to limit peak finding to a user defined sub-region of the image.
        #
        self.peak_mask = fitting.peakMask(variances[0].shape, self.parameters, self.margin)

        # Create matched filter for background. There is one of these for
        # each imaging plane for the benefit of memoization.
        #
        for i in range(self.n_channels):
            bg_psf = fitting.gaussianPSF(variances[0].shape, self.parameters.getAttr("background_sigma"))
            self.bg_filters.append(matchedFilterC.MatchedFilter(bg_psf,
                                                                memoize = True,
                                                                max_diff = 1.0e-3))

        # Process variance arrays now as they don't change from frame
        # to frame.
        #
        # This initializes the self.variances array with a list
        # of lists with the same organization as foreground and
        # psf / variance filters.
        #
        # Use variance filter. I now think this is correct as this is
        # also what we are doing with the image background term. In
        # the case of the image background we are estimating the
        # variance under the assumption that it is Poisson so the
        # mean of the background is the variance. With the cameras
        # we know what the variance is because we measured it. Now
        # we need to weight it properly given the PSF filter that
        # we are applying to the foreground.
        #
        
        # Iterate over z values.
        #
        for i in range(len(self.mfilters)):
            variance = numpy.zeros(variances[0].shape)

            # Iterate over channels / planes.
            for j in range(len(self.mfilters[i])):

                # Convolve variance with the appropriate variance filter.
                conv_var = self.vfilters[i][j].convolve(variances[j])

                # Transform variance to the channel 0 frame.
                if self.atrans[j] is None:
                    variance += conv_var
                else:
                    variance += self.atrans[j].transform(conv_var)

            self.variances.append(variance)

        # Save results if needed for debugging purposes.
        if self.check_mode:
            with tifffile.TiffWriter("camera_variances.tif") as tf:
                for var in self.variances:
                    tf.save(var.astype(numpy.float32))