Example #1
0
def iterateFit(fit_func, fit_data):

    if (type(fit_data.peaks) == type(numpy.array([]))):

        # Calculate "regularized" image.
        regularized_image = fit_data.regularizer.regularizeImage(fit_data.image)
        #regularized_image = fit_data.regularizer.normalizeImage(fit_data.image)
        #regularized_image = fit_data.image

        # Fit to update peak locations.
        result = fit_func(regularized_image,
                          fit_data.peaks,
                          scmos_cal = fit_data.lg_scmos_cal)
        fit_peaks = multi_c.getGoodPeaks(result[0],
                                         0.9*fit_data.threshold,
                                         0.5*fit_data.sigma)
        
        # Remove peaks that are too close to each other & refit.
        fit_peaks = util_c.removeClosePeaks(fit_peaks, fit_data.sigma, fit_data.neighborhood)
        result = fit_func(regularized_image, 
                          fit_peaks,
                          scmos_cal = fit_data.lg_scmos_cal)
        fit_peaks = multi_c.getGoodPeaks(result[0],
                                         0.9*fit_data.threshold,
                                         0.5*fit_data.sigma)

        fit_data.peaks = fit_peaks

        # FIXME:
        #  For multi-peak finding to work correctly we should subtract the
        #  peaks out of the actual image, not the regularized image, which
        #  is what this is.
        fit_data.residual = result[1]
    def fitPeaks(self, peaks):

        # Adjust to z starting position.
        z_index = utilC.getZCenterIndex()
        peaks[:,z_index] = peaks[:,z_index] * float(self.sfitter.getSize())

        if False:
            print "Before fitting"
            for i in range(5):
                print " ", peaks[i,0], peaks[i,1], peaks[i,3], peaks[i,5], peaks[i,6], peaks[i,7]
            print ""

        # Fit to update peak locations.
        self.sfitter.doFit(peaks)
        fit_peaks = self.sfitter.getGoodPeaks(min_height = 0.9 * self.fit_threshold)

        # Remove peaks that are too close to each other & refit.
        fit_peaks = utilC.removeClosePeaks(fit_peaks, self.fit_sigma, self.fit_neighborhood)

        # Redo the fit for the remaining peaks.
        self.sfitter.doFit(fit_peaks)
        fit_peaks = self.sfitter.getGoodPeaks(min_height = 0.9*self.fit_threshold)
        residual = self.sfitter.getResidual()

        if False:
            print "After fitting"
            for i in range(5):
                print " ", fit_peaks[i,0], fit_peaks[i,1], fit_peaks[i,3], fit_peaks[i,5], fit_peaks[i,6], fit_peaks[i,7]
            print ""
        
        return [fit_peaks, residual]
    def fitPeaks(self, peaks):

        # Fit to update peak locations.
        self.sfitter.doFit(peaks)
        fit_peaks = self.sfitter.getGoodPeaks(min_height = 0.9 * self.threshold,
                                              verbose = False)

        # Remove peaks that are too close to each other & refit.
        fit_peaks = utilC.removeClosePeaks(fit_peaks, self.sigma, self.neighborhood)

        # Redo the fit for the remaining peaks.
        self.sfitter.doFit(fit_peaks)
        fit_peaks = self.sfitter.getGoodPeaks(min_height = 0.9*self.threshold,
                                              verbose = False)
        residual = self.sfitter.getResidual()

        return [fit_peaks, residual]
Example #4
0
def iterateFit(fit_func, fit_data):

    if (type(fit_data.peaks) == type(numpy.array([]))):

        # Fit to update peak locations.
        result = fit_func(fit_data.image, fit_data.peaks)
        fit_peaks = multi_c.getGoodPeaks(result[0],
                                         0.9*fit_data.threshold,
                                         0.5*fit_data.sigma)
        
        # Remove peaks that are too close to each other & refit.
        fit_peaks = util_c.removeClosePeaks(fit_peaks, fit_data.sigma, fit_data.neighborhood)
        result = fit_func(fit_data.image, fit_peaks)
        fit_peaks = multi_c.getGoodPeaks(result[0],
                                         0.9*fit_data.threshold,
                                         0.5*fit_data.sigma)

        fit_data.peaks = fit_peaks
        fit_data.residual = result[1]
    def peakFinder(self, no_bg_image):

        all_new_peaks = None

        save_convolution = False
        if save_convolution:
            tif = tifffile.TiffWriter("image.tif")
            tif.save(self.image.astype(numpy.uint16) + 100)
            tif.save(no_bg_image.astype(numpy.uint16) + 100)
            tif.save(self.background.astype(numpy.uint16) + 100)
            
        #
        # Find peaks in image convolved with the PSF at different z values.
        #
        for i in range(len(self.mfilter)):

            height_rescale = self.height_rescale[i]
            mfilter = self.mfilter[i]
            taken = self.taken[i]
            z_value = self.z_value[i]

            # Smooth image with gaussian filter.
            smooth_image = mfilter.convolve(no_bg_image)

            if save_convolution:
                tif.save(smooth_image.astype(numpy.uint16) + 100)

            # Mask the image so that peaks are only found in the AOI.
            masked_image = smooth_image * self.peak_mask
        
            # Identify local maxima in the masked image.
            [new_peaks, taken] = utilC.findLocalMaxima(masked_image,
                                                       taken,
                                                       self.cur_threshold,
                                                       self.find_max_radius,
                                                       self.margin)

            #
            # Fill in initial values for peak height, background and sigma.
            #
            # FIXME: We just add the smoothed image and the background together
            #        as a hack so that we can still use the initializePeaks()
            #        function.
            #
            new_peaks = utilC.initializePeaks(new_peaks,                      # The new peaks.
                                              smooth_image + self.background, # Smooth image + background.
                                              self.background,                # The current estimate of the background.
                                              self.sigma,                     # The starting sigma value.
                                              z_value)                        # The starting z value.

            # Correct initial peak heights.
            h_index = utilC.getHeightIndex()
            new_peaks[:,h_index] = new_peaks[:,h_index] * height_rescale
            
            if all_new_peaks is None:
                all_new_peaks = new_peaks
            else:
                all_new_peaks = numpy.append(all_new_peaks, new_peaks, axis = 0)

        if save_convolution:
            tif.close()
                
        #
        # Remove the dimmer of two peaks with similar x,y values but different z values.
        #
        if (len(self.mfilter) > 1):

            if False:
                print "before", all_new_peaks.shape
                for i in range(all_new_peaks.shape[0]):
                    print all_new_peaks[i,:]
                print ""
            
            all_new_peaks = utilC.removeClosePeaks(all_new_peaks,                                               
                                                   self.find_max_radius,
                                                   self.find_max_radius)

            if False:
                print "after", all_new_peaks.shape
                for i in range(all_new_peaks.shape[0]):
                    print all_new_peaks[i,:]
                print ""
                
        return all_new_peaks