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 d24c8a8
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 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
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun2/components/class_id_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: typ
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))
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun2/components/color_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun2/components/instance_key_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: typ
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))
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun2/components/keypoint_id_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: typ
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: 5 additions & 3 deletions rerun_py/rerun_sdk/rerun2/components/point2d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Point2DArrayExt:
@staticmethod
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))
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun2/components/radius_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def _from_similar(data: Any | None, *, mono: type, mono_aliases: Type, many: typ
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))
8 changes: 5 additions & 3 deletions rerun_py/rerun_sdk/rerun2/datatypes/vec2d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Vec2DArrayExt:
@staticmethod
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
Expand Up @@ -2,6 +2,8 @@

import itertools

from typing import Sequence, Any

import numpy as np
import rerun2 as rr
from rerun2 import components as rrc
Expand All @@ -18,6 +20,8 @@

def test_points2d() -> None:
points_arrays = [
[],
np.array([]),
# Point2DArrayLike: Sequence[Point2DLike]: Point2D
[
rrc.Point2D([1, 2]),
Expand All @@ -29,7 +33,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 +49,8 @@ def test_points2d() -> None:

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

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

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

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

keypoint_id_arrays = [
[],
np.array([]),
# KeypointIdArrayLike: Sequence[KeypointIdLike]: int
[2, 3],
# KeypointIdArrayLike: Sequence[KeypointIdLike]: KeypointId
Expand All @@ -202,6 +215,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 +262,20 @@ 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 d24c8a8

Please sign in to comment.