Difference between revisions of "Python1"

From SourceWiki
Jump to navigation Jump to search
Line 243: Line 243:
  
 
=Matplotlib=
 
=Matplotlib=
 +
 +
The above examples are quite natty, but we have deliberately kept the array sizes small so that we can print the element values easily.  In practice, you may find that your array sizes are much larger and printing the values to the screen is impractical.  Fear not!  Python has many packages which help you plot your data, so that you can explore it.  One of the many possibilities are outlines below, where we have crafted an array of values which are more pleasing to the eye thusly:
 +
 +
<pre>
 +
>>> x = arange(-5,10)
 +
>>> y = arange(-4,11)
 +
>>> z1 = sqrt(add.outer(x**2,y**2))
 +
>>> Z = sin(z1)/z1
 +
>>> import matplotlib.pyplot as plt
 +
>>> from pylab import meshgrid
 +
>>> X, Y = meshgrid(x,y)
 +
>>> plt.figure()
 +
>>> plt.contour(X,Y,Z)
 +
>>> plt.show()
 +
</pre>
 +
 +
and you should get a window similar to:
 +
 +
[[Image:Sinc-matplotlib-contour.png|thumb|300px|none|A contour map of the sinc function]]
 +
 +
The '''clip''' function is an interesting one.  Using it, we can replace values greater than some threshold with a ceiling and lower than another threshold with a floor.  For example:
 +
 +
<pre>
 +
>>> Z1 = clip(Z,-0.1,0.1)
 +
>>> plt.figure()
 +
>>> plt.contour(X,Y,Z1)
 +
>>> plt.show()
 +
</pre>
 +
 +
gives us:
 +
 +
[[Image:Sinc-contour-truncated.png|thumb|300px|none|A contour map of the sinc function]]
 +
 +
=Input and Output=
 +
 +
 +
The foregoing is all very interesting, but life would be rather dull if you had to re-enter all your data by hand whenever you set to work with Python and numpy.  Therefore we need a means to save data to a file and load it again.  Happily, we can do this rather easily using a couple of routines from the '''pylab''' package:
 +
 +
<pre>
 +
>>> from numpy import *
 +
>>> from pylab import load
 +
>>> from pylab import save
 +
>>> data = zeros((3,3))
 +
>>> save('myfile.txt', data)
 +
>>> read_data = load("myfile.txt")
 +
</pre>
 +
 +
'''warning, the load() function of numpy will be shadowed''' in the above example.  One way to protect yourself against this is to make use of '''namespaces''':  Modify your import command to '''import pylab''' and then use '''pylab.load(..)'''.

Revision as of 11:56, 3 October 2012

Python for Scientists

Introduction

http://xkcd.com/353/

Getting Started on BlueCrystal Phase-2

After you have logged in, type the following at the command line:

module add languages/python-2.7.2.0
python

This should start up an interactive python session:

Python 2.7.2 (default, Aug 25 2011, 10:51:03) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

where we can type commands at the >>> prompt.

With thanks to Simon Metson and Mike Wallace for much of the following material.

Python as a Calculator

Let's try a few commands out. If you type:

>>> print "Hello!"

you'll get:

Hello!

If you try:

>>> print 5 + 9

you'll get:

14

And here is a copy of a session containing a few more commands:

>>> five = 5
>>> neuf = 9
>>> print five + neuf
14
>>> def say_hello():
...     print "Hello, world!"
... # hit return here 
>>> say_hello()
Hello, world!

You can exit an interactive session but typing Ctrl-D.

Getting Help

Python has lots of useful documentation. For example, take a look at: http://docs.python.org/.

Making a Script

An interactive session can be fun and useful for trying things out. However--to save our fingers--we will typically want to execute a series of commands as a script, created using your favourite text editor. Here is the contents of an example script:

#!/bin/env python

print "Hello, from a python script!"

Ensure that your script is executable:

chmod u+x myscript.py

and now you can run it:

[ggdagw@bigblue4 ~]$ ./myscript.py 
Hello, from a python script!

Python and Whitespace

Python incorporates whitespace in it's syntax, so spacing is very important. For example:

Nuts and Bolts

Conditionals

Strings

For example, the eagle-eyed will have spotted in the previous examples that we could ask the length a character string--straight off the bat. No need to write a counting routine ourselves:

>>> print len(message)

We also take slices of our character string. In my case

>>> print message[:5]

elicits:

hello

Lists

An example of a list is:

>>> shopping = ['bread', 'marmalade', 'milk', 'tea']

and we can enquire about the length of that using the same function as before:

>>> len(shopping)

We can also take slices of a list, as we did with a string:

>>> shopping[0:2]

and even reset a portion of the list that way:

>>> shopping[0:2] = ['bagels', 'jam']
>>> shopping

A list object comes with a number of handy methods built in. For example we could type:

>>> shopping.append("butter")
>>> print shopping

and even:

>>> shopping.reverse()
>>> print shopping

or

>>> shopping.sort()
>>> print shopping

Dictionaries

Numpy

K, we could spend quite a while getting to grips with all of Python's myriad features, but we'll move onto looking at numerical features and arrays in particular. To do this we will load a package. You can do this by typing:

from numpy import *

Now that we have access to the package, let's create an array. Note that a numpy array is an objects of a different type to an intrinsic array in Python. A simple approach is to use the array function. For example we might enter:

>>> a = array([[1.0, 0.0, 0.0],[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]])
>>> print a

Given an array, we may enquire about it's shape:

>>> print a.shape

and we are told that it is a 2-dimensional array (i.e. an array of rank 2) and that the length of both dimensions is 3:

(3, 3)

Should we so desire, we could re-shape the array. One way to do this is to to set it's shape attribute directly:

>>> a.shape = (1,9)
>>> print a

We can also apply operators to array objects. For example:

>>> a = a * 9
>>> print a

Note, however, that most operations on numpy arrays are done element-wise, which may be different to a linear algera operation that you were expecting. We will return to linear algebra operations in a later section.

As with the list example, it can be useful to read or change the value of an element (or sub array) indidually. Let's turn the array back to it's rank-2 form and try it out:

>>> a.shape = (3,3)
>>> a[1,1] = 777.0
>>> print a
>>> a[1:,1:] = [[777.0, 777.0],[777.0, 777.0]]
>>> print a

This is all pretty handy so far, but specifying the value of each element explicitly could become a chore. Happily some helper functions exist to give you a head start with some building blocks. For example, your can use:

>>> b = zeros((3,3)
>>> print b
>>> b = ones((3,2))
>>> print b
>>> b = identity(2)
>>> print b
>>> big = resize(b, (6,6))
>>> print big

The use of resize in the last example illustrates a useful replicating feature.

Matplotlib

The above examples are quite natty, but we have deliberately kept the array sizes small so that we can print the element values easily. In practice, you may find that your array sizes are much larger and printing the values to the screen is impractical. Fear not! Python has many packages which help you plot your data, so that you can explore it. One of the many possibilities are outlines below, where we have crafted an array of values which are more pleasing to the eye thusly:

>>> x = arange(-5,10)
>>> y = arange(-4,11)
>>> z1 = sqrt(add.outer(x**2,y**2))
>>> Z = sin(z1)/z1 
>>> import matplotlib.pyplot as plt
>>> from pylab import meshgrid
>>> X, Y = meshgrid(x,y)
>>> plt.figure()
>>> plt.contour(X,Y,Z)
>>> plt.show()

and you should get a window similar to:

A contour map of the sinc function

The clip function is an interesting one. Using it, we can replace values greater than some threshold with a ceiling and lower than another threshold with a floor. For example:

>>> Z1 = clip(Z,-0.1,0.1)
>>> plt.figure()
>>> plt.contour(X,Y,Z1)
>>> plt.show()

gives us:

A contour map of the sinc function

Input and Output

The foregoing is all very interesting, but life would be rather dull if you had to re-enter all your data by hand whenever you set to work with Python and numpy. Therefore we need a means to save data to a file and load it again. Happily, we can do this rather easily using a couple of routines from the pylab package:

>>> from numpy import *
>>> from pylab import load
>>> from pylab import save
>>> data = zeros((3,3))
>>> save('myfile.txt', data)
>>> read_data = load("myfile.txt")

warning, the load() function of numpy will be shadowed in the above example. One way to protect yourself against this is to make use of namespaces: Modify your import command to import pylab and then use pylab.load(..).