IndexIOMixin#
- class scikitplot.annoy.IndexIOMixin[source]#
Mixin adding explicit Annoy-native persistence helpers.
The concrete class must provide low-level Annoy methods, typically from the C-extension backend:
save(path, prefault=...)load(path, prefault=...)serialize() -> bytes-likedeserialize(data: bytes-like, prefault=...)
Notes
Methods in this mixin acquire a per-instance lock if one is available.
save_indexdefaults to Annoy.savesave_bundle/load_bundlerequireto_json/from_json(compose withMetaMixin).
- classmethod from_bytes(data, *, f=None, metric=None, prefault=None)[source]#
Construct a new index and load it from serialized bytes.
- Parameters:
- data
Bytes produced by
to_bytes(backendserialize).- f
Vector dimension for construction.
- metric
Metric name for construction.
- prefault
Forwarded to the backend
deserializeif supported.
- Returns:
- index
Newly constructed index with the data loaded.
- Raises:
- TypeError
If
datais not bytes-like.- ValueError
If
formetricis invalid.- AttributeError
If the backend does not provide
deserialize.
- Parameters:
- Return type:
Notes
Portable blobs add a small header (version, ABI sizes, endianness, metric, f) to ensure incompatible binaries fail loudly and safely. They are not a cross-architecture wire format; the payload remains Annoy’s native snapshot.
For
dataif fedto_bytes(format='native') required params ``f`,metric.
- classmethod load_bundle(manifest_filename='manifest.json', index_filename='index.ann', *, prefault=None)[source]#
Load a directory bundle created by
save_bundle.- Parameters:
- manifest_filename
Filename for the metadata manifest inside the directory.
- index_filename
Filename for the Annoy index inside the directory.
- prefault
Forwarded to
load_index.
- Returns:
- index
Newly constructed index.
- Raises:
- AttributeError
If
from_jsonis not available (compose withMetaMixin).- TypeError
If
from_jsonreturns an unexpected type.- OSError
On filesystem failures.
- Parameters:
- Return type:
- classmethod load_index(f, metric, path, *, prefault=None)[source]#
Load (mmap) an Annoy index file into this object.
- Parameters:
- f
Vector dimension for construction.
- metric
Metric name for construction.
- pathstr or os.PathLike
Path to a file previously created by
save_indexor the backendsave.- prefault
Forwarded to the backend. If
None, the backend default is used.
- Raises:
- AttributeError
If the backend does not provide
load(path, prefault=...).- OSError
If loading fails (backend or filesystem).
- Parameters:
- Return type:
- save_bundle(manifest_filename='manifest.json', index_filename='index.ann', *, prefault=None)[source]#
Save a directory bundle containing metadata + the index file.
The bundle contains: -
manifest.json: metadata payload produced byto_json-index.ann: Annoy index produced bysave_index- Parameters:
- manifest_filename
Filename for the metadata manifest inside the directory.
- index_filename
Filename for the Annoy index inside the directory.
- prefault
Forwarded to
save_index.
- Raises:
- AttributeError
If
to_jsonis not available (compose withMetaMixin).- OSError
On filesystem failures.
- Parameters:
- Return type:
- save_index(path, *, prefault=None)[source]#
Persist the Annoy index to disk.
- Parameters:
- pathstr or os.PathLike
Destination path for the Annoy index file.
- prefault
Forwarded to the backend. If
None, the backend default is used.
- Raises:
- AttributeError
If the backend does not provide
save(path, prefault=...).- OSError
For filesystem-level failures.
- Parameters:
- Return type:
- to_bytes(format=None)[source]#
Serialize the built index to bytes (backend
serialize).- Parameters:
- format{“native”, “portable”, “canonical”} or None, optional, default=None
Serialization format. If
Noneused"canonical"“native” (legacy): raw Annoy memory snapshot. Fastest, but only compatible when the ABI matches exactly.
“portable”: prepend a small compatibility header (version, endianness, sizeof checks, metric, f) so deserialization fails loudly on mismatches.
“canonical”: rebuildable wire format storing item vectors + build parameters. Portable across ABIs (within IEEE-754 float32) and restores by rebuilding trees deterministically.
- Returns:
- data
Serialized index bytes.
- Raises:
- AttributeError
If the backend does not provide
serialize.- RuntimeError
If serialization fails.
- TypeError
If the backend returns non-bytes-like data.
- Return type:
Notes
“Portable” blobs are the native snapshot with additional compatibility guards. They are not a cross-architecture wire format.
“Canonical” blobs trade load time for portability: deserialization rebuilds the index with
n_jobs=1for deterministic reconstruction.