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 two RaggedBuffer variants, RaggedBufferF32 (storing float32 values) and RaggedBufferI64 (storing int64 values).

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

Uploaded Source

Built Distributions

ragged_buffer-0.2.1-cp310-none-win_amd64.whl (187.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

ragged_buffer-0.2.1-cp310-cp310-macosx_10_7_x86_64.whl (237.8 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

ragged_buffer-0.2.1-cp39-none-win_amd64.whl (188.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

ragged_buffer-0.2.1-cp39-cp39-macosx_10_7_x86_64.whl (237.8 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

ragged_buffer-0.2.1-cp38-none-win_amd64.whl (188.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

ragged_buffer-0.2.1-cp38-cp38-manylinux_2_24_x86_64.whl (256.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.2.1-cp38-cp38-macosx_10_7_x86_64.whl (237.8 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

ragged_buffer-0.2.1-cp37-none-win_amd64.whl (188.1 kB view details)

Uploaded CPython 3.7 Windows x86-64

ragged_buffer-0.2.1-cp37-cp37m-macosx_10_7_x86_64.whl (237.9 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

ragged_buffer-0.2.1-cp36-none-win_amd64.whl (185.0 kB view details)

Uploaded CPython 3.6 Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for ragged_buffer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c6fd699e0dafd42c381ccefd0fc977a64b8f2c3221a5ef37ed38074518091d12
MD5 bcd5a7aab2e9cc8c5c2789dba781d24b
BLAKE2b-256 f85e42212727d8fdecad661b8be719feec9151d8e8b1cf96501690f7c40ae2ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 f1ba271dd9567f371e1a3452b30e0865bd6a0189b11ff26c0b06938e7214096a
MD5 314a1decd763de3302eb6b78e40cdb16
BLAKE2b-256 c9f39b676e72dcc50059843aa6afb00f4a71d116b72ce8a5d2e756832d08d345

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 e1a9581942acf280cda8826c9fe9909b0ee2a946e472f9388e25464aff856b99
MD5 ce376f0c7a4d5ef0bf8a7fc5e30a2d05
BLAKE2b-256 5675668dc187f1e77bfc61f2dc9ee03ee469c7e6c886097bf937e2e56ec32594

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 af47e005b870d773cfdf827775163086b8be0840e0bd0878489cc0d59198d4da
MD5 b0c76a4f6ee424a576d154f3af8b7f13
BLAKE2b-256 3c455326fb5d820bf592ce7147278fa58254429ff26cb18449a9b5f754242a25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 d96ddc9c4aa88ea98949152010473080a1ca815abe225410f8913d04a4765f37
MD5 2ae49425679751505ce2b508749e89db
BLAKE2b-256 de47c447f64b7f5ccd2105f8b6f77feb0025a77b8f629ebfe3170a3d721d98b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 8919fefc5a51ad6b16833994e973f66ecf9241a9ed1c13315a8cfcc0e23eb1c5
MD5 8686d02cb6f8cf8bfb1bdaf5575edeb6
BLAKE2b-256 7adfa498046dc3783c0d6d954100cf21d4cefcfe0f0fba717520a9682a4ebfd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 88f5a01ce7c69e533dac1222514c64bb2570ca21a25453c77c8a626a92fa509b
MD5 220a422a9d77ffd78ade83fe8a2c39f0
BLAKE2b-256 02d26e7627b8bb5befcdd0b576bc7eb32734a394bbd5ed784d09039ad603dc9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 b71762070b906d2f2e1d82acd4199252a19ad5276307f25345e768e9ec0231c5
MD5 94ae6701c87e94df380f92725b443c64
BLAKE2b-256 8997e22d66b90f76a1b136e629b352cb9baeeb567cf63a27dccd4d3edda701b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 0e0da432e67a9ef001a1e35fe1564f13fa7bd2d05f31254fe274c5a6550d8eab
MD5 d032d0fc99607d5f9bb239e3f5922c43
BLAKE2b-256 4d4e95b903c5be0a5fbde11ea250afe3dd978b48284e5152d30728c12c7519af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 ded5f5b26878729731ed10bfffd8917767e6ea471d1714efa9cbe1ca00353366
MD5 40163b747d8860dd7f55d497a12836a8
BLAKE2b-256 a0b73398ddd5949db32bc32a5173965cd15d6321088fb5e95bfd013968aeb4f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.2.1-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 942f05ee1039425ce9bbbbec3e13750e5b29dc96e3c0935adfc315636aa7b0b3
MD5 ae127d09df0e09f057e08a50fd8cc631
BLAKE2b-256 506fdc80a679f44fa37607a0765bc42bffc3329e60d15db3f3d24ba11db208d8

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