23
PHY 688: Numerical Methods for (Astro)Physics Why Python? Very high-level language Provides many complex data-structures (lists, dictionaries, ...) Many modules to perform complex tasks (parse structured inputs files, send e-mail, interact with the operating system, make plots, make GUIs, do scientific computations, ...) Complex tasks in short amounts of code Easy to prototype new tools Cross-platform and Free Many in-class examples will be done in python We'll look at some basics from: http://docs.python.org/2/tutorial/

Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Why Python?● Very high-level language

– Provides many complex data-structures (lists, dictionaries, ...)

● Many modules to perform complex tasks (parse structured inputs files, send e-mail, interact with the operating system, make plots, make GUIs, do scientific computations, ...)

● Complex tasks in short amounts of code

● Easy to prototype new tools

● Cross-platform and Free

● Many in-class examples will be done in python

● We'll look at some basics from: http://docs.python.org/2/tutorial/

Page 2: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Python

(xkcd)

Page 3: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Python in Astro● Python is seeing rapidly increasing adoption in Astrophysics

– Open-source alternative to IDL

● Python for IDL users: https://www.cfa.harvard.edu/~jbattat/computer/python/science/idl-numpy.html

● Astropython: http://www.astropython.org/

– Lots of tutorials, links to resources, forum, ...

● Astropy mailing list: http://mail.scipy.org/mailman/listinfo/astropy

Page 4: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Overview● Dynamically-typed

● Automatic memory management (garbage collection)

● Whitespace matters

Page 5: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Data Types● Beyond the standard integer, floating point, string types that

most languages provide, python has some other built-in data-structures

● Lists are perhaps the most useful

● Data type examples...

Page 6: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Control Flow● Python uses indentation to denote blocks of code (no explicit

endif, enddo, ...)

● Control flow via. while, if, for , ...

– Can iterate over items in a list—very useful

● Control flow examples...

Page 7: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Functions● No distinction between functions and procedures/subroutines

– Always return something. If not explicitly set, then None

● Can have default values for arguments, optional arguments, variable number of arguments

● Can return multiple values, objects

● Function examples...

Page 8: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: More Data Structures

● Lists have methods to

– append, pop, sort, count, ...

– We already saw how easy it is to loop over the elements in a list

– Lots of advanced ways to construct lists—see the tutorial

● Tuples and sets—see the tutorial for these

● Dictionaries

– Keyword + value (indexed by keyword instead of a number)

– Keys must be unique

– Useful, for instance, for managing runtime parameters in a code

● Lists and dictionaries examples...

Page 9: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Modules● Modules are a collection of latex statements, functions,

variables, grouped together in a file ending with .py

– Can be imported to be used by any routine

– Python includes a host of useful modules

– You can define your own. Python will look in the current directory and then in the PYTHONPATH

● Modules have their own namespace

– Variables (even global variables) don't clash with those with the same name in the importing program

– You can change the name of a module on import or import it into the current namespace (*).

● General practice: put all the imports at the top of your code

● Modules examples...

Page 10: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Exceptions● Exceptions are raised if an action you tried (e.g. opening a file)

fails.

– Left alone, the code will abort, printing the exception

– You can catch the exception, and take appropriate action to fix the situation

● Built-in exceptions have particular names that let you check for specific failure modes

– Allows you to write robust code for other users

● Exceptions example...

Page 11: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Python: Classes● Classes are a fundamental concept of object-oriented

programming

● An object is an instance of a class

– Carries data (state) and has methods that can act on that data

– Objects are easy to pass around

● Simplest use is just as a container to carry associated data together (similar to a struct in C)

● Inheritance, operator overloading, ... all supported. See the tutorial.

Page 12: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Caveats● Slicing (we'll look at this with arrays soon)

● Function defaults: default values are only evaluated once.

– From python tutorial:

– Prints:

– Correct way:

def f(a, L=[]): L.append(a) return L

print f(1)print f(2)print f(3)

[1][1, 2][1, 2, 3]

def f(a, L=None): if L is None: L = [] L.append(a) return L

Page 13: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Arrays● NumPy provides a multidimensional array

– All elements must be the same data type

– Many different datatypes supported

● Arithmetic operations work on arrays

● Provides MANY functions that operate on whole arrays

– These operations are written in a compiled language, and run fast

– Generally speaking, you want to avoid loops to get the best performance.

● Can sometimes make code unreadable

● Lots of ways to create arrays

● Array creation and property examples...

Page 14: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Array Operations● Arithmetic operator (+, -, /, *) work elementwise

– A * B is not a matrix product, but instead multiples the corresponding elements in each array together

– dot(A,B) does a dot product

● Universal functions (sin, cos, exp, ...) work elementwise

● Array operations examples...

Page 15: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Array Indexing/Slicing

● Biggest source of confusion: selecting a range is best thought of as referring to the “edges” of the array locations

– Differs from Fortran, IDL

– For the array above:● A[2] = 2● A[2:3] = [2]● A[2:4] = [2 3]

– Note also: zero-based indexing

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8

Index of array element Index of “edges”

Note: this same behavior applies to Python lists and strings when slicing

Page 16: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Array Indexing/Slicing

● Remember, multi-dimensional arrays are stored in row-major fashion

– Rows are stored one after the other, within a row, the column data is closest to one another

– You see this when you print an array:● a = numpy.arange(15).reshape(3,5)● print a

[[ 0 1 2 3 4]

[ 5 6 7 8 9]

[10 11 12 13 14]]

● Some slicing examples...

3 rows, 5 columns

Note that the braces [ ] show that the columns are together

Page 17: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Array Views/Copies

● When “copying”, need to understand if two arrays, A and B, point to:

– the same array (including shape and data/memory space)

– the same data/memory space (but perhaps different shapes)

– a separate copy of the data (i.e. stored separately in memory)

● B = A (assignment)

– No copy is made. A and B point to the same data in memory and share the same shape, etc.

● B = A[:] (view or shallow copy)

– The shape info for A and B are stored independently, but both point to the same memory location for the data

● B = A.copy() (deep copy)

– The data in B is stored completely separately in memory from A

● Copying examples...

Page 18: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to NumPy: Boolean Indexing

● Many fancy ways to index arrays

● A[A > 4] = 0

– Boolean indexing

– Similar to IDL's where command

● Boolean indexing example...

Page 19: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

More NumPy

● See the tutorial for some other features:

– Shape manipulation

– Merging/splitting arrays

– Fancier indexing

– Other numpy functions/methods

Page 20: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to SciPy● SciPy is a collection of numerical algorithms that is used with

NumPy

● We'll use a few for convenience

(docs.scipy.org)

Page 21: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to Matplotlib● Very good plotting module

– Line plots, contours, bar/pie charts, histograms, scatter plots, some 3-d features, ...

– Publication-quality output, LaTeX embedding

– Easy to get started

– Very fine control possible

● Easiest way to get started: look at the matplotlib gallery and find a plot resembling what you want to do.

● Some quick examples...

Page 22: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Intro to f2py● Call Fortran routines from python

● Allows for creating “glue” code to stitch a bunch of packages together

● Usually just needs a few special comments at the top of the Fortran routine and then compile with f2py

● Works with numpy arrays

● Automatically deals with row-vs-column major issues

Page 23: Why Python?bender.astro.sunysb.edu/classes/phy688_spring2013/... · – the same array (including shape and data/memory space) – the same data/memory space (but perhaps different

PHY 688: Numerical Methods for (Astro)Physics

Ipython● Interactive python

● Alternate shell for python

● Provides web-based notebook to keep code, text, math, plots, etc.

– Similar to mathematica