-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
612 lines (539 loc) · 21.2 KB
/
filters.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import numpy as np
import math
from bitarray import bitarray
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.preprocessing import MinMaxScaler
from scipy.special import expit
import mmh3
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
fh = logging.FileHandler("logs/agreement_all.log")
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
class BloomFilter:
def __init__(self, size, hash_count):
self.size = size
self.hash_count = hash_count
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def add(self, x):
for seed in range(0, self.hash_count):
result = mmh3.hash(bytes(x), seed) % self.size
self.bit_array[result] = 1
def lookup(self, x):
for seed in range(0, self.hash_count):
result = mmh3.hash(bytes(x), seed) % self.size
if self.bit_array[result] == 0:
return False
return True
def bulk_add(self, X):
for x in X:
self.add(x)
def compute_fpr(self, X):
fp = 0
tn = 0
for x in X:
if self.lookup(x):
fp += 1
else:
tn += 1
return fp / (fp + tn)
def get_efficient_hash_count(bitarray_size, num_inserted):
eff_hash_count = int(math.log(2) * bitarray_size / num_inserted)
eff_hash_count = max(eff_hash_count, 1)
return eff_hash_count
def compute_fpr_theoretical(bitarray_size, num_inserted):
m = bitarray_size
n = num_inserted
k = BloomFilter.get_efficient_hash_count(m, n)
return (1 - (1 - (1 / m)) ** (n * k)) ** k
def compute_fprs_theoretical(bitarray_sizes, num_inserted):
n = num_inserted
fprs = []
for m in bitarray_sizes:
k = BloomFilter.get_efficient_hash_count(m, n)
fpr = (1 - (1 - (1 / m)) ** (n * k)) ** k
fprs.append(fpr)
return fprs
def filled(self):
cnt = 0
for i in range(self.size):
if self.bit_array[i] == 1:
cnt += 1
return cnt
class ProjectionBloomFilter:
def __init__(self, size, hash_count, dim):
# vectors = np.random.normal(
# 0, 1, size=(hash_count, dim)
# ) # Choose random vectors from a Gaussian with mean 0 and variance 1
vectors = np.random.uniform(0, 1, size=(hash_count, dim))
self._normalize_vectors(vectors)
self.vectors = np.transpose(vectors) # Each column corresponds to a vector
self.hash_count = hash_count
self.size = size
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def _normalize_vectors(
self, vectors
): # Converts an array where each row is a vector to corresponding unit vectors
for i in range(len(vectors)):
vectors[i] = vectors[i] / np.sqrt(np.sum(vectors[i] ** 2))
return vectors
def compute_hashes(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
projections = np.transpose(projections)
# projections = expit(projections) # Sigmoid on each value so that they are in the range (0,1)
# All values are integers in the range [0, bitarray_size-1]
hash_values = (projections * (self.size - 1)).astype(int)
return hash_values # Each row contains hash values of that datapoint corresponding to that row
def bulk_add(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
self.scaler = [
MinMaxScaler().fit(projections[i].reshape(-1, 1))
for i in range(projections.shape[0])
]
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
# projections = expit(projections)
projections = np.transpose(projections)
hash_values = (projections * (self.size - 1)).astype(int)
for row in hash_values:
for hash_value in row:
self.bit_array[hash_value] = 1
def lookup(self, X):
results = np.full(X.shape[0], True)
hash_values = self.compute_hashes(X)
for i in range(hash_values.shape[0]):
for hash_value in hash_values[i]:
if (
hash_value < 0
or hash_value >= self.size
or self.bit_array[hash_value] == 0
):
results[i] = False
break
return results
def compute_fpr(self, X): # Assumes that X contains only negative samples
fp = 0
tn = 0
results = self.lookup(X)
for result in results:
if result == True:
fp += 1
else:
tn += 1
return fp / (fp + tn)
def filled(self):
cnt = 0
for i in range(self.size):
if self.bit_array[i] == 1:
cnt += 1
return cnt
class MultipleBitarrayProjectionBloomFilter:
def __init__(self, size, hash_count, dim):
vectors = np.random.normal(
0, 1, size=(hash_count, dim)
) # Choose random vectors from a Gaussian with mean 0 and variance 1
self._normalize_vectors(vectors)
self.vectors = np.transpose(vectors) # Each column corresponds to a vector
self.hash_count = hash_count
self.size = int(size / hash_count)
self.bit_array = [bitarray(self.size) for i in range(hash_count)]
for i in range(len(self.bit_array)):
self.bit_array[i].setall(0)
def _normalize_vectors(
self, vectors
): # Converts an array where each row is a vector to corresponding unit vectors
for i in range(len(vectors)):
vectors[i] = vectors[i] / np.sqrt(np.sum(vectors[i] ** 2))
return vectors
def compute_hashes(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = expit(
projections
) # Sigmoid on each value so that they are in the range (0,1)
hash_values = (projections * (self.size - 1)).astype(
int
) # All values are integers in the range [0, bitarray_size-1]
return hash_values # Each row contains hash values of that datapoint corresponding to that row
def bulk_add(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
self.scaler = [
MinMaxScaler().fit(projections[i].reshape(-1, 1))
for i in range(projections.shape[0])
]
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
# projections = expit(projections)
projections = np.transpose(projections)
hash_values = (projections * (self.size - 1)).astype(int)
for row in hash_values:
for i in range(len(row)):
hash_value = row[i]
self.bit_array[i][hash_value] = 1
def lookup(self, X):
results = np.full(X.shape[0], True)
hash_values = self.compute_hashes(X)
for i in range(hash_values.shape[0]):
for j in range(len(hash_values[i])):
hash_value = hash_values[i][j]
if self.bit_array[j][hash_value] == 0:
results[i] = False
break
return results
def compute_fpr(self, X): # Assumes that X contains only negative samples
fp = 0
tn = 0
results = self.lookup(X)
for result in results:
if result == True:
fp += 1
else:
tn += 1
return fp / (fp + tn)
class HPBF:
def __init__(self, size, hash_count, dim, sample_factor=100, method="gaussian"):
self.hash_count = hash_count
self.size = int(size / hash_count)
self.bit_array = [bitarray(self.size) for i in range(hash_count)]
for i in range(len(self.bit_array)):
self.bit_array[i].setall(0)
self.method = method
self.sample_factor = sample_factor
self.dim = dim
def _select_vectors(self, X, Y):
size = (self.hash_count * self.sample_factor, self.dim)
if self.method == "gaussian":
candidates = np.random.normal(size=size)
elif self.method == "optimistic":
candidates = []
for i in range(size[0]):
vector = [1] # WARNING: WORST VECTORS
for j in range(size[1] - 1):
vector.append(0)
candidates.append(vector)
candidates = np.array(candidates)
else:
candidates = np.random.uniform(size=size)
pos_projections = np.dot(X, candidates.transpose())
pos_projections = np.transpose(pos_projections)
neg_projections = np.dot(Y, candidates.transpose())
neg_projections = np.transpose(neg_projections)
scaler = [
MinMaxScaler().fit(pos_projections[i].reshape(-1, 1))
for i in range(pos_projections.shape[0])
]
for i in range(pos_projections.shape[0]):
pos_projections[i] = (
scaler[i].transform(pos_projections[i].reshape(-1, 1)).reshape(1, -1)[0]
)
for i in range(neg_projections.shape[0]):
neg_projections[i] = (
scaler[i].transform(neg_projections[i].reshape(-1, 1)).reshape(1, -1)[0]
)
pos_hash_values = (pos_projections * (self.size - 1)).astype(int)
neg_hash_values = (neg_projections * (self.size - 1)).astype(int)
assert pos_hash_values.shape[0] == candidates.shape[0]
overlaps = []
for pos_array, neg_array in zip(pos_hash_values, neg_hash_values):
overlaps.append(len(np.intersect1d(pos_array, neg_array)))
assert len(overlaps) == candidates.shape[0]
overlaps = np.array(overlaps)
best_hashes_idx = np.argsort(overlaps)
best_hashes = candidates[best_hashes_idx[: self.hash_count]]
return best_hashes
def _normalize_vectors(
self, vectors
): # Converts an array where each row is a vector to corresponding unit vectors
for i in range(len(vectors)):
vectors[i] = vectors[i] / np.sqrt(np.sum(vectors[i] ** 2))
return vectors
def initialize(self, X, Y):
"""Initialize PBF
Parameters
----------
X : np.array
Data to be inserted
Y : np.array
Data to be queried on
"""
vectors = self._select_vectors(X, Y)
self._normalize_vectors(vectors)
self.vectors = np.transpose(vectors) # Each column corresponds to a vector
def compute_hashes(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
# print(projections[0][:3])
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
# print(projections[0][:3])
projections = np.transpose(projections)
# projections = expit(projections) # Sigmoid on each value so that they are in the range (0,1)
# All values are integers in the range [0, bitarray_size-1]
for i in range(projections.shape[0]):
for j in range(projections.shape[1]):
if projections[i][j] > 1 or projections[i][j] < 0:
projections[i][j] = -1
else:
projections[i][j] = (projections[i][j] * (self.size - 1))
hash_values = projections.astype(int)
# print(hash_values)
# hash_values = (projections * (self.size - 1)).astype(int)
return hash_values # Each row contains hash values of that datapoint corresponding to that row
def bulk_add(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
self.scaler = [
MinMaxScaler().fit(projections[i].reshape(-1, 1))
for i in range(projections.shape[0])
]
# print(self.scaler[0].data_max_)
# print(self.scaler[0].data_min_)
# print(projections[0][:3])
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
# print(projections[0][:3])
# projections = expit(projections)
projections = np.transpose(projections)
hash_values = (projections * (self.size - 1)).astype(int)
for row in hash_values:
for i in range(len(row)):
hash_value = row[i]
self.bit_array[i][hash_value] = 1
def lookup(self, X):
results = np.full(X.shape[0], True)
hash_values = self.compute_hashes(X)
for i in range(hash_values.shape[0]):
for j in range(len(hash_values[i])):
hash_value = hash_values[i][j]
if hash_value != -1:
if self.bit_array[j][hash_value] == 0:
results[i] = False
break
else:
# print("ERROR", hash_value)
results[i] = False
break
return results
def compute_fpr(self, X): # Assumes that X contains only negative samples
fp = 0
tn = 0
results = self.lookup(X)
for result in results:
if result == True:
fp += 1
else:
tn += 1
return fp / (fp + tn)
class ProjectionBloomFilterWithSelection:
def __init__(self, size, hash_count, dim, sample_factor=100, method="gaussian"):
"""
Parameters
----------
size : int
Size of the bit array
hash_count : int
Number of random vectors to use as hash functions
dim : int
Dimensionality of data
sample_factor : int, optional
sample_factor * hash_count vectors are sampled while selecting vectors, by default 100
method: string, "gaussian" or "uniform"
the distribution of sampled vectors
"""
self.hash_count = hash_count
self.size = size
self.dim = dim
self.sample_factor = sample_factor
self.method = method
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def _select_vectors(self, X, Y):
size = (self.hash_count * self.sample_factor, self.dim)
if self.method == "gaussian":
candidates = np.random.normal(size=size)
else:
candidates = np.random.uniform(size=size)
pos_projections = np.dot(X, candidates.transpose())
pos_projections = np.transpose(pos_projections)
neg_projections = np.dot(Y, candidates.transpose())
neg_projections = np.transpose(neg_projections)
scaler = [
MinMaxScaler().fit(pos_projections[i].reshape(-1, 1))
for i in range(pos_projections.shape[0])
]
for i in range(pos_projections.shape[0]):
pos_projections[i] = (
scaler[i].transform(pos_projections[i].reshape(-1, 1)).reshape(1, -1)[0]
)
for i in range(neg_projections.shape[0]):
neg_projections[i] = (
scaler[i].transform(neg_projections[i].reshape(-1, 1)).reshape(1, -1)[0]
)
pos_hash_values = (pos_projections * (self.size - 1)).astype(int)
neg_hash_values = (neg_projections * (self.size - 1)).astype(int)
assert pos_hash_values.shape[0] == candidates.shape[0]
overlaps = []
for pos_array, neg_array in zip(pos_hash_values, neg_hash_values):
overlaps.append(len(np.intersect1d(pos_array, neg_array)))
assert len(overlaps) == candidates.shape[0]
overlaps = np.array(overlaps)
best_hashes_idx = np.argsort(overlaps)
best_hashes = candidates[best_hashes_idx[: self.hash_count]]
return best_hashes
# for row in pos_hash_values:
# for hash_value in row:
# pos_bit_array[hash_value] = 1
# for row in neg_hash_values:
# for hash_value in row:
# try:
# neg_bit_array[hash_value] = 1
# except IndexError as e:
# # print(e)
# pass
# print(pos_bit_array)
# print(neg_bit_array)
def _normalize_vectors(self, vectors):
"""Converts an array where each row is a vector to corresponding unit vectors"""
for i in range(len(vectors)):
vectors[i] = vectors[i] / np.sqrt(np.sum(vectors[i] ** 2))
return vectors
def compute_hashes(self, X):
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
projections = np.transpose(projections)
# projections = expit(projections) # Sigmoid on each value so that they are in the range (0,1)
# All values are integers in the range [0, bitarray_size-1]
hash_values = (projections * (self.size - 1)).astype(int)
return hash_values # Each row contains hash values of that datapoint corresponding to that row
def initialize(self, X, Y):
"""Initialize PBF
Parameters
----------
X : np.array
Data to be inserted
Y : np.array
Data to be queried on
"""
vectors = self._select_vectors(X, Y)
self._normalize_vectors(vectors)
self.vectors = np.transpose(vectors) # Each column corresponds to a vector
def bulk_add(self, X):
"""Inserts X into Bloom filter. Initialize first."""
projections = np.dot(
X, self.vectors
) # Projections of datapoints on the vectors
projections = np.transpose(projections)
self.scaler = [
MinMaxScaler().fit(projections[i].reshape(-1, 1))
for i in range(projections.shape[0])
]
for i in range(projections.shape[0]):
projections[i] = (
self.scaler[i]
.transform(projections[i].reshape(-1, 1))
.reshape(1, -1)[0]
)
# projections = expit(projections)
projections = np.transpose(projections)
hash_values = (projections * (self.size - 1)).astype(int)
for row in hash_values:
for hash_value in row:
self.bit_array[hash_value] = 1
def lookup(self, X):
results = np.full(X.shape[0], True)
hash_values = self.compute_hashes(X)
for i in range(hash_values.shape[0]):
for hash_value in hash_values[i]:
if (
hash_value < 0
or hash_value >= self.size
or self.bit_array[hash_value] == 0
):
results[i] = False
break
return results
def compute_fpr(self, X): # Assumes that X contains only negative samples
fp = 0
tn = 0
results = self.lookup(X)
for result in results:
if result == True:
fp += 1
else:
tn += 1
return fp / (fp + tn)
def filled(self):
cnt = 0
for i in range(self.size):
if self.bit_array[i] == 1:
cnt += 1
return cnt
class KraskaBloomFilter:
def __init__(self, model):
self.num_inserted = 0 # Keeps track of the number of elements that were inserted into the filter
self.model = model # classifier model associated as hash function
self.size = None
self.bit_array = None
def create_filter(self, size):
self.size = size
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def filled(self):
cnt = 0
for i in range(self.size):
if self.bit_array[i] == 1:
cnt += 1
return cnt