Skip to content

Commit

Permalink
📑
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Jan 5, 2024
1 parent 106c36a commit 751de85
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 597 deletions.
61 changes: 3 additions & 58 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,9 @@ on:
branches: [ main ]

jobs:
test:
name: Unit tests
if: false # delete this line to enable automatic testing
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v5
with:
build-args: extras_require=dev
context: .
load: true
push: false
tags: "localhost/local/app:dev"
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run pytest
run: |
docker run -v "$GITHUB_WORKSPACE:/app:ro" -w /app localhost/local/app:dev \
pytest -o cache_dir=/tmp/pytest
build:
name: Build
if: false # delete this line and uncomment the line below to enable automatic builds
# if: github.event_name == 'push' || github.event_name == 'release'
# needs: [ test ] # uncomment to require passing tests
if: github.event_name == 'push' || github.event_name == 'release'
runs-on: ubuntu-22.04

steps:
Expand Down Expand Up @@ -105,37 +81,6 @@ jobs:
tags: ${{ steps.info.outputs.local_tag }}
load: true
cache-from: type=gha
# If you have a directory called examples/incoming/ and examples/outgoing/, then
# run your ChRIS plugin with no parameters, and assert that it creates all the files
# which are expected. File contents are not compared.
- name: Run examples
id: run_examples
run: |
if ! [ -d 'examples/incoming/' ] || ! [ -d 'examples/outgoing/' ]; then
echo "No examples."
exit 0
fi
dock_image=${{ steps.info.outputs.local_tag }}
output_dir=$(mktemp -d)
cmd=$(docker image inspect -f '{{ (index .Config.Cmd 0) }}' $dock_image)
docker run --rm -u "$(id -u):$(id -g)" \
-v "$PWD/examples/incoming:/incoming:ro" \
-v "$output_dir:/outgoing:rw" \
$dock_image $cmd /incoming /outgoing
for expected_file in $(find examples/outgoing -type f); do
fname="${expected_file##*/}"
out_path="$output_dir/$fname"
printf "Checking output %s exists..." "$out_path"
if [ -f "$out_path" ]; then
echo "ok"
else
echo "not found"
exit 1
fi
done
- name: Login to DockerHub
if: (github.event_name == 'push' || github.event_name == 'release') && contains(steps.info.outputs.tags_csv, 'docker.io')
uses: docker/login-action@v3
Expand All @@ -156,8 +101,8 @@ jobs:
context: .
file: ./Dockerfile
tags: ${{ steps.info.outputs.tags_csv }}
# if non-x86_84 architectures are supported, add them here
platforms: linux/amd64 #,linux/arm64,linux/ppc64le
# linux/ppc64le not working, see https://github.com/ANTsX/ANTs/issues/1644
platforms: linux/amd64,linux/arm64
push: ${{ steps.info.outputs.push }}
cache-to: type=gha,mode=max

Expand Down
17 changes: 9 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Python version can be changed, e.g.
# FROM python:3.8
# FROM ghcr.io/mamba-org/micromamba:1.5.1-focal-cuda-11.3.1
FROM docker.io/python:3.12.0-slim-bookworm
FROM docker.io/python:3.12.1-slim-bookworm

LABEL org.opencontainers.image.authors="FNNDSC <[email protected]>" \
org.opencontainers.image.title="ChRIS Plugin Title" \
org.opencontainers.image.description="A ChRIS plugin that..."
org.opencontainers.image.title="N4 Bias Field Correction" \
org.opencontainers.image.description="A ChRIS plugin wrapper for N4BiasFieldCorrection"

ARG SRCDIR=/usr/local/src/app
# install N4BiasFieldCorrection
COPY --from=docker.io/fnndsc/n4biasfieldcorrection:2.5.0 /opt/ants /opt/ants
ENV PATH=/opt/ants/bin:$PATH LD_LIBRARY_PATH=/opt/ants/lib:$LD_LIBRARY_PATH

ARG SRCDIR=/usr/local/src/pl-N4BiasFieldCorrection
WORKDIR ${SRCDIR}

COPY requirements.txt .
Expand All @@ -19,4 +20,4 @@ RUN pip install ".[${extras_require}]" \
&& cd / && rm -rf ${SRCDIR}
WORKDIR /

CMD ["commandname"]
CMD ["n4wrapper"]
171 changes: 12 additions & 159 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,177 +1,30 @@
# _ChRIS_ Plugin Template
# `pl-N4BiasFieldCorrection`

[![test status](https://github.com/FNNDSC/python-chrisapp-template/actions/workflows/src.yml/badge.svg)](https://github.com/FNNDSC/python-chrisapp-template/actions/workflows/src.yml)
[![MIT License](https://img.shields.io/github/license/FNNDSC/python-chrisapp-template)](LICENSE)
[![Version](https://img.shields.io/docker/v/fnndsc/pl-N4BiasFieldCorrection?sort=semver)](https://hub.docker.com/r/fnndsc/pl-N4BiasFieldCorrection)
[![MIT License](https://img.shields.io/github/license/fnndsc/pl-N4BiasFieldCorrection)](https://github.com/FNNDSC/pl-N4BiasFieldCorrection/blob/main/LICENSE)
[![ci](https://github.com/FNNDSC/pl-N4BiasFieldCorrection/actions/workflows/ci.yml/badge.svg)](https://github.com/FNNDSC/pl-N4BiasFieldCorrection/actions/workflows/ci.yml)

This is a minimal template repository for _ChRIS_ plugin applications in Python.

## About _ChRIS_ Plugins

A _ChRIS_ plugin is a scientific data-processing software which can run anywhere all-the-same:
in the cloud via a [web app](https://github.com/FNNDSC/ChRIS_ui/), or on your own laptop
from the terminal. They are easy to build and easy to understand: most simply, a
_ChRIS_ plugin is a command-line program which processes data from an input directory
and creates data to an output directory with the usage
`commandname [options...] inputdir/ outputdir/`.

For more information, visit our website https://chrisproject.org

## How to Use This Template

Go to https://github.com/FNNDSC/python-chrisapp-template and click "Use this template".
The newly created repository is ready to use right away.

A script `bootstrap.sh` is provided to help fill in and rename values for your new project.
It is optional to use.

1. Edit the variables in `bootstrap.sh`
2. Run `./bootstrap.sh`
3. Follow the instructions it will print out

## Example Plugins

Here are some good, complete examples of _ChRIS_ plugins created from this template.

- https://github.com/FNNDSC/pl-dcm2niix (basic command wrapper example)
- <https://github.com/FNNDSC/pl-adapt_object_mesh> (parallelizes a command)
- https://github.com/FNNDSC/pl-mri-preview (uses [NiBabel](https://nipy.org/nibabel/))
- https://github.com/FNNDSC/pl-pyvista-volume (example using Python package project structure and pytest)
- https://github.com/FNNDSC/pl-fetal-cp-surface-extract (has a good README.md)

## What's Inside

| Path | Purpose |
|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `app.py` | Main script: start editing here! |
| `tests/` | Unit tests |
| `setup.py` | [Python project metadata and installation script](https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#setup-py) |
| `requirements.txt` | List of Python dependencies |
| `Dockerfile` | [Container image build recipe](https://docs.docker.com/engine/reference/builder/) |
| `.github/workflows/ci.yml` | "continuous integration" using [Github Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions): automatic testing, building, and uploads to https://chrisstore.co |

## Contributing

The source code for the `main` branch of this repository is on the
[src](https://github.com/fnndsc/python-chrisapp-template/tree/src)
branch, which has an additional file
[`.github/workflows/src.yml`](https://github.com/FNNDSC/python-chrisapp-template/blob/src/.github/workflows/src.yml)
When tests pass, changes are automatically merged into `main`.
Developers should commit to or make pull requests targeting `src`.
Do not push directly to `main`.

This is a workaround in order to do automatic testing of this template
without including the `.github/workflows/src.yml` file in the template itself.

<!-- BEGIN README TEMPLATE
# ChRIS Plugin Title
[![Version](https://img.shields.io/docker/v/fnndsc/pl-appname?sort=semver)](https://hub.docker.com/r/fnndsc/pl-appname)
[![MIT License](https://img.shields.io/github/license/fnndsc/pl-appname)](https://github.com/FNNDSC/pl-appname/blob/main/LICENSE)
[![ci](https://github.com/FNNDSC/pl-appname/actions/workflows/ci.yml/badge.svg)](https://github.com/FNNDSC/pl-appname/actions/workflows/ci.yml)
`pl-appname` is a [_ChRIS_](https://chrisproject.org/)
_ds_ plugin which takes in ... as input files and
creates ... as output files.
## Abstract
...
`pl-N4BiasFieldCorrection` is a [_ChRIS_](https://chrisproject.org/)
_ds_ plugin wrapper for the [`N4BiasFieldCorrection`](https://github.com/ANTsX/ANTs/wiki/N4BiasFieldCorrection)
program from [ANTs](https://github.com/ANTsX/ANTs).
`pl-N4BiasFieldCorrection` runs `N4BiasFieldCorrection` on every `.nii` and `.nii.gz` file found in a specified input directory.

## Installation

`pl-appname` is a _[ChRIS](https://chrisproject.org/) plugin_, meaning it can
`pl-N4BiasFieldCorrection` is a _[ChRIS](https://chrisproject.org/) plugin_, meaning it can
run from either within _ChRIS_ or the command-line.

## Local Usage

To get started with local command-line usage, use [Apptainer](https://apptainer.org/)
(a.k.a. Singularity) to run `pl-appname` as a container:
(a.k.a. Singularity) to run `pl-N4BiasFieldCorrection` as a container:

```shell
apptainer exec docker://fnndsc/pl-appname commandname [--args values...] input/ output/
apptainer exec docker://fnndsc/pl-N4BiasFieldCorrection n4wrapper input/ output/
```

To print its available options, run:

```shell
apptainer exec docker://fnndsc/pl-appname commandname --help
apptainer exec docker://fnndsc/pl-N4BiasFieldCorrection n4wrapper --help
```
## Examples
`commandname` requires two positional arguments: a directory containing
input data, and a directory where to create output data.
First, create the input directory and move input data into it.
```shell
mkdir incoming/ outgoing/
mv some.dat other.dat incoming/
apptainer exec docker://fnndsc/pl-appname:latest commandname [--args] incoming/ outgoing/
```
## Development
Instructions for developers.
### Building
Build a local container image:
```shell
docker build -t localhost/fnndsc/pl-appname .
```
### Running
Mount the source code `app.py` into a container to try out changes without rebuild.
```shell
docker run --rm -it --userns=host -u $(id -u):$(id -g) \
-v $PWD/app.py:/usr/local/lib/python3.11/site-packages/app.py:ro \
-v $PWD/in:/incoming:ro -v $PWD/out:/outgoing:rw -w /outgoing \
localhost/fnndsc/pl-appname commandname /incoming /outgoing
```
### Testing
Run unit tests using `pytest`.
It's recommended to rebuild the image to ensure that sources are up-to-date.
Use the option `--build-arg extras_require=dev` to install extra dependencies for testing.
```shell
docker build -t localhost/fnndsc/pl-appname:dev --build-arg extras_require=dev .
docker run --rm -it localhost/fnndsc/pl-appname:dev pytest
```
## Release
Steps for release can be automated by [Github Actions](.github/workflows/ci.yml).
This section is about how to do those steps manually.
### Increase Version Number
Increase the version number in `setup.py` and commit this file.
### Push Container Image
Build and push an image tagged by the version. For example, for version `1.2.3`:
```
docker build -t docker.io/fnndsc/pl-appname:1.2.3 .
docker push docker.io/fnndsc/pl-appname:1.2.3
```
### Get JSON Representation
Run [`chris_plugin_info`](https://github.com/FNNDSC/chris_plugin#usage)
to produce a JSON description of this plugin, which can be uploaded to _ChRIS_.
```shell
docker run --rm docker.io/fnndsc/pl-appname:1.2.3 chris_plugin_info -d docker.io/fnndsc/pl-appname:1.2.3 > chris_plugin_info.json
```
Intructions on how to upload the plugin to _ChRIS_ can be found here:
https://chrisproject.org/docs/tutorials/upload_plugin
END README TEMPLATE -->
70 changes: 0 additions & 70 deletions app.py

This file was deleted.

Loading

0 comments on commit 751de85

Please sign in to comment.