Using PypeS programmatically

Applicable version(s):

Latest stable release & Development version

by Carlos Remuzzi, BEng Biomedical Engineering, Freelance Developer, London UK

The first and most immediate use of the Vmtk framework consists in entering your piped scripts on a command line. The vmtk scripts tutorial and the basic PypeS tutorial offer an extensive insight on this matter.

A more radical approach involves the creation of new Python scripts and modules based on the Vmtk libraries. The advanced PypeS tutorial gives you the basic knowledge to start developing with vmtk.

There is at least one more way of working with Vmtk, that is from the Python interactive shell. Advantages of this method are that you get the responsiveness of a command line and at the same time you are able to handle vmtk objects rather than just argument strings.

This section shows you a few examples on how you can handle Vmtk classes from within a Python shell.

First we are going to start our Python shell by entering python in our command line:

python
Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Next we want to import the vmtk.PypeS modules by typing :

>>> from vmtk import pypes

Now we can define our pype arguments as a string:

>>> myArguments = 'vmtkmarchingcubes -ifile myimage.vti -l 800 --pipe vmtksurfaceviewer'

The string myArguments contains the same commands that you would normally enter on a command line. Now we can execute our pype by typing:

>>> myPype = pypes.PypeRun(myArguments)

PypeRun will automatically execute our pype and you will be shown an isosurface extracted from myimage.vti through the vmtkmarchingcubes script.

At this point, if we want to work with the surface generated by our script we don’t need to run another Pype. The vmtkScripts instances created by PypeRun are still loaded and fully accessible.

We can get our surface by simply entering the following instruction:

>>> mySurface = myPype.GetScriptObject('vmtkmarchingcubes','0').Surface

We have used the GetScriptObject() method to get a pypeScript object named vmtkmarchingcubes and with id equal to 0 from the list of pypeScript objects created and used by our pype.

So we have named our surface as mySurface and defined it as the Surface attribute of the pypeScript object returned by GetScriptObject().

Now we can easily handle our new Surface object and use it for as many scripts as we need.

We might want to smooth our surface by calling the vmtkSurfaceSmoothing script. We can do it by typing:

>>> from vmtk import vmtkscripts
>>> mySmoother = vmtkscripts.vmtkSurfaceSmoothing()
>>> mySmoother.Surface = mySurface
>>> mySmoother.PassBand = 0.1
>>> mySmoother.NumberOfIterations = 30
>>> mySmoother.Execute()

We have imported the vmtk.vmtkScripts modules and next we have created a new instance of vmtkSurfaceSmoothing naming it as mySmoother . Then we have set a few attributes of mySmoother and finally we have invoked the Execute() method. Notice how we have easily passed mySurface as an attribute to mySmoother .

Finally if we want to save the smoothed surface as mysurface.vtp we can do it by typing:

>>> myWriter = vmtkscripts.vmtkSurfaceWriter()
>>> myWriter.Surface = mySmoother.Surface
>>> myWriter.OutputFileName = 'mysurface.vtp'
>>> myWriter.Execute()

We have created a new instance of vmtkSurfaceWriter as myWriter, then we have defined the Surface attribute as the output Surface from mySmoother . We have finally defined the filename we want for our surface and executed myWriter

This chain of operations can continue for as long as you need. If you find that your work session is getting long and perhaps you want to reproduce it in different times you can create a Python file and save all of the instructions you have used into a script.

The Python script for this example would look as follows:

#!/usr/bin/env python

from vmtk import pypes
from vmtk import vmtkscripts

myArguments = 'vmtkmarchingcubes -ifile myimage.vti -l 800 --pipe vmtksurfaceviewer'
myPype = pypes.PypeRun(myArguments)

mySurface = myPype.GetScriptObject('vmtkmarchingcubes','0').Surface

mySmoother = vmtkscripts.vmtkSurfaceSmoothing()
mySmoother.Surface = mySurface
mySmoother.PassBand = 0.1
mySmoother.NumberOfIterations = 30
mySmoother.Execute()
myWriter = vmtkscripts.vmtkSurfaceWriter()
myWriter.Surface = mySmoother.Surface
myWriter.OutputFileName = 'mysurface.vtp'
myWriter.Execute()

You can save it as myscript.py and you will be able to run it either from the Python shell or from the command line. To run this code from the command line you simply have to make it executable then invoke it with the ./ prefix as follows:

$ chmod u+x myscript.py
$ ./myscript.py

Further details about writing scripts based on Vmtk and Pypes are discussed in the advanced PypeS tutorial.