Beispiel #1
0
    def kernel(self, pixel_size, offset_max=None,
               discretize_model_kwargs=dict(factor=10)):
        """Make a 2-dimensional kernel image.

        The kernel image is evaluated on a cartesian
        grid with ``pixel_size`` spacing, not on the sphere.

        Calls `astropy.convolution.discretize_model`,
        allowing for accurate discretization.

        Parameters
        ----------
        pixel_size : `~astropy.coordinates.Angle` or `~astropy.units.Quantity`
            Kernel pixel size
        discretize_model_kwargs : dict
            Keyword arguments passed to
            `astropy.convolution.discretize_model`

        Returns
        -------
        kernel : `numpy.array`
            Kernel 2D image

        Notes
        -----
        * In the future, `astropy.modeling.Fittable2DModel` and
          `astropy.convolution.Model2DKernel` could be used to construct
          the kernel.
        """
        if not isinstance(pixel_size, Quantity):
            raise ValueError("pixel_size must be an Angle or Quantity object.")

        if offset_max == None:
            offset_max = self._offset.max()

        def _model(x, y):
            """Model in the appropriate format for discretize_model."""
            offset = np.sqrt(x * x + y * y) * pixel_size
            return self.eval(offset)

        npix = int(offset_max.radian / pixel_size.radian)
        pix_range = (-npix, npix + 1)

        # FIXME: Using `discretize_model` is currently very cumbersome due to these issue:
        # https://github.com/astropy/astropy/issues/2274
        # https://github.com/astropy/astropy/issues/1763#issuecomment-39552900
        #from astropy.modeling import Fittable2DModel
        #
        #class TempModel(Fittable2DModel):
        #    @staticmethod
        #    def eval(x, y):
        #        return 42 temp_model_function(x, y)
        #
        #temp_model = TempModel()

        #import IPython; IPython.embed()
        array = discretize_oversample_2D(_model,
                                         x_range=pix_range, y_range=pix_range,
                                         **discretize_model_kwargs)
        return array
Beispiel #2
0
    def kernel(self, pixel_size, offset_max=None, normalize=True,
               discretize_model_kwargs=dict(factor=1)):
        """Make a 2-dimensional kernel image.

        The kernel image is evaluated on a cartesian
        grid with ``pixel_size`` spacing, not on the sphere.

        Calls `astropy.convolution.discretize_model`,
        allowing for accurate discretization.

        Parameters
        ----------
        pixel_size : `~astropy.coordinates.Angle`
            Kernel pixel size
        discretize_model_kwargs : dict
            Keyword arguments passed to
            `astropy.convolution.discretize_model`

        Returns
        -------
        kernel : `~astropy.units.Quantity`
            Kernel 2D image of Quantities

        Notes
        -----
        * In the future, `astropy.modeling.Fittable2DModel` and
          `astropy.convolution.Model2DKernel` could be used to construct
          the kernel.
        """
        if offset_max is None:
            offset_max = self._offset.max()

        def _model(x, y):
            """Model in the appropriate format for discretize_model."""
            offset = np.sqrt(x * x + y * y) * pixel_size
            return self.evaluate(offset)

        npix = int(offset_max.radian / pixel_size.radian)
        pix_range = (-npix, npix + 1)

        # FIXME: Using `discretize_model` is currently very cumbersome due to these issue:
        # https://github.com/astropy/astropy/issues/2274
        # https://github.com/astropy/astropy/issues/1763#issuecomment-39552900
        # from astropy.modeling import Fittable2DModel
        #
        # class TempModel(Fittable2DModel):
        #    @staticmethod
        #    def evaluate(x, y):
        #        return 42 temp_model_function(x, y)
        #
        # temp_model = TempModel()

        array = discretize_oversample_2D(_model,
                                         x_range=pix_range, y_range=pix_range,
                                         **discretize_model_kwargs)
        if normalize:
            return array / array.value.sum()
        else:
            return array
Beispiel #3
0
    def kernel(self,
               reference,
               containment=0.99,
               normalize=True,
               discretize_model_kwargs=dict(factor=10)):
        """
        Make a 2-dimensional kernel image.

        The kernel image is evaluated on a cartesian grid defined by the
        reference sky image.

        Parameters
        ----------
        reference : `~gammapy.image.SkyImage` or `~gammapy.cube.SkyCube`
            Reference sky image or sky cube defining the spatial grid.
        containment : float
            Minimal containment fraction of the kernel image.
        normalize : bool
            Whether to normalize the kernel.

        Returns
        -------
        kernel : `~astropy.units.Quantity`
            Kernel 2D image of Quantities

        """
        from ..cube import SkyCube
        rad_max = self.containment_radius(containment)

        if isinstance(reference, SkyCube):
            reference = reference.sky_image_ref

        pixel_size = reference.wcs_pixel_scale()[0]

        def _model(x, y):
            """Model in the appropriate format for discretize_model."""
            rad = np.sqrt(x * x + y * y) * pixel_size
            return self.evaluate(rad)

        npix = int(rad_max.radian / pixel_size.radian)
        pix_range = (-npix, npix + 1)

        kernel = discretize_oversample_2D(_model,
                                          x_range=pix_range,
                                          y_range=pix_range,
                                          **discretize_model_kwargs)
        if normalize:
            kernel = kernel / kernel.sum()

        return kernel
Beispiel #4
0
    def kernel(self, reference, containment=0.99, normalize=True,
               discretize_model_kwargs=dict(factor=10)):
        """
        Make a 2-dimensional kernel image.

        The kernel image is evaluated on a cartesian grid defined by the
        reference sky image.

        Parameters
        ----------
        reference : `~gammapy.image.SkyImage` or `~gammapy.cube.SkyCube`
            Reference sky image or sky cube defining the spatial grid.
        containment : float
            Minimal containment fraction of the kernel image.
        normalize : bool
            Whether to normalize the kernel.

        Returns
        -------
        kernel : `~astropy.units.Quantity`
            Kernel 2D image of Quantities

        """
        from ..cube import SkyCube
        offset_max = self.containment_radius(containment)

        if isinstance(reference, SkyCube):
            reference = reference.sky_image_ref

        pixel_size = reference.wcs_pixel_scale()[0]

        def _model(x, y):
            """Model in the appropriate format for discretize_model."""
            offset = np.sqrt(x * x + y * y) * pixel_size
            return self.evaluate(offset)

        npix = int(offset_max.radian / pixel_size.radian)
        pix_range = (-npix, npix + 1)

        kernel = discretize_oversample_2D(_model, x_range=pix_range, y_range=pix_range,
                                          **discretize_model_kwargs)
        if normalize:
            return kernel / kernel.sum()
        else:
            return kernel