-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsketches.py
executable file
·329 lines (277 loc) · 10.4 KB
/
sketches.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
"""
This module implements Hash Sketch probabilistic data structure which is able to calculate the cardinality of large multisets in a single pass using little auxiliary memory
"""
try:
from bitarray import bitarray
except ImportError:
raise ImportError('requires bitarray')
from hashlib import sha1
from math import ceil
from math import log
from math import pow
from sys import getsizeof
from Interface import Interface
def get_SHA1_bin(word):
"""
Returns the SHA1 hash of any string
"""
hash_s = sha1()
hash_s.update(word)
return bin(long(hash_s.hexdigest(),16))[2:].zfill(160)
def get_index(binString,endIndex=160):
"""
Returns the position of the first 1 bit
from the left in the word until endIndex
"""
res = -1
try:
res = binString.index('1')+1
except(ValueError):
res = endIndex
return res
class Sketch(Interface):
"""
Class implements a Hyperloglog probabilistic data structure
"""
def __init__(self,max_cardinality):
"""Implementes a Hash Sketch
maxCardinality
this Sketch is able to count cardinalities up to cardinality *maxCardinality*
"""
self.__maxCardinality=maxCardinality
self.__maxIndex = 0
self.__bitarray = bitarray(160)
self.__bitarray.setall(False)
self.__name = "Sketch"
def getName(self):
"""
Returns name
"""
return self.__name
def add(self,item):
"""
Adds the item to the Hash Sketch
"""
binword = get_SHA1_bin(item)
index = get_index(binword)
self.__bitarray[index]= True
self.__maxIndex = max(self.__maxIndex,index)
def get_raw_estimate(self):
"""
Returns the raw estimate of sets cardinality
"""
return self.__maxIndex
def get_number_estimate(self):
"""
Returns Linear Count estimate of the sets cardinality
"""
zerobits = self.__bitarray.count(False)
fraction = float(zerobits)/len(self.__bitarray)
res = -len(self.__bitarray)*log(fraction)
return res
class LogLogSketch(Interface):
"""
Implements a LogLog Sketch
"""
def __init__(self, maxCardinality, error_rate):
"""
Implementes a LogLog Sketch
*maxCardinality
this Sketch is able to count cardinalities up to cardinality *maxCardinality*
error_rate
the error_rate of the sketch when calculating the cardinality of the set
"""
if not (0 < error_rate < 1):
raise ValueError("Error_Rate must be between 0 and 1.")
if not maxCardinality > 0:
raise ValueError("maxCardinality must be > 0")
self._maxCardinality = maxCardinality
self._k = int(round(log(pow(1.30/error_rate,2),2)))
self._bucketNumber = 1<<self._k
self._bucketSize = self._wordSizeCalculator(self._maxCardinality)
self._bucketList =[bitarray(self._bucketSize) for _ in xrange(self._bucketNumber)]
for barray in self._bucketList:
barray.setall(False)
self.__name = "LogLogSketch"
def getName(self):
"""
Returns name
"""
return self.__name
def add(self,item):
"""
Adds the item to the LogLog Sketch
"""
binword = get_SHA1_bin(item)
pos = int(binword[:self._k],2)
aux = getIndex(binword[self._k:],160-self._k)
index = min(aux,(1<<self._bucketSize)-1)
newValue = max(int(self._bucketList[pos].to01(),2),index)
self._bucketList[pos] = bitarray(bin(newValue)[2:])
def getNumberEstimate(self):
"""
Returns the estimate of the cardinality
"""
m = self._bucketNumber
e = 0.39701 * m*2**((1.0/m)*sum([int(x.to01(),2) for x in self._bucketList]))
return e
def __sizeof__(self):
return self._bucketNumber* self._bucketSize
def _wordSizeCalculator(self,Nmax):
"""
Estimates the size of the memory Units, using the maximum cardinality as an argument
"""
return int(ceil(log(log(Nmax,2),2)))
class SuperLogLogSketch(Interface):
"""
Implements the improved version of LogLog Sketches, SuperLogLog Sketches
"""
def __init__(self, maxCardinality, error_rate):
"""
Implementes a SuperLogLog Sketch
*maxCardinality
this Sketch is able to count cardinalities up to cardinality *maxCardinality*
error_rate
the error_rate of the sketch when calculating the cardinality of the set
"""
if not (0 < error_rate < 1):
raise ValueError("Error_Rate must be between 0 and 1.")
if not maxCardinality > 0:
raise ValueError("maxCardinality must be > 0")
self._maxCardinality = maxCardinality
self._k = int(round(log(pow(1.05/error_rate,2),2)))
self._bucketNumber = 1<<self._k
self._bucketSize = int(ceil(log(log(float(self._maxCardinality)/self._bucketNumber+3,2),2)))
self._B = 1 << self._bucketSize
self.__name = "SuperLogLogSketch"
self._bucketList = [0 for _ in xrange(self._bucketNumber)]
def getName(self):
"""
Returns name
"""
return self.__name
def getNumberEstimate(self,beta = 0.7):
"""
Returns the estimate of the cardinality
Arguments:
beta= Used to get the truncated list. Keep only the *beta* smallest values and discard the rest
"""
newList = sorted(self._bucketList)
lastIndex = ceil(len(newList)*beta)
nbeta = lastIndex/len(newList)
newList = newList[:int(lastIndex)]
m = self._bucketNumber*nbeta
e = 0.39701 * m*2**((1.0/m)*sum(newList))
return e
def _restrition_rule(self,unrestricted_value):
return min(unrestricted_value,self._B)
def add(self,item):
"""
Adds the item to the LogLog Sketch
"""
binword = get_SHA1_bin(item)
index = int(binword[:self._k],2)
self._bucketList[index] = self._restrition_rule(max(self._bucketList[index],getIndex(binword[self._k:],160-self._k)))
class HyperLogLogSketch(Interface):
"""
Implements a HyperLogLog Sketch
"""
def __init__(self, maxCardinality, error_rate):
"""Implementes a HyperLogLog Sketch
*maxCardinality
this Sketch is able to count cardinalities up to cardinality *maxCardinality*
error_rate
the error_rate of the sketch when calculating the cardinality of the set
"""
self.__ALPHA16=0.673
self.__ALPHA32=0.697
self.__ALPHA64=0.709
if not (0 < error_rate < 1):
raise ValueError("Error_Rate must be between 0 and 1.")
if not maxCardinality > 0:
raise ValueError("maxCardinality must be > 0")
self._maxCardinality = maxCardinality
self._k = int(round(log(pow(1.04/error_rate,2),2)))
self._bucketNumber = 1<<self._k
self._bucketSize = self._wordSizeCalculator(self._maxCardinality)
self._bucketList =[0 for _ in xrange(self._bucketNumber)]
self.__name = "HyperLogLogSketch"
self._alpha = self.__getALPHA(self._bucketNumber)
def __getALPHA(self,m):
if m <=16:
return self.__ALPHA16
elif m<=32:
return self.__ALPHA32
elif m<=64:
return self.__ALPHA64
else:
return 0.7213/(1+1.079/float(m))
def getName(self):
return self.__name
def add(self,item):
"""
Adds the item to the LogLog Sketch
"""
binword = get_SHA1_bin(item)
pos = int(binword[:self._k],2)
#Retrives the position of leftmost 1
aux = get_index(binword[self._k:],160-self._k)
# Sets its own register value to maximum value seen so far
self._bucketList[pos] = max(aux,self._bucketList[pos])
def getNumberEstimate(self):
"""
Returns the estimate of the cardinality
"""
m = self._bucketNumber
raw_e = self._alpha*pow(m,2)/sum([pow(2,-x) for x in self._bucketList])
if raw_e <= 5/2.0*m:
v = self._bucketList.count(0)
if v!=0:
return m*log(m/float(v),2)
else:
return raw_e
elif raw_e <= 1/30.0*2**160:
return raw_e
else:
return -2**160*log(1-raw_e/2.0**160,2)
def join(self,*HyperLogLogList):
"""
Joins the HyperLogLog Sketches passed as argument, with this HyperLogLog Sketch
"""
if HyperLogLogList:
for sketch in HyperLogLogList:
if type(sketch)!=type(self):
raise TypeError("all arguments must be HyperLogLog Sketches")
bucketLists = zip(self._bucketList,*[sketch._bucketList for sketch in HyperLogLogList])
self._bucketList = map(max,bucketLists)
else:
raise TypeError("join expected at least 1 argument, got 0")
def __sizeof__(self):
return self._bucketNumber* self._bucketSize
def _wordSizeCalculator(self,Nmax):
"""
Estimates the size of the memory Units, using the maximum cardinality as an argument
"""
return int(ceil(log(log(Nmax,2),2)))
def bs(number):
"""
Returns the binary representation of the number given as argument
"""
return str(number) if number<=1 else bs(number>>1) + str(number&1)
if __name__ == '__main__':
import sys
a = HyperLogLogSketch(2000000,0.05)
b = HyperLogLogSketch(2000000,0.05)
c = HyperLogLogSketch(2000000,0.05)
for i in xrange(100000):
a.add(str(i))
for i in xrange(1500):
b.add(str(i))
for i in xrange(100000,200000):
c.add(str(i))
#print sys.getsizeof(a)
print "1-100,000 random items put in set - Estimated count: ", a.getNumberEstimate()
print "1500 random items put in set - Estimated count: ", b.getNumberEstimate()
print "100,000-200,000 random items put in set - Estimated count: ", c.getNumberEstimate()
print "Making a joined set with items numbered 1-100k and 100k-200k", c.join(a,b)
print "Here is the joined count: ", c.getNumberEstimate()