- Documentation >
- Create component >
- Add a method
Table of contents
Add a method
A method is a technique to solve a specific problem when analyzing omics data. Its performance is assessed by comparing it to other methods and control methods.
This guide will show you how to create a new Viash component.
In the following we will show examples for both Python and R.
Note that the Task template repo is used throughout the guide, so make sure to replace any occurrences of "task_template"
with your task of interest.
Make sure you have followed the "Getting started" guide.
Step 1: Create a new component
Use the create_*_method.sh
script found in the scripts repository to start creating a new method. Open the script and update the name
parameter to the desired name of the method.
Change the --name
to a unique name for your method. It must match the regex [a-z][a-z0-9_]*
(snakecase).
- A config file contains metadata of the component and the dependencies required to run it. In steps 2 and 3 we will fill in the required information.
- A script contains the code to run the method. In step 4 we will edit the script.
Some tasks have multiple method subtypes (e.g. batch_integration
), which will require you to use a different value for --type
corresponding to the desired method subtype.
Step 2: Fill in metadata
The Viash config contains metadata of your method, which script is used to run it, and the required dependencies.
Generated config file
This is what the config.vsh.yaml
generated by the create_component
component looks like:
Required metadata fields
Please edit info
section in the config file to fill in the necessary metadata.
-
.merge
: The API specifies which type of component this is. It contains specifications for:- The input/output files
- Common parameters
- A unit test
-
.name
: A unique identifier. Can only contain lowercase letters, numbers or underscores. -
.label
: A unique, human-readable, short label. Used for creating summary tables and visualizations. -
.summary
: A one sentence summary of purpose and methodology. Used for creating an overview tables. -
.description
: A longer description (one or more paragraphs). Used for creating reference documentation and supplementary information.
Step 3: Add dependencies
Each component has it's own set of dependencies, because different components might have conflicting dependencies.
base images
For your convenience we have created several base images that can be used for python or R scripts. These images can be found in the OpenProblems Docker repository. Click on the packages to view the url you need to use. You are not required to use these images but install the required packages to make sure OpenProblems works properly.
-
openproblems/base_python
Base image for python scripts. -
openproblems/base_r
Base image for R scripts. -
openproblems/base_pytorch_nvidia
Base image for scripts that use pytorch with nvidia gpu support. -
openproblems/base_tensorflow_nvidia
Base image for scripts that use tensorflow with nvidia gpu support.
custom image
Update the setup
definition in the platforms
section of the config file.
This section describes the packages that need to be installed in the Docker image and are required for your method to run.
If you're using a custom image use the following minimum setup:
Please check out this guide for more information on how to add extra package dependencies.
Tip: After making changes to the components dependencies, you will need to rebuild the docker container as follows:
viash run src/methods/my_python_method/config.vsh.yaml -- \
---setup cachedbuild
output
#| echo: false
viash run src/methods/my_python_method/config.vsh.yaml -- \
---setup cachedbuild
:::
Step 4: Edit script
A component's script typically has five sections:
a. Imports and libraries b. Argument values c. Read input data d. Generate results e. Write output data to file
This is what the script generated by the create_component
component looks like:
The required sections are explained here in more detail:
a. Imports and libraries
In the top section of the script you can define which packages/libraries the method needs. If you add a new or different package add the dependency to config.vsh.yaml
in the setup
field (see above).
b. Argument block
The Viash code block is designed to facilitate prototyping, by enabling you to execute directly by running python script.py
(or Rscript script.R
for R users). Note that anything between "VIASH START" and "VIASH END" will be removed and replaced with a CLI argument parser when the components are being built by Viash.
Here, the par
dictionary contains all the arguments
defined in the config.vsh.yaml
file (including those from the defined __merge__
file). When adding a argument
in the par
dict also add it to the config.vsh.yaml
in the arguments
section.
c. Read input data
This section reads any input AnnData files passed to the component.
d. Generate results
This is the most important section of your script, as it defines the core functionality provided by the component. It processes the input data to create results for the particular task at hand.
e. Write output data to file
The output stored in a AnnData object and then written to an .h5ad
file. The format is specified by the API file specified in the __merge__
field in the config file.
Step 5: Add resources (optional)
It is possible to add additional resources such as a file containing helper functions or other resources. Please visit this page for more information on how to do this.
Step 6: Try component
Your component's API file contains the necessary unit tests to check whether your component works and the output is in the correct format.
You can test your component by using the following command:
viash test src/methods/my_python_method/config.vsh.yaml
Output
#| echo: false
# use logistic_regression instead of 'my_python_method' because the script won't work.
# maybe copy a 'working' script here
viash test src/methods/logistic_regression/config.vsh.yaml
Visit "Run tests" for more information on running unit tests and how to interpret common error messages.
You can also run your component on local files using the viash run
command. For example:
viash run src/methods/my_python_method/config.vsh.yaml -- \
--input_train resources_test/task_template/cxg_mouse_pancreas_atlas/train.h5ad \
--input_test resources_test/task_template/cxg_mouse_pancreas_atlas/test.h5ad \
--output output.h5ad
Next steps
If your component works, please create a pull request.