Example: matlab

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

The following matlab script simply thresholds the orig/FLAIR image at gray value 800. To load/save the images, it uses the “Tools for NIfTI and ANALYZE image“. This is placed next to the script in a separate folder, which we add to the matlab path during runtime.

function example()
inputDir = '/input';
outputDir = '/output';

% Add subfolders to the path
currentFolder = fileparts(which(mfilename)); 
addpath(genpath(currentFolder));

nii = load_untouch_nii([inputDir '/orig/FLAIR.nii.gz']);

nii.img(nii.img <  800) = 0;
nii.img(nii.img >= 800) = 1;

save_untouch_nii(nii, [outputDir '/result.nii.gz']);

Since we cannot run the matlab GUI inside a Docker, we need to create a standalone application from this matlab script. The instructions on the matlab website are quite clear, but some small details:

  • Select “Runtime downloaded from web”, since we will pre-install the runtime in the Docker container. Hence it does not need to be included.
  • Be sure to include all “Files required for your application”.

After compilation, you only need the output in the for_redistribution_files_only directory.

Note: for this example, I used MATLAB R2016a running on CentOS 7 64bit.

The Dockerfile should use the correct CentOS version, install the matlab runtime, and add our compiled script. Hence the Dockerfile looks like:

FROM centos:latest
MAINTAINER hjkuijf

RUN yum update -y \
 && yum install wget unzip libXext libXt-devel libXmu -y \
 && mkdir /mcr-install \
 && cd /mcr-install \
 && wget -nv http://www.mathworks.com/supportfiles/downloads/R2016a/deployment_files/R2016a/installers/glnxa64/MCR_R2016a_glnxa64_installer.zip \
 && unzip MCR_R2016a_glnxa64_installer.zip \
 && ./install -mode silent -agreeToLicense yes \
 && rm -Rf /mcr-install
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/MATLAB/MATLAB_Runtime/v901/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v901/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v901/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v901/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/MATLAB_Runtime/v901/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/MATLAB_Runtime/v901/sys/java/jre/glnxa64/jre/lib/amd64
ENV XAPPLRESDIR=/usr/local/MATLAB/MATLAB_Runtime/v901/X11/app-defaults
ENV MCR_CACHE_VERBOSE=true
ENV MCR_CACHE_ROOT=/tmp

ADD matlab/example/for_redistribution_files_only /wmhseg_example
RUN ["chmod", "+x", "/wmhseg_example/example"]

The middle block of code downloads and installs the matlab runtime and correctly updates the environment variables. Please note: combine all RUN-commands into a single statement and be sure to include the clean-up (rm)! The final ADD command copies our compiled code into the container at the location /wmhseg_example. Finally we chmod the executable, so we can run it.

With the following command, we build a Docker container from our Dockerfile and the compiled matlab code:

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

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 matlab script:

docker exec [CONTAINER-ID] /wmhseg_example/example

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]