[docs]classHnswParameters(Parameters):"""Parameters for the hierarchical navigable small worlds (HNSW) algorithm, see `here <https://github.com/nmslib/hnswlib>`_ for details. """
[docs]def__init__(self,num_links:int=16,ef_construction:int=200,ef_search:int=10,distance:Literal["Euclidean","Manhattan","Cosine"]="Euclidean",):""" Args: num_links: Number of bi-directional links to create per observation during index construction. Larger values improve accuracy at the expense of speed and memory usage. ef_construction: Size of the dynamic list for index generation. Larger values improve the quality of the index at the expense of time. ef_search Size of the dynamic list for neighbor searching. Larger values improve accuracy at the expense of a slower search. distance: Distance metric for index construction and search. """self.num_links=num_linksself.ef_construction=ef_constructionself.ef_search=ef_searchself.distance=distance
@propertydefdistance(self)->str:"""Distance metric, see :meth:`~__init__()`."""returnself._distance@distance.setterdefdistance(self,distance:str):""" Args: distance: Distance metric, see :meth:`~__init__()`. """ifdistancenotin["Euclidean","Manhattan","Cosine"]:raiseValueError("unsupported 'distance'")self._distance=distance@propertydefnum_links(self)->int:"""Number of links, see :meth:`~__init__()`."""returnself._num_links@num_links.setterdefnum_links(self,num_links:int):""" Args: num_links: Number of links, see :meth:`~__init__()`. """ifnum_links<1:raiseValueError("'num_links' should be a positive integer")self._num_links=num_links@propertydefef_construction(self)->int:"""Size of the dynamic list during index construction, see :meth:`~__init__()`."""returnself._ef_construction@ef_construction.setterdefef_construction(self,ef_construction:int):""" Args: ef_construction: Size of the dynamic list during index construction, see :meth:`~__init__()`. """ifef_construction<1:raiseValueError("'ef_construction' should be a positive integer")self._ef_construction=ef_construction@propertydefef_search(self)->int:"""Size of the dynamic list during search, see :meth:`~__init__()`."""returnself._ef_search@ef_search.setterdefef_search(self,ef_search:int):""" Args: ef_search: Size of the dynamic list during search, see :meth:`~__init__()`. """ifef_search<1:raiseValueError("'ef_search' should be a positive integer")self._ef_search=ef_search
[docs]classHnswIndex(GenericIndex):"""A prebuilt index for the hierarchical navigable small worlds (HNSW) algorithm, created by :py:func:`~knncolle.define_builder.define_builder` with a :py:class:`~knncolle.hnsw.HnswParameters` object. """
[docs]def__init__(self,ptr):""" Args: ptr: Address of a ``knncolle_py::WrappedPrebuilt`` containing a HNSW search index, allocated in C++. """super().__init__(ptr)