WrappersCompilation: Difference between revisions

From Openrtk
Jump to navigation Jump to search
(Making wrappers cohabit)
(Small compilation instructions for RTK)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Compiling RTK Python wrappers =
= RTK 2.0 and above =


This little guide will show you how to compile and set up ITK and RTK wrappers on your system. First thing first, copy, paste and adapt the following Bash script to your ITK directory:
Since RTK 2.0, RTK is bundled as a module of ITK. Compiling the wrappers became easier. Simply activate the options <code>Module_RTK</code> and <code>ITK_WRAP_PYTHON</code> when compiling ITK. Then simply add <code><INSTALLATION_PATH>/lib/python3.7/site-packages</code> 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:


<source lang="bash">set -e
<source lang="bash">set -e
Line 32: Line 36:
''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.
''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 =
== Set up the compiled wrappers ==


After compilation, one must add the new wrappers to Python's path. Open up the <code>site-packages</code> directory of your Python installation. For instance,
After compilation, one must add the new wrappers to Python's path. Open up the <code>site-packages</code> directory of your Python installation. For instance,
Line 51: Line 55:
They should complete without errors. You can now use ITK and RTK through Python.
They should complete without errors. You can now use ITK and RTK through Python.


= If you do have errors... =
== 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:
...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:
Line 66: Line 70:
If it now works, your Python's path is not correctly configured: check that your <code>.pth</code> 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 <code>miniconda</code> Python installed. Therefore, check up that the Python which is called when you run <code>python</code> in a terminal actually runs the Python for which you set up the wrappers (in which you created the file <code>WrapITK.pth</code>).
If it now works, your Python's path is not correctly configured: check that your <code>.pth</code> 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 <code>miniconda</code> Python installed. Therefore, check up that the Python which is called when you run <code>python</code> in a terminal actually runs the Python for which you set up the wrappers (in which you created the file <code>WrapITK.pth</code>).


= Making wrappers cohabit =
== Making wrappers cohabit ==


For development purposes, 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
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 (<code lang="bash">CUDA=OFF</code>);
* CUDA is disabled (<code lang="bash">CUDA=OFF</code>);
* <code lang="bash">INSTALL_PATH</code> is set to a new, not yet existent directory (for instance, <code lang="bash">INSTALL_PATH=${BUILD_DIR}-install-cudaoff</code>);
* <code lang="bash">INSTALL_PATH</code> is set to a new, not yet existent directory (for instance, <code lang="bash">INSTALL_PATH=${BUILD_DIR}-install-cudaoff</code>);
Line 78: Line 82:
cd $SCRIPT_PATH/../../RTK-dev/${SRCBUILDDIR}-PythonWrapping-cudaoff
cd $SCRIPT_PATH/../../RTK-dev/${SRCBUILDDIR}-PythonWrapping-cudaoff
# ...</source>
# ...</source>
In this case, I didn't modified 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!'''
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 <code>WrapITK.pth</code> in order to add the new wrappers. For instance, this is my <code>WrapITK.pth</code>'s content:
Now that the two versions are compiled, edit <code>WrapITK.pth</code> in order to add the new wrappers. For instance, this is my <code>WrapITK.pth</code>'s content:
<source>
<source>

Latest revision as of 07:39, 8 July 2019

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

SRCBUILDDIR=lin64
CUDA=ON
NTHREADS=12 # Number of cores on your computer

SCRIPT_PATH=$(realpath $(dirname $0))
BUILD_DIR=$SCRIPT_PATH/${SRCBUILDDIR}-PythonWrapping
INSTALL_PATH=${BUILD_DIR}-install

# ITK
mkdir -p $BUILD_DIR 
cd $BUILD_DIR 
cmake $SCRIPT_PATH/src -DITK_LEGACY_SILENT=ON -DBUILD_EXAMPLES=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DITK_WRAP_PYTHON=ON -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH # <---
make -j${NTHREADS} install

# RTK
mkdir -p $SCRIPT_PATH/../../RTK/${SRCBUILDDIR}-PythonWrapping # <---
cd $SCRIPT_PATH/../../RTK/${SRCBUILDDIR}-PythonWrapping # <---
cmake ../RTK -DITK_DIR=$BUILD_DIR -DBUILD_SHARED_LIBS=OFF -DRTK_BUILD_APPLICATIONS=OFF -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH -DRTK_USE_CUDA=${CUDA} ${CMAKE_OPTIONS} # <---
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,

/export/home/acoussat/Software/RTK/ITK/lin64-PythonWrapping-install/lib/python3.7/site-packages

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

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
sys.path

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 :
# ...
# RTK
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.

# CUDA ON
#/export/home/acoussat/Software/RTK/ITK/lin64-PythonWrapping-install/lib/python3.7/site-packages

# CUDA OFF
/export/home/acoussat/Software/RTK/ITK/lin64-PythonWrapping-install-cudaoff/lib/python3.7/site-packages

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.