Dataset processor
The dataset processor component reads a common OpenProblems dataset and writes one or more task-specific AnnData files. It is the first component in the task pipeline and is responsible for splitting the data into the files your methods, control methods, and metrics expect.
Why use a dataset processor?
Section titled “Why use a dataset processor?”The processor is the single place where ground-truth labels are separated from method inputs. By
writing a solution.h5ad that is never passed to methods, the pipeline guarantees that no method
can access labels it is not supposed to see. Metrics receive the solution file independently, after
a method has already produced its output.
How to build the processor
Section titled “How to build the processor”The dataset processor is a standard Viash component: a config.vsh.yaml that declares the
interface and a script that implements the logic.
Step 1: Create the Viash config
Section titled “Step 1: Create the Viash config”__merge__: ../../api/comp_data_processor.yaml # 1
name: process_dataset # 2namespace: data_processors
info: label: Process dataset summary: "Filter and split a common dataset into task-specific train, test, and solution files."
arguments: - name: "--input" __merge__: ../../api/file_common_dataset.yaml type: file required: true - name: "--output_train" __merge__: ../../api/file_train.yaml type: file required: true direction: output - name: "--output_test" __merge__: ../../api/file_test.yaml type: file required: true direction: output - name: "--output_solution" __merge__: ../../api/file_solution.yaml type: file required: true direction: output
engines: - type: docker image: openproblems/base_python:1.0.0 setup: - type: python github: - "openproblems-bio/core#subdirectory=packages/python/openproblems"
runners: - type: executable - type: nextflow directives: label: [midtime, midmem, lowcpu]__merge__: ../../api/comp_data_processor.yaml # 1
name: process_dataset # 2namespace: data_processors
info: label: Process dataset summary: "Filter and split a common dataset into task-specific train, test, and solution files."
arguments: - name: "--input" __merge__: ../../api/file_common_dataset.yaml type: file required: true - name: "--output_train" __merge__: ../../api/file_train.yaml type: file required: true direction: output - name: "--output_test" __merge__: ../../api/file_test.yaml type: file required: true direction: output - name: "--output_solution" __merge__: ../../api/file_solution.yaml type: file required: true direction: output
engines: - type: docker image: openproblems/base_python:1.0.0 setup: - type: python github: - "openproblems-bio/core#subdirectory=packages/python/openproblems"
runners: - type: executable - type: nextflow directives: label: [midtime, midmem, lowcpu]Annotations:
__merge__imports the shared component interface fromsrc/api/comp_data_processor.yaml. This provides the unit test and any shared arguments.namemust always beprocess_datasetfor dataset processor components.- Add one output argument for each task-specific AnnData file your task requires. Update the
__merge__path for each to reference the matchingfile_*.yamlinsrc/api/. - The
enginesblock selects the Docker base image. Addsetupentries for any additional Python or R packages your script needs. - Keep both
executableandnextflowrunners so the component works in both local testing and the full Nextflow pipeline.
Step 2: Create the script
Section titled “Step 2: Create the script”import anndata as adfrom openproblems.data import subset_h5ad_by_format
## VIASH STARTpar = { "input": "resources_test/common/cxg_mouse_pancreas_atlas/dataset.h5ad", "output_train": "output_train.h5ad", "output_test": "output_test.h5ad", "output_solution": "output_solution.h5ad",}## VIASH END
print("Reading input dataset", flush=True)adata = ad.read_h5ad(par["input"])
# Split cells into train and testis_test = adata.obs["batch"] == adata.obs["batch"].iloc[-1]adata_train = adata[~is_test].copy()adata_test = adata[is_test].copy()
print("Writing train output", flush=True)out_train = subset_h5ad_by_format(adata_train, par, "output_train")out_train.write_h5ad(par["output_train"], compression="gzip")
print("Writing test output", flush=True)out_test = subset_h5ad_by_format(adata_test, par, "output_test")out_test.write_h5ad(par["output_test"], compression="gzip")
print("Writing solution output", flush=True)out_solution = subset_h5ad_by_format(adata_test, par, "output_solution")out_solution.write_h5ad(par["output_solution"], compression="gzip")
print("Done!", flush=True)library(anndata)library(openproblems)
## VIASH STARTpar <- list( input = "resources_test/common/cxg_mouse_pancreas_atlas/dataset.h5ad", output_train = "output_train.h5ad", output_test = "output_test.h5ad", output_solution = "output_solution.h5ad")## VIASH END
cat("Reading input dataset\n")adata <- read_h5ad(par$input)
# Split cells into train and testis_test <- adata$obs$batch == tail(unique(adata$obs$batch), 1)adata_train <- adata[!is_test, ]adata_test <- adata[is_test, ]
cat("Writing train output\n")out_train <- subset_h5ad_by_format(adata_train, par, "output_train")out_train$write_h5ad(par$output_train, compression = "gzip")
cat("Writing test output\n")out_test <- subset_h5ad_by_format(adata_test, par, "output_test")out_test$write_h5ad(par$output_test, compression = "gzip")
cat("Writing solution output\n")out_solution <- subset_h5ad_by_format(adata_test, par, "output_solution")out_solution$write_h5ad(par$output_solution, compression = "gzip")
cat("Done!\n")Step 3: Create test resources
Section titled “Step 3: Create test resources”The test resource script runs the processor on a small dataset and writes the outputs to
resources_test/. These files are used by the unit tests of all downstream components.
#!/bin/bash
set -eo pipefail
echo "Running dataset processor"
viash run src/data_processors/process_dataset/config.vsh.yaml -- \ --input resources_test/common/cxg_mouse_pancreas_atlas/dataset.h5ad \ --output_train resources_test/task_template/cxg_mouse_pancreas_atlas/train.h5ad \ --output_test resources_test/task_template/cxg_mouse_pancreas_atlas/test.h5ad \ --output_solution resources_test/task_template/cxg_mouse_pancreas_atlas/solution.h5ad
echo "Done!"Run the script from the repository root:
bash scripts/create_datasets/test_resources.sh