-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetloss.py
executable file
·97 lines (76 loc) · 2.28 KB
/
getloss.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
#!/usr/bin/python3
import sys
import arisu_data
import logger
import getopt
def usage():
print("""\
Usage: getloss.py [-e <max error>] <path> [<original>]
-e <max_error>: maximum error rate to original value(0~1)
""")
class GetLoss:
def __init__(self, err_rate):
self.__err_rate = err_rate
def run(self, path, path_org):
data = arisu_data.ArisuData()
if not data.load(path):
return 1
data_org = None
if not path_org is None:
data_org = arisu_data.ArisuData()
if not data_org.load(path_org):
return 1
if not data_org is None and len(data.arisuqs) != len(data_org.arisuqs):
logger.error("original data is mismatched")
return 1
idx = 0
n_loss = 0
for aq in data.arisuqs:
aq_org = None
if not data_org is None:
aq_org = data_org.arisuqs[idx]
if self.__is_lossy_arisuq(aq, aq_org):
n_loss += 1
idx += 1
n_totals = len(data.arisuqs)
print("Loss rate: {}% ({}/{})".format(round(n_loss / n_totals * 100, 4), n_loss, n_totals))
return 0
def __is_lossy_arisuq(self, aq, aq_org):
if aq.is_loss:
return True
if not aq_org is None:
if aq_org.is_loss:
return True
fs1 = aq.get_features()
fs2 = aq_org.get_features()
for i in range(0, 5):
if abs(fs1[i] - fs2[i]) > fs1[i] * self.__err_rate:
return True
return False
err_rate = 0.3
def parseArgs():
global err_rate, args
try:
opts, args = getopt.getopt(sys.argv[1:], "e:")
except getopt.GetoptError:
return False
for o, a in opts:
if o == '-e':
err_rate = float(a)
if err_rate < 0 or err_rate > 1:
logger.error("invalid max error rate: {}".format(err_rate))
return False
if len(args) < 1:
return False
return True
if __name__ == "__main__":
import getloss
logger.init("getloss")
if not parseArgs():
usage()
exit(1)
path_org = None
if len(args) == 2:
path_org = args[1]
gl = getloss.GetLoss(err_rate)
exit(gl.run(args[0], path_org))