Skip to content

Instantly share code, notes, and snippets.

@vfmatzkin
Created July 2, 2021 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfmatzkin/0fcc79a61f9bafcc2113fd83a8900937 to your computer and use it in GitHub Desktop.
Save vfmatzkin/0fcc79a61f9bafcc2113fd83a8900937 to your computer and use it in GitHub Desktop.
Install SimpleElastix on a conda env

I've decided to create this article because I've continously followed the official and alternative tutorials around the internet with no luck. Anyway, this last one was the closest, so some steps will be the same.

This process was done in Ubuntu 20.04 LTS, and covers only SimpleElastix wrapping for Python 3.

If you use a platform such as Anaconda or virtualenv, you will have to deactivate them (conda deactivate) before compiling SimpleElastix, and once it's compiled activate it before the last step (I'll do this in this tutorial). If you activate them before that step, the SimpleElastix SuperBuild will skip some steps and then the instalation may fail. With deactivating I refer also to the base environment, your terminal must look as before installing anaconda or virtualenv.

Also you have to uninstall SimpleITK if you have it already installed (otherwise you will end up with two SimpleITK installations).

  1. Install the dependencies:

    1. Since monodevelop is not in the Ubuntu 20.04 LTS repo, you should follow the steps in this Askubuntu answer:

      • Add mono-project's repo for 18.04

        sudo apt install gnupg ca-certificates
        sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
        echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
        sudo apt update
        
      • install mono-roslyn

        sudo apt install mono-roslyn

    2. Install the prerequisites

      sudo apt-get install cmake swig monodevelop r-base r-base-dev ruby ruby-dev python python-dev python3 python3-dev tcl tcl-dev tk tk-dev

  2. Build SimpleElastix: Make sure you have the DPYTHON files mentioned here (you can use locate for finding them). Make sure you have 16GB of RAM, otherwise, change the make -j4 4 number with a lower value (1: 4GB, 2: 8GB ..):

    git clone https://github.com/kaspermarstal/SimpleElastix
    cd ~/Software/SimpleElastix
    mkdir build
    cd build
    cmake -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 \
    -DPYTHON_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpython3.5.so \
    -DPYTHON_INCLUDE_DIR:PATH=/usr/include/python3.5 \
    -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF \
    -WRAP_CSHARP=OFF -WRAP_JAVA=OFF -WRAP_LUA=OFF \
    -WRAP_R=OFF -WRAP_DEFAULT=OFF -WRAP_RUBY=OFF \
    -WRAP_TCL=OFF -WRAP_PYTHON=ON ../SuperBuild
    make -j4
    

    If the compilation ends with no errors, something like:

    [100%] Completed 'SimpleITK'
    [100%] Built target SimpleITK
    

    don't celebrate yet.

  3. Head to {BUILD_DIRECTORY}/SimpleITK-build/Wrapping/Python

  4. In case you won't use anaconda, run:

    sudo python3 setup.py install

    Otherwise, activate the environment and install it without sudo privileges:

    conda activate myenv
    python3 setup.py install

    If you get an error like:

     running build_ext
     error: can't copy '/home/user/Code/SimpleElastix/build/SimpleITK-build/Wrapping/Python/Packaging/_SimpleITK.so': doesn't exist or not a regular file

    Locate the file in the parent folder (in this case _SimpleITK.so), copy it to the Packaging folder and repeat the install step.

Note that if you install it system-wide first, for installing it to your conda environment, you will have to own the build folder and subfolders with:

sudo chown user -R ~/Code/SimpleElastix/build

otherwise you will get error: [Errno 13] Permission denied.

  1. Check if the installation succeded, seeing if the Elastix function is avaiable:
(venv) user@user~PC:~/Code/SimpleElastix/build/SimpleITK-build/Wrapping/Python/Packaging$ python3
Python 3.7.0 (default, Oct  9 2018, 10:31:47) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import SimpleITK
>>> SimpleITK.Elastix
<function Elastix at 0x7fd9508d26a8>

Now you can celebrate. If you see something like:

>>> sitk.Elastix
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'SimpleITK' has no attribute 'Elastix'
>>> 

The instalation failed.

@henkvanvoorst92
Copy link

Hi, Great overview do you have some advise for me? I was not able to instal python and python-dev since I am using Ubunutu 22.04. At 70% installation after running the make command I get an error (see below). Do you have any clue how this comes. Tried several things including running installation in a separate conda env or not but it did not work.

[ 70%] Built target hdf5-static
make[4]: Leaving directory '/SimpleElastix/build/ITK-build'
make[3]: *** [Makefile:156: all] Error 2
make[3]: Leaving directory '
/SimpleElastix/build/ITK-build'
make[2]: *** [CMakeFiles/ITK.dir/build.make:86: ITK-prefix/src/ITK-stamp/ITK-build] Error 2
make[2]: Leaving directory '/SimpleElastix/build'
make[1]: *** [CMakeFiles/Makefile2:234: CMakeFiles/ITK.dir/all] Error 2
make[1]: Leaving directory '
/SimpleElastix/build'
make: *** [Makefile:91: all] Error 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment