15
Python for Science Shane Grigsby

Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Embed Size (px)

Citation preview

Page 1: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Python for Science

Shane Grigsby

Page 2: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

What is python? Why python?

• Interpreted, object oriented language• Free and open source

• Focus is on readability• Fast to write and debug code• Large community– Lots of documentation– Lots of packages…

• General purpose language

Page 3: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

The python scientific stack:

Page 4: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Python: Fast to write, slow to run?

• Depends on how you use it– if something is slow there is probably a faster way to do it!

Are you using a numeric library?Are you slicing through arrays, or looping lists?Is your code vectorized?

Numpy calls fortran code to do array operationsMany other libraries call C for operations… …or have functions written in both C and python

e.g., scipy.spatial.kdtree vs. scipy.spatial.cKDTree

Page 5: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

How is python different from MATLAB?

• Indexing starts at 0• Space delimited• Default behavior is element-by-element when dealing with arrays

• Functions use ()’s, indexes use []’s, tuples and dictionaries use {}’s• You don’t have to use a ‘;’ on every command• Object oriented

See also: http://mathesaurus.sourceforge.net/matlab-python-xref.pdf

Page 6: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Today’s tutorial• Intro to the scientific stack• Importing modules• Intro to the development environments:

– Spyder– iPython

• Indexing• Defining functions• Plotting and graphics• Intro to data structures• Basic looping (maybe…)• Additional pandas

– data import from clipboard– time series (on your own)

Notebooks:• SARP python tutorial• Prism Data• Regression loopsFiles:• Leaf_Angles.csv• good_12_leafangle.h5• monthly.nc # Optional• *_spec.txt

Page 7: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Terminals and Prompts

• We’ll use python and python tools from three different ‘prompts’:– The system prompt (i.e., cmd)– From spyder– From the iPython Notebook

• Note that these will all run the same version of python, but with slightly different behaviors

Page 8: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Notebooks:• SARP python tutorial• Prism Data• Regression loopsFiles:• Leaf_Angles.csv• good_12_leafangle.h5• monthly.nc # Optional

Page 9: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Imports

Basic python is sparse……but we can import!

import tables

import numpy as np

from pylab import *

From scipy import ones, array

Page 10: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Example time: Using the iPython notebook

• Notes:– ‘%’s are specific to the iPython NB’s; they won’t

work in your terminal– We’ll use: %pylab inline• This doesn’t work for 3D or interactive plots (yet)• Use spyder or ipython (without the notebook) to access

interactive graphics.

Page 11: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Imports

• Pull from the python install directory first– i.e., lib/python2.7/site-packages

• Pull from the current directory second• Conflicting imports are replaced with the last

import

Page 12: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Defining a function• Multiline comment• Keyword arguments

– can use default values• Definition syntax

– return is optional• Constants defined at

the top of the script

• Top line brings in physical constants, so we don’t have to define them ourselves…

from scipy.constants import *

def Xwave(wavelength, temp, unit=1): X_wave = (h*c)/(k*(wavelength*unit)*temp) return X_wave def Lwave(wavelength, temp, unit=1):

"""Calculates L given wavelength and Temp To get M, multiply by pi Note that units are: W * m**-2 * sr**-1 * m**-1 I.e, units are given in meter of spectrum multiply by nm to get: W * m**-2 *sr**-1 nm**-1""”

X_funct= Xwave(wavelength, temp, unit) L=(2*h*(c**2))/(((wavelength*unit)**5)*(exp(X_funct)-1)) return L

ang = 1E-10nm = 1E-9um = 1E-6Cm = 1E-2hH = 1.0kH = 1E3mH = 1E6gH = 1E9tH = 1E12

Page 13: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Defining Functions

• Functions are defined using ‘def’, function name, ‘()’’s with parameters, and an ending ‘:’

• The function body is demarcated using white space (as in for loops)

• Functions are called using the function name, ‘()’’s, and input parameters– Note that the input parameters don’t have to

match the names that requested…

Page 14: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

Looping in pythonfor item in list: print item • ‘item’ is a variable name; it is not declared in advance; it is arbitrary

(i.e., could be ‘i’, ‘point’, or some other label). There could also be more them one variable here—see next slide…

• ‘for’ and ‘in’ are syntactically required; they bracket our variables.• ‘list’ could be another data structure (dictionary, array, etc.), or could be

a function in advanced use.• Note: else and elseif are not required, but can be used• Note: the white space is required—either a tab or four spaces• Note: we don’t need to know how many items are in the data structure

for i in range(len(list)): # NO… print list[i] # executes, but is wrong

Page 15: Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast

A more advanced loop:from liblas import fileimport scipy

f = file.File('/Users/grigsbye/Downloads/Alameda_park_trees_pts.las',mode='r')treeData = scipy.ones((len(f),3))for i, p in enumerate(f): treeData[i,0], treeData[i,1], treeData[i,2] = p.x,p.y,p.z

• First line imports a special module to read .las files• Third line reads a .las file into a python object we can loop over• Fourth line creates a scipy/numpy array to hold our data• ‘Enumerate’ returns an index number for each point that we are looping over• The last line assigns the x, y, and z values to our array, incrementing the index by one with

each loop • For a more complete guide to looping, see:http://nedbatchelder.com/text/iter.html