Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
IFISC March, 2008 Python for Scientists: theory and practical examples Adrian Jacobo http://ifisc.uib.es - Mallorca - Spain Outline • What is Python, and why is useful • Basic notions • Examples • Conclusions • Bonus tracks http://ifisc.uib.es What is Python? • Python is a Open Source programming language, available for Windows, Linux, Mac, etc. • Python is strongly typed (i.e. types are enforced), • Python is dynamically and implicitly typed (i.e. you don't have to declare variables) • Python is case sensitive (i.e. var and VAR are two different variables) • Python is object-oriented (i.e. everything is an object). • Python has a very clear syntax, and is very easy to use. http://ifisc.uib.es Why do I need to learn Python? • You don’t need to learn Python, but if you do (at least just a bit) it can help you with your work making it easier. • You can create scripts to run your programs, interact with your libraries, plot your results, and even analyze your data and act in consequence. • You can create graphic interfaces for your programs quite easily (if you find a good reason to do it). • It’s always good to learn new things. (Is the easiest way I know to learn object-oriented programing) • To run Python, just type python in a terminal window. http://ifisc.uib.es Objects 101 • Python is object-oriented: everything is an object. • An object has properties and methods: File= open ( 'someFile.txt', 'w' ) • File is an object. It has his own attributes which describe it: File.closed File.encoding File.mode File.name File.newlines File.softSpace • And it has its own methods: File.next() File.read() File.readline() File.readlines() File.xreadlines() File.seek() File.tell() http://ifisc.uib.es How do I get help? • To get help about an object, just type: > help(5) [This will give you all the available documentation about integers] • If you want to know the methods of an object, type: > dir(5) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__‘,…] • To know what a method does, type: >abs.__doc__ 'abs(number) -> number\n\nReturn the absolute value of the argument.' http://ifisc.uib.es Syntax • Python has no mandatory statement termination characters and blocks are specified by indentation. Indent in to begin a block, indent out to end one. Statements that expect an indentation level end in a colon : a=1 if a==0: print ‘a is equal to 0!’ elif a==1: print ‘a is equal to 1!’ else: print ‘Im, a computer, if a is not equal to 1 or to 0, a does not exists’ http://ifisc.uib.es Data Types • Python has all the traditional data types: integers, floats, strings, etc. • Data arrays are: lists, tuples and dictionaries: • List are one dimensional data arrays, you can combine several data types in one list, and even make lists of lists: > mylista=[1,1.2,’cow’,[‘milk’,2]] The first element this list is mylista[0], and the last mylista[-1] • Tuples are also one dimensional arrays, but their elements are fixed, once defined cannot be changed. > mytuple=(1,1.2,’cow’,mylista) • Dictionaries are associative arrays, it’s content is indexed by keys: > mydict = {"Key 1": "Value 1", 2: 3, "pi": 3.14} > print mydict[“pi”] 3.14 • Arrays can be combined in any way, you can create dictionaries of lists, or tuples, list of dictionaries, etc. http://ifisc.uib.es Strings • Strings in Pyhton are very powerful and flexible. • Strings are defined with simple (‘) or double (“) quotes: > str=‘This is a string’ • Elements of the string can be accessed as elements of a list: > print str[0:4] This • • Modulo operator (%) replaces elements of a tuple in the string: > str= ‘Pi is approximately %s:’ (3.14) > print str Pi is approximately 3.14 You can also substitute by dictionary entries: > print "This %(verb)s a %(noun)s." % {"noun": "test", "verb": "is"} This is a test http://ifisc.uib.es Functions • Functions are defined like this: >def myfunction(arg1, arg2 = 100, arg3 = "test"): ... return arg3, arg2, arg1 Here, arg2, and arg3 are optional parameters whose default values are 100 and “test” respectively: > ret1, ret2, ret3 = myfunction("Argument 1", arg3 = "Named argument") > print ret1,ret2,ret3 Named argument 100 Argument • Lambda functions are ad hoc functions that are comprised of a single statement : > addone = lambda x: x + 1 > print addone(3) 4 http://ifisc.uib.es Modules and exceptions • External libraries (modules) are imported and called like this: >import time >time.sleep(3) • Individual functions of a library can be also imported: > from time import sleep > sleep(3) • Exceptions allow you to handle errors: > ... try: file=(‘Nonexistentfile.txt’,’r’) ... except : ... print "Oops, the file does not exist!“ ... else: ... pass http://ifisc.uib.es Example 1 • Run several simulations with different parameters: #!/usr/bin/python #path to the python interpreter import os import time alist=[1.2,2.2,3.5] #a parameter list blist=[1.1,2.1,3.1] #b parameter list for a in alist: for b in blist: f=open('param.dat','w') #open the parameter file name = "%.4f" % a+ '_' +"%.4f" % b+'.dat‘ #generate the filename f.write("%.8f" % a +'\n') #write parameter a to the parameter file f.write("%.8f" % b +'\n') #write parameter b to the parameter file f.write(name +'\n') #write the output filename f.close() #close the parameter file os.system(‘run myprogram <param.dat &') #run the program (nuredduna style) time.sleep(3)#wait 3 seconds so the program can read the parameter file. http://ifisc.uib.es Example 2 • Run several simulations but only 10 at a time, so the rest of nuredduna users don’t hate you: #!/usr/bin/python #path to the python interpreter import os import time alist=range(0,1000,0.1) for a in alist: f=open('param.dat','w') #open the parameter file name = "%.4f" % a+ '_' +"%.4f" % b+'.dat‘ #generate the filename f.write("%.8f" % a +'\n') #write parameter a to the parameter file f.write(name +'\n') #write the output filename f.close() #close the parameter file os.system(‘run myprogram <param.dat &') #run the program (nuredduna style) noend=1 . . . http://ifisc.uib.es Example 2 for a in alist: ... noend=1 while noend: #keep repeating this while the number of processes is larger than 10 try: #i do this because grep sometimes fails fin,fout=os.popen2('estado |grep jacobo') #do a “Estado”, and save the output in fout gr=fout.readline() #read the first line of fout, which says how many processes are you running l= gr.split() #split the line nproc=int(l[3]) #read the third word of gr and convert it to an integer (number of processes runing) except: nproc=10 #if I cant read estado, I assume that the number of processes runing is 10 if nproc > 9: print l[3],' procesos ejecutandose' time.sleep(30.0) #if there are more than 10 processes I wait else: noend=0 #if there are less than 10 I stop waiting and run another process http://ifisc.uib.es Example 3 • Run several simulations from parameter files that I’ve already generated: #!/usr/bin/python #path to the python interpreter import os list = [ i for i in os.listdir("./") if ".dat" in i] #create a list with the # names of the parameter files (*.dat) list.sort() #sort the list alphabeticaly (just because I’m fancy) for paramfile in list: #do a loop over the file names os.system(‘run myprogram <‘+paramfile+’ &’) #run the program for each file http://ifisc.uib.es Python modules • Thousand of free python libraries are available, some of the most utile for physics are: • Numpy: Fast arrays, complex numbers, fourier transforms, basic algebraic operations. Tools for integrating C/C++ or fortran code. • Scypy: Numeric calculus in python. Numerical integration, optimization, etc. Also a powerful (very powerful) graphic module. When run in console mode, is very similar to Matlab. • WxWindows: one of the many graphic libraries for Python. Create graphic interfaces for your program fast and easy (if you find a reason to do so) • And many more: modules that support thousands of file formats, SSH, data mining from internet or specific databases, python for java, etc, etc, etc. http://ifisc.uib.es Bonus Track 1 • How do I create a movie with my results? • First create a series of images in your favorite format (is suggest you png) using your favorite program (i.e. matlab, idl, etc) • In windows you can use Image To Avi, the old but very functional version is free and really (really) easy to use. You can find it in Google. • In Windows or in Linux or Mac you can use also mencoder like this: mencoder 'mf://*.png' -mf fps=1 -o peli.avi -ovc xvid -xvidencopts bitrate=687 • Where fps is the frames per second, and peli.avi the name of the output file. This will encode the movie using xvid codec, which is free. But be careful! You must be sure that the code that you use to encode the movie is installed in the Pc where you want to play it. http://ifisc.uib.es Bonus Track 2 • I did a really nice movie, but when I play it in PowerPoint I only see a black square… • That’s because you pressed Fn+F5 when connecting the projector… • What you have to do is press the right button over the desktop, and choose “Properties”: • Once you extended the desktop choose in PowerPoint the menu “Presentation” • Then choose “Configure Presentation” and in the option “Several monitors” choose the monitor number 2. • Finnally activate the option “Show moderator view” http://ifisc.uib.es Bonus Track 3 • • How to use RSS to be up to date with the scientific publications? • Look for the RSS logo • Click over the link or the logo • Follow the instructions. • It’s really simple, that’s why is called “Real Simple Syndication” or a link with the name RSS Useful links: • www.python.org • www.scipy.org • http://www.poromenos.org/tutorials/python • www.google.com • www.wikipedia.org • http://ifisc.uib.es/intraweb/wiki/doku.php?id=informatica:general:inicio http://ifisc.uib.es The End Thank you for your attention http://ifisc.uib.es