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

Uploaded Source

Built Distributions

ragged_buffer-0.2.11-cp310-none-win_amd64.whl (210.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

ragged_buffer-0.2.11-cp310-cp310-macosx_10_7_x86_64.whl (253.8 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

ragged_buffer-0.2.11-cp39-none-win_amd64.whl (210.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

ragged_buffer-0.2.11-cp39-cp39-macosx_10_7_x86_64.whl (253.8 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

ragged_buffer-0.2.11-cp38-none-win_amd64.whl (210.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

ragged_buffer-0.2.11-cp38-cp38-manylinux_2_24_x86_64.whl (274.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.2.11-cp38-cp38-macosx_10_7_x86_64.whl (253.8 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

ragged_buffer-0.2.11-cp37-none-win_amd64.whl (210.1 kB view details)

Uploaded CPython 3.7 Windows x86-64

ragged_buffer-0.2.11-cp37-cp37m-macosx_10_7_x86_64.whl (253.8 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

ragged_buffer-0.2.11-cp36-none-win_amd64.whl (204.5 kB view details)

Uploaded CPython 3.6 Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for ragged_buffer-0.2.11.tar.gz
Algorithm Hash digest
SHA256 8e04ea195356ab3ed8ed759a22f343830047d3d3f04dc7e2d56b9349983c49b5
MD5 7df75bebd38b7b043830e07e7dcbfa75
BLAKE2b-256 13dacc46d299990ae282f398fdf66dcc7d8430cb7ef1bf0b866a89b06ae98057

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 5eed7c9104b8f0891c9d9be2fa12fe633ded01d2e9293d4a1f9c247c61f31d52
MD5 8e1cc7f34a0131da30584b6d11a261a6
BLAKE2b-256 3d63f1a522930c81c4c4bb083725bf3351cc94e76e5c84afa8b3fbcbdf732cf8

See more details on using hashes here.

File details

Details for the file ragged_buffer-0.2.11-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 475339854fa4afed3703e3e7d986705b30a8e1111b2f9b72b9d1e4b1be402206
MD5 8d8db75159c27fbbed5e914f33e9cf4d
BLAKE2b-256 61b79dd6a18bfecbd3009c7b7004fc2a2b774039a9414a2aca00b1b410055f32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 0811a6e903f73c9c0c11cdc69c5bf7afc86911b74822cd10e0018ca5f34c36f4
MD5 b7f72099deed4d409c480294ba0cae0f
BLAKE2b-256 ba176a0348f099b09ef7256ad8f2006ec51a8204c19200bec563d133e0b76715

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 f1d87f95d9b75de07b98ff7a684130d80f8eaffd8efa2916ca04e2275ef607ec
MD5 f5b3dea03329d2979a5deaecc06c5c36
BLAKE2b-256 0f1cc95a144462b4ec430fca28a4d6dc1ebeeef43e38ac906601270d28b31731

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 960f3f144cbabdbe033ec64ebef720d48149fe9c2d60cf3ddafe68ea442b2b38
MD5 46666b45eb187a753cdf43cc7661f319
BLAKE2b-256 3d0e3fde364de39231b71e8b50f275871cd0b7b9f0f4561bd02cf85d273b8abd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 63268b8f988bfe9e6141697446ba8b853737a84eee68f658874151794b8c7257
MD5 5a4e6328f68140ade527782007a18089
BLAKE2b-256 41ec3f0bd6c662c3fb048575d31fccfb9816b2d962b6a05fef2d953d809b4cc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 975715867186a4a963736d0f5031f00afa87efd80adbac4276b969620011ebb2
MD5 5b2c59e2bbcde2fb8bf1ca39f6dba902
BLAKE2b-256 71ef50e0ced723b30a3e67dd7b77ace4410592959b4fbe172150299a08342de1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 950cbed3e2f36b0cd7ede893c60b91a6ef12d3820cdbddd83b538438cd218d0b
MD5 6704bda204e5a0b31b16340dbbec81aa
BLAKE2b-256 8a23ef2b94a94fe287d19a707f9c9d3715b2350f2aa3f1453f06b0207be38459

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e1244c1dc089f85ec6584f55dd867c3c06ef75c3bf383cdeff5ce2e18df4f0d3
MD5 713b8f138cb43347284bdfd174f2b2f0
BLAKE2b-256 b20174b7cd7fef8cd78850010be2bf93bc4c9640c03d774b8b13089a79c30eca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.11-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 9433f480beebef25eda8146264278a81d596d378ddffaadba4d822241021341b
MD5 1208c472eca7fef962c7f16e819fb257
BLAKE2b-256 6ff97ae5208a1988b062a279f89ff64dc0edf07f958625db76d1c36c7a9a1a9a

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