HpxSparseMap

class gammapy.maps.HpxSparseMap(geom, data=None, dtype='float32', meta=None)[source]

Bases: gammapy.maps.HpxMap

Representation of a N+2D map using HEALPIX with two spatial dimensions and N non-spatial dimensions.

This class uses a sparse matrix for HEALPix pixel values.

Parameters:

geom : HpxGeom

HEALPIX geometry object.

data : ndarray

HEALPIX data array.

meta : OrderedDict

Dictionary to store meta data.

Attributes Summary

data Data array (ndarray)
geom Map geometry (MapGeom)

Methods Summary

coadd(map_in) Add the contents of map_in to this map.
create([nside, binsz, nest, map_type, …]) Factory method to create an empty HEALPix map.
crop(crop_width) Crop the spatial dimension of the map by removing a number of pixels from the edge of the map.
downsample(factor) Downsample the spatial dimension of the map by a given factor.
fill_by_coord(coords[, weights]) Fill pixels at the given map coordinates with values in weights vector.
fill_by_idx(idx[, weights]) Fill pixels at the given pixel indices with values in weights vector.
fill_by_pix(pix[, weights]) Fill pixels at the given pixel coordinates with values in weights vector.
from_geom(geom[, meta, map_type]) Generate an empty map from a Geom instance.
from_hdu(hdu[, hdu_bands]) Create from a FITS HDU.
from_hdu_list(hdulist[, hdu, hdu_bands, …])
from_hdulist(hdu_list[, hdu, hdu_bands]) Make a HpxMap object from a FITS HDUList.
get_by_coord(coords) Return map values at the given map coordinates.
get_by_idx(idx) Return map values at the given pixel indices.
get_by_pix(pix[, interp]) Return map values at the given pixel coordinates.
interp_by_coord(coords[, interp]) Interpolate map values at the given map coordinates.
interp_by_pix(pix[, interp]) Interpolate map values at the given pixel coordinates.
iter_by_coord() Iterate over elements of the map returning a tuple with values and map coordinates.
iter_by_image() Iterate over image planes of the map returning a tuple with the image array and image plane index.
iter_by_pix() Iterate over elements of the map returning a tuple with values and pixel coordinates.
make_hdu([hdu, hdu_bands, sparse, conv]) Make a FITS HDU with input data.
pad(pad_width) Pad the spatial dimension of the map by extending the edge of the map by the given number of pixels.
read(filename[, hdu, hdu_bands, map_type]) Read a map from a FITS file.
reproject(geom[, order, mode]) Reproject this map to a different geometry.
set_by_coord(coords, vals) Set pixels at the given map coordinates to the values in vals vector.
set_by_idx(idx, vals) Set pixels at the given pixel indices to the values in vals vector.
set_by_pix(pix, vals) Set pixels at the given pixel coordinates to the values in vals vector.
sum_over_axes() Reduce to a 2D image by summing over non-spatial dimensions.
to_hdulist([hdu, hdu_bands, sparse, conv]) Convert to HDUList.
to_swapped() Return a new map with the opposite scheme (ring or nested).
to_ud_graded() Upgrade or downgrade the resolution of the map to the chosen nside.
to_wcs([sum_bands, normalize, proj, oversample]) Make a WCS object and convert HEALPIX data into WCS projection.
upsample(factor) Upsample the spatial dimension of the map by a given factor.
write(filename, **kwargs) Write to a FITS file.

Attributes Documentation

data

Data array (ndarray)

geom

Map geometry (MapGeom)

Methods Documentation

coadd(map_in)

Add the contents of map_in to this map. This method can be used to combine maps containing integral quantities (e.g. counts) or differential quantities if the maps have the same binning.

Parameters:

map_in : Map

Input map.

create(nside=None, binsz=None, nest=True, map_type='hpx', coordsys='CEL', data=None, skydir=None, width=None, dtype='float32', region=None, axes=None, conv='gadf', meta=None)

Factory method to create an empty HEALPix map.

Parameters:

nside : int or ndarray

HEALPix NSIDE parameter. This parameter sets the size of the spatial pixels in the map.

binsz : float or ndarray

Approximate pixel size in degrees. An NSIDE will be chosen that correponds to a pixel size closest to this value. This option is superseded by nside.

nest : bool

True for HEALPix “NESTED” indexing scheme, False for “RING” scheme.

coordsys : {‘CEL’, ‘GAL’}, optional

Coordinate system, either Galactic (‘GAL’) or Equatorial (‘CEL’).

skydir : tuple or SkyCoord

Sky position of map center. Can be either a SkyCoord object or a tuple of longitude and latitude in deg in the coordinate system of the map.

map_type : {‘hpx’, ‘hpx-sparse’}

Map type. Selects the class that will be used to instantiate the map.

width : float

Diameter of the map in degrees. If None then an all-sky geometry will be created.

axes : list

List of MapAxis objects for each non-spatial dimension.

conv : {‘fgst-ccube’,’fgst-template’,’gadf’}, optional

Default FITS format convention that will be used when writing this map to a file. Default is ‘gadf’.

meta : OrderedDict

Dictionary to store meta data.

Returns:

map : HpxMap

A HPX map object.

crop(crop_width)[source]

Crop the spatial dimension of the map by removing a number of pixels from the edge 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 pad.

Returns:

map : Map

Cropped map.

downsample(factor)[source]

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

Parameters:

factor : int

Downsampling factor.

preserve_counts : bool

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).

Returns:

map : Map

Downsampled map.

fill_by_coord(coords, weights=None)

Fill pixels at the given map coordinates with values in weights vector.

Parameters:

coords : tuple or MapCoord

MapCoord object or tuple of 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.

weights : ndarray

Weights vector. If None then a unit weight will be assumed for each element in coords.

fill_by_idx(idx, weights=None)[source]

Fill pixels at the given pixel indices with values in weights vector.

Parameters:

idx : tuple

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.

weights : ndarray

Weights vector. If None then a unit weight will be assumed for each element in idx.

fill_by_pix(pix, weights=None)

Fill pixels at the given pixel coordinates with values in weights vector.

Parameters:

pix : tuple

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. Pixel indices can be either float or integer type. Float indices will be rounded to the nearest integer.

weights : ndarray

Weights vector. If None then a unit weight will be assumed for each element in pix.

from_geom(geom, meta=None, map_type='auto')

Generate an empty map from a Geom instance.

Parameters:

geom : MapGeom

Map geometry.

meta : OrderedDict

Dictionary to store meta data.

map_type : {‘wcs’, ‘wcs-sparse’, ‘hpx’, ‘hpx-sparse’, ‘auto’}

Map type. Selects the class that will be used to instantiate the map. The map type should be consistent with the geometry. If map_type is ‘auto’ then an appropriate map type will be inferred from type of geom.

Returns:

map_out : Map

Map object

classmethod from_hdu(hdu, hdu_bands=None)[source]

Create from a FITS HDU.

Parameters:

hdu : BinTableHDU

The FITS HDU

hdu_bands : BinTableHDU

The BANDS table HDU

from_hdu_list(hdulist, hdu=None, hdu_bands=None, map_type='auto')
from_hdulist(hdu_list, hdu=None, hdu_bands=None)

Make a HpxMap object from a FITS HDUList.

Parameters:

hdu_list : HDUList

HDU list containing HDUs for map data and bands.

hdu : str

Name or index of the HDU with the map data. If None then the method will try to load map data from the first BinTableHDU in the file.

hdu_bands : str

Name or index of the HDU with the BANDS table.

Returns:

hpx_map : HpxMap

Map object

get_by_coord(coords)

Return map values at the given map coordinates.

Parameters:

coords : tuple or MapCoord

MapCoord object or tuple of 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.

Returns:

vals : ndarray

Values of pixels in the map. np.nan used to flag coords outside of map.

get_by_idx(idx)[source]

Return map values at the given pixel indices.

Parameters:

idx : tuple

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:

vals : ndarray

Array of pixel values. np.nan used to flag coordinate outside of map

get_by_pix(pix, interp=None)[source]

Return map values at the given pixel coordinates.

Parameters:

pix : tuple

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. Pixel indices can be either float or integer type.

Returns:

vals : ndarray

Array of pixel values. np.nan used to flag coordinates outside of map

interp_by_coord(coords, interp=None)[source]

Interpolate map values at the given map coordinates.

Parameters:

coords : tuple or MapCoord

MapCoord object or tuple of 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.

interp : {None, ‘nearest’, ‘linear’, ‘cubic’, 0, 1, 2, 3}

Method to interpolate data values. By default no interpolation is performed and the return value will be the amplitude of the pixel encompassing the given coordinate. Integer values can be used in lieu of strings to choose the interpolation method of the given order (0=’nearest’, 1=’linear’, 2=’quadratic’, 3=’cubic’). Note that only ‘nearest’ and ‘linear’ methods are supported for all map types.

Returns:

vals : ndarray

Interpolated pixel values.

interp_by_pix(pix, interp=None)[source]

Interpolate map values at the given pixel coordinates.

Parameters:

pix : tuple

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.

interp : {None, ‘nearest’, ‘linear’, ‘cubic’, 0, 1, 2, 3}

Method to interpolate data values. By default no interpolation is performed and the return value will be the amplitude of the pixel encompassing the given coordinate. Integer values can be used in lieu of strings to choose the interpolation method of the given order (0=’nearest’, 1=’linear’, 2=’quadratic’, 3=’cubic’). Note that only ‘nearest’ and ‘linear’ methods are supported for all map types.

Returns:

vals : ndarray

Interpolated pixel values.

iter_by_coord()[source]

Iterate over elements of the map returning a tuple with values and map coordinates.

Parameters:

buffersize : int

Set the size of the buffer. The map will be returned in chunks of the given size.

Returns:

val : ndarray

Map values.

coords : tuple

Tuple of map coordinates.

iter_by_image()[source]

Iterate over image planes of the map returning a tuple with the image array and image plane index.

Returns:

val : ndarray

Array of image plane values.

idx : tuple

Index of image plane.

iter_by_pix()[source]

Iterate over elements of the map returning a tuple with values and pixel coordinates.

Parameters:

buffersize : int

Set the size of the buffer. The map will be returned in chunks of the given size.

Returns:

val : ndarray

Map values.

pix : tuple

Tuple of pixel coordinates.

make_hdu(hdu=None, hdu_bands=None, sparse=False, conv=None)

Make a FITS HDU with input data.

Parameters:

hdu : str

The HDU extension name.

hdu_bands : str

The HDU extension name for BANDS table.

sparse : bool

Set INDXSCHM to SPARSE and sparsify the map by only writing pixels with non-zero amplitude.

conv : {‘fgst-ccube’,’fgst-template’,’gadf’,None}, optional

FITS format convention. If None this will be set to the default convention of the map.

Returns:

hdu_out : BinTableHDU or ImageHDU

Output HDU containing map data.

pad(pad_width)[source]

Pad the spatial dimension of the map by extending the edge of the map by the given number of pixels.

Parameters:

pad_width : {sequence, array_like, int}

Number of pixels padded to the edges of each axis.

mode : {‘edge’, ‘constant’, ‘interp’}

Padding mode. ‘edge’ pads with the closest edge value. ‘constant’ pads with a constant value. ‘interp’ pads with an extrapolated value.

cval : float

Padding value when mode=’consant’.

order : int

Order of interpolation when mode=’constant’ (0 = nearest-neighbor, 1 = linear, 2 = quadratic, 3 = cubic).

Returns:

map : Map

Padded map.

read(filename, hdu=None, hdu_bands=None, map_type='auto')

Read a map from a FITS file.

Parameters:

filename : str

Name of the FITS file.

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.

map_type : {‘wcs’, ‘wcs-sparse’, ‘hpx’, ‘hpx-sparse’, ‘auto’}

Map type. Selects the class that will be used to instantiate the map. The map type should be consistent with the format of the input file. If map_type is ‘auto’ then an appropriate map type will be inferred from the input file.

Returns:

map_out : Map

Map object

reproject(geom, order=1, mode='interp')

Reproject this map to a different geometry.

Parameters:

geom : MapGeom

Geometry of projection.

mode : {‘interp’, ‘exact’}

Method for reprojection. ‘interp’ method interpolates at pixel centers. ‘exact’ method integrates over intersection of pixels.

order : int or str

Order of interpolating polynomial (0 = nearest-neighbor, 1 = linear, 2 = quadratic, 3 = cubic).

Returns:

map : Map

Reprojected map.

set_by_coord(coords, vals)

Set pixels at the given map coordinates to the values in vals vector.

Parameters:

coords : tuple or MapCoord

MapCoord object or tuple of 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.

vals : ndarray

Values vector. Pixels at coords will be set to these values.

set_by_idx(idx, vals)[source]

Set pixels at the given pixel indices to the values in vals vector.

Parameters:

idx : tuple

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.

vals : ndarray

Values vector. Pixels at idx will be set to these values.

set_by_pix(pix, vals)

Set pixels at the given pixel coordinates to the values in vals vector.

Parameters:

pix : tuple

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. Pixel indices can be either float or integer type. Float indices will be rounded to the nearest integer.

vals : ndarray

Values vector. Pixels at pix will be set to these values.

sum_over_axes()[source]

Reduce to a 2D image by summing over non-spatial dimensions.

to_hdulist(hdu='SKYMAP', hdu_bands=None, sparse=False, conv=None)

Convert to HDUList.

Parameters:

hdu : str

The HDU extension name.

hdu_bands : str

The HDU extension name for BANDS table.

sparse : bool

Set INDXSCHM to SPARSE and sparsify the map by only writing pixels with non-zero amplitude.

conv : {‘fgst-ccube’,’fgst-template’,’gadf’,None}, optional

FITS format convention. If None this will be set to the default convention of the map.

Returns:

hdu_list : HDUList

to_swapped()[source]

Return a new map with the opposite scheme (ring or nested).

Returns:

map : HpxMap

Map object.

to_ud_graded()[source]

Upgrade or downgrade the resolution of the map to the chosen nside.

Parameters:

nside : int

NSIDE parameter of the new map.

preserve_counts : bool

Choose whether to preserve counts (total amplitude) or intensity (amplitude per unit solid angle).

Returns:

map : HpxMap

Map object.

to_wcs(sum_bands=False, normalize=True, proj='AIT', oversample=2)[source]

Make a WCS object and convert HEALPIX data into WCS projection.

Parameters:

sum_bands : bool

Sum over non-spatial axes before reprojecting. If False then the WCS map will have the same dimensionality as the HEALPix one.

normalize : bool

True -> preserve integral by splitting HEALPIX values between bins

proj : str

WCS-projection

oversample : float

Oversampling factor for WCS map. This will be the approximate ratio of the width of a HPX pixel to a WCS pixel. If this parameter is None then the width will be set from width_pix.

width_pix : int

Width of the WCS geometry in pixels. The pixel size will be set to the number of pixels satisfying oversample or width_pix whichever is smaller. If this parameter is None then the width will be set from oversample.

hpx2wcs : HpxToWcsMapping

Set the HPX to WCS mapping object that will be used to generate the WCS map. If none then a new mapping will be generated based on proj and oversample arguments.

Returns:

map_out : WcsMap

WCS map object.

upsample(factor)[source]

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

Parameters:

factor : int

Upsampling factor.

order : int

Order of the interpolation used for upsampling.

preserve_counts : bool

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).

Returns:

map : Map

Upsampled map.

write(filename, **kwargs)

Write to a FITS file.

Parameters:

filename : str

Output file name.

hdu : str

Set the name of the image extension. By default this will be set to SKYMAP (for BINTABLE HDU) or PRIMARY (for IMAGE HDU).

hdu_bands : str

Set the name of the bands table extension. By default this will be set to BANDS.

conv : str

FITS format convention. By default files will be written to the gamma-astro-data-formats (GADF) format. This option can be used to write files that are compliant with format conventions required by specific software (e.g. the Fermi Science Tools). Supported conventions are ‘gadf’, ‘fgst-ccube’, ‘fgst-ltcube’, ‘fgst-bexpcube’, ‘fgst-template’, ‘fgst-srcmap’, ‘fgst-srcmap-sparse’, ‘galprop’, and ‘galprop2’.

sparse : bool

Sparsify the map by dropping pixels with zero amplitude. This option is only compatible with the ‘gadf’ format.