Esempio n. 1
0
def psf_normalize(psf_ims):
    """
    The PSF tends to have some bias and needs to have a unit area-under-curve
    The bias is removed by fitting to a Gaussian including the offset
    and then removing the offset.
    """

    normalized_psf_ims = np.zeros_like(psf_ims)

    divs = psf_ims.shape[0]
    for y in range(divs):
        for x in range(divs):

            psf_im = psf_ims[y, x]

            if np.sum(psf_im) > 0:

                # FIT to Gaussian to get the offset
                fit_params, _ = imops.fit_gauss2(psf_im)
                bias = fit_params[6]

                psf_im = (psf_im - bias).clip(min=0)

                # NORMALIZE so that all PSF estimates have unit area-under-curve
                # The z_and_region_to_psf can have all-zero elements thus we use np_safe_divide below
                denominator = psf_im.sum()
                normalized_psf_ims[y, x] = utils.np_safe_divide(
                    psf_im, denominator)

    return normalized_psf_ims
    def it_extracts_a_clean_psf_with_subpixel_alignment():
        # Sweep over various psf z depths
        # Generate a seris of small synth images (representing a region over many fields)
        # Pass in the locs directly from the synth image maker (bypassing peak finder)
        # Check that we get back a good approximation of the PSF

        for i, depth in enumerate(np.linspace(-0.25, 0.25, 4)):
            peaks, im, expected_std = _make_image(depth)

            psf, reasons = worker._psf_estimate(
                im, peaks.locs, mea=17, return_reasons=True
            )

            fit_params, _ = imops.fit_gauss2(psf)
            got = np.array(fit_params)
            expected = np.array([np.nan, expected_std, expected_std, 8, 8, 0, 0, 17])
            assert np.all((got[1:] - expected[1:]) ** 2 < 0.15 ** 2)
Esempio n. 3
0
    def estimate(self, psf_ims):
        check.array_t(psf_ims, ndim=4)
        n_divs_y, n_divs_x, peak_mea_h, peak_mea_w = psf_ims.shape
        assert n_divs_y == n_divs_x and self.hyper_n_divs == n_divs_y
        assert peak_mea_h == peak_mea_w and self.hyper_peak_mea == peak_mea_h

        self.sigma_x = np.zeros((self.hyper_n_divs, self.hyper_n_divs))
        self.sigma_y = np.zeros((self.hyper_n_divs, self.hyper_n_divs))
        self.rho = np.zeros((self.hyper_n_divs, self.hyper_n_divs))
        for y in range(n_divs_y):
            for x in range(n_divs_x):
                im = psf_ims[y, x]
                if np.sum(im) > 0:
                    fit_params, _ = imops.fit_gauss2(im)
                    self.sigma_x[y, x] = fit_params[Gauss2Params.SIGMA_X]
                    self.sigma_y[y, x] = fit_params[Gauss2Params.SIGMA_Y]
                    self.rho[y, x] = fit_params[Gauss2Params.RHO]
Esempio n. 4
0
 def it_fits_what_it_generates():
     true_params = (10.0, 1.5, 1.8, 8.5, 8.0, 0.2, 5.0, 17)
     im = imops.gauss2_rho_form(*true_params)
     fit_params, _ = imops.fit_gauss2(im)
     assert np.all(
         (np.array(fit_params) - np.array(true_params))**2 < 0.0001**2)