IndexIOMixin#

class scikitplot.annoy.IndexIOMixin[source]#

Mixin adding explicit Annoy-native index file persistence.

The concrete class must provide low-level Annoy save/load methods. This is satisfied either by:

  • inheritance style: the class subclasses Annoy (save/load on self)

  • composition style: the class stores a low-level Annoy instance on self._annoy

Notes

This mixin never pickles the Python object. It delegates persistence to Annoy’s native binary index format.

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

Load a directory bundle produced by save_bundle.

Parameters:
directorystr or os.PathLike

Bundle directory containing manifest_filename and index_filename.

index_filenamestr, default=”index.ann”

Index filename inside directory.

manifest_filenamestr, default=”manifest.json”

Manifest filename inside directory.

prefaultbool or None, default=None

Passed through to load_index.

Returns:
objSelf

A newly constructed object with the index loaded.

Raises:
AttributeError

If the class does not provide from_json (from ManifestMixin).

FileNotFoundError

If either file is missing.

OSError

If files cannot be read or mapped.

RuntimeError

If the index is incompatible.

Parameters:
Return type:

Self

See also

save_bundle
load_index(path, *, prefault=None)[source]#

Load (mmap) an Annoy index file into this object.

Parameters:
pathstr or os.PathLike

Path to a file previously created by save_index or the low-level Annoy.save.

prefaultbool or None, default=None

If None, defer to the low-level object’s configured prefault behavior.

Raises:
FileNotFoundError

If the file does not exist.

OSError

If the file cannot be opened or mapped.

RuntimeError

If the index is incompatible with the current object.

Parameters:
Return type:

None

Notes

Annoy requires that the in-memory object is compatible with the file being loaded (dimension and metric).

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

Save a directory bundle containing both manifest and index file.

This is a convenience for packaging a self-describing Annoy index artifact.

Parameters:
directorystr or os.PathLike

Destination directory. Created if it does not exist.

index_filenamestr, default=”index.ann”

Filename for the Annoy index file inside directory.

manifest_filenamestr, default=”manifest.json”

Filename for the manifest JSON inside directory.

prefaultbool or None, default=None

Passed through to save_index.

Raises:
AttributeError

If the object does not provide to_json (from ManifestMixin).

OSError

If files cannot be written.

Parameters:
Return type:

None

Notes

The manifest is written using an atomic replace (write temp + rename) to avoid partial writes.

save_index(path, *, prefault=None)[source]#

Persist the Annoy index to disk.

Parameters:
pathstr or os.PathLike

Destination path for the Annoy index file.

prefaultbool or None, default=None

If None, defer to the low-level object’s configured prefault behavior.

Raises:
OSError

If the index file cannot be written.

RuntimeError

If the low-level index is not initialized or saving fails.

Parameters:
Return type:

None

See also

load_index