Skip to content

Commit

Permalink
Python Components now implement the ComponentBatchLike interface (rer…
Browse files Browse the repository at this point in the history
…un-io#6543)

### What
New blueprint APIs focus on overriding components rather than requiring
that things be Archetypes.

However, using mono components directly was somewhat inconvenient. This
lets you now pass in a component instance anywhere you would expect to
pass in a ComponentBatchLike.

This component knows it's own batch type and uses it to construct the
correct wrapper for a mono batch type.

This had the side effect that you can also do things now like:
```
rr.log("points", rr.Points3D(positions))
rr.log("points", [rr.components.Color([255, 0, 0])])
```

### 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've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6543?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6543?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6543)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
jleibs authored Jun 12, 2024
1 parent 9242f0f commit b12df49
Show file tree
Hide file tree
Showing 181 changed files with 1,309 additions and 262 deletions.
29 changes: 27 additions & 2 deletions crates/re_types_builder/src/codegen/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ impl PythonCodeGenerator {
Archetype,
BaseExtensionType,
BaseBatch,
ComponentBatchMixin
ComponentBatchMixin,
ComponentMixin,
)
from {rerun_path}_converters import (
int_or_none,
Expand Down Expand Up @@ -612,6 +613,10 @@ fn code_for_struct(
));
}

if *kind == ObjectKind::Component {
superclasses.push("ComponentMixin".to_owned());
}

if let Some(deprecation_notice) = obj.deprecation_notice() {
code.push_unindented(format!(r#"@deprecated("""{deprecation_notice}""")"#), 1);
}
Expand All @@ -634,6 +639,10 @@ fn code_for_struct(

code.push_indented(1, quote_obj_docs(obj), 0);

if *kind == ObjectKind::Component {
code.push_indented(1, "_BATCH_TYPE = None", 1);
}

if ext_class.has_init {
code.push_indented(
1,
Expand Down Expand Up @@ -775,7 +784,23 @@ fn code_for_struct(

match kind {
ObjectKind::Archetype => (),
ObjectKind::Datatype | ObjectKind::Component => {
ObjectKind::Component => {
code.push_indented(
0,
quote_arrow_support_from_obj(reporter, arrow_registry, ext_class, objects, obj),
1,
);

code.push_indented(
0,
format!(
"# This is patched in late to avoid circular dependencies.
{name}._BATCH_TYPE = {name}Batch # type: ignore[assignment]"
),
1,
);
}
ObjectKind::Datatype => {
code.push_indented(
0,
quote_arrow_support_from_obj(reporter, arrow_registry, ext_class, objects, obj),
Expand Down
26 changes: 26 additions & 0 deletions rerun_py/rerun_sdk/rerun/_baseclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,32 @@ def component_name(self) -> str:
return self._ARROW_TYPE._TYPE_NAME # type: ignore[attr-defined, no-any-return]


class ComponentMixin(ComponentBatchLike):
"""
Makes components adhere to the ComponentBatchLike interface.
A single component will always map to a batch of size 1.
The class using the mixin must define the `_BATCH_TYPE` field, which should be a subclass of `BaseBatch`.
"""

def component_name(self) -> str:
"""
The name of the component.
Part of the `ComponentBatchLike` logging interface.
"""
return self._BATCH_TYPE._ARROW_TYPE._TYPE_NAME # type: ignore[attr-defined, no-any-return]

def as_arrow_array(self) -> pa.Array:
"""
The component as an arrow batch.
Part of the `ComponentBatchLike` logging interface.
"""
return self._BATCH_TYPE([self]).as_arrow_array() # type: ignore[attr-defined, no-any-return]


@catch_and_log_exceptions(context="creating empty array")
def _empty_pa_array(type: pa.DataType) -> pa.Array:
if type == pa.null():
Expand Down
4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/annotation_context.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/arrows2d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/arrows3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/asset3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/axes3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/bar_chart.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/boxes2d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/boxes3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/clear.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/depth_image.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/disconnected_space.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/image.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/line_strips2d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/line_strips3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/mesh3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/pinhole.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/points2d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/points3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/scalar.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/segmentation_image.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/series_line.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/series_point.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/tensor.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/text_document.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/text_log.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/transform3d.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/view_coordinates.py

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

4 changes: 3 additions & 1 deletion rerun_py/rerun_sdk/rerun/blueprint/archetypes/background.py

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

Loading

0 comments on commit b12df49

Please sign in to comment.