-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute.py
240 lines (188 loc) · 6.04 KB
/
compute.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
"""
"""
import os
from os.path import join
import argparse
import pickle
import gzip
import time
import json
import sys
from concurrent.futures import ProcessPoolExecutor
import numpy as np
from tqdm import tqdm
from gziplength import GzipLengthCalc
clen = lambda data : len(gzip.compress(data))
NCD = lambda c1, c2, c12 : (c12-min(c1,c2))/max(c1, c2)
def do_block(test_data, train_data,
precomputed_lengths,
#outfile,
method,
i,
num_save,
args_dtype,
passthrough):
"""
train_lengths: pre-computed
passthough dictionary is just .update() to the output.
"""
start = time.time()
n_test = len(test_data)
n_train = len(train_data)
print(f"[start] {i=} {n_test=} {n_train=}")
sys.stdout.flush()
D = np.zeros((n_test,n_train))
assert(method in (
'orig',
'precomputed',
'gziplength',
'zeros',
))
#orig
if method == 'orig':
for i,t1 in enumerate(test_data):
l1 = clen(t1.encode('utf8'))
for j, t2 in enumerate(train_data):
l2 = clen(t2.encode('utf8'))
l12 = clen( (t1 + ' ' + t2).encode('utf8') )
D[i,j] = NCD(l1, l2, l12)
elif method == 'precomputed':
for i,t1 in enumerate(test_data):
l1 = clen(t1)
for j, t2 in enumerate(train_data):
l2 = precomputed_lengths[j]
l12 = clen(t1 + b" " + t2)
D[i,j] = NCD(l1, l2, l12)
elif method == 'gziplength':
for i,t1 in enumerate(test_data):
g = GzipLengthCalc(t1)
l1 = g.length1
for j, t2 in enumerate(train_data):
l2 = precomputed_lengths[j]
l12 = g.length2(t2)
D[i,j] = NCD(l1, l2, l12)
elif method == 'zeros':
#D is already zeros
pass
else:
raise ValueError('bad method:' + repr(method))
out = {
"size" : D.shape,
"time" : time.time() - start,
"i": i,
}
out.update(passthrough)
print("out:", out)
top_args = np.argsort(D,axis=1)[:,:num_save].astype(args_dtype)
return top_args
def done_callback(future):
"""
"""
print("[done]")
sys.stdout.flush()
ex = future.exception()
if ex:
print("ERROR:",ex)
def main():
"""
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dataset',
required=True,help="path of .pkl of dataset")
parser.add_argument('--method',
default='gziplength',
choices = [
'orig',
'precomputed',
'gziplength',
'zeros',
])
parser.add_argument('--splitsize', default=500, type=int)
parser.add_argument('--num_save', default=100, type=int,
help = "number of nearest neighbors to save")
parser.add_argument('--limit_train', default=None, type=int)
parser.add_argument('--limit_test', default=None, type=int)
parser.add_argument('--outfile',
help = "output path for .pkl of sorted indices")
args = parser.parse_args()
method = args.method
ds = pickle.load(open(args.dataset,"rb"))
print(ds.keys())
if method in ('orig','zeros'):
pass #keep as strings
train_data = ds['train_data']
test_data = ds['test_data']
else:
# convert strings to bytes
train_data = [t.encode('utf8') for t in ds['train_data']]
test_data = [t.encode('utf8') for t in ds['test_data']]
train_labels = ds['train_labels']
test_labels = ds['test_labels']
if args.limit_train != None:
train_data = train_data[:args.limit_train]
train_labels = train_labels[:args.limit_train]
if args.limit_test != None:
test_data = test_data[:args.limit_test]
test_labels = test_labels[:args.limit_test]
n_train = len(train_data)
n_test = len(test_data)
#pre-process train_data
if method in ('orig','zeros'):
train_lengths = None # not used
else:
train_lengths = []
for j,t2 in enumerate(tqdm(train_data)):
train_lengths.append(clen(t2))
num_save = args.num_save
splitsize = args.splitsize
start_indices = list(range(0, n_test, splitsize))
num_splits = len(start_indices)
#or multiprocessing.cpu_count()
ncpu = os.cpu_count()
max_workers = ncpu
print(json.dumps(dict(
n_train = n_train,
n_test = n_test,
num_splits = num_splits,
ncpu = ncpu,
max_workers = max_workers,
method = method,
splitsize = splitsize,
)))
#
args_dtype = 'uint32'
futures = []
with ProcessPoolExecutor(max_workers=max_workers) as executor:
for i,k in enumerate(start_indices):
future = executor.submit(
do_block,
test_data[k:k+splitsize],
train_data,
train_lengths,
method,
i,
num_save,
args_dtype,
{"num_splits":num_splits},
)
future.add_done_callback(done_callback)
futures.append((k,future))
print("#futures:", len(futures))
top_args = np.zeros((n_test,num_save), args_dtype)
for k,future in futures:
top_args1 = future.result()
n_test1 = top_args1.shape[0]
top_args[k:k+n_test1] = top_args1
#compute 1st nearest neighbor score:
hyp = train_labels[top_args[:,0]]
ref = test_labels
acc = (hyp == ref).mean()
print(f"acc:{acc:0.3f}")
pickle.dump({
'train_labels': train_labels,
'test_labels': test_labels,
'args': top_args,
}, open(args.outfile,'wb'))
print("wrote",args.outfile)
if __name__ == "__main__":
main()