Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 14, 2023
1 parent 51553d6 commit b187381
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 39 deletions.
4 changes: 4 additions & 0 deletions crates/re_types/definitions/rerun/components/color.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace rerun.components;
// ---

/// An RGBA color tuple with unmultiplied/separate alpha, in sRGB gamma space with linear alpha.
///
/// \py Float colors are assumed to be in 0-1 gamma sRGB space.
/// \py All other colors are assumed to be in 0-255 gamma sRGB space.
/// \py If there is an alpha, we assume it is in linear space, and separate (NOT pre-multiplied).
struct Color (
"attr.arrow.transparent",
"attr.python.aliases": "int, npt.NDArray[np.uint8], npt.NDArray[np.uint32], npt.NDArray[np.float32], npt.NDArray[np.float64]",
Expand Down
6 changes: 3 additions & 3 deletions rerun_py/rerun_sdk/rerun2/components/class_id_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

__all__ = ["ClassIdArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class ClassIdArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
array = np.asarray([class_id.id for class_id in data], np.uint16)
else:
array = np.require(np.asarray(data), np.uint16).flatten()
array = np.asarray(data, dtype=np.uint16).flatten()

return arrow().wrap_array(pa.array(array, type=arrow().storage_type))
8 changes: 7 additions & 1 deletion rerun_py/rerun_sdk/rerun2/components/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

@dataclass
class Color:
"""An RGBA color tuple with unmultiplied/separate alpha, in sRGB gamma space with linear alpha."""
"""
An RGBA color tuple with unmultiplied/separate alpha, in sRGB gamma space with linear alpha.
Float colors are assumed to be in 0-1 gamma sRGB space.
All other colors are assumed to be in 0-255 gamma sRGB space.
If there is an alpha, we assume it is in linear space, and separate (NOT pre-multiplied).
"""

rgba: int

Expand Down
7 changes: 3 additions & 4 deletions rerun_py/rerun_sdk/rerun2/components/color_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

__all__ = ["ColorArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa

from rerun.color_conversion import u8_array_to_rgba


class ColorArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
"""
Normalize flexible colors arrays.
Expand All @@ -33,7 +32,7 @@ def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: typ
if array.dtype.type in [np.float32, np.float64]:
# Assume gamma-space colors
array = np.asarray(data).reshape((-1, 4))
array = u8_array_to_rgba(np.require(np.round(array * 255.0), np.uint8))
array = u8_array_to_rgba(np.asarray(np.round(array * 255.0), np.uint8))
elif array.dtype.type == np.uint32:
array = np.asarray(data).flatten()
else:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun2/components/draw_order_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

__all__ = ["DrawOrderArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class DrawOrderArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
array = np.asarray([draw_order.value for draw_order in data], np.float32)
else:
Expand Down
6 changes: 3 additions & 3 deletions rerun_py/rerun_sdk/rerun2/components/instance_key_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

__all__ = ["InstanceKeyArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class InstanceKeyArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
array = np.asarray([datum.value for datum in data], np.uint64)
else:
array = np.require(np.asarray(data), np.uint64).flatten()
array = np.asarray(data, dtype=np.uint64).flatten()

return arrow().wrap_array(pa.array(array, type=arrow().storage_type))
6 changes: 3 additions & 3 deletions rerun_py/rerun_sdk/rerun2/components/keypoint_id_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

__all__ = ["KeypointIdArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class KeypointIdArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
array = np.asarray([class_id.id for class_id in data], np.uint16)
else:
array = np.require(np.asarray(data), np.uint16).flatten()
array = np.asarray(data, dtype=np.uint16).flatten()

return arrow().wrap_array(pa.array(array, type=arrow().storage_type))
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun2/components/label_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

__all__ = ["LabelArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import pyarrow as pa


class LabelArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence):
array = [str(datum) for datum in data]
else:
Expand Down
12 changes: 7 additions & 5 deletions rerun_py/rerun_sdk/rerun2/components/point2d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

__all__ = ["Point2DArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class Point2DArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
arrays = [np.asarray(datum) for datum in data]
points = np.concatenate([np.asarray(datum.position, dtype=np.float32) for datum in data])
else:
arrays = np.require(np.asarray(data), np.float32).reshape((-1, 2)).tolist()
points = np.asarray(data, dtype=np.float32)

return arrow().wrap_array(pa.array(arrays, type=arrow().storage_type))
points = points.reshape((-1,))

return arrow().wrap_array(pa.FixedSizeListArray.from_arrays(points, type=arrow().storage_type))
6 changes: 3 additions & 3 deletions rerun_py/rerun_sdk/rerun2/components/radius_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

__all__ = ["RadiusArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class RadiusArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
array = np.asarray([radius.value for radius in data], np.float32)
else:
array = np.require(np.asarray(data), np.float32).flatten()
array = np.asarray(data, dtype=np.float32).flatten()

return arrow().wrap_array(pa.array(array, type=arrow().storage_type))
12 changes: 7 additions & 5 deletions rerun_py/rerun_sdk/rerun2/datatypes/vec2d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

__all__ = ["Vec2DArrayExt"]

from typing import Any, Sequence, Type
from typing import Any, Sequence

import numpy as np
import pyarrow as pa


class Vec2DArrayExt:
@staticmethod
def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: type, many_aliases: Type, arrow: type):
def _from_similar(data: Any | None, *, mono: type, mono_aliases: type, many: type, many_aliases: type, arrow: type):
if isinstance(data, Sequence) and (len(data) > 0 and isinstance(data[0], mono)):
arrays = [np.asarray(datum) for datum in data]
points = np.concatenate([np.asarray(datum.position, dtype=np.float32) for datum in data])
else:
arrays = np.require(np.asarray(data), np.float32).reshape((-1, 2)).tolist()
points = np.asarray(data, dtype=np.float32)

return arrow().wrap_array(pa.array(arrays, type=arrow().storage_type))
points = points.reshape((-1,))

return arrow().wrap_array(pa.FixedSizeListArray.from_arrays(points, type=arrow().storage_type))
34 changes: 26 additions & 8 deletions rerun_py/tests/unit/test_points2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import itertools
from typing import Any, Sequence

import numpy as np
import rerun2 as rr
Expand All @@ -18,6 +19,8 @@

def test_points2d() -> None:
points_arrays = [
[],
np.array([]),
# Point2DArrayLike: Sequence[Point2DLike]: Point2D
[
rrc.Point2D([1, 2]),
Expand All @@ -29,7 +32,7 @@ def test_points2d() -> None:
np.array([3, 4], dtype=np.float32),
],
# Point2DArrayLike: Sequence[Point2DLike]: Tuple[float, float]
[[1, 2], [3, 4]],
[(1, 2), (3, 4)],
# Point2DArrayLike: Sequence[Point2DLike]: Sequence[float]
[1, 2, 3, 4],
# Point2DArrayLike: npt.NDArray[np.float32]
Expand All @@ -45,6 +48,8 @@ def test_points2d() -> None:

radii_arrays = [
None,
[],
np.array([]),
# RadiusArrayLike: Sequence[RadiusLike]: float
[42, 43],
# RadiusArrayLike: Sequence[RadiusLike]: Radius
Expand All @@ -58,6 +63,8 @@ def test_points2d() -> None:

colors_arrays = [
None,
[],
np.array([]),
# ColorArrayLike: Sequence[ColorLike]: int
[
0xAA0000CC,
Expand Down Expand Up @@ -154,6 +161,7 @@ def test_points2d() -> None:

labels_arrays = [
None,
[],
# LabelArrayLike: Sequence[LabelLike]: str
["hello", "friend"],
# LabelArrayLike: Sequence[LabelLike]: Label
Expand All @@ -172,6 +180,8 @@ def test_points2d() -> None:
]

class_id_arrays = [
[],
np.array([]),
# ClassIdArrayLike: Sequence[ClassIdLike]: int
[126, 127],
# ClassIdArrayLike: Sequence[ClassIdLike]: ClassId
Expand All @@ -187,6 +197,8 @@ def test_points2d() -> None:
]

keypoint_id_arrays = [
[],
np.array([]),
# KeypointIdArrayLike: Sequence[KeypointIdLike]: int
[2, 3],
# KeypointIdArrayLike: Sequence[KeypointIdLike]: KeypointId
Expand All @@ -202,6 +214,8 @@ def test_points2d() -> None:
]

instance_key_arrays = [
[],
np.array([]),
# InstanceKeyArrayLike: Sequence[InstanceKeyLike]: int
[U64_MAX_MINUS_1, U64_MAX],
# InstanceKeyArrayLike: Sequence[InstanceKeyLike]: InstanceKey
Expand Down Expand Up @@ -247,17 +261,21 @@ def test_points2d() -> None:
)
print(f"{arch}\n")

assert arch.points == rrc.Point2DArray.from_similar([[1.0, 2.0], [3.0, 4.0]])
assert arch.radii == rrc.RadiusArray.from_similar([42, 43] if radii is not None else [])
assert arch.colors == rrc.ColorArray.from_similar([0xAA0000CC, 0x00BB00DD] if colors is not None else [])
assert arch.labels == rrc.LabelArray.from_similar(["hello", "friend"] if labels is not None else [])
assert arch.points == rrc.Point2DArray.from_similar([[1.0, 2.0], [3.0, 4.0]] if non_empty(points) else [])
assert arch.radii == rrc.RadiusArray.from_similar([42, 43] if non_empty(radii) else [])
assert arch.colors == rrc.ColorArray.from_similar([0xAA0000CC, 0x00BB00DD] if non_empty(colors) else [])
assert arch.labels == rrc.LabelArray.from_similar(["hello", "friend"] if non_empty(labels) else [])
assert arch.draw_order == rrc.DrawOrderArray.from_similar([300] if draw_order is not None else [])
assert arch.class_ids == rrc.ClassIdArray.from_similar([126, 127] if class_ids is not None else [])
assert arch.keypoint_ids == rrc.KeypointIdArray.from_similar([2, 3] if keypoint_ids is not None else [])
assert arch.class_ids == rrc.ClassIdArray.from_similar([126, 127] if non_empty(class_ids) else [])
assert arch.keypoint_ids == rrc.KeypointIdArray.from_similar([2, 3] if non_empty(keypoint_ids) else [])
assert arch.instance_keys == rrc.InstanceKeyArray.from_similar(
[U64_MAX_MINUS_1, U64_MAX] if instance_keys is not None else []
[U64_MAX_MINUS_1, U64_MAX] if non_empty(instance_keys) else []
)


def non_empty(v: Sequence[Any] | None) -> bool:
return v is not None and len(v) > 0


if __name__ == "__main__":
test_points2d()

0 comments on commit b187381

Please sign in to comment.