forked from jsemric/ahofa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-reduction.py
executable file
·86 lines (74 loc) · 3.18 KB
/
app-reduction.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
#!/usr/bin/env python3
# Approximate NFA reduction and error evaluation of the reduction NFA.
import sys
import tempfile
import argparse
import multiprocessing
from nfa import Nfa
from reduction_eval import reduce_nfa, armc
from helpers import export_labeling
def main():
parser = argparse.ArgumentParser(description='Approximate NFA reduction.')
parser.add_argument('-r','--ratio', metavar='N', type=float,
default=.2, help='reduction ratio')
parser.add_argument('input', type=str, help='NFA to reduce')
parser.add_argument('-n','--nw', type=int,
default=multiprocessing.cpu_count() - 1,
help='number of workers to run in parallel')
parser.add_argument('--test', nargs='+', type=str, metavar='PCAP',
help='test pcap files')
parser.add_argument('--train', type=str, metavar='PCAP',
help='train pcap file')
parser.add_argument('-d', '--dest-dir', dest='dest_dir',
help='Destination dir for labeling export.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-m','--merge', action='store_true',
help='merging reduction')
group.add_argument('-a','--armc', action='store_true',
help='merging reduction inspired by abstract regular model checking')
parser.add_argument('-th','--thresh', type=float, metavar='N',
help='threshold for merging', default=.995)
parser.add_argument('-mf','--maxfr', type=float, default=.1,
metavar='N', help='max frequency of a state allowed to be merged')
parser.add_argument('-o','--output', type=str,default='output.fa')
args = parser.parse_args()
if (args.merge or args.armc) and not args.train:
raise SystemError('--train option is required when merging')
# get NFA
aut = Nfa.parse(args.input)
filename = args.input.split('/')[-1].split('.')[0]
if args.armc:
# merge using armc and prune
aut, m = armc(aut, args.train, ratio=args.ratio, th=args.thresh,
merge_empty=False)
sys.stderr.write('states merged: ' + str(m) + '\n')
else:
sys.stderr.write('reduction ratio: ' + str(args.ratio) + '\n')
freq = aut.get_freq(args.train)
if args.dest_dir:
export_labeling(freq, '{}{}.lab'.format(args.dest_dir, filename), args.train)
exit()
aut, m = reduce_nfa(aut, freq, ratio=args.ratio, merge=args.merge,
th=args.thresh, mf=args.maxfr)
if args.merge:
sys.stderr.write('states merged: ' + str(m) + '\n')
with open(args.output,'w') as f:
sys.stderr.write('saved as ' + args.output + '\n')
aut.print(f)
if args.test:
sys.stderr.write('evaluation reduction error\n')
reduced = args.output
r = Nfa.eval_accuracy(args.input, args.output, ' '.join(args.test),
nw=args.nw)
total, fp, tp = 0, 0, 0
for b in r.split('\n'):
if b != '':
_, _, s1, _, _, s2, s3 = b.split(',')
total += int(s1)
fp += int(s2)
tp += int(s3)
print('error:', round(fp/total,4))
if tp + fp > 0:
print('precision:', round(tp/(fp+tp),4))
if __name__ == '__main__':
main()