Difference between revisions of "Slicer3:Execution Model Documentation:Python"

From NAMIC Wiki
Jump to: navigation, search
(New page: A Python interpreter has been integrated into Slicer3. This interpreter may be used to execute Python script plugins. The modules have a few specific requirements to be correctly found a...)
 
m (Text replacement - "http://www.slicer.org/slicerWiki/index.php/" to "https://www.slicer.org/wiki/")
 
(33 intermediate revisions by 4 users not shown)
Line 1: Line 1:
A Python interpreter has been integrated into Slicer3.  This interpreter may be used to execute Python script plugins.  The modules have a few specific requirements to be correctly found and used as plugins.  Like the standard executable and shared library plugins, Python plugins need to be self describing.  To do this, the script must have a top level variable called '''XML''' or provide a '''toXML''' procedure. For example:
+
<big>'''Note:''' We are migrating this content to the slicer.org domain - <font color="orange">The newer page is [https://www.slicer.org/wiki/Slicer3:Execution_Model_Documentation:Python  here]</font></big>a
 
 
XML = """<?xml version="1.0" encoding="utf-8"?>
 
<executable>
 
  <category>Filtering.Denoising</category>
 
  ...
 
 
def toXML():
 
  return XML;
 
 
 
 
 
Details of the XML format are found in the main [[Slicer3:Execution Model Documentation | Execution Model documentation]].  Rather than construct a command line to pass into Python, Slicer3 directly calls an '''Execute''' procedure.  It is assumed that the '''Execute''' function expects positional arguments first, and any optional arguments are passed in as keyword arguments.  For instance, the GradientAnisotropicDiffusion.py module provides an '''Execute''':
 
 
 
def Execute ( inputVolume, outputVolume, conductance=1.0, timeStep=0.0625, iterations=1 ):
 
    print "Executing Python Demo Application!"
 
    Slicer = __import__ ( "Slicer" );
 
    slicer = Slicer.Slicer()
 
    in = slicer.MRMLScene.GetNodeByID ( inputVolume );
 
    out = slicer.MRMLScene.GetNodeByID ( outputVolume );
 
   
 
    filter = slicer.vtkITKGradientAnisotropicDiffusionImageFilter.New()
 
    filter.SetConductanceParameter ( conductance )
 
    filter.SetTimeStep ( timeStep )
 
    filter.SetNumberOfIterations ( iterations )
 
    filter.SetInput ( in.GetImageData() )
 
    filter.Update()
 
    out.SetAndObserveImageData(filter.GetOutput())
 
    return
 
 
 
 
 
The function first constructs a '''Slicer''' object by importing the Slicer module.  The '''Slicer''' object is the main interface into Slicer3 as a whole.  The first two arguments, '''inputVolume''' and '''outputVolume''' are not proper MRMLVolumes, and must be looked up using the '''Slicer''' object.  The filter is constructed through the '''Slicer''' object, and the parameters are set.  After the filter is updated, the output image is put using the '''SetAndObserverImageData''' method on the output volume.
 
 
 
===== ToDo =====
 
* Progress functionality
 
* Casting image arguments to proper MRML volume objects before calling '''Execute'''
 

Latest revision as of 17:28, 10 July 2017

Home < Slicer3:Execution Model Documentation:Python

Note: We are migrating this content to the slicer.org domain - The newer page is herea