How box-robotics/ros2-bagutils works
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.py Baggie lifecycle · filter ROS 2 entry scripts · _baggie.cpp Pybind11 bindings · filter.py filter
Components 14
Python library
- Public API · Exports Baggie reader and writer APIs.
baggie/baggie/__init__.py - Baggie lifecycle · One logical bag with context-managed open and close.
baggie/baggie/_Baggie.py - Time utilities · Bridges ROS nanoseconds and Python datetime precision.
baggie/baggie/util.py
CLI transforms
- filter · Selects, remaps, and copies messages.
baggie/baggie/cmd/filter.py - split · Partitions an input bag into time-window bags.
baggie/baggie/cmd/split.py - join · Copies multiple input bags into one output bag.
baggie/baggie/cmd/join.py - timeline · Emits JSON bag metadata without payload decoding.
baggie/baggie/cmd/timeline.py - ROS 2 entry scripts · Launch filter, split, join, and timeline.
baggie/scripts/filter
ROS 2 native boundary
- Pybind11 bindings · Owns rosbag2 access and message serialization.
baggie/src/py/_baggie.cpp - Native build
baggie/CMakeLists.txt - rosbag2_cpp · Reader, writer, converters, and storage plugins.
- Package manifest
baggie/package.xml
Other components
- ROS messages and rclpy time
- ROS 2 bag storage · Metadata plus storage files, commonly SQLite .db3 with optional Zstandard compression.
Connections 16
- 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