sc_tools.bm — Benchmarking#
Integration and segmentation benchmarking tools.
Integration Benchmark#
Compare batch-correction methods and select the best by batch score.
from sc_tools.bm import run_full_integration_workflow
adata, comparison_df, best_method = run_full_integration_workflow(
adata,
modality="visium",
batch_key="library_id",
methods=["harmony", "combat", "scvi"],
output_dir="results",
)
- sc_tools.bm.integration.run_full_integration_workflow(adata, modality='visium', batch_key='library_id', celltype_key=None, methods=None, output_dir='results', *, subsample_n=None, subsample_fraction=None, use_gpu='auto', max_epochs=200, use_scib='auto', save_intermediates=True)[source]#
Run the full integration benchmark workflow.
Orchestrates: subsample -> benchmark all methods -> save intermediates -> select best (by batch score) -> apply to full dataset.
- Parameters:
adata (
AnnData) – AnnData with raw or normalized data. Modified in place with the winning integration embedding.modality (
str) – Data modality (determines default method set).batch_key (
str) – Batch column inobs.celltype_key (
str|None) – Cell type column (optional; improves scoring but not required).methods (
list[str] |None) – Integration methods to benchmark. IfNone, uses modality defaults.output_dir (
str|Path) – Directory for intermediate outputs.subsample_n (
int|None) – Number of cells to subsample for benchmarking. Default: auto (all if <50k cells, else 50k stratified by batch).subsample_fraction (
float|None) – Fraction of cells to subsample (overrides subsample_n).max_epochs (
int) – Max training epochs for VAE methods.use_scib (
str) – Metric computation backend.save_intermediates (
bool) – If True, save per-method embeddings tooutput_dir/tmp/integration_test/{method}.h5ad.
- Returns:
The AnnData with the best integration applied, the comparison DataFrame, and the name of the selected method.
- Return type:
- sc_tools.bm.integration.run_integration_benchmark(adata, modality='visium', batch_key='library_id', celltype_key=None, methods=None, use_gpu='auto', max_epochs=200, use_scib='auto')[source]#
Run multiple integration methods and benchmark them.
Orchestrates integration across methods appropriate for the given modality, computes quality metrics for each, and returns the AnnData with all embeddings plus a comparison DataFrame.
- Parameters:
adata (
AnnData) – AnnData with raw counts (for VAE methods) or normalized data. Modified in place with embeddings added toobsm.modality (
str) – Data modality (determines default method set and normalization).batch_key (
str) – Column inadata.obsfor batch correction.celltype_key (
str|None) – Column inadata.obswith cell type labels. If provided andscib-metricsis available, theBenchmarkerclass is used.methods (
list[str] |None) – List of method names to run. IfNone, uses modality defaults. Valid names:harmony,bbknn,combat,scanorama,scvi,scanvi,cytovi,pca.max_epochs (
int) – Maximum epochs for VAE methods.use_scib (
str) – Passed to metric computation.
- Returns:
The AnnData with all embeddings, and the comparison DataFrame sorted by
overall_score.- Return type:
tuple[AnnData, pd.DataFrame]
- sc_tools.bm.integration.compute_integration_metrics(adata, embedding_key, batch_key, celltype_key=None, use_scib='auto', resolution=1.0, random_state=0)[source]#
Compute integration quality metrics for one embedding.
- Parameters:
adata (
AnnData) – AnnData object with embeddings inobsm.embedding_key (
str) – Key inadata.obsm(e.g."X_scVI").batch_key (
str) – Column inadata.obswith batch labels.celltype_key (
str|None) – Column inadata.obswith cell type labels. IfNoneor not present inadata.obs, bio conservation metrics are skipped and only batch removal metrics are returned.use_scib (
str) –"auto"(default): use scib-metrics if available, else sklearn."scib": require scib-metrics (error if missing)."sklearn": force sklearn fallbacks.resolution (float)
random_state (int)
- Return type:
- Returns:
Dict with batch removal and bio conservation metric values, all in [0, 1].
When celltype_key is
None, bio metrics are omitted.
- sc_tools.bm.integration.compute_composite_score(metrics, batch_weight=0.2, bio_weight=0.8)[source]#
Compute composite integration score from individual metrics.
- sc_tools.bm.integration.compare_integrations(adata=None, embeddings=None, batch_key='batch', celltype_key=None, bio_key=None, batch_weight=0.2, bio_weight=0.8, include_unintegrated=True, use_scib='auto', subsample_n=None, seed=42, resolution=1.0, random_state=0, embedding_files=None)[source]#
Compare multiple integration methods side-by-side.
- Parameters:
adata (
AnnData|None) – AnnData with multiple embeddings inobsm. Can beNonewhenembedding_filesprovides all methods.embeddings (
dict[str,str] |None) – Dict mapping method name toobsmkey (e.g.{"scVI": "X_scVI", "Harmony": "X_pca_harmony"}).batch_key (
str) – Column inobswith batch labels.celltype_key (
str|None) – Column inobswith cell type labels. IfNoneor not present inadata.obs, bio conservation metrics are skipped.bio_key (
str|None) – Column to use for bio conservation metrics. Defaults tocelltype_keywhen not provided. Allows using any clinically relevant variable (e.g."condition","disease_status").batch_weight (
float) – Weight for batch removal in composite score.bio_weight (
float) – Weight for bio conservation in composite score.include_unintegrated (
bool) – If True and"X_pca"exists, add unintegrated PCA as baseline.use_scib (
str) – Passed tocompute_integration_metrics.subsample_n (
int|None) – If set, subsample to this many cells (stratified bybatch_key) before computing metrics.seed (
int) – Random seed for subsampling reproducibility.resolution (
float) – Leiden clustering resolution for ARI/NMI bio conservation metrics.embedding_files (
dict[str,str] |None) – Dict mapping method name to h5ad file path. Embeddings are loaded via h5py without reading the full AnnData, enabling benchmarking of datasets too large to fit in memory.random_state (int)
- Return type:
DataFrame with rows=methods, sorted by
overall_scoredescending.
Segmentation Benchmark#
Compare cell segmentation methods with panoptic quality, boundary F1, and cell-type preservation metrics.
- sc_tools.bm.segmentation.compute_segmentation_accuracy(pred, gt, iou_thresholds=(0.5, 0.75))[source]#
Compute segmentation accuracy metrics.
- sc_tools.bm.segmentation.score_segmentation(mask, intensity_image=None, gt_mask=None, marker_names=None)[source]#
Run all applicable metrics and return raw results.
- Parameters:
- Return type:
- Returns:
Dict with keys
morphology,spatial_coherence,size_distribution, and optionallymarker_quality,detection,accuracy.
- sc_tools.bm.segment.run_cellpose(image, nuclear_channels=None, membrane_channels=None, nuclear_idx=1, cytoplasm_idx=2, model_type='cyto2', diameter=None, flow_threshold=0.4, cellprob_threshold=0.0, gpu=False)[source]#
Run Cellpose segmentation.
- Parameters:
image (
ndarray) – Either a probability map(H, W, C)from Ilastik (e.g. 3 channels: background, nucleus, cytoplasm), or a multi-channel intensity TIFF(C, H, W). Detected automatically from shape.nuclear_channels (
list[int] |None) – For(C, H, W)input: indices of nuclear channels. Ignored for(H, W, C)probability maps.membrane_channels (
list[int] |None) – For(C, H, W)input: indices of membrane channels. Ignored for(H, W, C)probability maps.nuclear_idx (
int) – For(H, W, C)probability maps: index of the nuclear channel (default 1).cytoplasm_idx (
int) – For(H, W, C)probability maps: index of the cytoplasm channel (default 2).model_type (
str) – Cellpose model type (default"cyto2").diameter (
float|None) – Expected cell diameter in pixels. None = auto-estimate.flow_threshold (
float) – Flow error threshold for Cellpose.cellprob_threshold (
float) – Cell probability threshold for Cellpose.gpu (
bool) – Whether to use GPU.
- Return type:
Labeled segmentation mask, shape
(H, W), dtype uint32.
- sc_tools.bm.segment.run_stardist(image, nuclear_channels=None, nuclear_idx=1, model_name='2D_versatile_fluo', prob_thresh=None, nms_thresh=None, scale=None)[source]#
Run StarDist segmentation.
- Parameters:
image (
ndarray) – Either a probability map(H, W, C)from Ilastik (nuclear channel atnuclear_idx), or a multi-channel intensity TIFF(C, H, W)(nuclear channels atnuclear_channels), or a 2D nuclear image(H, W).nuclear_channels (
list[int] |None) – For(C, H, W)input: indices of nuclear channels.nuclear_idx (
int) – For(H, W, C)probability maps: index of the nuclear channel (default 1).model_name (
str) – StarDist pretrained model name (default"2D_versatile_fluo").prob_thresh (
float|None) – Probability threshold. None = model default.nms_thresh (
float|None) – Non-maximum suppression threshold. None = model default.scale (
float|None) – Scale factor for the image. None = no rescaling.
- Return type:
Labeled segmentation mask, shape
(H, W), dtype uint32.
- sc_tools.bm.segment.run_deepcell(image, nuclear_channels=None, membrane_channels=None, nuclear_idx=1, cytoplasm_idx=2, compartment='whole-cell', image_mpp=1.0, postprocess_kwargs=None)[source]#
Run DeepCell Mesmer segmentation.
Thin re-export of
sc_tools.bm.deepcell_runner.run_deepcell. See that module for full documentation.
Mask I/O#
- sc_tools.bm.mask_io.load_mask(path, format='auto')[source]#
Load a segmentation mask, auto-detecting format from extension.
- sc_tools.bm.mask_io.load_cellpose_mask(path)[source]#
Load a Cellpose segmentation mask from
*_seg.npy.Cellpose saves a dict with key
"masks"containing the labeled array. Also handles plain labeled arrays saved directly.
- sc_tools.bm.mask_io.load_stardist_mask(path)[source]#
Load a StarDist label image from
.tif/.tiffor.npz.For
.npzfiles, expects a"labels"key.