-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_fig_from_meta.py
122 lines (100 loc) · 3.61 KB
/
signal_fig_from_meta.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
#!/usr/bin/env python3
# This script plots the figure from a meta csv file
import argparse
import copy
from src.meta.merge import Merger
from src.plots.plotter import Plotter
argparser = argparse.ArgumentParser(description="Reads the metadata csv files and draw a figure for it")
argparser.add_argument("-i", "--input", help="Input metadata csv.")
argparser.add_argument("-o", "--output", help="The output fig.", default="./meta_fig")
argparser.add_argument("-t", "--plot-type", choices=["per-flow", "leg-mal-per-flow"], default="per-flow", help="The kind of figure.")
fontsize = 16
font = {
'family':'normal',
# 'weight':'bold',
'size':fontsize
}
def process_input(input_filename)-> Merger:
merger = Merger()
merger.add_a_metafile(input_filename)
return merger
def process_plots(input_data):
pass
def process_legitimate_malicious_per_flow(input_data: Merger, output_filename):
data, label = input_data.get_series_data()
def _divide(op1, op2):
if op2 == 0:
return 0
return op1/op2
no_group = int(len(label)/2)
def total_to_legitimate(data):
for mal_sig, tot_sig, mal_f, tot_f in zip(data[1], data[2], data[3], data[4]):
tot_sig -= mal_sig
tot_f -= mal_f
total_to_legitimate(data)
series_name = ["Malicious", "Legitimate"]
series_data = []
for i in range(no_group):
id1 = {0:1,1:2}[i]
id2 = {0:3,1:4}[i]
series = []
for v1, v2 in zip(data[id1], data[id2]):
series.append(_divide(v1,v2))
series_data.append(series)
plot_data = {}
for i, name in enumerate(series_name):
plot_data[name] = {}
plot_data[name]['x'] = copy.copy(data[0])
plot_data[name]['y'] = series_data[i]
plotter = Plotter(
data = plot_data,
x_legend= "Time",
y_legend= "# of signals per flow"
)
plotter.set_matplotlib_param('font', font)
plotter.linePlot(alignX=True, mva=240)
# plotter.addGrayBox_X(4380,5520) # IDS2018-LOIC-UDP
plotter.addGrayBox_X(500, 4397) # IDS2018-LOIC-HTTP
# plotter.addGrayBox_X(448, 1315) # DNS-DDoS-Amp
plotter.saveFig(output_filename + ".png")
plotter.saveEps(output_filename + '.eps')
def process_per_flow(input_data: Merger, output_filename):
data, label = input_data.get_series_data()
def _divide(op1, op2):
if op2 == 0:
return 0
return op1/op2
no_group = int(len(label)/2)
series_name = ["Malicious", "Total"]
series_data = []
for i in range(no_group):
id1 = {0:1,1:2}[i]
id2 = {0:3,1:4}[i]
series = []
for v1, v2 in zip(data[id1], data[id2]):
series.append(_divide(v1,v2))
series_data.append(series)
plot_data = {}
for i, name in enumerate(series_name):
plot_data[name] = {}
plot_data[name]['x'] = copy.copy(data[0])
plot_data[name]['y'] = series_data[i]
plotter = Plotter(
data = plot_data,
x_legend= "Time",
y_legend= "# of signals per flow"
)
plotter.set_matplotlib_param('font', font)
plotter.linePlot(alignX=True, mva=20)
# plotter.addGrayBox_X(4380,5520) # IDS2018-LOIC-UDP
# plotter.addGrayBox_X(500, 4397) # IDS2018-LOIC-HTTP
plotter.addGrayBox_X(448, 1315) # DNS-DDoS-Amp
plotter.saveFig(output_filename + ".png")
plotter.saveEps(output_filename + '.eps')
if __name__ == "__main__":
args = argparser.parse_args()
data = process_input(args.input)
{
"per-flow": process_per_flow,
"leg-mal-per-flow": process_legitimate_malicious_per_flow,
}[args.plot_type](data, args.output)