Skip to content

Commit

Permalink
Fix not taking np.array for single colors (#2569)
Browse files Browse the repository at this point in the history
plus fix bad error message when supplying single dimensional position
array to line_segments

Fixes #2486
*  #2486

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I have tested https://demo.rerun.io/pr/2569 (if applicable)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2569

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/89f20b6/docs
Examples preview: https://rerun.io/preview/89f20b6/examples
<!-- pr-link-docs:end -->
  • Loading branch information
Wumpf authored Jun 30, 2023
1 parent 8384714 commit 4b9ff53
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions rerun_py/rerun_sdk/rerun/components/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions rerun_py/rerun_sdk/rerun/log/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/rects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/rerun_sdk/rerun/log/text_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4b9ff53

Please sign in to comment.