-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
205 lines (175 loc) · 8.69 KB
/
test.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
import os
import math
import numpy as np
import bisect
import FIRRun
import RNS
Filter = {
'CWA': {'func': FIRRun.CWARun, 'rns': 2},
'HWA': {'func': FIRRun.HWARun, 'rns': 1},
'MWA': {'func': FIRRun.MWARun, 'rns': 1},
'OLMUX': {'func': FIRRun.OLMUXRun, 'rns': 1}
}
def Test_SCLen(arch, minLen, maxLen, rnType, samples, weight):
"""test effect of sc's length on the precision of filter"""
CWD = os.getcwd()
samplesPower = int(math.log2(len(weight)))
numOfExprm = samples.shape[1]
"""calculate reference"""
Ref = np.dot(np.transpose(samples), weight)
error = np.empty(maxLen-minLen+1)
if rnType == 'lfsr':
for rnsLen in range(minLen, maxLen+1):
"""select rns"""
rngFolder = os.path.join(CWD, 'rng/lfsr', '{}'.format(rnsLen))
rngFiles = sorted(os.listdir(rngFolder))
rns = np.empty((2**rnsLen, samplesPower+Filter[arch]['rns']))
for i in range(rns.shape[1]):
rns[:, i] = np.load(os.path.join(rngFolder,rngFiles[i]))
"""filter run"""
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
elif rnType == 'halton':
haltonDir = os.path.join(CWD, 'rng/halton')
folders = os.listdir(haltonDir)
folders = sorted([int(element) for element in folders])
index = bisect.bisect_left(folders, maxLen)
haltonSubDir = os.path.join(haltonDir, '{}'.format(folders[index]))
numRns = samplesPower + Filter[arch]['rns']
rnsSource = np.empty((2**folders[index], numRns))
files = os.listdir(haltonSubDir)
files = sorted([int(element.rstrip('.npy')) for element in files])
for i in range(numRns):
fileName = os.path.join(haltonSubDir, '{}.npy'.format(files[i]))
rnsSource[:, i] = np.load(fileName)
for rnsLen in range(minLen, maxLen+1):
rns = rnsSource[:2**rnsLen, :]
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
return error
def Test_DeterministicSel(arch, minLen, maxLen, rnType, samples, weight):
CWD = os.getcwd()
samplesPower = int(math.log2(len(weight)))
numOfExprm = samples.shape[1]
"""calculate reference"""
Ref = np.dot(np.transpose(samples), weight)
error = np.empty(maxLen-minLen+1)
if rnType == 'lfsr':
for rnsLen in range(minLen, maxLen+1):
"""select rns"""
rngFolder = os.path.join(CWD, 'rng/lfsr', '{}'.format(rnsLen))
rngFiles = sorted(os.listdir(rngFolder))
rns = np.empty((2**rnsLen, samplesPower+Filter[arch]['rns']))
for i in range(Filter[arch]['rns']):
rns[:, i] = np.load(os.path.join(rngFolder,rngFiles[i]))
rns[:, Filter[arch]['rns']:] = RNS.deterministicSel(rnsLen, samplesPower)
"""filter run"""
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
elif rnType == 'halton':
haltonDir = os.path.join(CWD, 'rng/halton')
folders = os.listdir(haltonDir)
folders = sorted([int(element) for element in folders])
index = bisect.bisect_left(folders, maxLen)
haltonSubDir = os.path.join(haltonDir, '{}'.format(folders[index]))
numRns = samplesPower + Filter[arch]['rns']
rnsSource = np.empty((2**folders[index], Filter[arch]['rns']))
files = os.listdir(haltonSubDir)
files = sorted([int(element.rstrip('.npy')) for element in files])
for i in range(Filter[arch]['rns']):
fileName = os.path.join(haltonSubDir, '{}.npy'.format(files[i]))
rnsSource[:, i] = np.load(fileName)
for rnsLen in range(minLen, maxLen+1):
rns = np.empty((2**rnsLen, numRns))
rns[:, :Filter[arch]['rns']] = rnsSource[:2**rnsLen, :]
rns[:, Filter[arch]['rns']:] = RNS.deterministicSel(rnsLen, samplesPower)
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
return error
def Test_Input_Halton_Sel_Lfsr(arch, minLen, maxLen, samples, weight):
CWD = os.getcwd()
samplesPower = int(math.log2(len(weight)))
numOfExprm = samples.shape[1]
"""calculate reference"""
Ref = np.dot(np.transpose(samples), weight)
error = np.empty(maxLen-minLen+1)
"""load halton rns"""
rngHaltonDir = os.path.join(CWD, 'rng/halton')
rngHaltonFolders = os.listdir(rngHaltonDir)
rngHaltonFolders = sorted([int(element) for element in rngHaltonFolders])
index = bisect.bisect_left(rngHaltonFolders, maxLen)
rngHaltonSubDir = os.path.join(rngHaltonDir, '{}'.format(rngHaltonFolders[index]))
rnsHaltonSource = np.empty((2**rngHaltonFolders[index], Filter[arch]['rns']))
rngHaltonFiles = os.listdir(rngHaltonSubDir)
rngHaltonFiles = sorted([int(element.rstrip('.npy')) for element in rngHaltonFiles])
for i in range(Filter[arch]['rns']):
fileName = os.path.join(rngHaltonSubDir, '{}.npy'.format(rngHaltonFiles[i]))
rnsHaltonSource[:, i] = np.load(fileName)
for rnsLen in range(minLen, maxLen+1):
"""select rns"""
rns = np.empty((2**rnsLen, samplesPower+Filter[arch]['rns']))
"""load lfsr rns"""
rngLfsrFolder = os.path.join(CWD, 'rng/lfsr', '{}'.format(rnsLen))
rngLfsrFiles = sorted(os.listdir(rngLfsrFolder))
for i in range(samplesPower):
rns[:, Filter[arch]['rns']+i] = np.load(os.path.join(rngLfsrFolder,rngLfsrFiles[i]))
"""select halton rns"""
rns[:, :Filter[arch]['rns']] = rnsHaltonSource[:2**rnsLen, :]
"""filter run"""
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
return error
def Test_Input_Lfsr_Sel_Halton(arch, minLen, maxLen, samples, weight):
CWD = os.getcwd()
samplesPower = int(math.log2(len(weight)))
numOfExprm = samples.shape[1]
"""calculate reference"""
Ref = np.dot(np.transpose(samples), weight)
error = np.empty(maxLen-minLen+1)
"""load halton rns"""
rngHaltonDir = os.path.join(CWD, 'rng/halton')
rngHaltonFolders = os.listdir(rngHaltonDir)
rngHaltonFolders = sorted([int(element) for element in rngHaltonFolders])
index = bisect.bisect_left(rngHaltonFolders, maxLen)
rngHaltonSubDir = os.path.join(rngHaltonDir, '{}'.format(rngHaltonFolders[index]))
rnsHaltonSource = np.empty((2**rngHaltonFolders[index], samplesPower))
rngHaltonFiles = os.listdir(rngHaltonSubDir)
rngHaltonFiles = sorted([int(element.rstrip('.npy')) for element in rngHaltonFiles])
for i in range(samplesPower):
fileName = os.path.join(rngHaltonSubDir, '{}.npy'.format(rngHaltonFiles[i]))
rnsHaltonSource[:, i] = np.load(fileName)
for rnsLen in range(minLen, maxLen+1):
"""select rns"""
rns = np.empty((2**rnsLen, samplesPower+Filter[arch]['rns']))
"""load lfsr rns"""
rngLfsrFolder = os.path.join(CWD, 'rng/lfsr', '{}'.format(rnsLen))
rngLfsrFiles = sorted(os.listdir(rngLfsrFolder))
for i in range(Filter[arch]['rns']):
rns[:, i] = np.load(os.path.join(rngLfsrFolder,rngLfsrFiles[i]))
"""select halton rns"""
rns[:, Filter[arch]['rns']:] = rnsHaltonSource[:2**rnsLen, :]
"""filter run"""
result, calib = Filter[arch]['func'](samples, weight, rns)
error[rnsLen-minLen] = (np.sum(np.power(Ref-calib, 2))/numOfExprm)**(1/2)
return error
def Test_Input_Pattern(arch, samples, weight):
"""test effect of sc's length on the precision of filter"""
CWD = os.getcwd()
samplesPower = int(math.log2(len(weight)))
distributions = samples.shape[2]
numOfExprm = samples.shape[1]
rnsLen = 12
"""calculate reference"""
Ref = np.transpose(np.array([np.dot(np.transpose(samples[:, :, i]), weight) for i in range(distributions)]))
error = np.empty(distributions)
"""select rns"""
rngFolder = os.path.join(CWD, 'rng/lfsr', '{}'.format(rnsLen))
rngFiles = sorted(os.listdir(rngFolder))
rns = np.empty((2**rnsLen, samplesPower+Filter[arch]['rns']))
for i in range(rns.shape[1]):
rns[:, i] = np.load(os.path.join(rngFolder,rngFiles[i]))
for i in range(distributions):
"""filter run"""
result, calib = Filter[arch]['func'](samples[:,:, i], weight, rns)
error[i] = (np.sum(np.power(Ref[:, i]-calib, 2))/numOfExprm)**(1/2)
return error