No project description provided
Project description
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-PPO
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 emptyRaggedBuffer
with the specified number of features.RaggedBufferF32.from_flattened(flattened: np.ndarray, lenghts: np.ndarray)
creates aRaggedBuffer
from a flattened 2D numpy array and a 1D numpy array of lengths.RaggedBufferF32.from_array
creates aRaggedBuffer
(with equal sequence lenghts) from a 3D numpy array.
Creating an empty buffer and pushing each row:
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create an empty RaggedBuffer with a feature size of 3
buffer = RaggedBufferF32(3)
# Push sequences with 3, 5, 0, and 1 elements
buffer.push(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32))
buffer.push(np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]], dtype=np.float32))
buffer.push(np.array([], dtype=np.float32)) # Alternative: `buffer.push_empty()`
buffer.push(np.array([[25, 25, 27]], dtype=np.float32))
Creating a RaggedBuffer from a flat 2D numpy array which combines the first and second dimension, and an array of sequence lengths:
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_flattened(
np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 25, 27]], dtype=np.float32),
np.array([3, 5, 0, 1], dtype=np.int64))
)
Creating a RaggedBuffer from a 3D numpy array (all sequences have the same length):
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
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.
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_flattened(
np.zeros((9, 64), dtype=np.float32),
np.array([3, 5, 0, 1], dtype=np.int64))
)
# Get size of the first/batch dimension.
assert buffer.size0() == 10
# Get size of individual sequences.
assert buffer.size1(1) == 5
assert buffer.size1(2) == 0
# Get size of the last/feature dimension.
assert buffer.size2() == 64
Convert to numpy array
as_aray
converts a RaggedBuffer
to a flat 2D numpy array that combines the first and second dimension.
import numpy as np
from ragged_buffer import RaggedBufferI64
buffer = RaggedBufferI64(1)
buffer.push(np.array([[1], [1], [1]], dtype=np.int64))
buffer.push(np.array([[2], [2]], dtype=np.int64))
assert np.all(buffer.as_array(), np.array([[1], [1], [1], [2], [2]], dtype=np.int64))
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.
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create a new `RaggedBufferF32`
buffer = RaggedBufferF32.from_flattened(
np.arange(0, 40, dtype=np.float32).reshape(10, 4),
np.array([3, 5, 0, 1], dtype=np.int64)
)
# Retrieve the first sequence.
assert np.all(
buffer[0].as_array() ==
np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], dtype=np.float32)
)
# Get a RaggedBatch with 2 randomly selected sequences.
buffer[np.random.permutation(4)[:2]]
Addition
You can add two RaggedBuffer
s 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.
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create ragged buffer with dimensions (3, [1, 3, 2], 1)
rb3 = RaggedBufferI64(1)
rb3.push(np.array([[0]], dtype=np.int64))
rb3.push(np.array([[0], [1], [2]], dtype=np.int64))
rb3.push(np.array([[0], [5]], dtype=np.int64))
# Create ragged buffer with dimensions (3, [1, 1, 1], 1)
rb4 = RaggedBufferI64.from_array(np.array([0, 3, 10], dtype=np.int64).reshape(3, 1, 1))
# Add rb3 and rb4, broadcasting along the sequence dimension.
rb5 = rb3 + rb4
assert np.all(
rb5.as_array() == np.array([[0], [3], [4], [5], [10], [15]], dtype=np.int64)
)
Concatenation
The extend
method can be used to mutate a RaggedBuffer
by appending another RaggedBuffer
to it.
import numpy as np
from ragged_buffer import RaggedBufferF32
rb1 = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
rb2 = RaggedBufferF32.from_array(np.zeros((2, 5, 3), dtype=np.float32))
rb1.extend(r2)
assert rb1.size0() == 6
Clear
The clear
method removes all elements from a RaggedBuffer
without deallocating the underlying memory.
import numpy as np
from ragged_buffer import RaggedBufferF32
rb = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
rb.clear()
assert rb.size0() == 0
License
ENN Ragged Buffer dual-licensed under Apache-2.0 and MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file ragged_buffer-0.2.12.tar.gz
.
File metadata
- Download URL: ragged_buffer-0.2.12.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2942439be9296661c841a29b459b25aa2ebac9757cb262f72919b990b1ae21b |
|
MD5 | b2b59a4e954d773f14d730eab55cd653 |
|
BLAKE2b-256 | faec0bc005f05bec9840cb5bfc02fba43c9708726e21e61236e6cd1cc0b1e382 |
File details
Details for the file ragged_buffer-0.2.12-cp310-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp310-none-win_amd64.whl
- Upload date:
- Size: 210.3 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75a4b56584f71f90bed0263f8d169f700f69127ce49d6ad56d290bee56317ae2 |
|
MD5 | 73ea27fd152ffd2fc0299da66d53bb8d |
|
BLAKE2b-256 | 903b501be038f0248a21efa677635e276fce2b1f4d266a781b7ef5a9790dcfe9 |
File details
Details for the file ragged_buffer-0.2.12-cp39-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp39-none-win_amd64.whl
- Upload date:
- Size: 210.5 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c8611aed0ec8a92ca6429f766fafcdc6ea06e6b9e5b7568a69f58f55d932c82 |
|
MD5 | 1816fdb10133ff9edcc167d61703a14d |
|
BLAKE2b-256 | f952c14a886e2a0c0d89428beee037a4afe58d7d2bae78fe1633c64aed7d2a8b |
File details
Details for the file ragged_buffer-0.2.12-cp39-cp39-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp39-cp39-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 274.5 kB
- Tags: CPython 3.9, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8e89bf5070d9ccd61089586b659bb1507b1994888784d01a5c03839960bb146 |
|
MD5 | 3fd938ea3fbe5d3bfa5ecd56fd7c1922 |
|
BLAKE2b-256 | c95ed3e1508052581a85d7ca91e27843885b9236764623a69f9ca028b228a16e |
File details
Details for the file ragged_buffer-0.2.12-cp39-cp39-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp39-cp39-macosx_10_7_x86_64.whl
- Upload date:
- Size: 253.8 kB
- Tags: CPython 3.9, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9457cf7036972b9eed4ed782b6b4b85aeb852a8fb62c03b46868b08a19b54fec |
|
MD5 | 8ce905a33d430d49f8ca34c761b4e56b |
|
BLAKE2b-256 | b1b3696e1304282ecf5eddb42379187ccf2187f5c35500c25a35b6054c7ab9c6 |
File details
Details for the file ragged_buffer-0.2.12-cp38-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp38-none-win_amd64.whl
- Upload date:
- Size: 210.6 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d647f9e667eba6108637e080e02503f83c08cdfd75f35edc77a649b8befc3cb1 |
|
MD5 | 0859c62c52a30cf1c17ba9f2f6c617fe |
|
BLAKE2b-256 | cdd9eb36fe8c52ea13ae4edcc2c87a7b94866b976c4f58324b5156135e7431d1 |
File details
Details for the file ragged_buffer-0.2.12-cp38-cp38-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp38-cp38-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 274.7 kB
- Tags: CPython 3.8, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04da2eaaddbd062fb36677452039d69a54b9df5daa45ad51c7bfed2a07a67f56 |
|
MD5 | f38a97709a6bbad72737a7122ac11b89 |
|
BLAKE2b-256 | 5be68341f8bec5349b5d805815a7276e7fad164ccbae20c1cf927b8936d42ad5 |
File details
Details for the file ragged_buffer-0.2.12-cp38-cp38-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp38-cp38-macosx_10_7_x86_64.whl
- Upload date:
- Size: 253.8 kB
- Tags: CPython 3.8, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 243d343723b26dd2d2d5a81eeed66a687f0774fe6752c8073cd30281104c6706 |
|
MD5 | 43c93b51f91a540ec1752fce10ac6d7d |
|
BLAKE2b-256 | 7296ba0ec90e1032f5ffaf9243a210e05db49da50a3be96f7b79b49e90c2b84b |
File details
Details for the file ragged_buffer-0.2.12-cp37-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp37-none-win_amd64.whl
- Upload date:
- Size: 210.6 kB
- Tags: CPython 3.7, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c36a6700d3746b0120b70ddf9590c6fc1c689b25fbc8c86578d71f4620d5f6ed |
|
MD5 | 9388541dc0b2c7f0d5f2b5c405765964 |
|
BLAKE2b-256 | 405409e8b6d12a14b227f4354bbc9a585584bc6cc38878b2e84f55af24a35c46 |
File details
Details for the file ragged_buffer-0.2.12-cp36-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.2.12-cp36-none-win_amd64.whl
- Upload date:
- Size: 204.5 kB
- Tags: CPython 3.6, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 321a300720e89037702b4e1f7e97fe48708c9aa59160888dfb3919ce220b9b59 |
|
MD5 | f304ea30e31dfd97297bb159fcf6e355 |
|
BLAKE2b-256 | c5a12942276fbdf8424647380bbbbf6936aca5ea025b2efee0a747ef92225103 |