MapAxis

class gammapy.maps.MapAxis(nodes, interp='lin', name='', node_type='edges', unit='')[source]

Bases: object

Class representing an axis of a map.

Provides methods for transforming to/from axis and pixel coordinates. An axis is defined by a sequence of node values that lie at the center of each bin. The pixel coordinate at each node is equal to its index in the node array (0, 1, ..). Bin edges are offset by 0.5 in pixel coordinates from the nodes such that the lower/upper edge of the first bin is (-0.5,0.5).

Parameters:
nodes : ndarray or Quantity

Array of node values. These will be interpreted as either bin edges or centers according to node_type.

interp : str

Interpolation method used to transform between axis and pixel coordinates. Valid options are ‘log’, ‘lin’, and ‘sqrt’.

name : str

Axis name

node_type : str

Flag indicating whether coordinate nodes correspond to pixel edges (node_type = ‘edge’) or pixel centers (node_type = ‘center’). ‘center’ should be used where the map values are defined at a specific coordinate (e.g. differential quantities). ‘edge’ should be used where map values are defined by an integral over coordinate intervals (e.g. a counts histogram).

unit : str

String specifying the data units.

Attributes Summary

center Return array of bin centers.
edges Return array of bin edges.
interp Interpolation scale of the axis.
name Name of the axis.
nbin Return number of bins.
node_type Return node type (‘center’ or ‘edge’).
unit Return coordinate axis unit.

Methods Summary

coord_to_idx(self, coord[, clip]) Transform from axis coordinate to bin index.
coord_to_pix(self, coord) Transform from axis to pixel coordinates.
copy(self, \*\*kwargs) Copy MapAxis instance and overwrite given attributes.
downsample(self, factor) Downsample map axis by a given factor.
from_bounds(lo_bnd, hi_bnd, nbin, \*\*kwargs) Generate an axis object from a lower/upper bound and number of bins.
from_edges(edges, \*\*kwargs) Generate an axis object from a sequence of bin edges.
from_nodes(nodes, \*\*kwargs) Generate an axis object from a sequence of nodes (bin centers).
group_table(self, edges) Compute bin groups table for the map axis, given coarser bin edges.
pix_to_coord(self, pix) Transform from pixel to axis coordinates.
slice(self, idx) Create a new axis object by extracting a slice from this axis.
squash(self) Create a new axis object by squashing the axis into one bin.
upsample(self, factor) Upsample map axis by a given factor.

Attributes Documentation

center

Return array of bin centers.

edges

Return array of bin edges.

interp

Interpolation scale of the axis.

name

Name of the axis.

nbin

Return number of bins.

node_type

Return node type (‘center’ or ‘edge’).

unit

Return coordinate axis unit.

Methods Documentation

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

Transform from axis coordinate to bin index.

Parameters:
coord : ndarray

Array of axis coordinate values.

clip : bool

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

Returns:
idx : ndarray

Array of bin indices.

coord_to_pix(self, coord)[source]

Transform from axis to pixel coordinates.

Parameters:
coord : ndarray

Array of axis coordinate values.

Returns:
pix : ndarray

Array of pixel coordinate values.

copy(self, **kwargs)[source]

Copy MapAxis instance and overwrite given attributes.

Parameters:
**kwargs : dict

Keyword arguments to overwrite in the map axis constructor.

Returns:
copy : MapAxis

Copied map axis.

downsample(self, factor)[source]

Downsample map axis by a given factor.

Parameters:
factor : int

Downsampling factor.

Returns:
axis : MapAxis

Downsampled map axis.

classmethod from_bounds(lo_bnd, hi_bnd, nbin, **kwargs)[source]

Generate an axis object from a lower/upper bound and number of bins.

If node_type = ‘edge’ then bounds correspond to the lower and upper bound of the first and last bin. If node_type = ‘center’ then bounds correspond to the centers of the first and last bin.

Parameters:
lo_bnd : float

Lower bound of first axis bin.

hi_bnd : float

Upper bound of last axis bin.

nbin : int

Number of bins.

interp : {‘lin’, ‘log’, ‘sqrt’}

Interpolation method used to transform between axis and pixel coordinates. Default: ‘lin’.

classmethod from_edges(edges, **kwargs)[source]

Generate an axis object from a sequence of bin edges.

This method should be used to construct an axis where the bin edges should lie at specific values (e.g. a histogram). The number of bins will be one less than the number of edges.

Parameters:
edges : ndarray

Axis bin edges.

interp : {‘lin’, ‘log’, ‘sqrt’}

Interpolation method used to transform between axis and pixel coordinates. Default: ‘lin’.

classmethod from_nodes(nodes, **kwargs)[source]

Generate an axis object from a sequence of nodes (bin centers).

This will create a sequence of bins with edges half-way between the node values. This method should be used to construct an axis where the bin center should lie at a specific value (e.g. a map of a continuous function).

Parameters:
nodes : ndarray

Axis nodes (bin center).

interp : {‘lin’, ‘log’, ‘sqrt’}

Interpolation method used to transform between axis and pixel coordinates. Default: ‘lin’.

group_table(self, edges)[source]

Compute bin groups table for the map axis, given coarser bin edges.

Parameters:
edges : Quantity

Group bin edges.

Returns:
groups : Table

Map axis group table.

pix_to_coord(self, pix)[source]

Transform from pixel to axis coordinates.

Parameters:
pix : ndarray

Array of pixel coordinate values.

Returns:
coord : ndarray

Array of axis coordinate values.

slice(self, idx)[source]

Create a new axis object by extracting a slice from this axis.

Parameters:
idx : slice

Slice object selecting a subselection of the axis.

Returns:
axis : MapAxis

Sliced axis object.

squash(self)[source]

Create a new axis object by squashing the axis into one bin.

Returns:
axis : MapAxis

Sliced axis object.

upsample(self, factor)[source]

Upsample map axis by a given factor.

Parameters:
factor : int

Upsampling factor.

Returns:
axis : MapAxis

Usampled map axis.