Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Luca Antiga
Medical Imaging Unit, Mario Negri Institute, Bergamo, Italy
Acknowledgements
Steve Piper
Michael Halle
...
Why Python
Python features
Strongly typed
>>> a = 1
>>> b = cow
>>> c = a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Dynamically typed
>>> a = 1
>>> type(a)
<type 'int'>
>>> a = cow
>>> type(a)
<type str>
Python features
Variables as leashes
>>>
>>>
>>>
>>>
a = [1,2,3]
b = a
a[1] = 4
b = [1,4,3]
Basic datatypes
t = (a,2)
t = [a,2]
t = {a:2}; t[a] = 3
def Add(a,b):
return a+b
t = Add
c = t(a,b)
Python features
Numpy, Scipy
array datatype
Multidimensional array
Operations are done in an element by element basis
matrix datatype
Numpy, Scipy
Python / Numpy
a(2:5)
a[1:4]
a(1:end)
a(0:)
a.T
a(a>.5)
a[a>.5]
[V,D]=eig(a)
V,D=linalg.eig(a)
Numpy, Scipy
Numpy: slicing
>>> a[0,3:5]
array([3,4])
>>> a[4:,4:]
array([[44, 45],
[54, 55]])
>>> a[:,2]
array([2,22,52])
>>> a[2::2,::2]
array([[20,22,24]
[40,42,44]])
Slicing does not create
copies of the arrays
contents
[Jones,Oliphant]
Numpy, Scipy
>>> a = arange(0,80,10)
# fancy indexing
>>> y = a[[1, 2, -3]]
>>> print y
[10 20 50]
# fancy indexing
>>> y = a[mask]
>>> print y
[10,20,50]
# using take
>>> y = take(a,[1,2,-3])
>>> print y
[10 20 50]
# using compress
>>> y = compress(mask, a)
>>> print y
[10,20,50]
[Jones,Oliphant]
Numpy, Scipy
5]]
35],
45]])
55]])
[Jones,Oliphant]
Numpy, Scipy
Numpy broadcasting
Semantic of binary operations between arrays
4x3
4x3
+
4x3
stretch
4x1
=
[Oliphant]
stretch
stretch
Python in Slicer
Python VTK
Tcl VTK
C++ VTK
...
Python in Slicer
Tcl Slicer
C++ Slicer
Python in Slicer
Python Slicer
Tcl Slicer
C++ Slicer
Run in the main thread, i.e. they can change the MRML
scene.
Slicer3/Modules/Python/
SurfaceICPRegistration.py
>>>
>>>
>>>
>>>
>>>
>>>
import Slicer
from Slicer import slicer
volume1 = slicer.MRMLScene.GetNodeByID(vtkMRMLVolumeNode1)
volume2 = slicer.MRMLScene.GetNodeByID(vtkMRMLVolumeNode2)
plugin = Slicer.Plugin(Subtract Images)
plugin.Execute(volume1.GetID(),volume2.GetID())
Example: PythonGADScriptedModule
from SlicerScriptedModule import ScriptedModuleGUI
from Slicer import slicer
class PythonGADScriptedModuleGUI(ScriptedModuleGUI):
def __init__(self):
...
def AddGUIObservers(self):
...
def ProcessGUIEvents(self,caller,event):
...
def UpdateGUI(self):
...
def BuildGUI(self):
...