-
Notifications
You must be signed in to change notification settings - Fork 159
/
lshash.py
301 lines (249 loc) · 11.5 KB
/
lshash.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
# lshash/lshash.py
# Copyright 2012 Kay Zhu (a.k.a He Zhu) and contributors (see CONTRIBUTORS.txt)
#
# This module is part of lshash and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import os
import json
import numpy as np
from storage import storage
try:
from bitarray import bitarray
except ImportError:
bitarray = None
class LSHash(object):
""" LSHash implments locality sensitive hashing using random projection for
input vectors of dimension `input_dim`.
Attributes:
:param hash_size:
The length of the resulting binary hash in integer. E.g., 32 means the
resulting binary hash will be 32-bit long.
:param input_dim:
The dimension of the input vector. E.g., a grey-scale picture of 30x30
pixels will have an input dimension of 900.
:param num_hashtables:
(optional) The number of hash tables used for multiple lookups.
:param storage_config:
(optional) A dictionary of the form `{backend_name: config}` where
`backend_name` is the either `dict` or `redis`, and `config` is the
configuration used by the backend. For `redis` it should be in the
format of `{"redis": {"host": hostname, "port": port_num}}`, where
`hostname` is normally `localhost` and `port` is normally 6379.
:param matrices_filename:
(optional) Specify the path to the compressed numpy file ending with
extension `.npz`, where the uniform random planes are stored, or to be
stored if the file does not exist yet.
:param overwrite:
(optional) Whether to overwrite the matrices file if it already exist
"""
def __init__(self, hash_size, input_dim, num_hashtables=1,
storage_config=None, matrices_filename=None, overwrite=False):
self.hash_size = hash_size
self.input_dim = input_dim
self.num_hashtables = num_hashtables
if storage_config is None:
storage_config = {'dict': None}
self.storage_config = storage_config
if matrices_filename and not matrices_filename.endswith('.npz'):
raise ValueError("The specified file name must end with .npz")
self.matrices_filename = matrices_filename
self.overwrite = overwrite
self._init_uniform_planes()
self._init_hashtables()
def _init_uniform_planes(self):
""" Initialize uniform planes used to calculate the hashes
if file `self.matrices_filename` exist and `self.overwrite` is
selected, save the uniform planes to the specified file.
if file `self.matrices_filename` exist and `self.overwrite` is not
selected, load the matrix with `np.load`.
if file `self.matrices_filename` does not exist and regardless of
`self.overwrite`, only set `self.uniform_planes`.
"""
if "uniform_planes" in self.__dict__:
return
if self.matrices_filename:
file_exist = os.path.isfile(self.matrices_filename)
if file_exist and not self.overwrite:
try:
npzfiles = np.load(self.matrices_filename)
except IOError:
print("Cannot load specified file as a numpy array")
raise
else:
npzfiles = sorted(npzfiles.items(), key=lambda x: x[0])
self.uniform_planes = [t[1] for t in npzfiles]
else:
self.uniform_planes = [self._generate_uniform_planes()
for _ in xrange(self.num_hashtables)]
try:
np.savez_compressed(self.matrices_filename,
*self.uniform_planes)
except IOError:
print("IOError when saving matrices to specificed path")
raise
else:
self.uniform_planes = [self._generate_uniform_planes()
for _ in xrange(self.num_hashtables)]
def _init_hashtables(self):
""" Initialize the hash tables such that each record will be in the
form of "[storage1, storage2, ...]" """
self.hash_tables = [storage(self.storage_config, i)
for i in xrange(self.num_hashtables)]
def _generate_uniform_planes(self):
""" Generate uniformly distributed hyperplanes and return it as a 2D
numpy array.
"""
return np.random.randn(self.hash_size, self.input_dim)
def _hash(self, planes, input_point):
""" Generates the binary hash for `input_point` and returns it.
:param planes:
The planes are random uniform planes with a dimension of
`hash_size` * `input_dim`.
:param input_point:
A Python tuple or list object that contains only numbers.
The dimension needs to be 1 * `input_dim`.
"""
try:
input_point = np.array(input_point) # for faster dot product
projections = np.dot(planes, input_point)
except TypeError as e:
print("""The input point needs to be an array-like object with
numbers only elements""")
raise
except ValueError as e:
print("""The input point needs to be of the same dimension as
`input_dim` when initializing this LSHash instance""", e)
raise
else:
return "".join(['1' if i > 0 else '0' for i in projections])
def _as_np_array(self, json_or_tuple):
""" Takes either a JSON-serialized data structure or a tuple that has
the original input points stored, and returns the original input point
in numpy array format.
"""
if isinstance(json_or_tuple, basestring):
# JSON-serialized in the case of Redis
try:
# Return the point stored as list, without the extra data
tuples = json.loads(json_or_tuple)[0]
except TypeError:
print("The value stored is not JSON-serilizable")
raise
else:
# If extra_data exists, `tuples` is the entire
# (point:tuple, extra_data). Otherwise (i.e., extra_data=None),
# return the point stored as a tuple
tuples = json_or_tuple
if isinstance(tuples[0], tuple):
# in this case extra data exists
return np.asarray(tuples[0])
elif isinstance(tuples, (tuple, list)):
try:
return np.asarray(tuples)
except ValueError as e:
print("The input needs to be an array-like object", e)
raise
else:
raise TypeError("query data is not supported")
def index(self, input_point, extra_data=None):
""" Index a single input point by adding it to the selected storage.
If `extra_data` is provided, it will become the value of the dictionary
{input_point: extra_data}, which in turn will become the value of the
hash table. `extra_data` needs to be JSON serializable if in-memory
dict is not used as storage.
:param input_point:
A list, or tuple, or numpy ndarray object that contains numbers
only. The dimension needs to be 1 * `input_dim`.
This object will be converted to Python tuple and stored in the
selected storage.
:param extra_data:
(optional) Needs to be a JSON-serializable object: list, dicts and
basic types such as strings and integers.
"""
if isinstance(input_point, np.ndarray):
input_point = input_point.tolist()
if extra_data:
value = (tuple(input_point), extra_data)
else:
value = tuple(input_point)
for i, table in enumerate(self.hash_tables):
table.append_val(self._hash(self.uniform_planes[i], input_point),
value)
def query(self, query_point, num_results=None, distance_func=None):
""" Takes `query_point` which is either a tuple or a list of numbers,
returns `num_results` of results as a list of tuples that are ranked
based on the supplied metric function `distance_func`.
:param query_point:
A list, or tuple, or numpy ndarray that only contains numbers.
The dimension needs to be 1 * `input_dim`.
Used by :meth:`._hash`.
:param num_results:
(optional) Integer, specifies the max amount of results to be
returned. If not specified all candidates will be returned as a
list in ranked order.
:param distance_func:
(optional) The distance function to be used. Currently it needs to
be one of ("hamming", "euclidean", "true_euclidean",
"centred_euclidean", "cosine", "l1norm"). By default "euclidean"
will used.
"""
candidates = set()
if not distance_func:
distance_func = "euclidean"
if distance_func == "hamming":
if not bitarray:
raise ImportError(" Bitarray is required for hamming distance")
for i, table in enumerate(self.hash_tables):
binary_hash = self._hash(self.uniform_planes[i], query_point)
for key in table.keys():
distance = LSHash.hamming_dist(key, binary_hash)
if distance < 2:
candidates.update(table.get_list(key))
d_func = LSHash.euclidean_dist_square
else:
if distance_func == "euclidean":
d_func = LSHash.euclidean_dist_square
elif distance_func == "true_euclidean":
d_func = LSHash.euclidean_dist
elif distance_func == "centred_euclidean":
d_func = LSHash.euclidean_dist_centred
elif distance_func == "cosine":
d_func = LSHash.cosine_dist
elif distance_func == "l1norm":
d_func = LSHash.l1norm_dist
else:
raise ValueError("The distance function name is invalid.")
for i, table in enumerate(self.hash_tables):
binary_hash = self._hash(self.uniform_planes[i], query_point)
candidates.update(table.get_list(binary_hash))
# rank candidates by distance function
candidates = [(ix, d_func(query_point, self._as_np_array(ix)))
for ix in candidates]
candidates.sort(key=lambda x: x[1])
return candidates[:num_results] if num_results else candidates
### distance functions
@staticmethod
def hamming_dist(bitarray1, bitarray2):
xor_result = bitarray(bitarray1) ^ bitarray(bitarray2)
return xor_result.count()
@staticmethod
def euclidean_dist(x, y):
""" This is a hot function, hence some optimizations are made. """
diff = np.array(x) - y
return np.sqrt(np.dot(diff, diff))
@staticmethod
def euclidean_dist_square(x, y):
""" This is a hot function, hence some optimizations are made. """
diff = np.array(x) - y
return np.dot(diff, diff)
@staticmethod
def euclidean_dist_centred(x, y):
""" This is a hot function, hence some optimizations are made. """
diff = np.mean(x) - np.mean(y)
return np.dot(diff, diff)
@staticmethod
def l1norm_dist(x, y):
return sum(abs(x - y))
@staticmethod
def cosine_dist(x, y):
return 1 - np.dot(x, y) / ((np.dot(x, x) * np.dot(y, y)) ** 0.5)