High-performance scientific graphics in Python -...

30
High-performance scientic graphics in Python Luke Campagnola 2014

Transcript of High-performance scientific graphics in Python -...

Page 1: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

High-performancescientific graphics

in Python

Luke Campagnola2014

Page 2: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Background

PhD Neurobiology 2014,UNC-CH

BS Physics 2004, Colorado School of Mines

13-year Python programmer

Page 3: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Neurobiology

The brain:Neurons connected in complex circuits form the basis of all thought and behavior.

~100 billion neurons~100 trillion synapses

Page 4: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

How can we determinethe connectivity of a large,

3D circuit whose connections canonly be seen by EM?

Page 5: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

BC

DS

TV

TS

TV

MeasuringFunctional Connectivity

Page 6: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

ACQ4

..this requires the coordinationof several pieces of hardware

Page 7: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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

Page 8: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

ACQ4

Page 9: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Is Python suitable for high-performance computing?

Yes, by making use of compiled libraries.

Page 10: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

"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!

Page 11: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

The contenders 6 years ago:

MatplotlibPyQwtChacoVTK

Write-your-own

What graphicspackage to use?

Page 12: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Never write whatyou don't need to!

Graphics

Userinterface

Datahandling

Science (the fun stuff)

Page 13: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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

Page 14: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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)

Page 15: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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!

Page 16: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Usability

Perf

orm

ance

C

Python

Java

That languageeverybody hates

Page 17: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

OpenGL

- Fast!- Very low-level

What graphicspackage to use?

Page 18: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Another option: Qt GraphicsView + numpy

What graphicspackage to use?

Page 19: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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

Page 20: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

PyQtGraph

Page 21: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers
Page 22: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

PyQtgraph is successful because it fills a vacuum.

Where there is a vacuum, there is fragmentation.

Page 23: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

- 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

Page 24: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Limitations of QtGraphicsView

- 2D graphics only- Poor GPU utilization

- All transforms are affine + projection- Limited primitives

Page 25: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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)

Page 26: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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)

Page 27: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Bonus:All modern computers have a functioning GLSL compiler

Page 28: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

Transformations andthe scenegraph

Vispy supports arbitrary transformationsat any point in the scenegraph

All transformations may be computedon the GPU

Page 29: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers

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.

Page 30: High-performance scientific graphics in Python - TriZPUGtrizpug.org/meetings/pyqtgraph-0514-final.pdf · scientific graphics based on Qt - Maintained by one developer - C++ wrappers