Exemplo n.º 1
0
def test_close():
    mask = SkyMask.empty(nxpix=5, nypix=5)
    mask.data[1:-1, 1:-1] = 1
    mask.data[2, 2] = 0
    structure = Box2DKernel(3).array
    mask = mask.close(structure)
    assert mask.data.sum() == 9
Exemplo n.º 2
0
def convolve(image, smooth=3, kernel='gauss'):

    if smooth is None and kernel in ['box', 'gauss']:
        return image

    if smooth is not None and not np.isscalar(smooth):
        raise ValueError("smooth= should be an integer - for more complex "
                         "kernels, pass an array containing the kernel "
                         "to the kernel= option")

    # The Astropy convolution doesn't treat +/-Inf values correctly yet, so we
    # convert to NaN here.

    image_fixed = np.array(image, dtype=float, copy=True)
    image_fixed[np.isinf(image)] = np.nan

    if kernel == 'gauss':
        if make_kernel is None:
            kernel = Gaussian2DKernel(smooth,
                                      x_size=smooth * 5,
                                      y_size=smooth * 5)
        else:
            kernel = make_kernel((smooth * 5, smooth * 5), smooth, 'gaussian')
    elif kernel == 'box':
        if make_kernel is None:
            kernel = Box2DKernel(smooth, x_size=smooth * 5, y_size=smooth * 5)
        else:
            kernel = make_kernel((smooth * 5, smooth * 5), smooth, 'boxcar')
    else:
        kernel = kernel

    return astropy_convolve(image, kernel, boundary='extend')
Exemplo n.º 3
0
def test_convolve_nd():
    energy_axis = MapAxis.from_edges(np.logspace(-1.0, 1.0, 4),
                                     unit="TeV",
                                     name="energy_true")
    geom = WcsGeom.create(binsz=0.02 * u.deg,
                          width=4.0 * u.deg,
                          axes=[energy_axis])
    m = Map.from_geom(geom)
    m.fill_by_coord([[0.2, 0.4], [-0.1, 0.6], [0.5, 3.6]])

    # TODO : build EnergyDependentTablePSF programmatically rather than using CTA 1DC IRF
    filename = (
        "$GAMMAPY_DATA/cta-1dc/caldb/data/cta//1dc/bcf/South_z20_50h/irf_file.fits"
    )
    psf = EnergyDependentMultiGaussPSF.read(filename,
                                            hdu="POINT SPREAD FUNCTION")
    table_psf = psf.to_energy_dependent_table_psf(theta=0.5 * u.deg)

    psf_kernel = PSFKernel.from_table_psf(table_psf,
                                          geom,
                                          max_radius=1 * u.deg)

    assert psf_kernel.psf_kernel_map.data.shape == (3, 101, 101)

    mc = m.convolve(psf_kernel)
    assert_allclose(mc.data.sum(axis=(1, 2)), [0, 1, 1], atol=1e-5)

    kernel_2d = Box2DKernel(3, mode="center")
    kernel_2d.normalize("peak")
    mc = m.convolve(kernel_2d.array)
    assert_allclose(mc.data[0, :, :].sum(), 0, atol=1e-5)
    assert_allclose(mc.data[1, :, :].sum(), 9, atol=1e-5)
Exemplo n.º 4
0
def test_convolve_nd():
    energy_axis = MapAxis.from_edges(
        np.logspace(-1.0, 1.0, 4), unit="TeV", name="energy_true"
    )
    geom = WcsGeom.create(binsz=0.02 * u.deg, width=4.0 * u.deg, axes=[energy_axis])
    m = Map.from_geom(geom)
    m.fill_by_coord([[0.2, 0.4], [-0.1, 0.6], [0.5, 3.6]])

    psf = PSFMap.from_gauss(energy_axis, sigma=[0.1, 0.2, 0.3] * u.deg)
    psf_kernel = psf.get_psf_kernel(geom=geom, max_radius=1 * u.deg)

    assert psf_kernel.psf_kernel_map.data.shape == (3, 101, 101)

    mc = m.convolve(psf_kernel)
    assert_allclose(mc.data.sum(axis=(1, 2)), [0, 1, 1], atol=1e-5)

    kernel_2d = Box2DKernel(3, mode="center")
    kernel_2d.normalize("peak")
    mc = m.convolve(kernel_2d.array)
    assert_allclose(mc.data[0, :, :].sum(), 0, atol=1e-5)
    assert_allclose(mc.data[1, :, :].sum(), 9, atol=1e-5)

    kernel_2d = Gaussian2DKernel(15, mode="center")
    kernel_2d.normalize("peak")
    mc_full = m.convolve(kernel_2d.array, mode="full")
    mc_same = m.convolve(kernel_2d.array, mode="same")
    coords = [[0.2, 0.1, 0.4, 0.44, -1.3], [-0.1, -0.13, 0.6, 0.57, 0.91], [0.5, 0.5, 3.6, 3.6, 0.5]]
    values_full = mc_full.get_by_coord(coords)
    values_same = mc_same.get_by_coord(coords)

    assert mc_same.data.shape == (3, 200, 200)
    assert mc_full.data.shape == (3, 320, 320)
    assert_allclose(values_full, values_same, rtol=1e-5)
Exemplo n.º 5
0
def convolve(image, smooth=3, kernel='gauss'):
    """ Convolve 2D image. Hacked from aplpy
    """
    if smooth is None and isinstance(kernel, str) and kernel in ['box', 'gauss']:
        return image

    if smooth is not None and not np.isscalar(smooth):
        raise ValueError("smooth= should be an integer - for more complex "
                         "kernels, pass an array containing the kernel "
                         "to the kernel= option")

    # The Astropy convolution doesn't treat +/-Inf values correctly yet, so we
    # convert to NaN here.
    image_fixed = np.array(image, dtype=float, copy=True)
    image_fixed[np.isinf(image)] = np.nan

    if isinstance(kernel, str):
        if kernel == 'gauss':
            kernel = Gaussian2DKernel(
                smooth, x_size=smooth * 5, y_size=smooth * 5)
        elif kernel == 'box':
            kernel = Box2DKernel(smooth, x_size=smooth * 5, y_size=smooth * 5)
        else:
            raise ValueError("Unknown kernel: {0}".format(kernel))

    return astropy_convolve(image, kernel, boundary='extend')
Exemplo n.º 6
0
def laplacian(data, x, y, bw=5, w='default'):
    import numpy as np
    from astropy.convolution import convolve, Box2DKernel
    # https://docs.astropy.org/en/latest/api/astropy.convolution.Box2DKernel.html

    if (w == 'default'):
        w = data.shape[0] / data.shape[1]

    data_smth = convolve(data, Box2DKernel(bw))

    # crop the edges
    bo = int(bw / 2 + 1)
    data_smth = data_smth[bo:-bo, bo:-bo]
    x = x[bo:-bo]
    y = y[bo:-bo]

    # Laplacian
    diff2 = np.gradient(np.gradient(data_smth, axis=0), axis=0) + \
        w*w*np.gradient(np.gradient(data_smth, axis=1), axis=1)

    # NaN wrapping for edges instead of cropping
    # for ii in range(data.shape[0]):
    #     for jj in range(data.shape[1]):
    #         if (ii <= bo+1 or ii >= data.shape[0]-bo-1 or \
    #             jj <= bo+1 or jj >= data.shape[1]-bo-1):
    #             diff2[ii][jj] = np.nan

    return diff2, x, y
Exemplo n.º 7
0
 def sub_bkg(self, verbose=True):
     # Here I subtract local sky background
     # Evaluate local sky backgroud within `halo_i`
     # Actually this should be estimated in larger cutuouts.
     # So make another cutout (larger)!
     from astropy.convolution import convolve, Box2DKernel
     from .image import extract_obj, seg_remove_cen_obj
     from sep import Background
     img_blur = convolve(abs(self.image), Box2DKernel(2))
     img_objects, img_segmap = extract_obj(abs(img_blur),
                                           b=5,
                                           f=4,
                                           sigma=4.5,
                                           minarea=2,
                                           pixel_scale=self.pixel_scale,
                                           deblend_nthresh=32,
                                           deblend_cont=0.0001,
                                           sky_subtract=False,
                                           show_fig=False,
                                           verbose=False)
     bk = Background(self.image, img_segmap != 0)
     glbbck = bk.globalback
     self.globalback = glbbck
     if verbose:
         print('# Global background: ', glbbck)
     self.image -= glbbck
Exemplo n.º 8
0
    def surface(self, box_size=None):
        """Retrieve surface covered by unmasked pixels
        Parameters
        ----------
        box_size : scalar or tuple, optional
            The edge of the map is cropped by the box_size if not None.
            Default is None.

        Returns
        -------
        :class:`astropy.units.Quantity`
            Surface covered by unmasked pixels

        Notes
        -------
            Default value for box_size in detect_sources is 5"""

        nvalid = np.prod(self.data.shape)

        if self.mask is not None:
            mask = self.mask
            if box_size is not None:
                box_kernel = Box2DKernel(box_size)
                mask = shrink_mask(mask, box_kernel)

            nvalid = np.sum(~mask)

        conversion = (u.pix.to(u.arcsec, equivalencies=self._pixel_scale)) ** 2

        return nvalid * conversion * u.arcsec ** 2
Exemplo n.º 9
0
    def calculate_gradient(self,P,G,Z,smooth=False):
        """Calculate the gradient of any property. Deals with grid points that have no mass (that should'nt contribute to the gradient).

        :param P: A param.Param instance.

         .. warning::

             This returns the gradient of the input field ... kind of ... sometimes
        """

        method = 'astropy'
        if method == 'self_defined':
            # SELF-DEFINED METHOD
            # This should set any boundary grid points using first order gradients, and then set any interior grid points using the numpy gradient method
            Z[isnan(Z)] = 0.
            mask = (G.m<P.M_tol).reshape(P.G.ny,P.G.nx)
            good_values = (G.m>P.M_tol).reshape(P.G.ny,P.G.nx)

            Z = Z.reshape(P.G.ny,P.G.nx)
            dZdy_nice,dZdx_nice = gradient(Z,G.dy,G.dx)

            has_right = hstack([good_values[:,1:], zeros([P.G.ny,1],dtype=bool)])*good_values
            has_left = hstack([zeros([P.G.ny,1],dtype=bool), good_values[:,:-1]])*good_values
            has_down = vstack([good_values[1:], zeros([1,P.G.nx],dtype=bool)])*good_values
            has_up = vstack([zeros([1,P.G.nx],dtype=bool), good_values[:-1]])*good_values

            right_grad = hstack([(Z[:,1:] - Z[:,:-1])/G.dx, zeros([P.G.ny,1])])
            up_grad = vstack([(Z[1:] - Z[:-1])/G.dy, zeros([1,P.G.nx])])

            dZdx = zeros([P.G.ny,P.G.nx])
            dZdx[has_right] = right_grad[has_right]
            dZdx[has_left]  = right_grad[roll(has_left,-1,axis=1)]
            dZdx[has_right*has_left] = dZdx_nice[has_right*has_left]

            dZdy = zeros([P.G.ny,P.G.nx])
            dZdy[has_up]   = up_grad[has_up]
            dZdy[has_down] = up_grad[roll(has_down,-1,axis=0)]
            dZdy[has_up*has_down] = dZdy_nice[has_up*has_down]
        elif method == 'interp':
            # INTERPOLATION METHOD
            F = interp2d(G.X[good_values],G.Y[good_values],Z[good_values])
            Z[mask] = F(G.X[mask],G.Y[mask])
            Z = Z.reshape(P.G.ny,P.G.nx)
            dZdy,dZdx = gradient(Z,G.dy,G.dx)
        elif method == 'astropy': # see here: https://astropy.readthedocs.io/en/v0.3/convolution/index.html#using-convolve
            Z = ma.masked_where(G.m<P.M_tol,Z).reshape(P.G.ny,P.G.nx)
            # if smooth:
            #     kernel = Box2DKernel(3) # smallest possible square kernel
            #     Z = convolve(Z,kernel)
            dZdy,dZdx = gradient(Z,G.dy,G.dx)
            if smooth:
                # kernel = Gaussian2DKernel(1) # smallest possible gaussian - maybe something even smaller?
                kernel = Box2DKernel(3) # smallest possible square kernel
                dZdy = convolve(dZdy,kernel)
                dZdx = convolve(dZdx,kernel)

        grad = array([dZdx.flatten(),
                      dZdy.flatten(),
                      zeros_like(G.m)]).T
        return grad
Exemplo n.º 10
0
    def interped(self):
        if not hasattr(self, '_interped'):
            kernel = Box2DKernel(5)  # Gaussian2DKernel(stddev=2.5) #

            crmask, _ = detect_cosmics(indat=np.ascontiguousarray(
                self.bkg_sub_img.filled(-9999)),
                                       inmask=self.bkg_sub_img.mask,
                                       sigclip=6.,
                                       cleantype='medmask')
            self.bkg_sub_img.mask = np.ma.mask_or(self.bkg_sub_img.mask,
                                                  crmask)
            self.bkg_sub_img.mask = np.ma.mask_or(self.bkg_sub_img.mask,
                                                  np.isnan(self.bkg_sub_img))

            print(('Masked pixels: ', np.sum(self.bkg_sub_img.mask)))
            img = self.bkg_sub_img.filled(np.nan)
            img_interp = interpolate_replace_nans(img, kernel, convolve=conv)

            while np.any(np.isnan(img_interp)):
                img_interp = interpolate_replace_nans(img_interp,
                                                      kernel,
                                                      convolve=conv)
            # clipped = sigma_clip(self.bkg_sub_img,
            # iters=5, sigma_upper=40).filled(np.nan)
            # img_interp = interpolate_replace_nans(img_interp, kernel)
            self._interped = img_interp

        return self._interped
Exemplo n.º 11
0
def cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w='default'):
    import numpy as np
    from astropy.convolution import convolve, Box2DKernel
    # https://docs.astropy.org/en/latest/api/astropy.convolution.Box2DKernel.html

    if (w == 'default'):
        w = data.shape[0] / data.shape[1]

    data_smth = convolve(data, Box2DKernel(bw))

    # crop the edges
    bo = int(bw / 2 + 1)
    data_smth = data_smth[bo:-bo, bo:-bo]
    x = x[bo:-bo]
    y = y[bo:-bo]

    dE = np.gradient(data_smth, axis=0)
    dT = np.gradient(data_smth, axis=1) * w
    d2E = np.gradient(np.gradient(data_smth, axis=0), axis=0)
    d2T = np.gradient(np.gradient(data_smth, axis=1), axis=1) * w * w
    dEdT = np.gradient(np.gradient(data_smth, axis=1), axis=0) * w

    # 2D curvature - https://doi.org/10.1063/1.3585113
    cv2d = ((1 + c1*dE**2)*c2*d2T - 2*c1*c2*dE*dT*dEdT + (1 + c2*dT**2)*c1*d2E)/ \
                       (1 + c1*dE**2 + c2*dT**2)**1.5

    # NaN wrapping for edges instead of cropping
    # for ii in range(data.shape[0]):
    #     for jj in range(data.shape[1]):
    #         if (ii <= bo+1 or ii >= data.shape[0]-bo-1 or \
    #             jj <= bo+1 or jj >= data.shape[1]-bo-1):
    #             diff2[ii][jj] = np.nan

    return cv2d, x, y
Exemplo n.º 12
0
def identify_objects(image_data, nsigma, min_area, deb_n_thresh, deb_cont,
                     param_dict):
    '''
    This function performs source identification and generates a segmentation map,
    which is then used for masking the sources.
    :param image_data: provide the image data, which is a mxn numpy nd array. e.g., fits.getdata('image_file_name')
    :param nsigma: source detection significance.
    :param min_area: minimum area to be considered as a source
    :param deb_n_thresh: number of threshold values for deblending routine. e.g., 32, 64 etc.
    :param deb_cont: deblend minimum contrast ratio (see source extraction or SEP python page).
    :param param_dict: a dictionary containing the
    'sep_filter_kwarg' = filter keyword argument, which can be a 'tophat', 'gauss', or 'boxcar'
    'sep_filter_size' = the 'size' of the filter. In case of gaussian, it is the FWHM of the gaussian. For tophat, it
    is the radius of the tophat filter. For boxcar, it is the side length of the 2D Box.
    :return: objects: a numpy array of the objects, ordered as per their segmentation values in the segmap.
    segmap: a segmentation map, where each source is marked with unique source identification number.
    '''

    # Note, this whole routine uses a Python-based source identification module named SEP (Barbary et al., 2016)

    # Unpack the filter keyword and its size from the parameter dictionary.
    filter_kwarg = param_dict['sep_filter_kwarg']
    filter_size = float(param_dict['sep_filter_size'])

    # Look at the SEP webpage, this is suggested for working of SEP.
    byte_swaped_data = image_data.byteswap().newbyteorder()

    # SEP estimates a global background.
    global_bkg = sep.Background(byte_swaped_data)

    # background subtracted data = original data - estimated global background.
    bkg_subtracted = byte_swaped_data - global_bkg

    # In the following block, we check for the user's choice of filter and its size.
    # We define a kernel based on their choice.
    if filter_kwarg.lower() not in ['tophat', 'gauss', 'boxcar']:
        warnings.warn(
            'The filter %s is not supported as of yet, defaulting to tophat of radius 5'
        )
        source_kernel = Tophat2DKernel(5)
    elif filter_kwarg.lower() == 'tophat':
        source_kernel = Tophat2DKernel(filter_size)
    elif filter_kwarg.lower() == 'gauss':
        _gauss_sigma = gaussian_fwhm_to_sigma(filter_size)
        source_kernel = Gaussian2DKernel(_gauss_sigma)
    elif filter_kwarg.lower() == 'boxcar':
        source_kernel = Box2DKernel(filter_size)

    # Object detection and Segmentation map generataion.
    objects, segmap = sep.extract(bkg_subtracted,
                                  nsigma,
                                  err=global_bkg.globalrms,
                                  minarea=min_area,
                                  deblend_nthresh=deb_n_thresh,
                                  deblend_cont=deb_cont,
                                  segmentation_map=True,
                                  filter_kernel=source_kernel.array)

    return objects, segmap
Exemplo n.º 13
0
def nb_cogphot(nbima, nbvar, xc, yc, maxrad=15, growthlim=1.025, plots=False):

    from photutils import CircularAperture, CircularAnnulus
    from photutils import aperture_photometry
    from astropy.stats import sigma_clipped_stats as scl
    from astropy.convolution import convolve, Box2DKernel

    rad = np.arange(1, maxrad + 1)
    phot = np.zeros_like(rad, dtype=float)
    photerr = np.zeros_like(rad, dtype=float)
    growth = np.zeros_like(rad, dtype=float)

    skyaper = CircularAnnulus((xc - 1, yc - 1),
                              r_in=maxrad,
                              r_out=np.max([1.5 * maxrad, 20]))
    skymask = skyaper.to_mask(method='center')
    skydata = skymask.multiply(nbima)[skymask.data > 0]

    bkg_avg, bkg_med, _ = scl(skydata)

    for ii, r in enumerate(rad):

        aper = CircularAperture((xc - 1, yc - 1), r=r)
        phot[ii] = (aperture_photometry(
            nbima, aper))['aperture_sum'][0] - bkg_med * aper.area
        photerr[ii] = np.sqrt((aperture_photometry(nbvar,
                                                   aper))['aperture_sum'][0])
        if ii < 3:
            growth[ii] = 100
        else:
            growth[ii] = phot[ii] / phot[ii - 1]

    rlim = np.argmin(growth > growthlim) - 1

    fluxarr = phot[rlim]
    errarr = photerr[rlim]
    radarr = rad[rlim]

    if plots:
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5))
        ax.imshow(convolve(nbima, Box2DKernel(5)),
                  vmin=-2,
                  vmax=30,
                  origin='lower')
        #ax.imshow(tmpnbima, vmin=-2, vmax=30, origin='lower')
        circ = plt.Circle((xc - 1, yc - 1),
                          rad[rlim],
                          color='r',
                          fill=False,
                          lw=3)
        ax.add_artist(circ)
        ax.set_xlim(xc - 50, xc + 50)
        ax.set_ylim(yc - 50, yc + 50)
        plt.show()

        plt.plot(rad, phot)
        plt.show()

    return fluxarr, errarr, radarr
Exemplo n.º 14
0
def smooth_iter(a, boxsize, n, lo, hi):
    a = chop_once(a, lo, hi)
    box_2D_kernel = Box2DKernel(boxsize)

    for i in range(n):
        a = convolve(a, box_2D_kernel)
        a = chop_once(a, lo, hi)
    return a
Exemplo n.º 15
0
    def mask_out_contam(self,
                        sigma=4.5,
                        deblend_cont=0.0001,
                        blowup=True,
                        show_fig=True,
                        verbose=True):
        """
        Mask out contamination in the cutout of star. Contamination may be stars, galaxies or artifacts. 
        This function uses ``sep`` to identify and mask contamination.
        ** DO THIS AFTER CENTERIZING! **

        Parameters:
            sigma (float): The sigma in ``SExtractor``. Default is 4.5.
            deblend_cont (float): Deblending parameter. Default is 0.0005.
            blowup (bool): Whether blow up the segmentation mask by convolving a 1.5 pixel Gaussian kernel.
            show_fig (bool): Whether show the figure.
            verbose (bool): Whether print out results.

        Returns:
            None
        """

        from astropy.convolution import convolve, Box2DKernel
        from .utils import extract_obj, seg_remove_cen_obj
        img_blur = convolve(abs(self.image), Box2DKernel(2))
        img_objects, img_segmap = extract_obj(abs(img_blur),
                                              b=10,
                                              f=3,
                                              sigma=sigma,
                                              minarea=1,
                                              pixel_scale=self.pixel_scale,
                                              deblend_nthresh=72,
                                              deblend_cont=deblend_cont,
                                              flux_aper=None,
                                              sky_subtract=True,
                                              show_fig=show_fig,
                                              verbose=verbose)
        # remove central object from segmap
        cen_obj = img_objects[img_segmap[img_segmap.shape[1] // 2,
                                         img_segmap.shape[0] // 2] - 1]
        img_segmap = seg_remove_cen_obj(img_segmap)
        detect_mask = (img_segmap != 0).astype(float)
        if blowup is True:
            from astropy.convolution import convolve, Gaussian2DKernel
            cv = convolve(1e3 * detect_mask / np.nansum(detect_mask),
                          Gaussian2DKernel(1.5))
            detect_mask = (cv > 0.5).astype(float)

        self.mask = detect_mask
        #imgcp = copy.copy(self.image)
        #imgcp[detect_mask.astype(bool)] = cval
        #self.image = imgcp
        # Shift mask will be very horrible!!! Hence we still don't use self.mask.
        # Instead we directly mask out on the image.

        return
Exemplo n.º 16
0
def smooth_map(map, mask, smooth_kernal, smooth_scale, nan_flag):

    map[mask==0] = nan_flag

    if smooth_kernal=='box':
        kernel = Box2DKernel(smooth_scale)
    if smooth_kernal=='tophat':
        kernel = Tophat2DKernel(smooth_scale/2)

    return convolution.convolve_fft(map, kernel, normalize_kernel=True, ignore_edge_zeros=True, interpolate_nan=True) 
Exemplo n.º 17
0
def smooth_tidal_feature(feat,box_size):
    '''
    Smoothing the input image using a Boxcar filter.
    :param feat: input image to be smoothed
    :param box_size: size of the boxcar filter
    :return: smoothed image
    '''
    from astropy.convolution import Box2DKernel, convolve #import modules
    kernel = Box2DKernel(box_size) # initiate the kernel
    convolved_feature = convolve(feat,kernel) #convolve the image with the kernel
    return convolved_feature # return the smoothed feature.
Exemplo n.º 18
0
def smooth_corr(corr, statistics, nbins=9, width=3):
    nstats = len(statistics)
    kernel = Box2DKernel(width=width)
    corr_convolved = np.zeros_like(corr)
    for i in range(nstats):
        for j in range(nstats):
            corr_sub = corr[i * nbins:(i + 1) * nbins,
                            j * nbins:(j + 1) * nbins]
            corr_sub_convolved = convolve(corr_sub, kernel, boundary='extend')
            corr_convolved[i * nbins:(i + 1) * nbins,
                           j * nbins:(j + 1) * nbins] = corr_sub_convolved
    return corr_convolved
Exemplo n.º 19
0
def kappag_map_bin(N2d, mask, fraction, z_l1, z_l2, z_s, cosmo, smooth_kernal,
                   smooth_scale):
    """
    Calculate kappa_g map for a lens redshift bin from z_l1 to z_l2 
    and a source redshift of z_s.
    """

    c_light = 3.0e5
    if smooth_kernal == 'box':
        kernel = Box2DKernel(smooth_scale)
    if smooth_kernal == 'tophat':
        kernel = Tophat2DKernel(smooth_scale / 2)

    # make 2D galaxy over-density maps ########
    if fraction == None:
        fraction = mask.copy()

    N2d = N2d * mask

    area_fraction = np.sum(fraction[mask == 1]) / len(fraction[mask == 1])

    dN2d = N2d * 0.0
    ave = np.mean(N2d[mask == 1]) / area_fraction
    dN2d[mask == 1] = (N2d[mask == 1] -
                       (ave * fraction[mask == 1])) / (ave *
                                                       fraction[mask == 1])
    # print(ave)

    # make 2D kappa_g maps ########

    zl1_cd = cd.comoving_distance(z_l1, **cosmo)  # Mpc
    zl2_cd = cd.comoving_distance(z_l2, **cosmo)  # Mpc
    zs_cd = cd.comoving_distance(z_s, **cosmo)  # Mpc
    delta_cd = zl2_cd - zl1_cd
    const = ((100. * cosmo['h'])**2 *
             cosmo['omega_M_0']) * (3 / 2.) * (1 / c_light**2)

    integ = lens_weight(np.array([z_l1, z_l2]), z_s, cosmo)[0]

    temp_dN = dN2d * 1.0
    temp_dN[mask == 0] = 'nan'
    kg = const * delta_cd * integ * convolution.convolve_fft(
        temp_dN,
        kernel,
        normalize_kernel=True,
        ignore_edge_zeros=True,
        interpolate_nan=True)
    kg[mask == 0] = 0
    return kg
Exemplo n.º 20
0
def boxcar2D_smooth(data, scale=90, mask=False):
    dims = _get_dims(data)

    box2d_kernel = Box2DKernel(scale).array
    sc_convolve = lambda data: convolve(data, box2d_kernel, mode='same')

    if mask:
        data_masked = data.where(data[mask_vars[dims]])
    else:
        data_masked = data.fillna(0.)

    return xr.apply_ufunc(sc_convolve,
                          data_masked,
                          vectorize=True,
                          dask='parallelized',
                          input_core_dims=[dims],
                          output_core_dims=[dims],
                          output_dtypes=[data.dtype])
Exemplo n.º 21
0
 def mask_contam(self,
                 method='hscmask',
                 blowup=True,
                 show_fig=True,
                 verbose=True):
     if method == 'hscmask':
         from unagi import mask
         from .image import mask_remove_cen_obj
         detect_mask = mask.Mask(
             self.hscmask,
             data_release='s18a').extract('DETECTED').astype(float)
         detect_mask = mask_remove_cen_obj(detect_mask)
         if blowup is True:
             from astropy.convolution import convolve, Gaussian2DKernel
             cv = convolve(detect_mask, Gaussian2DKernel(1.5))
             detect_mask = (cv > 0.1).astype(float)
         self.mask = detect_mask
         return
     else:  # method = 'sep'
         from astropy.convolution import convolve, Box2DKernel
         from .image import extract_obj, seg_remove_cen_obj
         img_blur = convolve(abs(self.image), Box2DKernel(2))
         img_objects, img_segmap = extract_obj(abs(img_blur),
                                               b=5,
                                               f=4,
                                               sigma=4.5,
                                               minarea=2,
                                               pixel_scale=0.168,
                                               deblend_nthresh=32,
                                               deblend_cont=0.0001,
                                               sky_subtract=False,
                                               show_fig=show_fig,
                                               verbose=verbose)
         # remove central object from segmap
         img_segmap = seg_remove_cen_obj(img_segmap)
         detect_mask = (img_segmap != 0).astype(float)
         if blowup is True:
             from astropy.convolution import convolve, Gaussian2DKernel
             cv = convolve(detect_mask, Gaussian2DKernel(1.5))
             detect_mask = (cv > 0.1).astype(float)
         self.mask = detect_mask
         return
Exemplo n.º 22
0
def smooth(data, box_width=15):
    """Create a smoothed version of the 2D input data

    Parameters
    ----------
    data : numpy.ndarray
        2D array of data to be smoothed

    box_width : int
        Width of the smoothing box, in pixels

    Returns
    -------
    smoothed : numpy.ndarray
        A smoothed version of ``data``
    """
    smoothing_kernel = Box2DKernel(box_width)
    smoothed = convolve(data, smoothing_kernel, boundary='fill', fill_value=np.nanmedian(data),
                        nan_treatment='interpolate')
    return smoothed
Exemplo n.º 23
0
def image(url,position,size):
    image_file = download_file(url, cache=True, show_progress='True')
    hdu_lists = fits.open(image_file, memmap='True')
    hdu_lists.info()
    image_data = hdu_lists[0].data
    print(
        "Above is the list of Header Data Unit of FITS file. \nWe will be working on PRIMARY Block. \nThe shape of the array is as below:")
    print((type(image_data)))
    print(image_data.shape)
    hdu_lists.close()
    print("Viewing Image..\nStatistics of the Image: ")
    plt.imshow(image_data, cmap='gray')
    plt.colorbar()
    plt.savefig('Original_Image.png')
    plt.show()
    print('Min:', np.min(image_data))
    print('Max:', np.max(image_data))
    print('Mean:', np.mean(image_data))
    print('Stdev:', np.std(image_data))
    crop = Cutout2D(image_data, position, size)
    plt.imshow(crop.data, origin='upper', cmap='gray')
    plt.savefig('Cropped_Image.png')
    plt.show()

    gauss_kernel = Gaussian2DKernel(0.5)
    smoothed_data_gauss = convolve(crop.data, gauss_kernel)
    plt.imshow(smoothed_data_gauss, cmap='gray')
    plt.savefig('Gaussian Smoothing Filter')
    plt.show()

    tophat_kernel = Tophat2DKernel(2)
    smoothed_data_tophat = convolve(crop.data, tophat_kernel)
    plt.imshow(smoothed_data_tophat, cmap='gray')
    plt.savefig('Tophat Smoothing Filter')
    plt.show()

    box_kernel = Box2DKernel(5)
    smoothed_data_box = convolve(crop.data, box_kernel)
    plt.imshow(smoothed_data_box, cmap='gray')
    plt.savefig('Box_kernel Smoothing Filter')
    plt.show()
Exemplo n.º 24
0
def kappag_map_bin_nofz2(N2d, mask, z_l1, z_l2, nofz_s, cosmo, smooth_kernal,
                         smooth_scale):
    """
    Calculate kappa_g map for a lens redshift bin with nofz_l and a 
    source redshift with nofz_s.

    nofz_s are arrays that are normalized to 1 and with 200 bins from 0 to 1.8.
    """

    c_light = 3.0e5
    if smooth_kernal == 'box':
        kernel = Box2DKernel(smooth_scale)
    if smooth_kernal == 'tophat':
        kernel = Tophat2DKernel(smooth_scale / 2)

    # make 2D galaxy over-density maps ########

    dN2d = N2d * 0.0
    ave = np.mean(N2d[mask == 1])
    dN2d[mask == 1] = (N2d[mask == 1] - ave) / ave

    # make 2D kappa_g maps ########
    const = ((100. * cosmo['h'])**2 *
             cosmo['omega_M_0']) * (3 / 2.) * (1 / c_light**2)

    integ = lens_weight_nofz2(z_l1, z_l2, nofz_s, cosmo)
    temp_dN = dN2d * 1.0
    temp_dN[mask == 0] = 'nan'

    kg = const * integ * convolution.convolve_fft(temp_dN,
                                                  kernel,
                                                  normalize_kernel=True,
                                                  ignore_edge_zeros=True,
                                                  interpolate_nan=True)

    kg[mask == 0] = 0
    return kg
Exemplo n.º 25
0
 def mask_out_contam(self, blowup=True, show_fig=True, verbose=True):
     from astropy.convolution import convolve, Box2DKernel
     from .utils import extract_obj, seg_remove_cen_obj
     img_blur = convolve(abs(self.image), Box2DKernel(2))
     img_objects, img_segmap = extract_obj(abs(img_blur),
                                           b=5,
                                           f=4,
                                           sigma=4.5,
                                           minarea=2,
                                           pixel_scale=self.pixel_scale,
                                           deblend_nthresh=32,
                                           deblend_cont=0.0005,
                                           sky_subtract=False,
                                           show_fig=show_fig,
                                           verbose=verbose)
     # remove central object from segmap
     img_segmap = seg_remove_cen_obj(img_segmap)
     detect_mask = (img_segmap != 0).astype(float)
     if blowup is True:
         from astropy.convolution import convolve, Gaussian2DKernel
         cv = convolve(detect_mask, Gaussian2DKernel(1.5))
         detect_mask = (cv > 0.1).astype(float)
     self.mask = detect_mask
     return
Exemplo n.º 26
0
    def interped(self):
        if not hasattr(self, "_interped"):
            kernel = Box2DKernel(5)  # Gaussian2DKernel(stddev=2.5) #

            crmask, _ = detect_cosmics(
                indat=np.ascontiguousarray(self.bkg_sub_img.filled(-9999)),
                inmask=self.bkg_sub_img.mask,
                sigclip=6.0,
                cleantype="medmask",
            )
            self.bkg_sub_img.mask = np.ma.mask_or(self.bkg_sub_img.mask,
                                                  crmask)
            self.bkg_sub_img.mask = np.ma.mask_or(self.bkg_sub_img.mask,
                                                  np.isnan(self.bkg_sub_img))
            img = self.bkg_sub_img.filled(np.nan)
            img_interp = interpolate_replace_nans(img, kernel, convolve=conv)

            while np.any(np.isnan(img_interp)):
                img_interp = interpolate_replace_nans(img_interp,
                                                      kernel,
                                                      convolve=conv)
            self._interped = img_interp

        return self._interped
Exemplo n.º 27
0
    def sub_bkg(self, sigma=4.5, deblend_cont=0.0001, verbose=True):
        """
        Subtract the locally-measured background of ``Star`` object. The sky is measured by masking out objects using ``sep``.
        Be cautious and be aware what you do when using this function.

        Parameters:
            sigma (float): The sigma in ``SExtractor``.
            deblend_cont (float): Deblending parameter.
            verbose (bool): Whether print out background value.
        
        Returns:
            None
        """
        # Actually this should be estimated in larger cutuouts.
        # So make another cutout (larger)!
        from astropy.convolution import convolve, Box2DKernel
        from .image import extract_obj, seg_remove_cen_obj
        from sep import Background
        img_blur = convolve(abs(self.image), Box2DKernel(2))
        img_objects, img_segmap = extract_obj(abs(img_blur),
                                              b=10,
                                              f=4,
                                              sigma=sigma,
                                              minarea=2,
                                              pixel_scale=self.pixel_scale,
                                              deblend_nthresh=32,
                                              deblend_cont=deblend_cont,
                                              sky_subtract=False,
                                              show_fig=False,
                                              verbose=False)
        bk = Background(self.image, img_segmap != 0)
        glbbck = bk.globalback
        self.globalback = glbbck
        if verbose:
            print('# Global background: ', glbbck)
        self.image -= glbbck
Exemplo n.º 28
0
def makeplot(filename,
             data,
             head,
             verbose=True,
             thumbnail=False,
             lims=None,
             dpi=1000):
    """
    Produce the plots. Will open an interactive matplotlib plot for inspection. After closing the image will be saved to a PNG.

    Args:
        filename: Name of the file (str).
        data: Astropy array of containing selected channel image.
        head: Astropy fits header.
        verbose: Verbosity (bool).
        thumbnail: EXPERIMENTAL -- Convolve the image with a 2D box and produce image (bool).
        lims: The vmin and vmax of the image.

    Returns:
        None

    """

    # Make thumbnail image - work in progress
    if thumbnail:
        if verbose:
            print('Making thumbnail')

        kernel = Box2DKernel(head['NAXIS1'] / 1000)
        astropy_conv = convolve_fft(data, kernel, allow_huge=True)
        image = np.power(astropy_conv, 2)
        if lims is None:
            rms = np.nanstd(image)
        proj = WCS(head).dropaxis(2).dropaxis(2)

        fig = plt.figure()
        fig.set_size_inches(8, 8)
        ax = fig.add_subplot(111, projection=proj)
        if lims is None:
            im = ax.imshow(image,
                           origin='lower',
                           cmap='cubehelix_r',
                           vmax=10 * rms)
        if lims is not None:
            im = ax.imshow(image,
                           origin='lower',
                           cmap='cubehelix_r',
                           vmin=lims[0],
                           vmax=lims[1])
        lon = ax.coords[0]
        lon.set_ticklabel(size=8)
        lon.set_axislabel(r'$RA$')
        lon.display_minor_ticks(True)
        lat = ax.coords[1]
        lat.set_ticklabel(size=8)
        lat.display_minor_ticks(True)
        lat.set_axislabel(r'$DEC$')
        c = plt.colorbar(im)
        c.set_label('$S^2$')
        c.ax.tick_params(length=3)
        plt.title('Thumbnail')
        plt.show()

        outfile = re.sub('.fits', '.thumbnail.png', filename)
        fig.savefig(outfile, dpi=dpi)
        if verbose:
            print('Saved to', outfile)

    # Make main image
    if verbose:
        print('Making plot')
    image = np.power(data, 2)
    if lims is None:
        rms = np.nanstd(image)
    proj = WCS(head).dropaxis(2).dropaxis(2)
    print()
    fig = plt.figure()
    fig.set_size_inches(8, 8)
    ax = fig.add_subplot(111, projection=proj)
    if lims is None:
        im = ax.imshow(image,
                       origin='lower',
                       cmap='cubehelix_r',
                       vmax=10 * rms)
    if lims is not None:
        im = ax.imshow(image,
                       origin='lower',
                       cmap='cubehelix_r',
                       vmin=lims[0],
                       vmax=lims[1])
    lon = ax.coords[0]
    lon.set_ticklabel(size=8)
    lon.set_axislabel(head['CTYPE1'])
    lon.display_minor_ticks(True)
    lat = ax.coords[1]
    lat.set_ticklabel(size=8)
    lat.display_minor_ticks(True)
    lat.set_axislabel(head['CTYPE2'])
    c = plt.colorbar(im)
    c.set_label('$S^2$')
    c.ax.tick_params(length=3)
    plt.title('Full image')
    plt.show()

    outfile = re.sub('.fits', '.medimage.png', filename)
    fig.savefig(outfile, dpi=dpi)
    if verbose:
        print('Saved to', outfile)
Exemplo n.º 29
0
# peak minimum S/N, minimum width in channels, maximum difference from SAA velocity, maximum difference from SAA FWHM, maximum separation in units FWHM
tol = [3.0, 1.0, 3.0, 2.0, 0.5]
# Spectral resolution
#specres          = 0.07

RG = False
nRG = 1.
TS = False
verb = True
fittype = 'gaussian'
njobs = 1

cube = SpectralCube.read('n2h+10_37.fits').with_spectral_unit(u.km / u.s)
# smooth and downsample for first-round fitting
# (smoothing is maybe optional..)
dscube = cube.spatial_smooth(Box2DKernel(2))[:, ::2, ::2]
dscube.write('n2h+10_37_ds.fits', overwrite=True)

# run the full scouse suite on the downsampled data
if os.path.exists(datadirectory + filename + '/stage_1/s1.scousepy'):
    s = scouse(outputdir=datadirectory,
               filename=filename,
               fittype=fittype,
               datadirectory=datadirectory)
    s.load_stage_1(datadirectory + filename + '/stage_1/s1.scousepy')
    s.load_cube(fitsfile=filename + ".fits")
else:
    s = scouse.stage_1(filename,
                       datadirectory,
                       wsaa,
                       ppv_vol=ppv_vol,
Exemplo n.º 30
0
    def smoothTaData(self, ALEXIgeodict):

        ALEXILatRes = ALEXIgeodict['ALEXI_LatRes']
        ALEXILonRes = ALEXIgeodict['ALEXI_LonRes']
        sceneID = self.sceneID
        scene = self.scene
        outFN = os.path.join(self.resultsBase, scene,
                             '%s_Ta.tif' % sceneID[:-5])
        inProj4 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
        # =======================convert fine TA to coarse resolution=========
        outfile = os.path.join(self.resultsBase, scene, 'Ta_DisALEXI.tif')

        coarseFile = os.path.join(self.resultsBase, scene, 'TaCoarse.tif')
        coarse2fineFile = os.path.join(self.resultsBase, scene,
                                       'TaCoarse2Fine.tif')

        if not os.path.exists(outFN):
            print 'get->Ta'
            # get mask from Landsat LAI
            ls = GeoTIFF(outfile)
            sceneDir = os.path.join(self.satscene_path, 'CF_MASK')
            maskFN = os.path.join(sceneDir, '%s_Mask.tif' % sceneID)
            g = gdal.Open(maskFN, GA_ReadOnly)
            cfmask = g.ReadAsArray()
            g = None
            # =============find Average Ta====================================== COMMENTED FOR TESTING
            in_ds = gdal.Open(outfile)
            coarseds = gdal.Translate(coarseFile,
                                      in_ds,
                                      options=gdal.TranslateOptions(
                                          resampleAlg='average',
                                          xRes=400,
                                          yRes=400))
            fineds = gdal.Warp(outFN,
                               coarseds,
                               options=gdal.WarpOptions(resampleAlg='average',
                                                        height=ls.nrow,
                                                        width=ls.ncol))
            coarseds = None
            # ========smooth Ta data========================================
            ta = fineds.ReadAsArray()
            fineRes = ls.Lat[1, 0] - ls.Lat[0, 0]
            coarseRes = ALEXILatRes
            course2fineRatio = coarseRes**2 / fineRes**2
            rid2 = int(np.sqrt(course2fineRatio))
            #            gauss_kernal = Gaussian2DKernel(rid2)
            box_kernal = Box2DKernel(rid2)
            ta = convolve_fft(ta, box_kernal, allow_huge=True)
            fineds.GetRasterBand(1).WriteArray(ta)
            fineds = None

            ulx = ls.ulx
            uly = ls.uly
            delx = ls.delx
            dely = -ls.dely
            fineRes = ls.Lat[1, 0] - ls.Lat[0, 0]
            coarseRes = ALEXILatRes
            inUL = [ulx, uly]
            inRes = [delx, dely]

            #            Ta = interp_ta(ta,coarseRes,fineRes)-273.16
            # Ta = ta - 273.16  # FOR TESTING!!
            Ta = ta
            outFormat = gdal.GDT_Float32
            writeArray2Tiff(Ta, inRes, inUL, ls.proj4, outFN, outFormat)
            os.remove(coarseFile)