Skip to content

End-to-end tagging: basic helpers for Python & Rust #8332

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

Merged
merged 5 commits into from
Dec 9, 2024

Conversation

teh-cmc
Copy link
Member

@teh-cmc teh-cmc commented Dec 5, 2024

Introduce some very basic helpers so that overriding tags doesn't require a PhD.

The goal here is not great, it's simply not completely horrible.

Python

Before:

class CustomPosition3DBatch(rr.ComponentBatchLike):
    def __init__(self: Any, positions: npt.ArrayLike) -> None:
        self.position = rr.components.Position3DBatch(positions)

    def component_descriptor(self) -> rr.ComponentDescriptor:
        return rr.ComponentDescriptor(
            "user.CustomPosition3D",
            archetype_name="user.CustomPoints3D",
            archetype_field_name="custom_positions",
        )

    def as_arrow_array(self) -> pa.Array:
        return self.position.as_arrow_array()


class CustomPoints3D(rr.AsComponents):
    def __init__(self: Any, positions: npt.ArrayLike, colors: npt.ArrayLike) -> None:
        self.positions = CustomPosition3DBatch(positions)
        self.colors = rr.components.ColorBatch(colors)

    def as_component_batches(self) -> Iterable[rr.ComponentBatchLike]:
        return (
            [rr.IndicatorComponentBatch("user.CustomPoints3D")]
            + [
                rr.DescribedComponentBatch(
                    self.positions,
                    self.positions.component_descriptor().or_with_overrides(
                        archetype_name="user.CustomPoints3D", archetype_field_name="custom_positions"
                    ),
                )
            ]
            + [
                rr.DescribedComponentBatch(
                    self.colors,
                    self.colors.component_descriptor().or_with_overrides(
                        archetype_name="user.CustomPoints3D", archetype_field_name="colors"
                    ),
                )
            ]
        )

After:

class CustomPoints3D(rr.AsComponents):
    def __init__(self: Any, positions: npt.ArrayLike, colors: npt.ArrayLike) -> None:
        self.positions = rr.components.Position3DBatch(positions).with_descriptor(
            rr.ComponentDescriptor(
                "user.CustomPosition3D",
                archetype_name="user.CustomPoints3D",
                archetype_field_name="custom_positions",
            )
        )
        self.colors = rr.components.ColorBatch(colors).or_with_descriptor_overrides(
            archetype_name="user.CustomPoints3D",
            archetype_field_name="colors",
        )

    def as_component_batches(self) -> Iterable[rr.ComponentBatchLike]:
        return [rr.IndicatorComponentBatch("user.CustomPoints3D"), self.positions, self.colors]

Rust

Before:

#[derive(Debug, Clone, Copy)]
struct CustomPosition3D(rerun::components::Position3D);

impl rerun::SizeBytes for CustomPosition3D {
    #[inline]
    fn heap_size_bytes(&self) -> u64 {
        0
    }
}

impl rerun::Loggable for CustomPosition3D {
    #[inline]
    fn arrow2_datatype() -> arrow2::datatypes::DataType {
        rerun::components::Position3D::arrow2_datatype()
    }

    #[inline]
    fn to_arrow2_opt<'a>(
        data: impl IntoIterator<Item = Option<impl Into<std::borrow::Cow<'a, Self>>>>,
    ) -> rerun::SerializationResult<Box<dyn arrow2::array::Array>>
    where
        Self: 'a,
    {
        rerun::components::Position3D::to_arrow2_opt(
            data.into_iter().map(|opt| opt.map(Into::into).map(|c| c.0)),
        )
    }
}

impl rerun::Component for CustomPosition3D {
    #[inline]
    fn descriptor() -> ComponentDescriptor {
        ComponentDescriptor::new("user.CustomPosition3D")
    }
}

struct CustomPoints3D {
    positions: Vec<CustomPosition3D>,
    colors: Option<Vec<rerun::components::Color>>,
}

impl CustomPoints3D {
    fn indicator() -> rerun::NamedIndicatorComponent {
        rerun::NamedIndicatorComponent("user.CustomPoints3DIndicator".into())
    }

    fn overridden_position_descriptor() -> ComponentDescriptor {
        CustomPosition3D::descriptor()
            .or_with_archetype_name(|| "user.CustomPoints3D".into())
            .or_with_archetype_field_name(|| "custom_positions".into())
    }

    fn overridden_color_descriptor() -> ComponentDescriptor {
        rerun::components::Color::descriptor()
            .or_with_archetype_name(|| "user.CustomPoints3D".into())
            .or_with_archetype_field_name(|| "colors".into())
    }
}

impl rerun::AsComponents for CustomPoints3D {
    fn as_component_batches(&self) -> Vec<rerun::MaybeOwnedComponentBatch<'_>> {
        [
            Some(Self::indicator().to_batch()),
            Some(
                rerun::MaybeOwnedComponentBatch::new(&self.positions as &dyn rerun::ComponentBatch)
                    .with_descriptor_override(Self::overridden_position_descriptor()),
            ),
            self.colors.as_ref().map(|colors| {
                rerun::MaybeOwnedComponentBatch::new(colors as &dyn rerun::ComponentBatch)
                    .with_descriptor_override(Self::overridden_color_descriptor())
            }),
        ]
        .into_iter()
        .flatten()
        .collect()
    }
}

After:

struct CustomPoints3D {
    positions: Vec<rerun::components::Position3D>,
    colors: Option<Vec<rerun::components::Color>>,
}

impl CustomPoints3D {
    fn indicator() -> rerun::NamedIndicatorComponent {
        rerun::NamedIndicatorComponent("user.CustomPoints3DIndicator".into())
    }

    fn overridden_position_descriptor() -> ComponentDescriptor {
        ComponentDescriptor {
            archetype_name: Some("user.CustomPoints3D".into()),
            archetype_field_name: Some("custom_positions".into()),
            component_name: "user.CustomPosition3D".into(),
        }
    }

    fn overridden_color_descriptor() -> ComponentDescriptor {
        <rerun::components::Color as rerun::Component>::descriptor()
            .or_with_archetype_name(|| "user.CustomPoints3D".into())
            .or_with_archetype_field_name(|| "colors".into())
    }
}

impl rerun::AsComponents for CustomPoints3D {
    fn as_component_batches(&self) -> Vec<rerun::MaybeOwnedComponentBatch<'_>> {
        [
            Some(Self::indicator().to_batch()),
            Some(
                self.positions
                    .with_descriptor(Self::overridden_position_descriptor()),
            ),
            self.colors
                .as_ref()
                .map(|colors| colors.with_descriptor(Self::overridden_color_descriptor())),
        ]
        .into_iter()
        .flatten()
        .collect()
    }
}

C++

Too complicated, @Wumpf and I made the wise decision of giving up 🙃

@teh-cmc teh-cmc added do-not-merge Do not merge this PR enhancement New feature or request 🐍 Python API Python logging API exclude from changelog PRs with this won't show up in CHANGELOG.md 🪵 Log & send APIs Affects the user-facing API for all languages and removed 🐍 Python API Python logging API labels Dec 5, 2024
Copy link

github-actions bot commented Dec 5, 2024

Web viewer built successfully. If applicable, you should also test it:

  • I have tested the web viewer
Result Commit Link
e78665d https://rerun.io/viewer/pr/8332

Note: This comment is updated whenever you push a commit.

@teh-cmc teh-cmc marked this pull request as ready for review December 5, 2024 17:07
@teh-cmc
Copy link
Member Author

teh-cmc commented Dec 5, 2024

@rerun-bot full-check

Copy link

github-actions bot commented Dec 5, 2024

@teh-cmc teh-cmc changed the title End-to-end tagging: basic helpers for all languages End-to-end tagging: basic helpers for Python & Rust Dec 5, 2024
@teh-cmc teh-cmc added 🐍 Python API Python logging API 🦀 Rust API Rust logging API labels Dec 5, 2024
@Wumpf Wumpf self-requested a review December 6, 2024 10:45
Copy link
Member

@Wumpf Wumpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@teh-cmc teh-cmc force-pushed the cmc/investigating_component_descriptors_chunk_diff branch from 56ef384 to 77b1f3f Compare December 9, 2024 09:33
Base automatically changed from cmc/investigating_component_descriptors_chunk_diff to main December 9, 2024 09:54
@teh-cmc teh-cmc removed the do-not-merge Do not merge this PR label Dec 9, 2024
@teh-cmc teh-cmc force-pushed the cmc/tagging_helpers branch from 5fd5244 to ed1e775 Compare December 9, 2024 10:00
@teh-cmc teh-cmc merged commit 27084ad into main Dec 9, 2024
32 checks passed
@teh-cmc teh-cmc deleted the cmc/tagging_helpers branch December 9, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request exclude from changelog PRs with this won't show up in CHANGELOG.md 🪵 Log & send APIs Affects the user-facing API for all languages 🐍 Python API Python logging API 🦀 Rust API Rust logging API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants