|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Show several live plots of random walk data using a scrolling fixed window size.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import time |
| 8 | +from typing import Iterator |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import rerun as rr # pip install rerun-sdk |
| 12 | +import rerun.blueprint as rrb |
| 13 | + |
| 14 | + |
| 15 | +def random_walk_generator() -> Iterator[float]: |
| 16 | + value = 0.0 |
| 17 | + while True: |
| 18 | + value += np.random.normal() |
| 19 | + yield value |
| 20 | + |
| 21 | + |
| 22 | +def main() -> None: |
| 23 | + parser = argparse.ArgumentParser(description="Plot dashboard stress test") |
| 24 | + rr.script_add_args(parser) |
| 25 | + |
| 26 | + parser.add_argument("--num-plots", type=int, default=6, help="How many different plots?") |
| 27 | + parser.add_argument("--num-series-per-plot", type=int, default=5, help="How many series in each single plot?") |
| 28 | + parser.add_argument("--freq", type=float, default=100, help="Frequency of logging (applies to all series)") |
| 29 | + parser.add_argument("--window-size", type=float, default=5.0, help="Size of the window in seconds") |
| 30 | + parser.add_argument("--duration", type=float, default=60, help="How long to log for in seconds") |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + plot_paths = [f"plot_{i}" for i in range(0, args.num_plots)] |
| 35 | + series_paths = [f"series_{i}" for i in range(0, args.num_series_per_plot)] |
| 36 | + |
| 37 | + rr.script_setup(args, "rerun_example_live_scrolling_plot") |
| 38 | + |
| 39 | + # Always send the blueprint since it is a function of the data. |
| 40 | + rr.send_blueprint( |
| 41 | + rrb.Grid( |
| 42 | + contents=[ |
| 43 | + rrb.TimeSeriesView( |
| 44 | + origin=plot_path, |
| 45 | + time_ranges=[ |
| 46 | + rrb.VisibleTimeRange( |
| 47 | + "time", |
| 48 | + start=rrb.TimeRangeBoundary.cursor_relative(seconds=-args.window_size), |
| 49 | + end=rrb.TimeRangeBoundary.cursor_relative(), |
| 50 | + ) |
| 51 | + ], |
| 52 | + plot_legend=rrb.PlotLegend(visible=False), |
| 53 | + ) |
| 54 | + for plot_path in plot_paths |
| 55 | + ] |
| 56 | + ), |
| 57 | + ) |
| 58 | + |
| 59 | + # Generate a list of generators for each series in each plot |
| 60 | + values = [[random_walk_generator() for _ in range(args.num_series_per_plot)] for _ in range(args.num_plots)] |
| 61 | + |
| 62 | + cur_time = time.time() |
| 63 | + end_time = cur_time + args.duration |
| 64 | + time_per_tick = 1.0 / args.freq |
| 65 | + |
| 66 | + while cur_time < end_time: |
| 67 | + # Advance time and sleep if necessary |
| 68 | + cur_time += time_per_tick |
| 69 | + sleep_for = cur_time - time.time() |
| 70 | + if sleep_for > 0: |
| 71 | + time.sleep(sleep_for) |
| 72 | + |
| 73 | + if sleep_for < -0.1: |
| 74 | + print(f"Warning: missed logging window by {-sleep_for:.2f} seconds") |
| 75 | + |
| 76 | + rr.set_time_seconds("time", cur_time) |
| 77 | + |
| 78 | + # Output each series based on its generator |
| 79 | + for plot_idx, plot_path in enumerate(plot_paths): |
| 80 | + for series_idx, series_path in enumerate(series_paths): |
| 81 | + rr.log(f"{plot_path}/{series_path}", rr.Scalar(next(values[plot_idx][series_idx]))) |
| 82 | + |
| 83 | + rr.script_teardown(args) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments