NdArray¶
-
class
finalfusion.storage.ndarray.
NdArray
(array: numpy.ndarray)[source]¶ Bases:
numpy.ndarray
,finalfusion.storage.storage.Storage
Array storage.
Wraps an numpy matrix, either in-memory or memory-mapped.
Examples
>>> storage = NdArray(np.array([[1., 0.5], [0.5, 1.], [0.3, 0.4]], ... dtype=np.float32)) >>> # slicing an NdArray returns a storage backed by the same array >>> storage[:2] NdArray([[1. , 0.5], [0.5, 1. ]], dtype=float32) >>> # NdArray storage can be treated as numpy arrays >>> storage * 2 NdArray([[2. , 1. ], [1. , 2. ], [0.6, 0.8]], dtype=float32) >>> # Indexing with arrays, lists or ints returns numpy.ndarray >>> storage[0] array([1. , 0.5], dtype=float32)
-
static
__new__
(cls, array: numpy.ndarray)[source]¶ Construct a new NdArray storage.
- Parameters
array (np.ndarray) – The storage buffer.
- Raises
TypeError – If the array is not a 2-dimensional float32 array.
-
classmethod
load
(file: BinaryIO, mmap: bool = False) → finalfusion.storage.ndarray.NdArray[source]¶ Load Storage from the given finalfusion file.
- Parameters
file (IO[bytes]) – Finalfusion file with a storage chunk
mmap (bool)
- Returns
storage (Storage) – The first storage in the input file
mmap (bool) – Toggles memory mapping the storage buffer as read-only.
- Raises
ValueError – If the file did not contain a storage.
-
static
chunk_identifier
() → finalfusion.io.ChunkIdentifier[source]¶ Get the ChunkIdentifier for this Chunk.
- Returns
chunk_identifier
- Return type
-
static
read_chunk
(file: BinaryIO) → finalfusion.storage.ndarray.NdArray[source]¶ Read the Chunk and return it.
The file must be positioned before the contents of the
Chunk
but after its header.- Parameters
file (BinaryIO) – a finalfusion file containing the given Chunk
- Returns
chunk – The chunk read from the file.
- Return type
-
property
shape
¶ Tuple of array dimensions.
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required.
Examples
>>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4) >>> y.shape = (3, 8) >>> y array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3, 6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification. Use `.reshape()` to make a copy with the desired shape.
See also
numpy.reshape
similar function
ndarray.reshape
similar method
-
static
mmap_chunk
(file: BinaryIO) → finalfusion.storage.ndarray.NdArray[source]¶ Memory maps the storage as a read-only buffer.
- Parameters
file (IO[bytes]) – Finalfusion file with a storage chunk
- Returns
storage – The first storage in the input file
- Return type
- Raises
ValueError – If the file did not contain a storage.
-
static
-
finalfusion.storage.ndarray.
load_ndarray
(file: Union[str, bytes, int, os.PathLike], mmap: bool = False) → finalfusion.storage.ndarray.NdArray[source]¶ Load an array chunk from the given file.
- Parameters
file (str, bytes, int, PathLike) – Finalfusion file with a ndarray chunk.
mmap (bool) – Toggles memory mapping the array buffer as read only.
- Returns
storage – The NdArray storage from the file.
- Return type
- Raises
ValueError – If the file did not contain and NdArray chunk.