Modules:FourDAnalysis-Documentation-3.6-SciPy-Memo
Go back to 4D Analysis Module for 3D Slicer 3.6
Contents
About this page
This is a memo on how to set up SciPy in 3D Slicer version 3, for those who are interested in 4D Analysis module. This page explains how to build SciPy package from the source. Please note that building SciPy is not always straightforward, due to its dependencies on other libraries e.g. BLAS, LAPACK, etc, and the author of this page is not capable to answer to questions regarding installation of SciPy. This page only provides the steps that worked for the author's computer environment for the users information.
Installation
Please refer the official SciPy website for the instruction. The following sections describe the author's experience of installing SciPy in 3D Slicer.
Mac OS X (tested on 10.6.7 / 64-bit, Tested on April 24, 2011)
The following steps were based on the following web page, but modified for 3D Slicer
Before Installation...
If you have MacPorts or any other UNIX package manager, please make sure that your environment variable PATH doesn't contain any paths to those directories. For example, do not have /opt/local/bin in Mac OS with MacPort installed. Some tools in those paths may screw up the installing process.
Note that the following instruction was prepared for 3D Slicer compiled in 64-bit mode. If you use 3D Slicer build in 32-bit, please try replacing compiler options accordingly (for example, replace '-arch x86_64' with '-arch i386')
Install GNU Fortran
To build SciPy, you need to have GNU Fortran compiler. The official SciPy instruction recommends gfortran-4.2.3 available at [1]
Install 3D Slicer version 3 (latest revision)
Please follow the 3D Slicer Build Instructions, except that:
- You download the latest source code instead of stable release branch. The latest source code is available in the subversion repository at http://svn.slicer.org/Slicer3/trunk
- Turn off NumPy by editing slicer_variables.tcl. The variable that controls NumPy building is around line 250. Please edit it as:
set ::USE_NUMPY "OFF"
Install UMF
We will use Slicer3-lib/python as a working directory. First, obtain UMFPACK, UFConfig, and AMD:
cd ~/tmp wget http://www.cise.ufl.edu/research/sparse/umfpack/current/UMFPACK.tar.gz wget http://www.cise.ufl.edu/research/sparse/UFconfig/current/UFconfig.tar.gz wget http://www.cise.ufl.edu/research/sparse/amd/current/AMD.tar.gz tar xf AMD.tar.gz tar xf UFconfig.tar.gz tar xf UMFPACK.tar.gz
Before start building, configure UMFConfig/UMFConfig.mk (64-bit environment):
F77 = gfortran CFLAGS = -O3 -fno-common -no-cpp-precomp -fexceptions -arch x86_64 LIB = -lstdc++ BLAS = -framework Accelerate LAPACK = -framework Accelerate UMFPACK_CONFIG = -DNCHOLMOD
Then run the following commands:
cp UFconfig/UFconfig.h AMD/Include/ cp UFconfig/UFconfig.h UMFPACK/Include/ cd UMFPACK make
This ends up with the following message, but just ignore the errors (?)
/usr/bin/ranlib: file: libumfpack.a(umf_di_dump.o) has no symbols /usr/bin/ranlib: file: libumfpack.a(umf_dl_dump.o) has no symbols /usr/bin/ranlib: file: libumfpack.a(umf_zi_dump.o) has no symbols /usr/bin/ranlib: file: libumfpack.a(umf_zl_dump.o) has no symbols ranlib libumfpack.a ranlib: file: libumfpack.a(umf_di_dump.o) has no symbols ranlib: file: libumfpack.a(umf_dl_dump.o) has no symbols ranlib: file: libumfpack.a(umf_zi_dump.o) has no symbols ranlib: file: libumfpack.a(umf_zl_dump.o) has no symbols ( cd Demo ; make ) ( cd ../../CHOLMOD ; make library ) /bin/sh: line 0: cd: ../../CHOLMOD: No such file or directory make[2]: *** No rule to make target `library'. Stop. make[1]: *** [../../CHOLMOD/Lib/libcholmod.a] Error 2 make: *** [all] Error 2
Install NumPy/SciPy
We will use Slicer3-lib/python as a working directory as we did for UMF. First, download the source code:
$ cd <Slicer3 working directory>/Slicer3-lib/python $ git clone https://github.com/numpy/numpy.git $ git clone https://github.com/scipy/scipy.git $ cd numpy
To make NumPy with AMD and UMFPACK built above, copy site.cfg.example to site.cfg in the numpy directory and put the following line:
[amd] library_dirs = <Path to the 3D Slicer working directory>/Slicer3-lib/python/umfpack/AMD/Lib include_dirs = <Path to the 3D Slicer working directory>/Slicer3-lib/python/umfpack/AMD/Include amd_libs = amd [umfpack] library_dirs = <Path to the 3D Slicer working directory>/Slicer3-lib/python/umfpack/UMFPACK/Lib include_dirs = <Path to the 3D Slicer working directory>/Slicer3-lib/python/umfpack/UMFPACK/Include umfpack_libs = umfpack
Before compiling NumPy and SciPy, set the following environment variables. If you are using bash (default in Mac OS X)
$ export MACOSX_DEPLOYMENT_TARGET=10.6 $ export LDFLAGS="-arch x86_64" $ export FFLAGS="-m64" $ export CFLAGS="-arch x86_64"
Once you configure site.cfg and the environment variables, you're ready to run setup.py:
$ cd <Slicer3 working directory>/Slicer3-lib/python/numpy $ ../../python-build/bin/python setup.py build --fcompiler=gnu95
If the setup.py fails with the following message, gfortran compiler is not found.
numpy.distutils.fcompiler.CompilerNotFound: gnu95: f90 nor f77
This can be solved by creating a symbolic link to the compiler in /usr/bin. For example:
$ sudo ln -s /usr/local/bin/i686-apple-darwin8-gfortran-4.2 /usr/bin/gfortran
Please note that "sudo ln -s /usr/local/bin/gfortran /usr/bin" doesn't work, since /usr/local/bin/gfortran is a symbolic link.
Then, you need to remove "/usr/local/bin" from PATH environment variable. You can check if gfortran is properly by typing the following commands:
$ which gfortran /usr/bin/gfortran $ gfortran gfortran: no input files
To install NumPy to the copy of Python in Slicer3-lib, run:
$ ../../python-build/bin/python setup.py install
SciPy can be installed by the following process:
$ cd <Slicer3 working directory>/Slicer3-lib/python/scipy $ ../../python-build/bin/python setup.py build --fcompiler=gnu95 $ ../../python-build/bin/python setup.py install
To confirm that SciPy is installed correctly, run Python interpreter and try loading scipy package:
$ ../../python-build/bin/python Python 2.6.6 (r266:163, Apr 21 2011, 14:29:38) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import scipy >>>
Trouble Shooting Tips
In 64-bit Mac OS X environment, even if you successfully finish the building above, you may still have trouble with running Python script that calls SciPy functions, due to 'wrong architecture' error in dynamic linking. For example:
ImportError: dlopen(/Users/junichi/igtdev/slicer3_test/Slicer3-lib/python-build/lib/python2.6/site-packages/scipy/optimize/minpack2.so, 2): no suitable image found. Did find: /Users/junichi/igtdev/slicer3_test/Slicer3-lib/python-build/lib/python2.6/site-packages/scipy/optimize/minpack2.so: mach-o, but wrong architecture
One possible reason of the error is that gfortran generates 32-bit binary, while the other codes (including 3D Slicer) are compiled in 64-bit mode. Although the setup script in SciPy (actually, fortran compiler seems to be handled by python codes come with NumPy) try to figure out which architecture options e.g. "-arch i386", "-arch x86_64") are available and set one, it does not always work properly.
One way (there could be better ways, but I found this worked for me, so far) is to modify the Python code which in charge of setting gfortran options. Two functions defined in gnu.py in the python package directory (<Slicer working directory>/Slicer3-lib/python-build/lib/python2.6/site-packages/numpy/distutils/fcompiler/gnu.py) are in charge of setting architecture options. I modified them as follows:
def get_flags(self): flags = GnuFCompiler.get_flags(self) #arch_flags = self._universal_flags(self.compiler_f90) #if arch_flags: # flags[:0] = arch_flags flags[:0] = ["-m64"] return flags
def get_flags_linker_so(self): flags = GnuFCompiler.get_flags_linker_so(self) #arch_flags = self._universal_flags(self.linker_so) #if arch_flags: # flags[:0] = arch_flags flags[:0] = ["-m64"] return flags
Windows
Linux
Contributor
- Junichi Tokuda (Brigham and Women's Hospital)