cras_bag_tools package

Submodules

cras_bag_tools.bag_filter module

Filter a bag file using a MessageFilter.

cras_bag_tools.bag_filter.filter_bag(bag, out, filter=<cras_bag_tools.message_filter.Passthrough object>)

Filter the given bagfile ‘bag’ into bagfile ‘out’ using message filter ‘filter’.

Parameters:
  • bag (rosbag.bag.Bag) – The input bag (open in read mode).
  • out (rosbag.bag.Bag) – The output bag (open in write mode).
  • filter (MessageFilter) – The filter to apply.

cras_bag_tools.fix_msg_defs module

Module that allows to fix message definitions in a bag file according to local definitions.

cras_bag_tools.fix_msg_defs.fix_msg_defs(bag, topics=None)

In some cases, wrong message definitions are stored to bag files and they do not match the MD5 sums. This function goes through an open bag file and adds the missing definitions (according to the definitions found in the currently sourced workspace).

Parameters:
  • bag (rosbag.Bag) – The bag file.
  • topics (List[AnyStr]) – The topics to fix. Leave out to fix all topics.

cras_bag_tools.message_filter module

A message filter that can decide whether a message should be kept or not, and possibly alter it.

class cras_bag_tools.message_filter.DeserializedMessageFilter(*args, **kwargs)

Bases: cras_bag_tools.message_filter.MessageFilter

Message filter that processes deserialized messages.

filter(topic, msg, stamp, header)

Do the filtering.

Parameters:
  • topic (str) – Topic of the message.
  • msg (genpy.Message) – The decoded message.
  • stamp (rospy.Time) – Receive timestamp of the message.
  • header (dict) – Connection header.
Returns:

None if the message should be discarded, or a deserialized message(s).

class cras_bag_tools.message_filter.FilterChain(filters)

Bases: cras_bag_tools.message_filter.RawMessageFilter

A chain of message filters.

Constructor.

Parameters:filters (list of MessageFilter) – The filters to add.
connection_filter(topic, datatype, md5sum, msg_def, header)

Connection filter passed to Bag.read_messages().

Parameters:
  • topic
  • datatype
  • md5sum
  • msg_def
  • header
Returns:

If False, the topic will not be read from the input bag.

Return type:

bool

filter(topic, datatype, data, md5sum, pytype, stamp, header)

Do the filtering.

Parameters:
  • topic (str) – Topic of the message.
  • datatype (str) – ROS datatype of the message (as string).
  • data (bytes) – The raw data.
  • md5sum (str) – MD5 sum of the datatype.
  • pytype (type) – ROS datatype of the message (as Python type).
  • stamp (rospy.Time) – Receive timestamp of the message.
  • header (dict) – Connection header.
Returns:

None if the message should be discarded, or raw message(s).

reset()

Reset the filter. This should be called e.g. before starting a new bag.

set_bag(bag)

If this filter is working on a bag, it should be set here before the filter starts being used on the bag.

Parameters:bag (rosbag.bag.Bag) – The bag file open for reading.
topic_filter(topic)

Filter of topics to be read from the bag file.

Parameters:topic
Returns:If False, the topic will not be read from the input bag.
Return type:bool
class cras_bag_tools.message_filter.MessageFilter(is_raw, include_topics=None, exclude_topics=None, include_types=None, exclude_types=None, min_stamp=None, max_stamp=None)

Bases: object

Base class for message filters. Do not implement this directly: instead implement either RawMessageFilter or DeserializeMessageFilter.

The workflow of the filters is as follows: 1. Get the topic filter and connection filter from the message filter and apply these to a bag reader (or other

message stream provider, as this library is not bound to only processing bag messages).
  1. Read messages that satisfy the topic and connections filters.
  2. If the message does not satisfy consider_message(), it is passed further without a change. This is a kind of pre-filter that allows us to not deserialize messages just to tell to throw them away or pass them along.
  3. filter_message() is called. If it returns None, the message should be discarded. Otherwise, it either returns one message or a list of messages. The first (or only) message is considered to be the “direct followup” of the input message and continues going through the filter (or stops the filter if it is None). The other messages in the returned list should be fed into this filter again as new input messages.

Constructor.

Parameters:
  • is_raw (bool) – Whether the filter works on raw or deserialized messages.
  • include_topics (list) – If nonempty, the filter will only work on these topics.
  • exclude_topics (list) – If nonempty, the filter will skip these topics (but pass them further).
  • include_types (list) – If nonempty, the filter will only work on these message types.
  • exclude_types (list) – If nonempty, the filter will skip these message types (but pass them further).
  • min_stamp (rospy.Time) – If set, the filter will only work on messages after this timestamp.
  • max_stamp (rospy.Time) – If set, the filter will only work on messages before this timestamp.
static add_cli_args(parser)

Subclasses may reimplement this static method to specify extra CLI args they provide.

Parameters:parser (argparse.ArgumentParser) – The argument parser to configure.
connection_filter(topic, datatype, md5sum, msg_def, header)

Connection filter passed to Bag.read_messages().

Parameters:
  • topic
  • datatype
  • md5sum
  • msg_def
  • header
Returns:

If False, the topic will not be read from the input bag.

Return type:

bool

consider_message(topic, datatype, stamp, header)

This function should be called before calling filter(). If it returns False, filter() should not be called and the original message should be used instead.

Parameters:
  • topic (str) –
  • datatype (str) –
  • stamp (Time) –
  • header (dict) –
Returns:

Whether filter() should be called.

Return type:

bool

filter(*args, **kwargs)

Filter the message.

Parameters:
  • args – The message can be either a RawMessageData or a DeserializedMessageData tuple.
  • kwargs
Returns:

None if the message should be discarded. The possibly changed message otherwise. Multiple messages can be returned, too. In that case, the additional messages should be passed through the filter as if they are newly read messages.

static from_config(cfg)

Create a MessageFilter from a config dict.

Other filters can be defined by 3rd-party packages via pluginlib. The package has to <exec_depend>cras_bag_tools</exec_depend> and it has to put this line in its <export> tag in package.xml: <cras_bag_tools filters="$PACKAGE.$MODULE" />. With this in place, filter_bag will search the specified module for all classes that subclass cras_bag_tools.MessageFilter and it will provide these as additional filters.

Parameters:cfg (dict or list or tuple) – The filter configuration. If a sequence is given, a FilterChain will be created.
Returns:The configured filter.
Return type:MessageFilter
is_raw = None

Whether the filter works on raw or deserialized messages.

static process_cli_args(filters, args)

Subclasses may reimplement this static method to process the custom CLI args from add_cli_args().

Parameters:
  • filters (list) – The list of loaded filters. This method can add to the list.
  • args (argparse.Namespace) – The parsed args.
reset()

Reset the filter. This should be called e.g. before starting a new bag.

set_bag(bag)

If this filter is working on a bag, it should be set here before the filter starts being used on the bag.

Parameters:bag (rosbag.bag.Bag) – The bag file open for reading.
topic_filter(topic)

Filter of topics to be read from the bag file.

Parameters:topic
Returns:If False, the topic will not be read from the input bag.
Return type:bool
static yaml_config_args()

Subclasses may reimplement this static method to specify extra YAML keys they provide.

These keys should correspond to the names of the CLI args and they will be read into CLI args when found in the YAML file.

Returns:The list of provided YAML keys.
Return type:list
class cras_bag_tools.message_filter.Passthrough(*args, **kwargs)

Bases: cras_bag_tools.message_filter.RawMessageFilter

Just pass all messages through.

filter(topic, datatype, data, md5sum, pytype, stamp, header)

Do the filtering.

Parameters:
  • topic (str) – Topic of the message.
  • datatype (str) – ROS datatype of the message (as string).
  • data (bytes) – The raw data.
  • md5sum (str) – MD5 sum of the datatype.
  • pytype (type) – ROS datatype of the message (as Python type).
  • stamp (rospy.Time) – Receive timestamp of the message.
  • header (dict) – Connection header.
Returns:

None if the message should be discarded, or raw message(s).

class cras_bag_tools.message_filter.RawMessageFilter(*args, **kwargs)

Bases: cras_bag_tools.message_filter.MessageFilter

Message filter that processes raw messages.

filter(topic, datatype, data, md5sum, pytype, stamp, header)

Do the filtering.

Parameters:
  • topic (str) – Topic of the message.
  • datatype (str) – ROS datatype of the message (as string).
  • data (bytes) – The raw data.
  • md5sum (str) – MD5 sum of the datatype.
  • pytype (type) – ROS datatype of the message (as Python type).
  • stamp (rospy.Time) – Receive timestamp of the message.
  • header (dict) – Connection header.
Returns:

None if the message should be discarded, or raw message(s).

cras_bag_tools.message_filter.filter_message(topic, msg, stamp, connection_header, filter, raw_output=True)

Apply the given filter to a message.

Parameters:
  • topic (str) – The message topic.
  • msg (genpy.Message or tuple) – The message (either a deserialized message or a raw message as 4-tuple).
  • stamp (rospy.Time) – Receive timestamp of the message.
  • connection_header (dict) – Connection header.
  • filter (MessageFilter) – The filter to apply.
  • raw_output (bool) – Whether to output a raw message or a deserialized one.
Returns:

None if the message should be discarded, or a message.

Return type:

tuple or None

cras_bag_tools.message_filter.fix_connection_header(header, datatype, md5sum, pytype)
cras_bag_tools.message_filter.get_filters()

Get all defined message filters.

Returns:The list of message filters.
Return type:list of MessageFilter
cras_bag_tools.message_filter.is_sequence(o)

cras_bag_tools.message_filters module

cras_bag_tools.topic_set module

Efficient data structure to hold a static set of topics with super-fast is-in-set queries.

The querying is done using expressions like topic in set or topic not in set.

class cras_bag_tools.topic_set.TopicSet(items=None)

Bases: object

Efficient data structure to hold a static set of topics with super-fast is-in-set queries.

Build the data structure. Adding items later is not supported.

Parameters:items (Iterable[AnyStr]|None) – The topics to search. Leading slash will be removed.

cras_bag_tools.tqdm_bag module

Bag file reader that shows progressbars when loading index or reading messages.

class cras_bag_tools.tqdm_bag.TqdmBag(f, mode='r', compression='none', chunk_threshold=786432, allow_unindexed=False, options=None, skip_index=False)

Bases: rosbag.bag.Bag

Drop-in replacement for rosbag.bag.Bag which shows nice progressbars when loading index and reading messages.

Open a bag file. The mode can be ‘r’, ‘w’, or ‘a’ for reading (default), writing or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. Simultaneous reading and writing is allowed when in writing or appending mode. @param f: filename of bag to open or a stream to read from @type f: str or file @param mode: mode, either ‘r’, ‘w’, or ‘a’ @type mode: str @param compression: compression mode, see U{rosbag.Compression} for valid modes @type compression: str @param chunk_threshold: minimum number of uncompressed bytes per chunk @type chunk_threshold: int @param allow_unindexed: if True, allow opening unindexed bags @type allow_unindexed: bool @param options: the bag options (currently: compression and chunk_threshold) @type options: dict @param skip_index: if True, don’t read the connection index records on open [2.0+] @type skip_index: bool @raise ValueError: if any argument is invalid @raise ROSBagException: if an error occurs opening file @raise ROSBagFormatException: if bag format is corrupted

read_index()

Force reading index if it was skipped in the constructor.

read_messages(topics=None, start_time=None, end_time=None, connection_filter=None, raw=False, return_connection_header=False)

Read messages from the bag, optionally filtered by topic, timestamp and connection details. @param topics: list of topics or a single topic. if an empty list is given all topics will be read [optional] @type topics: list(str) or str @param start_time: earliest timestamp of message to return [optional] @type start_time: U{genpy.Time} @param end_time: latest timestamp of message to return [optional] @type end_time: U{genpy.Time} @param connection_filter: function to filter connections to include [optional] @type connection_filter: function taking (topic, datatype, md5sum, msg_def, header) and returning bool @param raw: if True, then generate tuples of (datatype, (data, md5sum, position), pytype) @type raw: bool @return: generator of BagMessage(topic, message, timestamp) namedtuples for each message in the bag file @rtype: generator of tuples of (str, U{genpy.Message}, U{genpy.Time}) [not raw] or (str, (str, str, str, tuple, class), U{genpy.Time}) [raw]

Module contents