# Difference between revisions of "Slicer3:Python"

Line 99: | Line 99: | ||

[[Image:PythonAfter.png]] | [[Image:PythonAfter.png]] | ||

+ | |||

+ | == Matplotlib plotting functionality == | ||

+ | Matplotlib may be used from within Slicer, but currently (on the Mac) the Tk backend locks up and crashes Slicer. However, Matplotlib may still be used through one of the non-GUI backends. More details can be found on the [http://matplotlib.sourceforge.net/ MatPlotLib] | ||

+ | pages. | ||

+ | |||

+ | import matplotlib | ||

+ | matplotlib.use ( 'Agg' ) | ||

+ | from pylab import * | ||

+ | |||

+ | t1 = arange(0.0, 5.0, 0.1) | ||

+ | t2 = arange(0.0, 5.0, 0.02) | ||

+ | t3 = arange(0.0, 2.0, 0.01) | ||

+ | |||

+ | subplot(211) | ||

+ | plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', t2, cos(2*pi*t2)*exp(-t2), 'k') | ||

+ | grid(True) | ||

+ | title('A tale of 2 subplots') | ||

+ | ylabel('Damped') | ||

+ | |||

+ | subplot(212) | ||

+ | plot(t3, cos(2*pi*t3), 'r--') | ||

+ | grid(True) | ||

+ | xlabel('time (s)') | ||

+ | ylabel('Undamped') | ||

+ | savefig ( 'MatplotlibExample.png' ) | ||

+ | |||

+ | [[Media:MatplotlibExample.png]] | ||

+ | |||

== Where to go from here == | == Where to go from here == |

## Revision as of 03:14, 18 August 2008

Home < Slicer3:Python## Contents

## Status of Python in Slicer

In post-3.2 Slicer3, Python support is currently disabled by default. However, it is easy to enable and should compile on all tested platform (Mac, Windows, Linux). Though matplotlib is still not properly interacting with Slicer3, NumPy and Python support should work well for all platforms. Python command line modules are well supported, as is building full GUI modules in 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

#### Enabling Python in the Slicer Build

- Edit slicer_variables.tcl
- Change "set ::USE_PYTHON "off"" to "on"

**Optional:**If you prefer to use your system Python installation, change set ::USE_SYSTEM_PYTHON "false" to "true"- This will give an error under Linux and Windows, but it can be easily fixed by adding the system path for Python, just search for "if { $::USE_SYSTEM_PYTHON } {"

**Optional:**installing SciPy- Download the scipy egg for your platform
- Unzip the egg in any directory that is part of Slicer's Python path, eg Slicer3-build/lib/Slicer3/Plugins
- SciPy should now be available in Slicer's Python interpreter

Build Slicer3 using "getbuildtest.tcl", i.e.:

Macintosh:Slicer3 blezek$ Scripts/getbuildtest.tcl

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

This command should bring up the **Python Console** window.

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

The main interface to Slicer from Python is through the Slicer Python module. From there, the Python user can access any of Slicer's global objects and MRML tree. For example:

>>> import Slicer >>> Slicer.slicer <Slicer.Slicer object at 0x9f325f0> >>> Slicer.slicer.MRMLScene.GetNumberOfNodesByClass ( 'vtkMRMLVolumeNode' ) 0 >>>

In this case, no 'vtkMRMLVolumeNode's were loaded into Slicer.

VTK objects may be easily constructed.

>>> a = Slicer.slicer.vtkPolyData() >>> help ( a ) Help on vtkPolyData in module Slicer object: class vtkPolyData(vtkPointSet) | Method resolution order: | vtkPolyData | vtkPointSet | vtkDataSet <output truncated>

VTK objects created in this manner conform as closely as possible to the VTK Python bindings, but must be created through the slicer object inside the Slicer module. Each object may be fully subclassed following the meta object pattern.

#### 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[:] >>> t2.Modified()

Before:

After:

## Matplotlib plotting functionality

Matplotlib may be used from within Slicer, but currently (on the Mac) the Tk backend locks up and crashes Slicer. However, Matplotlib may still be used through one of the non-GUI backends. More details can be found on the MatPlotLib

pages.

import matplotlib matplotlib.use ( 'Agg' ) from pylab import *

t1 = arange(0.0, 5.0, 0.1) t2 = arange(0.0, 5.0, 0.02) t3 = arange(0.0, 2.0, 0.01) subplot(211) plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', t2, cos(2*pi*t2)*exp(-t2), 'k') grid(True) title('A tale of 2 subplots') ylabel('Damped') subplot(212) plot(t3, cos(2*pi*t3), 'r--') grid(True) xlabel('time (s)') ylabel('Undamped') savefig ( 'MatplotlibExample.png' )

## Where to go from here

Far from exhaustive, this documentation is intended to whet the appitite of researchers who love the power of Matlab, but feel trapped inside, unable to break out and deploy applications. This Python interface to Slicer is intended to help! Here are some selected resources to begin to understand the power of what the combined NumPy - Slicer package offers.