projects links |
Tutorials / PypesProgrammatically
Using PypeS programmaticallyby 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 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 >>> 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 >>> myPype = pypes.PypeRun(myArguments)
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 So we have named our surface as 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 >>> from vmtk import vmtkscripts >>> mySmoother = vmtkscripts.vmtkSurfaceSmoothing() >>> mySmoother.Surface = mySurface >>> mySmoother.PassBand = 0.1 >>> mySmoother.NumberOfIterations = 30 >>> mySmoother.Execute() We have imported the Finally if we want to save the smoothed surface as >>> myWriter = vmtkscripts.vmtkSurfaceWriter() >>> myWriter.Surface = mySmoother.Surface >>> myWriter.OutputFileName = 'mysurface.vtp' >>> myWriter.Execute() We have created a new instance of 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 $ chmod u+x myscript.py $ ./myscript.py Further details about writing scripts based on Vmtk and Pypes are discussed in the advanced PypeS tutorial . |