-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosbag_tools.py
100 lines (79 loc) · 3.03 KB
/
rosbag_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Copyright (C) Ghost Robotics - All Rights Reserved
Written by Adarsh Kulkarni <[email protected]>
"""
import yaml
import rosbag
from rosbag.bag import Bag
import glob
import argparse
import math
def truncate(number, digits) -> float:
stepper = 10.0 ** digits
return math.trunc(stepper * number) / stepper
def pretty_list(l, indent=0):
for value in l:
if isinstance(value, dict):
pretty_dict(value, indent + 1)
elif isinstance(value, list):
pretty_list(value, indent + 1)
else:
print('\t' * (indent + 1) + str(value))
def pretty_dict(d, indent=0):
for key, value in d.items():
if key == "types":
continue
print('\t' * indent + str(key))
if isinstance(value, dict):
pretty_dict(value, indent + 1)
elif isinstance(value, list):
pretty_list(value, indent + 1)
else:
print('\t' * (indent + 1) + str(value))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="")
parser.add_argument("directory", help="Directory with rosbags")
parser.add_argument("--print", default=False, help="print out the yamls")
parser.add_argument("--topic", default=None, help="If you want bags with a specific topic, provide it here")
parser.add_argument("--write", default=False)
parser.add_argument("--write_file", default="bags.txt")
args = parser.parse_args()
topic_count = {}
topic_total_msgs = {}
bag_files = glob.glob(args.directory + "*.bag")
for bag in bag_files:
info_dict = yaml.load(Bag(bag, 'r')._get_yaml_info())
if args.print:
pretty_dict(info_dict)
print("-" * 40)
for topic_dict in info_dict["topics"]:
topic = topic_dict["topic"]
if topic not in topic_count:
topic_count[topic] = []
topic_total_msgs[topic] = 0
topic_count[topic].append(bag)
topic_total_msgs[topic] += topic_dict["messages"]
f = None
if args.write:
f = open("summary_" + args.write_file, 'w')
print("Overall Topic Counts: ")
f.write("Overall Topic Counts:\n")
print("-" * 50)
f.write("-" * 50 + "\n\n")
for key, val in topic_count.items():
print("|", key, (40 - len(key)) * " ", "|", len(val), "\t|", truncate(len(val) / len(bag_files) * 1.0, 2),
"\t|", topic_total_msgs[key])
f.write("|" + key + (40 - len(key)) * " " + "|" + str(len(val)) + "\t|" + str(truncate(len(val) / len(bag_files) * 1.0, 2)) + "\t|" +
str(topic_total_msgs[key]) + "\n")
print("-" * 50, "\n\n")
f.write("-" * 50 + "\n\n")
if args.topic is not None:
print("Bags with {} in them".format(args.topic))
f = None
if args.write:
f = open(args.topic[1:].replace("/", "_") + "_" + args.write_file, 'w')
for bag in topic_count[args.topic]:
print(bag)
if args.write:
f.write(bag + "\n")
print("-" * 50, "\n\n")