forked from moonlight200/quic-opensand-evaluation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.py
1980 lines (1738 loc) · 101 KB
/
analyze.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import multiprocessing as mp
import os
from typing import Optional, Dict, Tuple, Iterable, List, Callable, Generator
import numpy as np
import pandas as pd
from pygnuplot import gnuplot
import matplotlib.pyplot as plt
import seaborn as sns
from common import MeasureType, GRAPH_DIR, DATA_DIR, logger
LINE_COLORS = ['000000', 'FF0000', '9400D3', '0000FF', '006400', 'FF8C00', 'FFD700', '00FFFF', '00FF7F',
'FFA500', 'ADFF2F', 'EE82EE', '4169E1', 'FF1493', 'FFC0CB', '2E8B57']
POINT_TYPES = [2, 4, 8, 10, 6, 12, 9, 11, 13, 15, 17, 20, 22, 33, 34, 50]
SI = {
'K': 2 ** 10, 'M': 2 ** 20, 'G': 2 ** 30, 'T': 2 ** 40, 'P': 2 ** 50, 'E': 2 ** 60,
'k': 10 ** 3, 'm': 10 ** 6, 'g': 10 ** 9, 't': 10 ** 12, 'p': 10 ** 15, 'e': 10 ** 18,
}
GRAPH_PLOT_SIZE_CM = (12, 6)
GRAPH_PLOT_SECONDS = 30
GRAPH_PLOT_RTT_SECONDS = 100
GRAPH_X_BUCKET = 0.1
VALUE_PLOT_SIZE_CM = (8, 8)
MATRIX_KEY_SIZE = 0.12
# For sideways figure
MATRIX_SUBPLOT_SIZE_CM = (GRAPH_PLOT_SIZE_CM[0] * 0.75, GRAPH_PLOT_SIZE_CM[1] * 0.75)
# For normal figure
# MATRIX_SUBPLOT_SIZE_CM = (GRAPH_PLOT_SIZE_CM[0] * 0.5, GRAPH_PLOT_SIZE_CM[1] * 0.75)
DEBUG_GNUPLOT = False
PointMap = Dict[any, int]
LineMap = Dict[any, str]
FileTuple = Tuple[any, ...]
DataTuple = Tuple[any, ...]
def get_point_type(point_map: PointMap, val: any):
"""
Selects the gnuplot 'pointtype' based on the given value. The map ensures, that the same values give the same types.
:param point_map: The map to lookup point types from
:param val: The value to lookup or generate a point type for
:return:
"""
if val not in point_map:
idx = len(point_map)
# Use default value if more point types than specified are requested
point_map[val] = 7 if idx >= len(POINT_TYPES) else POINT_TYPES[idx]
return point_map[val]
def get_line_color(line_map: LineMap, val: any):
"""
Selects the gnuplot 'linecolor' based on the given value. The map ensures, that the same values give the same color.
:param line_map: The map to lookup line colors from
:param val: The value to lookup or generate a line color for
:return:
"""
if val not in line_map:
idx = len(line_map)
# Use default value if more line colors than specified are requested
line_map[val] = '7F7F7F' if idx >= len(LINE_COLORS) else LINE_COLORS[idx]
return line_map[val]
def sat_key(sat: str):
"""
Provides the key for sorting sat orbits from closest to earth to furthest away from earth.
:param sat: The satellite name to sort
:return:
"""
try:
return ['NONE', 'LEO', 'MEO', 'GEO'].index(sat.upper())
except ValueError:
return -1
def sat_tuple_key(section_tuple: Tuple[any, ...]):
"""
Wrapper for sat_key receiving a tuple where the first element is the sat.
:param section_tuple: A tuple with the sat value on the first entry
:return:
"""
return sat_key(section_tuple[0])
def apply_si(val: str) -> int:
"""
Parse an integer from a string with an optional SI suffix.
:param val: The string to parse the integer from.
:return: An integer with the si suffix applied.
"""
if val.isdecimal():
return int(val)
if val[:-1].isdecimal():
return int(val[:-1]) * SI.get(val[-1:], -1)
def create_output_dirs(out_dir: str):
"""
Creates output directories (GRAPH_DIR and DATA_DIR) of the analysis if the don't already exist.
:param out_dir: The output folder set via the command line
:return:
"""
graph_dir = os.path.join(out_dir, GRAPH_DIR)
data_dir = os.path.join(out_dir, DATA_DIR)
if not os.path.exists(graph_dir):
os.makedirs(graph_dir)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
def sprint_buffer_sizes(tbs: str, qbs: str, ubs: str, separator: str = " - ", short_labels: bool = False) -> str:
"""
Format the buffer sizes to the shortest possible values.
:param tbs: Transport buffer string to format
:param qbs: Quicly buffer string to format
:param ubs: UDP buffer string to format
:param separator: Separator between buffer sizes if needed
:param short_labels: Whether to use short labels
:return: Formatted buffer sizes
"""
bs_vals = [
['t', tbs.split(",")],
['q', qbs.split(",")],
['u', ubs.split(",")],
]
identical = False
if bs_vals[1][1][1:3] == bs_vals[0][1] and bs_vals[1][1] == bs_vals[2][1]:
identical = True
for bs_val in bs_vals:
_, vals = bs_val
if np.all([v == vals[0] for v in vals]):
bs_val[1] = vals[:1]
if np.all([v == bs_vals[0][1] for _, v in bs_vals]):
identical = True
if identical:
return "b%s%s" % ("" if short_labels else "s=", ",".join(bs_vals[1][1]))
return separator.join([
"%s%s%s" % (c, "" if short_labels else "bs=", ",".join(val))
for c, val in bs_vals
])
def sprint_tuple(col_names: List[str], col_values: Tuple[any, ...]) -> str:
"""
Format a tuple into a printable string.
:param col_names: Names of the columns in the tuple
:param col_values: Tuple values
:return: A string with all column names and tuple values
"""
return ', '.join(["%s=%s" % (col, str(val)) for col, val in zip(col_names, col_values)])
def unique_cartesian_product(df: pd.DataFrame, *col_names: str) -> Generator[Tuple[any, ...], None, None]:
"""
Generates the cartesian product of the unique values for each column in the dataframe.
:param df: The dataframe to read the unique values per column from.
:param col_names: The names of the columns to use for the cartesian product.
:return: A generator for value tuples based on the specified columns in the given dataframe.
"""
if len(col_names) < 1:
yield tuple()
return
unique_vals = tuple(list(df[name].unique()) for name in col_names)
vids = [0 for _ in col_names]
while vids[0] < len(unique_vals[0]):
yield tuple(unique_vals[cid][vid] for cid, vid in enumerate(vids))
# Increment
for cid in range(len(col_names) - 1, -1, -1):
vids[cid] += 1
if vids[cid] < len(unique_vals[cid]):
break
elif cid != 0:
vids[cid] = 0
def not_nan_tuples(df: pd.DataFrame, data_cols: List[str], nan_cols: List[str]) \
-> Generator[Tuple[any, ...], None, None]:
"""
Creates a unique cartesian product of the data cols and filters those tuples out where all values in the nan_cols are
NaN.
:param df: The dataframe to create the cartesian product from and which to use for the NaN check
:param data_cols: The names of the columns to create the unique cartesian product from
:param nan_cols: The names of the columns to look at when deciding if the data are NaN
:return: The filtered unique cartesian product.
"""
for data_tuple in unique_cartesian_product(df, *data_cols):
data_filter = True
for col_name, col_val in zip(data_cols, data_tuple):
data_filter &= df[col_name] == col_val
if not df.loc[data_filter][nan_cols].isnull().values.all():
yield data_tuple
def filter_by_tuples(df: pd.DataFrame, tuples: List[Tuple[any, ...]], cols: List[str]) -> pd.DataFrame:
"""
Filters the dataframe by the given tuples. Only such rows remain, where the specified columns have one of the tuples
as values.
:param df: The dataframe to filter
:param tuples: The tuples to filter by
:param cols: The names of the columns to match to the tuples
:return: The filtered dataframe
"""
data_filter = False
for filter_tuple in tuples:
tuple_filter = True
for col_name, col_val in zip(cols, filter_tuple):
tuple_filter &= df[col_name] == col_val
data_filter |= tuple_filter
return df.loc[data_filter]
def filter_graph_data(df: pd.DataFrame, x_col: str, x_range: Optional[Tuple[int, int]], file_cols: List[str],
file_tuple: FileTuple) -> Optional[pd.DataFrame]:
"""
Filter data relevant for the graph from the dataframe.
:param df: The dataframe to filter
:param x_col: Name of the column that has the data for the x-axis, only used if x_range is given
:param x_range: (min, max) tuple for filtering the values for the x-axis, or None for no filter
:param file_cols: Column names that define values for which separate graphs are generated
:param file_tuple: The set of values for the file_cols that are used in this graph
:return:
"""
gdf_filter = True
if x_range is not None:
gdf_filter = (df[x_col] >= x_range[0]) & (df[x_col] < x_range[1])
for col_name, col_val in zip(file_cols, file_tuple):
gdf_filter &= df[col_name] == col_val
gdf = df.loc[gdf_filter]
return None if gdf.empty else gdf
def prepare_time_series_graph_data(df: pd.DataFrame, x_col: str, y_col: str, x_range: Optional[Tuple[int, int]],
x_bucket: Optional[float], y_div: float, extra_title_col: str, file_cols: List[str],
file_tuple: FileTuple, data_cols: List[str], point_map: PointMap, line_map: LineMap,
point_type_indices: List[int], line_color_indices: List[int],
format_data_title: Callable[[DataTuple], str],
data_tuple_key_transform: Callable[[DataTuple], any] = lambda x: x,
plot_confidence_area: bool = False
) -> Optional[Tuple[pd.DataFrame, List[str], List[tuple]]]:
"""
Prepare data to be used in a time series graph.
:param df: The dataframe to read the data from
:param x_col: Name of the column that has the data for the x-axis
:param y_col: Name of the column that has the data for the y-axis
:param x_range: (min, max) tuple for filtering the values for the x-axis
:param x_bucket: Size of the bucket to use for aggregating data on the x-axis
:param y_div: Number to divide the values on the y-axis by before plotting
:param extra_title_col: Name of the column that holds a string prefix for the data title
:param file_cols: Column names that define values for which separate graphs are generated
:param file_tuple: The set of values for the file_cols that are used in this graph
:param data_cols: Column names of the columns used for the data lines
:param point_map: Map that ensures identical point types for same data lines
:param line_map: Map that ensures identical line colors for same data lines
:param point_type_indices: Indices of file_cols used to determine point type
:param line_color_indices: Indices of file_cols used to determine line color
:param format_data_title: Function to format the title of a data line, receives a data_tuple
:param data_tuple_key_transform: Function to transform a data tuple as key for the sort method
:param plot_confidence_area: Whether to add the confidence area to the plot commands
:return: A tuple consisting of a dataframe that holds all data for the graph, a list of plot commands and a list of
data_tuples that will be plotted in the graph. If at some point there are no data left and therefore plotting the
graph would be useless, None is returned.
"""
# Filter data for graph
gdf = filter_graph_data(df, x_col, x_range, file_cols, file_tuple)
if gdf is None or gdf.empty:
return None
gdf = pd.DataFrame(gdf)
if x_bucket is not None:
if x_range is not None:
start, end = x_range
else:
start = gdf[x_col].min()
end = gdf[x_col].max()
# Start one bucket earlier to add zero data point (lines start at origin)
# End one bucket after since each bucket is defined as [a;b) with a being the name of the bucket
buckets = np.arange(start=start - x_bucket, stop=end + x_bucket, step=x_bucket)
gdf[x_col] = pd.cut(gdf[x_col], buckets, labels=buckets[1:])
# Calculate mean average per y_value (e.g. per second calculate mean average from each run)
gdf = gdf[[extra_title_col, *data_cols, x_col, y_col]]
gdf = gdf.groupby([extra_title_col, *data_cols, x_col]).aggregate(
mean=pd.NamedAgg(y_col, np.mean),
# If this is not a lambda, it will produce NaN for dataframes with only one value per bucket
std=pd.NamedAgg(y_col, lambda x: np.std(x))
)
# Calculate data lines
gdata = []
if not gdf.empty:
for data_tuple in unique_cartesian_product(df, extra_title_col, *data_cols):
try:
line_df = gdf.loc[data_tuple, ['mean', 'std']]
except KeyError:
# Combination in data_tuple does not exist
continue
if line_df.empty or line_df.isnull().values.all():
# Combination in data_tuple has no data
continue
gdata.append((line_df, data_tuple))
if len(gdata) == 0:
return None
gdata = sorted(gdata, key=lambda x: data_tuple_key_transform(x[1][1:]))
# Merge line data into single df
plot_df = pd.concat([x[0] for x in gdata], axis=1)
# Make first category (named 0.0) start at the origin
plot_df.iloc[0] = 0
# Generate plot commands
plot_cmds = []
if plot_confidence_area:
plot_cmds += [
"using 1:((${y_col:d}+${std_col:d})/{y_div:f}):((${y_col:d}-${std_col:d})/{y_div:f})"
" with filledcurve fillcolor rgb '#E0{fc:s}' fillstyle solid notitle"
"".format(
y_col=index * 2 + 2,
std_col=index * 2 + 3,
y_div=y_div,
fc=get_line_color(line_map, (data_tuple[0], *tuple(data_tuple[i + 1] for i in line_color_indices)))
)
for index, (_, data_tuple) in enumerate(gdata)
]
plot_cmds += [
"using 1:(${y_col:d}/{y_div:f})"
" with linespoints pointtype {pt:d} linecolor '#{lc:s}' title '{extra_title:s}{title:s}'"
"".format(
y_col=index * 2 + 2,
y_div=y_div,
pt=get_point_type(point_map, tuple(data_tuple[i + 1] for i in point_type_indices)),
lc=get_line_color(line_map, (data_tuple[0], *tuple(data_tuple[i + 1] for i in line_color_indices))),
extra_title=data_tuple[0] if len(data_tuple[0]) == 0 else ("%s " % data_tuple[0]),
title=format_data_title(*data_tuple[1:])
)
for index, (_, data_tuple) in enumerate(gdata)
]
return plot_df, plot_cmds, [data_tuple for _, data_tuple in gdata]
def plot_time_series(df: pd.DataFrame, out_dir: str, analysis_name: str, file_cols: List[str], data_cols: List[str],
x_col: str, y_col: str, x_range: Optional[Tuple[int, int]], x_bucket: Optional[float],
y_div: float, x_label: str, y_label: str, point_type_indices: List[int],
line_color_indices: List[int], format_data_title: Callable[[DataTuple], str],
format_file_title: Callable[[FileTuple], str], format_file_base: Callable[[FileTuple], str],
data_sort_key: Callable[[DataTuple], any] = lambda x: x,
extra_title_col: Optional[str] = None) -> None:
"""
Plot a time series graph. It is built for, but not restricted to having a time unit (e.g. seconds) on the x-axis.
:param df: The dataframe to read the data from
:param out_dir: Directory where all output files are placed
:param analysis_name: A name for the analysis used in log statements
:param file_cols: Column names that define values for which separate graphs are generated
:param data_cols: Column names of the columns used for the data lines
:param x_col: Name of the column that has the data for the x-axis
:param y_col: Name of the column that has the data for the y-axis
:param x_range: (min, max) tuple for filtering the values for the x-axis
:param x_bucket: Size of the bucket to use for aggregating data on the x-axis
:param y_div: Number to divide the values on the y-axis by before plotting
:param x_label: Label for the x-axis of the generated graphs
:param y_label: LAbel for the y-axis of the generated graphs
:param point_type_indices: Indices of file_cols used to determine point type
:param line_color_indices: Indices of file_cols used to determine line color
:param format_data_title: Function to format the title of a data line, receives a data_tuple (data_cols values)
:param format_file_title: Function to format the title of a graph, receives a file_tuple (file_cols values)
:param format_file_base: Function to format the base name of a graph file, receives a file_tuple (file_cols values)
:param data_sort_key: Function to transform a data tuple to a key to sort the data by
:param extra_title_col: Name of the column that holds a string prefix for the data title
"""
create_output_dirs(out_dir)
# Ensures same point types and line colors across all graphs
point_map: PointMap = {}
line_map: LineMap = {}
if extra_title_col is None:
extra_title_col = 'default_extra_title'
df[extra_title_col] = ""
for file_tuple in unique_cartesian_product(df, *file_cols):
print_file_tuple = sprint_tuple(file_cols, file_tuple)
logger.info("Generating %s %s", analysis_name, print_file_tuple)
prepared_data = prepare_time_series_graph_data(df,
x_col=x_col,
y_col=y_col,
x_range=x_range,
x_bucket=x_bucket,
y_div=y_div,
extra_title_col=extra_title_col,
file_cols=file_cols,
file_tuple=file_tuple,
data_cols=data_cols,
point_map=point_map,
line_map=line_map,
point_type_indices=point_type_indices,
line_color_indices=line_color_indices,
format_data_title=format_data_title,
data_tuple_key_transform=data_sort_key,
plot_confidence_area=True)
if prepared_data is None:
logger.debug("No data for %s %s", analysis_name, print_file_tuple)
continue
plot_df, plot_cmds, data_tuples = prepared_data
file_base = format_file_base(*file_tuple)
g = gnuplot.Gnuplot(log=DEBUG_GNUPLOT,
title='"%s"' % format_file_title(*file_tuple),
key='outside right center vertical samplen 2',
xlabel='"%s"' % x_label,
ylabel='"%s"' % y_label,
pointsize='0.5',
xrange=None,
yrange='[0:]',
term="pdf size %dcm, %dcm" % GRAPH_PLOT_SIZE_CM,
out='"%s.pdf"' % os.path.join(out_dir, GRAPH_DIR, file_base))
if x_range is not None:
g.set(xrange='[%d:%d]' % x_range)
g.plot_data(plot_df, *plot_cmds)
# Save plot data
plot_df.to_csv(os.path.join(out_dir, DATA_DIR, file_base + '.csv'))
with open(os.path.join(out_dir, DATA_DIR, file_base + '.gnuplot'), 'w+') as f:
f.write("\n".join(plot_cmds))
def plot_time_series_matrix(df: pd.DataFrame, out_dir: str, analysis_name: str, file_cols: List[str],
data_cols: List[str], matrix_x_cols: List[str], matrix_y_cols: List[str], x_col: str,
y_col: str, x_range: Optional[Tuple[int, int]], x_bucket: Optional[float], y_div: float,
x_label: str, y_label: str, point_type_indices: List[int], line_color_indices: List[int],
format_data_title: Callable[[DataTuple], str],
format_subplot_title: Callable[[any, any], str],
format_file_title: Callable[[FileTuple], str],
format_file_base: Callable[[FileTuple], str],
data_sort_key: Callable[[DataTuple], any] = lambda x: x,
sort_matrix_x: Callable[[Iterable], Iterable] = lambda x: sorted(x),
sort_matrix_y: Callable[[Iterable], Iterable] = lambda y: sorted(y),
extra_title_col: Optional[str] = None) -> None:
"""
Plot multiple time series graphs arranged like a 2d-matrix based on two data values. It is built for, but not
restricted to having a time unit (e.g. seconds) on the x-axis of each individual graph.
:param df: The dataframe to read the data from
:param out_dir: Directory where all output files are placed
:param analysis_name: A name for the analysis used in log statements
:param file_cols: Column names that define values for which separate graphs are generated
:param data_cols: Column names of the columns used for the data lines
:param matrix_x_cols: Graphs are horizontally arranged based on values of these columns
:param matrix_y_cols: Graphs are vertically arranged based on values of these columns
:param x_col: Name of the column that has the data for the x-axis
:param y_col: Name of the column that has the data for the y-axis
:param x_range: (min, max) tuple for filtering the values for the x-axis
:param x_bucket: Size of the bucket to use for aggregating data on the x-axis
:param y_div: Number to divide the values on the y-axis by before plotting
:param x_label: Label for the x-axis of the generated graphs
:param y_label: LAbel for the y-axis of the generated graphs
:param point_type_indices: Indices of file_cols used to determine point type
:param line_color_indices: Indices of file_cols used to determine line color
:param format_data_title: Function to format the title of a data line, receives a data_tuple (data_cols values)
:param format_subplot_title: Function to format the title of a subplot, receives a tuple with the values of matrix_x_cols and matrix_y_cols
:param format_file_title: Function to format the title of a graph, receives a file_tuple (file_cols values)
:param format_file_base: Function to format the base name of a graph file, receives a file_tuple (file_cols values)
:param data_sort_key: Function to transform a data tuple to a key to sort the data by
:param sort_matrix_x: Function to sort values of the matrix_x_cols, graphs will be arranged accordingly
:param sort_matrix_y: Function to sort values of the matrix_y_cols, graphs will be arranged accordingly
:param extra_title_col: Name of the column that holds a string prefix for the data title
"""
create_output_dirs(out_dir)
# Ensures same point types and line colors across all graphs
point_map: PointMap = {}
line_map: LineMap = {}
if extra_title_col is None:
extra_title_col = 'default_extra_title'
df[extra_title_col] = ""
for file_tuple in unique_cartesian_product(df, *file_cols):
print_file_tuple = sprint_tuple(file_cols, file_tuple)
logger.info("Generating %s matrix %s", analysis_name, print_file_tuple)
file_df = filter_graph_data(df, x_col, x_range, file_cols, file_tuple)
mx_unique = list(sort_matrix_x(unique_cartesian_product(file_df, *matrix_x_cols)))
my_unique = list(sort_matrix_y(unique_cartesian_product(file_df, *matrix_y_cols)))
mx_cnt = float(max(1, len(mx_unique)))
my_cnt = float(max(1, len(my_unique)))
sub_size = "%f, %f" % ((1.0 - MATRIX_KEY_SIZE) / mx_cnt, 1.0 / my_cnt)
subfigures = []
key_data = set()
# [('1M,1M', '1M,1M,1M,1M', '1M,1M,1M,1M'), ('2M,2M', '1M,2M,2M,1M', '1M,2M,2M,1M'), ('4M,4M', '1M,4M,4M,1M', '1M,4M,4M,1M'), ('8M,8M', '1M,8M,8M,1M', '1M,8M,8M,1M')]
# Generate subfigures
y_max = max(1, np.ceil(file_df[y_col].replace(0, np.nan).quantile(.99) / y_div))
for matrix_y_idx, matrix_y_tuple in enumerate(my_unique):
for matrix_x_idx, matrix_x_tuple in enumerate(mx_unique):
print_subplot_tuple = sprint_tuple([*file_cols, *matrix_x_cols, *matrix_y_cols],
(*file_tuple, *matrix_x_tuple, *matrix_y_tuple))
prepared_data = prepare_time_series_graph_data(file_df,
x_col=x_col,
y_col=y_col,
x_range=x_range,
x_bucket=x_bucket,
y_div=y_div,
extra_title_col=extra_title_col,
file_cols=[*file_cols, *matrix_x_cols, *matrix_y_cols],
file_tuple=(
*file_tuple, *matrix_x_tuple, *matrix_y_tuple),
data_cols=data_cols,
point_map=point_map,
line_map=line_map,
point_type_indices=point_type_indices,
line_color_indices=line_color_indices,
format_data_title=format_data_title,
data_tuple_key_transform=data_sort_key,
plot_confidence_area=True)
if prepared_data is None:
logger.debug("No data for %s %s", analysis_name, print_subplot_tuple)
continue
plot_df, plot_cmds, data_tuples = prepared_data
# Add data for key
key_data.update(data_tuples)
subfigures.append(gnuplot.make_plot_data(
plot_df,
*plot_cmds,
title='"%s"' % format_subplot_title(*matrix_x_tuple, *matrix_y_tuple),
key='off',
xlabel='"%s"' % x_label,
ylabel='"%s"' % y_label,
xrange=None if x_range is None else ('[%d:%d]' % x_range),
yrange='[0:%d]' % y_max,
pointsize='0.5',
size=sub_size,
origin="%f, %f" % (matrix_x_idx * (1.0 - MATRIX_KEY_SIZE) / mx_cnt, matrix_y_idx / my_cnt)
))
# Check if a matrix plot is useful
if len(subfigures) <= 1:
logger.debug("Skipping %s matrix plot for %s, not enough individual plots", analysis_name, print_file_tuple)
continue
# Add null plot for key
key_cmds = [
"NaN with linespoints pointtype %d linecolor '#%s' title '%s%s'" %
(
get_point_type(point_map, tuple(data_tuple[i + 1] for i in point_type_indices)),
get_line_color(line_map, (data_tuple[0], *tuple(data_tuple[i + 1] for i in line_color_indices))),
data_tuple[0] if len(data_tuple[0]) == 0 else ("%s " % data_tuple[0]),
format_data_title(*data_tuple[1:])
)
for data_tuple in sorted(key_data, key=lambda dt: data_sort_key(dt[1:]))
]
subfigures.append(gnuplot.make_plot(
*key_cmds,
key='on inside center center vertical Right samplen 2',
pointsize='0.5',
size="%f, 1" % MATRIX_KEY_SIZE,
origin="%f, 0" % (1.0 - MATRIX_KEY_SIZE),
title=None,
xtics=None,
ytics=None,
xlabel=None,
ylabel=None,
xrange='[0:1]',
yrange='[0:1]',
border=None,
))
gnuplot.multiplot(
*subfigures,
title='"%s"' % format_file_title(*file_tuple),
term='pdf size %dcm, %dcm' %
(MATRIX_SUBPLOT_SIZE_CM[0] * mx_cnt, MATRIX_SUBPLOT_SIZE_CM[1] * my_cnt),
output='"%s.pdf"' % os.path.join(out_dir, GRAPH_DIR, format_file_base(*file_tuple)),
)
def plot_timing(df: pd.DataFrame, out_dir: str, analysis_name: str, file_cols: List[str],
section_cols: List[str], tick_cols: List[str], skew_cols: List[str], y_col: str,
x_label: str, y_label: str,
format_file_title: Callable[[FileTuple], str],
format_file_base: Callable[[FileTuple], str],
format_section_title: Callable[[Tuple[any, ...]], str],
format_tick_title: Callable[[Tuple[any, ...]], str],
format_skew_title: Callable[[Tuple[any, ...]], str],
sort_section: Callable[[List[Tuple[any, ...]]], List[Tuple[any, ...]]] = lambda x: sorted(x),
sort_tick: Callable[[List[Tuple[any, ...]]], List[Tuple[any, ...]]] = lambda x: sorted(x),
sort_skew: Callable[[List[Tuple[any, ...]]], List[Tuple[any, ...]]] = lambda x: sorted(x),
percentile_low: int = 5, percentile_high: int = 95) -> None:
"""
Plot a timing graph that compares the average time and min and max percentiles of a time value in multiple
scenarios. The timing values are separated into (1) sections with a large gap in between, (2) ticks on the x-axis
and (3) skew within a tick.
:param df: The dataframe to read the data from
:param out_dir: Directory where all the output files are placed
:param analysis_name: A name for the analysis used in log statements
:param file_cols: Column names that define values for which separate graphs are generated
:param section_cols: Column names that define values by which the data is separated into sections
:param tick_cols: Column names that define values by which the data is separated into ticks
:param skew_cols: Column names that define values by which the data is skewed within a tick
:param y_col: Column name of the column that has the data being plotted on the y-axis
:param x_label: Label for the x-axis
:param y_label: Label for the y-axis
:param format_file_title: Function to format the title of a graph, receives a file_tuple (file_cols values)
:param format_file_base Function to format the base name of a graph file, receives a file_tuple (file_cols values)
:param format_section_title Function to format the label of a section, receives a section_tuple (section_cols values)
:param format_tick_title Function to format the label of a tick, receives a tick_tuple (tick_cols values)
:param format_skew_title Function to format the title of a data line, receives a skew_tuple (skew_cols values)
:param sort_section Sort section_tuples (section_cols values) to display them in this order
:param sort_tick Sort tick_tuples (tick_cols values) to display them in this order
:param sort_skew Sort skew_tuples (skew_cols values) to display them in this order
:param percentile_low Percentile to display on the lower bound [0;100]
:param percentile_high Percentile to display on the upper bound [0;100]
"""
assert 0 <= percentile_low <= 100
assert 0 <= percentile_high <= 100
create_output_dirs(out_dir)
# Ensures same point types and line colors across all graphs
point_map: PointMap = {}
line_map: LineMap = {}
# Generate graphs
for file_tuple in unique_cartesian_product(df, *file_cols):
print_file_tuple = sprint_tuple(file_cols, file_tuple)
logger.info("Generating %s timing %s", analysis_name, print_file_tuple)
# Filter data relevant for graph
gdf = filter_graph_data(df, "", None, file_cols, file_tuple)
if gdf is None:
logger.debug("No data for %s timing %s", analysis_name, print_file_tuple)
continue
gdf = gdf[[*section_cols, *tick_cols, *skew_cols, y_col]]
gdf = gdf.groupby([*section_cols, *tick_cols, *skew_cols]).aggregate(
mean=pd.NamedAgg(y_col, np.mean),
p_low=pd.NamedAgg(y_col, lambda x: np.percentile(x, q=percentile_low)),
p_high=pd.NamedAgg(y_col, lambda x: np.percentile(x, q=percentile_high))
)
# Make sure that all combinations of sections, ticks and skews exists (needed for gnuplot commands)
# Generate a df with all combinations and NaN values, then update with real values keeping
# NaN's where there are no data in gdf
full_idx = pd.MultiIndex.from_product(gdf.index.levels)
full_gdf = pd.DataFrame(index=full_idx, columns=gdf.columns)
full_gdf.update(gdf)
# Move index back to columns
full_gdf.reset_index(inplace=True)
data_cols = ['mean', 'p_low', 'p_high']
# Filter not nan tuples
sections_sorted = sort_section(list(not_nan_tuples(full_gdf, section_cols, data_cols)))
ticks_sorted = sort_tick(list(not_nan_tuples(full_gdf, tick_cols, data_cols)))
skews_sorted = sort_skew(list(not_nan_tuples(full_gdf, skew_cols, data_cols)))
# Filter dataframe accordingly
full_gdf = filter_by_tuples(full_gdf, sections_sorted, section_cols)
full_gdf = filter_by_tuples(full_gdf, ticks_sorted, tick_cols)
full_gdf = filter_by_tuples(full_gdf, skews_sorted, skew_cols)
# Generate indexes used to calculate x coordinate in plot
full_gdf['section_idx'] = full_gdf[section_cols].apply(lambda x: sections_sorted.index(tuple(x)), axis=1)
full_gdf['tick_idx'] = full_gdf[tick_cols].apply(lambda x: ticks_sorted.index(tuple(x)), axis=1)
full_gdf['skew_idx'] = full_gdf[skew_cols].apply(lambda x: skews_sorted.index(tuple(x)), axis=1)
full_gdf = full_gdf[['section_idx', 'tick_idx', 'skew_idx', 'mean', 'p_low', 'p_high',
*section_cols, *tick_cols, *skew_cols]]
full_gdf.sort_values(by=['section_idx', 'tick_idx', 'skew_idx'], inplace=True, ignore_index=True)
# Create graph
section_cnt = len(sections_sorted)
tick_cnt = len(ticks_sorted)
skew_cnt = len(skews_sorted)
if section_cnt * tick_cnt * skew_cnt == 0:
logger.debug("No data for %s timing %s", analysis_name, print_file_tuple)
continue
x_max = (tick_cnt + 1) * section_cnt
y_max = max(full_gdf['mean'].max(), full_gdf['p_low'].max(), full_gdf['p_high'].max())
y_max_base = 10 ** np.floor(np.log10(y_max))
y_max = int(max(1, np.ceil(y_max / y_max_base) * y_max_base))
file_base = format_file_base(*file_tuple)
g = gnuplot.Gnuplot(log=DEBUG_GNUPLOT,
title='"%s"' % (format_file_title(*file_tuple)),
key='top left samplen 2',
xlabel='"%s"' % x_label,
ylabel='"%s"' % y_label,
xrange="[0:%d]" % x_max,
yrange="[0:%d]" % y_max,
term="pdf size %dcm, %dcm" % VALUE_PLOT_SIZE_CM,
out='"%s.pdf"' % os.path.join(out_dir, GRAPH_DIR, file_base),
pointsize='0.5')
# Add labels for sections
for section_idx, section_tuple in enumerate(sections_sorted):
g.set(label='"%s" at %f,%f center' % (
format_section_title(*section_tuple),
(section_idx + 0.5) * (tick_cnt + 1),
y_max * 0.075
))
# Add xtics for ticks
g.set(xtics='out rotate (%s)' % ", ".join([
'"%s" %d' % (format_tick_title(*tick_tuple), section_idx * (tick_cnt + 1) + tick_idx + 1)
for tick_idx, tick_tuple in enumerate(ticks_sorted)
for section_idx in range(section_cnt)
]))
plot_cmds = [
# using: select values for error bars (x:y:y_low:y_high)
"every %d::%d using ($2*%d+$3+1+%f):5:6:7 with errorbars pointtype %d linecolor '#%s' title '%s'" %
(
skew_cnt, # point increment
skew_idx + 1, # start point
tick_cnt + 1, # section offset
(skew_idx + 1) * (0.8 / (skew_cnt + 1)) - 0.4, # skew within [-0.4; +0.4]
get_point_type(point_map, None),
get_line_color(line_map, skew_tuple),
format_skew_title(*skew_tuple)
)
for skew_idx, skew_tuple in enumerate(skews_sorted)
]
g.plot_data(full_gdf, *plot_cmds)
# Save plot data
full_gdf.to_csv(os.path.join(out_dir, DATA_DIR, file_base + '.csv'))
with open(os.path.join(out_dir, DATA_DIR, file_base + '.gnuplot'), 'w+') as f:
f.write("\n".join(plot_cmds))
def plot_boxplot(df: pd.DataFrame, analysis_name: str, file_cols: List[str], value_cols: List[str], x_label: str, y_label: str, format_file_title: Callable[[FileTuple], str],
format_file_base: Callable[[FileTuple], str], id_vars: List[str], id_var_function: Callable[[FileTuple], str], section_var_name: str, out_dir: str, log_scale: bool = False):
"""
Plot a box plot graph
:param df: The dataframe to read the data from
:param analysis_name: A name for the analysis used in log statements
:param file_cols: Column names that define values for which separate graphs are generated
:param value_cols: Column names that define values by which the data is separated into sections
:param x_label: Label for the x-axis
:param y_label: Label for the y-axis
:param format_file_title: Function to format the title of a graph, receives a file_tuple (file_cols values)
:param format_file_base: Function to format the base name of a graph file, receives a file_tuple (file_cols values)
:param id_vars: Column names that define value by which the data is separated into sections
:param id_var_function: Function to format the grouped id_vars (sections)
:param section_var_name: Name of column that contains aggregated id_var
:param out_dir: Directory where all the output files are placed
:param log_scale: Whether or not to use a logarithmic scale
"""
plt.rcParams["figure.figsize"] = (20,15)
plt.rcParams["font.size"] = 15
df['unique_col_name']=df.apply(id_var_function, axis=1)
ids = df['unique_col_name'].unique()
for file_tuple in unique_cartesian_product(df, *file_cols):
print_file_tuple = sprint_tuple(file_cols, file_tuple)
logger.info("Generating %s timing %s", analysis_name, print_file_tuple)
gdf = filter_graph_data(df, 'unique_col_name', None, file_cols, file_tuple)
df_melted = pd.melt(gdf, id_vars=['unique_col_name'], value_vars=value_cols)
df_melted=df_melted.rename({'unique_col_name': section_var_name}, axis=1)
ax = sns.boxplot(x='value', y='variable', hue=section_var_name, data=df_melted, orient='h')
ax.set_ylabel(y_label)
ax.set_xlabel(x_label)
ax.set_title(format_file_title(*file_tuple))
if log_scale is True:
ax.set_xscale("log")
ax.grid(linestyle='-', linewidth='0.5', which='both')
plt.savefig(os.path.join(out_dir, GRAPH_DIR, f'{format_file_base(*file_tuple)}.pdf'), format='pdf')
plt.clf()
def analyze_netem_goodput(df: pd.DataFrame, out_dir: str, extra_title_col: Optional[str] = None):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series(df, out_dir,
analysis_name='NETEM_GOODPUT_%gS' % x_bucket,
file_cols=['sat', 'rate', 'queue'],
data_cols=['protocol', 'pep', 'loss'],
x_col='second',
y_col='bps',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=x_bucket,
y_div=1000,
x_label="Time (s)",
y_label="Goodput (kbps)",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_file_title=lambda sat, rate, queue:
"Goodput Evolution - %s - %.0f Mbit/s - BDP*%d" % (sat, rate, queue),
format_file_base=lambda sat, rate, queue:
"goodput_%gs_%s_r%s_q%d" % (x_bucket, sat, rate, queue),
extra_title_col=extra_title_col)
def analyze_netem_goodput_matrix(df: pd.DataFrame, out_dir: str):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series_matrix(df, out_dir,
analysis_name='NETEM_GOODPUT_%gS' % x_bucket,
file_cols=['queue'],
data_cols=['protocol', 'pep', 'loss'],
matrix_x_cols=['sat'],
matrix_y_cols=['rate'],
x_col='second',
y_col='bps',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=x_bucket,
y_div=1000,
x_label="Time (s)",
y_label="Goodput (kbps)",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_subplot_title=lambda sat, rate:
"Goodput Evolution - %s - %.0f Mbit/s" % (sat, rate),
format_file_title=lambda queue: "Goodput Evolution - BDP*%d" % queue,
format_file_base=lambda queue: "matrix_goodput_%gs_q%d" % (x_bucket, queue),
sort_matrix_x=lambda xvals: sorted(xvals, key=lambda xt: sat_key(xt[0])),
sort_matrix_y=lambda yvals: sorted(yvals, reverse=True))
def analyze_netem_cwnd_evo(df: pd.DataFrame, out_dir: str):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series(df, out_dir,
analysis_name='NETEM_CWND_EVO_%gS' % x_bucket,
file_cols=['sat', 'rate', 'queue'],
data_cols=['protocol', 'pep', 'loss'],
x_col='second',
y_col='cwnd',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=x_bucket,
y_div=1000,
x_label="Time (s)",
y_label="Congestion window (KB)",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_file_title=lambda sat, rate, queue:
"Congestion Window Evolution - %s - %.0f Mbit/s - BDP*%d" % (sat, rate, queue),
format_file_base=lambda sat, rate, queue:
"cwnd_evo_%gs_%s_r%s_q%d" % (x_bucket, sat, rate, queue))
def analyze_netem_cwnd_evo_matrix(df: pd.DataFrame, out_dir: str):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series_matrix(df, out_dir,
analysis_name='NETEM_CWND_EVO_%gS' % x_bucket,
file_cols=['queue'],
data_cols=['protocol', 'pep', 'loss'],
matrix_x_cols=['sat'],
matrix_y_cols=['rate'],
x_col='second',
y_col='cwnd',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=x_bucket,
y_div=1000,
x_label="Time (s)",
y_label="Congestion window (KB)",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_subplot_title=lambda sat, rate:
"Goodput Evolution - %s - %.0f Mbit/s" % (sat, rate),
format_file_title=lambda queue: "Congestion Window Evolution - BDP*%d" % queue,
format_file_base=lambda queue: "matrix_cwnd_evo_%gs_q%d" % (GRAPH_X_BUCKET, queue),
sort_matrix_x=lambda xvals: sorted(xvals, key=lambda xt: sat_key(xt[0])),
sort_matrix_y=lambda yvals: sorted(yvals, reverse=True))
def analyze_netem_packet_loss(df: pd.DataFrame, out_dir: str):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series(df, out_dir,
analysis_name='NETEM_PACKET_LOSS_%gS' % x_bucket,
file_cols=['sat', 'rate', 'queue', 'loss'],
data_cols=['protocol', 'pep'],
x_col='second',
y_col='packets_lost',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=GRAPH_X_BUCKET,
y_div=1,
x_label="Time (s)",
y_label="Packets lost",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_file_title=lambda sat, rate, queue, loss:
"Packet Loss - %s - %.0f Mbit/s - BDP*%d - Loss %d" % (sat, rate, queue, loss * 100),
format_file_base=lambda sat, rate, queue, loss:
"packet_loss_%gs_%s_r%s_q%d_l%e" % (x_bucket, sat, rate, queue, loss))
def analyze_netem_packet_loss_matrix(df: pd.DataFrame, out_dir: str):
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series_matrix(df, out_dir,
analysis_name='NETEM_PACKET_LOSS_%gS' % x_bucket,
file_cols=['queue'],
data_cols=['protocol', 'pep', 'loss'],
matrix_x_cols=['sat'],
matrix_y_cols=['rate'],
x_col='second',
y_col='packets_lost',
x_range=(0, GRAPH_PLOT_SECONDS),
x_bucket=x_bucket,
y_div=1,
x_label="Time (s)",
y_label="Packets lost",
point_type_indices=[2],
line_color_indices=[0, 1],
format_data_title=lambda protocol, pep, loss:
"%s%s l=%.2f%%" % (protocol.upper(), " (PEP)" if pep else "", loss * 100),
format_subplot_title=lambda sat, rate:
"Goodput Evolution - %s - %.0f Mbit/s" % (sat, rate),
format_file_title=lambda queue: "Packet Loss - BDP*%d" % queue,
format_file_base=lambda queue: "matrix_packet_loss_%gs_q%d" % (x_bucket, queue),
sort_matrix_x=lambda xvals: sorted(xvals, key=lambda xt: sat_key(xt[0])),
sort_matrix_y=lambda yvals: sorted(yvals, reverse=True))
def analyze_netem_rtt(df: pd.DataFrame, out_dir: str):
df['second'] = df['seq'] / 100.0
for x_bucket in {GRAPH_X_BUCKET, 1}:
plot_time_series(df, out_dir,
analysis_name='NETEM_RTT_%gS' % x_bucket,
file_cols=['sat', 'rate', 'queue'],
data_cols=['loss'],
x_col='second',
y_col='rtt',
x_range=(0, GRAPH_PLOT_RTT_SECONDS),
x_bucket=x_bucket,
y_div=1,
x_label="Time (s)",
y_label="RTT (ms)",
point_type_indices=[0],
line_color_indices=[0],
format_data_title=lambda loss:
"l=%.2f%%" % (loss * 100),
format_file_title=lambda sat, rate, queue: