Difference between revisions of "2013 Project Week Breakout Session:Slicer4Python"

From NAMIC Wiki
Jump to: navigation, search
Line 37: Line 37:
  
 
== Configuring slicer to use the module==
 
== Configuring slicer to use the module==
 +
 +
[[image:VolumeScrollerModule.png|thumb|right|200px|Initial module as created from the template]]
 +
 +
You are now ready to test your module.  The easiest way to do this is by specifying the path on the command line, like this:
 +
 +
./Slicer-build/Slicer --additional-module-paths ../VolumeTools/VolumeScroller
 +
 +
The path above assumes you are in the Slicer-superbuild directory of a local build directory, but you can substitute the appropriate path to start slicer.
 +
 +
From there, you should find the VolumeScroller module in the Examples category of the module menu.  Congratulations!  You are now ready to start programming your scripted module.
 +
 +
=== Notes for "Real World" usage ===
 +
 +
For the purposes of this tutorial we don't worry about some things.  But if you are planning to complete the process of making this into an extension you should refer to the information about [http://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers/Build_Module how to build the module with CMake], [http://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers/Tutorials/BundleModulesIntoExtension how to bundle the module], and other extension-related topics on the "How To" column on the right of the [http://www.slicer.org/slicerWiki/index.php/Documentation/Nightly/Developers slicer developer documentation page].  Even though we are making a scripted extension, as of now it is still necessary to use CMake and have a local build tree for at least your initial submission of an extension.
 +
 
== Basic Development Cycle==
 
== Basic Development Cycle==
 
=== Edit / Reload the code===
 
=== Edit / Reload the code===

Revision as of 20:45, 12 June 2013

Home < 2013 Project Week Breakout Session:Slicer4Python
Back to Summer project week Agenda

Goals

The material here provides a guided walk through of the resources available for python scripting in Slicer 4. It is based on what is available in the nightly builds as of project week (June 17, 2013). One goal is to demonstrate the development of a python scripted slicer module. Another goal is to provide A Guide to Python in Slicer for the Casual Power User which means that this walkthrough of the features of slicer should give you an idea how to make a custom module that helps organize and automate your processing. This can be useful for your own research or you can create helper modules that simplify the work for users who are exploring an algorithm or working with a large set of studies.

Topics

Creating a scripted module from templates with ModuleWizard

This topic is covered in the ModuleWizard documentation. It allows you to create a skeleton extension with any combination of modules you want.

Prerequisites

We'll go through the steps on a Mac, but the steps are essentially the same on all platforms. We'll be using a locally built version of slicer created using these instructions, but the steps can also be performed using a binary download as long as you have a checkout of the slicer source code available.

Make an Extension

For this example we'll make an extension with a single scripted module as a demonstration. We run the following command from the Slicer source directory.

./Utilities/Scripts/ModuleWizard.py --template ./Extensions/Testing/ScriptedLoadableExtensionTemplate --target ../VolumeTools VolumeTools

We call the extension "VolumeTools" because we will use it to host a demonstration scripted module that scrolls through the volumes available in the current scene.

Make a Scripted Module

Now we want to put a scripted module inside the extension. We can do this with this command (again from the Slicer source directory)

./Utilities/Scripts/ModuleWizard.py --template ./Extensions/Testing/ScriptedLoadableExtensionTemplate/ScriptedLoadableModuleTemplate --target ../VolumeTools/VolumeScroller VolumeScroller

Note that we've used the Module template, which is inside the Extension template and directed the result to go inside of our new extension.

Set up the CMakeLists.txt file

Since the template included a stand-in scripted module, we want to delete it and tell CMake to use our newly created VolumeScroller module instead. We also want to get rid of the dummy module. These commands on unix can do this:

rm -rf ../VolumeTools/ScriptedLoadableModuleTemplate
perl -pi -e 's/ScriptedLoadableModuleTemplate/VolumeScroller/g' ../VolumeTools/CMakeLists.txt

Note that you'll actually want to edit the CMakeLists.txt file by hand, since it contains the metadata about your extension, like the author, category, documentation URL, etc.

Configuring slicer to use the module

Initial module as created from the template

You are now ready to test your module. The easiest way to do this is by specifying the path on the command line, like this:

./Slicer-build/Slicer --additional-module-paths ../VolumeTools/VolumeScroller

The path above assumes you are in the Slicer-superbuild directory of a local build directory, but you can substitute the appropriate path to start slicer.

From there, you should find the VolumeScroller module in the Examples category of the module menu. Congratulations! You are now ready to start programming your scripted module.

Notes for "Real World" usage

For the purposes of this tutorial we don't worry about some things. But if you are planning to complete the process of making this into an extension you should refer to the information about how to build the module with CMake, how to bundle the module, and other extension-related topics on the "How To" column on the right of the slicer developer documentation page. Even though we are making a scripted extension, as of now it is still necessary to use CMake and have a local build tree for at least your initial submission of an extension.

Basic Development Cycle

Edit / Reload the code

Develop self-tests

Refine functionality

Document

Package as extension

The hard parts

building the GUI

finding the right slicer class/calls to implement what you want

debugging

optimizing performance

Special Topics

The slicer architecture

the qSlicerApplication

the vtkSlicerApplicationLogic

the vtkMRMLScene

slicer.modules

slicer.util

Manipulating the slicer session via python

Working with python modules that are not bundled with slicer's binary distribution