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

Fix pip install lshash for python3 #18

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
==========
Developers
==========

* Kay Zhu (a.k.a He Zhu) <[email protected]>
16 changes: 5 additions & 11 deletions lshash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# lshash/__init__.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 pkg_resources

__title__ = 'lshash'
__author__ = 'Kay Zhu ([email protected])'
__license__ = 'MIT'
__version__ = '0.0.4dev'

from lshash import LSHash
try:
__version__ = pkg_resources.get_distribution(__name__).version
except:
__version__ = '0.0.4dev'
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# bitarray
numpy
# redis
84 changes: 84 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[metadata]

name = lshash
summary = A fast Python implementation of locality sensitive hashing with persistance (Redis) support.
author = Kay Zhu
author-email = [email protected]
license = MIT
home-page = http://...
description-file = README.rst
# Add here all kinds of additional classifiers as defined under
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifier =
Development Status :: 4 - Beta
Programming Language :: Python
Programming Language :: Python :: 2
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Topic :: Software Development :: Libraries

[entry_points]
# Add here console scripts like:
# console_scripts =
# script_name = lshash.module:function
# For example:
# console_scripts =
# fibonacci = lshash.skeleton:run
# as well as other entry_points.


[files]
# Add here 'data_files', 'packages' or 'namespace_packages'.
# Additional data files are defined as key value pairs of source and target:
packages =
lshash
# data_files =
# share/lshash_docs = docs/*

[extras]
# Add here additional requirements for extra features, like:
Redis =
redis>=2.10.0
Hamming =
bitarray

[test]
# py.test options when running `python setup.py test`
addopts = tests

[pytest]
# Options for py.test:
# Specify command line options as you would do when invoking py.test directly.
# e.g. --cov-report html (or xml) for html/xml output or --junitxml junit.xml
# in order to write a coverage file that can be read by Jenkins.
addopts =
--cov lshash --cov-report term-missing
--verbose

[aliases]
docs = build_sphinx

[bdist_wheel]
# Use this option if your package is pure-python
universal = 1

[build_sphinx]
source_dir = docs
build_dir = docs/_build

[pbr]
# Let pbr run sphinx-apidoc
autodoc_tree_index_modules = True
# autodoc_tree_excludes = ...
# Let pbr itself generate the apidoc
# autodoc_index_modules = True
# autodoc_exclude_modules = ...
# Convert warnings to errors
# warnerrors = True

[devpi:upload]
# Options for the devpi: PyPI server and packaging tool
# VCS export must be deactivated since we are using setuptools-scm
no-vcs = 1
formats = bdist_wheel
49 changes: 13 additions & 36 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Setup file for lshash.
This file was generated with PyScaffold 2.5.6
"""

import lshash
import sys
from setuptools import setup

try:
from setuptools import setup
except ImportError:
from distutils.core import setup

def setup_package():
needs_sphinx = {'build_sphinx', 'upload_docs'}.intersection(sys.argv)
sphinx = ['sphinx'] if needs_sphinx else []
setup(setup_requires=['six', 'pyscaffold>=2.5a0,<2.6a0'] + sphinx,
use_pyscaffold=True)

with open('README.rst') as f:
readme = f.read()

with open('LICENSE') as f:
license = f.read()

with open('CHANGES.rst') as f:
changes = f.read()

required = ['numpy']

setup(
name='lshash',
version=lshash.__version__,
packages=['lshash'],
author='Kay Zhu',
author_email='[email protected]',
maintainer='Kay Zhu',
maintainer_email='[email protected]',
description='A fast Python implementation of locality sensitive hashing with persistance support.',
long_description=readme + '\n\n' + changes,
license=license,
requires=required,
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Topic :: Software Development :: Libraries',
],
)
if __name__ == "__main__":
setup_package()
5 changes: 5 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Add requirements only needed for your unittests and during development here.
# They will be installed automatically when running `python setup.py test`.
# ATTENTION: Don't remove pytest-cov and pytest as they are needed.
pytest-cov
pytest
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Dummy conftest.py for lshash.

If you don't know what this is for, just leave it empty.
Read more about conftest.py under:
https://pytest.org/latest/plugins.html
"""
from __future__ import print_function, absolute_import, division

import pytest
48 changes: 48 additions & 0 deletions tests/test_spheres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest
import numpy as np

from lshash import LSHash

__author__ = "Hobson Lane"
__copyright__ = "Kay Zhu (a.k.a He Zhu)"
__license__ = "MIT"


def test_sphere():
X = np.random.normal(size=(1000, 3))
lsh = LSHash(10, 3, num_hashtables=5)
for x in X:
x /= np.linalg.norm(x)
lsh.index(x)
closest = lsh.query(X[0] + np.array([-0.001, 0.001, -0.001]), distance_func="cosine")
assert len(closest) >= 10
assert 0.05 >= closest[9][-1] > 0.0003


def test_hyperspheres():
tenthclosest = []
for D in range(2, 11):
X = np.random.normal(size=(200000, D))
lsh = LSHash(int(64 / D) + D, D, num_hashtables=D)

# query vector
q = np.random.normal(size=(D,))
q /= np.linalg.norm(q)

distances = []
for x in X:
lsh.index(x)
x /= np.linalg.norm(x)
distances += [1 - np.sum(x * q)]
distances = sorted(distances)
closest = lsh.query(q, distance_func='cosine')
N = len(closest)
rank = min(10, N)
tenthclosest += [[D, N - 1, closest[rank - 1][-1] if N else None, distances[rank - 1]]]
print(tenthclosest[-1])
for i, tc in enumerate(tenthclosest):
assert 1e-9 < tc[-2] or 1e-6 < 0.2
return tenthclosest