174 lines
5.6 KiB
Python
Raw Normal View History

2023-01-11 15:21:14 +01:00
# Copyright 2020-2023 Ternaris.
2021-05-02 14:53:20 +02:00
# SPDX-License-Identifier: Apache-2.0
2021-08-06 10:08:26 +02:00
"""Tool checking if contents of two rosbags are equal."""
2021-05-02 14:53:20 +02:00
# pylint: disable=import-error
from __future__ import annotations
import array
import math
import sys
2021-08-06 10:08:26 +02:00
from pathlib import Path
2021-05-02 14:53:20 +02:00
from typing import TYPE_CHECKING
from unittest.mock import Mock
import genpy # type: ignore
2022-04-11 00:07:53 +02:00
import numpy
2021-05-02 14:53:20 +02:00
import rosgraph_msgs.msg # type: ignore
from rclpy.serialization import deserialize_message # type: ignore
from rosbag2_py import ConverterOptions, SequentialReader, StorageOptions # type: ignore
from rosidl_runtime_py.utilities import get_message # type: ignore
rosgraph_msgs.msg.Log = Mock()
rosgraph_msgs.msg.TopicStatistics = Mock()
import rosbag.bag # type:ignore # noqa: E402 pylint: disable=wrong-import-position
if TYPE_CHECKING:
2022-04-11 00:07:53 +02:00
from typing import Generator, List, Protocol, Union, runtime_checkable
2021-05-02 14:53:20 +02:00
2022-04-11 00:07:53 +02:00
@runtime_checkable
class NativeMSG(Protocol): # pylint: disable=too-few-public-methods
"""Minimal native ROS message interface used for benchmark."""
def get_fields_and_field_types(self) -> dict[str, str]:
"""Introspect message type."""
raise NotImplementedError
2021-05-02 14:53:20 +02:00
class Reader: # pylint: disable=too-few-public-methods
2021-08-06 10:08:26 +02:00
"""Mimimal shim using rosbag2_py to emulate rosbags API."""
2021-05-02 14:53:20 +02:00
2021-08-06 10:08:26 +02:00
def __init__(self, path: Union[str, Path]):
2021-05-02 14:53:20 +02:00
"""Initialize reader shim."""
self.reader = SequentialReader()
self.reader.open(StorageOptions(path, 'sqlite3'), ConverterOptions('', ''))
self.typemap = {x.name: x.type for x in self.reader.get_all_topics_and_types()}
2021-11-25 14:26:17 +01:00
def messages(self) -> Generator[tuple[str, int, bytes], None, None]:
2021-05-02 14:53:20 +02:00
"""Expose rosbag2 like generator behavior."""
while self.reader.has_next():
topic, data, timestamp = self.reader.read_next()
pytype = get_message(self.typemap[topic])
yield topic, timestamp, deserialize_message(data, pytype)
2022-04-11 00:07:53 +02:00
def fixup_ros1(conns: List[rosbag.bag._Connection_Info]) -> None:
2021-05-02 14:53:20 +02:00
"""Monkeypatch ROS2 fieldnames onto ROS1 objects.
Args:
conns: Rosbag1 connections.
"""
genpy.Time.sec = property(lambda x: x.secs)
genpy.Time.nanosec = property(lambda x: x.nsecs)
genpy.Duration.sec = property(lambda x: x.secs)
genpy.Duration.nanosec = property(lambda x: x.nsecs)
if conn := next((x for x in conns if x.datatype == 'sensor_msgs/CameraInfo'), None):
2022-07-27 16:39:26 +02:00
print('Patching CameraInfo') # noqa: T201
2021-05-02 14:53:20 +02:00
cls = rosbag.bag._get_message_type(conn) # pylint: disable=protected-access
cls.d = property(lambda x: x.D, lambda x, y: setattr(x, 'D', y)) # noqa: B010
cls.k = property(lambda x: x.K, lambda x, y: setattr(x, 'K', y)) # noqa: B010
cls.r = property(lambda x: x.R, lambda x, y: setattr(x, 'R', y)) # noqa: B010
cls.p = property(lambda x: x.P, lambda x, y: setattr(x, 'P', y)) # noqa: B010
2022-04-11 00:07:53 +02:00
def compare(ref: object, msg: object) -> None:
2021-05-02 14:53:20 +02:00
"""Compare message to its reference.
Args:
ref: Reference ROS1 message.
msg: Converted ROS2 message.
"""
2022-04-11 00:07:53 +02:00
if isinstance(msg, NativeMSG):
2021-05-02 14:53:20 +02:00
for name in msg.get_fields_and_field_types():
refval = getattr(ref, name)
msgval = getattr(msg, name)
compare(refval, msgval)
elif isinstance(msg, array.array):
if isinstance(ref, bytes):
assert msg.tobytes() == ref
else:
2022-04-11 00:07:53 +02:00
assert isinstance(msg, numpy.ndarray)
2021-05-02 14:53:20 +02:00
assert (msg == ref).all()
elif isinstance(msg, list):
2022-04-11 00:07:53 +02:00
assert isinstance(ref, (list, numpy.ndarray))
2021-05-02 14:53:20 +02:00
assert len(msg) == len(ref)
for refitem, msgitem in zip(ref, msg):
compare(refitem, msgitem)
elif isinstance(msg, str):
assert msg == ref
2022-04-11 00:07:53 +02:00
elif isinstance(msg, float) and math.isnan(msg):
assert isinstance(ref, float)
assert math.isnan(ref)
2021-05-02 14:53:20 +02:00
else:
assert ref == msg
2021-11-25 14:26:17 +01:00
def main_bag1_bag1(path1: Path, path2: Path) -> None:
2021-08-06 10:08:26 +02:00
"""Compare rosbag1 to rosbag1 message by message.
Args:
path1: Rosbag1 filename.
path2: Rosbag1 filename.
"""
reader1 = rosbag.bag.Bag(path1)
reader2 = rosbag.bag.Bag(path2)
src1 = reader1.read_messages(raw=True, return_connection_header=True)
src2 = reader2.read_messages(raw=True, return_connection_header=True)
for msg1, msg2 in zip(src1, src2):
assert msg1.connection_header == msg2.connection_header
assert msg1.message[:-2] == msg2.message[:-2]
assert msg1.timestamp == msg2.timestamp
assert msg1.topic == msg2.topic
assert next(src1, None) is None
assert next(src2, None) is None
2022-07-27 16:39:26 +02:00
print('Bags are identical.') # noqa: T201
2021-08-06 10:08:26 +02:00
2021-11-25 14:26:17 +01:00
def main_bag1_bag2(path1: Path, path2: Path) -> None:
2021-05-02 14:53:20 +02:00
"""Compare rosbag1 to rosbag2 message by message.
Args:
path1: Rosbag1 filename.
path2: Rosbag2 filename.
"""
reader1 = rosbag.bag.Bag(path1)
src1 = reader1.read_messages()
src2 = Reader(path2).messages()
fixup_ros1(reader1._connections.values()) # pylint: disable=protected-access
for msg1, msg2 in zip(src1, src2):
assert msg1.topic == msg2[0]
assert msg1.timestamp.to_nsec() == msg2[1]
compare(msg1.message, msg2[2])
assert next(src1, None) is None
assert next(src2, None) is None
2022-07-27 16:39:26 +02:00
print('Bags are identical.') # noqa: T201
2021-05-02 14:53:20 +02:00
if __name__ == '__main__':
if len(sys.argv) != 3:
2022-07-27 16:39:26 +02:00
print(f'Usage: {sys.argv} [rosbag1] [rosbag2]') # noqa: T201
2021-05-02 14:53:20 +02:00
sys.exit(1)
2021-08-06 10:08:26 +02:00
arg1 = Path(sys.argv[1])
arg2 = Path(sys.argv[2])
main = main_bag1_bag2 if arg2.is_dir() else main_bag1_bag1
main(arg1, arg2)