Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle older numpy versions / py 3.8 in VecND extensions #2896

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def arrow3d_native_to_pa_array(data: Arrow3DArrayLike, data_type: pa.DataType) -
# TODO(ab): not quite sure why i must unwrap `xyz` or face a cryptic error otherwise.

if isinstance(data, Arrow3D):
origins = rrd.Vec3DArray.from_similar(data.origin.xyz).storage
vectors = rrd.Vec3DArray.from_similar(data.vector.xyz).storage
origins = rrd.Vec3DArray.from_similar(data.origin).storage
vectors = rrd.Vec3DArray.from_similar(data.vector).storage
else:
origins = rrd.Vec3DArray.from_similar([d.origin.xyz for d in data]).storage
vectors = rrd.Vec3DArray.from_similar([d.vector.xyz for d in data]).storage
origins = rrd.Vec3DArray.from_similar([d.origin for d in data]).storage
vectors = rrd.Vec3DArray.from_similar([d.vector for d in data]).storage

return pa.StructArray.from_arrays(
arrays=[origins, vectors],
Expand Down
32 changes: 31 additions & 1 deletion rerun_py/rerun_sdk/rerun/_rerun2/datatypes/_overrides/vecxd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Sequence

import numpy as np
import pyarrow as pa
Expand All @@ -9,16 +9,46 @@
from .. import Vec2DArrayLike, Vec3DArrayLike, Vec4DArrayLike


NUMPY_VERSION = tuple(map(int, np.version.version.split(".")[:2]))


def vec2d_native_to_pa_array(data: Vec2DArrayLike, data_type: pa.DataType) -> pa.Array:
# TODO(ab): get rid of this once we drop support for Python 3.8. Make sure to pin numpy>=1.25.
if NUMPY_VERSION < (1, 25):
# Older numpy doesn't seem to support `data` in the form of [Point3D(1, 2), Point3D(3, 4)]
# this happens for python 3.8 (1.25 supports 3.9+)
from .. import Vec2D

if isinstance(data, Sequence):
data = [np.array(p.xy) if isinstance(p, Vec2D) else p for p in data] # type: ignore[assignment]

points = np.asarray(data, dtype=np.float32).reshape((-1,))
return pa.FixedSizeListArray.from_arrays(points, type=data_type)


def vec3d_native_to_pa_array(data: Vec3DArrayLike, data_type: pa.DataType) -> pa.Array:
# TODO(ab): get rid of this once we drop support for Python 3.8. Make sure to pin numpy>=1.25.
if NUMPY_VERSION < (1, 25):
# Older numpy doesn't seem to support `data` in the form of [Point3D(1, 2), Point3D(3, 4)]
# this happens for python 3.8 (1.25 supports 3.9+)
from .. import Vec3D

if isinstance(data, Sequence):
data = [np.array(p.xyz) if isinstance(p, Vec3D) else p for p in data] # type: ignore[assignment]

points = np.asarray(data, dtype=np.float32).reshape((-1,))
return pa.FixedSizeListArray.from_arrays(points, type=data_type)


def vec4d_native_to_pa_array(data: Vec4DArrayLike, data_type: pa.DataType) -> pa.Array:
# TODO(ab): get rid of this once we drop support for Python 3.8. Make sure to pin numpy>=1.25.
if NUMPY_VERSION < (1, 25):
# Older numpy doesn't seem to support `data` in the form of [Point3D(1, 2), Point3D(3, 4)]
# this happens for python 3.8 (1.25 supports 3.9+)
from .. import Vec4D

if isinstance(data, Sequence):
data = [np.array(p.xyzw) if isinstance(p, Vec4D) else p for p in data] # type: ignore[assignment]

points = np.asarray(data, dtype=np.float32).reshape((-1,))
return pa.FixedSizeListArray.from_arrays(points, type=data_type)