Skip to main content

A lightweight Python library to read ROS2 rosbag files.

Project description

ROS2Bag Reader

PyPI - License PyPI - Version PyPI - Python Version PyPI Downloads

A lightweight Python utility for reading ROS 2 (rosbag2) recordings stored with the default sqlite3 backend.

ROS2BagReader provides:

  • Direct access to deserialized ROS messages.
  • Convenient NumPy array extraction from message fields.
  • Flexible timestamp handling (raw, bag-relative, topic-relative).
  • Automatic support for split bag files.
  • Simple bag metadata summary.

The reader operates at the bag folder level (the directory containing metadata.yaml), not individual .db3 files.

Description

ROS 2 recordings created with:

ros2 bag record ...

generate a folder containing:

recording/
├── metadata.yaml
├── recording_0.db3
├── recording_1.db3
└── ...

ROS2BagReader:

  • Parses metadata.yaml
  • Opens all .db3 files automatically
  • Deserializes messages using rosidl_runtime_py
  • Provides NumPy-ready data extraction utilities

It is designed for data analysis, plotting, and post-processing workflows, especially inside Jupyter notebooks.

Installation

Python 3.10 or greater is required.

You can intall the module with pip using.

pip install ros2bag-reader

Or you can copy the module into your project or install it as a local package.

Dependencies

The library requires:

dependencies = [
    "numpy>=1.24.0",
    "rosidl-runtime-py>=0.9.3",
    "rclpy>=3.3.16",
    "PyYAML>=6.0.3",
    "setuptools>=82.0.0",
    "typeguard>=4.5.0",
    "jinja>=3.1.6",
]

Simple Usage Example

Example using a turtlesim recording:

from ros2bag_reader import ROS2BagReader

reader = ROS2BagReader("recording")

reader.summary()

pose = reader.get_messages("/turtle1/pose")

pose_data = reader.get_field_array(
    "/turtle1/pose",
    ["x", "y", "theta"]
)

timestamps = reader.get_time_array(
    "/turtle1/pose",
    reference="topic"
)

reader.close()

For a complete example including plotting and data visualization, see the Jupyter notebook in the examples/ folder.

Class Reference

ROS2BagReader(bag_folder: str)

Initialize the reader from a ROS 2 bag folder.

Parameters

  • bag_folder — Path to the directory containing metadata.yaml

close()

Closes all open SQLite database connections.

Call this when finished reading the bag to release file handles.


get_messages(topic_name: str)

Retrieve all messages from a topic.

Returns

list[(timestamp_ns: int, message: ROS message instance)]

Timestamps are returned in nanoseconds since epoch.


summary()

Print a human-readable summary of the bag:

  • Storage backend
  • Duration
  • Total message count
  • Number of database files
  • Per-topic type and message count

get_time_array(topic_name: str, unit="s", reference="topic")

Return timestamps as a NumPy array.

Parameters

  • unit

    • "s" → seconds (float64)
    • "ns" → nanoseconds (int64)
  • reference

    • "topic" → relative to first message of the topic
    • "bag" → relative to bag start time
    • "raw" → no normalization (epoch time)

Returns

np.ndarray

get_field_array(topic_name: str, fields: list[str], return_timestamps=False)

Extract selected numeric fields into a NumPy array.

Supports:

  • Nested attributes using dot notation
  • Indexed access for array fields

Examples

["x", "y", "theta"]

["pose.position.x", "pose.position.y"]

["ranges[0]", "ranges[10]"]

Returns

If return_timestamps=False:

np.ndarray shape (N, len(fields))

If return_timestamps=True:

timestamps_ns, data_array

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ros2bag_reader-0.0.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ros2bag_reader-0.0.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file ros2bag_reader-0.0.2.tar.gz.

File metadata

  • Download URL: ros2bag_reader-0.0.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ros2bag_reader-0.0.2.tar.gz
Algorithm Hash digest
SHA256 28dd1b005290801814f4058c8e38f07454ccaadfee16296c8616527e214033a1
MD5 2e80234227e7b0a32237f9c915b089f9
BLAKE2b-256 780dc133a94aa2dcb4d4a20d1cd2d587ecbb214659ee3bbf18011c844901561d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ros2bag_reader-0.0.2.tar.gz:

Publisher: python-publish.yml on alejotoro-o/ros2bag-reader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ros2bag_reader-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: ros2bag_reader-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ros2bag_reader-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 80d76561d38b9ddb23f31c3a79381b933b1619a1383101f52dc64b60ed8d058c
MD5 497d822a615f23aa1f88422e6f146cbf
BLAKE2b-256 20d1f0414982b69c0936e00f80340b741b0ee475f881d6bbe6f5dfd207e7cbcf

See more details on using hashes here.

Provenance

The following attestation bundles were made for ros2bag_reader-0.0.2-py3-none-any.whl:

Publisher: python-publish.yml on alejotoro-o/ros2bag-reader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page