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

Uploaded Source

Built Distributions

ragged_buffer-0.3.4-cp310-none-win_amd64.whl (263.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

ragged_buffer-0.3.4-cp39-none-win_amd64.whl (263.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

ragged_buffer-0.3.4-cp39-cp39-manylinux_2_24_x86_64.whl (324.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.3.4-cp39-cp39-macosx_10_7_x86_64.whl (299.6 kB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

ragged_buffer-0.3.4-cp38-none-win_amd64.whl (263.5 kB view details)

Uploaded CPython 3.8 Windows x86-64

ragged_buffer-0.3.4-cp38-cp38-manylinux_2_24_x86_64.whl (325.3 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

ragged_buffer-0.3.4-cp38-cp38-macosx_10_7_x86_64.whl (299.8 kB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

ragged_buffer-0.3.4-cp37-none-win_amd64.whl (263.6 kB view details)

Uploaded CPython 3.7 Windows x86-64

ragged_buffer-0.3.4-cp37-cp37m-manylinux_2_24_x86_64.whl (325.4 kB view details)

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

ragged_buffer-0.3.4-cp37-cp37m-macosx_10_7_x86_64.whl (299.8 kB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

ragged_buffer-0.3.4-cp36-none-win_amd64.whl (261.4 kB view details)

Uploaded CPython 3.6 Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for ragged_buffer-0.3.4.tar.gz
Algorithm Hash digest
SHA256 7eddcc9735fa5505c793371a75d9054b81ec3ddfcad9e3622f6e4b0673f8ec1c
MD5 320e11ce8b5849fbe3e380a03766febb
BLAKE2b-256 b78b8ee401837a3bd2b9212ff8b5150bee72941eff106a105cd82d1126af429d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 12e0037a65abbba354c5f046cf71aefc704a738bcaf691135e2a051a298a3030
MD5 f46cbd044574577f85764819e11916b6
BLAKE2b-256 805d2bc29169d3bb7355a8e273c6f4de5050417619e3667e8f649d8e160cd3aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 0f4d5ef52068aaf7b740f66a68647fe5f2b30b9cad2851b308d723950f757ad9
MD5 8095aca47a59fdf9d3e9a2d08023ade1
BLAKE2b-256 58a19641773e91db00c024251e0fbe81e8d91063545209e53f1ac05bcee24937

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 0574bbe61d8d0daa7157adf3325f4de0acb9672c2ebd8b3ad6c517a9ce67fee5
MD5 25408a70bd55f28b1925037115f0209b
BLAKE2b-256 e303c4d45eb24cf7f637d7a1a004c8f047e7497fb18e0194cd1a28f77d48a386

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a2fa864a9f570c6822e4e5cfd79b3c043b9923bdc63bc7cc72dd2ae596e82558
MD5 4d50e2c77031155cd6413cd4f8ce31f8
BLAKE2b-256 b1feb3ffe8e366e54106706878e9262a9abe9de4041622d0bc682ce69eb9b893

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 570768e2d2909249802cd5b6d760001e55ffdb88d1d4bccb11921cf895a9eaab
MD5 5110eecdcc89b2179baf1a021eba5c5e
BLAKE2b-256 298e743912c109e40f55b7d98ce0132e9127e45dee05374cd5670477d455e84f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 f54cb2f5c659c7e33c538671400b235482b9968cbe81ba04ff15c50ae52e5d21
MD5 05c068ee8358c068d2649e4dcdfc44d6
BLAKE2b-256 58c75d6e89b502784445ce5c57bb017bc9e4183d3876f07d0b0aa70cc4afcbd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4232a0e0722637873cf59df4883f6857f381a610c52e52f062b255850530ec06
MD5 552a29d506f01a419a877f3b3955fed1
BLAKE2b-256 a0906ae8102362e0198d189dda0fcff1f2c22277c1b9a8db84a280e0dd270b18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 3d07026946845ace2d1af352795df0dcc546415dbc3a5960f4ffdd89884015e0
MD5 f8b0d625a5af09a8a706c18cef316ed9
BLAKE2b-256 dfce689f531d9dbc8c601bc895b71361357057bf11da629e958fa7ef2856bd76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 a5cef215b2ebfd5d55c3d49c03aed27f0311565ebb8385224d8d193b9688cc07
MD5 6a99f2831b8c92838e6528e6eeff502e
BLAKE2b-256 5836a826789762796f157b24663c681ce3fb3140da85a22206b55adc83a0008c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7cf7199bbf7b96814af36b3ffd23a0df0ccada27a5dece089e6fcf7557015f58
MD5 53c79c7db8007cdc80a9f82f3a4aebf4
BLAKE2b-256 ec7fc5f67a1e05e764ea68f072635be288f4d0dc79d22bae60081b1036bbc8f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ragged_buffer-0.3.4-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 e40da532b8487bedece96d4a334660feff81a60ce949ccdb4c6b276162627dc1
MD5 75cc554bbd34171e276c2f6f166ce2ad
BLAKE2b-256 2f28f73acb8ae63651b078d7c9fe01819e0d26b19cbe247d476f2f4834c8fd04

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