Jul 17, 2016 - ... app = QtGui. ... improve the build a
Member of the Helmholtz Association
GR: Plotting with Python or Julia
July 11th – 17th, 2016 Scientific Computing with Python • Austin, Texas | Josef Heinen | @josef_heinen
http://goo.gl/XQsXhi
The main ideas behind GR ✓ procedural graphics backend (completely written in C)
➟ presentation of continuous
✓ significantly improve the performance of existing Matplotlib applications ✓ create plots containing both 2D and 3D graphics elements (from different packages) ✓ produce video contents on the fly by adding a single line of code
➟ no need to import an animation module or write extra code July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
3
Matplotlib using the GR backend $( $&
Arrow
Line2D
%#"#
%$$)"'
$# *
$%"'
(
$#"#
Ellipse
FancyBoxPatch
&
)"'
%
'"#
!
$!
"!! %! $! #! "!
#!
"!
#
%
& +
(
#"* #")
&"(
#"&
&"#
#"#
%"(
135°
$"( &"#
&"(
'"#
'"(
("# #"* #")
&"(
#"&
&"#
#"#
%"( %"#
! #")
$"(
July 11th – 17th, 2016
&"(
#!"# "!)+ "!'( "!%& "!## "!## "!%& "!'( "!)+ #!"#
6 4 0°
( 225°
315°
! #"* &"#
#!
# &$% # %$') # &$* # %$(+ # '$% # '
%$2
! #"&
%"(
%!
$!
10
180°
'"#
%"#
%!
$"%
'"(
$"(
$!
8
! #"* %"(
#!
# %$&'
# &$*
'"#
'"(
# &
%$# %$*
%
%$%$*
&
%$&$*
'
%$)/07,5/2- * (,0*82*9 1,6. !*440/+*7/32 73 ./-.# 5,63087/32 75/+327385/2-"
45°
! #")
+327385- :/7. 0,9,06
%$%%
&
%$! #"&
%"#
%"#
"!
#"#
$#
*
!
! "! (
$"%
'"#
$"(
%$&'
) %
%$%"'
!% !%
'"(
$"# $"#
%$')
90°
4+30351,6. :/7. 0,9,06
("#
&
%$&!
'
PathPatch
%$(+
# %$*
#
Polygon
&$*
%$*
%!
Circle
%$),
&!
$'"#
-
Rectangle
"!!
+ ,
Wedge
051..-7- 7/67 !'%% 431276"
'
%$%%"'
&
$
"
$ $
&
"
$
&
(
"!'
%$' "!* "!( "!& "!$ "!" "!$ "!& "!( "!*
"!% "!$ "!"
%
%$"!$ "!% "!' #!"
# %$'
"!& #!"
& (
(
270°
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
# &$% # &
%$# %$'
%
%$%$'
&
%$"!&
"!" "!"
"!&
"!& #!"
#!"
4
… with Matplotlib’s new colormaps
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
magma plasma inferno viridis terrain seismic rainbow ocean gnuplot2 gnuplot cubehelix CMRmap coolwarm bwr brg afmhot gist_stern gist_rainbow gist_ncar gist_heat gist_earth winter summer spring spectral pink jet hsv hot gray copper cool bone autumn pilatus brownscale flame redscale magentascale bluescale cyanscale greenscale geologic rainbowlike glowing grayscale temperature uniform
5 Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems July 11th – 17th, 2016
Use GR as a MATLAB–like plot interface Advanced LIGO WHITENED strain ) z += 0.05 * Z .* G .* rand((128, 128)) end
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
10
… or 3D (iso surface) using GR inline("mov") Ψv = calculate_electronic_density(3, 2, 0) for alpha in 30:210 isosurface(Ψv, isovalue=0.25, rotation=alpha) end
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
11
Mix 2D and 3D scenes and create video content on the fly
jlgr
GR3
GR
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
12
Use GR with Interact using Interact using GR inline() z = peaks() maps = Dict(“gnuplot"=>39, “viridis"=>44, "inferno"=>45) @manipulate for θ=20:1:70, Φ=20:1:70, cmap=radiobuttons([“gnuplot", “viridis", "inferno"]) surface(z, rotation=θ, tilt=Φ, colormap=maps[cmap]) end July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
13
… or with JavaScript JavaScript
GR can be transpiled to JS ➟ gr.js
(Emscripten: LLVM–to–JavaScript compiler) ➟ call GR functions from JS
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
14
Use GR with Gtk … function paint(w) ctx = Gtk.getgc(w) Gtk.show_text(ctx, "Contour Plot using Gtk ...") ENV["GKS_WSTYPE"] = "142" ENV["GKSconid"] = @sprintf("%lu", UInt64(ctx.ptr)) … x, y, z = gridit(xd, yd, zd, 200, 200) h = linspace(-0.5, 0.5, 20) surface(x, y, z, 5) contour(x, y, h, z, 0) polymarker(xd, yd) axes(0.25, 0.25, -2, -2, 2, 2, 0.01) end win = @Window("Gtk") canvas = @Canvas(600, 450) Gtk.push!(win, canvas) Gtk.draw(paint, canvas) Gtk.showall(win) … Gtk.gtk_main()
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
15
… or with PyQt / PySide class GrWidget(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setup_ui(self) self.rubberband = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self) self.setMouseTracking(True) environ["GKS_WSTYPE"] = "381" … def paintEvent(self, ev): self.painter.begin(self) if have_pyside: environ['GKSconid'] = "%x!%x" % ( long(shiboken.getCppPointer(self)[0]), long(shiboken.getCppPointer(self.painter)[0])) else: environ["GKSconid"] = "%x!%x" % (unwrapinstance(self), unwrapinstance(self.painter)) draw() self.painter.end() app = QtGui.QApplication(argv) win = GrWidget() win.show() exit(app.exec_())
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
16
Use GR in Atom …
✓ powerful IDE ✓ syntax highlighting ✓ command completion ✓ inline graphics
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
17
… or in iTerm2
✓ terminal emulator for macOS ✓ syntax highlighting ✓ command completion ✓ inline graphics
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
18
Render molecules … gr.setviewport(0, 1, 0, 1) gr3.setbackgroundcolor(1, 1, 1, 1) for angle in range(360): gr.clearws() gr3.clear() gr3.drawmolecule('data/dna.xyz', bond_delta=2, tilt=45, rotation=angle) gr3.drawimage(0, 1, 0, 1, 1000, 1000, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.text(0.15, 0.95, "DNA rendered using gr3.drawmolecule") gr.textext(0.15, 0.875, "\\alpha=%2d" % angle) gr.updatews()
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
19
… and export a POV–Ray scene
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
20
Render spins gr.setviewport(0, 1, 0, 1) gr3.setbackgroundcolor(1, 1, 1, 1) gr3.cameralookat(0, -60, 35, 0, -2, 0, 0, 2, 0) for positions, directions, colors in … gr.clearws() gr3.clear() gr3.drawspins(positions, directions, colors) gr3.drawimage(0, 1, 0, 1, 1000, 1000, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) gr.updatews() gr3.export('spins.html', 1000, 1000)
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
21
Demos
✓ GR interoperability example ✓ interactive widget demo for a simple surface plot ✓ visualizing atomic orbitals in a Jupyter notebook ✓ use GR to visualize MRI data in an interactive notebook
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
22
Programs using GR: pyMolDyn
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
23
… GR as a backend for Plots.jl julia> using Plots julia> gr() Plots.GRBackend()
10
10 250
8
75
250
y1 y2
8 200
200
6
150
100
4
6
150
100
4
50 2
Height
70
65
50 2 60 0
5
10
July 11th – 17th, 2016
15
20
5
10
15
20
2
4
6
8
VoicePart
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
24
Current Activities ✓ near completion of a “fully–featured” GR distribution for Windows based on the MinGW–w64 toolchain ✓ improve the build and deployment process (self–contained GR packages) ✓ provide more (MATLAB–like) convenience functions ✓ proof–of–concept for an in–browser JavaScript–based GR interactive renderer (based on gr.js)
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
25
What’s next? IPython
QtTerm
C / C++ ... GR3
GR
W IP
off-screen rendering
socket
communication
direct rendering
JavaScript
generation
POV-Ray
generation
QtTerm
✓ plattform–independent
Browser Qt
WebGL
✓ interactive (zoom, pan,
pick data, …)
OpenGL (WGL / CGL / GLX)
GKS
POV-Ray
EGS ✓ based on ModernGL
W IP
✓ stand–alone viewer
(C++, Qt 5.7)
Swift
PyPy
GKS logical device drivers
gksqt
wx
X11
Quartz
GKSTerm
Win32
Cairo
PNG
N ew
Qt
X11
TikZ
LATEX
PDF
MOV
PS
0MQ
SVG
OpenGL
EGS ...
✓ extensibility using plugins ✓ combines 2D and 3D
HTML5
✓ high–performance rendering ✓ GKS driver
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
26
Why EGS? … because performance matters! E.g. for the visualization of Micro Swimmers …
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
27
… or for the visualization of Skyrmion formations
July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
28
Thank you for your attention Questions? Contact:
[email protected]
@josef_heinen
Thanks to: Florian Rhiem, Ingo Heimbach, Christian Felder, David Knodt, Jörg Winkler, Fabian Beule, Philip Klinkhammer, Marvin Goblet, Daniel Kaiser, et al. July 11th – 17th, 2016
Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems
29