API

Toolkit functions

Preprocessing

Functions

generate_gene_df(ad, house_keeping_genes[, ...])

Generates a DataFrame with the top 50 genes for each observation in an AnnData object.

segment_patches(img_array, coord, patch_dir)

Extracts small image patches centered at specified coordinates and saves them as individual PNG files.

get_library_id(adata)

Retrieves the library ID from the AnnData object, assuming it contains spatial data.

get_scalefactors(adata[, library_id])

Retrieves the scalefactors from the AnnData object for a given library ID.

get_spot_diameter_in_pixels(adata[, library_id])

Retrieves the spot diameter in pixels from the AnnData object's scalefactors for a given library ID.

prepare_data_for_alignment(data_path[, ...])

Prepares data for alignment by reading an AnnData object and preparing the high-resolution tissue image.

load_data_for_annotation(st_data_path, json_path)

Loads spatial transcriptomics (ST) data from an .h5ad file and prepares it for annotation.

Utilities

Functions

load_model(model_path, device)

Load pretrained OmiCLIP (COCA ViT‑L‑14) model, its image preprocess, and tokenizer.

encode_images(model, preprocess, ...)

Batch–encode a list of image file paths into L2‑normalized embeddings.

encode_texts(model, tokenizer, texts, device)

Batch–encode a list of strings into L2‑normalized embeddings.

encode_text_df(model, tokenizer, df, ...)

Encodes an entire DataFrame column into (N, D) embeddings.

Loki Align

Functions

apply_homography(coordinates, H)

Apply a 3x3 homography matrix to 2D coordinates.

align_tissue(ad_tar_coor, ad_src_coor, ...)

Aligns the source coordinates to the target coordinates using Coherent Point Drift (CPD) registration, and applies a homography transformation to warp the source coordinates accordingly.

find_homography_translation_rotation(...)

Find the homography between two sets of coordinates with only translation and rotation.

Classes

DeformableRegistration([alpha, beta, ...])

Deformable registration.

EMRegistration(X, Y[, sigma2, ...])

Expectation maximization point cloud registration.

Loki Annotate

Functions

annotate_with_bulk(img_features, bulk_features)

Annotates tissue image with similarity scores between image features and bulk RNA-seq features.

annotate_with_marker_genes(classes, ...)

Annotates tissue image with similarity scores between image features and marker gene features.

load_image_annotation(image_path)

Loads an image with annotation.

Loki Decompose

Functions

generate_feature_ad(ad_expr, feature_path[, sc])

Generates an AnnData object with OmiCLIP text or image embeddings.

cell_type_decompose(sc_ad, st_ad[, ...])

Performs cell type decomposition on spatial data (ST or image) with single-cell data .

Loki Retrieve

Functions

retrieve_st_by_image(image_embeddings, ...)

Retrieves the top-k most similar ST based on the similarity between ST embeddings and image embeddings.

Loki PredEx

Functions

predict_st_gene_expr(image_text_similarity, ...)

Predicts ST gene expression by H&E image.

Plotting

Functions

plot_alignment(ad_tar_coor, ad_src_coor, ...)

Optimized plot: target, source, and aligned coordinates with titles.

plot_alignment_with_img(ad_tar_coor, ...[, s])

Optimized plot with images in the background and subplot titles.

plot_img_with_annotation(fullres_img, ...)

Plots image with polygons.

plot_annotation_heatmap(st_ad, roi_polygon, ...)

Plots tissue type annotation heatmap.