Skip to main content

Python bindings for the Dynamic Exploration Graph library by Nico Hezel

Project description

deglib: Python bindings for the Dynamic Exploration Graph

Python bindings for the C++ library Dynamic Exploration Graph used in the paper: Fast Approximate Nearest Neighbor Search with a Dynamic Exploration Graph using Continuous Refinement

Table of Contents

Installation

Using pip

pip install deglib

This will install a source package, that needs to compile the C++ code in order to create an optimized version for your system.

Compiling from Source

Create Virtual Environment

# create virtualenv with virtualenvwrapper or venv
mkvirtualenv deglib
# or
python -m venv /path/to/deglib_env && . /path/to/deglib_env/bin/activate

Get the Source

# clone git repository
git clone https://github.com/Visual-Computing/DynamicExplorationGraph.git
cd DynamicExplorationGraph/python

Install the Package from Source

pip install setuptools pybind11 build
python setup.py copy_build_files  # copy c++ library to ./lib/
pip install .

This will compile the C++ code and install deglib into your virtual environment, so it may take a while.

Testing

To execute all tests.

pytest

Building Packages

Build packages (sdist and wheels):

python -m build

Note: If you want to publish linux wheels to pypi you have to convert the wheel to musllinux-/manylinux-wheels. This can be easily done using cibuildwheel (if docker is installed):

cibuildwheel --archs auto64 --output-dir dist

Examples

Loading Data

To load a dataset formatted like the TexMex-Datasets:

import deglib
import numpy as np

dataset: np.ndarray = deglib.repository.fvecs_read("path/to/data.fvecs")
num_samples, dims = dataset.shape

The dataset is a numpy array with shape (N, D), where N is the number of feature vectors and D is the number of dimensions of each feature vector.

Building a Graph

graph = deglib.builder.build_from_data(dataset, edges_per_vertex=32, callback="progress")
graph.save_graph("/path/to/graph.deg")
rd_graph = deglib.graph.load_readonly_graph("/path/to/graph.deg")

Searching the Graph

# query can have shape (D,) or (Q, D), where
# D is the dimensionality of the dataset and
# Q is the number of queries.
query = np.random.random((dims,)).astype(np.float32)
result, dists = graph.search(query, eps=0.1, k=10)  # get 10 nearest features to query
print('best dataset index:', result[0])
best_match = dataset[result[0]]

For more examples see tests.

Referencing C++ memory

Consider the following example:

feature_vector = graph.get_feature_vector(42)
del graph
print(feature_vector)

This will crash as feature_vector is holding a reference to memory that is owned by graph. This can lead to undefined behaviour (most likely segmentation fault). Be careful to keep objects in memory that are referenced. If you need it use the copy=True option:

feature_vector = graph.get_feature_vector(10, copy=True)
del graph
print(feature_vector)  # no problem

Copying feature vectors will be slower.

Naming

Vertex = Feature Vector

Each vertex in the graph corresponds to a feature vector of the dataset.

Internal Index vs External Label

There are two kinds of indices used in a graph: internal_index and external_label. Both are integers and specify a vertex in a graph.

Internal Indices are dense, which means that every internal_index < len(graph) can be used. For example: If you add 100 vertices and remove the vertex with internal_index 42, the last vertex in the graph will be moved to index 42.

In contrast, external label is a user defined identifier for each added vertex (see builder.add_entry(external_label, feature_vector)). Adding or Removing vertices to the graph will keep the connection between external labels and associated feature vector.

When you create the external labels by starting with 0 and increasing it for each entry by 1 and don't remove elements from the graph, external labels and internal indices are equal.

# as long as no elements are removed
# external labels and internal indices are equal
for i, vec in enumerate(data):
    builder.add_entry(i, vec)

Eps

The eps-search-parameter controls how many nodes are checked during search. Lower eps values like 0.001 are faster but less accurate. Higher eps values like 0.1 are slower but more accurate. Should always be greater 0.

Relative Neighborhood Graph / RNG-conform

TODO

Limitations

  • The python wrapper at the moment only supports float32 and uint8 feature vectors.

Troubleshooting

BuildError: pybind11/typing.h:104:58: error: ‘copy_n’ is not a member of ‘std’

This is a pybind11 bug, that occurs when compiling it with gcc-14. Change the pybind version to 2.12.

How to publish a new version

  • Run git checkout main and git pull to be sure, all updates are fetched
  • Edit version number in python/src/deglib/__init__.py to x.y.z
  • Run git add -A, git commit -m 'vx.y.z' and git tag -a vx.y.z -m 'vx.y.z'
  • Run git push and git push origin --tags

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deglib-0.1.1.tar.gz (71.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

deglib-0.1.1-cp312-cp312-win_amd64.whl (285.1 kB view details)

Uploaded CPython 3.12Windows x86-64

deglib-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

deglib-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (546.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

deglib-0.1.1-cp312-cp312-macosx_11_0_x86_64.whl (279.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ x86-64

deglib-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (262.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

deglib-0.1.1-cp311-cp311-win_amd64.whl (284.6 kB view details)

Uploaded CPython 3.11Windows x86-64

deglib-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

deglib-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (547.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

deglib-0.1.1-cp311-cp311-macosx_11_0_x86_64.whl (277.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ x86-64

deglib-0.1.1-cp311-cp311-macosx_11_0_arm64.whl (262.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file deglib-0.1.1.tar.gz.

File metadata

  • Download URL: deglib-0.1.1.tar.gz
  • Upload date:
  • Size: 71.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for deglib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7115a2269da6a57aef6af2fc0702080997769062f186698a04294681bb9f1ccf
MD5 dedc15f1de29d1d56dd779f3f070aec7
BLAKE2b-256 1406fb08d0d7a32cc81b5222b2a18e681ab73baa60294952822f6b1bc09e0d96

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1.tar.gz:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: deglib-0.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 285.1 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for deglib-0.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f42dc80c15cb66a18086bfd2c929ea0a724728b0b1c35949637bf11fc7662dbd
MD5 e0ae03bfe2e20971b6039e43d62b4c00
BLAKE2b-256 ddef6e749ffc295364a89efc7a051f8e0e3dc559763d96ba12b8faad3052ec8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp312-cp312-win_amd64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6a3b18be87910816eb02257234e72b41ab8b9ddff0e6861c1a77a2131cbbe03f
MD5 f0c8040d308b72f9105f2b57e275d2c7
BLAKE2b-256 696a97ce38b5a6570e2c99c532f8dc8ffcd431aef1ead79f23104b46b8e499da

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 726ab989c41cabda9bbf86888c1982375e59ae24bd07f471bbc0e22989321c42
MD5 8d6d17382c7753959ceef586d214a29a
BLAKE2b-256 5b577d5671f36fda032218d643234c9bf037aa4a8053c94cad4f65023921c505

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp312-cp312-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 5ebc08eaea4378a1efab814b2ae31299542a6c6be4527d25932bde32f090994c
MD5 0f215f3a9804a23771a6a70c1c3e2c48
BLAKE2b-256 6fcbea4f907533b9997de592c7a3f1f2ea811a36e59eb3c98fe5994ea97a7c3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp312-cp312-macosx_11_0_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c14f175ad6e3be59af710e121f2122ea9c0f22ca47cd0585c03e6552de29a1e5
MD5 ba52e1af921d071c45dc4db671358845
BLAKE2b-256 40e7e2af47d46d52d1764f63b1b32a7f88957b264143a5d0e46a3b2a250c1e1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: deglib-0.1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 284.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for deglib-0.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2158dabae10570d4f12bd6d90fe34334045c0f7b22e86f409b0167ead547effe
MD5 e5f124bb228f2cc8d0617864d9ca2fc8
BLAKE2b-256 a16f3d7757af4d03e0c30b7c6a0a74bd5764063c3abbe80c2738ed820a2ffd9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp311-cp311-win_amd64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dfd46bf910accc377efa8483a2012d086f128a621a954f88ffb94ed5a85607c2
MD5 7e21b2f64c1fc6a5c956d56fdf654b9b
BLAKE2b-256 d90e14d8d32bbe8e200873693c66fafbd2f539613ca43a2dc7e391ed6cb6d9e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 85d4133597354861cf140fb6c878434fddea7bfb71114fe03f9b753473e3a825
MD5 e6fe8010957ec72315d65782efe5853f
BLAKE2b-256 20b370085a34c13499b23139d1378f0078ea5029484f989d75657f10dedb72aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp311-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 c3069eba68d5aca905e947fa5c42fe2894e9b77235cb954f312dd45bbfc9be34
MD5 7848b88428f1f5e12fc81eed944cbd6f
BLAKE2b-256 01306d25c1b75a97790e23ce2a61d0c6794d6deb2fbe112d155e31204dae63e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp311-cp311-macosx_11_0_x86_64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deglib-0.1.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for deglib-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 93c97a76c4de09c0525e0081f3052ac40de8077fb7cbb43edc8a5c0be56b41a7
MD5 4c19b8286c72f842c06c3865063ec088
BLAKE2b-256 2c787ea5bc57b0995a579ee4d03a3f315c70bc3d814fad03b395a8929d191970

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: BuildAndPublish.yml on Visual-Computing/DynamicExplorationGraph

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page