Skip to content

Figures

kdb.pl draws the benchmark's figures with matplotlib. Each function returns its axes or figure. These calls need only the kdomainbench package, not the method environments.

plot_k_estimates

plot_k_estimates(summary, ax=None, k_reference=None)

Plot the estimated k of each method against the reference domain number.

One row per method and one dot per estimate, laid out as Fig. 2b and 2c of the paper. A dashed line marks the reference domain number. A dot is blue when the method returned fewer domains than the reference, green when it returned as many, and red when it returned more. Methods are sorted by the median of their dots.

PARAMETER DESCRIPTION
summary

The table from load_results.

TYPE: DataFrame

ax

Axes to draw on. Default: a new figure.

TYPE: Axes DEFAULT: None

k_reference

Reference domain number of every run, for a file without an annotation. Default: the k_reference column of summary.

TYPE: int DEFAULT: None

RETURNS DESCRIPTION
Axes

The plot. ax.figure.savefig(..., bbox_inches="tight") saves it with its legend.

RAISES DESCRIPTION
TypeError

summary is not a table.

KeyError

summary lacks a column of the load_results table.

ValueError

summary has no successful run.

Notes
  • Runs of a method at the same place on the x axis share one dot, and its area gives their number.
  • When the runs have different reference domain numbers (several datasets), the x axis is the k error, estimated k − reference k.
  • Runs without a reference annotation are left out when other runs have one. When no run has one, the dots are gray and there is no line.
  • The paper's panels also shade a dot by the size of its error. Here the position on the x axis gives the size, and the color only the direction.

Examples:

>>> summary = kdb.load_results("kdb_results")
>>> ax = kdb.pl.plot_k_estimates(summary)
>>> ax.figure.savefig("k_estimates.png", dpi=150, bbox_inches="tight")

plot_metrics

plot_metrics(summary, ax=None)

Plot the metrics of each method as a matrix of bars.

One row per method and one column per metric, laid out as Fig. 2f of the paper. The length of a bar is the value of the metric, on the metric's own axis below the column. The fill is the rank of the method among the methods for that metric: the darker, the better. The arrow above a column says whether higher or lower is better.

PARAMETER DESCRIPTION
summary

The table from load_results. The columns of the twelve metrics are there for the runs scored with metrics. A method whose runs were not scored has n/a in the other columns, and a table without any scored run shows ARI and NMI only.

TYPE: DataFrame

ax

Axes to draw on. Default: a new figure.

TYPE: Axes DEFAULT: None

RETURNS DESCRIPTION
Axes

The plot. ax.figure.savefig(..., bbox_inches="tight") saves it.

RAISES DESCRIPTION
TypeError

summary is not a table.

KeyError

summary lacks a column of the load_results table.

ValueError

summary has no successful run, or none of its runs has a metric value.

Notes
  • With several runs per method, a bar is the mean over the runs that have a value, and the whisker is their standard deviation. Filter summary first to compare within one dataset or device. In the paper the datasets of each data source are averaged first, and a bar is the mean over the five sources.
  • An axis runs from 0 to a round number above the highest value, and from below 0 when a value is negative. The axes of the paper's panel are set by hand and do not all start at 0.
  • Methods are listed in the order of their first successful run in summary.
  • Methods with the same value share the better rank. n/a marks a metric without a value, such as SCS of a run with one domain.

Examples:

>>> summary = kdb.load_results("kdb_results")
>>> ax = kdb.pl.plot_metrics(summary)
>>> ax.figure.savefig("metrics.png", dpi=150, bbox_inches="tight")

plot_runtime

plot_runtime(summary, metric='wall_time_s', ax=None, log=True)

Plot run time or memory per method, CPU and GPU runs side by side.

PARAMETER DESCRIPTION
summary

The table from load_results.

TYPE: DataFrame

metric

"wall_time_s" (run time, s), "peak_rss_mb" (peak host memory, MB), or another numeric column of summary.

TYPE: str DEFAULT: 'wall_time_s'

ax

Axes to draw on. Default: a new figure.

TYPE: Axes DEFAULT: None

log

Log-scale y axis.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
Axes

The plot.

RAISES DESCRIPTION
TypeError

summary is not a table.

KeyError

summary has no column metric.

ValueError

summary has no successful run, or none with a value in metric.

Notes
  • A bar is the mean over the runs of a method on one device (device_used). A method without GPU code has only a CPU bar, also for runs made with device="gpu".
  • Methods are sorted by their mean over the devices, the smallest first.

Examples:

>>> summary = kdb.load_results("kdb_results")
>>> kdb.pl.plot_runtime(summary)
>>> kdb.pl.plot_runtime(summary, metric="peak_rss_mb", log=False)

plot_spatial

plot_spatial(clusters, h5ad, ax=None, title=None, s=8, spatial_key='spatial', legend=False)

Plot the spatial domains of one run on the tissue.

PARAMETER DESCRIPTION
clusters

Labels indexed by spot: what run returns, a Series, or a run folder.

TYPE: DataFrame or Series or path

h5ad

The input file or the data in memory. Its obsm[spatial_key] gives the spot positions.

TYPE: str or path or AnnData

ax

Axes to draw on. Default: a new figure.

TYPE: Axes DEFAULT: None

title

Default "k = <number of domains>".

TYPE: str DEFAULT: None

s

Marker size.

TYPE: float DEFAULT: 8

spatial_key

Key of the coordinates in obsm.

TYPE: str DEFAULT: 'spatial'

legend

Name the domains in a legend beside the map.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Axes

The map.

RAISES DESCRIPTION
FileNotFoundError

The run folder or the .h5ad file is missing.

KeyError

obsm has no spatial_key.

TypeError

clusters holds several runs. plot_spatial_grid draws those.

ValueError

No label belongs to a spot of the file: the two do not use the same barcodes.

Examples:

>>> kdb.pl.plot_spatial("kdb_results/SpaGCN/D3_151509_5k_cpu", "data/D3_151509_5k.h5ad")

plot_spatial_grid

plot_spatial_grid(results, h5ad, ncols=5, s=4, spatial_key='spatial')

Plot the spatial domains of several runs in a grid.

PARAMETER DESCRIPTION
results

Name -> clusters or run folder, as run_all returns. For finished runs: {name: folder}.

TYPE: dict

h5ad

The input file or the data in memory.

TYPE: str or path or AnnData

ncols

Maps per row.

TYPE: int DEFAULT: 5

s

Marker size.

TYPE: float DEFAULT: 4

spatial_key

Key of the coordinates in obsm.

TYPE: str DEFAULT: 'spatial'

RETURNS DESCRIPTION
Figure

The grid. fig.savefig(...) saves it.

RAISES DESCRIPTION
FileNotFoundError

A run folder or the .h5ad file is missing.

KeyError

obsm has no spatial_key.

TypeError

results is not a dict of runs.

ValueError

results is empty, or the labels of a run do not use the barcodes of the file.

Examples:

>>> results = kdb.run_all("data/D3_151509_5k.h5ad")
>>> kdb.pl.plot_spatial_grid(results, "data/D3_151509_5k.h5ad").savefig("domains.png")

For finished runs:

>>> summary = kdb.load_results("kdb_results")
>>> ok = summary[summary["success"]]
>>> runs = dict(zip(ok["method"], ok["out_dir"]))
>>> fig = kdb.pl.plot_spatial_grid(runs, "data/D3_151509_5k.h5ad")