Skip to main content

No project description provided

Project description

ENN Ragged Buffer

Actions Status PyPI Discord

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.

Ragged Buffer

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 empty RaggedBuffer with the specified number of features.
  • RaggedBufferF32.from_flattened(flattened: np.ndarray, lenghts: np.ndarray) creates a RaggedBuffer from a flattened 2D numpy array and a 1D numpy array of lengths.
  • RaggedBufferF32.from_array creates a RaggedBuffer (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 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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ragged_buffer-0.3.6.tar.gz (21.6 kB view details)

Uploaded Source

Built Distributions

ragged_buffer-0.3.6-cp310-none-win_amd64.whl (267.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

ragged_buffer-0.3.6-cp39-none-win_amd64.whl (267.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

ragged_buffer-0.3.6-cp39-cp39-manylinux_2_24_x86_64.whl (329.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.3.6-cp39-cp39-macosx_10_7_x86_64.whl (303.3 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

ragged_buffer-0.3.6-cp38-none-win_amd64.whl (267.7 kB view details)

Uploaded CPython 3.8 Windows x86-64

ragged_buffer-0.3.6-cp38-cp38-manylinux_2_24_x86_64.whl (329.3 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.3.6-cp38-cp38-macosx_10_7_x86_64.whl (303.4 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

ragged_buffer-0.3.6-cp37-none-win_amd64.whl (268.0 kB view details)

Uploaded CPython 3.7 Windows x86-64

ragged_buffer-0.3.6-cp37-cp37m-manylinux_2_24_x86_64.whl (329.3 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.24+ x86-64

ragged_buffer-0.3.6-cp37-cp37m-macosx_10_7_x86_64.whl (303.4 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

ragged_buffer-0.3.6-cp36-none-win_amd64.whl (265.2 kB view details)

Uploaded CPython 3.6 Windows x86-64

File details

Details for the file ragged_buffer-0.3.6.tar.gz.

File metadata

  • Download URL: ragged_buffer-0.3.6.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.12.6

File hashes

Hashes for ragged_buffer-0.3.6.tar.gz
Algorithm Hash digest
SHA256 927540fccbc7d17c68650d71860cc2f8e3a175972b1ddfb92b47d99d1b8b4e51
MD5 00bb9887334f1efac840396456ac469a
BLAKE2b-256 ed89145216d01f58eeee6e0bc67cc2738ed85cee51f718fef01960e1b752326f

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 05adb2b866ea20d86304cd76370df3d072d0b26ff44228fcf7a1c7a3ad7d1dbf
MD5 195a79f64382d3f9eb618c1ff194a9c0
BLAKE2b-256 0cbcfd778fa9fb8143890726a3e17ed5f3689bbfb59171e1bd81bbe4357b8d7a

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e1281ae04ca5770c57674c01c833bd184678afad1261a9549ff92ae8f1fddf80
MD5 9d137b30104151b6d440b74e4f9b270b
BLAKE2b-256 ea90fb02ee3eb730de6571861bd46c6773ebde9418ddc683953e6b6ac55d8e38

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 aef4893781dd9d4d11fa18a78e3dc84bccad83080d3652b2552d4405997f8680
MD5 36797036fda3303cb16df80956a0193b
BLAKE2b-256 5547bbbb25fd549bedbda1bb92d5f455992474dcc0e30d8f62a20812bd8eb14e

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 fe9faaad3839c5d7f99514f1c5c3005419c420216d60a74415c39e136d55c862
MD5 3793bcc97feb1d4ba462937cc3be8fa1
BLAKE2b-256 e9b92a3833f2a8386379c905e4088a285e0ac75a218f0fc1503752f90a735246

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 c0527e6eb7477a2e5a2ae606cb1ce6be0d471f85d5ca2ed443adff0c55e26f3b
MD5 bddbe983a28d226cedb032e75f9ee212
BLAKE2b-256 df8556493584a318901aa3eb4426b3f6a875b56e645152b92cf1b382eea0cf89

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 97d08ee0f4a7fdb419bc7da9e62c00067db5ca4eb42c8f8847cee62c0ce15f42
MD5 9360fe22d6f4da947d96d90e4a786642
BLAKE2b-256 b8b1a2095a839f629327bf44589d85917a90d963064fe276f98f2ca3aca83576

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 9b4eb6a8977aedf3b7ec67f120d5aadd6634aef191b172e5ed3ed4829b0e04c5
MD5 714cd6f2a9f3ed4c5e0ca31c7ae36221
BLAKE2b-256 09b6cd145ce5fdc340673eb235bedf789bf16532c471579298d85e658257bf44

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp37-none-win_amd64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 1e55becae8d3723ba091dd0afd4c2babfd8decbd1444bb455590890a86ba02f7
MD5 4ecb23e6496449d21e964309c88bd244
BLAKE2b-256 ea51e15c38f9451e2311b94548a924f3e29f75d4cea565b5240a83f292d0a10c

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 c46e4b0792761c2c3363a24f9df9cdb85289ce8a4e44b4720ce365c353669fa3
MD5 16d8e1dd5e72d2aa4a839e089dc542a4
BLAKE2b-256 499edc99bdf84ecc4c716221f4de6674acd6075ae32daaa133bdf9297418b45d

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e0d42c5948350ebb5333cbe89f7f0a840a90bc25364ea5e9594f71f85be56c91
MD5 a0109fb4f3654f0ffdbf68fc2eb16cd4
BLAKE2b-256 e2922a4495f15bd50568801b12e5739b1351507cb7b43cd33eb2c95ad0abb9a6

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.6-cp36-none-win_amd64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.6-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 efca6a2936cece6c3691fd4b95bf4f6edd9c8ebadd4b38a2c644804f9ca7c106
MD5 e711092b155b95482bb9170d142acad1
BLAKE2b-256 402d4b0f0895b530813c5faf69ce3ffc629bfe2e4bfd377485d615814e3b0ac9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page