Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004

Resources for self-directed learning 

Things you are going to need to get started

  • an editor that does python highlighting (gedit, emacs-linux, eclipse-linux,win,osx)

  • a python interpreter 

    • python < 3.0 is syntatically different from python >= 3.0

      • you want python 2.6 or 2.7

    • I use ipython 

  • Sage can also function as a python compiler, although the learning curve is a bit steep

  • There are hundreds of python libs for math/stats, the main ones you will need are scipynumpy,  and pylab 

    • There are (probablly) binaries for windows

    • There are Debain packages for Debian-flavored linux distros 

Python basics

  • python is an interpreted language (like R) 

  • python is a combination of object-oriented (classes, inheritance) and functional paradigms

  • unless you are designing a whole API (you are not) its probably best to think of it as mostly functional environment (declaring variables in a global name space and writing methods to manipulate the state of those variables)

  • python should written in the most readable way possible; the standard style guide and docstring guides are widely available and great for insomnia.

Python objects, Lists

#assign an empty list alist = list() blist = [] #append concatenates its argument to the list, extend is an item-wise append alist.append("a") blist.extend([1, 2, 3]) blist
[1, 2, 3]
#list indices start at 0 blist[0]
1
#lists can contain objects of any type including other lists alist.append(10) alist.append(["a string", 1e6, True]) alist
['a', 10, ['a string', 1.00000000000000e6, True]]
#contigious sub-lists can be extracted by slicing some_numbers = range(1000) subset = some_numbers[5:10] subset
[5, 6, 7, 8, 9]
#assign an empty dictionary to adict adict = {} #the string "case1" (the key) now maps to a list (the value) adict['case1'] = ["John", "Smith", "HIV-"] adict
{'case1': ['John', 'Smith', 'HIV-']}
#append a string to a hashed list adict['case1'].append('new data') adict
{'case1': ['John', 'Smith', 'HIV-', 'new data']}
#any immutable object can be a dictionary key (i.e. lists can not be keys) adict[[2, "case2"]] = ["Jill"]
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_32.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("I2FueSBpbW11dGFibGUgb2JqZWN0IGNhbiBiZSBhIGRpY3Rpb25hcnkga2V5IChpLmUuIGxpc3RzIGNhbiBub3QgYmUga2V5cykKCmFkaWN0W1syLCAiY2FzZTIiXV0gPSBbIkppbGwiXQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmp3KZArv/___code___.py", line 4, in <module> exec compile(u'adict[[_sage_const_2 , "case2"]] = ["Jill"]' + '\n', '', 'single') File "", line 1, in <module> TypeError: unhashable type: 'list'
#tuples are immutable lists and can function as keys adict[(2, "case2")] = ["Jill"] adict
{'case1': ['John', 'Smith', 'HIV-', 'new data'], (2, 'case2'): ['Jill']}

Python basics, loops and list comprehensions

  • Any itterable object can be looped (lists, tupples, dictionary, arrays, etc...)
iter = [1,2,3,4,5,6] for x in iter: print x**2
1 4 9 16 25 36
#for loops can assign variables from tuples in the loop definition #for example, dict.items() returns a list of key-value tuples which are unpacked and assigned to the names k and v respectively for k, v in adict.items(): print k, v[0]
case1 John (2, 'case2') Jill
  • while loops constantly executes while <expression> evaluates to true
  • while <expression>:
  • everything in python has a truth value and therefore any thing function as <expression>
#list comprehensions are very ways to do stuff to lists [float(x) for x in iter]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
#list comprehensions can accept conditionals also [float(x) for x in iter if x > 3]
[4.0, 5.0, 6.0]

The scipy ODE solver 

  • First, we need to import some external libraries into the global namespace
import scipy.integrate as slv import numpy as np import pylab as pl
slv.odeint?

File: /home/ethan/software/sage-4.6.2-linux-32bit-ubuntu_10.04_lts-i686-Linux-i686-Linux/local/lib/python2.6/site-packages/scipy/integrate/odepack.py

Type: <type ‘function’>

Definition: slv.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)

Docstring:

Integrate a system of ordinary differential equations.

Solve a system of ordinary differential equations using lsoda from the FORTRAN library odepack.

Solves the initial value problem for stiff or non-stiff systems of first order ode-s:

dy/dt = func(y,t0,...)

where y can be a vector.

func : callable(y, t0, ...)
Computes the derivative of y at t0.
y0 : array
Initial condition on y (can be a vector).
t : array
A sequence of time points for which to solve for y. The initial value point should be the first element of this sequence.
args : tuple
Extra arguments to pass to function.
Dfun : callable(y, t0, ...)
Gradient (Jacobian) of func.
col_deriv : boolean
True if Dfun defines derivatives down columns (faster), otherwise Dfun should define derivatives across rows.
full_output : boolean
True if to return a dictionary of optional outputs as the second output
printmessg : boolean
Whether to print the convergence message
y : array, shape (len(y0), len(t))
Array containing the value of y for each desired time in t, with the initial value y0 in the first row.
infodict : dict, only returned if full_output == True

Dictionary containing additional output information

key meaning
‘hu’ vector of step sizes successfully used for each time step.
‘tcur’ vector with the value of t reached for each time step. (will always be at least as large as the input times).
‘tolsf’ vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected.
‘tsw’ value of t at the time of the last method switch (given for each time step)
‘nst’ cumulative number of time steps
‘nfe’ cumulative number of function evaluations for each time step
‘nje’ cumulative number of jacobian evaluations for each time step
‘nqu’ a vector of method orders for each successful step.
‘imxer’ index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise.
‘lenrw’ the length of the double work array required.
‘leniw’ the length of integer work array required.
‘mused’ a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff)
ml, mu : integer
If either of these are not-None or non-negative, then the Jacobian is assumed to be banded. These give the number of lower and upper non-zero diagonals in this banded matrix. For the banded case, Dfun should return a matrix whose columns contain the non-zero bands (starting with the lowest diagonal). Thus, the return matrix from Dfun should have shape len(y0) * (ml + mu + 1) when ml >=0 or mu >=0
rtol, atol : float
The input parameters rtol and atol determine the error control performed by the solver. The solver will control the vector, e, of estimated local errors in y, according to an inequality of the form max-norm of (e / ewt) <= 1, where ewt is a vector of positive error weights computed as: ewt = rtol * abs(y) + atol rtol and atol can be either vectors the same length as y or scalars. Defaults to 1.49012e-8.
tcrit : array
Vector of critical points (e.g. singularities) where integration care should be taken.
h0 : float, (0: solver-determined)
The step size to be attempted on the first step.
hmax : float, (0: solver-determined)
The maximum absolute step size allowed.
hmin : float, (0: solver-determined)
The minimum absolute step size allowed.
ixpr : boolean
Whether to generate extra printing at method switches.
mxstep : integer, (0: solver-determined)
Maximum number of (internally defined) steps allowed for each integration point in t.
mxhnil : integer, (0: solver-determined)
Maximum number of messages printed.
mxordn : integer, (0: solver-determined)
Maximum order to be allowed for the nonstiff (Adams) method.
mxords : integer, (0: solver-determined)
Maximum order to be allowed for the stiff (BDF) method.

ode : a more object-oriented integrator based on VODE quad : for finding the area under a curve

#first task is to define a function that returns dY/dt (a vector) given a vector of parameters and a state vector at time t (useful for time variable parameters, can be otherwise ignored) #parameters beta = 0.05 #per-act probability of transmission c = 10 #contact rate d = 0.01 #death rate def odefunc(Y, t=0): sus, inf = Y #unpack and name the state variables for clarity N = np.sum(Y) incidence = sus * c * beta * inf/float(N) death = inf * d return [-incidence, incidence - death] #test the ode function odefunc([10000, 10])
[-4.99500499500499, 4.89500499500499]
#the solver needs the initial conditions and a sequence of times (the solver is adaptive so I'm not sure why the sequence is needed) start, stop = 0, 120 span = stop - start out = slv.odeint(odefunc, y0=[10000, 1], t=np.linspace(start, stop, span*100), full_output=True)
#when in doubt print the object type, multiple returns will almost print type(out)
<type 'tuple'>
#python allows multiple returns, returns are always placed in a tuple # in this case, the first element is the state of the system states = out[0] states
array([[ 1.00000000e+04, 1.00000000e+00], [ 9.99999499e+03, 1.00491194e+00], [ 9.99998995e+03, 1.00984801e+00], ..., [ -8.57803540e-10, 3.63588769e+03], [ -8.54621682e-10, 3.63552409e+03], [ -8.51391528e-10, 3.63516053e+03]])
# the second element is a dictionary with additional information on the solution infodic = out[1] #the keys to a dictionary with other useful information on the information for k, v in infodic.items(): print k, v
nfe [ 7 9 11 ..., 477 477 477] nje [0 0 0 ..., 0 0 0] tolsf [ 0. 0. 0. ..., 0. 0. 0.] nqu [2 2 2 ..., 3 3 3] lenrw 52 tcur [ 1.02343971e-02 2.04663527e-02 3.06983082e-02 ..., 1.21510430e+02 1.21510430e+02 1.21510430e+02] hu [ 0.01023196 0.01023196 0.01023196 ..., 1.85333389 1.85333389 1.85333389] imxer -1 leniw 22 tsw [ 0. 0. 0. ..., 0. 0. 0.] message Integration successful. nst [ 3 4 5 ..., 230 230 230] mused [1 1 1 ..., 1 1 1]

Plotting the time trajectory of the system with pylab

#first we want to extract the solution times and the state vector at those times #the solver returns a numpy array, which is like an unmodifiable list that behaves more 'mathy' than a regular list. #the solution is a 2D array (arrays can have any integer of dimensions), columns of 2D arrays can be selected thus-wise states[:,0]
array([ 1.00000000e+04, 9.99999499e+03, 9.99998995e+03, ..., -8.57803540e-10, -8.54621682e-10, -8.51391528e-10])
pl.close() pl.plot(infodic['tcur'], states[:,0][1:], 'b-', label='susceptibles') pl.show() pl.savefig('p1')
#additional aspects can be added to the plot by calling other pylab functions pl.plot(infodic['tcur'], states[:,1][1:], 'r-', label='infecteds') pl.legend() pl.show() pl.savefig('p1')