SlideShare a Scribd company logo
1 of 48
Download to read offline
Python Programming for Scientists

         Alexander Eberspächer



          October 12th 2011
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction       Basics     Python Modules for Science      Faster Python and Glueing   Summary




Who uses...


               We all use computers to generate or process data
      Question to the audience: who uses...


           C/C++?                                          IDL?
           Fortran?                                        Perl?
           Ada?                                            Ruby?
           Java?                                           Python?
           Matlab/Octave?
Introduction     Basics      Python Modules for Science      Faster Python and Glueing   Summary




What is Python?



      Python is/has...


           a scripting language                           multi-paradigm
           general purpose                                open-source
           interpreted                                    available for all major
           easy to learn                                  platforms
           clean syntax                                   great community
Introduction     Basics     Python Modules for Science    Faster Python and Glueing   Summary




The best of all:
      Python comes...
      ... with batteries included!

                              Libraries available for...

       daily IT needs...                                 science!
           networks                  efficient array operations (NumPy)
           OS interaction            general numerical algorithms (SciPy)
           temporary files            2D visualization (matplotlib)
           zip files                  3D visualization (Mayavi)
           ...                       special problems (e.g. finite elements with
                                     FEniCS, quantum optics with QuTiP)
                                     symbolic math (SageMath, sympy)
                                     ...
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Scientific Hello, World!

 import sys
 from math import sin, pi

 def sincSquare(x):
     """Return sinc(x)^2.                            cmp. H.-P. Langtangen,
     """                                             "Python Scripting for
     if(x <> 0.0):                                   Computational Science"
         return (sin(pi*x)/(pi*x))**2                run with:
     else:
         return 1.0                                  python HelloWorld.py 0.0

 x = sys.argv[1]
 y = sincSquare(float(x))
 print("sinc(%s)^2 = %s"%(x, y))
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Control structures

      # if statements:
      if(divisor == 0):
          ...
      elif(divisor > 1E20):
          ...
      else:
          ...

      # loops:
      for i in range(10): # i = 0, 1, ..., 9
          print("i = %s"%i)

      # while loops:
      while(True):
          ...
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




Functions


      # functions:
      def f(x, a=1.0, b=2.0):
          """Return a/x and a/x^b.
          """

               return a/x, a/x**b

      # somewhere else:
      y1, y2 = f(x, 5.0)
      y3, y4 = f(2, b=3.0)
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Data types

      a = 2 # integer
      b = 2.0 # float
      c = "3.0" # string
      d = [1, 2, "three"] # list
      e = "1"
      print(a*b) # valid, upcasting
      print(a*c) # valid, but probably not desired: ’3.03.0’
      print(b*c) # invalid
      print(d[1]) # prints 2
      for item in d: # lists are "iterable"
          print(item)
      for character in c: # strings are iterable
          print(character) # prints 3n.n0
      f = e + c # + joins strings: f = ’13.0’
      g = d + [someObj, "foobar"] # + joins lists
Introduction    Basics   Python Modules for Science      Faster Python and Glueing   Summary




Files

      readFile = open("infile", mode="r")
      writeFile = open("outfile", mode="w")

      for line in readFile: # iterate over file’s lines
          xString, yString = line.split() # split the line
          x = float(xString); y = float(yString)
          print("x = %s, y = %s"%(x, y))
          writeFile.write("%s * %s = %sn"%(x, y, x*y))

      readFile.close(); writeFile.close()

   infile:                                      outfile:
   1.0         2.0                             1.0 * 2.0 = 2.0
   3.0         4.0                             3.0 * 4.0 = 12.0
Introduction   Basics    Python Modules for Science   Faster Python and Glueing   Summary




Reusing code: modules
      Place code to be reused in Module.py:
      """A Python module for illustration.
      """

      def printData():
          print(data)

      data = 2
      In somewhereElse.py, do something like:
      import Module

      Module.data = 3
      Module.printData()
Introduction   Basics      Python Modules for Science    Faster Python and Glueing   Summary




Some Python magic

      x, y = y, x       # swapping

      print(1 > 2 > 3)       # prints False

      # filtering (there is also reduce(), map())
      numbers = range(50)
      evenNumbers = filter(lambda x: x % 2 == 0, numbers)
      print("All even numbers in [0; 50): %s"%evenNumbers)

      # list comprehensions:
      squares = [x**2 for x in numbers]

      a += 2    # a = a + 2

      print("string" in "Long string")                  # prints True
Introduction       Basics       Python Modules for Science   Faster Python and Glueing   Summary




Pitfalls
      ☇ Common pitfalls:
               slicing: last index is exclusive, not inclusive as in e.g. Fortran

               x = [1, 2, 3, 4]
               print(x[0:2]) # prints [1, 2], not [1, 2, 3]

               What looks like performing an assignment is actually setting a
               reference:

               a = []
               b = a
               a.append(2)
               print(a) # prints [2]
               print(b) # prints [2], not []!
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction     Basics   Python Modules for Science   Faster Python and Glueing   Summary




The IPython shell
      IPython
      An interactive shell - may replace MatLab [tm] for interactive work

           Syntax
           highlighting
           Tab
           completion
           Inline docu-
           mentation
           Easy
           profiling,
           timing...
           IPython ≥
           0.11: inline
           plots...
Introduction       Basics      Python Modules for Science   Faster Python and Glueing   Summary




NumPy: Python meets an array data type

      NumPy
      Fast and convenient array operations


               Lists: + does join, not add!
               NumPy array: basic vector/matrix data type
               Convenience functions (e.g. linspace(), zeros(),
               loadtxt()...)
               Array slicing
               element-wise operations
               Code using NumPy reads and writes very similar to modern
               Fortran (slicing, vector valued indices...)
Introduction   Basics       Python Modules for Science   Faster Python and Glueing   Summary




NumPy by examples


      import numpy as np

      a = np.array([1.0, 2.0, 3.0, 4.0])
      b = np.array([4.0, 3.0, 2.0, 1.0])
      for item in a: # arrays are iterable
          print(item)
      c = a + b # c = [5, 5, 5, 5]
      print(a[0:3:2]) # 1.0, 3.0; last element not included!
      a[0:3] = b[0:-1]

      print(a*b)        # prints [4, 6, 6, 4], not the scalar product!
Introduction    Basics      Python Modules for Science      Faster Python and Glueing   Summary




SciPy

      SciPy
      Numerical algorithms using NumPy arrays

      Wrappers around well-established libraries
      Submodules:
          linalg: Linear algebra
          (lapack)                                       integration: Integration
                                                         (quadpack, odepack)
           sparse: sparse matrices
                                                         special: special functions
           fft: FFT (fftpack)                             (amos...)
           optimize: Optimization,                       signal: Signal processing
           Zeros (minpack)
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




SciPy: an example

      import numpy as np
      from scipy.optimize import curve_fit
      from matplotlib.pyplot import plot, show, legend

      x, yExp = np.loadtxt("func.dat", unpack=True)
      plot(x, yExp, ls="--", c="blue", lw="1.5", label="Exp.")

      def fitFunc(x, a, b, c):
          return a*np.exp(-b*x) + c

      pOpt, pCov = curve_fit(fitFunc, x, yExp)
      yFit = fitFunc(x, a=pOpt[0], b=pOpt[1], c=pOpt[2])
      plot(x, yFit, label="Fit: $a = %s; b = %s; c= %s$"
           %(pOpt[0], pOpt[1], pOpt[2]), ls="-", lw="1.5", c="r")
      legend(); show()
Introduction   Basics     Python Modules for Science   Faster Python and Glueing   Summary




SciPy: the example’s output




      Already used here: Matplotlib
Introduction   Basics     Python Modules for Science   Faster Python and Glueing   Summary




Matplotlib
      (mostly) 2D plots




      Pylab: MatLab alternative for interactive work
Introduction   Basics   Python Modules for Science      Faster Python and Glueing   Summary




Some Pylab: the logistic map xn+1 = rxn (1 − xn )

      from matplotlib.pylab import *                 # some of NumPy, SciPy, MPL

      rVals = 2000; startVal = 0.5
      throwAway = 300; samples = 800
      vals = zeros(samples-throwAway)

      for r in linspace(2.5, 4.0, rVals): # iterate r
          x = startVal
          for s in range(samples):
              x = r*x*(1-x) # logistic map
              if(s >= throwAway): vals[s-throwAway] = x
          scatter(r*ones(samples-throwAway), vals, c="k", 
                  marker="o", s=0.3, lw=0) # plot

      xlabel("$r$"); ylabel("$x$"); title("Log. map"); show();
Introduction   Basics     Python Modules for Science   Faster Python and Glueing   Summary




Some Pylab: the logistic map xn+1 = rxn (1 − xn )
      The last script produces this image:
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction     Basics      Python Modules for Science     Faster Python and Glueing   Summary




Using Python as glue
      Python can wrap different different other programming languages

      Cython
      compiled, typed Python - interface C/C++ code


      f2py
      Fortran wrapper, included in NumPy

      Why do that?
           Python can be slow                             Wrap external C/Fortran...
           Python loops are slow                          libraries
           calling Python functions is                    Happily/unfortunately (?)
           slow                                           there is legacy code
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




Problem: sinc(x)2

      import numpy as np
      from math import sin, pi

      def sincSquare(x):
          """Return the sinc(x) = (sin(x)/x)**2 of the array
          argument x.
          """
          retVal = np.zeros_like(x)
          for i in range(len(x)):
              retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2

               return retVal


      106 array elements: 1 loops, best of 3: 4.91 s per loop
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




Problem: sinc(x)2

      import numpy as np
      from math import sin, pi

      def sincSquare(x):
          """Return the sinc(x) = (sin(x)/x)**2 of the array
          argument x.
          """
          retVal = np.zeros_like(x)
          for i in range(len(x)):
              retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2

               return retVal


      106 array elements: 1 loops, best of 3: 4.91 s per loop
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




First attempt: use NumPy array operations

      import numpy as np

      def sincSquareNumPy1(x):

               return (np.sin(np.pi*x[:])/(np.pi*x[:]))**2

      def sincSquareNumPy2(x):

               return np.sinc(x[:])**2


      106 array elements: first function: 10 loops, best of 3: 73 ms
      per loop, second function: 10 loops, best of 3: 92.9 ms
      per loop
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




First attempt: use NumPy array operations

      import numpy as np

      def sincSquareNumPy1(x):

               return (np.sin(np.pi*x[:])/(np.pi*x[:]))**2

      def sincSquareNumPy2(x):

               return np.sinc(x[:])**2


      106 array elements: first function: 10 loops, best of 3: 73 ms
      per loop, second function: 10 loops, best of 3: 92.9 ms
      per loop
Introduction       Basics       Python Modules for Science   Faster Python and Glueing   Summary




How Cython works


      Cython
      compiled, possibly typed Python:
                       Cython             C compiler
      .pyx file             ⇒ .c file           ⇒       .so/.dll file


               various levels of typing possible
               C output and Cython’s opinion on code speed can easily be
               inspected (optional .html output)
               interfacing C libraries is easy
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - Cython, Version 1

      cdef extern from "math.h":
          double sin(double)
          double pow(double, int)

      def sincSquareCython1(x):

               pi = 3.1415926535897932384626433
               retVal = np.zeros_like(x)

               for i in range(len(x)):
                   retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2

               return retVal

      106 array elements: 1 loops, best of 3: 4.39 s per loop
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - Cython, Version 1

      cdef extern from "math.h":
          double sin(double)
          double pow(double, int)

      def sincSquareCython1(x):

               pi = 3.1415926535897932384626433
               retVal = np.zeros_like(x)

               for i in range(len(x)):
                   retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2

               return retVal

      106 array elements: 1 loops, best of 3: 4.39 s per loop
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - Cython, Version 2

      cimport numpy as np # also C-import types

      cpdef np.ndarray[double] sincSquareCython2
          (np.ndarray[double] x):

               cdef int i
               cdef double pi = 3.1415926535897932384626433
               cdef np.ndarray[double] retVal = np.zeros_like(x)

               for i in range(len(x)):
                   retVal[i] = pow(sin(pi*x[i]) / (pi*x[i]), 2)


      106 array elements: 10 loops, best of 3: 49.1 ms per loop
      That’s a speedup by a factor ≈ 100!
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - Cython, Version 2

      cimport numpy as np # also C-import types

      cpdef np.ndarray[double] sincSquareCython2
          (np.ndarray[double] x):

               cdef int i
               cdef double pi = 3.1415926535897932384626433
               cdef np.ndarray[double] retVal = np.zeros_like(x)

               for i in range(len(x)):
                   retVal[i] = pow(sin(pi*x[i]) / (pi*x[i]), 2)


      106 array elements: 10 loops, best of 3: 49.1 ms per loop
      That’s a speedup by a factor ≈ 100!
Introduction       Basics          Python Modules for Science   Faster Python and Glueing   Summary




How f2py works



      f2py
      wrap Fortran code in Python:
                            f2py
      .f/.f90 file           ⇒ .so/.dll file

               f2py is included in NumPy
               exposes NumPy arrays to Fortran code
               once ’Fortran space’ is entered, you run at full Fortran speed
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - f2py, Version 1

      subroutine sincsquaref2py1(x, n, outVal)
          implicit none

               double precision, dimension(n), intent(in) :: x
               integer, intent(in) :: n
               double precision, dimension(n), intent(out) :: outVal
               double precision, parameter :: pi = 4.0d0 * atan(1.0d0)

               outVal(:) = (sin(pi*x(:)) / (pi*x(:)))**2

      end subroutine sincsquaref2py1


      106 array elements: 10 loops, best of 3: 47.4 ms per loop
      Again, a speedup by a factor of ≈ 100!
Introduction      Basics   Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - f2py, Version 1

      subroutine sincsquaref2py1(x, n, outVal)
          implicit none

               double precision, dimension(n), intent(in) :: x
               integer, intent(in) :: n
               double precision, dimension(n), intent(out) :: outVal
               double precision, parameter :: pi = 4.0d0 * atan(1.0d0)

               outVal(:) = (sin(pi*x(:)) / (pi*x(:)))**2

      end subroutine sincsquaref2py1


      106 array elements: 10 loops, best of 3: 47.4 ms per loop
      Again, a speedup by a factor of ≈ 100!
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Cheating: sinc(x)2 - f2py, Version 2 - OpenMP

      subroutine sincsquaref2py2(x, n, outVal)
          implicit none
          double precision, dimension(n), intent(in) :: x
          integer, intent(in) :: n
          double precision, dimension(n), intent(out) :: outVal
          integer :: i
          double precision, parameter :: pi = 4.0d0 * atan(1.0d0)
          !$OMP PARALLEL DO SHARED(x, outVal)
          do i = 1, n
              outVal(i) = (sin(pi*x(i)) / (pi*x(i)))**2
          end do
          !$OMP END PARALLEL DO
      end subroutine sincsquaref2py2

      106 array elements, 2 Threads: 10 loops, best of 3: 33.5 ms
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Cheating: sinc(x)2 - f2py, Version 2 - OpenMP

      subroutine sincsquaref2py2(x, n, outVal)
          implicit none
          double precision, dimension(n), intent(in) :: x
          integer, intent(in) :: n
          double precision, dimension(n), intent(out) :: outVal
          integer :: i
          double precision, parameter :: pi = 4.0d0 * atan(1.0d0)
          !$OMP PARALLEL DO SHARED(x, outVal)
          do i = 1, n
              outVal(i) = (sin(pi*x(i)) / (pi*x(i)))**2
          end do
          !$OMP END PARALLEL DO
      end subroutine sincsquaref2py2

      106 array elements, 2 Threads: 10 loops, best of 3: 33.5 ms
Introduction   Basics     Python Modules for Science   Faster Python and Glueing   Summary




sinc(x)2 - Overview
      Benchmark for an Intel i7:
Introduction       Basics      Python Modules for Science   Faster Python and Glueing   Summary




Techniques for faster Scripts

      After you have written a prototype in Python with NumPy and
      SciPy, check if your code is already fast enough. If not,
               profile your script (IPython’s run -p or cProfile module...)
               to find bottlenecks
               if a large numbers of function calls is the bottleneck, typing and
               using Cython’s cdef/cpdef for C calling conventions speeds
               your code up at the cost of flexibility
               loops greatly benefit from typing, too
               consider moving heavy computations to Fortran/C completely -
               f2py and Cython will help you wrapping
Introduction       Basics      Python Modules for Science   Faster Python and Glueing   Summary




Slightly OffTopic: mpi4py

      mpi4py
      Interface MPI in Python


               speed-up pure Python by parallelization using MPI (OpenMPI,
               mpich...)
               mpi4py also works with f2py and Cython (?)
         → run the steering Python script with mpirun..., take care of
           the communicator there and use it in Fortran, too
      Alternatives:
               IPython’s parallel computing facilities
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Slightly OffTopic: mpi4py


      from mpi4py import MPI

      MPIroot = 0 # define the root process
      MPIcomm = MPI.COMM_WORLD # MPI communicator

      MPIrank, MPIsize = MPIcomm.Get_rank(), MPIcomm.Get_size()

      ...

      MPIcomm.Reduce(tempVals, retVal, op=MPI.SUM, root=MPIroot)
Introduction   Basics   Python Modules for Science   Faster Python and Glueing   Summary




Outline


       1 Introduction


       2 Basics


       3 Python Modules for Science


       4 Faster Python and Glueing


       5 Summary
Introduction       Basics       Python Modules for Science   Faster Python and Glueing   Summary




Python in teaching

      Python/Pylab should be used in teaching because
               it is easy...
               and yet powerful;
               it may be used specialized to numerical computing...
               and also serve students as a general purpose language;
               it is safe;
               and best of all, it is free!

      Take home message 1
      Python is ideal for teaching
Introduction      Basics      Python Modules for Science   Faster Python and Glueing   Summary




Summary

      We have...
               introduced basic Python scripting
               shown some basic modules for scientific computing
               demonstrated how to wrap other languages
               learned how to speed Python up

      Take home message 2
      Python is a very valuable tool for Physicists

      Slides, LTEX and Python Sources available at
              A
      http://github.com/aeberspaecher

More Related Content

What's hot

Seven waystouseturtle pycon2009
Seven waystouseturtle pycon2009Seven waystouseturtle pycon2009
Seven waystouseturtle pycon2009A Jorge Garcia
 
Attention mechanisms with tensorflow
Attention mechanisms with tensorflowAttention mechanisms with tensorflow
Attention mechanisms with tensorflowKeon Kim
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Itzik Kotler
 
Python as number crunching code glue
Python as number crunching code gluePython as number crunching code glue
Python as number crunching code glueJiahao Chen
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorchJun Young Park
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Alessio Tonioni
 
Deep Learning in Your Browser
Deep Learning in Your BrowserDeep Learning in Your Browser
Deep Learning in Your BrowserOswald Campesato
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningOswald Campesato
 
Natural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usageNatural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usagehyunyoung Lee
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasOswald Campesato
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Python interview questions for experience
Python interview questions for experiencePython interview questions for experience
Python interview questions for experienceMYTHILIKRISHNAN4
 
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...Simplilearn
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)Oswald Campesato
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2Oswald Campesato
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2Oswald Campesato
 

What's hot (20)

Seven waystouseturtle pycon2009
Seven waystouseturtle pycon2009Seven waystouseturtle pycon2009
Seven waystouseturtle pycon2009
 
Attention mechanisms with tensorflow
Attention mechanisms with tensorflowAttention mechanisms with tensorflow
Attention mechanisms with tensorflow
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)
 
Python as number crunching code glue
Python as number crunching code gluePython as number crunching code glue
Python as number crunching code glue
 
Introduction to PyTorch
Introduction to PyTorchIntroduction to PyTorch
Introduction to PyTorch
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
 
Deep Learning in Your Browser
Deep Learning in Your BrowserDeep Learning in Your Browser
Deep Learning in Your Browser
 
D3, TypeScript, and Deep Learning
D3, TypeScript, and Deep LearningD3, TypeScript, and Deep Learning
D3, TypeScript, and Deep Learning
 
Natural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usageNatural language processing open seminar For Tensorflow usage
Natural language processing open seminar For Tensorflow usage
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and Keras
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python interview questions for experience
Python interview questions for experiencePython interview questions for experience
Python interview questions for experience
 
H2 o berkeleydltf
H2 o berkeleydltfH2 o berkeleydltf
H2 o berkeleydltf
 
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
What is TensorFlow? | Introduction to TensorFlow | TensorFlow Tutorial For Be...
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
Learn How to Master Solr1 4
Learn How to Master Solr1 4Learn How to Master Solr1 4
Learn How to Master Solr1 4
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 

Viewers also liked

Ovarian hyperstimulation syndrome
Ovarian hyperstimulation syndromeOvarian hyperstimulation syndrome
Ovarian hyperstimulation syndromeHesham Gaber
 
The study of language
The study of language The study of language
The study of language Cristian Zarza
 
Ovarian Hyperstimulation Syndrome
Ovarian Hyperstimulation SyndromeOvarian Hyperstimulation Syndrome
Ovarian Hyperstimulation Syndromeguest9dc181
 
The study of language
The study of languageThe study of language
The study of languageAhmad Suhaimi
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for ScientistsAndreas Dewes
 
ovarian hyperstimulation syndrome
ovarian hyperstimulation syndromeovarian hyperstimulation syndrome
ovarian hyperstimulation syndromeHemin Jamal
 

Viewers also liked (9)

Ovarian hyperstimulation syndrome
Ovarian hyperstimulation syndromeOvarian hyperstimulation syndrome
Ovarian hyperstimulation syndrome
 
The study of language
The study of language The study of language
The study of language
 
Ovarian Hyperstimulation Syndrome
Ovarian Hyperstimulation SyndromeOvarian Hyperstimulation Syndrome
Ovarian Hyperstimulation Syndrome
 
The study of language
The study of languageThe study of language
The study of language
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for Scientists
 
"The study of language" - Chapter 18
"The study of language" - Chapter 18"The study of language" - Chapter 18
"The study of language" - Chapter 18
 
ovarian hyperstimulation syndrome
ovarian hyperstimulation syndromeovarian hyperstimulation syndrome
ovarian hyperstimulation syndrome
 
The study of language. Chapter 19pptx
The study of language. Chapter 19pptxThe study of language. Chapter 19pptx
The study of language. Chapter 19pptx
 
"The study of language" - Chapter 20
"The study of language" - Chapter 20"The study of language" - Chapter 20
"The study of language" - Chapter 20
 

Similar to Python For Scientists

Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
The Joy of SciPy, Part I
The Joy of SciPy, Part IThe Joy of SciPy, Part I
The Joy of SciPy, Part IDinu Gherman
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyTravis Oliphant
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and MatplotlibFrançois Bianco
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance PythonIan Ozsvald
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experiencedzynofustechnology
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfExaminationSectionMR
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobikrmboya
 
Python workshop #1 at UGA
Python workshop #1 at UGAPython workshop #1 at UGA
Python workshop #1 at UGAEric Talevich
 
PPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.TechPPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.Techssuser2678ab
 
Python 3000
Python 3000Python 3000
Python 3000Bob Chao
 
AI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python DevsAI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python DevsAmr Shawqy
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2sadhana312471
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & styleKevlin Henney
 

Similar to Python For Scientists (20)

Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
The Joy of SciPy, Part I
The Joy of SciPy, Part IThe Joy of SciPy, Part I
The Joy of SciPy, Part I
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
Biopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and OutlookBiopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and Outlook
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and Matplotlib
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobi
 
Using Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUGUsing Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUG
 
Python workshop #1 at UGA
Python workshop #1 at UGAPython workshop #1 at UGA
Python workshop #1 at UGA
 
PPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.TechPPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.Tech
 
Python 3000
Python 3000Python 3000
Python 3000
 
AI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python DevsAI Machine Learning Complete Course: for PHP & Python Devs
AI Machine Learning Complete Course: for PHP & Python Devs
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
 
Advance python
Advance pythonAdvance python
Advance python
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 

Recently uploaded

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentMahmoud Rabie
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 

Recently uploaded (20)

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Digital Tools & AI in Career Development
Digital Tools & AI in Career DevelopmentDigital Tools & AI in Career Development
Digital Tools & AI in Career Development
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 

Python For Scientists

  • 1. Python Programming for Scientists Alexander Eberspächer October 12th 2011
  • 2. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 3. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 4. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Who uses... We all use computers to generate or process data Question to the audience: who uses... C/C++? IDL? Fortran? Perl? Ada? Ruby? Java? Python? Matlab/Octave?
  • 5. Introduction Basics Python Modules for Science Faster Python and Glueing Summary What is Python? Python is/has... a scripting language multi-paradigm general purpose open-source interpreted available for all major easy to learn platforms clean syntax great community
  • 6. Introduction Basics Python Modules for Science Faster Python and Glueing Summary The best of all: Python comes... ... with batteries included! Libraries available for... daily IT needs... science! networks efficient array operations (NumPy) OS interaction general numerical algorithms (SciPy) temporary files 2D visualization (matplotlib) zip files 3D visualization (Mayavi) ... special problems (e.g. finite elements with FEniCS, quantum optics with QuTiP) symbolic math (SageMath, sympy) ...
  • 7. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 8. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Scientific Hello, World! import sys from math import sin, pi def sincSquare(x): """Return sinc(x)^2. cmp. H.-P. Langtangen, """ "Python Scripting for if(x <> 0.0): Computational Science" return (sin(pi*x)/(pi*x))**2 run with: else: return 1.0 python HelloWorld.py 0.0 x = sys.argv[1] y = sincSquare(float(x)) print("sinc(%s)^2 = %s"%(x, y))
  • 9. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Control structures # if statements: if(divisor == 0): ... elif(divisor > 1E20): ... else: ... # loops: for i in range(10): # i = 0, 1, ..., 9 print("i = %s"%i) # while loops: while(True): ...
  • 10. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Functions # functions: def f(x, a=1.0, b=2.0): """Return a/x and a/x^b. """ return a/x, a/x**b # somewhere else: y1, y2 = f(x, 5.0) y3, y4 = f(2, b=3.0)
  • 11. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Data types a = 2 # integer b = 2.0 # float c = "3.0" # string d = [1, 2, "three"] # list e = "1" print(a*b) # valid, upcasting print(a*c) # valid, but probably not desired: ’3.03.0’ print(b*c) # invalid print(d[1]) # prints 2 for item in d: # lists are "iterable" print(item) for character in c: # strings are iterable print(character) # prints 3n.n0 f = e + c # + joins strings: f = ’13.0’ g = d + [someObj, "foobar"] # + joins lists
  • 12. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Files readFile = open("infile", mode="r") writeFile = open("outfile", mode="w") for line in readFile: # iterate over file’s lines xString, yString = line.split() # split the line x = float(xString); y = float(yString) print("x = %s, y = %s"%(x, y)) writeFile.write("%s * %s = %sn"%(x, y, x*y)) readFile.close(); writeFile.close() infile: outfile: 1.0 2.0 1.0 * 2.0 = 2.0 3.0 4.0 3.0 * 4.0 = 12.0
  • 13. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Reusing code: modules Place code to be reused in Module.py: """A Python module for illustration. """ def printData(): print(data) data = 2 In somewhereElse.py, do something like: import Module Module.data = 3 Module.printData()
  • 14. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Some Python magic x, y = y, x # swapping print(1 > 2 > 3) # prints False # filtering (there is also reduce(), map()) numbers = range(50) evenNumbers = filter(lambda x: x % 2 == 0, numbers) print("All even numbers in [0; 50): %s"%evenNumbers) # list comprehensions: squares = [x**2 for x in numbers] a += 2 # a = a + 2 print("string" in "Long string") # prints True
  • 15. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Pitfalls ☇ Common pitfalls: slicing: last index is exclusive, not inclusive as in e.g. Fortran x = [1, 2, 3, 4] print(x[0:2]) # prints [1, 2], not [1, 2, 3] What looks like performing an assignment is actually setting a reference: a = [] b = a a.append(2) print(a) # prints [2] print(b) # prints [2], not []!
  • 16. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 17. Introduction Basics Python Modules for Science Faster Python and Glueing Summary The IPython shell IPython An interactive shell - may replace MatLab [tm] for interactive work Syntax highlighting Tab completion Inline docu- mentation Easy profiling, timing... IPython ≥ 0.11: inline plots...
  • 18. Introduction Basics Python Modules for Science Faster Python and Glueing Summary NumPy: Python meets an array data type NumPy Fast and convenient array operations Lists: + does join, not add! NumPy array: basic vector/matrix data type Convenience functions (e.g. linspace(), zeros(), loadtxt()...) Array slicing element-wise operations Code using NumPy reads and writes very similar to modern Fortran (slicing, vector valued indices...)
  • 19. Introduction Basics Python Modules for Science Faster Python and Glueing Summary NumPy by examples import numpy as np a = np.array([1.0, 2.0, 3.0, 4.0]) b = np.array([4.0, 3.0, 2.0, 1.0]) for item in a: # arrays are iterable print(item) c = a + b # c = [5, 5, 5, 5] print(a[0:3:2]) # 1.0, 3.0; last element not included! a[0:3] = b[0:-1] print(a*b) # prints [4, 6, 6, 4], not the scalar product!
  • 20. Introduction Basics Python Modules for Science Faster Python and Glueing Summary SciPy SciPy Numerical algorithms using NumPy arrays Wrappers around well-established libraries Submodules: linalg: Linear algebra (lapack) integration: Integration (quadpack, odepack) sparse: sparse matrices special: special functions fft: FFT (fftpack) (amos...) optimize: Optimization, signal: Signal processing Zeros (minpack)
  • 21. Introduction Basics Python Modules for Science Faster Python and Glueing Summary SciPy: an example import numpy as np from scipy.optimize import curve_fit from matplotlib.pyplot import plot, show, legend x, yExp = np.loadtxt("func.dat", unpack=True) plot(x, yExp, ls="--", c="blue", lw="1.5", label="Exp.") def fitFunc(x, a, b, c): return a*np.exp(-b*x) + c pOpt, pCov = curve_fit(fitFunc, x, yExp) yFit = fitFunc(x, a=pOpt[0], b=pOpt[1], c=pOpt[2]) plot(x, yFit, label="Fit: $a = %s; b = %s; c= %s$" %(pOpt[0], pOpt[1], pOpt[2]), ls="-", lw="1.5", c="r") legend(); show()
  • 22. Introduction Basics Python Modules for Science Faster Python and Glueing Summary SciPy: the example’s output Already used here: Matplotlib
  • 23. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Matplotlib (mostly) 2D plots Pylab: MatLab alternative for interactive work
  • 24. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Some Pylab: the logistic map xn+1 = rxn (1 − xn ) from matplotlib.pylab import * # some of NumPy, SciPy, MPL rVals = 2000; startVal = 0.5 throwAway = 300; samples = 800 vals = zeros(samples-throwAway) for r in linspace(2.5, 4.0, rVals): # iterate r x = startVal for s in range(samples): x = r*x*(1-x) # logistic map if(s >= throwAway): vals[s-throwAway] = x scatter(r*ones(samples-throwAway), vals, c="k", marker="o", s=0.3, lw=0) # plot xlabel("$r$"); ylabel("$x$"); title("Log. map"); show();
  • 25. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Some Pylab: the logistic map xn+1 = rxn (1 − xn ) The last script produces this image:
  • 26. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 27. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Using Python as glue Python can wrap different different other programming languages Cython compiled, typed Python - interface C/C++ code f2py Fortran wrapper, included in NumPy Why do that? Python can be slow Wrap external C/Fortran... Python loops are slow libraries calling Python functions is Happily/unfortunately (?) slow there is legacy code
  • 28. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Problem: sinc(x)2 import numpy as np from math import sin, pi def sincSquare(x): """Return the sinc(x) = (sin(x)/x)**2 of the array argument x. """ retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2 return retVal 106 array elements: 1 loops, best of 3: 4.91 s per loop
  • 29. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Problem: sinc(x)2 import numpy as np from math import sin, pi def sincSquare(x): """Return the sinc(x) = (sin(x)/x)**2 of the array argument x. """ retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2 return retVal 106 array elements: 1 loops, best of 3: 4.91 s per loop
  • 30. Introduction Basics Python Modules for Science Faster Python and Glueing Summary First attempt: use NumPy array operations import numpy as np def sincSquareNumPy1(x): return (np.sin(np.pi*x[:])/(np.pi*x[:]))**2 def sincSquareNumPy2(x): return np.sinc(x[:])**2 106 array elements: first function: 10 loops, best of 3: 73 ms per loop, second function: 10 loops, best of 3: 92.9 ms per loop
  • 31. Introduction Basics Python Modules for Science Faster Python and Glueing Summary First attempt: use NumPy array operations import numpy as np def sincSquareNumPy1(x): return (np.sin(np.pi*x[:])/(np.pi*x[:]))**2 def sincSquareNumPy2(x): return np.sinc(x[:])**2 106 array elements: first function: 10 loops, best of 3: 73 ms per loop, second function: 10 loops, best of 3: 92.9 ms per loop
  • 32. Introduction Basics Python Modules for Science Faster Python and Glueing Summary How Cython works Cython compiled, possibly typed Python: Cython C compiler .pyx file ⇒ .c file ⇒ .so/.dll file various levels of typing possible C output and Cython’s opinion on code speed can easily be inspected (optional .html output) interfacing C libraries is easy
  • 33. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - Cython, Version 1 cdef extern from "math.h": double sin(double) double pow(double, int) def sincSquareCython1(x): pi = 3.1415926535897932384626433 retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2 return retVal 106 array elements: 1 loops, best of 3: 4.39 s per loop
  • 34. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - Cython, Version 1 cdef extern from "math.h": double sin(double) double pow(double, int) def sincSquareCython1(x): pi = 3.1415926535897932384626433 retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = (sin(pi*x[i]) / (pi*x[i]))**2 return retVal 106 array elements: 1 loops, best of 3: 4.39 s per loop
  • 35. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - Cython, Version 2 cimport numpy as np # also C-import types cpdef np.ndarray[double] sincSquareCython2 (np.ndarray[double] x): cdef int i cdef double pi = 3.1415926535897932384626433 cdef np.ndarray[double] retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = pow(sin(pi*x[i]) / (pi*x[i]), 2) 106 array elements: 10 loops, best of 3: 49.1 ms per loop That’s a speedup by a factor ≈ 100!
  • 36. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - Cython, Version 2 cimport numpy as np # also C-import types cpdef np.ndarray[double] sincSquareCython2 (np.ndarray[double] x): cdef int i cdef double pi = 3.1415926535897932384626433 cdef np.ndarray[double] retVal = np.zeros_like(x) for i in range(len(x)): retVal[i] = pow(sin(pi*x[i]) / (pi*x[i]), 2) 106 array elements: 10 loops, best of 3: 49.1 ms per loop That’s a speedup by a factor ≈ 100!
  • 37. Introduction Basics Python Modules for Science Faster Python and Glueing Summary How f2py works f2py wrap Fortran code in Python: f2py .f/.f90 file ⇒ .so/.dll file f2py is included in NumPy exposes NumPy arrays to Fortran code once ’Fortran space’ is entered, you run at full Fortran speed
  • 38. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - f2py, Version 1 subroutine sincsquaref2py1(x, n, outVal) implicit none double precision, dimension(n), intent(in) :: x integer, intent(in) :: n double precision, dimension(n), intent(out) :: outVal double precision, parameter :: pi = 4.0d0 * atan(1.0d0) outVal(:) = (sin(pi*x(:)) / (pi*x(:)))**2 end subroutine sincsquaref2py1 106 array elements: 10 loops, best of 3: 47.4 ms per loop Again, a speedup by a factor of ≈ 100!
  • 39. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - f2py, Version 1 subroutine sincsquaref2py1(x, n, outVal) implicit none double precision, dimension(n), intent(in) :: x integer, intent(in) :: n double precision, dimension(n), intent(out) :: outVal double precision, parameter :: pi = 4.0d0 * atan(1.0d0) outVal(:) = (sin(pi*x(:)) / (pi*x(:)))**2 end subroutine sincsquaref2py1 106 array elements: 10 loops, best of 3: 47.4 ms per loop Again, a speedup by a factor of ≈ 100!
  • 40. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Cheating: sinc(x)2 - f2py, Version 2 - OpenMP subroutine sincsquaref2py2(x, n, outVal) implicit none double precision, dimension(n), intent(in) :: x integer, intent(in) :: n double precision, dimension(n), intent(out) :: outVal integer :: i double precision, parameter :: pi = 4.0d0 * atan(1.0d0) !$OMP PARALLEL DO SHARED(x, outVal) do i = 1, n outVal(i) = (sin(pi*x(i)) / (pi*x(i)))**2 end do !$OMP END PARALLEL DO end subroutine sincsquaref2py2 106 array elements, 2 Threads: 10 loops, best of 3: 33.5 ms
  • 41. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Cheating: sinc(x)2 - f2py, Version 2 - OpenMP subroutine sincsquaref2py2(x, n, outVal) implicit none double precision, dimension(n), intent(in) :: x integer, intent(in) :: n double precision, dimension(n), intent(out) :: outVal integer :: i double precision, parameter :: pi = 4.0d0 * atan(1.0d0) !$OMP PARALLEL DO SHARED(x, outVal) do i = 1, n outVal(i) = (sin(pi*x(i)) / (pi*x(i)))**2 end do !$OMP END PARALLEL DO end subroutine sincsquaref2py2 106 array elements, 2 Threads: 10 loops, best of 3: 33.5 ms
  • 42. Introduction Basics Python Modules for Science Faster Python and Glueing Summary sinc(x)2 - Overview Benchmark for an Intel i7:
  • 43. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Techniques for faster Scripts After you have written a prototype in Python with NumPy and SciPy, check if your code is already fast enough. If not, profile your script (IPython’s run -p or cProfile module...) to find bottlenecks if a large numbers of function calls is the bottleneck, typing and using Cython’s cdef/cpdef for C calling conventions speeds your code up at the cost of flexibility loops greatly benefit from typing, too consider moving heavy computations to Fortran/C completely - f2py and Cython will help you wrapping
  • 44. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Slightly OffTopic: mpi4py mpi4py Interface MPI in Python speed-up pure Python by parallelization using MPI (OpenMPI, mpich...) mpi4py also works with f2py and Cython (?) → run the steering Python script with mpirun..., take care of the communicator there and use it in Fortran, too Alternatives: IPython’s parallel computing facilities
  • 45. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Slightly OffTopic: mpi4py from mpi4py import MPI MPIroot = 0 # define the root process MPIcomm = MPI.COMM_WORLD # MPI communicator MPIrank, MPIsize = MPIcomm.Get_rank(), MPIcomm.Get_size() ... MPIcomm.Reduce(tempVals, retVal, op=MPI.SUM, root=MPIroot)
  • 46. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Outline 1 Introduction 2 Basics 3 Python Modules for Science 4 Faster Python and Glueing 5 Summary
  • 47. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Python in teaching Python/Pylab should be used in teaching because it is easy... and yet powerful; it may be used specialized to numerical computing... and also serve students as a general purpose language; it is safe; and best of all, it is free! Take home message 1 Python is ideal for teaching
  • 48. Introduction Basics Python Modules for Science Faster Python and Glueing Summary Summary We have... introduced basic Python scripting shown some basic modules for scientific computing demonstrated how to wrap other languages learned how to speed Python up Take home message 2 Python is a very valuable tool for Physicists Slides, LTEX and Python Sources available at A http://github.com/aeberspaecher