Package rosbag :: Module bag :: Class Bag
[frames] | no frames]

Class Bag

source code

object --+
         |
        Bag

Bag serialize messages to and from a single file on disk using the bag format.

Instance Methods
 
__init__(self, f, mode='r', compression='none', chunk_threshold=786432, allow_unindexed=False, options=None, skip_index=False)
Open a bag file.
source code
 
__iter__(self) source code
 
__enter__(self) source code
 
__exit__(self, exc_type, exc_value, traceback) source code
generator of tuples of (str, genpy.Message, genpy.Time) [not raw] or (str, (str, str, str, tuple, class), genpy.Time) [raw]
read_messages(self, topics=None, start_time=None, end_time=None, connection_filter=None, raw=False)
Read messages from the bag, optionally filtered by topic, timestamp and connection details.
source code
 
flush(self)
Write the open chunk to disk so subsequent reads will read all messages.
source code
 
write(self, topic, msg, t=None, raw=False)
Write a message to the bag.
source code
 
reindex(self)
Reindexes the bag file.
source code
 
close(self)
Close the bag file.
source code
generator of CompressionTuple of (str, int, int)
get_compression_info(self)
Returns information about the compression of the bag
source code
int
get_message_count(self, topic_filters=None)
Returns the number of messages in the bag.
source code
float, timestamp in seconds, includes fractions of a second
get_start_time(self)
Returns the start time of the bag.
source code
float, timestamp in seconds, includes fractions of a second
get_end_time(self)
Returns the end time of the bag.
source code
 
get_type_and_topic_info(self, topic_filters=None)
Coallates info about the type and topics in the bag.
source code
 
__str__(self)
str(x)
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Properties
  options
Get the options.
  filename
Get the filename.
  version
Get the version.
  mode
Get the mode.
  size
Get the size in bytes.
  compression
Get the compression method to use for writing.
  chunk_threshold
Get the chunk threshold to use for writing.

Inherited from object: __class__

Method Details

__init__(self, f, mode='r', compression='none', chunk_threshold=786432, allow_unindexed=False, options=None, skip_index=False)
(Constructor)

source code 

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.

Parameters:
  • f (str or file) - filename of bag to open or a stream to read from
  • mode (str) - mode, either 'r', 'w', or 'a'
  • compression (str) - compression mode, see rosbag.Compression for valid modes
  • chunk_threshold (int) - minimum number of uncompressed bytes per chunk
  • allow_unindexed (bool) - if True, allow opening unindexed bags
  • options (dict) - the bag options (currently: compression and chunk_threshold)
  • skip_index (bool) - if True, don't read the connection index records on open [2.0+]
Raises:
Overrides: object.__init__

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

source code 

Read messages from the bag, optionally filtered by topic, timestamp and connection details.

Parameters:
  • topics (list(str) or str) - list of topics or a single topic. if an empty list is given all topics will be read [optional]
  • start_time (genpy.Time) - earliest timestamp of message to return [optional]
  • end_time (genpy.Time) - latest timestamp of message to return [optional]
  • connection_filter (function taking (topic, datatype, md5sum, msg_def, header) and returning bool) - function to filter connections to include [optional]
  • raw (bool) - if True, then generate tuples of (datatype, (data, md5sum, position), pytype)
Returns: generator of tuples of (str, genpy.Message, genpy.Time) [not raw] or (str, (str, str, str, tuple, class), genpy.Time) [raw]
generator of BagMessage(topic, message, timestamp) namedtuples for each message in the bag file

flush(self)

source code 

Write the open chunk to disk so subsequent reads will read all messages.

Raises:
  • ValueError - if bag is closed

write(self, topic, msg, t=None, raw=False)

source code 

Write a message to the bag.

Parameters:
  • topic (str) - name of topic
  • msg (Message or tuple of raw message data) - message to add to bag, or tuple (if raw)
  • t (genpy.Time) - ROS time of message publication, if None specifed, use current time [optional]
  • raw (bool) - if True, msg is in raw format, i.e. (msg_type, serialized_bytes, md5sum, pytype)
Raises:
  • ValueError - if arguments are invalid or bag is closed

reindex(self)

source code 

Reindexes the bag file. Yields position of each chunk for progress.

close(self)

source code 

Close the bag file. Closing an already closed bag does nothing.

get_compression_info(self)

source code 

Returns information about the compression of the bag

Returns: generator of CompressionTuple of (str, int, int)
generator of CompressionTuple(compression, uncompressed, compressed) describing the type of compression used, size of the uncompressed data in Bytes, size of the compressed data in Bytes. If no compression is used then uncompressed and compressed data will be equal.

get_message_count(self, topic_filters=None)

source code 

Returns the number of messages in the bag. Can be filtered by Topic

Parameters:
  • topic_filters (Could be either a single str or a list of str.) - One or more topics to filter by
Returns: int
The number of messages in the bag, optionally filtered by topic

get_start_time(self)

source code 

Returns the start time of the bag.

Returns: float, timestamp in seconds, includes fractions of a second
a timestamp of the start of the bag

get_end_time(self)

source code 

Returns the end time of the bag.

Returns: float, timestamp in seconds, includes fractions of a second
a timestamp of the end of the bag

get_type_and_topic_info(self, topic_filters=None)

source code 

Coallates info about the type and topics in the bag.

Note, it would be nice to filter by type as well, but there appear to be some limitations in the current architecture
that prevent that from working when more than one message type is written on the same topic.

@param topic_filters: specify one or more topic to filter by.
@type topic_filters: either a single str or a list of str.
@return: generator of TypesAndTopicsTuple(types{key:type name, val: md5hash}, 
    topics{type: msg type (Ex. "std_msgs/String"),
        message_count: the number of messages of the particular type,
        connections: the number of connections,
        frequency: the data frequency,
        key: type name,
        val: md5hash}) describing the types of messages present
    and information about the topics
@rtype: TypesAndTopicsTuple(dict(str, str), 
    TopicTuple(str, int, int, float, str, str))

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

Property Details

options

Get the options.

Get Method:
unreachable.options(self) - Get the options.

filename

Get the filename.

Get Method:
unreachable.filename(self) - Get the filename.

version

Get the version.

Get Method:
unreachable.version(self) - Get the version.

mode

Get the mode.

Get Method:
unreachable.mode(self) - Get the mode.

size

Get the size in bytes.

Get Method:
unreachable.size(self) - Get the size in bytes.

compression

Get the compression method to use for writing.

Get Method:
_get_compression(self) - Get the compression method to use for writing.
Set Method:
_set_compression(self, compression) - Set the compression method to use for writing.

chunk_threshold

Get the chunk threshold to use for writing.

Get Method:
_get_chunk_threshold(self) - Get the chunk threshold to use for writing.
Set Method:
_set_chunk_threshold(self, chunk_threshold) - Set the chunk threshold to use for writing.