Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: raw_data + easier read_messages interface #50

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/version.bzl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
EMBAG_VERSION = "0.0.41"
EMBAG_VERSION = "0.0.42"
28 changes: 22 additions & 6 deletions python/embag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ PYBIND11_MODULE(libembag, m) {
return std::make_shared<Embag::Bag>(std::make_shared<const std::string>(bytes));
}))
.def("topics", &Embag::Bag::topics)
.def("read_messages", [](std::shared_ptr<Embag::Bag> &bag, const std::vector<std::string>& topics) {
Embag::View view{};
view.addBag(bag);
view.getMessages(topics);
.def(
"read_messages",
[](std::shared_ptr<Embag::Bag> &bag, py::object topics) {
Embag::View view{};
view.addBag(bag);
if (topics.is_none()) {
view.getMessages();
} else if (py::isinstance<py::str>(topics)) {
view.getMessages(topics.cast<std::string>());
} else if (py::isinstance<py::list>(topics)) {
view.getMessages(topics.cast<std::vector<std::string>>());
} else {
throw std::runtime_error("topics must be None, a string, or a list!");
}

return py::make_iterator(IteratorCompat{view.begin()}, IteratorCompat{view.end()});
}, py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */, py::arg("topics"))
return py::make_iterator(IteratorCompat{view.begin()}, IteratorCompat{view.end()});
},
py::keep_alive<0, 1>(), /* Essential: keep object alive while iterator exists */
py::arg("topics") = py::none()
)
.def("getSchema", [](std::shared_ptr<Embag::Bag> &bag, const std::string &topic) {
auto builder = SchemaBuilder{bag};
return builder.generateSchema(topic);
Expand Down Expand Up @@ -77,6 +90,9 @@ PYBIND11_MODULE(libembag, m) {
.def("data", [](std::shared_ptr<Embag::RosMessage> &m) {
return m->data();
})
.def_property_readonly("raw_data", [](std::shared_ptr<Embag::RosMessage> &m) {
return py::bytes(&m->raw_buffer->at(m->raw_buffer_offset), m->raw_data_len);
})
Comment on lines +93 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also want to add raw=True functionality to the read_messages interface in embag.Bag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could - but to do that (and maintain compatibility with rosbag) I would need to change the constructor of RosMessage and add a new field for the raw message definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to leave this for potential future improvement!

.def(
"dict",
[](
Expand Down
2 changes: 2 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ py_test(
srcs = ["embag_test_python.py"],
data = [
"test.bag",
"test_bag_raw_messages.P",
"//python:libembag.so",
],
python_version = "PY3",
Expand All @@ -27,6 +28,7 @@ py_test(
srcs = ["embag_test_python.py"],
data = [
"test.bag",
"test_bag_raw_messages.P",
"//python:libembag.so",
],
python_version = "PY2",
Expand Down
11 changes: 11 additions & 0 deletions test/embag_test_python.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import python.libembag as embag
from collections import OrderedDict
import numpy as np
import pickle
import struct
import sys
import unittest
Expand Down Expand Up @@ -243,5 +244,15 @@ def testROSTimeDicting(self):
assert as_ros_time.to_nsec() == as_int
assert as_ros_time.to_sec() == as_float

def testMessageRawData(self):
"""
Tests that the raw_data property of a RosMessage returns the same data as rospy
"""
with open('test/test_bag_raw_messages.P', 'rb') as raw_messages_file:
raw_messages = pickle.load(raw_messages_file)

for index, msg in enumerate(self.view.getMessages()):
assert raw_messages[index] == msg.raw_data

if __name__ == "__main__":
unittest.main()
Binary file added test/test_bag_raw_messages.P
Binary file not shown.