MapAxes#

class gammapy.maps.MapAxes(axes, n_spatial_axes=None)[source]#

Bases: Sequence

MapAxis container class.

Parameters:
axeslist of MapAxis

List of map axis objects.

Attributes Summary

center_coord

Center coordinates.

is_flat

Whether axes is flat.

is_unidimensional

Whether axes is unidimensional.

iter_with_reshape

Generator that iterates over axes and their shape.

names

Names of the axes.

primary_axis

Primary extra axis, defined as the longest one.

reverse

Reverse axes order.

shape

Shapes of the axes.

Methods Summary

assert_names(required_names[, allow_extra])

Assert required axis names and order.

bin_volume()

Bin axes volume.

coord_to_idx(coord[, clip])

Transform from axis to pixel indices.

coord_to_pix(coord)

Transform from axis to pixel coordinates.

copy()

Initialize a new map axes instance by copying each axis.

count(value)

downsample(factor, axis_name)

Downsample axis by a given factor.

drop(axis_name)

Drop an axis.

from_default(axes[, n_spatial_axes])

Make a sequence of MapAxis objects.

from_table(table[, format])

Create MapAxes from table.

from_table_hdu(hdu[, format])

Create MapAxes from BinTableHDU.

get_coord([mode, axis_name])

Get axes coordinates.

index(axis_name)

Get index in list.

index_data(axis_name)

Get data index of the axes.

is_allclose(other, **kwargs)

Check if other map axes are all close.

pad(axis_name, pad_width)

Pad axis.

pix_to_coord(pix)

Convert pixel coordinates to map coordinates.

pix_to_idx(pix[, clip])

Convert pixel to pixel indices.

rename_axes(names, new_names)

Rename the axes.

replace(axis)

Replace a given axis.

resample(axis)

Resample axis binning.

slice_by_idx(slices)

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

squash(axis_name)

Squash axis.

to_header([format])

Convert axes to FITS header.

to_table([format])

Convert axes to table.

to_table_hdu([format, hdu_bands])

Make FITS table columns for map axes.

upsample(factor, axis_name)

Upsample axis by a given factor.

Attributes Documentation

center_coord#

Center coordinates.

is_flat#

Whether axes is flat.

is_unidimensional#

Whether axes is unidimensional.

iter_with_reshape#

Generator that iterates over axes and their shape.

names#

Names of the axes.

primary_axis#

Primary extra axis, defined as the longest one.

Returns:
axisMapAxis

Map axis.

reverse#

Reverse axes order.

shape#

Shapes of the axes.

Methods Documentation

assert_names(required_names, allow_extra=False)[source]#

Assert required axis names and order.

Parameters:
required_nameslist of str

Required names.

allow_extrabool

Allow extra axes beyond required ones.

bin_volume()[source]#

Bin axes volume.

Returns:
bin_volumeQuantity

Bin volume.

coord_to_idx(coord, clip=True)[source]#

Transform from axis to pixel indices.

Parameters:
coorddict of ndarray or MapCoord

Array of axis coordinate values.

clipbool, optional

Choose whether to clip indices to the valid range of the axis. Default is True. If False, then indices for coordinates outside the axis range will be set to -1.

Returns:
pixtuple of ndarray

Array of pixel indices values.

coord_to_pix(coord)[source]#

Transform from axis to pixel coordinates.

Parameters:
coorddict of ndarray

Array of axis coordinate values.

Returns:
pixtuple of ndarray

Array of pixel coordinate values.

copy()[source]#

Initialize a new map axes instance by copying each axis.

count(value) integer -- return number of occurrences of value#
downsample(factor, axis_name)[source]#

Downsample axis by a given factor.

Parameters:
factorint

Downsampling factor.

axis_namestr

Axis to downsample.

Returns:
axesMapAxes

Map axes.

drop(axis_name)[source]#

Drop an axis.

Parameters:
axis_namestr

Name of the axis to remove.

Returns:
axesMapAxes

Axes without the axis_name.

classmethod from_default(axes, n_spatial_axes=None)[source]#

Make a sequence of MapAxis objects.

Parameters:
axeslist of MapAxis or ndarray

Sequence of axis or edges defining the axes.

n_spatial_axesint, optional

Number of spatial axes. Default is None.

Returns:
axesMapAxes

Map axes object.

classmethod from_table(table, format='gadf')[source]#

Create MapAxes from table.

Parameters:
tableTable

Bin table HDU.

format{“gadf”, “gadf-dl3”, “fgst-ccube”, “fgst-template”, “fgst-bexcube”, “ogip-arf”}

Format to use. Default is “gadf”.

Returns:
axesMapAxes

Map axes object.

classmethod from_table_hdu(hdu, format='gadf')[source]#

Create MapAxes from BinTableHDU.

Parameters:
hduBinTableHDU

Bin table HDU.

format{“gadf”, “gadf-dl3”, “fgst-ccube”, “fgst-template”, “fgst-bexcube”, “ogip-arf”}

Format to use. Default is “gadf”.

Returns:
axesMapAxes

Map axes object.

get_coord(mode='center', axis_name=None)[source]#

Get axes coordinates.

Parameters:
mode{“center”, “edges”}

Coordinate center or edges. Default is “center”.

axis_namestr, optional

Axis name for which mode=’edges’ applies. Default is None.

Returns:
coordsdict of Quantity.

Map coordinates as a dictionary.

index(axis_name)[source]#

Get index in list.

index_data(axis_name)[source]#

Get data index of the axes.

Parameters:
axis_namestr

Name of the axis.

Returns:
idxint

Data index.

is_allclose(other, **kwargs)[source]#

Check if other map axes are all close.

Parameters:
otherMapAxes

Other map axes.

**kwargsdict, optional

Keyword arguments forwarded to is_allclose

Returns:
is_allclosebool

Whether other axes are all close.

pad(axis_name, pad_width)[source]#

Pad axis.

Parameters:
axis_namestr

Name of the axis to pad.

pad_widthint or tuple of int

Pad width.

Returns:
axesMapAxes

Axes with squashed axis.

pix_to_coord(pix)[source]#

Convert pixel coordinates to map coordinates.

Parameters:
pixtuple

Tuple of pixel coordinates.

Returns:
coordstuple

Tuple of map coordinates.

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

Convert pixel to pixel indices.

Parameters:
pixtuple of ndarray

Pixel coordinates.

clipbool, optional

Choose whether to clip indices to the valid range of the axis. Default is False. If False, then indices for coordinates outside the axi range will be set to -1.

Returns:
idxtuple ndarray

Pixel indices.

rename_axes(names, new_names)[source]#

Rename the axes.

Parameters:
namesstr or list of str

Names of the axis.

new_namesstr or list of str

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

Returns:
axesMapAxes

Renamed Map axes object.

replace(axis)[source]#

Replace a given axis. In order to be replaced, the name of the new axis must match the name of the old axis.

Parameters:
axisMapAxis

Map axis.

Returns:
axesMapAxes

Map axes.

resample(axis)[source]#

Resample axis binning.

This method groups the existing bins into a new binning.

Parameters:
axisMapAxis

New map axis.

Returns:
axesMapAxes

Axes object 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 dictionary are kept unchanged.

Returns:
axesMapAxes

Sliced axes.

Examples

>>> import astropy.units as u
>>> from astropy.time import Time
>>> from gammapy.maps import MapAxis, MapAxes, TimeMapAxis
>>> energy_axis = MapAxis.from_energy_bounds(1*u.TeV, 3*u.TeV, 6)
>>> time_ref = Time("1999-01-01T00:00:00.123456789")
>>> time_axis = TimeMapAxis(
...     edges_min=[0, 1, 3] * u.d,
...     edges_max=[0.8, 1.9, 5.4] * u.d,
...     reference_time=time_ref,
... )
>>> axes = MapAxes([energy_axis, time_axis])
>>> slices = {"energy": slice(0, 3), "time": slice(0, 1)}
>>> sliced_axes = axes.slice_by_idx(slices)
squash(axis_name)[source]#

Squash axis.

Parameters:
axis_namestr

Axis to squash.

Returns:
axesMapAxes

Axes with squashed axis.

to_header(format='gadf')[source]#

Convert axes to FITS header.

Parameters:
format{“gadf”}

Header format. Default is “gadf”.

Returns:
headerHeader

FITS header.

to_table(format='gadf')[source]#

Convert axes to table.

Parameters:
format{“gadf”, “gadf-dl3”, “fgst-ccube”, “fgst-template”, “ogip”, “ogip-sherpa”, “ogip-arf”, “ogip-arf-sherpa”} # noqa E501

Format to use. Default is “gadf”.

Returns:
tableTable

Table with axis data.

to_table_hdu(format='gadf', hdu_bands=None)[source]#

Make FITS table columns for map axes.

Parameters:
format{“gadf”, “fgst-ccube”, “fgst-template”}

Format to use. Default is “gadf”.

hdu_bandsstr, optional

Name of the bands HDU to use. Default is None.

Returns:
hduBinTableHDU

Bin table HDU.

upsample(factor, axis_name)[source]#

Upsample axis by a given factor.

Parameters:
factorint

Upsampling factor.

axis_namestr

Axis to upsample.

Returns:
axesMapAxes

Map axes.