RegionNDMap#

class gammapy.maps.RegionNDMap[source]#

Bases: Map

N-dimensional region map.

A RegionNDMap owns a RegionGeom instance as well as a data array containing the values associated to that region in the sky along the non-spatial axis, usually an energy axis. The spatial dimensions of a RegionNDMap are reduced to a single spatial bin with an arbitrary shape, and any extra dimensions are described by an arbitrary number of non-spatial axes.

Parameters:
geomRegionGeom

Region geometry object.

datandarray

Data array. If None then an empty array will be allocated.

dtypestr, optional

Data type. Default is “float32”.

metadict, optional

Dictionary to store metadata. Default is None.

unitstr or Unit, optional

The map unit. Default is “”.

Notes

Attributes Summary

data

Data array as a ndarray object.

Methods Summary

create(region[, axes, dtype, meta, unit, ...])

Create an empty region map object.

crop()

Crop the spatial dimensions of the map.

cutout(*args, **kwargs)

Return self.

downsample(factor[, preserve_counts, ...])

Downsample the non-spatial dimension by a given factor.

fill_by_idx(idx[, weights])

Fill pixels at idx with given weights.

from_hdulist(hdulist[, format, ogip_column, hdu])

Create from HDUList.

from_table(table[, format, colname])

Create region map from table.

get_by_idx(idxs)

Return map values at the given pixel indices.

get_spectrum(*args, **kwargs)

Return self.

interp_by_coord(coords, **kwargs)

Interpolate map values at the given map coordinates.

interp_by_pix(pix, **kwargs)

Interpolate map values at the given pixel coordinates.

iter_by_axis_data(axis_name)

Iterate data by axis.

plot([ax, axis_name])

Plot the data contained in region map along the non-spatial axis.

plot_hist([ax])

Plot as histogram.

plot_interactive()

Plot map with interactive widgets to explore the non-spatial axes.

plot_mask([ax])

Plot the mask as a shaded area in a xmin-xmax range.

plot_region([ax])

Plot region.

read(filename[, format, ogip_column, hdu, ...])

Read from file.

set_by_idx(idx, value)

Set pixels at idx with given vals.

stack(other[, weights, nan_to_num])

Stack other region map into map.

to_hdulist([format, hdu, hdu_bands, hdu_region])

Convert to HDUList.

to_region_nd_map(*args, **kwargs)

Return self.

to_table([format])

Convert to Table.

upsample(factor[, order, preserve_counts, ...])

Upsample the non-spatial dimension by a given factor.

write(filename[, overwrite, format, hdu, ...])

Write map to file.

Attributes Documentation

data#

Methods Documentation

classmethod create(region, axes=None, dtype='float32', meta=None, unit='', wcs=None, binsz_wcs='0.1 deg', data=None)[source]#

Create an empty region map object.

Parameters:
regionstr or SkyRegion

Region specification.

axeslist of MapAxis, optional

Non-spatial axes. Default is None.

dtypestr, optional

Data type. Default is ‘float32’.

unitstr or Unit, optional

Data unit. Default is “”.

metadict, optional

Dictionary to store metadata. Default is None.

wcsWCS, optional

WCS projection to use for local projections of the region. Default is None.

binsz_wcs: `~astropy.units.Quantity` or str, optional

Bin size used for the default WCS, if wcs=None. Default is “0.1 deg”.

datandarray, optional

Data array. Default is None.

Returns:
mapRegionNDMap

Region map.

crop()[source]#

Crop the spatial dimensions of the map.

Parameters:
crop_width{sequence, array_like, int}

Number of pixels cropped from the edges of each axis. Defined analogously to pad_with from numpy.pad.

Returns:
mapMap

Cropped map.

cutout(*args, **kwargs)[source]#

Return self.

downsample(factor, preserve_counts=True, axis_name=None, weights=None)[source]#

Downsample the non-spatial dimension by a given factor.

By default, the first axes is downsampled.

Parameters:
factorint

Downsampling factor.

preserve_countsbool, optional

Preserve the integral over each bin. This should be true if the map is an integral quantity (e.g. counts) and false if the map is a differential quantity (e.g. intensity). Default is True.

axis_namestr, optional

Which axis to downsample. Default is “energy”.

weightsRegionNDMap, optional

Contains the weights to apply to the axis to reduce. Default weights of one.

Returns:
mapRegionNDMap

Downsampled region map.

fill_by_idx(idx, weights=None)[source]#

Fill pixels at idx with given weights.

Parameters:
idxtuple

Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps.

weightsndarray, optional

Weights vector. If None, weights are set to 1. Default is None.

classmethod from_hdulist(hdulist, format='gadf', ogip_column=None, hdu=None, **kwargs)[source]#

Create from HDUList.

Parameters:
hdulistHDUList

HDU list.

format{“gadf”, “ogip”, “ogip-arf”}

Format specification. Default is “gadf”.

ogip_column{“COUNTS”, “QUALITY”, “BACKSCAL”}, optional

If format ‘ogip’ is chosen which table HDU column to read. Default is None.

hdustr, optional

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

Returns:
region_nd_mapRegionNDMap

Region map.

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

Create region map from table.

Parameters:
tableTable

Table with input data.

format{“gadf-sed”, “lightcurve”, “profile”}

Format to use.

colnamestr

Column name to take the data from.

Returns:
region_mapRegionNDMap

Region map.

get_by_idx(idxs)[source]#

Return map values at the given pixel indices.

Parameters:
idxtuple

Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps.

Returns:
valsndarray

Array of pixel values. numpy.nan is used to flag coordinates outside the map.

get_spectrum(*args, **kwargs)[source]#

Return self.

interp_by_coord(coords, **kwargs)[source]#

Interpolate map values at the given map coordinates.

Parameters:
coordstuple, dict or MapCoord

Coordinate arrays for each dimension of the map. Tuple should be ordered as (lon, lat, x_0, …, x_n) where x_i are coordinates for non-spatial dimensions of the map. “lon” and “lat” are optional and will be taken at the center of the region by default.

method{“linear”, “nearest”}

Method to interpolate data values. Default is “linear”.

fill_valueNone or float value

The value to use for points outside the interpolation domain. If None, values outside the domain are extrapolated.

values_scale{“lin”, “log”, “sqrt”}

Optional value scaling.

Returns:
valsndarray

Interpolated pixel values.

interp_by_pix(pix, **kwargs)[source]#

Interpolate map values at the given pixel coordinates.

Parameters:
pixtuple

Tuple of pixel coordinate arrays for each dimension of the map. Tuple should be ordered as (p_lon, p_lat, p_0, …, p_n) where p_i are pixel coordinates for non-spatial dimensions of the map.

method{“linear”, “nearest”}

Method to interpolate data values. Default is “linear”.

fill_valuefloat, optional

The value to use for points outside the interpolation domain. If None, values outside the domain are extrapolated. Default is None.

Returns:
valsndarray

Interpolated pixel values.

iter_by_axis_data(axis_name)[source]#

Iterate data by axis.

Parameters:
axis_namestr

Axis name.

Returns:
idx, datatuple, Quantity

Data and index.

plot(ax=None, axis_name=None, **kwargs)[source]#

Plot the data contained in region map along the non-spatial axis.

Parameters:
axAxis, optional

Axis used for plotting. Default is None.

axis_namestr, optional

Which axis to plot on the x-axis. Extra axes will be plotted as additional lines. Default is None.

**kwargsdict

Keyword arguments passed to errorbar.

Returns:
axAxis

Axis used for plotting.

plot_hist(ax=None, **kwargs)[source]#

Plot as histogram.

Parameters:
axaxis, optional

Axis instance to be used for the plot. Default is None.

**kwargsdict

Keyword arguments passed to hist.

Returns:
axAxis

Axis used for plotting.

plot_interactive()[source]#

Plot map with interactive widgets to explore the non-spatial axes.

Parameters:
rc_paramsdict, optional

Passed to matplotlib.rc_context(rc=rc_params) to style the plot. Default is None.

**kwargsdict, optional

Keyword arguments passed to WcsNDMap.plot.

Examples

You can try this out e.g. using a Fermi-LAT diffuse model cube with an energy axis:

from gammapy.maps import Map

m = Map.read("$GAMMAPY_DATA/fermi_3fhl/gll_iem_v06_cutout.fits")
m.plot_interactive(add_cbar=True, stretch="sqrt")

If you would like to adjust the figure size you can use the rc_params argument:

rc_params = {'figure.figsize': (12, 6), 'font.size': 12}
m.plot_interactive(rc_params=rc_params)
plot_mask(ax=None, **kwargs)[source]#

Plot the mask as a shaded area in a xmin-xmax range.

Parameters:
axaxis, optional

Axis instance to be used for the plot. Default is None.

**kwargsdict

Keyword arguments passed to axvspan.

Returns:
axAxis

Axis used for plotting.

plot_region(ax=None, **kwargs)[source]#

Plot region.

Parameters:
axWCSAxes, optional

Axes to plot on. If no axes are given, the region is shown using the minimal equivalent WCS geometry. Default is None.

**kwargsdict

Keyword arguments forwarded to as_artist.

classmethod read(filename, format='gadf', ogip_column=None, hdu=None, checksum=False)[source]#

Read from file.

Parameters:
filenamepathlib.Path or str

Filename.

format{“gadf”, “ogip”, “ogip-arf”}

Which format to use. Default is “gadf”.

ogip_column{None, “COUNTS”, “QUALITY”, “BACKSCAL”}, optional

If format ‘ogip’ is chosen which table hdu column to read. Default is None.

hdustr, optional

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

checksumbool

If True checks both DATASUM and CHECKSUM cards in the file headers. Default is False.

Returns:
region_mapRegionNDMap

Region map.

set_by_idx(idx, value)[source]#

Set pixels at idx with given vals.

Parameters:
idxtuple

Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps.

valsndarray

Values vector.

stack(other, weights=None, nan_to_num=True)[source]#

Stack other region map into map.

Parameters:
otherRegionNDMap

Other map to stack.

weightsRegionNDMap, optional

Array to be used as weights. The spatial geometry must be equivalent to other and additional axes must be broadcastable. Default is None.

nan_to_num: bool, optional

Non-finite values are replaced by zero if True. Default is True.

to_hdulist(format='gadf', hdu='SKYMAP', hdu_bands=None, hdu_region=None)[source]#

Convert to HDUList.

Parameters:
format{“gadf”, “ogip”, “ogip-sherpa”, “ogip-arf”, “ogip-arf-sherpa”}

Format specification. Default is “gadf”.

hdustr, optional

Name of the HDU with the map data, used for “gadf” format. Default is “SKYMAP”.

hdu_bandsstr, optional

Name or index of the HDU with the BANDS table, used for “gadf” format. Default is None.

hdu_regionstr, optional

Name or index of the HDU with the region table. Default is None.

Returns:
hdulistHDUList

HDU list.

to_region_nd_map(*args, **kwargs)[source]#

Return self.

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

Convert to Table.

Data format specification: PHA.

Parameters:
format{“gadf”, “ogip”, “ogip-arf”, “ogip-arf-sherpa”}

Format specification. Default is “gadf”.

Returns:
tableTable

Table.

upsample(factor, order=0, preserve_counts=True, axis_name=None)[source]#

Upsample the non-spatial dimension by a given factor.

By default, the first axes is upsampled.

Parameters:
factorint

Upsampling factor.

orderint, optional

Order of the interpolation used for upsampling. Default is 0.

preserve_countsbool, optional

Preserve the integral over each bin. This should be true if the RegionNDMap is an integral quantity (e.g. counts) and false if the RegionNDMap is a differential quantity (e.g. intensity). Default is True.

axis_namestr, optional

Which axis to upsample. Default is “energy”.

Returns:
mapRegionNDMap

Upsampled region map.

write(filename, overwrite=False, format='gadf', hdu='SKYMAP', checksum=False)[source]#

Write map to file.

Parameters:
filenamepathlib.Path or str

Filename.

overwritebool, optional

Overwrite existing file. Default is False.

format{“gadf”, “ogip”, “ogip-sherpa”, “ogip-arf”, “ogip-arf-sherpa”}

Which format to use. Default is “gadf”.

hdustr, optional

Name of the HDU. Default is “SKYMAP”.

checksumbool, optional

When True adds both DATASUM and CHECKSUM cards to the headers written to the file. Default is False.

__init__(geom, data=None, dtype='float32', meta=None, unit='')[source]#
classmethod __new__(*args, **kwargs)#