Example: python

This page shows a simple example on how to containerize your python script for this challenge. The source code can also be found on github: hjkuijf/wmhchallenge.

The following python script simply thresholds the orig/FLAIR image at gray value 800. At the moment, it uses the SimpleITK BinaryThreshold function.

import os
import SimpleITK as sitk

inputDir = '/input'
outputDir = '/output'

# Load the image
flairImage = sitk.ReadImage(os.path.join(inputDir, 'orig', 'FLAIR.nii.gz'))

# Binary threshold between 800 - 100000
resultImage = sitk.BinaryThreshold(flairImage, lowerThreshold=800, upperThreshold=100000)

sitk.WriteImage(resultImage, os.path.join(outputDir, 'result.nii.gz'))

This code needs a basic Python installation, with numpy and SimpleITK added. We therefore used miniconda, which has Docker container available that we can inherit from: continuumio/miniconda.

Our Dockerfile looks like this:

FROM continuumio/miniconda

RUN pip install numpy SimpleITK

ADD python/src /wmhseg_example

Our Python code is saved next to this Dockerfile in the folder python/src/example.py. With the following command, we build a Docker container from our Dockerfile and the Python source code:

docker build -f Dockerfile -t wmhchallenge/[TEAM-NAME] .

Note: the . at the end specifies that everything is in the current folder. Hence, you run this build command from the folder that contains the Dockerfile and the source code.

Once your container is ready, we can run it with the following command:

docker run -dit -v [TEST-ORIG]:/input/orig:ro -v [TEST-PRE]:/input/pre:ro -v /output wmhchallenge/[TEAM-NAME]

The -v options map the input folder into the container at /input, read-only. The last -v creates an output directory.

This command outputs the Container ID, which you can also look up with:

docker ps

Next, we will execute the example Python script:

docker exec [CONTAINER-ID] python /wmhseg_example/example.py

Since this script is quite small, it doesn’t take long to finish. Next we copy the output from the container to our local machine:

docker cp [CONTAINER-ID]:/output [RESULT-LOCATION]

Finally, we shut down the running container. This also removes the created /output folder and any other changes made to the container.

docker rm -v [CONTAINER-ID]