Difference between revisions of "Documentation/4.1/Developers/Python scripting"

From Slicer Wiki
Jump to: navigation, search
(Created page with '# do this before building slicer4 - or do "cd Slicer4-superbuild/; rm -rf python* ; make" sudo apt-get install libreadline6-dev cd Slicer4-superbuild # this should be done in s…')
 
Line 1: Line 1:
# do this before building slicer4 - or do "cd Slicer4-superbuild/; rm -rf python* ; make"
+
# Background
sudo apt-get install libreadline6-dev
 
  
cd Slicer4-superbuild
+
This is an evolution of the [[Slicer3:Python|python implementation in slicer3]].  Slicer's APIs are now natively wrapped in python.
  
# this should be done in superbuild script
+
# Usage options
 +
 
 +
## Python Interactor
 +
 
 +
Use the Window->Python Interactor (Control-P on window/linux, Command-P on mac) to bring up the Qt-based console with access to the vtk, Qt, and Slicer wrapped APIs.
 +
 
 +
Most python code can be installed and run from this window, but because it exists in the event driven Qt GUI environment, some operations like, like parallel processing or headless operation, are not easily supported.
 +
 
 +
## In iPython
 +
 
 +
[http://ipython.scipy.org iPython] is a powerful shell and can also be used to access the vtk and slicer APIs (but not Qt at the moment).
 +
 
 +
As of Slicer4 beta in February 2011, it is possible to use these steps for installation.  This has only been tested on a ubuntu linux system so far.
 +
 
 +
These instructions assume you have a Slicer4-superbuild directory created according to the [[Slicer4:Build_Instructions|Slicer4 Build Instructions]].
 +
 
 +
### Prerequisites
 +
 
 +
* Get readline - it will make iPython more useful.
 +
 
 +
# do this before building slicer4 - or do "cd Slicer4-superbuild/; rm -rf python* ; make"
 +
sudo apt-get install libreadline6-dev
 +
 
 +
cd to your Slicer4-superbuild directory for the rest of these steps
 +
 
 +
* Install the vtk package in the python tree
 +
# TODO: this should be done in superbuild script
 
  (cd ./VTK-build/Wrapping/Python;  ../../../Slicer-build/Slicer4 --launch ../../../python-build/bin/python setup.py install)
 
  (cd ./VTK-build/Wrapping/Python;  ../../../Slicer-build/Slicer4 --launch ../../../python-build/bin/python setup.py install)
  
 +
* Install ipython:
  
git clone git://github.com/ipython/ipython.git
+
git clone git://github.com/ipython/ipython.git
git checkout 0.10.2
+
git checkout 0.10.2
 
  (cd ./ipython;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
 
  (cd ./ipython;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
  
 +
* Install matplotlib (remove the source after installing so python import will not get confused by it.)
  
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint matplotlib-source
+
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint matplotlib-source
 
  (cd ./matplotlib-source;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
 
  (cd ./matplotlib-source;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
 +
rm -rf matplotlib-source
 +
 +
### Now try it!
 +
 +
Launch an xterm with all the paths set correctly to find the slicer python packages
 +
 +
./Slicer-build --launch xterm &
  
 +
Now, ''inside the xterm'' launch ipython
  
rm -rf matplotlib-source
+
./python-build/bin/ipython
  
./Slicer-build --launch xterm &
+
Inside ipython you can past the following script that does:
 +
* create a mrml scene and add a volume
 +
* make a numpy aray from the image data
 +
* do calculations in numpy and vtk for comparision
 +
* make a histogram plot of the data
  
# in xterm:
+
<pre>
./python-build/bin/ipython
 
  
 
import vtk
 
import vtk
Line 41: Line 79:
 
n, bins, patches = matplotlib.pyplot.hist(a, 50, facecolor='g', alpha=0.75)
 
n, bins, patches = matplotlib.pyplot.hist(a, 50, facecolor='g', alpha=0.75)
 
matplotlib.pyplot.show()
 
matplotlib.pyplot.show()
 +
 +
</pre>

Revision as of 21:58, 26 February 2011

Home < Documentation < 4.1 < Developers < Python scripting
  1. Background

This is an evolution of the python implementation in slicer3. Slicer's APIs are now natively wrapped in python.

  1. Usage options
    1. Python Interactor

Use the Window->Python Interactor (Control-P on window/linux, Command-P on mac) to bring up the Qt-based console with access to the vtk, Qt, and Slicer wrapped APIs.

Most python code can be installed and run from this window, but because it exists in the event driven Qt GUI environment, some operations like, like parallel processing or headless operation, are not easily supported.

    1. In iPython

iPython is a powerful shell and can also be used to access the vtk and slicer APIs (but not Qt at the moment).

As of Slicer4 beta in February 2011, it is possible to use these steps for installation. This has only been tested on a ubuntu linux system so far.

These instructions assume you have a Slicer4-superbuild directory created according to the Slicer4 Build Instructions.

      1. Prerequisites
  • Get readline - it will make iPython more useful.
# do this before building slicer4 - or do "cd Slicer4-superbuild/; rm -rf python* ; make"
sudo apt-get install libreadline6-dev

cd to your Slicer4-superbuild directory for the rest of these steps

  • Install the vtk package in the python tree
# TODO: this should be done in superbuild script
(cd ./VTK-build/Wrapping/Python;  ../../../Slicer-build/Slicer4 --launch ../../../python-build/bin/python setup.py install)
  • Install ipython:
git clone git://github.com/ipython/ipython.git
git checkout 0.10.2
(cd ./ipython;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
  • Install matplotlib (remove the source after installing so python import will not get confused by it.)
svn co https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint matplotlib-source
(cd ./matplotlib-source;  ../Slicer-build/Slicer4 --launch ../python-build/bin/python setup.py install)
rm -rf matplotlib-source
      1. Now try it!

Launch an xterm with all the paths set correctly to find the slicer python packages

./Slicer-build --launch xterm &

Now, inside the xterm launch ipython

./python-build/bin/ipython

Inside ipython you can past the following script that does:

  • create a mrml scene and add a volume
  • make a numpy aray from the image data
  • do calculations in numpy and vtk for comparision
  • make a histogram plot of the data

import vtk
import slicer
mrml = slicer.vtkMRMLScene()
vl = slicer.vtkSlicerVolumesLogic()
vl.SetAndObserveMRMLScene(mrml)
n = vl.AddArchetypeVolume('../Slicer4/Testing/Data/Input/MRHeadResampled.nhdr', 'CTC')
i = n.GetImageData()
print (i.GetScalarRange())

import vtk.util.numpy_support
a = vtk.util.numpy_support.vtk_to_numpy(i.GetPointData().GetScalars())
print(a.min(),a.max())

import matplotlib
import matplotlib.pyplot
n, bins, patches = matplotlib.pyplot.hist(a, 50, facecolor='g', alpha=0.75)
matplotlib.pyplot.show()