# box-robotics/ros2-bagutils architecture

> Architecture diagram of the GitHub repository box-robotics/ros2-bagutils, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/box-robotics/ros2-bagutils
- Repository: https://github.com/box-robotics/ros2-bagutils

## Overview

#### Purpose and Entry Points
`baggie` is a ROS 2 package for reading, writing, and transforming rosbag2 datasets from Python. Its public Python API is exposed through `baggie/baggie/__init__.py`; the central abstraction is `Baggie`, with reader/writer context-manager convenience APIs in `baggie/baggie/_Baggie.py`. ROS 2 command entry scripts are `baggie/scripts/filter`, `baggie/scripts/split`, `baggie/scripts/join`, and `baggie/scripts/timeline`.

#### Native ROS 2 Boundary
The package bridges Python to the C++ `rosbag2_cpp` API via a Pybind11 extension implemented in `baggie/src/py/_baggie.cpp`. The compiled extension is surfaced as `baggie/baggie/_Baggie.py`’s lower-level dependency and built through `baggie/CMakeLists.txt`. This boundary owns direct bag-storage access, ROS message serialization/deserialization, and rosbag2 reader/writer configuration.

#### Core Bag Lifecycle
`Baggie` represents exactly one logical on-disk ROS 2 bag, potentially backed by multiple storage files. It is instantiated in reader or writer mode, opened and closed through context management, and mediates sequential message access or persistence. Higher-level `BagReader`/`BagWriter` behavior is organized in `baggie/baggie/_Baggie.py` and exported from `baggie/baggie/__init__.py`.

#### Message Data Flow
Read flow: rosbag2 storage → native bindings → Python message stream yielding topic, decoded ROS message, and timestamp. Write flow: Python ROS message plus `rclpy.time.Time` or `datetime` timestamp → serialization/native writer → rosbag2 bag storage. Timestamp precision is an important boundary: ROS time supports nanoseconds, while Python `datetime` is microsecond-limited. Shared Python-side normalization/utilities reside in `baggie/baggie/util.py`.

#### Transformation Commands
The CLI command modules compose reader and writer APIs rather than bypassing the library:
- `baggie/baggie/cmd/filter.py`: copies messages while applying time/topic selection, topic remapping, and output compression choices.
- `baggie/baggie/cmd/split.py`: partitions one input bag into time-window output bags, inheriting source storage/converter/compression behavior.
- `baggie/baggie/cmd/join.py`: sequentially copies multiple input bags into one writer/output bag.
These define the primary bulk-data processing path: stream messages from source bags, optionally transform routing/selection, then write target bags.

#### Timeline Export
`baggie/baggie/cmd/timeline.py` is a read-only analysis path. It traverses one or more bags and emits JSON metadata records—timestamp, formatted time, topic, and ROS message type—without requiring consumers to decode payload content. It supports array-of-structs and struct-of-arrays output suitable for shell pipelines and analytics tools.

#### Runtime and Infrastructure
The material runtime is ROS 2: `rosbag2_cpp`/storage plugins, ROS message types and serialization, and Python `rclpy` time interoperability. The ROS package manifest is `baggie/package.xml`; native compilation is CMake-based (`baggie/CMakeLists.txt`). Bag persistence is delegated to rosbag2 storage, commonly SQLite3-backed `.db3` files, with rosbag2-managed metadata and optional Zstandard file compression.

Main entry points: [`baggie/baggie/_Baggie.py`](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/_Baggie.py) (Baggie lifecycle), [`baggie/scripts/filter`](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/scripts/filter) (ROS 2 entry scripts), [`baggie/src/py/_baggie.cpp`](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/src/py/_baggie.cpp) (Pybind11 bindings), [`baggie/baggie/cmd/filter.py`](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/filter.py) (filter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_python["Python library"]
  node_public_api["Public API<br/>Python package<br/>[__init__.py]"]
  node_baggie["Baggie lifecycle<br/>reader/writer API<br/>[_Baggie.py]"]
  node_utilities["Time utilities<br/>normalization<br/>[util.py]"]
end

subgraph group_commands["CLI transforms"]
  node_filter["filter<br/>CLI command<br/>[filter.py]"]
  node_split["split<br/>CLI command<br/>[split.py]"]
  node_join["join<br/>CLI command<br/>[join.py]"]
  node_timeline["timeline<br/>CLI command<br/>[timeline.py]"]
  node_scripts["ROS 2 entry scripts<br/>command launchers"]
end

subgraph group_native["ROS 2 native boundary"]
  node_native_bindings{{"Pybind11 bindings<br/>C++ extension<br/>[_baggie.cpp]"}}
  node_cmake["Native build<br/>CMake<br/>[CMakeLists.txt]"]
  node_rosbag2{{"rosbag2_cpp<br/>ROS 2 API"}}
  node_manifest["Package manifest<br/>ROS package metadata<br/>[package.xml]"]
end

node_ros_runtime["ROS messages and rclpy time<br/>ROS 2 runtime"]
node_bag_storage[("ROS 2 bag storage<br/>persistent dataset")]

node_public_api -->|"exports"| node_baggie
node_baggie -->|"normalizes timestamps"| node_utilities
node_baggie -->|"reads and writes"| node_native_bindings
node_native_bindings -->|"calls"| node_rosbag2
node_native_bindings -->|"serializes messages"| node_ros_runtime
node_rosbag2 -->|"persists bags"| node_bag_storage
node_cmake -->|"builds"| node_native_bindings
node_manifest -.->|"declares ROS package"| node_ros_runtime
node_scripts -->|"launches"| node_filter
node_scripts -->|"launches"| node_split
node_scripts -->|"launches"| node_join
node_scripts -->|"launches"| node_timeline
node_filter -->|"streams source to target"| node_baggie
node_split -->|"streams windows to outputs"| node_baggie
node_join -->|"streams inputs to output"| node_baggie
node_timeline -->|"reads metadata"| node_baggie

click node_public_api "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/__init__.py"
click node_baggie "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/_Baggie.py"
click node_utilities "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/util.py"
click node_filter "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/filter.py"
click node_split "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/split.py"
click node_join "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/join.py"
click node_timeline "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/timeline.py"
click node_native_bindings "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/src/py/_baggie.cpp"
click node_cmake "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/CMakeLists.txt"
click node_scripts "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/scripts/filter"
click node_manifest "https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/package.xml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_baggie,node_utilities toneBlue
class node_filter,node_split,node_join,node_timeline,node_scripts toneAmber
class node_native_bindings,node_cmake,node_rosbag2,node_manifest toneMint
class node_ros_runtime,node_bag_storage toneNeutral
```

## Components

### Python library

- [Public API](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/__init__.py): Python package · Exports Baggie reader and writer APIs. · `baggie/baggie/__init__.py`
- [Baggie lifecycle](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/_Baggie.py): reader/writer API · One logical bag with context-managed open and close. · `baggie/baggie/_Baggie.py`
- [Time utilities](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/util.py): normalization · Bridges ROS nanoseconds and Python datetime precision. · `baggie/baggie/util.py`

### CLI transforms

- [filter](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/filter.py): CLI command · Selects, remaps, and copies messages. · `baggie/baggie/cmd/filter.py`
- [split](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/split.py): CLI command · Partitions an input bag into time-window bags. · `baggie/baggie/cmd/split.py`
- [join](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/join.py): CLI command · Copies multiple input bags into one output bag. · `baggie/baggie/cmd/join.py`
- [timeline](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/baggie/cmd/timeline.py): CLI command · Emits JSON bag metadata without payload decoding. · `baggie/baggie/cmd/timeline.py`
- [ROS 2 entry scripts](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/scripts/filter): command launchers · Launch filter, split, join, and timeline. · `baggie/scripts/filter`

### ROS 2 native boundary

- [Pybind11 bindings](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/src/py/_baggie.cpp): C++ extension · Owns rosbag2 access and message serialization. · `baggie/src/py/_baggie.cpp`
- [Native build](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/CMakeLists.txt): CMake · `baggie/CMakeLists.txt`
- **rosbag2_cpp**: ROS 2 API · Reader, writer, converters, and storage plugins.
- [Package manifest](https://github.com/box-robotics/ros2-bagutils/blob/foxy-devel/baggie/package.xml): ROS package metadata · `baggie/package.xml`

### Other components

- **ROS messages and rclpy time**: ROS 2 runtime
- **ROS 2 bag storage**: persistent dataset · Metadata plus storage files, commonly SQLite .db3 with optional Zstandard compression.

## Connections

- Public API → Baggie lifecycle: exports
- Baggie lifecycle → Time utilities: normalizes timestamps
- Baggie lifecycle → Pybind11 bindings: reads and writes
- Pybind11 bindings → rosbag2_cpp: calls
- Pybind11 bindings → ROS messages and rclpy time: serializes messages
- rosbag2_cpp → ROS 2 bag storage: persists bags
- Native build → Pybind11 bindings: builds
- Package manifest → ROS messages and rclpy time: declares ROS package
- ROS 2 entry scripts → filter: launches
- ROS 2 entry scripts → split: launches
- ROS 2 entry scripts → join: launches
- ROS 2 entry scripts → timeline: launches
- filter → Baggie lifecycle: streams source to target
- split → Baggie lifecycle: streams windows to outputs
- join → Baggie lifecycle: streams inputs to output
- timeline → Baggie lifecycle: reads metadata
