MapGeom

class gammapy.maps.MapGeom[source]

Bases: abc.ABC

Base class for WCS and HEALPix geometries.

Attributes Summary

center_coord
center_pix
center_skydir
data_shape Shape of the Numpy data array matching this geometry.
is_allsky
is_image Whether the geom is equivalent to an image without extra dimensions.

Methods Summary

contains(self, coords) Check if a given map coordinate is contained in the geometry.
contains_pix(self, pix) Check if a given pixel coordinate is contained in the geometry.
coord_to_idx(self, coords[, clip]) Convert map coordinates to pixel indices.
coord_to_pix(self, coords) Convert map coordinates to pixel coordinates.
coord_to_tuple(self, coord) Generate a coordinate tuple compatible with this geometry.
copy(self, \*\*kwargs) Copy MapGeom instance and overwrite given attributes.
crop(self, crop_width) Crop the geometry at the edges.
downsample(self, factor, axis) Downsample the spatial dimension of the geometry by a given factor.
energy_mask(self[, emin, emax]) Create a mask for a given energy range.
from_hdulist(hdulist[, hdu, hdu_bands]) Load a geometry object from a FITS HDUList.
get_axis_by_name(self, name) Get an axis by name (case in-sensitive).
get_axis_index_by_name(self, name) Get an axis index by name (case in-sensitive).
get_coord(self[, idx, flat]) Get the coordinate array for this geometry.
get_idx(self[, idx, local, flat]) Get tuple of pixel indices for this geometry.
make_bands_hdu(self[, hdu, hdu_skymap, conv])
pad(self, pad_width) Pad the geometry at the edges.
pix_to_coord(self, pix) Convert pixel coordinates to map coordinates.
pix_to_idx(self, pix[, clip]) Convert pixel coordinates to pixel indices.
slice_by_idx(self, slices) Create a new geometry by slicing the non-spatial axes.
solid_angle(self) Solid angle (Quantity in sr).
to_cube(self, axes) Append non-spatial axes to create a higher-dimensional geometry.
to_image(self) Create 2D image geometry (drop non-spatial dimensions).
upsample(self, factor, axis) Upsample the spatial dimension of the geometry by a given factor.

Attributes Documentation

center_coord
center_pix
center_skydir
data_shape

Shape of the Numpy data array matching this geometry.

is_allsky
is_image

Whether the geom is equivalent to an image without extra dimensions.

Methods Documentation

contains(self, coords)[source]

Check if a given map coordinate is contained in the geometry.

Parameters:
coords : tuple or MapCoord

Tuple of map coordinates.

Returns:
containment : ndarray

Bool array.

contains_pix(self, pix)[source]

Check if a given pixel coordinate is contained in the geometry.

Parameters:
pix : tuple

Tuple of pixel coordinates.

Returns:
containment : ndarray

Bool array.

coord_to_idx(self, coords, clip=False)[source]

Convert map coordinates to pixel indices.

Parameters:
coords : tuple or MapCoord

Coordinate values in each dimension of the map. This can either be a tuple of numpy arrays or a MapCoord object. If passed as a tuple then the ordering should be (longitude, latitude, c_0, …, c_N) where c_i is the coordinate vector for axis i.

clip : bool

Choose whether to clip indices to the valid range of the geometry. If false then indices for coordinates outside the geometry range will be set -1.

Returns:
pix : tuple

Tuple of pixel indices in image and band dimensions. Elements set to -1 correspond to coordinates outside the map.

coord_to_pix(self, coords)[source]

Convert map coordinates to pixel coordinates.

Parameters:
coords : tuple

Coordinate values in each dimension of the map. This can either be a tuple of numpy arrays or a MapCoord object. If passed as a tuple then the ordering should be (longitude, latitude, c_0, …, c_N) where c_i is the coordinate vector for axis i.

Returns:
pix : tuple

Tuple of pixel coordinates in image and band dimensions.

coord_to_tuple(self, coord)[source]

Generate a coordinate tuple compatible with this geometry.

Parameters:
coord : MapCoord
copy(self, **kwargs)[source]

Copy MapGeom instance and overwrite given attributes.

Parameters:
**kwargs : dict

Keyword arguments to overwrite in the map geometry constructor.

Returns:
copy : MapGeom

Copied map geometry.

crop(self, crop_width)[source]

Crop the geometry at the edges.

Parameters:
crop_width : {sequence, array_like, int}

Number of values cropped from the edges of each axis.

Returns:
geom : MapGeom

Cropped geometry.

downsample(self, factor, axis)[source]

Downsample the spatial dimension of the geometry by a given factor.

Parameters:
factor : int

Downsampling factor.

axis : str

Axis to downsample.

Returns:
geom : MapGeom

Downsampled geometry.

energy_mask(self, emin=None, emax=None)[source]

Create a mask for a given energy range.

Parameters:
emin, emax : Quantity

Energy range

classmethod from_hdulist(hdulist, hdu=None, hdu_bands=None)[source]

Load a geometry object from a FITS HDUList.

Parameters:
hdulist : HDUList

HDU list containing HDUs for map data and bands.

hdu : str

Name or index of the HDU with the map data.

hdu_bands : str

Name or index of the HDU with the BANDS table. If not defined this will be inferred from the FITS header of the map HDU.

Returns:
geom : MapGeom

Geometry object.

get_axis_by_name(self, name)[source]

Get an axis by name (case in-sensitive).

Parameters:
name : str

Name of the requested axis

Returns:
axis : MapAxis

Axis

get_axis_index_by_name(self, name)[source]

Get an axis index by name (case in-sensitive).

Parameters:
name : str

Axis name

Returns:
index : int

Axis index

get_coord(self, idx=None, flat=False)[source]

Get the coordinate array for this geometry.

Returns a coordinate array with the same shape as the data array. Pixels outside the geometry are set to NaN. Coordinates for a single image plane can be accessed by setting idx to the index tuple of a plane.

Parameters:
idx : tuple, optional

A tuple of indices with one index for each non-spatial dimension. If defined only coordinates for the image plane with this index will be returned. If none then coordinates for all pixels will be returned.

flat : bool, optional

Return a flattened array containing only coordinates for pixels contained in the geometry.

Returns:
coords : tuple

Tuple of coordinate vectors with one vector for each dimension.

get_idx(self, idx=None, local=False, flat=False)[source]

Get tuple of pixel indices for this geometry.

Returns all pixels in the geometry by default. Pixel indices for a single image plane can be accessed by setting idx to the index tuple of a plane.

Parameters:
idx : tuple, optional

A tuple of indices with one index for each non-spatial dimension. If defined only pixels for the image plane with this index will be returned. If none then all pixels will be returned.

local : bool

Flag to return local or global pixel indices. Local indices run from 0 to the number of pixels in a given image plane.

flat : bool, optional

Return a flattened array containing only indices for pixels contained in the geometry.

Returns:
idx : tuple

Tuple of pixel index vectors with one vector for each dimension.

make_bands_hdu(self, hdu=None, hdu_skymap=None, conv=None)[source]
pad(self, pad_width)[source]

Pad the geometry at the edges.

Parameters:
pad_width : {sequence, array_like, int}

Number of values padded to the edges of each axis.

Returns:
geom : MapGeom

Padded geometry.

pix_to_coord(self, pix)[source]

Convert pixel coordinates to map coordinates.

Parameters:
pix : tuple

Tuple of pixel coordinates.

Returns:
coords : tuple

Tuple of map coordinates.

pix_to_idx(self, pix, clip=False)[source]

Convert pixel coordinates to pixel indices.

Returns -1 for pixel coordinates that lie outside of the map.

Parameters:
pix : tuple

Tuple of pixel coordinates.

clip : bool

Choose whether to clip indices to the valid range of the geometry. If false then indices for coordinates outside the geometry range will be set -1.

Returns:
idx : tuple

Tuple of pixel indices.

slice_by_idx(self, slices)[source]

Create a new geometry by slicing the non-spatial axes.

Parameters:
slices : dict

Dict of axes names and integers or slice object pairs. Contains one element for each non-spatial dimension. For integer indexing the correspoding axes is dropped from the map. Axes not specified in the dict are kept unchanged.

Returns:
geom : MapGeom

Sliced geometry.

solid_angle(self)[source]

Solid angle (Quantity in sr).

to_cube(self, axes)[source]

Append non-spatial axes to create a higher-dimensional geometry.

This will result in a new geometry with N+M dimensions where N is the number of current dimensions and M is the number of axes in the list.

Parameters:
axes : list

Axes that will be appended to this geometry.

Returns:
geom : MapGeom

Map geometry.

to_image(self)[source]

Create 2D image geometry (drop non-spatial dimensions).

Returns:
geom : MapGeom

Image geometry.

upsample(self, factor, axis)[source]

Upsample the spatial dimension of the geometry by a given factor.

Parameters:
factor : int

Upsampling factor.

axis : str

Axis to upsample.

Returns:
geom : MapGeom

Upsampled geometry.