Create a dataset loader
A dataset loader generates one or more raw datasets. A raw dataset is processed by the dataset preprocessing workflow to create a common dataset that can be used by multiple benchmarking tasks.
This guide shows how to create a new Viash component to fetch or generate datasets.
OpenProblems datasets
Section titled “OpenProblems datasets”Common datasets are created by generating raw datasets with a data loader and running them through the pre-processing pipeline. Afterwards, further task-specific processing occurs prior to the task-specific benchmarking workflow.
Step 1: Create a directory for the dataset loader
Section titled “Step 1: Create a directory for the dataset loader”To add a dataset to OpenProblems, create a Viash component for a dataset loader.
mkdir src/datasets/loaders/myloaderStep 2: Create a Viash config
Section titled “Step 2: Create a Viash config”Create a config for the dataset loader. The Viash config contains the component’s metadata, which script runs it, and its required dependencies.
functionality: name: "myloader" namespace: "datasets/loaders" description: "A new dataset loader" arguments: - name: "--output" __merge__: ../../api/file_raw.yaml direction: "output" resources: - type: python_script path: script.pyplatforms: - type: docker image: python:3.10 setup: - type: python pypi: anndata~=0.8.0 - type: nextflowfunctionality: name: "myloader" namespace: "datasets/loaders" description: "A new dataset loader" arguments: - name: "--output" __merge__: ../../api/file_raw.yaml direction: "output" resources: - type: r_script path: script.Rplatforms: - type: docker image: eddelbuettel/r2u:22.04 setup: - type: r cran: anndata - type: apt packages: [libhdf5-dev, libgeos-dev, python3, python3-pip, python3-dev, python-is-python3] - type: nextflowFor additional parameter options, see the Parameters section.
Step 3: Create a script
Section titled “Step 3: Create a script”Create a script that generates or loads the dataset. The example below generates a random dataset;
check src/datasets/loaders for real data examples. The output AnnData object must follow the
format described below.
import anndata as adimport pandas as pdimport scipyimport numpy as npimport random
## VIASH STARTpar = {"output": "output.h5ad"}## VIASH END
obs = pd.DataFrame({ "cell_type": random.choices(["enterocyte", "intestine goblet cell", "stem cell"], k=100), "batch": random.choices(["experiment1", "experiment2"], k=100), "tissue": random.choices(["colon", "ileum"], k=100)})
var = pd.DataFrame(index=["APP", "AXL", "ADA", "AMH"])
counts = scipy.sparse.csr_matrix( np.random.poisson(0.3, (obs.shape[0], var.shape[0])))
adata = ad.AnnData( layers={"counts": counts}, obs=obs, var=var, uns={ "dataset_id": "my_dataset", "dataset_name": "My dataset", "data_url": "https://url.to/dataset/source", "data_reference": "mydatasetbibtexreference", "dataset_summary": "A short description of the dataset.", "dataset_description": "Long description of the dataset.", "dataset_organism": "homo_sapiens" })
adata.write_h5ad(par["output"], compression="gzip")library(anndata)library(Matrix)
## VIASH STARTpar <- list("output" = "output.h5ad")## VIASH END
obs <- data.frame( cell_type = sample(c("enterocyte", "intestine goblet cell", "stem cell"), 100, replace = TRUE), batch = sample(c("experiment1", "experiment2"), 100, replace = TRUE), tissue = sample(c("colon", "ileum"), 100, replace = TRUE))
var <- data.frame(row.names = c("APP", "AXL", "ADA", "AMH"))
counts <- Matrix::rsparsematrix( nrow(obs), nrow(var), density = 0.3, rand.x = function(n) rpois(n, 100))
adata <- AnnData( layers = list(counts = counts), obs = obs, var = var, uns = list( dataset_id = "my_dataset", dataset_name = "My dataset", data_url = "https://url.to/dataset/source", data_reference = "mydatasetbibtexreference", dataset_summary = "A short description of the dataset.", dataset_description = "Long description of the dataset.", dataset_organism = "homo_sapiens" ))
adata$write_h5ad(par[["output"]], compression = "gzip")Step 4: Run the component
Section titled “Step 4: Run the component”Rebuild the Docker container after changing the platforms section:
viash run src/datasets/loaders/myloader/config.vsh.yaml -- ---setup cachedbuildRun the component:
viash run src/datasets/loaders/myloader/config.vsh.yaml -- --output mydataset.h5adParameters
Section titled “Parameters”Add arguments to the dataset loader by extending the functionality.arguments section:
arguments: - name: "--n_obs" type: "integer" description: "Number of cells to generate." default: 100 - name: "--n_vars" type: "integer" description: "Number of genes to generate." default: 100Access them via par in the script:
obs = pd.DataFrame({ "cell_type": random.choices(["enterocyte", "intestine goblet cell", "stem cell"], k=par["n_obs"]), "batch": random.choices(["experiment1", "experiment2"], k=par["n_obs"]), "tissue": random.choices(["colon", "ileum"], k=par["n_obs"])})var = pd.DataFrame(index=[f"Gene_{i}" for i in range(par["n_vars"])])obs <- data.frame( cell_type = sample(c("enterocyte", "intestine goblet cell", "stem cell"), par$n_obs, replace = TRUE), batch = sample(c("experiment1", "experiment2"), par$n_obs, replace = TRUE), tissue = sample(c("colon", "ileum"), par$n_obs, replace = TRUE))var <- data.frame(row.names = paste0("Gene_", seq_len(par$n_vars)))Format of a raw dataset object
Section titled “Format of a raw dataset object”The AnnData output must contain at least the following slots (derived from the
CELLxGENE schema v4.0.0).
The authoritative source is
src/api/file_raw.yaml
in the datasets repository.