1 / 59

SunPy : Python for solar physics

Steven Christe 1, , Matt Earnshaw 2 , Keith Hughitt 1 , Jack Ireland 1 , Florian Mayer 3 , Albert Shih 1 , Alex Young 1 1 NASA GSFC 2 Imperial College London 3 Vienna University of Technology. SunPy : Python for solar physics. Florian Mayer. Outline. What is Python?

kordell
Télécharger la présentation

SunPy : Python for solar physics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Steven Christe1,, Matt Earnshaw2, Keith Hughitt1, Jack Ireland1, Florian Mayer3, Albert Shih1, Alex Young1 1 NASA GSFC2 Imperial College London 3Vienna University of Technology SunPy: Python for solar physics Florian Mayer

  2. Outline • What is Python? • Introduction to Python • Scientific Python • NumPy • Matplotlib • SciPy • Python in solar physics

  3. What is Python? • General-purpose • Object-oriented (disputed) • Cross-platform • Windows • Mac OS • Linux • Other Unices (FreeBSD, Solaris, etc.) • High-level

  4. Who uses Python? • Internet companies • Google • Rackspace • Games • Battlefield 2 • Civilization 4 • Graphics • Walt Disney • Science • NASA • ESRI

  5. Why Python? • Easy • Comprehensive standard library (“batteries included”) Quality does vary, though. • Good support for scientific tasks • Permissive open-source license On the downside: • Slower, but ways to speed up

  6. Python / IDL Python IDL Proprietary software License cost Small community Cumbersome plotting Solar software • Free open-source software • Without cost • General purpose • Good plotting • No solar software

  7. History of Python • Implementation started 1989 by Guido van Rossum (BDFL) • 2.0 appeared 2000 • Garbage collection • Unicode • 3.0 appeared 2008

  8. Python in science • Astronomy • Artificial intelligence & machine learning • Bayesian Statistics • Biology (including Neuroscience) • Dynamical systems • Economics and Econometrics • Electromagnetics • Electrical Engineering • Geosciences • Molecular modeling • Signal processing • Symbolic math, number theory

  9. Python in astronomy • pyFITS– read FITS files • pyRAF – run IRAF tasks • pywcs • pyephem– compute positions of objects in space  • spacepy (space sciences, just released) • Planned standard library AstroPy

  10. Zen of Python by Tim Peters • Beautiful is better than ugly. • Explicit is better than implicit. • Simple is better than complex. • Readabilitycounts. • There should be one – and preferably only one – obvious way to do it. • Although that way may not be obvious at first unless you're Dutch. • >>> import this

  11. Python tutorial Brief introduction into Python

  12. Python as a calculator • Infix notation operations • Python 2 defaults to floor division • More mathematical operations in math • Complex math in cmath

  13. Integers and Floats • Integers are arbitrary size. • Floats are platform doubles. • decimal module for arbitrary precision decimal numbers • fractions module for fractions

  14. Strings and Unicode Strings / bytes Unicode u"foo" Store unicode codepoints Useful for text Behave as expected for multibyte characters • "foo" • Store bytes • Useful for binary data

  15. Lists and Tuples • [1, 2, 3, 4] • Mutable • Multiple records • (1, u"foo") • Immutable • Different objects describing one record

  16. Control flow • if/elif/else • for-loop • break • continue • else • while-loop • pass

  17. Functions • Default arguments are evaluated once at compile time! • lambda alternative syntax for definition of trivial functions • Functions are objects, too!

  18. Dictionaries • Unordered key-value mappings • Approx. O(1) lookup and storage • Keys must be immutable (hashable)

  19. Sets • Unordered collection of unique objects • Approx. O(1) membership test • Members must be immutable (hashable)

  20. Object-orientation • Classes • Explicit self • Multiple inheritance • Also in IDL 8; no escaping it

  21. Exception handling • try / except / else • raise • Exceptions inherit from Exception

  22. Current versions Python 2.7 Python 3.2 Print function Bytes / String Float Division Absolute imports Views • Print statement • String / Unicode • Floor division • Relative imports • Lists Tons of other changes http://bit.ly/newpy3

  23. NumPy • Fundamental package for science in Python • Multidimensional fixed-size, homogenous arrays • Derived objects: e.g. matrices • More efficient • Less code

  24. NumPy: Create arrays • Python list • arange • linspace / logspace • ones / zeros / eye / diag • random

  25. NumPy: Vectorization • Absence of explicit looping • Conciseness – less bugs • Closer to mathematical notation • More pythonic. • Also possible for user functions

  26. NumPy: Broadcasting • Expansion of multidimensional arrays • Implicit element-by-element behavior

  27. NumPy: Broadcasting illustrated

  28. NumPy: Indexing

  29. arr[0,3:5]

  30. arr[4:,4:]

  31. arr[:,2]

  32. arr[2::2,::2]

  33. Other Options • Boolean area • Integer area

  34. NumPyData-types: Integers

  35. NumPyData-types: Unsigned

  36. NumPyData-types: Floating point

  37. NumPyData-types: Complex

  38. Optimize Python • NumPy: weave.blitz (fast NumPy expressions) • NumPy: weave.inline (inline C/C++) • f2py (interface Fortran) • Pyrex/Cython (python-like compiled language)

  39. Matplotlib • 2D plotting library • Some 3D support • Publication-quality figures • “Make easy things easy and hard things possible” • Configurable using matplotlibrc

  40. Matplotlib: Simple Plot importnumpyasnp frommatplotlibimportpyplotasplt t=np.linspace(0,2,200) s=np.sin(2*pi*t) plt.plot(t,s,linewidth=1.0) plt.xlabel('time (s)') plt.ylabel('voltage (mV)') plt.title('About as simple as it gets, folks') plt.grid(True) plt.show()

  41. Matplotlib: Subplots importnumpyasnp frommatplotlibimportpyplotasplt deff(t): s1=np.cos(2*pi*t) e1=np.exp(-t) returnnp.multiply(s1,e1) t1=np.arange(0.0,5.0,0.1) t2=np.arange(0.0,5.0,0.02) t3=np.arange(0.0,2.0,0.01) plt.subplot(211) l=plot(t1,f(t1),'bo',t2,f(t2),'k--', markerfacecolor='green') plt.grid(True) plt.title('A tale of 2 subplots') plt.ylabel('Damped oscillation') plt.subplot(212) plt.plot(t3,np.cos(2*pi*t3),'r.') plt.grid(True) plt.xlabel('time (s)') plt.ylabel('Undamped') plt.show()

  42. Matplotlib: Paths importnumpyasnp importmatplotlib.pathasmpath importmatplotlib.patchesasmpatches importmatplotlib.pyplotasplt Path=mpath.Path fig=plt.figure() ax=fig.add_subplot(111) pathdata=[ (Path.MOVETO,(1.58,-2.57)), (Path.CURVE4,(0.35,-1.1)), (Path.CURVE4,(-1.75,2.0)), (Path.CURVE4,(0.375,2.0)), (Path.LINETO,(0.85,1.15)), (Path.CURVE4,(2.2,3.2)), (Path.CURVE4,(3,0.05)), (Path.CURVE4,(2.0,-0.5)), (Path.CLOSEPOLY,(1.58,-2.57)), ] codes,verts=zip(*pathdata) path=mpath.Path(verts,codes) patch=mpatches.PathPatch(path,facecolor='red',edgecolor='yellow',alpha=0.5) ax.add_patch(patch) x,y=zip(*path.vertices) line,=ax.plot(x,y,'go-') ax.grid() ax.set_xlim(-3,4) ax.set_ylim(-3,4) ax.set_title('spline paths') plt.show()

  43. Matplotlib: mplot3d frommpl_toolkits.mplot3dimportAxes3D frommatplotlibimportcm frommatplotlib.tickerimport(LinearLocator,FixedLocator,FormatStrFormatter) importmatplotlib.pyplotasplt importnumpyasnp fig=plt.figure() ax=fig.gca(projection='3d') X=np.arange(-5,5,0.25) Y=np.arange(-5,5,0.25) X,Y=np.meshgrid(X,Y) R=np.sqrt(X**2+Y**2) Z=np.sin(R) surf=ax.plot_surface(X,Y,Z,rstride=1,cstride=1, cmap=cm.jet, linewidth=0,antialiased=False) ax.set_zlim3d(-1.01,1.01) ax.w_zaxis.set_major_locator(LinearLocator(10)) ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f')) fig.colorbar(surf,shrink=0.5,aspect=5) plt.show()

  44. Matplotlib: Ellipses importnumpyasnp frommatplotlibimportpyplotasplt frommatplotlib.patchesimportEllipse NUM=250 ells=[ Ellipse(xy=rand(2)*10,width=np.rand(), height=np.rand(),angle=np.rand()*360) foriinxrange(NUM)] fig=plt.figure() ax=fig.add_subplot(111,aspect='equal') foreinells: ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_alpha(rand()) e.set_facecolor(rand(3)) ax.set_xlim(0,10) ax.set_ylim(0,10) plt.show()

  45. MatplotlibExample: EEG viewer

  46. Matplotlib: Gallery

  47. SciPy • Statistics • Optimization • Numerical integration • Linear algebra • Fourier transforms • Signal processing • Image processing • ODE solvers • Special functions • And more.

  48. SciPy Example: Problem • Three phases • Glass sample – light grey • Bubbles – black • Sand grains – dark grey • Determine • Fraction of the sample covered by these • Typical size of sand grains or bubbles

  49. SciPy Example: Solution • Open image and examine it • Crop away panel at bottom • Examine histogram • Apply median filter • Determine thresholds • Display colored image • Use mathematical morphology to clean the different phases • Attribute labels to all bubbles and sand grains • Remove from the sand mask grains that are smaller than 10 pixels • Compute the mean size of bubbles.

  50. SunPy • Spatially aware maps • Read FITS files • RHESSI • SDO/AIA • EIT • TRACE • LASCO • standard color tables and hist equalization • basic image coalignment • VSO • HEK

More Related