Datasets:
Formats:
csv
Languages:
English
Size:
< 1K
Tags:
robotics
embodied-ai
human-demonstration
egocentric-video
bimanual-manipulation
tactile-sensing
License:
File size: 5,287 Bytes
9ff4bea | 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | from __future__ import annotations
import warnings
from collections.abc import Sequence
from typing import TYPE_CHECKING
import numpy as np
import numpy.typing as npt
import pyarrow as pa
from .._color_conversion import u8_array_to_rgba
from ..error_utils import RerunWarning
if TYPE_CHECKING:
from . import Rgba32ArrayLike, Rgba32Like
def _numpy_array_to_u32(data: npt.NDArray[np.uint8 | np.float32 | np.float64]) -> npt.NDArray[np.uint32]:
if data.size == 0:
return np.array([], dtype=np.uint32)
if data.dtype.type in [np.float32, np.float64]:
array = u8_array_to_rgba(np.asarray(np.round(np.asarray(data) * 255.0), np.uint8))
else:
array = u8_array_to_rgba(np.asarray(data, dtype=np.uint8))
return array
class Rgba32Ext:
"""Extension for [Rgba32][rerun.encodings.Rgba32]."""
"""
Extension for the `Rgba32` datatype.
Possible input for `Rgba32`:
- Sequence[int]: interpreted as rgb or rgba values in 0-255 range
- numpy array: interpreted as rgb or rgba values, range depending on dtype
- anything else (int or convertible to int): interpreted as a 32-bit packed rgba value
Possible inputs for `Rgba32Batch()`:
- a single `Rgba32` instance
- a sequence of `Rgba32` instances
- Nx3 or Nx4 numpy array, range depending on dtype
"""
@staticmethod
def rgba__field_converter_override(data: Rgba32Like) -> int:
from . import Rgba32
if isinstance(data, Rgba32):
return data.rgba
if isinstance(data, np.ndarray):
return int(_numpy_array_to_u32(data.reshape((1, -1)))[0])
elif isinstance(data, Sequence):
data = np.array(data).reshape((1, -1))
if data.size not in (3, 4):
raise ValueError(f"expected sequence of length of 3 or 4, received {data.size}")
return int(_numpy_array_to_u32(data)[0])
else:
return int(data)
@staticmethod
def native_to_pa_array_override(data: Rgba32ArrayLike, data_type: pa.DataType) -> pa.Array:
from . import Rgba32
if isinstance(data, int) or isinstance(data, Rgba32):
# A single packed int or Rgba32 (which implements __int__())
int_array = np.array([data])
elif isinstance(data, Sequence) and len(data) == 0:
# An empty array
int_array = np.array([])
else:
# Try to coerce it to a numpy array
try:
arr = np.asarray(data).squeeze()
# If the array is flat
if len(arr.shape) <= 1:
# And not one of the known types
if arr.dtype not in (np.uint8, np.float32, np.float64, np.uint32):
# And not a length 3 or 4 array:
if arr.size not in (3, 4):
# We assume this is packed ints
arr = arr.astype(np.uint32)
# Otherwise, if all the values are less than 256
elif np.max(arr) < 256:
# Then treat it as a single color
arr = arr.reshape((1, -1))
else:
# But if not, then send a warning to the user
warnings.warn(
f"Ambiguous input for colors of length {arr.size}. If using 0xRRGGBBAA values, please wrap as np.array with dtype=np.uint32",
category=RerunWarning,
stacklevel=7,
)
arr = arr.astype(np.uint32)
elif arr.dtype != np.uint32:
if len(arr.shape) <= 1:
if arr.size > 4:
# multiple RGBA colors
arr = arr.reshape((-1, 4))
else:
# a single color
arr = arr.reshape((1, -1))
if arr.dtype == np.uint32:
# these are already packed values
int_array = arr.flatten()
else:
int_array = _numpy_array_to_u32(arr) # type: ignore[assignment]
except (ValueError, TypeError, IndexError):
# Fallback support
data_list = list(data) # type: ignore[arg-type]
# First try to coerce it to a single Rgba32 instance
try:
data_list = [Rgba32(data_list)] # type: ignore[arg-type]
except (IndexError, ValueError):
pass
# Finally, handle heterogeneous sequence of Rgba32-like object,
# such as Rgba32 instances, ints, sub-sequence, etc.
#
# Note how this is simplified by the flexible implementation of
# `Rgba32`, thanks to its converter function and the
# auto-generated `__int__()` method.
int_array = np.array([Rgba32(datum) for datum in data_list], np.uint32) # type: ignore[arg-type]
return pa.array(int_array, type=data_type)
|