Skip to content

Scipy Search

Exact SciPy cKDTree search adapter.

ScipyCKDTreeSearcher

Bases: BaseExactSearcher

Exact one-worker SciPy cKDTree search for L2-derived objectives.

Source code in src/vector_search_study/scipy_search.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class ScipyCKDTreeSearcher(BaseExactSearcher):
    """Exact one-worker SciPy cKDTree search for L2-derived objectives."""

    _supported: ClassVar[frozenset[SearchObjective]] = frozenset(
        {SearchObjective.SQUARED_L2, SearchObjective.NORMALIZED_COSINE}
    )

    def __init__(
        self,
        corpus: FloatMatrix,
        *,
        objective: SearchObjective | str,
        leaf_size: int = 16,
    ) -> None:
        """Build a balanced compact cKDTree outside search timing."""
        resolved = _require_supported(objective, self._supported, backend="SciPy cKDTree")
        super().__init__(corpus, objective=resolved)
        spatial = import_optional("scipy.spatial", extra="benchmark-backends")
        self._leaf_size = validate_positive_int(leaf_size, name="leaf_size")
        self._index: Any = spatial.cKDTree(
            self._corpus,
            leafsize=self._leaf_size,
            compact_nodes=True,
            copy_data=True,
            balanced_tree=True,
        )

    @property
    def leaf_size(self) -> int:
        """Return the configured tree leaf size."""
        return self._leaf_size

    def _search_prepared(self, queries: PreparedQueries, k: int) -> SearchResult:
        """Query the exact Euclidean tree with one worker."""
        distances, indices = self._index.query(queries.values, k=k, eps=0.0, p=2.0, workers=1)
        return canonical_result(indices, _euclidean_scores(distances, self.objective))

leaf_size property

leaf_size: int

Return the configured tree leaf size.

__init__

__init__(
    corpus: FloatMatrix,
    *,
    objective: SearchObjective | str,
    leaf_size: int = 16,
) -> None

Build a balanced compact cKDTree outside search timing.

Source code in src/vector_search_study/scipy_search.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(
    self,
    corpus: FloatMatrix,
    *,
    objective: SearchObjective | str,
    leaf_size: int = 16,
) -> None:
    """Build a balanced compact cKDTree outside search timing."""
    resolved = _require_supported(objective, self._supported, backend="SciPy cKDTree")
    super().__init__(corpus, objective=resolved)
    spatial = import_optional("scipy.spatial", extra="benchmark-backends")
    self._leaf_size = validate_positive_int(leaf_size, name="leaf_size")
    self._index: Any = spatial.cKDTree(
        self._corpus,
        leafsize=self._leaf_size,
        compact_nodes=True,
        copy_data=True,
        balanced_tree=True,
    )