High-performancescientific graphics
in Python
Luke Campagnola2014
Background
PhD Neurobiology 2014,UNC-CH
BS Physics 2004, Colorado School of Mines
13-year Python programmer
Neurobiology
The brain:Neurons connected in complex circuits form the basis of all thought and behavior.
~100 billion neurons~100 trillion synapses
How can we determinethe connectivity of a large,
3D circuit whose connections canonly be seen by EM?
BC
DS
TV
TS
TV
MeasuringFunctional Connectivity
ACQ4
..this requires the coordinationof several pieces of hardware
Must be able to visualize this datawith realtime interactivity,
without interfering with acquisition.
Data acquisition via ctypes andserial interfaces for cameras, NI DAQ
boards, amplifiers, etc.
General-purpose data acquisition system, meant for public release
ACQ4
Is Python suitable for high-performance computing?
Yes, by making use of compiled libraries.
"Premature optimization is the root of all evil."- Donald Knuth
1. Start with code that is readable2. Profile3. Optimize
In Python, optimization sometimesmeans reimplementing parts of the code in C.
..but if you have to go this way, try using a pre-existing compiled library!
The contenders 6 years ago:
MatplotlibPyQwtChacoVTK
Write-your-own
What graphicspackage to use?
Never write whatyou don't need to!
Graphics
Userinterface
Datahandling
Science (the fun stuff)
The sad tale of PyQwta wrapper around Qwt -- fast
scientific graphics based on Qt
- Maintained by one developer
- C++ wrappers are difficult and boring to maintain
Lessons learned:
- Dependencies can be a libability; binary dependencies especially. (CDH)
- The value of an open source project lies more in its community
than in its code!(and fragmentation is a problem)
Code quality:Performance vs Maintainability
We want to avoid issues with dependencies, especially with those that must be compiled
However we also want high performance, andPython is not a high performance language!
Usability
Perf
orm
ance
C
Python
Java
That languageeverybody hates
OpenGL
- Fast!- Very low-level
What graphicspackage to use?
Another option: Qt GraphicsView + numpy
What graphicspackage to use?
ArchitecturePyQtGraph
- Scientific graphics: plots,video, user interaction
Qt GraphicsView- Primitive graphics - lines,
shapes, antialiasing- Scenegraph - item
transformations
NumPy- Fast array operations
10k-100k sample plots1024x1024 video @ 50 fps
PyQtGraph
PyQtgraph is successful because it fills a vacuum.
Where there is a vacuum, there is fragmentation.
- High performance graphics- Publication quality- Emphasis on interactivity- High-level (plots, not lines)
- Community developed! 5 researchers, 2 GSOC students Code sprints @ ESRF in Grenoble
Limitations of QtGraphicsView
- 2D graphics only- Poor GPU utilization
- All transforms are affine + projection- Limited primitives
vispy.sceneScenegraph--interactivity and transformations
Architecture
vispy.visualsPrimitives--lines, shapes, volumes
vispy.ooglObject-oriented interface to OpenGL
NumPy- Fast array operations
(CPU)
1M-10M sample plots (100x faster)
OpenGL- Fast graphics operations
(GPU)
Modern OpenGL programming
Raw Data
Pre-processing
VertexShader
Transformations
FragmentShader
Pixel color
(0.0, 0.0)(0.2, 6.5)(7.3, 4.4)
(-1, -1)
(1, 1)
Bonus:All modern computers have a functioning GLSL compiler
Transformations andthe scenegraph
Vispy supports arbitrary transformationsat any point in the scenegraph
All transformations may be computedon the GPU
In Qt GraphicsView, we are limited to the primitives provided by QPainter.
In OpenGL, we can compile any GPU code we need at runtime.
This means we are free tocreate new primitives as we like.