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
Typical Development Workflows Start Slicer Load Data Test Code Edit Code Compile Start Slicer Load Data Test Code Edit Code Reload C++ Python
Tradeoffs: C++ C++ Advantages C++ Negatives Access to the Compiler Templates Iterators Declared Typing Efficiency: Pointer Arithmetic / Iterators Symbolic Debugging C++ Negatives Verbosity Run-Time Rigidity Errors Often Fatal
Tradeoffs: Python Python Advantages Python Negatives 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
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
Python Interaction Example b = qt.QPushButton('Toggle') b.connect('clicked()', toggle) b.show()
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
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
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
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…