Skip to main content

A library to store robot data in a msgpack format

Project description

image

The Collector

Latest Version License image image

This is still under heavy development

The idea behind this a container that can store data and time tag the data when it is captured. The main structure is a dict which has keys for each data series stored.

This was written for a class I taught on robotics. It is meant to be simple and teach the students some things. There are probably better solutions out there, but I like this. :smirk:

Setup

Install

The suggested way to install this is via the pip command as follows:

pip install the_collector
pip install the_collector[numpy]

If you install numpy, then you get access to working with numpy arrays using the functions: array_pack() and array_pack(). These really don't save you much.

Development

To submit git pulls, clone the repository and set it up as follows:

git clone https://github.com/MomsFriendlyRobotCompany/the-collector
cd the-collector
pip install -e .

Usage

Bag stores data in memory until the buffer size limit is reached then it dumps the data to a file.

from __future__ import print_function
from the_collector import BagReader, BagWriter


d = {'a': 1, 'b': 2}
# bag = BagWriter('bob.bag', buffer_size=1000)  # you can change buffer size
bag = BagWriter('bob.bag') # .bag is automagically appended if not present

# grab some data
for _ in range(100):
    bag.push('temperature', d)    # the key name can be anything
    bag.push('something else', d) # when you use BagReader, these become dict keys

# flushes any remaining data to the file and closes the file
bag.close()

# now read it back
bag = BagReader()
data = bag.read('bob.bag')

If you want to record a time stamp for each data collect (using python's time.time()), just do a bag.push_stamp(). However, when you read back the data, it will now be (data, time_stamp).

Messages

Use the built-in message types:

# from the_collector.messages import serialize, deserialize
from the_collector.messages import Pose, Vector, Quaternion, IMU, Image
from the_collector.messages import Messages

# this holds encode/decode for messages
messages = Messages()
bag = BagWriter(filename, buffer_size=10, pack=messages.serialize)

for i in range(20):
    d = Pose(Vector(1, 1, 1), Quaternion(1, 1, 1, 1))
    bag.push('test', d)


...

bag = BagReader(unpack=messages.deserialize)
load = bag.read(filename)

Create and add new ones:

# from the_collector.messages import serialize, deserialize
from the_collector.messages import Pose, Vector, Quaternion, IMU, Image
from the_collector.messages import Messages

# this is a simple message, only python types in it
Test = namedtuple('Test', 'a b c')
# this is a complex message, has other messages
Test2 = namedtuple('Test2', 'a b')

class myMessages(Messages):
    """
    How to add new messages?
    """
    def __init__(self):
        Messages.__init__(self, sm={'Test': Test}, cm={'Test2': Test2})

msgs = myMessages()
bag = BagWriter(filename, pack=msgs.serialize)

...

bag = BagReader(unpack=msgs.deserialize)
load = bag.read(filename)

Custom Pack/Unpack

You can pass functions to pack or unpack custom data structures to BagReader(pack=...) and BagWriter(unpack=...) as expained in the msgpack docs here

def ext_unpack(msg):
    # do some cool stuff here
    # see msgpack docs for examples

# calls the function ext_unpack when something custom is encountered
bag = BagReader(unpack=ext_unpack)
def ext_pack(msg):
    # do some cool stuff here
    # see msgpack docs for examples

# calls the function ext_unpack when something custom is encountered
bag = BagWriter('bob.bag', pack=ext_pack)

Todo

  • Maybe allow BagReader and BagWriter to accept a file-like object (io.BytesIO) but I am not sure of the value for this. It would be nice for testing, so I don't have to always use os.remove() to clean up bag files. What is a use case?

History

  • Originally started with storing the file as a json file
  • Added compression to reduce the size, this proved to be superior to python's pickle library
  • Looked at Google's protobufs, they seemed complex and the message types didn't really have what I wanted
  • msgpack seems to be fast and compact, tried using compression (gzip library) on msgpack data, but it actually made it worse. Decided to go with this and it saves me a compression step

Change Log

Date Version Notes
2018-07-25 0.7.0 added messages and a way to do custom messages
2018-07-14 0.6.0 changed interface to support buffered writing to disk
2018-07-09 0.5.0 moved away from json and now using msgpack
2017-11-23 0.4.0 fixes, documentation, unit tests
2017-10-04 0.0.1 init

The MIT License (MIT)

Copyright (c) 2017 Kevin J. Walchko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

the_collector-0.7.0.tar.gz (13.2 kB view hashes)

Uploaded Source

Built Distributions

the_collector-0.7.0-py3-none-any.whl (12.9 kB view hashes)

Uploaded Python 3

the_collector-0.7.0-py2-none-any.whl (12.9 kB view hashes)

Uploaded Python 2

Supported by

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