File size: 4,470 Bytes
9ff4bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import pyarrow as pa

    from .chunk._chunk import DataframeLike
    from .recording_stream import RecordingStream


class _AutoIndex:
    """Sentinel for the `index=…` argument: derive index columns from metadata."""


AUTO_INDEX = _AutoIndex()
"""Sentinel for the `index=…` argument: derive index columns from metadata."""

# The following constants mirror the Rerun Arrow metadata keys (see `re_sorbet::metadata`). They are
# kept here for backwards compatibility; the dataframe → chunk interpretation now lives in Rust.
SORBET_INDEX_NAME = b"rerun:index_name"
SORBET_ENTITY_PATH = b"rerun:entity_path"
SORBET_ARCHETYPE_NAME = b"rerun:archetype"
SORBET_COMPONENT = b"rerun:component"
SORBET_COMPONENT_TYPE = b"rerun:component_type"
SORBET_IS_TABLE_INDEX = b"rerun:is_table_index"
RERUN_KIND = b"rerun:kind"
RERUN_KIND_CONTROL = b"control"
RERUN_KIND_INDEX = b"index"

# Root entity path used for recording-scope properties (e.g. `start_time`).
# Mirrors `re_log_types::EntityPath::properties()` on the Rust side.
RECORDING_PROPERTIES_PATH = "/__properties"


def send_record_batch(
    batch: pa.RecordBatch,
    recording: RecordingStream | None = None,
    *,
    index: str | list[str] | _AutoIndex | None = AUTO_INDEX,
    entity_path: str | None = None,
) -> None:
    """
    Coerce a single pyarrow `RecordBatch` to Rerun structure and log it.

    A thin wrapper over [`Chunk.from_record_batch`][rerun.chunk.Chunk.from_record_batch]
    followed by [`send_chunks`][rerun.send_chunks]. See `Chunk.from_record_batch` for
    the full column-classification semantics and the conditions under which a `ValueError` is raised.

    Parameters
    ----------
    batch:
        The Arrow record batch to interpret.
    recording:
        Specifies the [`rerun.RecordingStream`][] to use.
        If left unspecified, defaults to the current active data recording, if there is one.
        See also: [`rerun.init`][], [`rerun.set_global_data_recording`][].
    index:
        Determines which columns are index (timeline) columns. See
        [`Chunk.from_record_batch`][rerun.chunk.Chunk.from_record_batch] for the full
        semantics. Defaults to deriving the index from the batch's Rerun metadata.
    entity_path:
        Default entity path for component columns that do not otherwise specify one.

    """
    from ._send_chunks import send_chunks
    from .chunk._chunk import Chunk

    chunks = Chunk.from_record_batch(batch, index=index, entity_path=entity_path)
    send_chunks(chunks, recording=recording)  # NOLINT: send_chunks casts the RecordingStream itself


def send_dataframe(
    df: DataframeLike,
    recording: RecordingStream | None = None,
    *,
    index: str | list[str] | _AutoIndex | None = AUTO_INDEX,
    entity_path: str | None = None,
) -> None:
    """
    Coerce a pyarrow `Table` / `RecordBatch` / `RecordBatchReader`, or a datafusion `DataFrame`, to Rerun structure and log it.

    A thin wrapper over [`Chunk.from_dataframe`][rerun.chunk.Chunk.from_dataframe] followed by
    [`send_chunks`][rerun.send_chunks]. See `Chunk.from_dataframe` for the accepted input
    types, and `Chunk.from_record_batch` for the full column-classification semantics and the
    conditions under which a `ValueError` is raised.

    Parameters
    ----------
    df:
        The dataframe to interpret. Must be a pyarrow `Table`, pyarrow `RecordBatch`, pyarrow
        `RecordBatchReader`, or datafusion `DataFrame` (an optional dependency) — each has a single
        fixed schema.
    recording:
        Specifies the [`rerun.RecordingStream`][] to use.
        If left unspecified, defaults to the current active data recording, if there is one.
        See also: [`rerun.init`][], [`rerun.set_global_data_recording`][].
    index:
        Determines which columns are index (timeline) columns. See
        [`Chunk.from_record_batch`][rerun.chunk.Chunk.from_record_batch] for the full
        semantics. Defaults to deriving the index from the dataframe's Rerun metadata.
    entity_path:
        Default entity path for component columns that do not otherwise specify one.

    """
    from ._send_chunks import send_chunks
    from .chunk._chunk import Chunk

    chunks = Chunk.from_dataframe(df, index=index, entity_path=entity_path)
    send_chunks(chunks, recording=recording)  # NOLINT: send_chunks casts the RecordingStream itself