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.3.3.tar.gz
.
File metadata
- Download URL: ragged_buffer-0.3.3.tar.gz
- Upload date:
- Size: 20.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b16b6a871458ea1be8b69a74713a64ed67245c6e0936707f9fe14d2203e5f1d8 |
|
MD5 | ad0fef276532d1149dc283f55e15d127 |
|
BLAKE2b-256 | cf4ef62da6ef1e681d1acd0581291e5cad4d4b2f92337791ee0be252d29e5d7a |
File details
Details for the file ragged_buffer-0.3.3-cp310-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp310-none-win_amd64.whl
- Upload date:
- Size: 255.7 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 080504d9e1fbf77c96ea2cb656ee2a0112f1f249b9f56503843c2d4d8fbf0fae |
|
MD5 | 5c328b00b23bcf19172c4a5da56b265d |
|
BLAKE2b-256 | 154edd998638d3dcfb94fb473f8973b103c82fc68ccf0e6d9fee8fc40a4b3266 |
File details
Details for the file ragged_buffer-0.3.3-cp39-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp39-none-win_amd64.whl
- Upload date:
- Size: 256.0 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fe1b5884103c0aab613922d108bb33b880a20563cdaea16e92ad2f40484b046 |
|
MD5 | 5703bd7587b5c1eb3569ba688eff5d57 |
|
BLAKE2b-256 | 1c4b7b6c5c1ee711d6fb85a6bff56209a021c8a098b02b8ea578372da8d9185f |
File details
Details for the file ragged_buffer-0.3.3-cp39-cp39-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp39-cp39-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 317.6 kB
- Tags: CPython 3.9, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d05e5bc19c5d073f9f37ff060fa337d3cf3c391a3ecb82770dd632bdeb93244a |
|
MD5 | 5b72b7764801370e8024e01910d5bb21 |
|
BLAKE2b-256 | 15f425620d015320ef666b680d6464b5e4483ff576555174edb552a439580731 |
File details
Details for the file ragged_buffer-0.3.3-cp39-cp39-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp39-cp39-macosx_10_7_x86_64.whl
- Upload date:
- Size: 292.0 kB
- Tags: CPython 3.9, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c869c9fae1ff2584b57d259a54328729fbf82430aa8fde5cb489d2b908870ee8 |
|
MD5 | b1af5560b46817ba57b138cf09f25716 |
|
BLAKE2b-256 | f77a08df93e0d5dbc2cf88a7aabec9515f4dacfd80fef8e115429572b6e06a7d |
File details
Details for the file ragged_buffer-0.3.3-cp38-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp38-none-win_amd64.whl
- Upload date:
- Size: 256.1 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15eedbbe36f74bde199c6efae8fa1961b00f51d09a621aa733d634f4c84ccd55 |
|
MD5 | af7c0563b11cc22ae212f303cfed59f0 |
|
BLAKE2b-256 | 498301a3723626d45e25d60b1973fb8b7f90ee615b4fb8fa08a204280458d6d0 |
File details
Details for the file ragged_buffer-0.3.3-cp38-cp38-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp38-cp38-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 318.0 kB
- Tags: CPython 3.8, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55f9664a0d141af3e451e9978ee4946366e6fd203bb44b23227733089f571be1 |
|
MD5 | d8b966b4a7860074a09845b01c4cc9fb |
|
BLAKE2b-256 | c4893982cf25ca8c98729d8f018e105ad22ef10aa77e0a94765ef689fdf6ba96 |
File details
Details for the file ragged_buffer-0.3.3-cp38-cp38-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp38-cp38-macosx_10_7_x86_64.whl
- Upload date:
- Size: 292.6 kB
- Tags: CPython 3.8, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7c52e3ee809fde41e8ad61f6a58a63384601e7994d1ae3045cb5694fb6762c3 |
|
MD5 | 51f740fb186f90e56902f681328b5e1b |
|
BLAKE2b-256 | c5163afd8d41b8accc5ed3d300db67ff9a00ca1d882f9b685b8fe8f1dab584e8 |
File details
Details for the file ragged_buffer-0.3.3-cp37-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp37-none-win_amd64.whl
- Upload date:
- Size: 256.4 kB
- Tags: CPython 3.7, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf5a1a979a28ab2d98da47d0e9c0d5b24ff259fe479a67894ad84832cea876e6 |
|
MD5 | 20c1cfc87c4441e0808be2026db3ac80 |
|
BLAKE2b-256 | 87c4a1ad911f798acd99e1ef937e00d563f288949df03f58e88fa6122b161dc3 |
File details
Details for the file ragged_buffer-0.3.3-cp37-cp37m-manylinux_2_24_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp37-cp37m-manylinux_2_24_x86_64.whl
- Upload date:
- Size: 318.0 kB
- Tags: CPython 3.7m, manylinux: glibc 2.24+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1daa672f7baad60e208f3f60d1b2d686308812695dc992f9ce1f630d9a08dbdb |
|
MD5 | 8bddcfa47cf966b62c2e212db4568a8f |
|
BLAKE2b-256 | b136b4a9104c24adbfdd84e615655c8a6e11933ad477b29dab0d3bc4d72d01e7 |
File details
Details for the file ragged_buffer-0.3.3-cp37-cp37m-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp37-cp37m-macosx_10_7_x86_64.whl
- Upload date:
- Size: 292.5 kB
- Tags: CPython 3.7m, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfc3b8ad24f5e42a59ab9227c5312b1a5d0dd44763d8172c1eeb98c453b3228c |
|
MD5 | a604055170125570662de134e0e8ce1e |
|
BLAKE2b-256 | 3036e35bda45a55232b6bcdbcef041473f725a6402c86a622d78b71480140e90 |
File details
Details for the file ragged_buffer-0.3.3-cp36-none-win_amd64.whl
.
File metadata
- Download URL: ragged_buffer-0.3.3-cp36-none-win_amd64.whl
- Upload date:
- Size: 253.4 kB
- Tags: CPython 3.6, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdff4df17d0b639748bb6e58027e4e4ce2118780e32204b2a2e35b979261863a |
|
MD5 | ab093ba935b40c60703316ac53939fa2 |
|
BLAKE2b-256 | 7c7f592476ddb36c96ac0e964866a4093ae09da0773fcf82b560b88e2e19a6c1 |