forked from romain-jacob/triscale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriscale.py
1573 lines (1355 loc) · 59.1 KB
/
triscale.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
"""
TriScale module
Public API
network_profiling
experiment_sizing
analysis_metric
analysis_kpi
analysis_variability
"""
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default='notebook'
from helpers import convergence_test, ThompsonCI, ThompsonCI_onesided, independence_test, min_number_samples, repeatability_test
from triplots import theil_plot, autocorr_plot, ThompsonCI_plot
# ----------------------------------------------------------------------------------------------------------------------------
# NETWORK PROFILING
# ----------------------------------------------------------------------------------------------------------------------------
def network_profiling( link_quality_data,
link_quality_bounds,
name=None,
print_output=False,
verbose=False):
"""
Perform the network profiling as suggested by TriScale [1].
The function looks for seasonal components in the link quality data.
The input data must be an equally spaced time series. The function aims
to identify seosanality with periodicity of at least twice the period
of the input data (i.e., if the data period is one hour, the minimal
seasonal component that may be detected is a two-hour correlation).
This function first performs TriScale's convergence test on the link quality
data (using 95% confidence level and 5% tolerance). The time series and
its Theil-Sen [3] linear regression are plotted.
If the convergence test is passed, the function continues with TriScale's
independence test, which verify whether the network link quality data
appears i.i.d.
If both tests are passed, it indicates that there is no significant
seasonal component in the data.
Overwise, the pics in the autocorellation plot identify the seasonal
components in the link quality data.
Parameters
----------
link_quality_data : pandas DataFrame
Contains link quality data from the network where the experiment is
expected to be performed.
- Must contain a `link_quality` column.
- Must contain a `data_time` column or having a DatetimeIndex.
link_quality_bounds : list-like of len 2.
Expected extremal values for the link quality data,
used for the convergence test.
name : string, optional
Label for the plots axis.
Default : None
print_output : True/False, optional
When True, produces and displays a textual summary of the network
profiling analysis.
Default : False
verbose : True/False, optional
When true, print non-functional and intermediary outputs.
Default : False
Returns
-------
figure : plotly graphical object
The function produces and display the autocorellation plot of the
link quality data. The figure is returned to the user (e.g., to modify
the default layout).
Notes
-----
- Computing autocorrelation of a time series requires equally spaced values.
Missing values must be interpolated based on existing data to compute the
autocorrelation coefficients.
In this function, missing values are replaced by the median of all
collected values.
- The autocolleration plot shows the the 95th confidence interval for the
autocorrelation coefficient values such that the data appears i.i.d.
This confidence interval is computed as +/-1.95*sqrt(N),
where N is the number of samples [2].
- A textual output of network profiling analysis is triggered by the
`print_output` parameter. The current implementation is minimal (it only
says whether the link quality data appears to be i.i.d.).
References
----------
.. [1] Anonymous, "TriScale: A Framework Supporting Reproducible
Performance Evaluations in Networking", 2020,
https://doi.org/10.5281/zenodo.3464273
.. [2] Peter J. Brockwell, Richard A. Davis, and Stephen E. Fienberg.
"Time Series: Theory and Methods: Theory and Methods." Springer Science
& Business Media, 1991.
.. [3] Henri Theil. A Rank-Invariant Method of Linear and Polynomial
Regression Analysis. In Baldev Raj and Johan Koerts, editors,
Henri Theil’s Contributions to Economics and Econometrics: Econometric
Theory and Methodology, Advanced Studies in Theoretical and Applied
Econometrics, pages 345–381. Springer Netherlands, Dordrecht, 1992.
"""
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO network_profiling \n'
todo += '# ---------------------------------------------------------------- \n'
todo += '- verbose display (clearly) advising people what they should do\n'
todo += 'i.e., how long should be the time span of an experiment\n'
todo += '- input check\n'
todo += '- handle the intermediary outputs\n'
todo += '- check and format the final output: stationary + independence\n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
convergence = { 'expected': True,
'confidence': 95, # in %
'tolerance': 5, # in %
}
# network_name = 'FlockLab - DPP-cc430'
profiling_output = ''
profiling_output += '# ---------------------------------------------------------------- \n'
profiling_output += '# TriScale report - Network profiling\n'
profiling_output += '# ---------------------------------------------------------------- \n'
# profiling_output += '\nNetwork \t%s\n' % (network_name)
##
# Checking the inputs
##
if isinstance(link_quality_data, str):
try:
link_quality_data = pd.read_csv( link_quality_data,
delimiter=',',
names=['date_time', 'link_quality'],
header=0,
usecols=[0,1], # consider only the first two columns
)
except FileNotFoundError:
print(repr(link_quality_data) + " not found")
return None, None
elif isinstance(link_quality_data, pd.DataFrame):
# Data must be a dataframe with (at least) two columns (can also be index)
# - link_quality
# - date_time
link_quality_data.reset_index(inplace=True)
try:
df = link_quality_data[['date_time', 'link_quality']]
except KeyError:
raise ValueError("Input DataFrame must contain columns names 'date_time' and 'link_quality'.")
else:
raise ValueError("Wrong input type. Expect a string or a DataFrame, got "+repr(link_quality_data)+".")
# Parse dates
if 'date_time' in link_quality_data.columns:
link_quality_data['date_time'] = pd.to_datetime(
link_quality_data['date_time'],
utc=True)
link_quality_data.set_index('date_time')
# Make sure the DataFrame is sorted
link_quality_data.sort_index(inplace=True)
profiling_output += '\nProfiling time span\n'
profiling_output += 'from \t\t%s\n' % link_quality_data.index[0]
profiling_output += 'to \t\t%s\n' % link_quality_data.index[-1]
profiling_output += '\nProfiling granularity\n'
profiling_output += '\t\t%s\n' % ( link_quality_data.index[1]
- link_quality_data.index[0] )
profiling_output += '\n# ---------------------------------------------------------------- \n'
##
# Convergence test
##
# Compute the trend of link quality data
results = convergence_test( link_quality_data.index,
link_quality_data.link_quality.values,
link_quality_bounds,
convergence['confidence'],
convergence['tolerance'])
# Plot the time series and its trend
default_layout={'xaxis' : {'title':None},
'yaxis' : {'title':name}}
datetime = np.array(link_quality_data.index, dtype=object)
fig_theil = theil_plot( link_quality_data.link_quality.values,
x=datetime,
convergence_data=results,
layout=default_layout)
##
# Stationarity test
##
# Replace missing samples with the series median
# -> We need continuous data for autocorrelation
data = link_quality_data.link_quality.values
data[np.isnan(data)] = np.nanmedian(data)
stationary = independence_test(data)
if stationary:
profiling_output += '\nNetwork link quality appears I.I.D.'
profiling_output += '(95%% confidence)\n'
else:
profiling_output += '\nNetwork link quality does NOT appears I.D.D. !\n\n'
# profiling_output += '\nNetwork link quality does NOT appears I.D.D. !\nSearching for a suitable time interval...\n\n'
# Plot the autocorrelation
fig_autocorr = autocorr_plot(data, show=False)
# # Search for a suitable test window
# window_size = 1
# while not stationary:
# window_size += 1
# data_subsampled = [np.nanmean(np.array(data[i:i+window_size])) for i in np.arange(0, len(data), window_size)]
# stationary = independence_test(data_subsampled)
# profiling_output += 'Window size: %g\tStationary: %i\n' % (window_size, stationary)
# # plot_autocorr(data_subsampled)
#
# # Compute the corresponding time span
# time_span = link_quality_data.index[window_size] - link_quality_data.index[0]
#
# profiling_output += '\n\nWith a confidence of 95%\n'
# profiling_output += 'network link quality appears stationary over a \n'
# profiling_output += 'time span of'
# profiling_output += '\t%s\n' % (time_span)
# profiling_output += '\n# ---------------------------------------------------------------- \n'
if print_output:
print(profiling_output)
return fig_theil, fig_autocorr
# ----------------------------------------------------------------------------------------------------------------------------
# EXPERIMENT SIZING
# ----------------------------------------------------------------------------------------------------------------------------
def experiment_sizing(percentile,
confidence,
robustness=0,
CI_class='one-sided',
verbose=False):
"""
Experiment sizing as suggested by TriScale [1].
The function returns the minimal number of data samples required to
computes a one-sided and a two-sided CI for `percentile`,
with `confidence` confidence level and with `robustness` data samples
excluded.
Computation based on [2].
Parameters
----------
percentile : float
Percentile to estimate, must be between 0 and 100
confidence : float
Confidence level of the estimation, must be between 0 and 100
robustness : positive integer, optional
Number to samples to exclude from the estimation
CI_class : 'one-sided' or 'two-sided', optional
The class of confidence interval: one- or two-sided.
Used only for textual output, displayed when `verbose` is True.
Default : 'one-sided'
verbose : True/False, optional
When true, print non-functional and intermediary outputs.
Default : False
Returns
-------
N_one : integer
Minimal number of samples for a one-sided CI for `percentile`,
with `confidence` confidence level, and with `robustness` data samples
excluded.
N_two : integer
Minimal number of samples for a two-sided CI for `percentile`,
with `confidence` confidence level, and with `robustness` data samples
excluded.
References
----------
.. [1] Anonymous, "TriScale: A Framework Supporting Reproducible
Performance Evaluations in Networking", 2020,
https://doi.org/10.5281/zenodo.3464273
.. [2] William R. Thompson, "On Confidence Ranges for the Median and Other
Expectation Distributions for Populations of Unknown Distribution Form",
The Annals of Mathematical Statistics, 7(3):122–128, 1936.
"""
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO experiment_design\n'
todo += '# ---------------------------------------------------------------- \n'
todo += '# - adjust the verbose output to say whether upper or lower bound is considered\n'
todo += '# - add an CI-side parameter to force computation of lower bound for >median percentiles\n'
todo += '# ---------------------------------------------------------------- \n'
# if verbose:
# print('%s' % todo)
##
# Checking the inputs
##
if confidence >= 100 or confidence <= 0:
raise ValueError("Invalid confidence: "+repr(confidence)+". Provide a real number strictly between 0 and 100.")
if percentile >= 100 or percentile <= 0:
raise ValueError("Invalid percentile: "+repr(percentile)+". Provide a real number strictly between 0 and 100.")
if not isinstance(robustness, int):
raise ValueError("Invalid robustness: "+repr(robustness)+". Provide a positive integer.")
if robustness < 0:
raise ValueError("Invalid robustness: "+repr(robustness)+". Provide a positive integer.")
if not (CI_class == 'one-sided' or CI_class == 'two-sided'):
raise ValueError("""Invalid CI_class: %s
Valid 'CI_class' values: 'one-sided' or 'two-sided'
""" % (repr(CI_class)) )
# Work with lower-percentiles
if percentile > 50:
wk_perc = 100-percentile
else:
wk_perc = percentile
# Compute the minimal number
# necessary to estimate the given percentile ant the given confidence
# (possibly with a given number of data sample to omit = robustness)
N_one, N_two = min_number_samples(wk_perc,confidence,robustness)
if verbose:
out = ''
if CI_class == 'one-sided':
out += ('A one-sided bound of the \t%i-th percentile\n'
% (percentile))
else:
out += ('A two-sided bound of the \t%i-th percentile\n'
% (percentile))
out += ('with a confidence level of\t%i %% \n'
% (confidence))
if CI_class == 'one-sided':
out += 'requires a minimum of \t\t%i samples\n' % (N_one)
else:
out += 'requires a minimum of \t\t%i samples\n' % (N_two)
if robustness != 0:
out += 'with the worst \t\t\t%i run(s) excluded\n' % (robustness)
print(out)
return N_one, N_two
# ----------------------------------------------------------------------------------------------------------------------------
# ANALYSIS_METRIC
# ----------------------------------------------------------------------------------------------------------------------------
def analysis_metric( data,
metric,
convergence=None,
plot_out_name=None,
showplot=True,
custom_layout=None,
verbose=False):
"""
Computation of metrics as suggested by TriScale [1].
Parameters
----------
data : string or pandas DataFrame
The input data is a two-dimentional series used for the computation of
the metric: one control variate (x), one independent variate (y).
- When a string is passed, `data` is expected to be a name of a csv file
(comma separated) with `x` data in the first column and `y` data in the
second column.
- When a pandas DataFrame is passed, `data` must contain (at least)
columns named `x` and `y`.
metric : dictionary
TriScale metric dictionary.
- "measure" key is compulsory.
The corresponding value can be a float between 0 and 100, or a string.
When a float, it corresponds to the percentile used as performance
measure for that metric.
When a string, it is the name of the metric to compute. Current supported are:
+ 'mean': arithmetic mean
+ 'minimum' : minimum
+ 'maximum' : maximum
Optional keys:
- "bounds" : list-like of len 2.
Expected extremal values for the measure, used for the convergence test.
- "name"/"units" : strings, for plot layout only
convergence : dictionary or None
TriScale convergence dictionary
- "expected" : True/False
Triggers the computation of TriScale convergence test when set to True.
- "confidence" : float, optional
Confidence level for TriScale convergence test.
Float between 0 and 100, default to 95.
- "tolerance" : float, optional
Tolerance for TriScale convergence test.
Float between 0 and 100, default to 5.
plot : True/False, optional
When true, generate a plot of the input data and convergence data
(if any).
Default : False
plot_out_name : string or None, optional
When a string, triggers saving of the plot under `plot_out_name` as
file name (if `plot` is True)
Default : None
showplot : True/False, optional
When true, display the generated plot (if `plot` is True).
Default : True
custom_layout : dictionary or None, optional
Plotly layout dictionary to edit the default layout of the
generated plot.
Default : None
verbose : True/False, optional
When true, print non-functional and intermediary outputs.
Default : False
Returns
-------
convergence test result : True/False
The outcome of the convergence test, when
`convergence["expected"] == True`
Always True otherwise.
measure :
The computed metric measure, interpolated to `nearest`.
figure : plotly graphical object or None
The generated plot when `plot == True`
References
----------
.. [1] Anonymous, "TriScale: A Framework Supporting Reproducible
Performance Evaluations in Networking", 2020,
https://doi.org/10.5281/zenodo.3464273
"""
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO analysis_metrics \n'
todo += '# ---------------------------------------------------------------- \n'
todo += '\n'
todo += '- Adapt to pake a PosixPath as input\n'
todo += '- return an object?\n'
todo += '- check for crazy values in the input dictionaries\n'
todo += '- change plot into show_plot\n'
# todo += '- \n'
# todo += '- \n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
##
# Checking the inputs
##
# Parse data
if isinstance(data, str):
try:
df = pd.read_csv( data,
delimiter=',',
names=['x', 'y'],
header=0,
usecols=[0,1], # consider only the first two columns
)
except FileNotFoundError:
print(repr(data) + " not found")
return False, np.nan, None
elif isinstance(data, pd.DataFrame):
try:
df = data[['x', 'y']]
except KeyError:
raise ValueError("Input DataFrame must contain columns names 'x' and 'y'.")
else:
raise ValueError("Wrong input type. Expect a string or a DataFrame, got "+repr(data)+".")
# Verify that the csv file is not empty (at least some 'y' data is in there)
df.dropna(inplace=True)
if len(df.index) < 2:
if verbose:
print("%s\n-> Input data has only %d data points (min 2 required)\n"
% ( repr(data), len(df.index) ))
return False, np.nan, None
# Initialize convenience variables
samples_x = df.x.values
samples_y = df.y.values
metric_y = []
metric_x = []
# Metric
if 'bounds' not in metric:
metric['bounds'] = [df.y.min(), df.y.max()]
if (('name' not in metric) or
(metric['name'] is None)):
metric['name'] = None
metric_label = ''
else:
metric_label = metric['name']
if 'unit' in metric:
metric_label += ' [' + metric['unit'] + ']'
# Convergence
if convergence is not None and convergence['expected'] == True:
# Convergence test should run
run_convergence_test = True
# Check the confidence and tolerance values
if 'confidence' not in convergence:
# Default to 95% confidence
convergence['confidence'] = 95
if 'tolerance' not in convergence:
# Default to 5% tolerance
convergence['tolerance'] = 5
else:
run_convergence_test = False
##
# Convergence test
##
if run_convergence_test:
# Compute the metric series
fixed_window=True
if fixed_window:
## Version with fixed window size
nb_chuncks = min(int(len(samples_y)/2)+1, 100)
chunck_len = int(len(samples_y)/2)
step = chunck_len/(nb_chuncks-1)
for i in range(0,nb_chuncks):
start_index = int(i*step)
stop_index = start_index+chunck_len
# Show the sample in the middle of the sliding window
metric_x.append(samples_x[int(start_index+chunck_len/2)])
if isinstance(metric['measure'], str):
if metric['measure'] == 'mean':
metric_y.append(np.mean(samples_y[start_index:stop_index]))
elif metric['measure'] == 'minimum':
metric_y.append(np.amin(samples_y[start_index:stop_index]))
elif metric['measure'] == 'maximum':
metric_y.append(np.amax(samples_y[start_index:stop_index]))
else:
raise ValueError('Unsupported measure')
else:
metric_y.append(np.percentile( samples_y[start_index:stop_index],
metric['measure'],
interpolation='midpoint' ))
else:
## Version with increasing window size
if len(samples_y) > 200:
nb_chuncks = 200
else:
nb_chuncks = len(samples_y)
for chuncks in range(0,int(nb_chuncks/2)):
chunck_x = chuncks*2+1
chunck_len = int((int(nb_chuncks/2+chuncks)*len(samples_y)/nb_chuncks))
if isinstance(metric['measure'], str):
if metric['measure'] == 'mean':
metric_y.append(np.mean(samples_y[:chunck_len]))
else:
raise ValueError('Unsupported measure')
else:
metric_y.append(np.percentile( samples_y[:chunck_len],
metric['measure'],
interpolation='midpoint' ))
metric_x.append(samples_x[int(chunck_x*len(samples_y)/nb_chuncks)])
# print(chunck_x, metric_x)
# Convergence test
bounds_equal = metric['bounds'][0] == metric['bounds'][1]
data_equal = np.all(np.array(metric_y) == metric_y[0])
if data_equal and bounds_equal:
#print("All values same are not tackled. Faking passed metric test")
#print(np.array(metric_y))
results = None
has_converged = True
#results[0] = True
else:
#print(np.array(metric_y))
results = convergence_test(np.array(metric_x),
np.array(metric_y),
metric['bounds'],
convergence['confidence'],
convergence['tolerance'],
verbose=verbose)
if results[0]:
has_converged = True
else:
has_converged = False
# Produce the output string
if verbose:
if has_converged:
flag_convergence1 = '[ PASSING ]'
flag_convergence2 = ''
preprocessing_warning = '\n'
else:
flag_convergence1 = '[ FAILED ]'
flag_convergence2 = 'NOT '
preprocessing_warning = '\n[ WARNING ] These data should not be used to estimate \nthe long-term performance of the system under test!\n'
preprocessing_output = ''
preprocessing_output += '%s\n' % flag_convergence1
preprocessing_output += 'With a confidence level of \t%g%%\n' % (convergence['confidence'])
preprocessing_output += 'given a tolerance of \t\t%g%%\n' % (convergence['tolerance'])
preprocessing_output += 'Run has %sconverged.\n' % flag_convergence2
preprocessing_output += '%s' % preprocessing_warning
print(preprocessing_output)
else:
results = None
##
# Plot
##
if showplot or plot_out_name is not None:
default_layout={'title' : ('%s' % metric_label),
'xaxis' : {'title':None},
'yaxis' : {'title':metric_label}}
if custom_layout is not None:
default_layout.update(custom_layout)
figure = theil_plot( samples_y,
x=samples_x,
metric_data=[metric_x, metric_y],
convergence_data=results,
layout=default_layout,
out_name=plot_out_name)
if showplot:
figure.show()
else:
figure = None
##
# Return the run's measure
##
if run_convergence_test:
# Test failed
if not has_converged:
return False, np.nan, figure
# Test passed
else:
# return the median of the computed metric data
return True, np.percentile(metric_y, 50 , interpolation='nearest'), figure
else:
if isinstance(metric['measure'], str):
if metric['measure'] == 'mean':
measure = np.mean(df.y.values)
elif metric['measure'] == 'minimum':
measure = np.amin(df.y.values)
elif metric['measure'] == 'maximum':
measure = np.amax(df.y.values)
else:
raise ValueError('Unsupported measure')
else:
measure = np.percentile(df.y.values, metric['measure'] , interpolation='nearest')
return True, measure, figure
# ----------------------------------------------------------------------------------------------------------------------------
# ANALYSIS_KPI
# ----------------------------------------------------------------------------------------------------------------------------
def analysis_kpi(data,
KPI,
plots=None,
showplot=False,
plot_out_name=None,
custom_layout=None,
verbose=False):
"""
Computation of KPIs as suggested by TriScale [1].
Parameters
----------
data : 1-d np.array or list
The metric data for a series of run.
KPI : dictionary
TriScale KPI dictionary.
Compulsory keys:
- "percentile" : float
Percentile to estimate, float between 0 and 100
- "confidence" : float
Confidence level for the percentile estimation,
float between 50 and 100
- "bounds" : list-like of len 2.
Expected extremal values for the measure,
used for the stationarity test.
Optional keys:
- "name"/"units" : strings, for plot layout only
- "bound" : 'upper' or 'lower'
Set whether the KPI is an upper- or lower-bound of the percentile.
Inferred based on the percentile value:
* 'lower' if `percentile < 50`
* 'upper' if `percentile > 50`
Must be defined by the user of `percentile == 50`
to_plot : list of strings or None (default), optional
List of plots to produce. Valid plot names are
'autocorr', 'horizontal', 'vertical'
plot_out_name : string or None, optional
File name to save the 'horizontal' or 'vertical' plot.
Default : None
custom_layout : dictionary or None, optional
Plotly layout dictionary to edit the default layout of the
generated plot.
Default : None
verbose : True/False, optional
When true, print non-functional and intermediary outputs.
Default : False
Returns
-------
independence test result : True/False
The outcome of the independence test
KPI_out : float or NaN
NaN if there are not enough data points to compute the KPI,
computed KPI value otherwise.
References
----------
.. [1] Anonymous, "TriScale: A Framework Supporting Reproducible
Performance Evaluations in Networking", 2020,
https://doi.org/10.5281/zenodo.3464273
"""
todo = ''
todo += '# ---------------------------------------------------------------- \n'
todo += '# TODO analysis_kpi \n'
todo += '# ---------------------------------------------------------------- \n'
todo += '# - replance "bound" by "side" \n'
todo += '# - update to use the newest Thompson CI function\n'
todo += '# - return an object?\n'
todo += '# - Improve the plot output (layout and doc!) \n'
todo += '# - Rename plot "horizontal" -> CI (remove the vertical one)\n'
# todo += '# - \n'
todo += '# ---------------------------------------------------------------- \n'
if verbose:
print('%s' % todo)
##
# Input checks
##
# Define as np array
data = np.array(data)
# Remove nan's
data = data[~np.isnan(data)]
# Force one-sided CI for the KPI
if 'class' in KPI:
if KPI['class'] != 'one-sided':
KPI['class'] = 'one-sided'
raise ValueError("TriScale KPIs can only have 'class' 'one-sided'.")
else:
KPI['class'] = 'one-sided'
if 'bound' not in KPI:
if KPI['percentile'] > 50:
KPI['bound'] = 'upper'
elif KPI['percentile'] < 50:
KPI['bound'] = 'lower'
else:
if KPI['percentile'] == 50:
raise ValueError("If the median is used as percentile, \n"
"\t\tspecify the desired 'bound': 'lower' of 'upper'")
if 'bounds' not in KPI:
KPI['bounds'] = [data.min(), data.max()]
# For now, we assume the inputs are correct...
output_log = ''
sorted_data = np.sort(data)
##
# Independence test
##
if len(data) < 2:
weak_stationary = False
if len(data) == 0:
print("Invalid metric data (no data points)")
else:
print("Invalid metric data (only one data point)")
return weak_stationary, np.nan
# Check if sufficient data
## Work with lower-percentiles (Fetched from above)
#if KPI['percentile'] > 50:
# wk_perc = 100 - KPI['percentile']
#else:
# wk_perc = KPI['percentile']
#N_one, N_two = min_number_samples(wk_perc, KPI['confidence'], robustness=0)
#if len(data) < N_one:
# output_log += ("Too few data points. Have %d, need %d" % (len(data), N_one))
# print("asd")
# print("asd")
# print("asd")
# print("asd")
# return False, np.nan
# Step 1: weak stationarity
bounds_equal = KPI['bounds'][0] == KPI['bounds'][1]
if bounds_equal:
print("All run values same are not tackled. Faking passed KPI test")
print(data)
weak_stationary = True
# TODO trend and tol is not set
else:
weak_stationary, trend, tol = convergence_test(np.arange(len(data)),
np.array(data),
KPI['bounds'],
50,
10)
# Step 2: independence
stationary = independence_test(data)
# print(weak_stationary,stationary)
# if not weak_stationary:
# print(weak_stationary,stationary)
# figure = theil_plot( np.array(data),
# convergence_data=[weak_stationary, trend, tol])
# figure.show()
#
# autocorr_plot( data )
#
# wait = input("PRESS ENTER TO CONTINUE.")
stationary_res = stationary
stationary = (stationary and weak_stationary)
if stationary:
output_log += ('Data appears i.i.d. (95% confidence)\n')
else:
# Check whether the data points have all the same value
# -> This leads the stationarity test to fail
# -> TriScale considers this as valid, but raises a warning.
if sorted_data[0] == sorted_data[-1]:
stationary = True
output_log += ('All data points are the same. Considered stationary.\n')
output_log += ('(but maybe you want to double-check that the data is really constant...)\n')
else:
output_log += ('Data appears NOT I.D.D. !\n')
if not weak_stationary:
output_log += ('Failed convergence-test! ')
output_log += ('Bounds: ' + str(KPI['bounds']) + ', ')
output_log += ('Trend: ' + str(trend) + ', tolerance: ' + str(tol) + '\n')
if not stationary_res:
output_log += ('Failed independence-test!\n')
output_log += ('Analysis continues but results are not trustworthy...')
if verbose:
print(output_log)
##
# Compute the KPI
##
LB,UB = ThompsonCI(len(data),
KPI['percentile'],
KPI['confidence'],
KPI['class'],
verbose)
if KPI['bound'] == 'lower':
KPI_CI = LB
else:
KPI_CI = UB
##
# Plots
##
layout = go.Layout(width=500)
if 'name' in KPI:
layout.update(title=KPI['name'])
if not np.isnan(KPI_CI) and \
plots is not None and \
(showplot is not None or plot_out_name is not None):
if 'series' in plots:
figure = theil_plot(
np.array(data),
convergence_data=[weak_stationary, trend, tol],
)
if showplot:
figure.show()
if 'autocorr' in plots:
autocorr_plot(data, show=showplot)
# KPI annotation
note_text = "KPI: %2.2f" % sorted_data[KPI_CI]
if 'unit' in KPI:
note_text += ' ' + KPI['unit']
note = go.layout.Annotation(
x=0.5,
y=0.15,
xref="paper",
yref="paper",
text=note_text,
showarrow=False,
)
layout['annotations'] = [note]
if custom_layout is not None:
layout.update(custom_layout)
if 'horizontal' in plots:
figure = ThompsonCI_plot( data, [LB,UB], KPI['bound'], 'horizontal', layout, out_name=plot_out_name)
if showplot:
figure.show()
if 'vertical' in plots:
figure = ThompsonCI_plot( data, [LB,UB], KPI['bound'], 'vertical', layout, out_name=plot_out_name)
if showplot:
figure.show()
##
# outputs
##
if np.isnan(KPI_CI):
return stationary, np.nan
else:
return stationary, sorted_data[KPI_CI]
# ----------------------------------------------------------------------------------------------------------------------------
# ANALYSIS_VARIABILITY
# ----------------------------------------------------------------------------------------------------------------------------
def analysis_variability(data,
score,
to_plot=None,
plot_out_name=None,
custom_layout=None,
verbose=False):
"""
Computation of variability scores as suggested by TriScale [1].
Parameters
----------
data : 1-d np.array or list
The metric data for a series of run.
score : dictionary
TriScale score dictionary.
Compulsory keys:
- "percentile" : float
Percentile to estimate, float between 0 and 100
- "confidence" : float
Confidence level for the percentile estimation,
float between 50 and 100
- "bounds" : list-like of len 2.
Expected extremal values for the measure,
used for the stationarity test.
Optional keys:
- "name"/"units" : strings, for plot layout only
to_plot : list of strings or None (default), optional
List of plots to produce. Valid plot names are
'autocorr', 'horizontal', 'vertical'
plot_out_name : string or None, optional
File name to save the 'horizontal' or 'vertical' plot.
Default : None
custom_layout : dictionary or None, optional
Plotly layout dictionary to edit the default layout of the
generated plot.
Default : None
verbose : True/False, optional
When true, print non-functional and intermediary outputs.
Default : False
Returns
-------
independence test result : True/False
The outcome of the independence test
score lower-bound : float
Lower-bound of the CI defining the variability score
score upper-bound : float
Upper-bound of the CI defining the variability score
score : float
Variability score (absolute)
relative score : float
Variability score (relative) : score / mean(upper-bound, lower-bound)
References
----------
.. [1] Anonymous, "TriScale: A Framework Supporting Reproducible
Performance Evaluations in Networking", 2020,