High-performance scientific graphics in Python Luke Campagnola 2014 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 determine the connectivity of a large, 3D circuit whose connections can only be seen by EM? B C T S D S T V T V Measuring Functional Connectivity ..this requires the coordination of several pieces of hardware ACQ4 General-purpose data acquisition system, meant for public release Data acquisition via ctypes and serial interfaces for cameras, NI DAQ boards, amplifiers, etc. Must be able to visualize this data with realtime interactivity, without interfering with acquisition. 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 readable 2. Profile 3. Optimize In Python, optimization sometimes means reimplementing parts of the code in C. ..but if you have to go this way, try using a pre-existing compiled library! What graphics package to use? The contenders 6 years ago: Matplotlib PyQwt Chaco VTK Write-your-own Never write what you don't need to! User interface Data handling Graphics Science (the fun stuff) The sad tale of PyQwt a 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, and Python is not a high performance language! Performance C Java Python That language everybody hates Usability What graphics package to use? OpenGL - Fast! - Very low-level What graphics package to use? Another option: Qt GraphicsView + numpy Architecture PyQtGraph - Scientific graphics: plots, video, user interaction NumPy - Fast array operations Qt GraphicsView - Primitive graphics - lines, shapes, antialiasing - Scenegraph - item transformations 10k-100k sample plots 1024x1024 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 Qt GraphicsView - 2D graphics only - Poor GPU utilization - All transforms are affine + projection - Limited primitives Architecture vispy.scene Scenegraph--interactivity and transformations vispy.visuals Primitives--lines, shapes, volumes vispy.oogl Object-oriented interface to OpenGL NumPy - Fast array operations (CPU) OpenGL - Fast graphics operations (GPU) 1M-10M sample plots (100x faster) Modern OpenGL programming Raw Data Pre-processing Vertex Shader Fragment Shader Transformations Pixel color (1, 1) (0.0, 0.0) (0.2, 6.5) (7.3, 4.4) (-1, -1) Bonus: All modern computers have a functioning GLSL compiler Transformations and the scenegraph Vispy supports arbitrary transformations at any point in the scenegraph All transformations may be computed on 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 to create new primitives as we like.