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.2.tar.gz (72.4 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.2-cp312-cp312-win_amd64.whl (284.9 kB view details)

Uploaded CPython 3.12Windows x86-64

deglib-0.1.2-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.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (551.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

deglib-0.1.2-cp312-cp312-macosx_11_0_x86_64.whl (279.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ x86-64

deglib-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (263.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

deglib-0.1.2-cp311-cp311-win_amd64.whl (284.4 kB view details)

Uploaded CPython 3.11Windows x86-64

deglib-0.1.2-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.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (551.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

deglib-0.1.2-cp311-cp311-macosx_11_0_x86_64.whl (278.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ x86-64

deglib-0.1.2-cp311-cp311-macosx_11_0_arm64.whl (262.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: deglib-0.1.2.tar.gz
  • Upload date:
  • Size: 72.4 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.2.tar.gz
Algorithm Hash digest
SHA256 f162cb0e07bf89afb111ebb76cf42d3cd4936555aa698d10e9d0bcc6ecc98a2c
MD5 11ad1555779ee0be04f8afc06d50bd95
BLAKE2b-256 0753f0805a00377ffd40093c5712dad020e1fbdc73c35459b2c9b62b6c78df33

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2.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.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: deglib-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 284.9 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c23fbfdfdaaff0ac76e82e99feed370069f4be99794a3ac136c1365e7d2072f1
MD5 bba16fde00f81496698285eb6ab2171d
BLAKE2b-256 ad465b6763b0c1ab0c3c1a65346bad109df3c5c00c652da584fb0716b9ae52c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fe83342ca47262cbb461afceb0a628331f3661c5ce1074844abbf2e9e0ca93ac
MD5 47e9f7665ec76661b6bf23e11ddf3f4e
BLAKE2b-256 439cf9ae69687be75df4ce71d338e2235613bff3f9e2c0bde238999eb9eed940

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 94b03972f9dfc0614a9a6877d38a9507b2c9b104bf4df61c33d8c4ed9c12b5a8
MD5 cb6b1d392ffe04eb476ead874b212bd8
BLAKE2b-256 de61796c1bfbeee2f079f0a220c6ba7090c47ac60f803cdb208ea74440312b4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp312-cp312-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 e893f7ef5cc72b37e6586270f541fd897a1b238dc647f984668b1eb01587a38c
MD5 d71d4401ccddf0c6285b743d70489060
BLAKE2b-256 5147d36b95822d4d122d2990a33ecf378955566128d14d0d9b7050b2ca63ca34

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 71f2692efd63055ba01b2e84660b1c6fe9ffe64e462cc2e37a17e64a750af6f1
MD5 bb360aad9a2f352327b88671ca931857
BLAKE2b-256 052b0d343666b39ea8ae0e3bf7399c84b18e3bc577673af1b5731201c535640b

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: deglib-0.1.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 284.4 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2c421652c2f96e2b4ed18dd2fddb7bfc3165de2987a1fdb37f7e6cd100fee38b
MD5 bbc64af734c80ac10b2dbecf9148b4e0
BLAKE2b-256 c77402bc74df03715945d31bf714118d7441f9d8df8d462285095eacdee71df8

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b5a75fd9dc71ba3523781916d550dd4e851368c3a324804bf316c485378b0c33
MD5 f0a3d8aec6447f7b5e52601dfc4b5aef
BLAKE2b-256 ec58b4afd54230b00297347be319e7f8713be2add57994492b94eefd98e72ab5

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 acded237c3eddc04523750d59c252a553b9d7c513c2013b96036dd3b4b6b0854
MD5 cbbc9c9c9d414285c550add1d224288e
BLAKE2b-256 59b3eade86d85e81a2148abd3aa4bf9ebed8f09c36f2f858be7325da0ef3b493

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp311-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 98cee0736af70ab80a0a5da752dd4ab3e48733a071838894a8f53deabf8c0450
MD5 ff1f36e8cb35a9b236d1726439b5701f
BLAKE2b-256 2aa04405c08b8aa362ca37fc8dcebf02e6e73b648acbb8387313a08258e92e5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for deglib-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 059b5647aaf0267479abfc5614178cd811fbc51ac239e6ab62ee3501ad6c6b2a
MD5 cc626a4c2b8dd8b084fac2ffa2e5417b
BLAKE2b-256 3fdbf1898f740415bbc10aff422723fad09adb338d5e76bec5516c15b7a35fd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for deglib-0.1.2-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