ENN Ragged Buffer
This Python package implements an efficient RaggedBuffer datatype that is similar to
a 3D numpy array, but which allows for variable sequence length in the second
dimension. It was created primarily for use in enn-trainer
and currently only supports a small selection of the numpy array methods.

User Guide
Install the package with pip install ragged-buffer.
The package currently supports three RaggedBuffer variants, RaggedBufferF32, RaggedBufferI64, and RaggedBufferBool.
Creating a RaggedBuffer
There are three ways to create a RaggedBuffer:
RaggedBufferF32(features: int)creates an emptyRaggedBufferwith the specified number of features.RaggedBufferF32.from_flattened(flattened: np.ndarray, lenghts: np.ndarray)creates aRaggedBufferfrom a flattened 2D numpy array and a 1D numpy array of lengths.RaggedBufferF32.from_arraycreates aRaggedBuffer(with equal sequence lenghts) from a 3D numpy array.
Creating an empty buffer and pushing each row:
# Create an empty RaggedBuffer with a feature size of 3
=
# Push sequences with 3, 5, 0, and 1 elements
# Alternative: `buffer.push_empty()`
Creating a RaggedBuffer from a flat 2D numpy array which combines the first and second dimension, and an array of sequence lengths:
=
)
Creating a RaggedBuffer from a 3D numpy array (all sequences have the same length):
=
Get size
The size0, size1, and size2 methods return the number of sequences, the number of elements in a sequence, and the number of features respectively.
=
)
# Get size of the first/batch dimension.
assert == 10
# Get size of individual sequences.
assert == 5
assert == 0
# Get size of the last/feature dimension.
assert == 64
Convert to numpy array
as_aray converts a RaggedBuffer to a flat 2D numpy array that combines the first and second dimension.
=
assert
Indexing
You can index a RaggedBuffer with a single integer (returning a RaggedBuffer with a single sequence), or with a numpy array of integers selecting/permuting multiple sequences.
# Create a new `RaggedBufferF32`
=
# Retrieve the first sequence.
assert
# Get a RaggedBatch with 2 randomly selected sequences.
Addition
You can add two RaggedBuffers with the + operator if they have the same number of sequences, sequence lengths, and features. You can also add a RaggedBuffer where all sequences have a length of 1 to a RaggedBuffer with variable length sequences, broadcasting along each sequence.
# Create ragged buffer with dimensions (3, [1, 3, 2], 1)
=
# Create ragged buffer with dimensions (3, [1, 1, 1], 1)
=
# Add rb3 and rb4, broadcasting along the sequence dimension.
= +
assert
Concatenation
The extend method can be used to mutate a RaggedBuffer by appending another RaggedBuffer to it.
=
=
assert == 6
Clear
The clear method removes all elements from a RaggedBuffer without deallocating the underlying memory.
=
assert == 0
License
ENN Ragged Buffer dual-licensed under Apache-2.0 and MIT.