SparseArray¶
-
class
gammapy.maps.SparseArray(shape, idx=None, data=None, dtype=<class 'float'>, fill_value=0.0)[source]¶ Bases:
objectSparse N-dimensional array object.
This class implements a data structure for sparse n-dimensional arrays such that only non-zero data values are allocated in memory. Supports numpy conventions for indexing and slicing logic.
Parameters: shape : tuple of ints
Shape of array.
idx :
ndarray, optionalFlattened index vector that initializes the array. If none then an empty array will be created.
data :
ndarray, optionalFlattened data vector that initializes the array. If none then an empty array will be created.
dtype : data-type, optional
Type of data vector.
fill_value : scalar, optional
Value assigned to array elements that are not allocated in memory.
Examples
A SparseArray is created in the same way as
ndarrayby passing the array shape to the constructor with an optional argument for the array type:>>> import numpy as np >>> from gammapy.maps import SparseArray >>> v = SparseArray((10,20), dtype=float)
Alternatively you can create a new SparseArray from an
ndarraywithfrom_array:>>> x = np.ones((10,20)) >>> v = SparseArray.from_array(x)
SparseArray follows numpy indexing and slicing conventions for setting/getting array elements. The primary difference with respect to the behavior of
ndarrayis that indexing always returns a copy rather than a view.>>> v[0,0] = 1.0 >>> print(v[0,0]) >>> v[:,0] = 1.0 >>> print(v[:,0])
Attributes Summary
dataReturn the sparsified data array. dtypeReturn the type of the data array member. idxReturn flattened index vector. ndimArray number of dimensions (int). shapeArray shape. sizeReturn current number of elements. Methods Summary
from_array(data[, min_value])Create a SparseArrayfrom a numpy array.get(idx_in)Get array values at indices idx_in.set(idx_in, vals[, fill])Set array values at indices idx_in.sum([axis, dtype, out, keepdims])Attributes Documentation
-
data¶ Return the sparsified data array.
-
dtype¶ Return the type of the data array member.
-
idx¶ Return flattened index vector.
-
ndim¶ Array number of dimensions (int).
-
shape¶ Array shape.
-
size¶ Return current number of elements.
Methods Documentation
-
classmethod
from_array(data, min_value=0.0)[source]¶ Create a
SparseArrayfrom a numpy array.Parameters: data :
numpy.ndarrayInput data array.
min_value : float
Threshold for sparsifying the data vector.
Returns: out :
SparseArrayOutput sparse array.
-