Core 1b – Engineering Computational Platform

Download Report

Transcript Core 1b – Engineering Computational Platform

NA-MIC
National Alliance for Medical Image Computing
http://na-mic.org
Core 1b – Engineering
Dynamic Coding
a.k.a. Python in Slicer
Steve Pieper
Isomics, Inc.
Overview
•
•
•
•
The Role of Dynamic Coding
Practical Slicer Python Programming
Python Examples and Resources
Bonus Language
National Alliance for Medical Image Computing
http://na-mic.org
Typical Development Workflows
Start Slicer
Start Slicer
Load Data
Load Data
Test Code
Test Code
Edit Code
Edit Code
Compile
Reload
C++
Python
National Alliance for Medical Image Computing
http://na-mic.org
Tradeoffs: C++
• C++ Advantages
– Access to the Compiler
• Templates
• Iterators
• Declared Typing
– Efficiency: Pointer Arithmetic / Iterators
– Symbolic Debugging
• C++ Negatives
– Verbosity
– Run-Time Rigidity
– Errors Often Fatal
National Alliance for Medical Image Computing
http://na-mic.org
Tradeoffs: Python
• Python Advantages
– Expressiveness: Less Code == Less Clutter
– Run-Time Flexibility
• Code Can be Redefined on the Fly
• Console Access to Program State
– Unified Coding / Testing / Debugging
• Python Negatives
– Incomplete
• Not all C/C++ APIs are Exposed
• Not an “Assembler Replacement”
– Asymmetric: Less Common to Expose Python APIs to C++
– Code Size / Load Time
– Heavy Dependencies
National Alliance for Medical Image Computing
http://na-mic.org
Language Independent Truisms
•
Bad Code is Bad Code
– Follow Good Design Examples: Qt
•
Programming Is a Lot of Work
– Write Tests & Documentation; Support Your Code
•
Consider Your Job, Colleagues, and Users
– Write Maintainable, Reusable Code
– Provide a Clean Interface (Ideally Pure Data - MRML)
– Don’t Sacrifice the User’s Interests for the Sake of Language Purity
•
Try to Pick the Right Tool for the Job
– If There’s a Great Library, Use It.
•
Always Consider the License
– Good: BSD, Apache 2.0, LGPL
– Bad: GPL, Proprietary, “Free for Academic”, Closed Source
National Alliance for Medical Image Computing
http://na-mic.org
Python Interaction Example
b = qt.QPushButton('Toggle')
b.connect('clicked()', toggle)
b.show()
National Alliance for Medical Image Computing
http://na-mic.org
What’s Available in Python?
• Basically Everything
–
–
–
–
–
VTK Pipelines
Qt Interfaces via PythonQt
SimpleITK (Work in Progress)
MRML, Slicer Logic, Slicer GUI
Modules Logics, CLIs
• But there are Limits
– Slicer Ships with Python 2.6 (not 2.7 or 3.x)
– Some Popular Python Libraries NOT Available in Distribution
• Scipy, matplotlib, iPython, PyOpenCL…
– Some Open Design Tradeoffs
• Reasonable People can Disagree on Best Implementation Choice
for Any Given Task
National Alliance for Medical Image Computing
http://na-mic.org
Structure of a Scripted Module
• Logic
– Structure as a ‘Python Module’ for Import in Other ‘Slicer
Modules’
– No GUI Dependencies
• No QtGui or vtkRendering classes
• Widget
– Implements the Slicer Module GUI
– Package Components Reusable in Other Modules (Editor)
• Self Test
– Essential Component of Your Development Workflow
– Runable by End User on Their Installation
– Automatically a CTest
National Alliance for Medical Image Computing
http://na-mic.org
Examples and Resources
• DICOM Module
• Editor Module
– Editor Extensions
•
•
•
•
DataProbe, LabelStatistics, Endoscopy…
Diffusion Processing Wizard
PET/CT Extension
Self Tests
– RSNA 2012
– Atlas Tests
• Python Programming Tutorial at slicer.org
National Alliance for Medical Image Computing
http://na-mic.org
Bonus Language: JavaScript!
•
Arguably the Most Popular Language in 2012 (google it)
– You Probably Spend a Good Portion of Your Day Running JavaScript
(GMail, github, …)
– JavaScript is a Great Language (XTK…)
•
Qt (and therefore Slicer) Includes a Full WebKit Engine
–
–
–
–
Like in Chrome and Safari
HTML5/CSS/JavaScript: World Class Look & Feel
Better WebKit in Qt5, Coming Soon (WebGL, …)
Great JavaScript Libraries Work in Slicer
• jQuery, jQueryUI, d3js…
•
Slicer 4.2 Usage in Slicer
– qMRMLChartView Implemented with jQPlot
– Extension Manager is a qWebView
•
Remember the Programming Truisms…
National Alliance for Medical Image Computing
http://na-mic.org