Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper function that just returns codes #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lshash/lshash.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ def index(self, input_point, extra_data=None):
table.append_val(self._hash(self.uniform_planes[i], input_point),
value)

def code(self, input_point):
""" Calculate LSH code for a single input point. Returns one code of
length `hash_size` for each `hash_table`.

: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.
"""

if isinstance(input_point, np.ndarray):
input_point = input_point.tolist()

return [self._hash(self.uniform_planes[i], input_point)
for i in xrange(self.num_hashtables)]


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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import lshash
import numpy as np

def test_code():
""" test codes are of the correct length and work with multiple hash tables """
l = lshash.LSHash(10, 20)

assert len(l.code(np.random.randn(20))) == 1
assert len(l.code(np.random.randn(20))[0]) == 10

l = lshash.LSHash(10, 20, num_hashtables=3)

assert len(l.code(np.random.randn(20))) == 3

for hash in l.code(np.random.randn(20)):
assert len(hash) == 10