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 data streams ✓ builtin support for 2D plotting and OpenGL (GR3) ➟ coexistent 2D and 3D world ✓ support for different programming languages including Python, Julia and JavaScript ✓ backend support for Matplotlib (Python) and Plots (Julia) ✓ interoperability with GUIs (Qt, wx, Gtk) and web applications (Jupyter) July 11th – 17th, 2016 Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems 2 Use GR as a backend for Matplotlib ✓ combine the power of Matplotlib and GR ➟ select GR as a backend by setting the MPLBACKEND environment variable export MPLBACKEND="module://gr.matplotlib.backend_gr" ✓ 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 data near GW150914 using GR figure(figsize=(6,4)) 4 2 whitented strain tevent = 1126259462.422 d = readdlm("strain.dat") time, H1_strain, L1_strain = d[:,1], d[:,2], d[:,3] d = readdlm("nr_waveform.dat") NRtime, NR_H1_whitenbp = d[:,1],d[:,2] H1 strain L1 strain matched NR waveform xlim([-0.1, +0.05]) ylim([-4,+4]) legend("H1 strain", "L1 strain”, "matched NR waveform") xlabel(@sprintf("time (s) since %.3f", tevent)) ylabel("whitented strain") title("Advanced LIGO WHITENED strain data near GW150914") plot(time-tevent, H1_strain, "r", time-tevent, L1_strain, "g", NRtime+0.002, NR_H1_whitenbp, "k") 0 −2 −4 −0.10 −0.05 0 0.05 time (s) since 1126259462.422 July 11th – 17th, 2016 Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems 6 … for 2D using LsqFit x = -16:0.5:35 y = 19.4./((x - 7).^2 + 15.8) + randn(size(x))./10; 1.0 model(x, p) = p[1]./((x-p[2]).^2+p[3]) fit = curve_fit(model, x, y, [25.,10.,10.]) p = fit.param f = p[1]./((x-p[2]).^2+p[3]) 0.5 using GR plot(x, y, "d", x, f, "-", linewidth=2) 0 - 20 July 11th – 17th, 2016 Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems 0 20 40 7 … for 2D (line, scatter) 1.0 1.0 J0 J1 J2 J3 0.8 0.5 0.6 0.4 0 0.2 - 0.5 July 11th – 17th, 2016 0 5 10 15 20 0 0 0.2 0.4 0.6 0.8 1.0 using GR using GR x = linspace(0,20,200) for order in 0:3 plot(x, besselj(order, x)) hold(true) end legend("J_0", "J_1", "J_2", “J_3") n = 500 x = rand(n) y = rand(n) area = pi .* (15 .* rand(n)).^2 scatter(x, y, area, 1:n, alpha=0.5) Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems 8 … or 2D (filled contours) using GR 6000 figure(size=(1000,500)) 5000 600 4000 z = readdlm("TerrainHeight.txt") 3000 400 contourf(max(z’,-1500), xlim=(0,1440), ylim=(0,720), colormap=GR.COLORMAP_TERRAIN) 2000 1000 200 0 - 1000 0 0 July 11th – 17th, 2016 Josef Heinen, Forschungszentrum Jülich, Peter Grünberg Institute, Scientific IT Systems 500 1000 9 … or 3D (surface) using GR inline("mov") figure(size=(800,800)) z = Z = readdlm("sans.dat") G = [ exp(-x^2 -y^2) for x in linspace(-1.5, 1.5, 128), y in linspace(-1.5, 1.5, 128) ] for t = 0:500 surface(z, title="Time: $t s") 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 <canvas id="canvas" width="500" height="500"></canvas> <script type="text/javascript" src="gr.js"></script> <script type="text/javascript"> GR.ready(function() { var gr = new GR(); var t = 0; var x = new Array(629); var y = new Array(629); var draw = function() { gr_clearws(); var i; for (i = 0; i < 629; i++) { x[i] = i / 630.0 * 2 * Math.PI; y[i] = Math.sin(x[i] + t / 10.0); } Use cases: gr_setviewport(0.1, 0.95, 0.1, 0.95); gr_setwindow(0, 8, -1, 1); gr_setcharheight(0.020); gr_grid(0.5, 0.1, 0, -1, 4, 5); gr_axes(0.5, 0.1, 0, -1, 4, 5, 0.01); gr_polyline(629, x, y); gr_updatews(); t = t + 1; if (t < 200) { setTimeout(draw, 1); } ✓ embed JS code in Jypyter ✓ interpret GR display list in the browser }; draw(); }); </script> 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
© Copyright 2024 ExpyDoc