API Reference#
Note
These are documented under their compiled-extension names
(ecpfs.ecp.*), which autodoc needs to find their members. Use them as
ecpfs.Index and ecpfs.Builder, the names the package re-exports.
Index#
- class ecpfs.ecp.IndexWrapper(index_path, memory_limit_bytes=Ellipsis)#
An eCP index opened from disk, for searching and inserting.
memory_limit_bytescaps the index data kept in memory, nodes and open queries together, and defaults to 80% of system RAM. Every method exceptset_memory_limit_bytesreleases the GIL while it works, so other Python threads keep running. Use the index as a context manager, or callclose, so unfinished queries are saved for later.- cleanup_persisted_queries_older_than(cutoff_unix_secs)#
Erases every query saved to disk before
cutoff_unix_secs, a Unix timestamp such asdatetime(...).timestamp(). Returns how many were erased.
- close()#
Saves every open query to disk so a later
Indexcan resume it, then closes this one, raising OSError if a save failed. Any other method raises ValueError afterwards. Safe to call more than once.
- get_next_k_items(query_id, k, search_exp, max_increments, exclude_vec)#
Returns the next
k(score, item_id)pairs of a query started withnew_search, searching further if needed. Empty for an unknownquery_id, and raises ValueError afterclose. See Search Parameters for the other arguments.
- insert(embeddings)#
Adds each row of
embeddingsto its nearest leaf and returns the new ids as(start_id, end_id), sorange(start_id, end_id)lists them in row order. Rows must match the index’s dimension, and be unit-length if it was built withis_normalized. Leaves only grow; the tree is never rebalanced.
- new_search(query, k, search_exp, max_increments, exclude_vec)#
Searches for
queryand returns(items, query_id), whereitemsare thekbest(score, item_id)pairs, lowest score first. Passquery_idtoget_next_k_itemsfor more. See Search Parameters for the arguments and what a score means.
- set_memory_limit_bytes(memory_limit_bytes)#
Changes the memory limit without reopening the index. Lowering it below what is cached evicts right away. Meant for occasional use, not per query.
Builder#
- class ecpfs.ecp.BuilderWrapper(index_path, levels, metric, is_normalized=False, memory_limit_bytes=Ellipsis, embedding_dtype=None, rep_chunk_bytes=Ellipsis, node_chunk_bytes=Ellipsis)#
Builds a new eCP index in three steps: create the
Builder, callselect_representativesorselect_representatives_custom, then callbuild. Every method releases the GIL while it works.The index is created at
index_path.levelsis the number of node levels below the root, andis_normalizedshould be set only if every embedding is unit-length.memory_limit_bytesis a target for the build’s memory use rather than a hard cap, and defaults to 80% of system RAM. Leaveembedding_dtypeas None to store each file’s own dtype; a narrower one logs a warning.rep_chunk_bytesis the chunk size for the representative arrays andnode_chunk_bytesthe chunk size for the tree nodes. Measure zarr read speed at a few chunk sizes on your own data before changing them.- build(embeddings_file, fallback_batch_rows, grp_name='embeddings')#
Builds the tree from the dataset
grp_nameinembeddings_file, whose rows get item ids 0, 1, 2, … in order.fallback_batch_rowsworks as inselect_representatives.
- select_representatives(embeddings_file, target_cluster_items, strategy, fallback_batch_rows, grp_name='embeddings')#
Picks the representatives the tree is built from, out of the dataset
grp_nameinembeddings_file(.h5or.zarr).strategyis “offset” (evenly spaced items) or “random”.target_cluster_itemsis the average cluster size to aim for, andfallback_batch_rowsthe chunk size assumed if the file isn’t chunked.
- select_representatives_custom(ids, embeddings)#
Uses your own representatives instead of picking them, such as ones from an external clustering step.
ids[i]is the id of rowiofembeddings.
Metric#
EmbeddingDtype#
- class ecpfs.ecp.EmbeddingDtype#
The type an index stores its embeddings as on disk:
F32,F16,UInt8(0 to 255) orInt8(-128 to 127). Every read widens to f32, so a narrower type saves disk, not memory.- F16 = EmbeddingDtype.F16#
- F32 = EmbeddingDtype.F32#
- Int8 = EmbeddingDtype.Int8#
- UInt8 = EmbeddingDtype.UInt8#
Logging#
See Logging for the level policy, the JSONL record format, and where the file goes.
- ecpfs.ecp.init_logging(log_dir=None, level='info')#
Starts logging this process to a new JSONL file in
log_dir(defaultecp_logs/) and returns the file’s path.levelis “off”, “trace”, “debug”, “info”, “warn” or “error”; “off” creates the file but logs nothing. Only the first call sets logging up; later calls return the same path.