From Openrtk
Revision as of 11:39, 8 July 2019 by Aurelien.coussat (talk | contribs) (Small compilation instructions for RTK)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

RTK 2.0 and above

Since RTK 2.0, RTK is bundled as a module of ITK. Compiling the wrappers became easier. Simply activate the options Module_RTK and ITK_WRAP_PYTHON when compiling ITK. Then simply add <INSTALLATION_PATH>/lib/python3.7/site-packages to your Python path by your favorite mean.

Before RTK 2.0

Before RTK 2.0, RTK was not a module of ITK. Compiling the wrappers needed some trickery. First thing first, copy, paste and adapt the following Bash script to your ITK directory:

set -e
set -x

NTHREADS=12 # Number of cores on your computer

SCRIPT_PATH=$(realpath $(dirname $0))

mkdir -p $BUILD_DIR 
make -j${NTHREADS} install

mkdir -p $SCRIPT_PATH/../../RTK/${SRCBUILDDIR}-PythonWrapping # <---
cd $SCRIPT_PATH/../../RTK/${SRCBUILDDIR}-PythonWrapping # <---
make -j${NTHREADS} install

Stuff to take care of:

  • Set an appropriate number of threads in order to speed things up (set it up equal to the number of CPU cores your machine has);
  • Add optirun if you need it;
  • Adapt the paths to ITK and RTK according to your own set up (lines marked with # <---).

Note that this script requires additional adaptations in order to function properly on Macs. The compilation should take about 1 hour or so. Upon completion, let's set the compiled code for Python.

Set up the compiled wrappers

After compilation, one must add the new wrappers to Python's path. Open up the site-packages directory of your Python installation. For instance,

cd ~/miniconda3/lib/python3.7/site-packages/

First, uninstall any ITK or RTK wrappers you might already have installed, if you have any. You can either use pip-autoremove (pip install pip-autoremove) or juste delete everything ITK or RTK related.

Now, create a file named WrapITK.pth and write in it the full path to the site-package's folder of your compiled Python wrappers. Do not use relative paths (for example using ~), use system's absolute path. For instance,


That should do the trick. Now, let's check that everything works. Open up a Python interactive console, for instance using IPython:


and run the two following commands:

import itk
from itk import RTK

They should complete without errors. You can now use ITK and RTK through Python.

If you do have errors...

...well, happy debugging! ITK can sometimes appear as not found if you're using some special kind of Python console (for instance, Spyder's console). The first thing to check is whether Python's path contains the correct path to your ITK and RTK compiled wrappers. In order to check it, you can simply print Python's path:

import sys

If the required path is contained in the list, well, you're out of luck. Check that ITK and RTK compilation correctly finishes, and I wish you the best of luck. If it is not, try adding it manually:

sys.path.append("path/to/your/compilation/site-packages") # for instance, /export/home/acoussat/Software/RTK/ITK/lin64-PythonWrapping-install/lib/python3.7/site-packages

Now, try again importing ITK and RTK:

import itk
from itk import RTK

If it now works, your Python's path is not correctly configured: check that your .pth has been correctly created. However, if it does not work, it might be because you're not running the Python version in which you set up your wrappers. It can occur if, for instance, you have both a system-wide and a miniconda Python installed. Therefore, check up that the Python which is called when you run python in a terminal actually runs the Python for which you set up the wrappers (in which you created the file WrapITK.pth).

Making wrappers cohabit

For development purposes, and because compilation times are generally high, it can be handy to have various wrappers versions installed and be able to switch between them: for instance, I use both a CUDA and a CUDA-free version. In order to do so, I copy-pasted the script presented above in the same location with a different name and tweaked it so that

  • CUDA is disabled (CUDA=OFF);
  • INSTALL_PATH is set to a new, not yet existent directory (for instance, INSTALL_PATH=${BUILD_DIR}-install-cudaoff);
  • RTK is set to build its wrappers in a new directory as well :
# ...
mkdir -p $SCRIPT_PATH/../../RTK-dev/${SRCBUILDDIR}-PythonWrapping-cudaoff
cd $SCRIPT_PATH/../../RTK-dev/${SRCBUILDDIR}-PythonWrapping-cudaoff
# ...

In this case, I didn't modify ITK (but only RTK by disabling CUDA) so we can use the same compiled wrappers for both of the versions, but it could not be the case! Now that the two versions are compiled, edit WrapITK.pth in order to add the new wrappers. For instance, this is my WrapITK.pth's content:

# Python pth file to add WrapITK's path to sys.path.



Note that one of the two is commented out. And that's it. Whenever you want to switch between the two versions, just comment/uncomment the corresponding lines and restart your Python kernel.