Skip to content

Commit 849cd57

Browse files
author
Enrique Fernández Perdomo
committed
Merge pull request #3 from clearpathrobotics/check_drop_plot_format
Check drop plot format
2 parents d3024b5 + a5e5486 commit 849cd57

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

bag_tools/scripts/check_drop.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import matplotlib.pyplot as plt
3939

40-
def check_drop(inbags):
40+
def check_drop(inbags, plot_format='png'):
4141
# Retrieve msg time, bag time and sequence number for all topics and messages:
4242
msg_time = {}
4343
bag_time = {}
@@ -96,6 +96,10 @@ def check_drop(inbags):
9696
bag_time_diff_topic = bag_time_diff[topic]
9797
seq_diff_topic = seq_diff[topic]
9898

99+
if len(msg_time_diff_topic) == 0:
100+
rospy.logwarn('%s has no messages', topic.ljust(max_len + 2))
101+
continue
102+
99103
msg_time_diff_min = numpy.min(msg_time_diff_topic)
100104
msg_time_diff_max = numpy.max(msg_time_diff_topic)
101105
msg_time_diff_mean = numpy.mean(msg_time_diff_topic)
@@ -119,31 +123,35 @@ def check_drop(inbags):
119123
N = len(msg_time_diff_topic)
120124
x = numpy.array([0, N - 1])
121125

122-
fig = plt.figure()
123-
fig.set_size_inches(20, 15)
124-
125-
plt.subplot(311)
126-
plt.title(topic + ' - Message time difference [s]')
127-
plt.plot(msg_time_diff_topic, 'b')
128-
plt.plot(x, numpy.array([msg_time_diff_min, msg_time_diff_min]), 'r--')
129-
plt.plot(x, numpy.array([msg_time_diff_max, msg_time_diff_max]), 'r--')
130-
plt.plot(x, numpy.array([msg_time_diff_mean, msg_time_diff_mean]), 'g')
131-
132-
plt.subplot(312)
133-
plt.title(topic + ' - Bag time difference [s]')
134-
plt.plot(bag_time_diff_topic, 'b')
135-
plt.plot(x, numpy.array([bag_time_diff_min, bag_time_diff_min]), 'r--')
136-
plt.plot(x, numpy.array([bag_time_diff_max, bag_time_diff_max]), 'r--')
137-
plt.plot(x, numpy.array([bag_time_diff_mean, bag_time_diff_mean]), 'g')
138-
139-
plt.subplot(313)
140-
plt.title(topic + ' - Sequence number difference')
141-
plt.plot(seq_diff_topic, 'b')
142-
plt.plot(x, numpy.array([seq_diff_min, seq_diff_min]), 'r--')
143-
plt.plot(x, numpy.array([seq_diff_max, seq_diff_max]), 'r--')
144-
plt.plot(x, numpy.array([seq_diff_mean, seq_diff_mean]), 'g')
145-
146-
plt.savefig(basename + topic.replace('/', '_').replace(' ', '_').replace(':', '_') + '.png')
126+
try:
127+
fig = plt.figure()
128+
fig.set_size_inches(20, 15)
129+
130+
plt.subplot(311)
131+
plt.title(topic + ' - Message time difference [s]')
132+
plt.plot(msg_time_diff_topic, 'b')
133+
plt.plot(x, numpy.array([msg_time_diff_min, msg_time_diff_min]), 'r--')
134+
plt.plot(x, numpy.array([msg_time_diff_max, msg_time_diff_max]), 'r--')
135+
plt.plot(x, numpy.array([msg_time_diff_mean, msg_time_diff_mean]), 'g')
136+
137+
plt.subplot(312)
138+
plt.title(topic + ' - Bag time difference [s]')
139+
plt.plot(bag_time_diff_topic, 'b')
140+
plt.plot(x, numpy.array([bag_time_diff_min, bag_time_diff_min]), 'r--')
141+
plt.plot(x, numpy.array([bag_time_diff_max, bag_time_diff_max]), 'r--')
142+
plt.plot(x, numpy.array([bag_time_diff_mean, bag_time_diff_mean]), 'g')
143+
144+
plt.subplot(313)
145+
plt.title(topic + ' - Sequence number difference')
146+
plt.plot(seq_diff_topic, 'b')
147+
plt.plot(x, numpy.array([seq_diff_min, seq_diff_min]), 'r--')
148+
plt.plot(x, numpy.array([seq_diff_max, seq_diff_max]), 'r--')
149+
plt.plot(x, numpy.array([seq_diff_mean, seq_diff_mean]), 'g')
150+
151+
plt.savefig(basename + topic.replace('/', '_').replace(' ', '_').replace(':', '_') + '.' + plot_format)
152+
plt.close(fig)
153+
except OverflowError as e:
154+
rospy.logerr('%s: Failed to save plots as %s image files (try other format, e.g. svg): %s', topic.ljust(max_len + 2), plot_format, e.message)
147155

148156
if __name__ == "__main__":
149157
rospy.init_node('check_drop', anonymous=True)
@@ -153,9 +161,10 @@ def check_drop(inbags):
153161
'message dropping in a bagfile. Prints out min, max and mean '
154162
'differences and all values are shown on a plot.')
155163
parser.add_argument('inbag', help='input bagfile(s)', nargs='+')
164+
parser.add_argument('--plot_format', help='output plot format', default='png')
156165
args = parser.parse_args()
157166
try:
158-
check_drop(args.inbag)
167+
check_drop(args.inbag, args.plot_format)
159168
except Exception, e:
160169
import traceback
161170
traceback.print_exc()

0 commit comments

Comments
 (0)