SparseArray¶
-
class
gammapy.maps.
SparseArray
(shape, idx=None, data=None, dtype=<class 'float'>, fill_value=0.0)[source]¶ Bases:
object
Sparse 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
, optional Flattened index vector that initializes the array. If none then an empty array will be created.
- data :
ndarray
, optional Flattened 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
ndarray
by 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
ndarray
withfrom_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
ndarray
is 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
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 Summary
from_array
(data[, min_value])Create a SparseArray
from 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
SparseArray
from a numpy array.Parameters: - data :
numpy.ndarray
Input data array.
- min_value : float
Threshold for sparsifying the data vector.
Returns: - out :
SparseArray
Output sparse array.
- data :