-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
executable file
·1537 lines (1429 loc) · 61.1 KB
/
plot.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
#!/usr/bin/python3
# deps install
# pip3 install matplotlib
# import os
import csv
import argparse
import numpy as np
import matplotlib.pyplot as plt
import math
# import matplotlib
import sys
import collections
# from Utils.Cd import Cd
from Utils.Terminal import Terminal as term
########################################################################################################################
# Utils
########################################################################################################################
def signedlog(values):
"""
:param values:
:return:
"""
return [i / abs(i) * math.log(abs(i)) for i in values]
def prepare_aic_bic_csv(list_aib_bic_files):
"""
:param list_aib_bic_files:
:return:
"""
for tab_file in list_aib_bic_files:
table2csv(tab_file)
def table2csv(filename):
"""
Convert a ASCII table to CSV format
:param filename:
:return:
"""
cmd = "cat {0} |sed 's/\(^|\)\|\(^+\(-\|+\)*\)\|\(|$\)//g' |sed 's/Function/#Function/g' |sed 's/|/,/g' |sed 's/[[:blank:]]//g' |sed '/^$/d' | awk 'BEGIN{{print(\"# AIC and BIC values\")}}{{print $0}}' > {0}.csv"
cmd = cmd.format(filename)
term.command(cmd=cmd, color="green")
def load_csv(datafile=''):
"""
Load float CSV file into a matrix.
mtr_float = load_csv(datafile='file.csv')
:param datafile: CSV file to be loaded
:return: matrix with the CSV file data
"""
try:
with open(datafile) as f:
lines = (line for line in f if not line.startswith('#'))
csv_matrix = np.loadtxt(lines, delimiter=',')
return csv_matrix
except:
term.print_color(color="red", data="File {" + datafile + "} not found.")
sys.exit("File not found")
def load_csv_str(datafile=''):
"""
Load a String CSV file into a matrix
:param datafile:
:return: string matrix with CSV data
mtr_str = load_csv_str(datafile='file.csv')
"""
ifile = ""
try:
ifile = open(datafile, "rU")
except:
term.print_color(color="red", data="File {" + datafile + "} not found.")
sys.exit("File not found")
reader = csv.reader(ifile, delimiter=",")
rownum = 0
a = []
for row in reader:
if len(row) == 0 or row[0][0] == '#':
continue
for index in range(0, len(row) - 1):
row[index] = row[index].strip()
a.append(row)
rownum += 1
ifile.close()
return a
def column(matrix, i):
"""
Returns a column of a two dimensional matrix
mtr_col = column(mtr, 2):
:param matrix: matrix
:param i: column index
:return: vector
"""
return [row[i] for row in matrix]
def order_matrix(mtr, n_column):
"""
Order the matrix according to the column n
m_ordered = order_matrix(m, 1)
:param mtr:
:param n_column:
:return:
"""
mtr = sorted(mtr, key=lambda mtr: float(mtr[n_column]))
return mtr
def test_order_matrix():
"""
#
:return:
"""
m = [['abacaxi', '3', '5', '6', '7', '7'], ['banana', '0', '4', '5', '6', '7'],
['caqui', '1', '3', '4', '5', '6'], ['damasco', '7', '14', '15', '16', '17'],
['damasco', '7', '14', '15', '16', '17'], ['caqui', '7', '14', '15', '16', '17'],
['figo', '2', '99', '98', '97', '96'], ['goiaba', '9', '10', '11', '12', '13']]
m_ordered = order_matrix(m, 1)
print(m_ordered)
def order_matrix_str(mtr, n_column):
"""
Order the matrix according to the column n
m_ordered = order_matrix(m, 1)
:param mtr:
:param n_column:
:return:
"""
col_str = sorted(column(mtr, n_column))
mtr_out = []
for i in range(0, len(col_str)):
for j in range(0, len(col_str)):
if mtr[j][n_column] == col_str[i]:
mtr_out.append(mtr[j])
return mtr_out
def test_order_matrix_str():
"""
#
:return:
"""
mtr = [['pera', '1', '2', '3'], ['uva', '3', '4', '5'],
['abacaxi', '7', '6', '5'], ['banana', 'd', 'f', 'g']]
mtr = order_matrix_str(mtr, 0)
print(mtr)
def get_mtr_position(mtr, model):
"""
Return the position of the model
:param mtr:
:param model:
:return:
"""
pos = -1
for i in range(0, len(mtr)):
if model == mtr[i][0]:
pos = i
break
return pos
def test_get_mtr_position():
"""
#
:return:
"""
m = [['banana', '0', '4', '5', '6', '7'],
['caqui', '1', '3', '4', '5', '6'],
['figo', '2', '99', '98', '97', '96'],
['abacaxi', '3', '5', '6', '7', '7'],
['damasco', '7', '14', '15', '16', '17'],
['goiaba', '9', '10', '11', '12', '13']]
pos = get_mtr_position(m, 'damasco')
print('pos=' + str(pos))
def calc_relative_position_rank_diff(mtr_costfunction, mtr_aicbic):
"""
#
:param mtr_costfunction:
:param mtr_aicbic:
:return:
"""
mtr_costfunction = order_matrix(mtr_costfunction, 1)
mtr_aicbic = order_matrix(mtr_aicbic, 1)
# print(" Cost Function=" + str(mtr_costfunction))
# print(" Aic/BIC="+ str(mtr_aicbic))
vet_relative_rank_diff = []
for i in range(0, len(mtr_costfunction)):
model = mtr_costfunction[i][0]
pos_costfunction = get_mtr_position(mtr_costfunction, model)
pos_aicbic = get_mtr_position(mtr_aicbic, model)
vet_relative_rank_diff.append(pos_costfunction - pos_aicbic)
# print("model:"+model + ", pos_costfunction:"+str(pos_costfunction)+ ", pos_aicbic:"+str(pos_aicbic))
return vet_relative_rank_diff
def test_calc_relative_position_rank_diff():
"""
#
:return:
"""
# banana, caqui, figo, abacaxi, damasco, goiaba
m1 = [['banana', '0', '4', '5', '6', '7'],
['caqui', '1', '3', '4', '5', '6'],
['figo', '2', '99', '98', '97', '96'],
['abacaxi', '3', '5', '6', '7', '7'],
['damasco', '7', '14', '15', '16', '17'],
['goiaba', '9', '10', '11', '12', '13']]
# banana, figo, caqui, goiaba, abacaxi, damasco
m2 = [['figo', '0', '4', '5', '6', '7'], ['caqui', '3', '3', '4', '5', '6'],
['banana', '-2', '99', '98', '97', '96'],
['abacaxi', '5', '5', '6', '7', '7'],
['damasco', '7', '14', '15', '16', '17'],
['goiaba', '4', '10', '11', '12', '13']]
# expected : 0, -1, 1, -1, -1, 2
vet_relative_rank_diff = calc_relative_position_rank_diff(m1, m2)
print(vet_relative_rank_diff)
def errorbar_helper(ax, xdata, ydata, yerror, param_dict, legend=True):
"""
#
:param ax: AxesSubplot object
:param xdata: x data
:param ydata: y data
:param yerror: vertical errorbar
:param param_dict: list of plot parameters
:param legend:
:return:
"""
out = ax.errorbar(xdata, ydata, yerror, **param_dict)
if legend: ax.legend()
return out
def saver_helper(figure_object, file_name="default"):
"""
Helper for saving figure in many formats
:param figure_object: object fig
:param file_name: file name to be saved
:return: void
"""
figure_object.savefig(fname=file_name + '.pdf')
# figure_object.savefig(fname=file_name+'.svg')
figure_object.savefig(fname=file_name + '.png')
figure_object.savefig(fname=file_name + '.eps')
def plt_free():
"""
"""
plt.cla()
plt.clf()
plt.close()
def print_info(title="title", location="path-file"):
"""
:param title:
:param location:
:return:
"""
print("Plotting `{0}` > `{1}`".format(title, location))
########################################################################################################################
# Plot functions
########################################################################################################################
def plot_cdf_fitting(plot_dir, fitting_data, original_datafile, plot_title, plot_file):
"""
#
:param plot_dir:
:param fitting_data:
:param original_datafile:
:param plot_title:
:param plot_file:
:return:
"""
# load data
original_data = load_csv(datafile=plot_dir + original_datafile)
fitting_data = load_csv(datafile=plot_dir + fitting_data)
ox = column(original_data, 0)
oy = column(original_data, 1)
fx = column(fitting_data, 0)
fy = column(fitting_data, 1)
olabel = "empirical"
flabel = "approximation"
xlabel = "Inter packet time (s)"
ylabel = "CDF function"
# plotting
print_info(title=plot_file, location=plot_dir)
fig1, ax1 = plt.subplots()
ax1.plot(ox, oy, 'r-', label=olabel, linewidth=2)
ax1.plot(fx, fy, '-.', color="darkblue", label=flabel, linewidth=2.5)
ax1.legend(loc='lower right')
# ax1.set_aspect(aspect=1.50)
plt.xlim([-0.1, 10])
plt.ylim([0, 1.01])
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid(color='black', linestyle=':')
plt.title(plot_title)
saver_helper(fig1, file_name=plot_dir + "Linear - " + plot_file)
fig2, ax2 = plt.subplots()
ax2.plot(ox, oy, 'r-', label=olabel, linewidth=2)
ax2.plot(fx, fy, '-.', color="darkblue", label=flabel, linewidth=2.5)
ax2.legend(loc='upper left')
# ax2.set_aspect(aspect=1.50)
plt.semilogx()
plt.ylim([0, 1.01])
plt.grid(color='black', linestyle=':')
plt.title(plot_title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
saver_helper(fig2, file_name=plot_dir + "Log - " + plot_file)
plt_free()
def plot_linear_regression(plot_dir, datafile, plot_title, plot_file):
"""
#
:param plot_dir:
:param datafile:
:param plot_title:
:param plot_file:
:return:
"""
# load data
original_data = load_csv(datafile=plot_dir + datafile)
fitting_data = load_csv(datafile=plot_dir + datafile)
lx = column(original_data, 0)
ly = column(original_data, 1)
ax = column(fitting_data, 2)
ay = column(fitting_data, 3)
llabel = "linearized data"
alabel = "linear approximation"
xlabel = "interPacketTime (s)"
ylabel = "F(interPacketTime)"
# plotting
print_info(title=datafile, location=plot_dir)
fig1, ax1 = plt.subplots()
ax1.plot(lx, ly, 'x', color="darkblue", label=llabel, linewidth=3)
ax1.plot(ax, ay, 'r-', label=alabel, linewidth=3)
ax1.legend(loc='best')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid(color='black', linestyle=':')
plt.title(plot_title)
saver_helper(fig1, file_name=plot_dir + plot_file)
plt_free()
def plot_cost_history(plot_dir, datafile, plot_title, plot_file):
"""
#
:param plot_dir:
:param datafile:
:param plot_title:
:param plot_file:
:return:
"""
# load data
original_data = load_csv(datafile=plot_dir + datafile)
x = column(original_data, 0)
y = column(original_data, 1)
xlabel = "iterations"
ylabel = "Cost J(iterations)"
# plotting
print_info(title=plot_file, location=plot_dir)
fig1, ax1 = plt.subplots()
ax1.plot(x, y, 'g-', linewidth=2)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid(color='black', linestyle=':')
plt.title(plot_title)
saver_helper(fig1, file_name=plot_dir + plot_file)
plt_free()
def qqplot(plot_dir, datafile, plot_title, plot_file):
"""
#
:param plot_dir:
:param datafile:
:param plot_title:
:param plot_file:
:return:
"""
# load data
original_data = load_csv(datafile=plot_dir + datafile)
fitting_data = load_csv(datafile=plot_dir + datafile)
lx = column(original_data, 0)
ly = column(original_data, 1)
ax = column(fitting_data, 2)
ay = column(fitting_data, 3)
llabel = "QQplot"
alabel = "linear"
xlabel = "estimated"
ylabel = "samples"
# plotting
print_info(title=datafile, location=plot_dir)
fig1, ax1 = plt.subplots()
ax1.plot(lx, ly, 'o', color="darkblue", label=llabel, linewidth=3)
ax1.plot(ax, ay, 'r-', label=alabel, linewidth=2)
ax1.legend(loc='best')
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.grid(color='black', linestyle=':')
plt.title(plot_title)
saver_helper(fig1, file_name=plot_dir + plot_file)
plt_free()
def plot_correlation(plot_dir, datafile, title, plotfile):
"""
#
:param plot_dir:
:param datafile:
:param title:
:param plotfile:
:return:
"""
# load hurst data
hustdata = load_csv_str(datafile=plot_dir + datafile)
bars1 = [float(number) for number in column(hustdata, 1)]
yer1 = [float(number) for number in column(hustdata, 2)]
xticks = column(hustdata, 0)
hline = 1
# width of the bars
bar_width = 0.3
ylabel = 'Correlation'
# plotting
print_info(title=datafile, location=plot_dir)
xpos = np.arange(len(bars1))
fig, ax = plt.subplots()
ax.bar(xpos, bars1, width=bar_width, color='yellow', edgecolor='black', yerr=yer1, capsize=7)
plt.axhline(hline, color="red")
plt.xticks(xpos, xticks, rotation=45)
plt.ylabel(ylabel)
plt.title(title)
plt.tight_layout()
saver_helper(fig, file_name=plot_dir + plotfile)
plt_free()
def plot_hurst(plot_dir, datafile, title, plotfile):
"""
#
:param plot_dir:
:param datafile:
:param title:
:param plotfile:
:return:
"""
# load hurst data
hustdata = load_csv_str(datafile=plot_dir + datafile)
bars1 = [float(number) for number in column(hustdata, 1)]
yer1 = [float(number) for number in column(hustdata, 2)]
xticks = column(hustdata, 0)
# filter data
hline = bars1[0]
bars1 = bars1[1:]
yer1 = yer1[1:]
xticks = xticks[1:]
# width of the bars
bar_width = 0.3
ylabel = 'Hurst Exponent'
print_info(title=datafile, location=plot_dir)
xpos = np.arange(len(bars1))
fig, ax = plt.subplots()
ax.bar(xpos, bars1, width=bar_width, color='cyan', edgecolor='black', yerr=yer1, capsize=7)
ax.text(1.02, hline, str(hline), va='center', ha="left", bbox=dict(facecolor="w", alpha=0.5),
transform=ax.get_yaxis_transform())
plt.axhline(hline, color="red")
plt.xticks(xpos, xticks, rotation=45)
plt.ylabel(ylabel)
plt.title(title)
plt.tight_layout()
saver_helper(fig, file_name=plot_dir + plotfile)
plt_free()
def plot_std_dev(plot_dir, datafile, title, plotfile):
"""
#
:param plot_dir:
:param datafile:
:param title:
:param plotfile:
:return:
"""
# load hurst data
hustdata = load_csv_str(datafile=plot_dir + datafile)
bars1 = [float(number) for number in column(hustdata, 1)]
yer1 = [float(number) for number in column(hustdata, 2)]
xticks = column(hustdata, 0)
# filter data
hline = bars1[0]
bars1 = bars1[1:]
yer1 = yer1[1:]
xticks = xticks[1:]
# width of the bars
bar_width = 0.3
ylabel = 'Standard Deviation'
print_info(title=datafile, location=plot_dir)
xpos = np.arange(len(bars1))
fig, ax = plt.subplots()
ax.bar(xpos, bars1, width=bar_width, color='lime', edgecolor='black', yerr=yer1, capsize=7)
ax.text(1.02, hline, str(hline), va='center', ha="left", bbox=dict(facecolor="w", alpha=0.5),
transform=ax.get_yaxis_transform())
plt.axhline(hline, color="red")
plt.xticks(xpos, xticks, rotation=45)
plt.ylabel(ylabel)
plt.title(title)
plt.tight_layout()
saver_helper(fig, file_name=plot_dir + plotfile)
plt_free()
def plot_mean(plot_dir, datafile, title, plotfile):
"""
#
:param plot_dir:
:param datafile:
:param title:
:param plotfile:
:return:
"""
# load hurst data
hustdata = load_csv_str(datafile=plot_dir + datafile)
bars1 = [float(number) for number in column(hustdata, 1)]
yer1 = [float(number) for number in column(hustdata, 2)]
xticks = column(hustdata, 0)
# filter data
hline = bars1[0]
bars1 = bars1[1:]
yer1 = yer1[1:]
xticks = xticks[1:]
# width of the bars
bar_width = 0.3
ylabel = 'Avarage inter-packet time'
print_info(title=datafile, location=plot_dir)
xpos = np.arange(len(bars1))
fig, ax = plt.subplots()
ax.bar(xpos, bars1, width=bar_width, color='magenta', edgecolor='black', yerr=yer1, capsize=7)
ax.text(1.02, hline, str(hline), va='center', ha="left", bbox=dict(facecolor="w", alpha=0.5),
transform=ax.get_yaxis_transform())
plt.axhline(hline, color="red")
plt.xticks(xpos, xticks, rotation=45)
plt.ylabel(ylabel)
plt.title(title)
plt.tight_layout()
saver_helper(fig, file_name=plot_dir + plotfile)
plt_free()
def plot_cost_function(plot_dir, datafile, title, plotfile):
"""
#
:param plot_dir:
:param datafile:
:param title:
:param plotfile:
:return:
"""
# load hurst data
hustdata = load_csv_str(datafile=plot_dir + datafile)
bars1 = [float(number) for number in column(hustdata, 1)]
xticks = column(hustdata, 0)
bar_width = 0.3
ylabel = 'Correlation'
print_info(title=datafile, location=plot_dir)
# The x position of bars
xpos = np.arange(len(bars1))
fig, ax = plt.subplots()
# Create
ax.bar(xpos, bars1, width=bar_width, color='purple', edgecolor='black', capsize=7)
plt.xticks(xpos, xticks, rotation=45)
plt.ylabel(ylabel)
plt.title(title)
plt.tight_layout()
saver_helper(fig, file_name=plot_dir + plotfile)
plt_free()
def _get_ModelValueByFunction(functionIndex, costfunction_data1, costfunction_data2, costfunction_data3,
costfunction_data4):
"""
:param functionIndex:
:param costfunction_data1:
:param costfunction_data2:
:param costfunction_data3:
:param costfunction_data4:
:return:
"""
value1 = float(costfunction_data1[functionIndex][1])
value2 = float(costfunction_data2[functionIndex][1])
value3 = float(costfunction_data3[functionIndex][1])
value4 = float(costfunction_data4[functionIndex][1])
label = costfunction_data1[functionIndex][0]
values = [value1, value2, value3, value4]
ModelCostByFunction = collections.namedtuple('ModelCostByFunction', ['label', 'values'])
m = ModelCostByFunction(label=label, values=values)
return m
def plot_cost_function_all2(costfunction1="", costfunction2="", costfunction3="", costfunction4="",
pcapname1="", pcapname2="", pcapname3="", pcapname4="",
title="title", plotfile="plotfile"):
"""
:param costfunction1:
:param costfunction2:
:param costfunction3:
:param costfunction4:
:param pcapname1:
:param pcapname2:
:param pcapname3:
:param pcapname4:
:param title:
:param plotfile:
:return:
"""
print_info(title=title, location=plotfile)
line_width = 2.2
mark_size = 8
costfunction_data1 = order_matrix_str(load_csv_str(datafile=costfunction1), 0)
costfunction_data2 = order_matrix_str(load_csv_str(datafile=costfunction2), 0)
costfunction_data3 = order_matrix_str(load_csv_str(datafile=costfunction3), 0)
costfunction_data4 = order_matrix_str(load_csv_str(datafile=costfunction4), 0)
m0 = _get_ModelValueByFunction(0, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m1 = _get_ModelValueByFunction(1, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m2 = _get_ModelValueByFunction(2, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m3 = _get_ModelValueByFunction(3, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m4 = _get_ModelValueByFunction(4, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m5 = _get_ModelValueByFunction(5, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
m6 = _get_ModelValueByFunction(6, costfunction_data1, costfunction_data2, costfunction_data3, costfunction_data4)
label0 = m0.label
label1 = m1.label
label2 = m2.label
label3 = m3.label
label4 = m4.label
label5 = m5.label
label6 = m6.label
values0 = m0.values
values1 = m1.values
values2 = m2.values
values3 = m3.values
values4 = m4.values
values5 = m5.values
values6 = m6.values
xlables = [pcapname1, pcapname2, pcapname3, pcapname4]
xvalues = [1, 2, 3, 4]
fig, ax = plt.subplots()
with plt.style.context('default'):
plt.xticks(xvalues, xlables)
plt.plot(xvalues, values0, label=label0, marker='o', linewidth=line_width, markersize=mark_size, color="darkblue", markeredgecolor="black")
plt.plot(xvalues, values1, label=label1, marker='*', linewidth=line_width, markersize=mark_size, color="dodgerblue", markeredgecolor="black")
plt.plot(xvalues, values2, label=label2, marker='d', linewidth=line_width, markersize=mark_size, color="springgreen", markeredgecolor="black")
plt.plot(xvalues, values3, label=label3, marker='<', linewidth=line_width, markersize=mark_size, color="lime", markeredgecolor="black")
plt.plot(xvalues, values4, label=label4, marker='>', linewidth=line_width, markersize=mark_size, color="gold", markeredgecolor="black")
plt.plot(xvalues, values5, label=label5, marker='s', linewidth=line_width, markersize=mark_size, color="red", markeredgecolor="black")
plt.plot(xvalues, values6, label=label6, marker='P', linewidth=line_width, markersize=mark_size, color="purple", markeredgecolor="black")
# Shrink current axis's height by 10% on the bottom
# ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])
# Put a legend below current axis
ax.legend(loc='lower center', bbox_to_anchor=(0.5, -0.4), fancybox=True, shadow=True, ncol=3)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax.set_aspect(aspect=0.10)
plt.plot(usetex=True)
plt.ylabel("Cost Function $J$")
plt.xlabel('Pcap Files')
plt.title(title)
# plt.legend()
plt.grid(color='black', linestyle=':', axis='y')
plt.tight_layout()
# plt.legend()
# plt.show()
saver_helper(fig, file_name=plotfile)
plt_free()
def plot_aic_bic2(aicbicfile1="", aicbicfile2="", aicbicfile3="", aicbicfile4="",
pcapname1="", pcapname2="", pcapname3="", pcapname4="",
title="title", plotfile="plotfile"):
"""
:param aicbicfile1:
:param aicbicfile2:
:param aicbicfile3:
:param aicbicfile4:
:param pcapname1:
:param pcapname2:
:param pcapname3:
:param pcapname4:
:param title:
:param plotfile:
:return:
"""
print_info(title=title, location=plotfile)
line_width = 2
mark_size = 8
# ---- Plot the order for each pcap
aicbic1 = load_csv_str(datafile=aicbicfile1)
aicbic2 = load_csv_str(datafile=aicbicfile2)
aicbic3 = load_csv_str(datafile=aicbicfile3)
aicbic4 = load_csv_str(datafile=aicbicfile4)
aicbic1 = column(order_matrix(aicbic1, 1), 0)
aicbic2 = column(order_matrix(aicbic2, 1), 0)
aicbic3 = column(order_matrix(aicbic3, 1), 0)
aicbic4 = column(order_matrix(aicbic4, 1), 0)
aicbicorder1 = order_matrix_str([[aicbic1[i], i + 1] for i in range(0, len(aicbic1))], 0)
aicbicorder2 = order_matrix_str([[aicbic2[i], i + 1] for i in range(0, len(aicbic2))], 0)
aicbicorder3 = order_matrix_str([[aicbic3[i], i + 1] for i in range(0, len(aicbic3))], 0)
aicbicorder4 = order_matrix_str([[aicbic4[i], i + 1] for i in range(0, len(aicbic4))], 0)
m0 = _get_ModelValueByFunction(0, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m1 = _get_ModelValueByFunction(1, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m2 = _get_ModelValueByFunction(2, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m3 = _get_ModelValueByFunction(3, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m4 = _get_ModelValueByFunction(4, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m5 = _get_ModelValueByFunction(5, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
m6 = _get_ModelValueByFunction(6, aicbicorder1, aicbicorder2, aicbicorder3, aicbicorder4)
label0 = m0.label
label1 = m1.label
label2 = m2.label
label3 = m3.label
label4 = m4.label
label5 = m5.label
label6 = m6.label
values0 = m0.values
values1 = m1.values
values2 = m2.values
values3 = m3.values
values4 = m4.values
values5 = m5.values
values6 = m6.values
xlables = [str(pcapname1), str(pcapname2), str(pcapname3), str(pcapname4)]
xvalues = [1, 2, 3, 4]
fig, ax = plt.subplots()
with plt.style.context('default'):
plt.xticks(xvalues, xlables)
plt.plot(xvalues, values0, label=label0, marker='o', linewidth=line_width, markersize=mark_size, color="darkblue", markeredgecolor="black")
plt.plot(xvalues, values1, label=label1, marker='*', linewidth=line_width, markersize=mark_size, color="dodgerblue", markeredgecolor="black")
plt.plot(xvalues, values2, label=label2, marker='d', linewidth=line_width, markersize=mark_size, color="springgreen", markeredgecolor="black")
plt.plot(xvalues, values3, label=label3, marker='<', linewidth=line_width, markersize=mark_size, color="lime", markeredgecolor="black")
plt.plot(xvalues, values4, label=label4, marker='>', linewidth=line_width, markersize=mark_size, color="gold", markeredgecolor="black")
plt.plot(xvalues, values5, label=label5, marker='s', linewidth=line_width, markersize=mark_size, color="red", markeredgecolor="black")
plt.plot(xvalues, values6, label=label6, marker='P', linewidth=line_width, markersize=mark_size, color="purple", markeredgecolor="black")
# Shrink current axis's height by 10% on the bottom
# box = ax.get_position()
# ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])
# Put a legend below current axis
# ax.legend(loc='lower center', bbox_to_anchor=(0.5, -0.4), fancybox=True, shadow=True, ncol=3)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax.set_aspect(aspect=0.25)
plt.ylabel("AIC/BIC position")
plt.xlabel('Pcap Files')
plt.title(title)
plt.grid(color='black', linestyle=':', axis='y')
plt.tight_layout()
# plt.show()
saver_helper(fig, file_name=plotfile)
plt_free()
""""
aicbic_data1 = order_matrix_str(load_csv_str(datafile=aicbicfile1), 0)
aicbic_data2 = order_matrix_str(load_csv_str(datafile=aicbicfile2), 0)
aicbic_data3 = order_matrix_str(load_csv_str(datafile=aicbicfile3), 0)
aicbic_data4 = order_matrix_str(load_csv_str(datafile=aicbicfile4), 0)
print('costfunction_data1: ' + str(aicbic_data1))
print('costfunction_data2: ' + str(aicbic_data2))
print('costfunction_data3: ' + str(aicbic_data3))
print('costfunction_data4: ' + str(aicbic_data4)
"""
"""
m = _get_ModelCostByFunction(0, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
print("m.label:"+m.label)
print("m.values:" + str(m.values))
m0 = _get_ModelCostByFunction(0, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m1 = _get_ModelCostByFunction(1, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m2 = _get_ModelCostByFunction(2, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m3 = _get_ModelCostByFunction(3, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m4 = _get_ModelCostByFunction(4, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m5 = _get_ModelCostByFunction(5, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
m6 = _get_ModelCostByFunction(6, aicbic_data1, aicbic_data2, aicbic_data3, aicbic_data4)
label0 = m0.label
label1 = m1.label
label2 = m2.label
label3 = m3.label
label4 = m4.label
label5 = m5.label
label6 = m6.label
values0 = m0.values
values1 = m1.values
values2 = m2.values
values3 = m3.values
values4 = m4.values
values5 = m5.values
values6 = m6.values
xlables = [pcapname1, pcapname2, pcapname3, pcapname4]
xvalues = [1, 2, 3, 4]
fig, ax = plt.subplots()
with plt.style.context('default'):
plt.xticks(xvalues, xlables)
plt.plot(xvalues, values0, label=label0, marker='o')
plt.plot(xvalues, values1, label=label1, marker='*')
plt.plot(xvalues, values2, label=label2, marker='d')
plt.plot(xvalues, values3, label=label3, marker='<')
plt.plot(xvalues, values4, label=label4, marker='>')
plt.plot(xvalues, values5, label=label5, marker='s')
plt.plot(xvalues, values6, label=label6, marker='P')
# Shrink current axis's height by 10% on the bottom
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
box.width, box.height * 0.9])
# Put a legend below current axis
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=3)
plt.ylabel("AIC/BIC position")
plt.xlabel('Model')
plt.title(title)
#plt.legend()
plt.grid(color='black', linestyle=':', axis='y')
plt.tight_layout()
#plt.legend()
plt.show()
saver_helper(fig, file_name='aic-bic-pos')
plt_free()
"""
def plot_cost_function_all(costfunction1="", costfunction2="", costfunction3="", costfunction4="",
pcapname1="", pcapname2="", pcapname3="", pcapname4="",
title="title", plotfile="plotfile"):
"""
#
:param costfunction1:
:param costfunction2:
:param costfunction3:
:param costfunction4:
:param pcapname1:
:param pcapname2:
:param pcapname3:
:param pcapname4:
:param title:
:param plotfile:
:return:
"""
print_info(title=title, location=plotfile)
costfunction_data1 = order_matrix_str(load_csv_str(datafile=costfunction1), 0)
costfunction_data2 = order_matrix_str(load_csv_str(datafile=costfunction2), 0)
costfunction_data3 = order_matrix_str(load_csv_str(datafile=costfunction3), 0)
costfunction_data4 = order_matrix_str(load_csv_str(datafile=costfunction4), 0)
bars1 = [float(i) for i in column(costfunction_data1, 1)]
bars2 = [float(i) for i in column(costfunction_data2, 1)]
bars3 = [float(i) for i in column(costfunction_data3, 1)]
bars4 = [float(i) for i in column(costfunction_data4, 1)]
xticks = column(costfunction_data1, 0)
bar_width = 0.2
ylabel = 'Cost Function'
bar1label = pcapname1
bar2label = pcapname2
bar3label = pcapname3
bar4label = pcapname4
# The x position of bars
r1 = np.arange(len(bars1))
r2 = [x + bar_width for x in r1]
r3 = [x + 2 * bar_width for x in r1]
r4 = [x + 3 * bar_width for x in r1]
fig, ax = plt.subplots()
ax.bar(r1, bars1, width=bar_width, color='springgreen', hatch="////", lw=1, edgecolor='#003300', capsize=7,
label=bar1label)
ax.bar(r2, bars2, width=bar_width, color='fuchsia', hatch="\\\\\\\\", lw=1, edgecolor='#660066', capsize=7,
label=bar2label)
ax.bar(r3, bars3, width=bar_width, color='mediumblue', hatch="----", lw=1, edgecolor='#000066', capsize=7,
label=bar3label)
ax.bar(r4, bars4, width=bar_width, color='gold', hatch="xxxx", lw=1, edgecolor='#666600', capsize=7,
label=bar4label)
plt.xticks([r + bar_width for r in range(len(bars1))], xticks, rotation=45)
plt.ylabel(ylabel)
plt.xlabel('model')
plt.title(title)
plt.legend()
plt.grid(color='black', linestyle=':', axis='y')
plt.tight_layout()
saver_helper(fig, file_name=plotfile)
plt_free()
"""
def plot_cost_function_all_pcap():
PLOT_DIR = "./plots/"
costfunction1 = PLOT_DIR + "skype/costFunction.dat"
pcaptitle1 = "skype"
costfunction2 = PLOT_DIR + "bigFlows/costFunction.dat"
pcaptitle2 = "lan gateway"
costfunction3 = PLOT_DIR + "lanDiurnal2/costFunction.dat"
pcaptitle3 = "lan firewall diurnal"
costfunction4 = PLOT_DIR + "equinix-1s/costFunction.dat"
pcaptitle4 = "wan"
costfunction_data1 = order_matrix(load_csv_str(datafile=costfunction1), 1)
costfunction_data2 = order_matrix(load_csv_str(datafile=costfunction2), 1)
costfunction_data3 = order_matrix(load_csv_str(datafile=costfunction3), 1)
costfunction_data4 = order_matrix(load_csv_str(datafile=costfunction4), 1)
data1 = create_plot_data_costfunction(costfunction_data1)
data2 = create_plot_data_costfunction(costfunction_data2)
data3 = create_plot_data_costfunction(costfunction_data3)
data4 = create_plot_data_costfunction(costfunction_data4)
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
ax00 = axs[0][0].bar(column(data1, 0), column(data1, 1))
ax10 = axs[1][0].bar(column(data2, 0), column(data2, 1))
ax01 = axs[0][1].bar(column(data3, 0), column(data3, 1))
ax11 = axs[1][1].bar(column(data4, 0), column(data4, 1))
ax11.plt.tight_layout(45)
ax11.set_horizontalalignment("right")
#plt.rotation = 45
for i in range(0, len(ax00)):
ax00[i].set_color(column(data1, 2)[i])
ax10[i].set_color(column(data2, 2)[i])
ax01[i].set_color(column(data3, 2)[i])
ax11[i].set_color(column(data4, 2)[i])
# for label in axs[0][0].get_xmajorticklabels() + axs[0][1].get_xmajorticklabels() + \
# axs[1][0].get_xmajorticklabels() + axs[1][1].get_xmajorticklabels():
# label.set_rotation(45)
# label.set_horizontalalignment("right")
#ax00[0].set_color('r')
# ax.bar([1,2,3,4], [1,2,3,4])
plt.tight_layout()
plt.show()
print(costfunction_data1)
print(costfunction_data2)
print(costfunction_data3)
print(costfunction_data4)
"""
"""
def map_model_color(model):
color = "white"
if model == "Cauchy":
color = "red"
elif model == "Exponential(LR)":
color = "dodgerblue"
elif model == "Exponential(Me)":
color = "yellow"
elif model == "Normal":
color = "blue"
elif model == "Pareto(LR)":
color = "limegreen"
elif model == "Pareto(MLH)":
color = "deeppink"
elif model == "Weibull":
color = "indigo"
else:
print("Error, model not found: "+str(model))
return color
"""
"""
def create_plot_data_costfunction(costfunction_vector):
data_vector = []
for i in range (0, len(costfunction_vector)):
data1 = []
data1.append(costfunction_vector[i][0])
data1.append(float(costfunction_vector[i][1]))
data1.append(map_model_color(costfunction_vector[i][0]))
data_vector.append(data1)
return data_vector
"""
def plot_costfunction_vs_aicbic(aicbic1, costfunction1, pcaptitle1,
aicbic2, costfunction2, pcaptitle2,
aicbic3, costfunction3, pcaptitle3,
aicbic4, costfunction4, pcaptitle4,
title, plotfile):
"""
Plots the relative difference between the model accuracy order of the cost
function and AIC/BIC order of model selection
:param aicbic1:
:param costfunction1:
:param pcaptitle1:
:param aicbic2:
:param costfunction2: