Stream from Python

In this section we'll log and visualize our first non-trivial dataset, putting many of Rerun's core concepts and features to use.

In a few lines of code, we'll go from a blank sheet to something you don't see every day: an animated, interactive, DNA-shaped abacus:

This guide aims to go wide instead of deep. There are links to other doc pages where you can learn more about specific topics.

At any time, you can checkout the complete code listing for this tutorial here to better keep track of the overall picture.

Prerequisites prerequisites

We assume you have working Python and rerun-sdk installations. If not, check out the setup page.

Initializing the SDK initializing-the-sdk

Start by opening your editor of choice and creating a new file called dna_example.py.

The first thing we need to do is to import rerun and initialize the SDK by calling rr.init. This init call is required prior to using any of the global logging calls, and allows us to name our recording using an ApplicationId.

We also import some other utilities we will use later in the example.

import rerun as rr

from math import tau
import numpy as np
from rerun.utilities import build_color_spiral
from rerun.utilities import bounce_lerp

rr.init("rerun_example_dna_abacus")

Among other things, a stable ApplicationId will make it so the Rerun Viewer retains its UI state across runs for this specific dataset, which will make our lives much easier as we iterate.

Check out the reference to learn more about how Rerun deals with applications and recordings.

Starting the Viewer starting-the-viewer

Next up, we want to spawn the Rerun Viewer itself.

To do this, you can add the line:

rr.spawn()

Now you can run your application just as you would any other Python script:

(venv) $ python dna_example.py

And with that, we're ready to start sending out data:

By default, the SDK will start a Viewer in another process and automatically pipe the data through. There are other means of sending data to a Viewer as we'll see at the end of this section, but for now this default will work great as we experiment.

Logging our first points logging-our-first-points

The core structure of our DNA looking shape can easily be described using two point clouds shaped like spirals. Add the following to your file:

NUM_POINTS = 100

# Points and colors are both np.array((NUM_POINTS, 3))
points1, colors1 = build_color_spiral(NUM_POINTS)
points2, colors2 = build_color_spiral(NUM_POINTS, angular_offset=tau*0.5)

rr.log("dna/structure/left", rr.Points3D(points1, colors=colors1, radii=0.08))
rr.log("dna/structure/right", rr.Points3D(points2, colors=colors2, radii=0.08))

Run your script once again and you should now see this scene in the viewer. Note that if the Viewer was still running, Rerun will simply connect to this existing session and replace the data with this new recording.

This is a good time to make yourself familiar with the viewer: try interacting with the scene and exploring the different menus. Checkout the Viewer Walkthrough and viewer reference for a complete tour of the viewer's capabilities.

Under the hood under-the-hood

This tiny snippet of code actually holds much more than meets the eye…

Archetypes archetypes

The easiest way to log geometric primitives is the use the rr.log function with one of the built-in archetype class, such as rr.Points3D. Archetypes take care of building batches of components that are recognized and correctly displayed by the Rerun viewer.

Components components

Under the hood, the Rerun Python SDK logs individual components like positions, colors, and radii. Archetypes are just one high-level, convenient way of building such collections of components. For advanced use cases, it's possible to add custom components to archetypes, or even log entirely custom sets of components, bypassing archetypes altogether.

For more information on how the Rerun data model works, refer to our section on Entities and Components.

Our Python SDK integrates with the rest of the Python ecosystem: the points and colors returned by build_color_spiral in this example are vanilla numpy arrays. Rerun takes care of mapping those arrays to actual Rerun components depending on the context (e.g. we're calling rr.Points3D in this case).

Entities & hierarchies entities--hierarchies

Note the two strings we're passing in: "dna/structure/left" & "dna/structure/right".

These are entity paths, which uniquely identify each entity in our scene. Every entity is made up of a path and one or more components. Entity paths typically form a hierarchy which plays an important role in how data is visualized and transformed (as we shall soon see).

Batches batches

One final observation: notice how we're logging a whole batch of points and colors all at once here. Batches of data are first-class citizens in Rerun and come with all sorts of performance benefits and dedicated features. You're looking at one of these dedicated features right now in fact: notice how we're only logging a single radius for all these points, yet somehow it applies to all of them. We call this clamping.

A lot is happening in these two simple function calls. Good news is: once you've digested all of the above, logging any other entity will simply be more of the same. In fact, let's go ahead and log everything else in the scene now.

Adding the missing pieces adding-the-missing-pieces

We can represent the scaffolding using a batch of 3D line strips:

rr.log(
    "dna/structure/scaffolding",
    rr.LineStrips3D(np.stack((points1, points2), axis=1), colors=[128, 128, 128])
)

Which only leaves the beads:

offsets = np.random.rand(NUM_POINTS)
beads = [bounce_lerp(points1[n], points2[n], offsets[n]) for n in range(NUM_POINTS)]
colors = [[int(bounce_lerp(80, 230, offsets[n] * 2))] for n in range(NUM_POINTS)]
rr.log(
    "dna/structure/scaffolding/beads",
    rr.Points3D(beads, radii=0.06, colors=np.repeat(colors, 3, axis=-1)),
)

Once again, although we are getting fancier and fancier with our numpy incantations, there is nothing new here: it's all about building out numpy arrays and feeding them to the Rerun API.

Animating the beads animating-the-beads

Introducing time introducing-time

Up until this point, we've completely set aside one of the core concepts of Rerun: Time and Timelines.

Even so, if you look at your Timeline View right now, you'll notice that Rerun has kept track of time on your behalf anyway by memorizing when each log call occurred.

screenshot of the beads with the timeline

Unfortunately, the logging time isn't particularly helpful to us in this case: we can't have our beads animate depending on the logging time, else they would move at different speeds depending on the performance of the logging process! For that, we need to introduce our own custom timeline that uses a deterministic clock which we control.

Rerun has rich support for time: whether you want concurrent or disjoint timelines, out-of-order insertions or even data that lives outside the timeline(s). You will find a lot of flexibility in there.

Let's add our custom timeline:

time_offsets = np.random.rand(NUM_POINTS)

for i in range(400):
    time = i * 0.01
    rr.set_time_seconds("stable_time", time)

    times = np.repeat(time, NUM_POINTS) + time_offsets
    beads = [bounce_lerp(points1[n], points2[n], times[n]) for n in range(NUM_POINTS)]
    colors = [[int(bounce_lerp(80, 230, times[n] * 2))] for n in range(NUM_POINTS)]
    rr.log(
        "dna/structure/scaffolding/beads",
        rr.Points3D(beads, radii=0.06, colors=np.repeat(colors, 3, axis=-1)),
    )

A call to set_time_seconds will create our new Timeline and make sure that any logging calls that follow gets assigned that time.

⚠️ If you run this code as is, the result will be… surprising: the beads are animating as expected, but everything we've logged until that point is gone! ⚠️

screenshot of the surprising situation

Enter…

Latest-at semantics latestat-semantics

That's because the Rerun Viewer has switched to displaying your custom timeline by default, but the original data was only logged to the default timeline (called log_time). To fix this, go back to the top of the file and add:

rr.spawn()
rr.set_time_seconds("stable_time", 0)
screenshot after using latest-at

This fix actually introduces yet another very important concept in Rerun: "latest-at" semantics. Notice how entities "dna/structure/left" & "dna/structure/right" have only ever been logged at time zero, and yet they are still visible when querying times far beyond that point.

Rerun always reasons in terms of "latest" data: for a given entity, it retrieves all of its most recent components at a given time.

Transforming space transforming-space

There's only one thing left: our original scene had the abacus rotate along its principal axis.

As was the case with time, (hierarchical) space transformations are first class-citizens in Rerun. Now it's just a matter of combining the two: we need to log the transform of the scaffolding at each timestamp.

Either expand the previous loop to include logging transforms or simply add a second loop like this:

for i in range(400):
    time = i * 0.01
    rr.set_time_seconds("stable_time", time)
    rr.log(
        "dna/structure",
        rr.Transform3D(rotation=rr.RotationAxisAngle(axis=[0, 0, 1], radians=time / 4.0 * tau)),
    )

Voila!

Other ways of logging & visualizing data other-ways-of-logging--visualizing-data

rr.spawn is great when you're experimenting on a single machine like we did in this tutorial, but what if the logging happens on, for example, a headless computer?

Rerun offers several solutions for such use cases.

Logging data over the network logging-data-over-the-network

At any time, you can start a Rerun Viewer by running rerun. This Viewer is in fact a server that's ready to accept data over TCP (it's listening on 0.0.0.0:9876 by default).

On the logger side, simply use rr.connect instead of rr.spawn to start sending the data over to any TCP address.

Checkout rerun --help for more options.

Saving & loading to/from RRD files saving--loading-tofrom-rrd-files

Sometimes, sending the data over the network is not an option. Maybe you'd like to share the data, attach it to a bug report, etc.

Rerun has you covered:

  • Use rr.save to stream all logged data to disk.
  • View it with rerun path/to/recording.rrd

You can also save a recording (or a portion of it) as you're visualizing it, directly from the viewer.

⚠️ RRD files are not yet stable across different versions! ⚠️

Closing closing

This closes our whirlwind tour of Rerun. We've barely scratched the surface of what's possible, but this should have hopefully given you plenty pointers to start experimenting.

As a next step, browse through our example gallery for some more realistic example use-cases, or browse the Types section for more simple examples of how to use the main datatypes.