Geom#

class gammapy.maps.Geom[source]#

Bases: ABC

Map geometry base class.

See also: WcsGeom and HpxGeom.

Attributes Summary

as_energy_true

If the geom contains an axis named 'energy' rename it to 'energy_true'.

center_coord

center_pix

center_skydir

data_shape

Shape of the Numpy data array matching this geometry.

has_energy_axis

Whether geom has an energy axis (either 'energy' or 'energy_true').

is_allsky

is_flat

Whether the geom non-spatial axes have length 1, equivalent to an image.

is_image

Whether the geom is an image without extra dimensions.

Methods Summary

contains(coords)

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

contains_pix(pix)

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

coord_to_idx(coords[, clip])

Convert map coordinates to pixel indices.

coord_to_pix(coords)

Convert map coordinates to pixel coordinates.

copy(**kwargs)

Copy and overwrite given attributes.

crop(crop_width)

Crop the geometry at the edges.

data_nbytes([dtype])

Estimate memory usage in megabytes of the Numpy data array matching this geometry depending on the given type.

downsample(factor, axis_name)

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

drop(axis_name)

Drop an axis from the geom.

energy_mask([energy_min, energy_max, ...])

Create a mask for a given energy range.

from_hdulist(hdulist[, hdu, hdu_bands])

Load a geometry object from a FITS HDUList.

get_coord([idx, flat])

Get the coordinate array for this geometry.

get_idx([idx, local, flat])

Get tuple of pixel indices for this geometry.

pad(pad_width, axis_name)

Pad the geometry at the edges.

pix_to_coord(pix)

Convert pixel coordinates to map coordinates.

pix_to_idx(pix[, clip])

Convert pixel coordinates to pixel indices.

rename_axes(names, new_names)

Rename axes contained in the geometry.

replace_axis(axis)

Replace axis with a new one.

resample_axis(axis)

Resample geom to a new axis binning.

slice_by_idx(slices)

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

solid_angle()

Solid angle as a Quantity object (in sr).

squash(axis_name)

Squash geom axis.

to_bands_hdu([hdu_bands, format])

to_cube(axes)

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

to_image()

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

upsample(factor[, axis_name])

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

Attributes Documentation

as_energy_true#

If the geom contains an axis named ‘energy’ rename it to ‘energy_true’.

center_coord#
center_pix#
center_skydir#
data_shape#

Shape of the Numpy data array matching this geometry.

has_energy_axis#

Whether geom has an energy axis (either ‘energy’ or ‘energy_true’).

is_allsky#
is_flat#

Whether the geom non-spatial axes have length 1, equivalent to an image.

is_image#

Whether the geom is an image without extra dimensions.

Methods Documentation

abstract contains(coords)[source]#

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

Parameters:
coordstuple or MapCoord

Tuple of map coordinates.

Returns:
containmentndarray

Bool array.

contains_pix(pix)[source]#

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

Parameters:
pixtuple

Tuple of pixel coordinates.

Returns:
containmentndarray

Bool array.

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

Convert map coordinates to pixel indices.

Parameters:
coordstuple 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.

clipbool

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. Default is False.

Returns:
pixtuple

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

abstract coord_to_pix(coords)[source]#

Convert map coordinates to pixel coordinates.

Parameters:
coordstuple

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:
pixtuple

Tuple of pixel coordinates in image and band dimensions.

copy(**kwargs)[source]#

Copy and overwrite given attributes.

Parameters:
**kwargsdict

Keyword arguments to overwrite in the map geometry constructor.

Returns:
copyGeom

Copied map geometry.

abstract crop(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:
geomGeom

Cropped geometry.

data_nbytes(dtype='float32')[source]#

Estimate memory usage in megabytes of the Numpy data array matching this geometry depending on the given type.

Parameters:
dtypestr, optional

The desired data-type for the array. Default is “float32”.

Returns:
memoryQuantity

Estimated memory usage in megabytes (MB).

abstract downsample(factor, axis_name)[source]#

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

Parameters:
factorint

Downsampling factor.

axis_namestr

Axis to downsample.

Returns:
geomGeom

Downsampled geometry.

drop(axis_name)[source]#

Drop an axis from the geom.

Parameters:
axis_namestr

Name of the axis to remove.

Returns:
geomGeom

New geom with the axis removed.

energy_mask(energy_min=None, energy_max=None, round_to_edge=False)[source]#

Create a mask for a given energy range.

The energy bin must be fully contained to be included in the mask.

Parameters:
energy_min, energy_maxQuantity

Energy range.

Returns:
maskMap

Map containing the energy mask. The geometry of the map is the same as the geometry of the instance which called this method.

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

Load a geometry object from a FITS HDUList.

Parameters:
hdulistHDUList

HDU list containing HDUs for map data and bands.

hdustr or int, optional

Name or index of the HDU with the map data. Default is None.

hdu_bandsstr, optional

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. Default is None.

Returns:
geomGeom

Geometry object.

abstract get_coord(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:
idxtuple, 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. Default is None.

flatbool, optional

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

Returns:
coordstuple

Tuple of coordinate vectors with one vector for each dimension.

abstract get_idx(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:
idxtuple, 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. Default is None.

localbool, optional

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

flatbool, optional

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

Returns:
idxtuple

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

pad(pad_width, axis_name)[source]#

Pad the geometry at the edges.

Parameters:
pad_width{sequence, array_like, int}

Number of values padded to the edges of each axis.

axis_namestr

Name of the axis to pad.

Returns:
geomGeom

Padded geometry.

abstract pix_to_coord(pix)[source]#

Convert pixel coordinates to map coordinates.

Parameters:
pixtuple

Tuple of pixel coordinates.

Returns:
coordstuple

Tuple of map coordinates.

abstract pix_to_idx(pix, clip=False)[source]#

Convert pixel coordinates to pixel indices.

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

Parameters:
pixtuple

Tuple of pixel coordinates.

clipbool

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. Default is False.

Returns:
idxtuple

Tuple of pixel indices.

rename_axes(names, new_names)[source]#

Rename axes contained in the geometry.

Parameters:
nameslist or str

Names of the axes.

new_nameslist or str

New names of the axes. The list must be of same length than names.

Returns:
geomGeom

Renamed geometry.

replace_axis(axis)[source]#

Replace axis with a new one.

Parameters:
axisMapAxis

New map axis.

Returns:
mapGeom

Geom with replaced axis.

resample_axis(axis)[source]#

Resample geom to a new axis binning.

This method groups the existing bins into a new binning.

Parameters:
axisMapAxis

New map axis.

Returns:
mapGeom

Geom with resampled axis.

slice_by_idx(slices)[source]#

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

Parameters:
slicesdict

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

Returns:
geomGeom

Sliced geometry.

Examples

>>> from gammapy.maps import MapAxis, WcsGeom
>>> import astropy.units as u
>>> energy_axis = MapAxis.from_energy_bounds(1*u.TeV, 3*u.TeV, 6)
>>> geom = WcsGeom.create(skydir=(83.63, 22.01), axes=[energy_axis], width=5, binsz=0.02)
>>> slices = {"energy": slice(0, 2)}
>>> sliced_geom = geom.slice_by_idx(slices)
abstract solid_angle()[source]#

Solid angle as a Quantity object (in sr).

squash(axis_name)[source]#

Squash geom axis.

Parameters:
axis_namestr

Axis to squash.

Returns:
geomGeom

Geom with squashed axis.

to_bands_hdu(hdu_bands=None, format='gadf')[source]#
abstract to_cube(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:
axeslist

Axes that will be appended to this geometry.

Returns:
geomGeom

Map geometry.

abstract to_image()[source]#

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

Returns:
geomGeom

Image geometry.

abstract upsample(factor, axis_name=None)[source]#

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

Parameters:
factorint

Upsampling factor.

axis_namestr

Axis to upsample.

Returns:
geomGeom

Upsampled geometry.