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-like

  • deserialize(data: bytes-like, prefault=...)

Notes

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 (backend serialize).

f

Vector dimension for construction.

metric

Metric name for construction.

prefault

Forwarded to the backend deserialize if supported.

Returns:
index

Newly constructed index with the data loaded.

Raises:
TypeError

If data is not bytes-like.

ValueError

If f or metric is invalid.

TypeError

If the backend does not provide deserialize.

Parameters:
Return type:

Self

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 data if fed to_bytes(format='native') required params ``f`, metric.

classmethod load_bundle(directory, *, manifest_filename='manifest.json', index_filename=None, prefault=None)[source]#

Load a bundle published by save_bundle.

Parameters:
directorystr or path-like

Bundle directory.

manifest_filenamestr, optional

Name of the manifest inside the bundle.

index_filenamestr or None, optional

Name of the index member inside the bundle. None (the default) takes the name the manifest records, which is the one the bundle was saved with; a bundle whose manifest records none has index.ann.

prefaultbool or None, optional

Forwarded to load_index.

Returns:
Self

An index with its vectors loaded and ready to query.

Raises:
OSError

If a member is missing or unreadable.

ValueError

If a member name is not a plain file name, or the manifest is of a bundle format this version does not read.

TypeError

If the manifest is not a JSON object.

Parameters:
Return type:

Self

Notes

User. A bundle is loaded from where it is. Nothing in the manifest can make this method read a file outside directory.

Developer. This previously did not load the index at all: the load_index call was commented out and both index_filename and prefault were marked unused, so the vectors arrived only as a side effect of the manifest carrying an absolute path recorded at save time. A bundle that had been moved therefore failed, and naming a different index file had no effect whatsoever. Members are now resolved relative to the bundle, which is what makes it relocatable, and the index is loaded here rather than by accident.

The manifest is read once, as data. Its description of the index (dimension, metric, parameters) goes through from_metadata with load=False, so a location recorded by an older writer is never opened; such a location is reported at INFO and otherwise ignored.

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_index or the backend save.

prefault

Forwarded to the backend. If None, the backend default is used.

Raises:
TypeError

If the backend does not provide load(path, prefault=...).

OSError

If loading fails (backend or filesystem).

Parameters:
Return type:

Self

save_bundle(directory, *, manifest_filename='manifest.json', index_filename='index.ann', prefault=None)[source]#

Publish a directory bundle holding the metadata and the index.

Parameters:
directorystr or path-like

Bundle directory. Created if absent, replaced if present.

manifest_filename, index_filenamestr, optional

Member names inside the bundle. They are never resolved against the process working directory.

prefaultbool or None, optional

Forwarded to save_index.

Returns:
list of str

Absolute paths of the published members, manifest first.

Raises:
OSError

If the bundle could not be written. Nothing is published in that case: a failed save leaves any previous bundle exactly as it was.

Parameters:
Return type:

list[str]

Notes

User. A bundle is self-contained and relocatable: move it, copy it, or publish it, and load_bundle reads it from wherever it is.

After a successful save this index is backed by the published index file (on_disk_path names it). After a failed save it holds the same vectors and trees as before, in memory.

Developer. This previously took two filenames and no directory, so the defaults resolved against the process working directory and two callers using them overwrote each other. It also wrote the index and then the manifest in place, so a failure between the two left an index no loader could find. Both are fixed the same way the corpus artifact fixes them: build a candidate, then swap, so nothing is destroyed before a complete replacement exists.

The backend’s save memory-maps the file it wrote, so once the index member exists the index is backed by a file inside the candidate. POSIX lets a directory be renamed or removed with a mapped file in it; Windows does not (PermissionError: [WinError 5] on the swap, seen in the Windows job of run 37555382019). The order below works on both, so there is one code path:

  1. write both members into the candidate;

  2. release the mapping (unload), swap the directories, map the published index (load);

  3. on any failure after step 1, take the index back into memory (serialize, unload, deserialize) before the candidate is removed, so the object keeps its contents and nothing of the candidate is in use when it is deleted.

The manifest records no file-system location and names the index member (see _bundle_manifest): it describes the bundle, wherever the bundle is. Steps and failures are logged on scikitplot.annoy._mixins._io: DEBUG for each step, WARNING when a failed publication is undone.

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:
TypeError

If the backend does not provide save(path, prefault=...).

OSError

For filesystem-level failures.

Parameters:
  • path (str | PathLike[str])

  • prefault (bool | None)

Return type:

Self

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 None used "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:
TypeError

If the backend does not provide serialize.

RuntimeError

If serialization fails.

TypeError

If the backend returns non-bytes-like data.

Return type:

bytes

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=1 for deterministic reconstruction.