Difference between revisions of "Slicer3:Python"

From NAMIC Wiki
Jump to: navigation, search
m
 
Line 8: Line 8:
 
# Install [http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103 NumPy], be sure to find the correct installer for your version of Python
 
# Install [http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103 NumPy], be sure to find the correct installer for your version of Python
 
# Run CMake setting the "USE_PYTHON" option, and setting the path to your Python installation
 
# Run CMake setting the "USE_PYTHON" option, and setting the path to your Python installation
 +
## Check Advanced Options
 +
## Verify that CMake found your Python installation
 
# Compile Slicer
 
# Compile Slicer
 
## It is best to clean the '''SlicerBaseGUI''' project, as this may not be correctly rebuilt.
 
## It is best to clean the '''SlicerBaseGUI''' project, as this may not be correctly rebuilt.
Line 13: Line 15:
  
 
A new menu command '''Python Interpreter''' should appear on the Window menu.
 
A new menu command '''Python Interpreter''' should appear on the Window menu.
 +
 +
[[Image:PythonMenu.png]]
 +
 +
This command should bring up the '''Python Console''' window.
 +
 +
[[Image:PythonConsole.png]]
 +
 +
=== Basic Slicer/Python tutorial ===
 +
''Note: this is initial documentation only, and is subject to change as the API evolves.''
 +
 +
The Slicer Python interpreter has access to all Python modules referenced by the environment variable '''PYTHONPATH''' as well as the '''Slicer''' internal module.  The '''Slicer''' module also supports the interface between Slicer Volume Nodes and NumPy.  It would be instructive for the reader to review the [http://www.scipy.org/Documentation NumPy documentation].  For serious users of NumPy, there is the [http://www.tramy.us/ ''Guide to NumPy''] by Travis Oliphant for purchase.
 +
 +
In this example, a volume named '''T2_MGH.nhdr''' has been loaded into Slicer.
 +
 +
==== Listing available datasets ====
 +
 +
>>> import Slicer
 +
>>> help ( Slicer.ListVolumeNodes )
 +
Help on function ListVolumeNodes in module Slicer:
 +
 +
ListVolumeNodes()
 +
    Returns a dictionary containing the index and
 +
    vtkMRMLVolumeNodes currently loaded by Slicer
 +
 +
>>> nodes = Slicer.ListVolumeNodes()
 +
>>> nodes
 +
{0: vtkTemp1554}
 +
>>> t2 = nodes[0]
 +
>>> t2.GetName()
 +
'T2_MGH.nhdr'
 +
>>> t2.GetSpacing()
 +
'0.84375 0.84375 0.84 '
 +
>>> print t2.ListMethods()
 +
<<<Output truncated>>>
 +
 +
Help on most Python functions is available using the built in '''help()''' command.  This example shows how to list the available MRML volumes loaded by Slicer, select the volume you would like to use, and print some information from the volume.  An exhaustive list of methods defined on each volume can be found using the
 +
'''ListMethods()''' method on the volume node.
 +
 +
==== NumPy tutorial ====
 +
Python and NumPy give direct access to the volume data in Slicer by wrapping the image data in a NumPy '''array''' object through the '''.ToArray()''' method on the volume node.  This tutorial assumes you have installed [http://scipy.org/Download SciPy].
 +
 +
>>> # Access the image data directly
 +
>>> data = t2.GetImageData().ToArray()
 +
>>> print data
 +
[[[ 0  0  0 ...,  0  0  0]
 +
  [ 0  0  1 ...,  0  1  0]
 +
  [ 0  0  0 ...,  0  0  0]
 +
  ...,
 +
  [ 0  8  0 ...,  0  1  2]
 +
  [ 0  0  0 ...,  4  1  2]
 +
  [ 0  0  2 ...,  6  0  2]]
 +
 +
<<< Some output truncated >>>
 +
 +
  [[ 0  0  0 ...,  0  0  0]
 +
  [ 0  0  0 ...,  0  0  0]
 +
  [ 0  0  0 ...,  0  0  0]
 +
  ...,
 +
  [ 0  3  3 ...,  0  0  8]
 +
  [ 0  6  0 ...,  5  3 13]
 +
  [ 0  5  1 ...,  9  3  8]]]
 +
>>> # Load the image filtering package from scipy
 +
>>> import scipy.ndimage
 +
>>> # Filter into a new array
 +
>>> temp = scipy.ndimage.gaussian_filter ( data, 2.0 )
 +
>>> # Copy back into Slicer
 +
>>> data[:] = temp[:]
 +
 +
Before:
 +
 +
[[Image:PythonBefore.png]]
 +
 +
After:
 +
 +
[[Image:PythonAfter.png]]

Revision as of 15:52, 12 February 2007

Home < Slicer3:Python

Python for scientific computing

Python has a fairly comprehensive package for scientific computing called SciPy. Of main interest to Slicer users/developers is NumPy. NumPy provides most of the features of the Matlab image processing toolbox and numeric computations, but in an Open Source package. A compelling reason to use NumPy is the ease of interaction and integration with Slicer3.

Slicer3 and Python

At the recent Slicer3 Mini-Retreat, a Python interpreter was added to the Slicer build. This may be built by following these steps.

  1. Install Python 2.4 or later
  2. Install NumPy, be sure to find the correct installer for your version of Python
  3. Run CMake setting the "USE_PYTHON" option, and setting the path to your Python installation
    1. Check Advanced Options
    2. Verify that CMake found your Python installation
  4. Compile Slicer
    1. It is best to clean the SlicerBaseGUI project, as this may not be correctly rebuilt.
    2. A new build target SlicerGUIPython should appear

A new menu command Python Interpreter should appear on the Window menu.

PythonMenu.png

This command should bring up the Python Console window.

PythonConsole.png

Basic Slicer/Python tutorial

Note: this is initial documentation only, and is subject to change as the API evolves.

The Slicer Python interpreter has access to all Python modules referenced by the environment variable PYTHONPATH as well as the Slicer internal module. The Slicer module also supports the interface between Slicer Volume Nodes and NumPy. It would be instructive for the reader to review the NumPy documentation. For serious users of NumPy, there is the Guide to NumPy by Travis Oliphant for purchase.

In this example, a volume named T2_MGH.nhdr has been loaded into Slicer.

Listing available datasets

>>> import Slicer
>>> help ( Slicer.ListVolumeNodes )
Help on function ListVolumeNodes in module Slicer:

ListVolumeNodes()
    Returns a dictionary containing the index and
    vtkMRMLVolumeNodes currently loaded by Slicer

>>> nodes = Slicer.ListVolumeNodes()
>>> nodes
{0: vtkTemp1554}
>>> t2 = nodes[0]
>>> t2.GetName()
'T2_MGH.nhdr'
>>> t2.GetSpacing()
'0.84375 0.84375 0.84 '
>>> print t2.ListMethods()
<<<Output truncated>>>

Help on most Python functions is available using the built in help() command. This example shows how to list the available MRML volumes loaded by Slicer, select the volume you would like to use, and print some information from the volume. An exhaustive list of methods defined on each volume can be found using the ListMethods() method on the volume node.

NumPy tutorial

Python and NumPy give direct access to the volume data in Slicer by wrapping the image data in a NumPy array object through the .ToArray() method on the volume node. This tutorial assumes you have installed SciPy.

>>> # Access the image data directly
>>> data = t2.GetImageData().ToArray()
>>> print data
[[[ 0  0  0 ...,  0  0  0]
  [ 0  0  1 ...,  0  1  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  8  0 ...,  0  1  2]
  [ 0  0  0 ...,  4  1  2]
  [ 0  0  2 ...,  6  0  2]]

<<< Some output truncated >>> 

 [[ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  [ 0  0  0 ...,  0  0  0]
  ..., 
  [ 0  3  3 ...,  0  0  8]
  [ 0  6  0 ...,  5  3 13]
  [ 0  5  1 ...,  9  3  8]]]
>>> # Load the image filtering package from scipy
>>> import scipy.ndimage
>>> # Filter into a new array
>>> temp = scipy.ndimage.gaussian_filter ( data, 2.0 )
>>> # Copy back into Slicer
>>> data[:] = temp[:]

Before:

PythonBefore.png

After:

PythonAfter.png