Scientific Programming in Python
Eric Christiansen
UCSD
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years.
It is:
cross platform object oriented open source
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years.
It is:
cross platform
object oriented open source
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years.
It is:
cross platform object oriented
What is Python?
Python in a very high level (scripting) language which has gained widespread popularity in recent years.
It is:
cross platform object oriented open source
Why should I care?
You may need to use a computer to run simulations
crunch data display data...
Why should I care?
You may need to use a computer to run simulations
crunch data display data...
Why should I care?
You may need to use a computer to run simulations
crunch data display data...
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python’s Scientific Libraries
Python is enhanced by a large set of scientific libraries that are being actively developed.
Suppose you want:
standard science and engineering functions or plotting (MATLAB)
SciPy, Matplotlib
a computer algebra system (Mathematica)
SAGE
data processing
Modular toolkit for Data Processing (MDP)
bioinformatics functions
Biopython
machine learning functions
PyML, mlpy, SHOGUN
neural nets
Fast Artificial Neural Network (FANN) Library
artificial intelligence or robotics routines
Python vs MATLAB
Advantages of MATLAB:
already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing
easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality
modern language with support for object orientation support for calling functions in other languages
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation
Python vs MATLAB
Advantages of MATLAB: already widely used
designed specifically for scientific computing easy to find documentation
good IDE with debugging and profiling support “out of the box”
Advantages of Python:
open source means no limits on use
appears to approximately superset MATLAB’s functionality modern language with support for object orientation support for calling functions in other languages
Python Intro - Basics
To get information on an object from the interpreter h e l p <o b j e c t >
Commenting:
Inline comments are preceded with # Block comments are surrounded with ””” Code blocks are denoted with indentation:
i f x == 2 : p r i n t x
Python is dynamically typed:
a = ” h e l l o ” # a i s a s t r i n g
Python Intro - Vectors and Matrices
Many of these functions come from SciPy. from s c i p y import ∗ Vectors: N = 5 # a s c a l a r v = [ 1 , 2 , 3 ] # a l i s t v = a r r a y ( [ 1 , 2 , 3 ] ) # a column v e c t o r v = a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) # a column v e c t o r v = a r r a y ( [ [ 1 , 2 , 3 ] ] ) # a column v e c t o r v = t r a n s p o s e ( v ) # t r a n s p o s e a v e c t o r # ( row t o column # o r column t o row ) v = a r a n g e ( −4 ,4) # a v e c t o r i n # a s p e c i f i e d r a n g e : v = p i ∗ a r a n g e ( −4 ,4)/4 v = a r a n g e ( − 4 , 4 , . 5 ) # a r a n g e ( s t a r t , s t o p , s t e p ) v = [ ] # empty l i s tPython Intro
Matrices: m = z e r o s ( [ 2 , 3 ] ) # a m a t r i x o f z e r o s v = o n e s ( [ 1 , 3 ] ) # a m a t r i x o f o n e s v = r a n d ( 3 , 1 ) # r a n d m a t r i x ( s e e a l s o r a n d n ) m = e y e ( 3 ) # i d e n t i t y m a t r i x ( 3 x3 )Python Intro - Vectors and Matrices
v = a r r a y ( [ 5 , 6 , 7 ] ) # a c c e s s a v e c t o r e l e m e n t v [ 2 ] # v e c t o r ( i n d e x ) − a r r a y s a r e # z e r o −i n d e x e d l e n ( v ) # number o f e l e m e n t s i n # a v e c t o r m = a r r a y ( [ [ 1 , 2 , 3 ] , \ [ 4 , 5 , 6 ] ] ) # a 2 x3 m a t r i x m[ 1 , 2 ] == m [ 1 ] [ 2 ] # a c c e s s a m a t r i x e l e m e n t # m a t r i x [ row , column ]blah
m [ 1 , : ] # a c c e s s a m a t r i x row # ( bottom row ) m [ : , 0 ] # a c c e s s a m a t r i x column # ( l e f t column ) m. r e s h a p e ( [ 4 , 1 ] ) # t u r n m a t r i x i n t o a column # v e c t o r ( c o n c a t e n a t e r o w s ) m. s h a p e # s i z e o f a m a t r i x [ rows , c o l s ] m. s h a p e [ 0 ] # number o f r o w s m. s h a p e [ 1 ] # number o f c o l u m n s z e r o s (m. s h a p e ) # c r e a t e a new m a t r i x w i t h # s i z e o f m m = a r r a y ( [ [ ’ h e l l o ’ , sum ] , \ [ 1 , 2 ] ] ) # p u t w h a t e v e r you want # i n t o an a r r a yPython Intro - Vectors and Matrices
Arithmetic operations performed on arrays are done “element by element”. a = a r r a y ( [ 1 , 2 , 3 , 4 ] ) # v e c t o r 2 ∗ a # s c a l a r m u l t i p l i c a t i o n a / 4 # s c a l a r d i v i s i o n b = a r r a y ( [ 5 , 6 , 7 , 8 ] ) # v e c t o r a + b # p o i n t w i s e v e c t o r a d d i t i o n a − b # p o i n t w i s e v e c t o r s u b t r a c t i o n a ∗∗ 2 # p o i n t i s e v e c t o r s q u a r i n g a ∗ b # p o i n t w i s e v e c t o r m u l t i p l y a / b # p o i n t w i s e v e c t o r d i v i d e l o g ( a ) # p o i n t w i s e l o g a r i t h m a r o u n d ( a r r a y ( [ [ . 6 ] , \ [ . 5 ] ] ) ) # p o i n t w i s e r o u n d i n g # ( . 5 r o u n d s t o 0 )
Vector Operations
a = a r r a y ( [ 1 , 4 , 6 , 3 ] ) # v e c t o r sum ( a ) # sum o f v e c t o r e l e m e n t s mean ( a ) # mean o f v e c t o r e l e m e n t s v a r ( a ) # v a r i a n c e s t d ( a ) # s t a n d a r d d e v i a t i o n max ( a ) # maximum a = a r r a y ( [ [ 1 , 2 , 3 ] , \ [ 4 , 5 , 6 ] ] ) # m a t r i xmean ( a , 0 ) # mean o f e a c h column
amax ( a , 1 ) # max o f e a c h row
amax ( a ) # t o o b t a i n max o f m a t r i x # n o t e we u s e 2
Matrix Operations
d o t ( t r a n s p o s e ( a r r a y ( [ 1 , 2 , 3 ] ) ) , \ a r r a y ( [ 4 , 5 , 6 ] ) ) # row v e c t o r 1 x3 t i m e s column # v e c t o r 3 x1 r e s u l t s i n a # s i n g l e number , a l s o known # a s d o t / i n n e r p r o d u c t d o t ( a r r a y ( [ [ 1 ] , [ 2 ] , [ 3 ] ] ) , \ a r r a y ( [ [ 4 , 5 , 6 ] ] ) ) # column v e c t o r 3 x1 t i m e s row # v e c t o r 1 x3 r e s u l t s i n 3 x3 # m a t r i x , a l s o known # a s o u t e r p r o d u c t a = r a n d ( 3 , 2 ) # 3 x2 m a t r i x b = r a n d ( 2 , 4 ) # 2 x4 m a t r i x d o t ( a , b ) # 3 x4 m a t r i xSaving Your Work
import c P i c k l e a s p i c k l e # t h i s module l e t s you # s a v e and r e l o a d o b j e c t s f = open ( ’ s a v e f i l e ’ , ’w ’ ) # open a r c h i v e f i l e p i c k l e . dump ( o b j , f ) # dump o b j e c t t o a r c h i v e f . c l o s e ( ) # c l o s e a r c h i v e f i l e d e l o b j # c l e a r o b j e c t # f r o m memory f = open ( ’ s a v e f i l e ’ , ’ r ’ ) # open a r c h i v e f i l e o b j = p i c k l e . l o a d ( f ) # r e a d o b j e c t # f r o m a r c h i v e f . c l o s e ( ) # c l o s e a r c h i v e f i l e
Relations and Control
Example: given a list v, create a new list u with values equal to v if they are greater than 0, and equal to 0 if they less than or equal to 0.
Using a for loop:
v = [ 3 , 5 , − 2 , 5 , − 1 , 0 ]
u = [ 0 ] ∗ l e n ( v ) # u i s a l l z e r o s f o r i i n r a n g e ( l e n ( v ) ) :
i f v ( i ) > 0 : u ( i ) = v ( i )
Using list comprehension:
v = [ 3 , 5 , − 2 , 5 , − 1 , 0 ]
Importing Functions
Save the following code to “mylib.py”:
d e f myfunc ( a , b ) : r e t u r n a+b ∗∗2
Import and use myfunc. Note, we might need to configure PYTHONPATH.
from m y l i b import myfunc myfunc ( 1 , 2 )
myfunc ( b =2 , a =1) # same a s a b o v e
Python also supports class creation:
c l a s s M y C l a s s :
d e f i n i t ( s e l f ) : p r i n t ” h e l l o ! ” m y c l a s s = M y C l a s s ( )
Plotting
The library matplotlib / pylab is your friend:
from p y l a b import ∗ x s = a r a n g e ( − 2 , 2 , . 0 1 ) p l o t ( x s , s i n ( x s ) ) show ( )
Imaging
We use the Python Imaging Library as well as matplotlib / pylab
from p y l a b import ∗ import Image
im = Image . open ( ’ my image . j p g ’ )
im . show ( ) # we c a n d i s p l a y t h e i m a g e ima = a r r a y ( im ) # t y p e c a s t i n g t o a r r a y # e x t r a c t s p i x e l v a l u e s i m r = Image . f r o m s t r i n g ( ’RGB ’ , \ ( ima . s h a p e [ 1 ] , ima . s h a p e [ 0 ] ) , \ ima . t o s t r i n g ( ) ) # c o n v e r t a r r a y i n t o i m a g e img = ima . mean ( 2 ) # a v e r a g e c o l o r i n t e n s i t i e s
# f o r e a c h p i x e l imshow ( img )
autumn ( ) # s e t d e f a u l t c o l o r m a p t o autumn show ( )
How do I start?
Many guides and tutorials are available online:
Dive Into Python
python introduction for programmers
A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html
How do I start?
Many guides and tutorials are available online: Dive Into Python
python introduction for programmers
A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html
How do I start?
Many guides and tutorials are available online: Dive Into Python
python introduction for programmers
A list of tutorials for Python and some of its many libraries can be found at http://www.awaretek.com/tutorials.html