-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosbag2usage.py
executable file
·100 lines (86 loc) · 2.81 KB
/
rosbag2usage.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
#!/bin/env python3
import numpy as np
import plotly
import argparse
import pathlib
from rosbag2_py import ConverterOptions
from rosbag2_py import SequentialReader
from rosbag2_py import StorageOptions
def analyze(bag_name):
def create_reader(bag_dir: str) -> SequentialReader:
storage_options = StorageOptions(
uri=bag_dir,
storage_id="sqlite3",
)
converter_options = ConverterOptions(
input_serialization_format="cdr",
output_serialization_format="cdr",
)
reader = SequentialReader()
reader.open(storage_options, converter_options)
return reader
reader = create_reader(str(bag_name))
type_map = {}
for topic_type in reader.get_all_topics_and_types():
type_map[topic_type.name] = topic_type.type
size_dict = {}
while reader.has_next():
(topic, data, stamp) = reader.read_next()
if topic in size_dict:
size_dict[topic] += np.int64(len(data))
else:
size_dict[topic] = np.int64(len(data))
return size_dict
def visualize(size_dict):
temp = {}
def register(name, size):
parent = name.rsplit('/',1)[0]
child = name
if child in temp:
temp[child]['size'] += np.int64(size)
else:
temp[child] = {'parent': parent, 'size': np.int64(size)}
if parent:
register(parent, size)
else:
return
for topic, size in size_dict.items():
register(topic, size)
def sizeof_fmt(num):
for unit in ["", "KB", "MB", "GB"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}"
num /= 1024.0
return f"{num:.1f}TB"
name=[]
parent=[]
size=[]
label=[]
text=[]
for topic, data in temp.items():
name.append(topic)
parent.append(data['parent'])
size.append(data['size'])
label.append(topic.rsplit('/',1)[1])
text.append(sizeof_fmt(data['size']))
fig = plotly.graph_objs.Figure()
fig.add_trace(plotly.graph_objs.Treemap(
ids = name,
labels = label,
parents = parent,
values = size,
hovertext=text,
tiling=plotly.graph_objects.treemap.Tiling(packing='squarify') #[‘squarify’, ‘binary’, ‘dice’, ‘slice’, ‘slice-dice’, ‘dice-slice’]
))
fig.show()
def main():
parser = argparse.ArgumentParser(description="Visualize the capacity of each topic. ")
parser.add_argument("input_bag", help="input bag stored path")
args = parser.parse_args()
input_path = pathlib.Path(args.input_bag)
if not input_path.exists():
raise FileNotFoundError("Input bag folder '{}' is not found.".format(input_path))
size_dict = analyze(input_path)
visualize(size_dict)
if __name__ == "__main__":
main()