Skip to content

Commit

Permalink
tests and extensions for components.Label
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 13, 2023
1 parent dca1a9a commit 75f268d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 16 additions & 0 deletions rerun_py/rerun_sdk/rerun2/components/label_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

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):
if isinstance(data, Sequence):
array = [str(datum) for datum in data]
else:
array = [str(data)]

return arrow().wrap_array(pa.array(array, type=arrow().storage_type))
24 changes: 21 additions & 3 deletions rerun_py/tests/unit/test_points2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from rerun_sdk import rerun2 as rr
from rerun_sdk.rerun2 import components as rrc

# TODO(cmc): roundtrips (serialize in python, deserialize in rust)


def test_points2d() -> None:
points_arrays = [
Expand Down Expand Up @@ -49,27 +51,43 @@ def test_points2d() -> None:
np.array([42, 43]),
]

# TODO: color

labels_arrays = [
None,
# LabelArrayLike: Sequence[LabelLike]: str
["hello", "friend"],
# LabelArrayLike: Sequence[LabelLike]: Label
[
rrc.Label("hello"),
rrc.Label("friend"),
],
]

all_permuted_arrays = list(
itertools.product( # type: ignore[call-overload]
*[
points_arrays,
radii_arrays,
labels_arrays,
]
)
)

for points, radii in all_permuted_arrays:
for points, radii, labels in all_permuted_arrays:
print(
f"rr.Points2D(\n"
f" {points}\n"
f" radii={radii}\n"
f" labels={labels}\n"
f")"
)
arch = rr.Points2D(points, radii=radii)
arch = rr.Points2D(points, radii=radii, labels=labels)
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 [])
print(f"{arch}\n")
assert arch.labels == rrc.LabelArray.from_similar(["hello", "friend"] if labels is not None else [])


if __name__ == "__main__":
Expand Down

0 comments on commit 75f268d

Please sign in to comment.