Skip to main content

Efficient RaggedBuffer datatype that implements 3D arrays with variable-length 2nd dimension.

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.7.tar.gz (21.7 kB view details)

Uploaded Source

Built Distributions

ragged_buffer-0.3.7-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl (320.1 kB view details)

Uploaded PyPy manylinux: glibc 2.5+ x86-64

ragged_buffer-0.3.7-cp310-none-win_amd64.whl (264.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

ragged_buffer-0.3.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl (321.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.5+ x86-64

ragged_buffer-0.3.7-cp39-none-win_amd64.whl (264.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

ragged_buffer-0.3.7-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (321.3 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

ragged_buffer-0.3.7-cp39-cp39-macosx_10_7_x86_64.whl (297.0 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

ragged_buffer-0.3.7-cp38-none-win_amd64.whl (264.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

ragged_buffer-0.3.7-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (321.4 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

ragged_buffer-0.3.7-cp38-cp38-macosx_10_7_x86_64.whl (297.8 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

ragged_buffer-0.3.7-cp37-none-win_amd64.whl (263.9 kB view details)

Uploaded CPython 3.7 Windows x86-64

ragged_buffer-0.3.7-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (321.5 kB view details)

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

ragged_buffer-0.3.7-cp37-cp37m-macosx_10_7_x86_64.whl (297.9 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

ragged_buffer-0.3.7-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (320.5 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.5+ x86-64

File details

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

File metadata

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

File hashes

Hashes for ragged_buffer-0.3.7.tar.gz
Algorithm Hash digest
SHA256 ff9fe024db3d71fd467e6069ab8491f1e188a999a98c54211b719cac2dd1cc2b
MD5 6ddfefb9cc8047a11f5209ddfe738584
BLAKE2b-256 d14107d261301da07943567f7e74db3ac2821b45a1d2c97c6d201bfc1a438153

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ec97f9e80c32b75f6fada76e476ea81629b9a2918a97467489cb28c78ac75d68
MD5 2e42a4381ea6431301e84a4113591c8a
BLAKE2b-256 c4b8bdcfd852ec090f7a8447a20a5c352347a92742079dda6db04278a2bc5deb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 8ef175c84a66f6133866b5a3c266f8a9da569d16405eaa4b8f067b26c11c51ca
MD5 ee42dd05c160561b1da7903b7a973ca2
BLAKE2b-256 841103892dda5e538dd45c9deda567fdd6a0e59948978b0916e7756213a5720f

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7d66de16710a09611577560787acce43aa8a556ade0af7323f121461765025db
MD5 1eaabedfef7d9651f62f355d9f4fd62b
BLAKE2b-256 eb39e9097549266e8615ad6e51f1f11c9b631990f2b3a3f35d61a5c7718ac478

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 edef89ddda66804c825876fb9b5642de7e1a24b5ac17bda2955eaba161629d7f
MD5 4c0dd61041e104ead8066bbf38dbe420
BLAKE2b-256 b709a0d9264f51f253a2bf39fda5c9e025d5d24cb8a868eeef28b85610e76db9

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 15c1ebab26c4c7b8e5fef1ea2fbeb1de376f1afbd0f0810d9f30860caa4f8285
MD5 6d88ed661cf478702a361c7a8d16294a
BLAKE2b-256 bf84d1cc7247e6d04a974cb0a27c3c42603d2ffa283f3054d54c3f69f2aeaf69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e575b13501a5f44fd60a41067818c986e0b1aca9ef21b7093f01f233e10bb9ca
MD5 66c12e0342e11a28dbbfbfc9bd70a7f0
BLAKE2b-256 4e94a09612d44adb799277004d46d8e57ae06ccd92c633fb14617ff5a058b696

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 e3372816d5733f6c5d435c7ef65a66bc7e13cd1de2711fc19da5b6a79d903819
MD5 ef5de8a73f7b356578588005f7194549
BLAKE2b-256 9e1cf9e3e050f8ab11b9d1b6ab4fab539f7099e1a7499f66d0e374a032f2693a

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 31a3ef16ba336546e731c7d76cdd126ea6ef7e6da50399124222f368a8cc89cc
MD5 75439202ddef489a27fffeaede5ba012
BLAKE2b-256 1d421b09909e7f5bbe4439c95e3943adfbc67577c138dfea026a6719fa2049a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 8ffb25c91b2dafbf455ca3948f14137b3a20b4e110cc7f8800307fa1a9d38cc6
MD5 3df3e0d03dd43ebc95524a17bb8a7b86
BLAKE2b-256 f2c420433f2423dc5bae417acfac09879a45897d4112a95527ce592e66073319

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 de95b25cea15a2d18acc3e49f0ac2c693eb4098fecdc43bf81a663430c8d707d
MD5 e029510689abcec5efb074a731ec3cd3
BLAKE2b-256 f4d8abd3c3d458fa2ddc71063033bda7d2128120d0fb271cddd74cbf6be07fa1

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 101a256f9797922c3e6b3f4e125311e1f60ef25791b77b26e37ab1e1f793a31f
MD5 1686790bfc80d9e5b0a8740d0d843abd
BLAKE2b-256 0c9558277f50ba04f789d9dc0a691c0c8ac7df73c0857723a84ab8456068e3c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c89e53c9f58d911a022f843d4bc9e8f5311898d6ab18c29c6165529b6b737d25
MD5 abf9cbbc94c37ceb305a76d95e7442a1
BLAKE2b-256 eaa764fbb560d7c110ff698a1cc59d4dc6978bb29994678d12d03020404bf95e

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.3.7-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.3.7-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b733912269f80dcc4f07ba75c625322239215992c8753788da5acb8cf1e02d8f
MD5 9b4f02c98478cf7c2265a78ddd99d134
BLAKE2b-256 56b54033548121a0fda03890a444ac999d533f8c6874c6790a06f60dc7cc56d4

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