示例#1
0
 def test_1d_conserve_sum(self):
     """Test 1D array with conserve_sum=False."""
     data = np.arange(2)
     block_size = 2.
     expected = block_replicate(data, block_size) * block_size
     result = block_replicate(data, block_size, conserve_sum=False)
     assert np.all(result == expected)
示例#2
0
 def test_2d_conserve_sum(self):
     """Test 2D array with conserve_sum=False."""
     data = np.arange(6).reshape(2, 3)
     block_size = 2.
     expected = block_replicate(data, block_size) * block_size**2
     result = block_replicate(data, block_size, conserve_sum=False)
     assert np.all(result == expected)
示例#3
0
 def upsample(self, factor=2, conserve_sum=True):
     data0 = self.datos
     data1 = block_replicate(data0,
                             block_size=factor,
                             conserve_sum=conserve_sum)
     return Imagen(data1, self.centro,
                   tuple(ti * factor for ti in self.size),
                   self.pixsize / factor)
示例#4
0
def get_masked_data_for_sep(filename, bin_factor=32, thresh=0.7):
    hdulist = fits.open(filename)
    data = hdulist['SCI'].data
    weight = hdulist['WHT'].data
    header = hdulist['SCI'].header

    binned = nddata.block_reduce(weight, bin_factor)
    debinned = nddata.block_replicate(binned, bin_factor)

    i0 = (weight.shape[0] - debinned.shape[0]) // 2
    i1 = (debinned.shape[0] - weight.shape[0]) // 2
    j0 = (weight.shape[1] - debinned.shape[1]) // 2
    j1 = (debinned.shape[1] - weight.shape[1]) // 2

    padded = np.pad(debinned, ((i0, -i1), (j0, -j1)), 'constant')
    mask = padded < thresh * np.max(weight)

    return data, mask, header
示例#5
0
 def test_block_size_len(self):
     """Test block_size length."""
     data = np.arange(5)
     with pytest.raises(ValueError):
         block_replicate(data, (2, 2))
示例#6
0
 def test_block_size_broadcasting(self):
     """Test scalar block_size broadcasting."""
     data = np.arange(4).reshape(2, 2)
     result1 = block_replicate(data, 2)
     result2 = block_replicate(data, (2, 2))
     assert np.all(result1 == result2)
示例#7
0
 def test_2d(self):
     """Test 2D array."""
     data = np.arange(2).reshape(2, 1)
     expected = np.array([[0, 0], [0, 0], [0.25, 0.25], [0.25, 0.25]])
     result = block_replicate(data, 2)
     assert np.all(result == expected)
示例#8
0
 def test_1d(self):
     """Test 1D array."""
     data = np.arange(2)
     expected = np.array([0, 0, 0.5, 0.5])
     result = block_replicate(data, 2)
     assert np.all(result == expected)
示例#9
0
def lacosmic(data, contrast, cr_threshold, neighbor_threshold,
             error=None, mask=None, background=None, effective_gain=None,
             readnoise=None, maxiter=4, border_mode='mirror'):
    r"""
    Remove cosmic rays from an astronomical image using the L.A.Cosmic
    algorithm.

    The `L.A.Cosmic algorithm
    <http://www.astro.yale.edu/dokkum/lacosmic/>`_ is based on Laplacian
    edge detection and is described in `van Dokkum (2001; PASP 113,
    1420)
    <https://ui.adsabs.harvard.edu/abs/2001PASP..113.1420V/abstract>`_.

    Parameters
    ----------
    data : array_like
        The 2D array of the image.

    contrast : float
        Contrast threshold between the Laplacian image and the
        fine-structure image.  If your image is critically sampled, use
        a value around 2.  If your image is undersampled (e.g., HST
        data), a value of 4 or 5 (or more) is more appropriate.  If your
        image is oversampled, use a value between 1 and 2.  For details,
        please see `PASP 113, 1420 (2001)
        <https://ui.adsabs.harvard.edu/abs/2001PASP..113.1420V/abstract>`_,
        which calls this parameter :math:`f_{\mbox{lim}}`.  In
        particular, Figure 4 shows the approximate relationship between
        the ``contrast`` parameter and the full-width half-maximum (in
        pixels) of stars in your image.

    cr_threshold : float
        The Laplacian signal-to-noise ratio threshold for cosmic-ray
        detection.

    neighbor_threshold : float
        The Laplacian signal-to-noise ratio threshold for detection of
        cosmic rays in pixels neighboring the initially-identified
        cosmic rays.

    error : array_like, optional
        The 1-sigma errors of the input ``data``.  If ``error`` is not
        input, then ``effective_gain`` and ``readnoise`` will be used to
        construct an approximate model of the ``error``.  If ``error``
        is input, it will override the ``effective_gain`` and
        ``readnoise`` parameters.  ``error`` must have the same shape as
        ``data``.

    mask : array_like (bool), optional
        A boolean mask, with the same shape as ``data``, where a `True`
        value indicates the corresponding element of ``data`` is masked.
        Masked pixels are ignored when identifying cosmic rays.  It is
        highly recommended that saturated stars be included in ``mask``.

    background : float or array_like, optional
        The background level previously subtracted from the input
        ``data``.  ``background`` may either be a scalar value or a 2D
        image with the same shape as the input ``data``.  If the input
        ``data`` has not been background subtracted, then set
        ``background=None`` (default).

    effective_gain : float, array-like, optional
        Ratio of counts (e.g., electrons or photons) to the units of
        ``data``.  For example, if your input ``data`` are in units of
        ADU, then ``effective_gain`` should represent electrons/ADU.  If
        your input ``data`` are in units of electrons/s then
        ``effective_gain`` should be the exposure time (or an exposure
        time map).  ``effective_gain`` and ``readnoise`` must be
        specified if ``error`` is not input.

    readnoise : float, optional
        The read noise (in electrons) in the input ``data``.
        ``effective_gain`` and ``readnoise`` must be specified if
        ``error`` is not input.

    maxiter : float, optional
        The maximum number of iterations. The default is 4. The routine
        will automatically exit if no additional cosmic rays are
        identified in an iteration. If the routine is still identifying
        cosmic rays after four iterations, then you are likely digging
        into sources (e.g., saturated stars) and/or the noise. In
        that case, try inputing a ``mask`` or increasing the value of
        ``cr_threshold``.

    border_mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional
        The mode in which the array borders are handled during
        convolution and median filtering.  For 'constant', the fill
        value is 0.  The default is 'mirror', which matches the original
        L.A.Cosmic algorithm.

    Returns
    -------
    cleaned_image : `~numpy.ndarray`
        The cosmic-ray cleaned image.

    crmask : `~numpy.ndarray` (bool)
        A mask image of the identified cosmic rays.  Cosmic-ray pixels
        have a value of `True`.
    """
    block_size = 2.0
    kernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0], [0.0, -1.0, 0.0]])

    clean_data = data.copy()
    if background is not None:
        clean_data += background
    final_crmask = np.zeros(data.shape, dtype=bool)

    if error is not None:
        if data.shape != error.shape:
            raise ValueError('error and data must have the same shape')
    clean_error_image = error

    ncosmics, ncosmics_tot = 0, 0
    for iteration in range(maxiter):
        sampled_img = block_replicate(clean_data, block_size)
        convolved_img = ndimage.convolve(sampled_img, kernel,
                                         mode=border_mode).clip(min=0.0)
        laplacian_img = block_reduce(convolved_img, block_size)

        if clean_error_image is None:
            if effective_gain is None or readnoise is None:
                raise ValueError('effective_gain and readnoise must be '
                                 'input if error is not input')
            med5_img = ndimage.median_filter(clean_data, size=5,
                                             mode=border_mode).clip(min=1.e-5)
            error_image = (np.sqrt(effective_gain * med5_img + readnoise ** 2)
                           / effective_gain)
        else:
            error_image = clean_error_image

        snr_img = laplacian_img / (block_size * error_image)
        # this is used to remove extended structures (larger than ~5x5)
        snr_img -= ndimage.median_filter(snr_img, size=5, mode=border_mode)

        # used to remove compact bright objects
        med3_img = ndimage.median_filter(clean_data, size=3, mode=border_mode)
        med7_img = ndimage.median_filter(med3_img, size=7, mode=border_mode)
        finestruct_img = ((med3_img - med7_img) / error_image).clip(min=0.01)

        cr_mask1 = snr_img > cr_threshold
        # NOTE: to follow the paper exactly, this condition should be
        # "> contrast * block_size".  "lacos_im.cl" uses simply "> contrast"
        cr_mask2 = (snr_img / finestruct_img) > contrast
        cr_mask = cr_mask1 * cr_mask2
        if mask is not None:
            cr_mask = np.logical_and(cr_mask, ~mask)

        # grow cosmic rays by one pixel and check in snr_img
        selem = np.ones((3, 3))
        neigh_mask = ndimage.binary_dilation(cr_mask, selem)
        cr_mask = cr_mask1 * neigh_mask
        # now grow one more pixel and lower the detection threshold
        neigh_mask = ndimage.binary_dilation(cr_mask, selem)
        cr_mask = (snr_img > neighbor_threshold) * neigh_mask

        # previously unknown cosmic rays found in this iteration
        crmask_new = np.logical_and(~final_crmask, cr_mask)
        ncosmics = np.count_nonzero(crmask_new)

        final_crmask = np.logical_or(final_crmask, cr_mask)
        ncosmics_tot += ncosmics
        log.info(f'Iteration {iteration + 1}: Found {ncosmics} cosmic-ray '
                 f'pixels, Total: {ncosmics_tot}')
        if ncosmics == 0:
            if background is not None:
                clean_data -= background
            return clean_data, final_crmask
        clean_data = _clean_masked_pixels(clean_data, final_crmask, size=5,
                                          exclude_mask=mask)

    if background is not None:
        clean_data -= background
    return clean_data, final_crmask