From 89f20b66be501399dedf53cde2423e5ede54c83d Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Thu, 29 Jun 2023 17:02:22 +0200 Subject: [PATCH] Fix not taking np.array for single colors + fix bad error message when supplying single dimensional position array to line_segments --- rerun_py/rerun_sdk/rerun/components/color.py | 2 ++ rerun_py/rerun_sdk/rerun/log/arrow.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/bounding_box.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/lines.py | 12 ++++++------ rerun_py/rerun_sdk/rerun/log/points.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/rects.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/scalar.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/text.py | 4 ++-- rerun_py/rerun_sdk/rerun/log/text_internal.py | 4 ++-- 9 files changed, 22 insertions(+), 20 deletions(-) diff --git a/rerun_py/rerun_sdk/rerun/components/color.py b/rerun_py/rerun_sdk/rerun/components/color.py index d2ff97cd9965..b980d7616ab4 100644 --- a/rerun_py/rerun_sdk/rerun/components/color.py +++ b/rerun_py/rerun_sdk/rerun/components/color.py @@ -16,6 +16,8 @@ class ColorRGBAArray(pa.ExtensionArray): # type: ignore[misc] def from_numpy(array: npt.NDArray[np.uint8]) -> ColorRGBAArray: """Build a `ColorRGBAArray` from an numpy array.""" + if array.ndim == 1: + array = np.reshape(array, (1, -1)) storage = pa.array(u8_array_to_rgba(array), type=ColorRGBAType.storage_type) # TODO(john) enable extension type wrapper # return cast(ColorRGBAArray, pa.ExtensionArray.from_storage(ColorRGBAType(), storage)) diff --git a/rerun_py/rerun_sdk/rerun/log/arrow.py b/rerun_py/rerun_sdk/rerun/log/arrow.py index ca01b302a4e9..9b212693d1af 100644 --- a/rerun_py/rerun_sdk/rerun/log/arrow.py +++ b/rerun_py/rerun_sdk/rerun/log/arrow.py @@ -78,8 +78,8 @@ def log_arrow( vector = np.require(vector, dtype="float32") instanced["rerun.arrow3d"] = Arrow3DArray.from_numpy(origin.reshape(1, 3), vector.reshape(1, 3)) - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if label: diff --git a/rerun_py/rerun_sdk/rerun/log/bounding_box.py b/rerun_py/rerun_sdk/rerun/log/bounding_box.py index bbb17943cc89..b8d76e334aaa 100644 --- a/rerun_py/rerun_sdk/rerun/log/bounding_box.py +++ b/rerun_py/rerun_sdk/rerun/log/bounding_box.py @@ -105,8 +105,8 @@ def log_obb( else: raise TypeError("rotation should be 1x4") - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) # We store the stroke_width in radius diff --git a/rerun_py/rerun_sdk/rerun/log/lines.py b/rerun_py/rerun_sdk/rerun/log/lines.py index 888369a98789..784ea7bb2529 100644 --- a/rerun_py/rerun_sdk/rerun/log/lines.py +++ b/rerun_py/rerun_sdk/rerun/log/lines.py @@ -105,8 +105,8 @@ def log_line_strip( else: raise TypeError("Positions should be either Nx2 or Nx3") - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) # We store the stroke_width in radius @@ -190,13 +190,13 @@ def log_line_segments( # If not a multiple of 2, drop the last row if len(positions) % 2: positions = positions[:-1] - if positions.shape[1] == 2: + if positions.ndim > 1 and positions.shape[1] == 2: # Reshape even-odd pairs into a collection of line-strips of length2 # [[a00, a01], [a10, a11], [b00, b01], [b10, b11]] # -> [[[a00, a01], [a10, a11]], [[b00, b01], [b10, b11]]] positions = positions.reshape([len(positions) // 2, 2, 2]) instanced["rerun.linestrip2d"] = LineStrip2DArray.from_numpy_arrays(positions) - elif positions.shape[1] == 3: + elif positions.ndim > 1 and positions.shape[1] == 3: # Same as above but for 3d points positions = positions.reshape([len(positions) // 2, 2, 3]) instanced["rerun.linestrip3d"] = LineStrip3DArray.from_numpy_arrays(positions) @@ -205,8 +205,8 @@ def log_line_segments( # The current API splats both color and stroke-width, though the data-model doesn't # require that we do so. - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) splats["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) # We store the stroke_width in radius diff --git a/rerun_py/rerun_sdk/rerun/log/points.py b/rerun_py/rerun_sdk/rerun/log/points.py index c71bdf5bb6af..ceb6233ceca5 100644 --- a/rerun_py/rerun_sdk/rerun/log/points.py +++ b/rerun_py/rerun_sdk/rerun/log/points.py @@ -116,8 +116,8 @@ def log_point( else: raise TypeError("Position must have a total size of 2 or 3") - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if radius: diff --git a/rerun_py/rerun_sdk/rerun/log/rects.py b/rerun_py/rerun_sdk/rerun/log/rects.py index 7d6fcce35e57..e18c54f8c9a9 100644 --- a/rerun_py/rerun_sdk/rerun/log/rects.py +++ b/rerun_py/rerun_sdk/rerun/log/rects.py @@ -85,8 +85,8 @@ def log_rect( instanced["rerun.rect2d"] = Rect2DArray.from_numpy_and_format(rects, rect_format) - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if label: diff --git a/rerun_py/rerun_sdk/rerun/log/scalar.py b/rerun_py/rerun_sdk/rerun/log/scalar.py index 6e4d9f909d5e..c58f0ea7d05c 100644 --- a/rerun_py/rerun_sdk/rerun/log/scalar.py +++ b/rerun_py/rerun_sdk/rerun/log/scalar.py @@ -130,8 +130,8 @@ def log_scalar( if label: instanced["rerun.label"] = LabelArray.new([label]) - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if radius: diff --git a/rerun_py/rerun_sdk/rerun/log/text.py b/rerun_py/rerun_sdk/rerun/log/text.py index 2cc333ba6240..5088b2c664eb 100644 --- a/rerun_py/rerun_sdk/rerun/log/text.py +++ b/rerun_py/rerun_sdk/rerun/log/text.py @@ -117,8 +117,8 @@ def log_text_entry( else: logging.warning(f"Null text entry in log_text_entry('{entity_path}') will be dropped.") - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if ext: diff --git a/rerun_py/rerun_sdk/rerun/log/text_internal.py b/rerun_py/rerun_sdk/rerun/log/text_internal.py index 620bef756671..b455bb71c118 100644 --- a/rerun_py/rerun_sdk/rerun/log/text_internal.py +++ b/rerun_py/rerun_sdk/rerun/log/text_internal.py @@ -92,8 +92,8 @@ def log_text_entry_internal( else: logging.warning(f"Null text entry in log_text_entry('{entity_path}') will be dropped.") - if color: - colors = _normalize_colors([color]) + if color is not None: + colors = _normalize_colors(color) instanced["rerun.colorrgba"] = ColorRGBAArray.from_numpy(colors) if splats: