Index (cython) python-api benchmark with examples#

An example showing the Index class.

import pytest
import numpy as np
import random; random.seed(0)
from pprint import pprint

# from annoy import Annoy, AnnoyIndex
# from scikitplot.cexternals._annoy import Annoy, AnnoyIndex
# from scikitplot.annoy import Annoy, AnnoyIndex, Index
from scikitplot.annoy._annoy import Index

# print(Index.__doc__)
import subprocess
import sys
import inspect
import joblib
from pathlib import Path

def get_current_script_dir() -> Path:
    """
    Returns the directory of the current script in a robust way.

    Returns
    -------
    Path
        Absolute directory path.

    Raises
    ------
    RuntimeError
        If location cannot be determined.
    """
    # Case 1: normal Python execution
    if "__file__" in globals():
        return Path(__file__).resolve().parent

    # Case 2: fallback for Sphinx-gallery / exec environments
    frame = inspect.currentframe()
    if frame is not None:
        file = inspect.getfile(frame)
        return Path(file).resolve().parent

    raise RuntimeError("Cannot determine script directory.")

BASE_DIR = get_current_script_dir()
PROJECT_ROOT = BASE_DIR.parents[2]

# pytest ../../../scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table
cmd = [
    sys.executable,
    "-m",
    "pytest",
    # "../../../scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table",
    "scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table",
    "-vv",
]

result = subprocess.run(
    cmd,
    cwd=PROJECT_ROOT,
    check=False,
    capture_output=True,
    text=True,
)

print("Return code:", result.returncode)
print(result.stdout)
print(result.stderr)
Return code: 4

2026-03-20 19:50:23.323484: W scikitplot 133168083962752 __init__.py:82:<module>] ⚠︎ BOOM! :: Error importing scikitplot: you cannot import scikitplot while being in scikitplot source directory; please exit the scikitplot source tree first and relaunch your Python interpreter.
ImportError while loading conftest '/home/circleci/repo/scikitplot/conftest.py'.
scikitplot/conftest.py:31: in <module>
    from ._lib import _pep440
scikitplot/_lib/__init__.py:45: in <module>
    from . import _ccallback_c
E   ImportError: cannot import name '_ccallback_c' from partially initialized module 'scikitplot._lib' (most likely due to a circular import) (/home/circleci/repo/scikitplot/_lib/__init__.py)

Tags: model-workflow: vector-db level: beginner purpose: showcase

Total running time of the script: (0 minutes 3.787 seconds)

Related examples

Index (cython) python-api with examples

Index (cython) python-api with examples

visualkeras: Vector Index DB

visualkeras: Vector Index DB

annoy.Annoy legacy c-api with examples

annoy.Annoy legacy c-api with examples

annoy.Index python-api with examples

annoy.Index python-api with examples

Gallery generated by Sphinx-Gallery