Skip to content

Benchmark the ten methods

This tutorial runs all ten methods on sample 151509 and asks how close each one gets to the reference domain number (reference k).

Summary

import kdomainbench as kdb
h5ad = "data/D3_151509_5k.h5ad"

results = kdb.run_all(h5ad, kmax=30, device="cpu", out_root="bench/")
summary = kdb.load_results("bench/")
kdb.k_accuracy(summary)
kdb.pl.plot_k_estimates(summary)
kdb.pl.plot_spatial_grid(results, h5ad)

1. The data

D3_151509_5k.h5ad is sample 151509 (Example data). Its reference annotation has 7 domains, six cortical layers and white matter: k = 7.

import kdomainbench as kdb

h5ad = "data/D3_151509_5k.h5ad"
reference = kdb.reference_annotation(h5ad).to_frame("cluster")
kdb.pl.plot_spatial(reference, h5ad, title="Reference annotation (k = 7)", legend=True)

Reference annotation of sample 151509: six cortical layers and white matter

2. Run every method

results = kdb.run_all(h5ad, kmax=30, device="cpu", out_root="bench/")

run_all runs all ten with kmax = 30 and otherwise as their method scripts set them.

3. Estimated k and accuracy

summary = kdb.load_results("bench/")
best_first = kdb.k_accuracy(summary).index      # smallest k error first, then highest ARI
summary.set_index("method").loc[best_first, ["k_estimated", "k_error", "ARI", "NMI"]]
Method Estimated k k error ARI NMI
PRECAST 5 -2 0.26 0.38
DeepST 4 -3 0.54 0.64
SpaGCN 4 -3 0.45 0.57
BayesSpace 4 -3 0.37 0.58
SpatialDE2 10 +3 0.36 0.53
SC.MEB 10 +3 0.34 0.54
iSC.MEB 3 -4 0.38 0.52
CellCharter 2 -5 0.37 0.46
BNPSpace 2 -5 0.05 0.16
DR.SC 15 +8 0.23 0.48

The methods are sorted by the size of their k error (estimated k − reference k), then by ARI. ARI and NMI compare the domains with the annotated ones (Evaluate a run). The raw numbers are in summary_5k.csv.

kdb.pl.plot_k_estimates(summary)

Estimated k of each method on sample 151509, with the reference k of 7 as a dashed line

Blue dots are below the reference k and red ones above. A dot on it would be green.

4. The domains on the tissue

fig = kdb.pl.plot_spatial_grid(results, h5ad, ncols=5)

Spatial domains of the ten methods on sample 151509

Compare each map with the reference annotation above. Colors follow the sorted label names, so the same color in two maps does not mean the same domain.

5. The twelve metrics

kdb.metrics scores a run with the twelve metrics of the paper, and kdb.pl.plot_metrics draws them:

for run in summary.loc[summary["success"], "out_dir"]:
    kdb.metrics(run)                      # saved as metric.csv in the run folder
summary = kdb.load_results("bench/")      # read again, now with the metrics
kdb.pl.plot_metrics(summary)

The twelve metrics of the ten methods on sample 151509 as a matrix of bars

A darker bar is a better rank among the ten methods (Evaluate a run explains the figure).

From the shell

terminal
kdomainbench run -m all -i data/D3_151509_5k.h5ad -o bench/
kdomainbench metrics bench/
kdomainbench summarize bench/ --csv summary.csv --plot k.png

The first command runs the methods one after another, and the second needs the metric packages (Installation).