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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#!/usr/bin/env python3
#
# Copyright 2012 Ettus Research LLC
# Copyright 2018 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
import sys, re
from optparse import OptionParser
import matplotlib.pyplot as plt
import matplotlib.font_manager
import numpy as np
try:
from gnuradio.eng_option import eng_option
except:
eng_option = None
_units = [
(3, "k"),
(6, "M"),
(9, "G")
]
def _format_rate(rate):
for (u1, s1), (u2, s2) in zip(_units, _units[1:]):
n = pow(10, u1)
if rate >= n and rate < pow(10, u2):
r = rate % n
if r > 0:
return str(1.0 * rate / n) + " " + s1
else:
return str(rate / n) + " " + s1
return str(rate) + " "
def _sort(series, keys):
if len(keys) == 0:
return []
key = keys[0]
rev = False
if key[0] == '-':
key = key[1:]
rev = True
l = []
for s in series:
if s[key] not in l:
l += [s[key]]
l.sort()
if rev:
l.reverse()
return [(key, l)] + _sort(series, keys[1:])
def _order(series, sort_list):
if len(sort_list) == 0:
return series
(sort_key, sort_key_list) = sort_list[0]
if len(sort_key_list) == 0:
return []
#print sort_key, sort_key_list
l = []
for s in series:
if s[sort_key] == sort_key_list[0]:
l += [s]
#print l
return _order(l, sort_list[1:]) + _order(series, [(sort_list[0][0], sort_list[0][1][1:])] + sort_list[1:])
def get_option_parser():
usage = "%prog: [options]"
opt_kwds = {}
if eng_option: opt_kwds['option_class'] = eng_option
parser = OptionParser(usage=usage, **opt_kwds)
parser.add_option("", "--id", type="string", help="device ID [default: %default]", default=None)
parser.add_option("", "--sort", type="string", help="sort order [default: %default]", default="rate -spb -spp")
parser.add_option("", "--output", type="string", help="output file [default: %default]", default=None)
parser.add_option("", "--output-type", type="string", help="output file type [default: %default]", default="pdf")
parser.add_option("", "--output-size", type="string", help="output file size [default: %default pixels]",
default="1600,900")
parser.add_option("", "--xrange", type="float", help="X range [default: %default]", default=None)
parser.add_option("", "--title", type="string", help="additional title [default: %default]", default=None)
parser.add_option("", "--legend", type="string", help="legend position [default: %default]", default="lower right")
parser.add_option("", "--diff", action="store_true", help="compare results instead of just plotting them", default=None)
return parser
def get_sorted_series(args, options):
series = []
if len(args) > 0:
with open(args[0]) as f:
lines = f.readlines()
else:
lines = sys.stdin.readlines()
if lines is None or len(lines) == 0:
return
for line in lines:
line = line.strip()
if len(line) == 0:
continue
x = {'file': line}
idx2 = 0
idx = line.find("latency-stats")
if idx > 0:
x['prefix'] = line[0:idx]
idx = line.find(".id_")
if idx > -1:
idx += 4
idx2 = line.find("-", idx)
x['id'] = line[idx:idx2]
if options.id is None:
options.id = x['id']
elif options.id != x['id']:
print "Different IDs:", options.id, x['id']
idx = line.find("-rate_")
if idx > -1:
idx += 6
idx2 = line.find("-", idx)
x['rate'] = int(line[idx:idx2])
idx = line.find("-spb_")
if idx > -1:
idx += 5
idx2 = line.find("-", idx)
x['spb'] = int(line[idx:idx2])
idx = line.find("-spp_")
if idx > -1:
idx += 5
#idx2 = line.find(".", idx)
idx2 = re.search("\D", line[idx:])
if idx2:
idx2 = idx + idx2.start()
else:
idx2 = -1
x['spp'] = int(line[idx:idx2])
idx = line.rfind(".")
if idx > -1 and idx >= idx2:
idx2 = re.search("\d", line[::-1][len(line) - idx:])
if idx2 and (idx2.start() > 0):
idx2 = idx2.start()
x['suffix'] = line[::-1][len(line) - idx:][0:idx2][::-1]
print x
series += [x]
sort_keys = options.sort.split()
print sort_keys
sorted_key_list = _sort(series, sort_keys)
print sorted_key_list
series = _order(series, sorted_key_list)
return series
def main():
# Create object with all valid options
parser = get_option_parser()
# Read in given command line options and arguments
(options, args) = parser.parse_args()
# series contains path and attributes for all data sets given by args.
series = get_sorted_series(args, options)
# Read in actual data sets from file
data = read_series_data(series)
if options.diff:
data = calculate_data_diff(data)
# Get all the wanted properties for this plot
plt_props = get_plt_props(options)
print plt_props
mpl_plot(data, plt_props)
return 0
def read_series_data(series):
if series is None: return []
result = []
for s in series:
data = {}
[data_x, data_y] = np.loadtxt(s['file'], delimiter=" ", unpack=True)
data['x'] = data_x
data['y'] = data_y
data['metadata'] = s
result.append(data)
return result
def find_values(data, key):
result = []
for d in data:
val = d['metadata'][key]
if not val in result:
result.append(val)
return result
def find_match(data, key, val):
result = []
for d in data:
meta = d['metadata']
if meta[key] == val:
result.append(d)
return result
def get_data_diff(data):
if not data:
return data # just return. User didn't input any data.
if len(data) < 2:
return data[0] # Single data set. Can't calculate a diff.
print "diff %d: rate %s, spb %s, spp %s" % (len(data), data[0]['metadata']['rate'], data[0]['metadata']['spb'], data[0]['metadata']['spp'])
data = align_data(data)
min_len = len(data[0]['x'])
for d in data:
min_len = min(min_len, len(d['x']))
metadiff = ""
for d in data:
m = d['metadata']['prefix']
for r in "/._":
m = m.replace(r, "")
metadiff += m + "-"
xd = data[0]['x'][0:min_len]
yd = data[0]['y'][0:min_len]
meta = data[0]['metadata']
meta['diff'] = metadiff
other = data[1:]
for d in other:
y = d['y']
for i in range(len(yd)):
yd[i] -= y[i]
result = {}
result['x'] = xd
result['y'] = yd
result['metadata'] = meta
return result
def align_data(data):
x_start = 0
for d in data:
x_start = max(x_start, d['x'][0])
for i in range(len(data)):
s = np.where(data[i]['x'] == x_start)[0]
data[i]['x'] = data[i]['x'][s:]
data[i]['y'] = data[i]['y'][s:]
return data
def calculate_data_diff(data):
spps = find_values(data, "spp")
spbs = find_values(data, "spb")
rates = find_values(data, "rate")
print spps, "\t", spbs, "\t", rates
result = []
for rate in rates:
rd = find_match(data, "rate", rate)
for spb in spbs:
bd = find_match(rd, "spb", spb)
for spp in spps:
pd = find_match(bd, "spp", spp)
if len(pd) > 0:
result.append(get_data_diff(pd))
return result
def get_plt_props(options):
plt_props = {}
plt_out = None
if options.output is not None:
try:
idx = options.output_size.find(",")
x = int(options.output_size[0:idx])
y = int(options.output_size[idx + 1:])
plt_out = {'name': options.output,
'type': options.output_type,
'size': [x, y]}
except:
plt_out = None
plt_props['output'] = plt_out
if not options.id: options.id = "no data"
plt_title = "Latency (" + options.id + ")"
if options.title is not None and len(options.title) > 0:
plt_title += " - " + options.title
plt_props['title'] = plt_title
plt_props['xlabel'] = "Latency (us)"
plt_props['ylabel'] = "Normalised success of on-time burst transmission"
plt_legend_loc = None
if options.legend is not None:
plt_legend_loc = options.legend
plt_props['legend'] = plt_legend_loc
plt_xrange = None
if options.xrange is not None:
plt_xrange = [0, options.xrange]
plt_props['xrange'] = plt_xrange
return plt_props
def mpl_plot(data, props):
plt_out = props['output']
plt_title = props['title']
plt_xlabel = props['xlabel']
plt_ylabel = props['ylabel']
plt_legend_loc = props['legend']
plt_xrange = props['xrange']
markers = ['.', ',', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8',
's', 'p', '*', 'h', 'H', '+', 'D', 'd', '|', '_']
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
midx = 0
# plot available data.
mylegend = []
for d in data:
mylegend.append(get_legend_str(d['metadata']))
plt.plot(d['x'], d['y'], marker=markers[midx], markerfacecolor=None)
midx = (midx + 1) % len(markers)
# Set all plot properties
plt.title(plt_title)
plt.xlabel(plt_xlabel)
plt.ylabel(plt_ylabel)
plt.grid()
fontP = matplotlib.font_manager.FontProperties()
fontP.set_size('x-small')
plt.legend(mylegend, loc=plt_legend_loc, prop=fontP, ncol=2)
if plt_xrange is not None:
plt.xlim(plt_xrange)
# Save plot to file, if option is given.
if plt_out is not None:
fig = plt.gcf() # get current figure
dpi = 100.0 # Could be any value. It exists to convert the input in pixels to inches/dpi.
figsize = (plt_out['size'][0] / dpi, plt_out['size'][1] / dpi) # calculate figsize in inches
fig.set_size_inches(figsize)
name = plt_out['name'] + "." + plt_out['type']
plt.savefig(name, dpi=dpi, bbox_inches='tight')
plt.show()
def get_legend_str(meta):
lt = ""
if meta.has_key('diff') and meta['diff']:
lt += meta['diff'] + " "
lt += "%ssps, SPB %d, SPP %d" % (_format_rate(meta['rate']), meta['spb'], meta['spp'])
return lt
if __name__ == '__main__':
main()
|