Skip to content

Run

Run methods, each in the environment it needs. Needs the environments from Installation.

run

run(method, h5ad, kmax=30, device='cpu', out_root='kdb_results', gpu_id=None, verbose=True)

Run one method on one .h5ad file and return its spatial domains.

The method runs in the environment it needs, as a separate process, and writes to its run folder <out_root>/<method>/<dataset>_<device>. A run into a folder that already holds a run replaces it.

PARAMETER DESCRIPTION
method

Method name, case-insensitive, from list_methods.

TYPE: str

h5ad

Input .h5ad file with raw counts in X. The R methods read obs["array_row"] and obs["array_col"], SpaGCN obs["pixel_row"] and obs["pixel_col"], and CellCharter, DeepST and SpatialDE2 obsm["spatial"]. An AnnData object in memory is not accepted: save it with adata.write_h5ad(...) first.

TYPE: str or path

kmax

Largest number of domains the method may consider, at least 2. DeepST and SpaGCN ignore it. BNPSpace starts from it and accepts at most 30.

TYPE: int DEFAULT: 30

device

"cpu" or "gpu". With "gpu", CellCharter, DeepST and SpatialDE2 are started with a GPU visible. The other methods have no GPU code and run on the CPU, with a warning.

TYPE: str DEFAULT: 'cpu'

out_root

Folder that collects the run folders, the same as for run_all.

TYPE: str or path DEFAULT: 'kdb_results'

gpu_id

GPU to use with device="gpu", as numbered by nvidia-smi. Default: the GPUs already named in CUDA_VISIBLE_DEVICES, as a job scheduler sets it, else GPU 0.

TYPE: int or str DEFAULT: None

verbose

Print the method's output while it runs.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
DataFrame

One row per spot, indexed by spot_id, with the domain label (as a string) in cluster.

RAISES DESCRIPTION
MethodFailed

The method stopped with an error or wrote no labels. run_info.json records the run with "success": false.

FileNotFoundError

h5ad does not exist or is a folder, or the method's environment is missing. Nothing is written.

TypeError

h5ad is an AnnData object, not a file.

ValueError

Unknown method or device, an input file that is not .h5ad, or a kmax the method does not accept.

WARNS DESCRIPTION
UserWarning

device="gpu" for a method without GPU code.

Notes

The run folder holds:

  • cluster_results.csv: the label of each spot.
  • run.log: everything the method printed.
  • packages.txt: the packages and versions of the method's environment.
  • run_info.json, with these fields:

    • method, dataset (the file name without .h5ad), h5ad (the input path), kmax.
    • device_requested, and device_used, which is "cpu" for a method without GPU code. gpu_id: the GPU of a GPU run.
    • started, wall_time_s (the whole run, data loading included) and peak_rss_mb (peak memory of the method's largest process, 1 GB = 1024 MB, without its worker processes and the GPU).
    • returncode and success. A successful run adds n_spots (spots with a label) and k_estimated.
    • kdomainbench (the package version), command (the call of the method script, started in the run folder, to rerun it by hand) and env (the environment variables that change what the method does).

Examples:

>>> clusters = kdb.run("SpaGCN", "data/D3_151509_5k.h5ad", kmax=30)
>>> clusters["cluster"].nunique()
4
>>> kdb.run("CellCharter", "data/D3_151509_5k.h5ad", device="gpu", gpu_id=0)

run_all

run_all(h5ad, kmax=30, device='cpu', methods=None, out_root='kdb_results', gpu_id=None, verbose=True)

Run several methods on one .h5ad file, one after another.

A method that fails, or whose environment is missing, is reported and skipped, so it does not stop the others. At the end one warning names every method that did not finish.

PARAMETER DESCRIPTION
h5ad

Input file, see run.

TYPE: str or path

kmax

Largest number of domains the methods may consider. BNPSpace accepts at most 30: for a larger kmax, leave it out with methods=.

TYPE: int DEFAULT: 30

device

"cpu" or "gpu", see run.

TYPE: str DEFAULT: 'cpu'

methods

Methods to run. Default: all ten. Each one runs again, also when out_root already holds a run of it.

TYPE: str or list of str DEFAULT: None

out_root

Each method writes to <out_root>/<method>/<dataset>_<device>.

TYPE: str or path DEFAULT: 'kdb_results'

gpu_id

GPU for device="gpu", see run.

TYPE: int or str DEFAULT: None

verbose

Print the methods' output while they run.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
dict

Method name -> its clusters, for the methods that succeeded.

RAISES DESCRIPTION
FileNotFoundError

h5ad does not exist or is a folder.

TypeError

h5ad is an AnnData object, not a file.

ValueError

An unknown method or device, an input file that is not .h5ad, or a kmax that one of the methods does not accept. Raised before the first method starts.

WARNS DESCRIPTION
UserWarning

Some methods did not finish. The warning names them and their logs.

Examples:

>>> results = kdb.run_all("data/D3_151509_5k.h5ad", kmax=30)
>>> results = kdb.run_all("data/D3_151509_5k.h5ad", methods=["DR.SC", "SC.MEB"])
>>> sorted(results)
['DR.SC', 'SC.MEB']

MethodFailed

MethodFailed(message, log=None)

Bases: RuntimeError

A method exited with an error or wrote no labels to cluster_results.csv.

The message names the run's run.log and ends with its last 20 lines. log is the path of that run.log.