Exemplo n.º 1
0
def _regional_balance_chcy_ims(chcy_ims, calib):
    """
    Balance and subtract background on each channel according to calibration data.

    Returns:
       balanced_chcy_ims: The regionally balanced chcy_ims
    """
    n_channels, n_cycles = chcy_ims.shape[0:2]
    balanced_chcy_ims = np.zeros_like(chcy_ims)
    dim = chcy_ims.shape[-2:]
    for ch in range(n_channels):
        regional_bg_mean = np.array(
            calib[f"regional_bg_mean.instrument_channel[{ch}]"])
        regional_balance = np.array(
            calib[f"regional_illumination_balance.instrument_channel[{ch}]"])

        cy_ims = chcy_ims[ch]
        balance_im = imops.interp(regional_balance, dim)
        bg_im = imops.interp(regional_bg_mean, dim)

        if np.any(np.isnan(cy_ims)):
            raise ValueError(
                f"regional_balance_chcy_ims chcy_ims contains nan")
        if np.any(np.isnan(bg_im)):
            raise ValueError(f"regional_balance_chcy_ims bg_im contains nan")

        balanced_chcy_ims[ch] = (cy_ims - bg_im) * balance_im

    return balanced_chcy_ims
Exemplo n.º 2
0
 def it_interpolates_to_1_by_1_to_4_by_4():
     # Bypasses interp
     out_size = 4
     src = np.array([[3.0]])
     res = imops.interp(src, (out_size, out_size))
     assert res.shape == (4, 4)
     assert np.all(res == 3.0)
Exemplo n.º 3
0
 def it_interpolates_to_2_by_2_to_4_by_4():
     # Requires a linear interp
     out_size = 4
     src = np.array([[0, 2], [2, 3],])
     res = imops.interp(src, (out_size, out_size))
     assert res.shape == (4, 4)
     _check(res)
Exemplo n.º 4
0
 def it_interpolates_to_4_by_4_to_12_by_12():
     # Uses cubic interp
     out_size = 12
     src = np.array([[0, 1, 1, 2], [1, 1, 1, 2], [1, 1, 1, 2], [2, 2, 2, 3],])
     res = imops.interp(src, (out_size, out_size))
     assert res.shape == (12, 12)
     _check(res)
Exemplo n.º 5
0
def _calibrate_bg_and_psf_im(im, divs=5, keep_dist=8, peak_mea=11, locs=None):
    """
    Run background & PSF calibration for one image.

    These are typically combined from many fields and for each channel
    to get a complete calibration.

    This returns the accepted locs so that a z-stack can be estimated
    by using the most in-focus frame for the locations

    Arguments:
        im: One image
        divs: Spatial divisions
        keep_dist: Pixel distancer under which is considered a collision
        peak_mea: n pixel width and height to hold the peak image
        locs: If None it will use the peak finder; otherwise these
              locs are being passed in and are expected to coorespond
              to the peak locs found in a previous step.

    Returns:
        locs (location of accepted peaks)
        regional_bg_mean
        regional_bg_std
        regional_psf_zstack
    """
    check.array_t(im, ndim=2)
    stats = _regional_bg_fg_stats(im, divs=divs)
    reg_bg_mean = stats[:, :, 0]
    reg_bg_std = stats[:, :, 1]
    check.array_t(reg_bg_mean, shape=(divs, divs))
    check.array_t(reg_bg_std, shape=(divs, divs))

    bg_im = imops.interp(reg_bg_mean, im.shape[-2:])
    im = im - bg_im

    if locs is None:
        locs = _peak_find(im)

    n_locs = locs.shape[0]
    accepted = np.zeros((n_locs, ))

    # In each region gather a PSF estimate and a list of
    # locations that were accepted. These locs can be
    # re-used when analyzing other z slices
    reg_psfs = np.zeros((divs, divs, peak_mea, peak_mea))
    for win_im, y, x, coord in imops.region_enumerate(im, divs):
        mea = win_im.shape[0]
        assert win_im.shape[1] == mea

        local_locs = locs - coord
        local_locs_mask = np.all((local_locs > 0) & (local_locs < mea), axis=1)
        local_locs = local_locs[local_locs_mask]
        n_local_locs = local_locs.shape[0]

        psfs, reasons = _psf_estimate(win_im,
                                      local_locs,
                                      peak_mea,
                                      keep_dist=keep_dist,
                                      return_reasons=True)
        reg_psfs[y, x] = psfs

        # for reason in (
        #     PSFEstimateMaskFields.accepted,
        #     # PSFEstimateMaskFields.skipped_near_edges,
        #     # PSFEstimateMaskFields.skipped_too_crowded,
        #     # PSFEstimateMaskFields.skipped_has_nan,
        #     # PSFEstimateMaskFields.skipped_empty,
        #     # PSFEstimateMaskFields.skipped_too_dark,
        #     # PSFEstimateMaskFields.skipped_too_oval,
        # ):
        #     n_local_rejected = (reasons[:, reason] > 0).sum()
        #     print(f"y,x={y},{x} {str(reason)}:, {n_local_rejected}")

        # Go backwards from local to global space.
        local_accepted_iz = np.argwhere(
            reasons[:, PSFEstimateMaskFields.accepted] == 1).flatten()
        local_loc_i_to_global_loc_i = np.arange(n_locs)[local_locs_mask]
        assert local_loc_i_to_global_loc_i.shape == (n_local_locs, )

        global_accepted_iz = local_loc_i_to_global_loc_i[local_accepted_iz]
        accepted[global_accepted_iz] = 1

    return locs[accepted > 0], reg_bg_mean, reg_bg_std, reg_psfs