Home

Python Scientific lecture notes

image

Contents

1. view gt readonly 0 view gt format i view gt len 4 view gt itemsize sizeof int view gt ndim 2 view gt shape malloc sizeof Py_ssize_t 2 view gt shape 0 2 view gt shape 1 2 view gt strides malloc sizeof Py_ssize_t x 2 view gt strides 0 2x sizeof int view gt strides 1 sizeof int view gt suboffsets NULL x Note if correct interpretation requires strides or shape you need to check flags for what was requested and raise appropriate errors The same if the buffer is not readable view gt obj PyObject self Py_INCREF self return 0 static void myobject_releasebuffer PyMemoryViewObject self Py_buffer x view if view gt shape fore view gt shape view gt shape NULL if view gt strides BE view gt strides view gt strides NULL static PyBufferProcs myobject_as_buffer getbufferproc myobject_getbuffer releasebufferproc myobject_releasebuffer hi Standard stuff follows x PyTypeObject PyMyObject_Type static PyObject myobject_new PyTypeObject subtype PyObject xargs PyObject kwds PyMyObjectObject xself static char kwlist NULL if PyArg_ParseTupleAndKeywords args kwds kwlist return NULL self PyMyObjectObject x PyObject_New PyMyObjectObject amp PyMyObject_Type Sel gt ureters lO y self gt buffer 1
2. import datetime dl datetime datetime 2000 1 1 delta datetime timedelta 15 dates dl x x delta for x in range 1 dates datetime idatetime 2000 1 i OF 00 datetime datetime 2000 1 16 0 0 datetime datetime 2000 1 31 0 0 datetime datetime 20007 27 O 0 datetime datetime 2000 3 1 0 0 datetime datetime 2000 3r 16 07 0 datetime datetime 2000 3 31 0 0 datetime datetime 2000 4 15 0 0 datetime datetime 2000 4 30 0 0 datetime datetime 2000 5 15 0 0 We reuse our data from the scatter plot see above 5 8 Other Types of Plots 106 Python Scientific lecture notes Release 2011 1 y cre 0 We 42 gt Sp 27 Be ie ie Sy 2 Now we can plot the dates at the x axis plot_date dates y AE 5 9 The Class Library So far we have used the pylab interface only As the name suggests it is just wrapper around the class library All pylab commands can be invoked via the class library using an object oriented approach The class Figure lives in the module matplot1lib figure Its constructor takes these arguments figsize None dpi None facecolor None dgecolor None linewidth 1 0 frameon True subplotpars None Comparing this with the arguments of figure in pylab shows significant overlap num None figsize None dpi None facecolor Non dgecolor None frameon Tru
3. Python Scientific lecture notes Release 2011 1 In 39 etc 6 11 2 Image filtering gt gt gt lena scipy lena gt gt gt import numpy as np gt gt gt noisy_lena np copy lena gt gt gt noisy_lena lena std 0 5 np random standard_normal lena shape gt gt gt blurred_lena ndimage gaussian_filter noisy_lena sigma 3 gt gt gt median_lena ndimage median_filter blurred_lena size 5 gt gt gt import scipy signal gt gt gt wiener_lena scipy signal wiener blurred_lena 5 5 noisy lena Gaussian filter median filter Wiener filter And many other filters in scipy ndimage filters and scipy signal can be applied to images Exercise Compare histograms for the different filtered images 6 11 3 Mathematical morphology Mathematical morphology is a mathematical theory that stems from set theory It characterizes and transforms geometrical structures Binary black and white images in particular can be transformed using this theory the sets to be transformed are the sets of neighboring non zero valued pixels The theory was also extended to gray valued images Erosion Dilation Opening Closing a ber Elementary mathematical morphology operations use a structuring element in order to modify other geometrical structures Let us first generate a structuring element 6 11 Image processing scipy ndimage 122 Python Scientific
4. Figure provides lots of methods many of them have equivalents in pylab The methods add_axes and add_subplot are called if new axes or subplot are created with axes or subplot in pylab Also the method gca maps directly to pylab as do legend text and many others There are also several set_something method such as set_facecolor or set_edgecolor that will be called through pylab to set properties of the figure Figure also implements get__somet hing methods such as get_axes or get_facecolor to get properties of the figure In the class Axes we find most of the figure elements such as Axis Tick Line2D or Text It also sets the coordinate system The class Subplot inherits from Axes and adds some more functionality to arrange the plots in a grid Analogous to Figure it has methods to get and set properties and methods already encountered as functions in pylab such as annotate In addition Axes has methods for all types of plots shown in the previous section Other classes such as text Legend or Ticker are setup very similarly They can be understood mostly by comparing to the pylab interface Let s look at an example for using the object oriented API file matplotlib oo py from matplotlib figure import Figure 1 figsize 8 5 2 fig Figure figsize figsize 3 ax fig add_subplot 111 4 line ax plot range 10 0 5 ax set_title Plotted with OO interface 6 ax set_xlabel measured
5. add hook find file hook flymake find file hook 9 2 Debugging workflow I you do have a non trivial bug this is when debugging strategies kick in There is no silver bullet Yet strategies help For debugging a given problem the favorable situation is when the problem is isolated in a small number of lines of code outside framework or application code with short modify run fail cycles 9 2 Debugging workflow 192 Python Scientific lecture notes Release 2011 1 1 Make it fail reliably Find a test case that makes the code fail every time 2 Divide and Conquer Once you have a failing test case isolate the failing code e Which module e Which function e Which line of code gt isolate a small reproducible failure a test case 3 Change one thing at a time and re run the failing test case 4 Use the debugger to understand what is going wrong 5 Take notes and be patient It may take a while Note Once you have gone through this process isolated a tight piece of code reproducing the bug and fix the bug using this piece of code add the corresponding code to your test suite 9 3 Using the Python debugger The python debugger pdb http docs python org library pdb html allows you to inspect your code interactively Specifically it allows you to e View the source code e Walk up and down the call stack e Inspect values of variables e Modify values of variables e Set breakpoints
6. Tta 6 2 ie 19 In 7 while abs z lt 100 Z Zxx2 1 En Lels Cwe lela EnS More advanced features break out of enclosing for while loop ran Dol eae a al In 10 while abs z lt 100 AAE if z imag STE break E ae ARR k melt continue the next iteration of a loop Ss ara MO 2 i gt gt gt for element in a if element 0 continue print 1 element 2 4 Control Flow 17 Python Scientific lecture notes Release 2011 1 o al 2 4 4 Conditional Expressions e if object Evaluates to False any number equal to zero 0 0 0 0 03 an empty container list tuple set dictionary False None Evaluates to True everything else ea Tests equality with logics In 19 1 Out 19 True ea isb Tests identity both sides are the same object ta 20 Tais i Out 20 False Ine 21 a ll pa In 22 b ll pp In 23 a is b Out 23 True ea inb For any collection b b contains a A lp S gt gt gt 2 in b True gt gt gt 5 in b False If b is a dictionary this tests that a is a key of b 2 4 5 Advanced iteration literate over any sequence e You can iterate over any sequence string list keys in a dictionary lines in a file In 11 vowels aeiouy in 12 for in powerful AP if i in vowels haa
7. Painful usage no interactivity during development mandatory compilation steps verbose syntax amp z etc manual memory management tricky in C These are difficult languages for non com puter scientists Scripting languages Matlab e Advantages Very rich collection of libraries with numerous algorithms for many different domains Fast execution because these libraries are often written in a compiled language Pleasant development environment comprehensive and well organized help integrated editor etc Commercial support is available e Drawbacks Base language is quite poor and can become restrictive for advanced users Not free Other script languages Scilab Octave Igor R IDL etc e Advantages Open source free or at least cheaper than Matlab Some features can be very advanced statistics in R figures in Igor etc e Drawbacks fewer available algorithms than in Matlab and the language is not more advanced Some software are dedicated to one domain Ex Gnuplot or xmgrace to draw curves These programs are very powerful but they are restricted to a single type of usage such as plotting What about Python e Advantages Very rich scientific computing libraries a bit less than Matlab though Well thought language allowing to write very readable and well structured code we code what we think Many libraries for other tasks than scient
8. gt gt gt a np ones 3 3 gt gt gt a dtype dtype float64 gt gt gt b np linspace 0 1 6 gt gt gt b dtype dtype float 64 There are also other types gt gt gt d np array 1 2j 3 43 5 6x13 gt gt gt d dtype dtype complex128 gt gt gt e np array True False False True gt gt gt e dtype dtype bool gt gt gt f np array I Bonjour Hello p Hallo Terve Hey IN gt gt gt f dtype dtype S7 t lt strings containing max 7 letters 3 1 5 Basic visualization Now that we have our first data arrays we are going to visualize them Matplotlib is a 2D plotting package We can import its functions as below gt gt gt import matplotlib pyplot as plt the tidy way SS ee On aces gt gt gt from matplotlib pyplot import imports everything in the namespace If you launched Ipython with python x y or with ipython pylab under Linux both of the above com mands have been run In the remainder of this tutorial we assume you have run gt gt gt import matplotlib pyplot as plt or are using ipython pylab which does it automatically 1D plotting gt gt gt x np linspace 0 3 20 gt gt gt y np linspace 0 9 20 22 gt ala Es y line plot gt gt gt ple plot vr 01 7 dot plot gt gt gt plt show lt shows the plot not needed with I
9. This function works with a regular orthogonal grid In 25 s mlab pipeline scalar_field values 14 2 3D plotting functions 266 Python Scientific lecture notes Release 2011 1 In 26 ipw_x mlab pipeline image_plane_widget s plane_orientation x_axes In 27 ipw_y mlab pipeline image_plane_widget s plane_orientation y_axes Interactive image plane widgets drag to change the visualized plane 14 3 Figures and decorations 14 3 1 Figure management Here is a list of functions useful to control the current figure Get the current figure mlab gcf Clear the current figure mlab clf Set the current figure mlab figure 1 bgcolor 1 1 1 fgcolor 0 5 0 5 0 5 Save figure to image file mlab savefig foo png size 300 300 Change the view mlab view azimuth 45 elevation 54 distance 1 14 3 2 Changing plot properties In general many properties of the various objects on the figure can be changed If these visualization are created via mlab functions the easiest way to change them is to use the keyword arguments of these functions as described in the docstrings 14 3 Figures and decorations 267 Python Scientific lecture notes Release 2011 1 Example docstring mlab mesh Plots a surface using grid spaced data supplied as 2D arrays Function signatures MES Wp Zr 5 65 x y z are 2D arrays al
10. gt gt gt al 0 3 5 array 3 4 gt gt gt af4 4 array 44 45 54 55 gt gt gt ar 2 array 2 22 52 gt gt gt a 2 2 2 array 20 22 24 40 42 44 3 1 7 Copies and views A slicing operation creates a view on the original array which is just a way of accessing array data Thus the original array is not copied in memory When modifying the view the original array is modified as well gt gt gt a np arange 10 gt gt gt a array LO Ly 2 S7 gt gt gt 6b al 2 05 array 107 2 4 6 81 gt gt gt b 0 12 gt gt gt b am eave 2y 4 6 81 gt gt gt a 2 array les Hp 2p Se We Be Gp Tp Sie SN gt gt gt a np arange 10 gt gt gt b a 2 copy force a copy gt gt gt b 0 12 gt gt gt a anrrav ALO I 25 du Le Do Gp Yo By SN This behavior can be surprising at first sight but it allows to save both memory and time 3 1 8 Data files Text files Example populations txt 1900 30e3 4e3 51300 1901 aA AaS Oo lEs 48200 1902 TOAS A OSS 41500 3 1 The numpy array object 50 Python Scientific lecture notes Release 2011 1 gt gt gt data np loadtxt populations txt if in current directory gt gt gt data arsav I SO S00 00 4000 51300 LO AAO es 6100 48200 AE TOZO 9800 7 41500 17 gt gt gt np savetxt pop2 txt data gt gt gt data2 np l
11. print Yes print statements do work as a debugging tool However to inspect runtime it is often more efficient to use the debugger 9 3 1 Invoking the debugger Ways to launch the debugger 1 Postmortem launch debugger after module errors 2 Launch the module with the debugger 3 Call the debugger inside the module Postmortem Situation You re working in ipython and you get a traceback Here we debug the file index_error py When running it an IndexError is raised Type Sdebug and drop into the debugger 9 3 Using the Python debugger 193 Python Scientific lecture notes Release 2011 1 In 1 Srun index_error py IndexError Traceback most recent call last home varoquau dev scipy lecture notes advanced debugging_optimizing index_error py in lt module gt 6 a name _ main_ gt index _error 9 home varoquau dev scipy lecture notes advanced debugging_optimizing index_error py in index_erro 3 def index _error 4 ist list foobar gt 5 print lst len lst 6 Ji ase name main__ IndexError list index out of range In 2 debug gt home varoquau dev scipy lecture notes advanced debugging_optimizing index_error pv 5 index_err 4 ist list foobar gt 5 print lst len lst 6 ipdb gt list 1 Small snippet to raise an IndexError 2 3 def index_error 4 ist list foobar gt
12. Following the cumulative probability definition p_i from the previous section the corresponding values will be gt gt gt cprob np arange years_nb dt ype np float 32 ab 1 years_nb cr aL and they are assumed to fit the given wind speeds gt gt gt sorted_max_speeds np sort max_speeds Prediction with UnivariateSpline In this section the quantile function will be estimated by using the UnivariateSpline class which can represent a spline from points The default behavior is to build a spline of degree 3 and points can have different weights according to their reliability Variants are InterpolatedUnivariateSpline and LSQUnivariateSpline on which errors checking is going to change In case a 2D spline is wanted the BivariateSpline class family is provided All those classes for 1D and 2D splines use the FITPACK For tran subroutines that s why a lower library access is available through the splrep and splev functions for respectively representing and evaluating a spline Moreover interpolation functions without the use of FITPACK parameters are also provided for simpler use see interpld interp2d barycentric_interpolate and so on 6 12 Summary exercises on scientific computing 126 Python Scientific lecture notes Release 2011 1 For the Sprogg maxima wind speeds the UnivariateSpline will be used because a spline of degree 3 seems to correctly fit the data gt gt gt from scipy
13. In Ipython it is not possible to open a separated window for help and documentation however one can always open a second Ipython shell just to display help and docstrings e Numpy s and Scipy s documentations can be browsed online on http docs scipy org doc The search button is quite useful inside the reference documentation of the two packages http docs scipy org doc numpy reference and http docs scipy org doc scipy reference Tutorials on various topics as well as the complete API with all docstrings are found on this website Numpy and Scipy Documentation SciPy v0 8 dev Reference Guide DRAFT next modules index SciPy Release 0 8 dev Date February 11 2010 SciPy pronounced Sigh Pie is open source software for mathematics science and engineering SciPy Tutorial o Introduction o Basic functions in Numpy and top level scipy Table Of Contents o Special functions scipy special SciPy o Integration scipy integrate Reference o Optimization optimize Next topic o Interpolation scipy interpolate o Signal Processing signal o Linear Algebra This Page o Statistics o Multi dimensional image processing ndimage o File 10 scipy io Resources o Weave e Release Notes SciPy Tutorial Show Source Scipy org website Fale page Reference Quick search e Clustering package scipy cluster Constants scipy constants e Fourier transforms scipy fftpack Go Integrati
14. ga a Alolo eji If you want two plots side by side you create one row and two columns with subplot 121 and subplot 112 The result looks like this 5 7 Figures Subplots and Axes 100 Python Scientific lecture notes Release 2011 1 AGE 5 os subplot 121 subplot 122 80070706 0810 bo oz or os 08 10 Molo aja You can arrange as many figures as you want A two by two arrangement can be created with subplot 221 subplot 222 subplot 223 and subplot 224 The result looks like this i x 19 es subplot 221 es subplot 222 oa 04 az o2 Yoon oa 06 0s 10 do oz os 06 08 10 os oe es subplot 223 af subplot 224 a oa obo oz os os os 10 So oz as os 08 10 Aolo H Asa Frequently you don t want all subplots to have ticks or labels You can set the xticklabels or the yticklabels to an empty list Every subplot defines the methods is_first_row is_first_col is_last_row is_last_col These can help to set ticks and labels only for the outer pots Axes are very similar to subplots but allow placement of plots at any location in the figure So if we want to put a smaller plot inside a bigger one we do so with axes an 12218 pilora Out 22 lt matplotlib lines Line2D instance at 0x02C9CE90 gt im ASIS a es 102 Wea 029 We 291 ine AA oo E The result look
15. 3 2 Numerical operations on arrays 67 Python Scientific lecture notes Release 2011 1 Indexing with a mask can be very useful to assign a new value to a sub array gt gt gt ala 3 0 1 S55 d area LO l 8 Si 29 10 ii 10 i 1 20 1 7 dl Indexing with an array of integers gt gt gt a np arange 10 gt gt gt a 2 3 to avoid having always the same np arange 10 gt gt gt a array 3 le Di Sp Vo OF So Up aL hi ces Sibley Gp dlp Billy as tole al cara Pa Gy a teil array i 5S 5 ap 1 Indexing can be done with an array of integers where the same index is repeated several time gt Sn 2 Ar Sle A noce 255s 2 47 2 as aeython Last az Sp De Ye Sil New values can be assigned with this kind of indexing gt gt gt als Vil SO gt gt gt a array 3 ile S7 37 he 57 OA ON gt ea Sie 2 Ep El al gt gt gt a array 3 i 6 4 Bp De 9 10 ti 101 When a new array is created by indexing with an array of integers the new array has the same shape than the array of integers gt gt gt a np arange 10 gt gt gt idx np array 3 41 9 711 gt gt gt alidx array Loin 4 ly 9 711 gt gt gt b np arange 10 gt gt gt a np arange 12 reshape 3 4 gt gt gt a array it 0 Wn 2 al Ll SA 6 11 I 8 Oe ALO ATA gt gt gt pra rra I0 1 17 21 gt reel S S See ea l array il
16. Integers signed ints 8 bits int16 16 bits int32 32 bits same as int on 32 bit platform int64 64 bits same as int on 64 bit platform gt gt gt np array 1 dtype int dtype dtype int64 See MO AMO MO ES aie Mead gt al 2147483647 2147483647 gt gt gt np iinfo np int64 max 2 63 1 9223372036854775807 9223372036854775807L Unsigned integers uint8 8 bits uint16 16 bits uint32 32 bits uint64 64 bits 3 3 Moving on 75 Python Scientific lecture notes Release 2011 1 gt gt gt NP o ins Dale SA max Sta I 2147483647 2147483647 gt gt gt np iinfo np uint64 max 2 64 1 9223S 720368547 753807 I223387 20S 68547 75810 7 Floating point numbers float16 16 bits float 32 32 bits float 64 64 bits same as float float96 96 bits platform dependent same as np longdouble float128 128 bits platform dependent same as np longdouble gt gt gt np finfo np float32 eps AAA AN gt gt gt np finfo np float64 eps 2 2204460492503131e 16 gt gt gt np float32 le 8 np float32 1 1 True gt gt gt np float64 1le 8 np float64 1 1 Ealse Complex floating point numbers complex64 two 32 bit floats complex128 two 64 bit floats complex192 two 96 bit floats platform dependent complex256 two 128 bit floats platform dependent Smaller data
17. No further knowledge of OOP than understanding the notation is necessary for going through this tutorial Note Discovering methods In IPython tab completion press tab In 28 r e add A CLASS gt COMta Rs lt aAelatir delitem _ delslice__ doc E if LE E E E iadd e Im o e le _ len Je rae r i Be Steer ie Te ie Int Ini Ik doy Tair toy ain Setatte __setitem__ setslice__ TS Zeor es SE __ subclasshook__ append 2 2 Basic types 12 Python Scientific lecture notes Release 2011 1 Cc eg ema count E format cs r extend ra ge NS r index r _ getattribute r _ reduce r insert r __getitem__ r __reduce_ex__ E POP r __getslice__ r __repr__ r remove Ey OG r __ reversed__ r reverse re hash 125 Ao 1a 5 SIOUAE Strings Different string syntaxes simple double or triple quotes ll s Hello how are you s Hin whats up s anello tripling the quotes allows the how are you the string to span more than one line S SOW what s Upa wu Tn je Ha whee sup ji File lt ipython console gt line 1 Hay whats ups A SyntaxError invalid syntax The newline character is n and the tab character is Xt Strings are collections like lists Hence they can be indexed and sliced using the same syntax and rules Indexing gt gt gt a hello
18. gdb up 9 PyEval_EvalFrameEx f Frame 0x8537lec for file home varoquau usr lib python2 6 site packages numpy core arrayprini A EY E DOn cen ala 202 2412 in Python ceval c gdb Once we are in the Python execution loop we can use our special Python helper function For instance we can find the corresponding Python code gdb pyframe gdb up home varoquau usr lib python2 6 site packages numpy core arrayprint py 158 _leading_trailing gdb This is numpy code we need to go up until we find code that we have written gdb up 34 0x080dc97a in PyEval_EvalFrameEx f Frame 0x82f064c for file segfault py line 11 in print_big_array small_array lt numpy ndarra 1630 Python ceval c No such file or directory ine o Pytnon cevalke gdb pyframe Isegi aullt py LZ R print big r array The corresponding code is def print_big_array small_array big_array make_big_array small_array print big_array 10 return big_array Thus the segfault happens when printing big_array 10 The reason is simply that big_array has been allocated with its end outside the program memory Note For a list of Python specific commands defined in the gdbinit read the source of this file 9 4 Debugging segmentation faults using gdb 198 Python Scientific lecture notes Release 2011 1 Wrap up exercise The following script is well documented and hopefully legible
19. gt gt gt void filtdat lt 50 gt gt gt sand np logical_and filtdat gt 50 filtdat lt 114 gt gt gt glass filtdat gt 114 5 Display an image in which the three phases are colored with three different colors gt gt gt phases void astype np int 2 glass astype np int 3xsand astype np int 6 12 Summary exercises on scientific computing 136 Python Scientific lecture notes Release 2011 1 6 Use mathematical morphology to clean the different phases gt gt gt sand_op ndimage binary_opening sand iterations 2 7 Attribute labels to all bubbles and sand grains and remove from the sand mask grains that are smaller than 10 pixels To do so use ndimage sum or np bincount to compute the grain sizes gt gt gt sand_labels sand_nb ndimage label sand_op gt gt gt sand_areas np array ndimage sum sand_op sand_labels np arange sand_labels max 1 gt gt gt mask sand_areas gt 100 gt gt gt remove_small_sand mask sand_labels ravel reshape sand_labels shape 8 Compute the mean size of bubbles gt gt gt bubbles_labels bubbles_nb ndimage label void gt gt gt bubbles_areas np bincount bubbles_labels ravel 1 gt gt gt mean _ bubble _ size bubbles_areas mean gt gt gt median_bubble_size np median bubbles_areas 6 12 Summary exercises on scientific computing 137 Python Scientific lecture note
20. AttributeError list object has no attribute foobar Different types of exceptions for different errors 2 9 Exception handling in Python 37 Python Scientific lecture notes Release 2011 1 2 9 2 Catching exceptions try except In 8 while True IS Leys Speen x int raw_input Please enter a number ES break ds except ValueError Sore print That was mo val rd number Gry again Please enter a number a That was no valid number Try again Please enter a number 1 In 9 x Owns Shi sil try finally tn 10 Ery da x int raw_input Please enter a number Sees EA alii OR print Thank you for your input Please enter a number a Thank you for your input ValueError invalid literal for int with base 10 a Important for resource management e g closing a file Easier to ask for forgiveness than for permission In 11 def print_sorted collection EAR try METE collection sort ene except AttributeError a pass ties print collection T 2 print sorted AS 2N tp 2p Si In Prsi joreintkaseoreedl seta ly 3 2N ser Lo 2 a In 14 print sorted 132 132 2 9 3 Raising exceptions e Capturing and reraising an exception 2 9 Exception handling in Python 38 Python Scientific lecture notes Release 2011 1 In 15 def filter_name name eek CEY A E name nam nco
21. e Try to limit interdependencies of your code Loose Coupling e Give your variables functions and modules meaningful names not mathematics names 9 1 2 pyflakes fast static analysis They are several static analysis tools in Python to name a few pylint pychecker and pyflakes Here we focus on pyflakes which is the simplest tool e Fast simple e Detects syntax errors missing imports typos on names Integrating pyflakes in your editor is highly recommended it does yield productivity gains Running pyflakes on the current edited file You can bind a key to run pyflakes in the current buffer e In kate Menu settings gt configure kate In plugins enable external tools In external Tools add pyflakes kdialog title pyflakes filename msgbox S pyflakes Sfilename In TextMate Menu TextMate gt Preferences gt Advanced gt Shell variables add a shell variable TM_PYCHECKER Library Frameworks Python framework Versions Current bin pyflakes Then Ctr1 Shi ft V is binded to a pyflakes report In vim In your vimrc binds FS to pyflakes r 9 autocmd FileType python let amp mp echo x x x running pyflakes autocmd FileType tex mp rst python imap lt Esc gt 15 lt C 0 gt make M autocmd FileType tex mp rst python map lt Esc gt 15 make M autocmd FileType tex mp rst python set autowrite Inemacs In y
22. gt gt gt np ma sgre Mi 2 10 masked_array data 1 0 1 41421356237 8 4 Siblings chararray maskedarray matrix 185 Python Scientific lecture notes Release 2011 1 mask Fals True Fals True fill_value 1e 20 Note There s more to them recarray purely convenience gt Ri pS Ce say Ea me gt gt gt arr2 arr view np recarray gt gt gt ares x charra vee Oe ally dtype S1 299 Gelato NV array 1r 210 matrix convenience e always 2 D e x is the matrix product not the elementwise one gt gt gt np matrix 1 01 0 1 np matrix 1 2 3 411 mati 2 ES 411 8 5 Summary e Anatomy of the ndarray data dtype strides e Universal functions elementwise operations how to make new ones e Ndarray subclasses e Various buffer interfaces for integration with other tools e Recent additions PEP 3118 generalized ufuncs 8 6 Contributing to Numpy Scipy Get this tutorial http www euroscipy org talk 882 8 6 1 Why e There s a bug e I don t understand what this is supposed to do e I have this fancy code Would you like to have it e Td like to help What can I do 8 5 Summary 186 Python Scientific lecture notes Release 2011 1 8 6 2 Reporting bugs e Bug tracker prefer this http projects scipy org numpy http projects scipy org scip
23. lt 5x5 sparse matrix of type lt type numpy float64 gt with 20 stored elements in Dictionary Of Keys format gt e slicing and indexing gt Mex 14 0 0 gt gt mins AS lt 1x2 sparse matrix of type lt type numpy float64 gt with 1 stored elements in Dictionary Of Keys format gt gt gt gt mtx 1 1 3 todense maja T on TND gt gt gt mex A AS To dense Traceback most recent call last lt snip gt NotImplementedError fancy indexing supported over one axis only Coordinate Format COO e also known as the ijv or triplet format three NumPy arrays row col data data i is value at row i col i position permits duplicate entries subclass of _data_matrix sparse matrix classes with data attribute e fast format for constructing sparse matrices e constructor accepts dense matrix array sparse matrix 11 2 Storage Schemes 213 Python Scientific lecture notes Release 2011 1 shape tuple create empty matrix data ij tuple e very fast conversion to and from CSR CSC formats e fast matrix vector sparsetools e fast and easy item wise operations manipulate data array directly fast NumPy machinery e no slicing no arithmetics directly e use facilitates fast conversion among sparse formats when converting to other format usually CSR or CSC duplicate entries are summed together x facilitates
24. 2 8 3 Interoperability features 183 Python Scientific lecture notes Release 2011 1 self gt buffer 2 3 self gt buffer 3 4 return PyObjectx self PyTypeObject PyMyObject_1 PyVarObject_HEAD_INI MyObject sizeof PyMyObjectObject 0 x methods x ype o SS on ere eS eS Ss ee eS Ss Sa Ss amp myobject_as_buffer Py_TPFLAGS_DEFAULT A Os os 5 oe gt oe oe E is ee eS 7S eS oe fe Ss 2 fe St GO myobject_new SS AS ere 1S is le er te er struct PyModuleDef moduledef PyModuleDef_HEAD_INIT myobJece NULL 1 NULL NULL 0 x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x a tp_itemsize tp_dealloc x to orime vs tp_getattr x tp_setattr x tp_reserved tp_repr x tp_as_number x tp_as_sequence tp_as_mapping tp_hash x ga ceuli EPs tp_getattro tp_setattro tp_as_buffer x tp_flags tp doc tp_traverse pre lar tp_richcompare a tp_weaklistoffset x qa es 2 tp_iternext tp_methods x tp_ members tp_getset tp_base x Ep dict pides erige tp_descr_set x EpUGiCcEOm nS e Cio iaa lt 3 tp_alloc x tp_new tp_free
25. Inflows hm3 release Range low min_release high max_release spillage Float desc Spillage hm3 def print_state self print Storage tRelease tInflows tSpillage stre format Ne sem si 2b for a Gin range AN print str_format format self storage self release self inflows self spillage PELOS Traits listeners HRRRRRR AAA AAA AA AA ARA RARARARARAMARRARARRARAARARA RARA def _release_changed self new When the release is higher than zero warn all the inhabitants of the valley won if new gt 0 print Warning we are releasing hm3 of water format new if name main__ projectA Reservoir name Project A max_storage 30 max_release 100 0 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 10 state release 90 state inflows 0 state print_state The static trait notification signatures can be e def _release_changed self pass e def _release_changed self new pass e def _release_changed self old new pass e def _release_changed self name old new pass Note Listening to all the changes To listen to all the changes on a HasTraits class the magic _any_trait_changed method can be implemented In many situations you do not know in advance what type of listeners need to be activated Traits offers the ability to register listeners on the fly with the dynami
26. Other spatial measures ndimage center_of_mass ndimage maximum_position etc Can be used outside the limited scope of segmentation applications Example block mean gt gt gt 1 scipy lena gt gt gt sx sy 1 shape gt gt gt X Y np ogridp0tsx Of syl gt gt gt regions sy 6 x X 4 Y 6 note that we use broadcasting gt gt gt block_mean ndimage mean l labels regions index np arange 1 gt gt gt regions max 1 gt gt gt block_mean shape sx 4 sy 6 When regions are regular blocks it is more efficient to use stride tricks Example fake dimensions with strides page 165 Non regularly spaced blocks radial mean gt gt gt rbin 20x r r max astype np int gt gt gt radial_mean ndimage mean 1 labels rbin index np arange 1 rbin max 1 12 6 Measuring objects properties 241 Python Scientific lecture notes Release 2011 1 e Other measures Correlation function Fourier wavelet spectrum etc One example with mathematical morphology granulometry http en wikipedia org wiki Granulometry_ 28morphology 29 gt gt gt def disk_structure n struct np zeros 2 z mr O Mor aeS A 2 iol cr il 2 e ag ae al mask x n 2 y n 2 lt nxx 2 struct mask 1 return struct astype np bool gt gt gt gt gt def granulometry data sizes None s max data shape if sizes None sizes
27. Python Scientific lecture notes Release 2011 1 return functools update_wrapper _wrapper function return _decorator gt gt gt better_replacing decorator _with_args abc def function extensive documentation prine insiden impera y return 14 defining the decorator doing decoration abc gt gt gt function lt HUnCELOn function at Os gt gt gt print function __doc xtensive documentation One important thing is missing from the list of attributes which can be copied to the replacement func tion the argument list The default values for arguments can be modified through the __defaults__ __kwdefaults__ attributes but unfortunately the argument list itself cannot be set as an attribute This means that help function will display a useless argument list which will be confusing for the user of the function An effective but ugly way around this problem is to create the wrapper dynamically using eval This can be automated by using the external decorator module It provides support the decorator decorator which takes a wrapper and turns it into a decorator which preserves the function signature To sum things up decorators should always use functools update_wrapper or some other means of copying function attributes 7 2 4 Examples in the standard library First it should be mentioned that there s a number of useful decorators available in the standard library There are three decorators which really form a p
28. Warning Not all Numpy functions respect masks for instance np dot so check the return types The masked_array returns a view to the original array gt gt gt ms 9 gt gt gt x array Al oF 3 99 51 3 3 Moving on 80 Python Scientific lecture notes Release 2011 1 The mask You can modify the mask by assigning gt gt gt mx 1 np ma masked gt gt gt mx maskcedltarrcay data SS mask Fals True Fals True False Piva lue 999999 The mask is cleared on assignment gt gt gt mx 1 9 gt gt gt mx maskcagtarrcay data lt 5 gt 31 mask False False Fals True False E yaa gt USO The mask is also available directly gt gt gt mx mask array False False False True False dtype bool The masked entries can be filled with a given value to get an usual array back gt gt gt x2 mx filled 1 gt gt gt x2 arcay il Se Se i 7 Sil The mask can also be cleared gt gt gt mx mask np ma nomask gt gt gt mx masked_array data 1 9 3 99 5 mask False False False False False va gt O99 919 Domain aware functions The masked array package also contains domain aware functions 222 Mao doe inyeceucieehy Il 2 il 2 3 93110 Masked array data 10 070 09511718056 1 093861223867 mask False Fals Treg True Fals
29. gt gt gt x array 0 11 EL 911 gt gt gt y np array 1 2 3 4 gt gt gt y y T copy gt gt gt y array 0 1 AL GAN e xandx transpose share data e x x transpose modifies the data element by element e because x and x t ranspose have different striding modified data re appears on the RHS 8 1 Life of ndarray 168 Python Scientific lecture notes Release 2011 1 8 1 5 Findings in dissection ndarray e memory block may be shared base data e data type descriptor structured data sub arrays byte order casting viewing astype view e strided indexing strides C F order slicing w integers as_st rided broadcasting stride tricks diag CPU cache coherence 8 2 Universal functions 8 2 1 What they are e Ufunc performs and elementwise operation on all elements of an array Examples np add np subtract scipy special x e Automatically support broadcasting casting The author of an ufunc only has to supply the elementwise operation Numpy takes care of the rest e The elementwise operation needs to be implemented in C or e g Cython Parts of an Ufunc 1 Provided by user void ufunc_loop void xargs int dimensions int steps void xdata int8 output elementwise _function intg8 input_1 int8 input_2 This function must compute the ufunc for many values at once
30. input_output_types 1 number of supported input types TODO number of input args TODO number of output args 0 identity element never mind this mandel function name man dci aC OMS SA Ou O unused docstring data parameter for the generic T NPY_UINT NPY_DOUBL Y DATETIME Reminder some pre made Ufunc loops PyUfunc_f_f float elementwise_func float input_1 PyUfunc_ff_ffloat elementwise_func float input_1 float input_2 PyUfunc_d_d doub mentwise_func double input_1 PyUfunc_dd doub mentwise_func double input_1 double input_2 PyUfunc_D_D elementwise_func complex_double input complex _doublex output PyUfunc_DD Delementwise_func complex_double inl complex _ double xin2 complex_doublex out Type codes NPY_BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_INT NPY_UINT NPY_LONG NPY_ULONG NPY_LONGLONG NPY_ULONGLONG NPY_FLOAT NPY_DOUBLE NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NPY_DATETIME NPY_TIMEDELTA NPY_OBJECT NPY_STRING NPY_UNICODE NPY_VOID 8 2 3 Solution building an ufunc from scratch The elementwise function cdef void mandel_single_point double complex x z_in 8 2 Universal functions 173 Python Scientific lecture notes Release 2011 1 double complex x
31. rr 2 5 Defining functions 23 Python Scientific lecture notes Release 2011 1 os function body SS pass In 68 funcname Type function Base Class lt type function gt Sian E oem lt function funcname at Oxeaa0f0 gt Namespace Interactiv File Users cburns src scipy2009 lt ipython console gt Definition funcname params Docstring Concise one line sentence describing the function Extended summary which can contain multiple paragraphs Note Docstring guidelines For the sake of standardization the Docstring Conventions webpage documents the semantics and conventions associated with Python docstrings Also the Numpy and Scipy modules have defined a precised standard for documenting scientific functions that you may want to follow for your own functions with a Parameters section an Examples section etc See http projects scipy org numpy wiki CodingStyleGuidelines docstring standard and http projects scipy org numpy browser trunk doc example py L37 2 5 8 Functions are objects Functions are first class objects which means they can be e assigned to a variable e an item in a list or any collection e passed as an argument to another function In 38 va variable_args In 39 va three x 1 y 2 args is three kwargs ales y 2 So 2 5 9 Methods Methods are functions attached to objects You ve se
32. search the minimum only between 0 and 10 gt gt gt optimize fminbound f 0 10 array Sots ao You can find algorithms with the same functionalities for multi dimensional problems in scipy optimize See the summary exercise on Non linear least squares curve fitting application to point extraction in topograph ical lidar data page 128 for a more advanced example 6 11 Image processing scipy ndimage The submodule dedicated to image processing in scipy is scipy ndimage gt gt gt from scipy import ndimage Image processing routines may be sorted according to the category of processing they perform 6 11 1 Geometrical transformations on images Changing orientation resolution gt gt gt import scipy gt gt gt lena scipy lena gt gt gt shifted_lena ndimage shift lena 50 50 gt gt gt shifted_lena2 ndimage shift lena 50 50 mode nearest gt gt gt rotated_lena ndimage rotate lena 30 gt gt gt cropped_lena lena 50 50 50 50 gt gt gt zoomed_lena ndimage zoom lena 2 gt gt gt zoomed_lena shape 1024 1024 rn 35 2 subploe Gist Out 35 lt matplotlib axes AxesSubplot object at 0x925f46c gt In 36 imshow shifted_lena cmap cm gray Out 36 lt matplotlib image AxesImage object at 0x9593f6c gt ta Si ee SO EN mc lle Usa Sill Ss Silas 10 2 5 6 11 Image processing scipy ndimage 121
33. 1 Open the image file MV_HFV_012 jpg and display it Browse through the keyword arguments in the docstring of imshow to display the image with the right orientation origin in the bottom left corner and not the upper left corner as for standard arrays This Scanning Element Microscopy image shows a glass sample light gray matrix with some bubbles on black and unmolten sand grains dark gray We wish to determine the fraction of the sample covered by these three phases and to estimate the typical size of sand grains and bubbles their sizes etc 2 Crop the image to remove the lower panel with measure information 3 Slightly filter the image with a median filter in order to refine its histogram Check how the histogram changes 4 Using the histogram of the filtered image determine thresholds that allow to define masks for sand pixels glass pixels and bubble pixels Other option homework write a function that determines automatically the thresholds from the minima of the histogram 5 Display an image in which the three phases are colored with three different colors 6 Use mathematical morphology to clean the different phases 7 Attribute labels to all bubbles and sand grains and remove from the sand mask grains that are smaller than 10 pixels To do so use ndimage sum or np bincount to compute the grain sizes 8 Compute the mean size of bubbles 6 12 Summary exercises on scientific computing 134 Python Scie
34. 3 3 Moving on 77 Python Scientific lecture notes Release 2011 1 l gt gt gt a np exp 2j np pi np arange 3 gt gt gt b a np newaxis a np newaxis Sea at 00 arrays 02 Dra 0o 023 LO 0 3 Dlls Ty O Ol SOS I ORO OREO eit See help np fft andhelp np fft fft for more These functions in general take the axes argument and you can additionally specify padding etc Worked example Crude periodicity finding 80000 70000 60000 50000 40000 30000 20000 10000 1900 1905 1910 1915 1920 3 3 Moving on 78 Python Scientific lecture notes Release 2011 1 300000 250000 200000 150000 100000 50000 Worked example Gaussian image blur Convolution f t J dt K t t fo t 3 3 Moving on 79 Python Scientific lecture notes Release 2011 1 50 100 150 0 50 100 150 200 250 3 3 4 Masked arrays Masked arrays are arrays that may have missing or invalid entries For example suppose we have an array where the fourth entry is invalid gt gt gt x NO aarray ie 3 99 SI One way to describe this is to create a masked array gt gt gt mx ma masked_array x mask 0 0 0 1 0 gt gt gt mx masked_array data 122 SS a mask False False False True False fill_value 999999 Masked mean ignores masked data gt gt gt mx mean 2o15 gt gt gt np mean mx ATS
35. 5 MOPE As A S oy y IW 20 Bil 22 23 Da AO 2p Bish Al Si Say Ss S44 San OM Go N o oS WNP FP gt gt gt b a reshape 6 1 unspecified 1 value is inferred Copies or views ndarray reshape may return a view cf help np reshape not a copy gt gt gt b 0 0 99 gt gt gt a cae IS i 2 3 4 Sp o To lo Do 10 E 20 al 22r 2r 24 A 267 2i SAS il Li NS y 12 297 13 30 14 SL 15 Se 15 33 Beware gt gt gt a np zeros 3 2 gt gt gt b a T reshape 3x2 gt gt gt b 0 9 gt gt gt a ne Ilys Oly T RON ORT OS Wag To understand see Under the hood below Dimension shuffling gt gt gt a np arange 4 3 2 reshape 4 3 2 gt gt gt a shape 47 37 2 gt gt O aa gt gt gt b a transpose 1 2 0 gt gt gt b shape Gr 27 A gt gt gt b2 10 5 Also creates a view Es bie Q Gl gt gt gt O A i Resizing Size of an array can be changed with ndarray resize gt gt gt a np arange 4 gt gt gt a resize 8 gt gt gt a array Or i 2 8 OO 0 0 01 3 2 Numerical operations on arrays 65 Python Scientific lecture notes Release 2011 1 However it must not be referred to somewhere else gt gt gt b a gt gt gt a resize 4 ValueError cannot
36. Python Scientific lecture notes Release 2011 1 10 2 Profiling Python code No optimization without measuring e Measure profiling timing e You ll have surprises the fastest code is not always what you think 10 2 1 Timeit In IPython use t imeit http docs python org library timeit html to time elementary operations In 1 import numpy as np In 2 a np arange 1000 In 3 Stimeit a xx 2 100000 loops best of 3 5 73 us per loop In 4 Stimeit a xs 2 1 1000 loops best of 3 154 us per loop In 5 timeit a a 100000 loops bese al Se ae Us per loop Use this to guide your choice between strategies Note For long running calls using t ime instead of Stimeit it is less precise but faster 10 2 2 Profiler Useful when you have a large program to profile for example the following file import numpy as np from scipy import linalg from ica import fastica def test data np random random 5000 100 u S v linalg svd data pea np dot u 10 data results fastica pca T whiten False test In IPython we can time the script In 1 srun t demo py IPython CPU timings estimated User TAT SOI isis System Ce SO Uo sr and profile it In 2 run D demo py 916 Eunction calls am W455i CPU seconds 10 2 Profiling Python code 201 Python Scientific lecture notes Release 2011 1 Ordered by internal time ncalls
37. Vector LUT ModuleManager PINZAS N Y Mayavi Scene 1 LUT Look Up Table Manager 1 606 16 Y GridSource x OO V PolyDataNormals Lut mode t as gist_earth Y R Colors and legends i Lo m Surface Number of colors 256 m Outline Reverse lut C oe W Axes Edit LUT properties r on GridSource Launch LUT editor Z v PolyDataNormals J 5 Colors and legends a Show E rae W Surface 1 Title Number of labels 0 1 64 ao 290 17 Shadow C Use default name Y Data name scalar Edit bar Title Edit bar Text Edit bar Actor Edit bar Widget To find out what code can be used to program these changes click on the red button as you modify those properties and it will generate the corresponding lines of code 14 4 Interaction 270 CHAPTER 15 Sympy Symbolic Mathematics in Python author Fabian Pedregosa Objectives 1 Evaluate expressions with arbitrary precision Perform algebraic manipulations on symbolic expressions Perform basic calculus tasks limits differentiation and integration with symbolic expressions Solve polynomial and transcendental equations Solve some differential equations UB un What is SymPy SymPy is a Python library for symbolic mathematics It aims become a full featured computer algebra system that can compete directly with commercial alternatives Mathematica Maple while kee
38. Walking a directory os path walk generates a list of filenames in a directory tree In 10 for dirpath dirnames file for fp in filenames aver ae print os path abspa Users cburns src scipy2009 scipy_20 Users cbhurns sue scipy2009 scipy 20 Users cburns src scipy2009 scipy_20 Users cburns src scipy2009 scipy_20 Users cburns src scipy2009 scipy_20 names in os walk os curdir th fp 09_tutorial source index rst swo 09_tutorial source view_array py swp 09_tutorial source basic_types rst 09_tutorial source conf py 09 _tutorial source control_flow rst Environment variables In 9 import os Eni Lie Cue BAE ESE TE FS os environ keys DIR PROGRAM VERSION MOTECALL PS SH EDITOR WORKON_HOME PYTHONPATH H r In 12 os environ PYTHONPATH QUE MAA Users cours cue ue ais Users cComEns she mit Ooms Users cburns local lib python2 5 site packages usr local lib python2 5 site packages Library Frameworks Python framework Versions 2 5 lib python2 5 In 16 os getenv PYTHONPATH ouelel Us eses couse scout lse Users couric sre mito cols Users cburns local lib python2 5 site packages usr local lib python2 5 site packages Library Frameworks Python framework Versions 2 5 lib python2 5 2 8 Standard Library 35 Python Scientific lecture notes Rele
39. gt gt gt x yY MP oO gra 0s 5 OS gt gt gt X y array CELON pL ea ESA AI cra CIO y 2 S 211 gt gt gt x shape y shape MS O gt gt gt distance np sqrt x xx 2 y xx 2 So np ogridis very useful as soon as we have to handle computations on a grid On the other hand np mgrid directly provides matrices full of indices for cases where we can t or don t want to benefit from broadcasting gt gt gt x y np mgrid 0 4 0 4 gt gt gt X arcay IO Op 10 ll dlls Mila y De 2y A Sy Sp Sp Sill gt gt gt y ara IO ty 2 Sil Op SA OF lies Sally QO y 24 311 However in practice this is rarely needed 3 2 5 Array shape manipulation Flattening 2 gt gt a no e 27 31 T4 5 oll gt gt gt a ravel cua 2 Se e Dp gt gt gt aid array 1 4 Ze Sly ESPACIAL gt gt gt a T ravel cuca Ml 4 2 D Sp 1 Higher dimensions last dimensions ravel out first Reshaping The inverse operation to flattening gt gt gt a shape ies gt gt gt b a ravel gt gt gt b reshape 2 3 arcay ES 4 5 Gil Creating an array with a different shape from another array gt gt gt a np arange 36 gt gt gt b a reshape 6 6 gt gt gt b 3 2 Numerical operations on arrays 64 Python Scientific lecture notes Release 2011 1 array J 00 Xe
40. gt gt gt y readonly Halise gt gt gt y shape Cre gt gt Wo SETLER 8 4 e Roundtrips work gt gt gt z np asarray y gt gt gt Z array Ill 21 3 4 gt gt gt x 0 0 9 gt gt gt Z array IS 217 32 211 e Interoperability with the built in Python array module gt gt gt import array AY ray Ya D22 2 of intl6 but no Numpy gt gt gt y np asarray x gt gt gt y array 112849 12849 dtype int16 gt gt gt y base lt memory at Oxa225acc gt 8 3 6 PEP 3118 details e Suppose you have a new Python extension type MyOb ject defined in C e and want it to interoperable with a 2 2 array of int32 See Also myobject c myobject_test py setup_myobject py static PyBufferProcs myobject_as_buffer getbufferproc myobject_getbuffer releasebufferproc myobject_releasebuffer y and stick this to the type object x static int myobject_getbuffer PyObject obj Py_buffer view int flags PyMyObjectObject self PyMyObjectObject obj Called when something requests that a MyObject type object provides a buffer interface view gt buf self gt buffer view gt readonly 0 view gt format i view gt shape malloc sizeof Py_ssize_t 2 view gt strides malloc sizeof Py_ssize_t 2 view gt suboffsets NULL TODO Just fill in view gt len view gt itemsize view gt str
41. int output_strides_n steps 7 int output_strides_p steps 8 int m dimensioonil i core dimensions are added arter int n dimension 2 the main dimension order as in intep dimensiono y esuigine tune ine i for a o ak amens ions O a matmul_for_strided_matrices input_1 input_2 output strides for each array input_1 steps 0 input_2 steps 1 8 2 Universal functions 177 Python Scientific lecture notes Release 2011 1 output steps 2 8 3 Interoperability features 8 3 1 Sharing multidimensional typed data Suppose you 1 Write a library than handles multidimensional binary data 2 Want to make it easy to manipulate the data with Numpy or whatever other library 3 but would not like to have Numpy as a dependency Currently 3 solutions 1 the old buffer interface 2 the array interface 3 the new buffer interface PEP 3118 8 3 2 The old buffer protocol e Only 1 D buffers e No data type information e C level interface PyBufferProcs tp_as_buffer in the type object e But it s integrated into Python e g strings support it Mini exercise using PIL Python Imaging Library See Also pilbuffer py gt gt gt import Image gt gt gt x np zeros 200 200 4 dtype np int8 e TODO RGBA images consist of 32 bit integers whose bytes are RR GG BB AA Fill x with opaque red 255 0 0 255 Mangle it to
42. 256 0 cure 25o 2560m 259s 260 gt gt gt y np array 256 dtype np int32 cn 1258 7 259 2607 2610p e Casting on setitem dtype of the array is not changed on item assignment IS gt gt gt y array 2 3 4 5 dtype int8 Note Exact rules see documentation http docs scipy org doc numpy reference ufuncs html casting rules Re interpretation viewing e Data block in memory 4 bytes 0x01 Il 0x02 Il 0x03 Il 0x04 4 of uint8 OR 4 of int8 OR 2 of int16 OR 1 of int32 OR 1 of float32 OR How to switch from one to another 1 Switch the dtype 8 1 Life of ndarray 161 Python Scientific lecture notes Release 2011 1 gt gt gt x np array 1 2 3 4 dtype np uint8 gt gt gt x dtype lt i2 gt gt gt x array 513 1027 dtype int16 gt gt gt 0x0201 0x0403 A E027 0x01 0x02 Il 0x03 0x04 Note little endian least significant byte is on the left in memory 2 Create a new view gt gt gt y x view lt i4 gt gt gt y array 67305985 gt gt gt 0x04030201 67305985 0x01 0x02 0x03 0x04 Note e view makes views does not copy or alter the memory block e only changes the dtype and adjusts array shape gt gt gt x 1 5 gt gt gt y array S281991 gt gt gt y base IS x
43. 5 peine st len Gist 6 TE name _ main_ 8 index_error 9 ipdb gt len lst 6 pan print Ist emi Gls i 2 ipdb gt quit E Se 9 3 Using the Python debugger 194 Python Scientific lecture notes Release 2011 1 In gt gt P lig In Un Ru gt gt P EL ami gani i ake Post mortem debugging without Python some situations you cannot use IPython for instance to debug a script that wants to be called from the command line In this case you can call the script with python m pdb script py python m pdb index_error py home varoquau dev scipy lecture notes advanced debugging_optimizing index_error pyf 1 lt module gt WUT Small snipe to false an imdexErerconm 4AN db continue aceback most recent call last e usr lib python2 6 pdb py line 1296 in main pdb _runscript mainpyfile e usr lab python2 6 pdb py line 1215 ian runseript self run statement e use lib python2 6 bdb py line 372 an run exec cmd in globals locals le lt string gt line 1 in lt module gt le index_error py line 8 in lt module gt index_error e index_error py line 5 in index_error print lst len lst dexError list index out of range caught exception Entering post mortem debugging naing cont or step will restart the program home varoquau dev scipy lecture notes advanced debugging_optimizing index_error pyA5 inde
44. amp and for logical operations not and and or y g p 3 2 Numerical operations on arrays 55 Python Scientific lecture notes Release 2011 1 Shape mismatches gt gt gt a array iin 27 Stn i gt gt gt a mos asen 2n Traceback most recent call last File es tdin gt dane i am lt meduile gt ValueError shape mismatch objects cannot be broadcast to a single shape Broadcast We ll return to that later 3 2 2 Basic linear algebra Matrix multiplication gt gt gt a np triu np ones 3 3 1 gt gt gt a array ko Ke Ta S S gt gt gt b nowdrag l 27 3 gt gt aco 1 see help np triu array il On 2a Sail Wan Wer stall Wop Woz Watt gt gt gt np dot a a cz LO 0 Li 07 Ore Ok Op 07 Wii Transpose gt gt gt a T array ilu le Olen ee OF top Wep Wo Ss il 0 Inverses and linear equation systems gt gt gt A atnb gt gt gt A array Ii is Ter Tel O Bar ksa On On 2 11 gt gt gt B np linalg inv A gt gt gt B dot A arcan I te Os Wall ORF Lo Mods O Or i S gt gt x np linalg solve Ar T 27 31 gt gt gt X A Wee ile i gt gt gt A dot x cua IL ila Leg SEIN Eigenvalues gt gt gt np linalg eigvals A arrcay ilo Bag a le and so on see help np linalg 3 2 Nume
45. code google com p pyamg e Pysparse own Sparse matrix classes matrix and eigenvalue problem solvers 10 11 4 Other Interesting Packages 224 Python Scientific lecture notes Release 2011 1 http pysparse sourceforge net 11 4 Other Interesting Packages 225 CHAPTER 12 Image manipulation and processing using Numpy and Scipy authors Emmanuelle Gouillart Ga l Varoquaux Image 2 D numerical array or 3 D CT MRI 2D time 4 D Here image Numpy array np array Tools used in this tutorial e numpy basic array manipulation scipy scipy ndimage submodule dedicated to image processing n dimensional images See http docs scipy org doc scipy reference tutorial ndimage html e a few examples use specialized toolkits working with np array Scikit Image scikit learn Common tasks in image processing e Input Output displaying images e Basic manipulations cropping flipping rotating e Image filtering denoising sharpening e Image segmentation labeling pixels corresponding to different objects e Classification More powerful and complete modules e OpenCV Python bindings e CellProfiler e ITK with Python bindings many more 226 Python Scientific lecture notes Release 2011 1 Chapters contents e Opening and writing to image files page 227 e Displaying images page 228 e Basic manipulations page 229 Stat
46. dbo a2 la lt type complex gt gt gt gt a 3 gt gt gt type a lt type int gt Type conversion gt gt gt float P O 2 2 Basic types 10 Python Scientific lecture notes Release 2011 1 2 2 2 Containers Python provides many efficient types of containers in which collections of objects can be stored Lists A list is an ordered collection of objects that may have different types For example SS le fil 2 3 45 Si gt gt gt type 1 lt type list gt e Indexing accessing individual objects contained in the list gt gt gt NZ 3 Counting from the end with negative indices cee E 5 ees EN 4 Warning Indexing starts at 0 as in C not at 1 as in Fortran or Matlab e Slicing obtaining sublists of regularly spaced elements gt gt gt 1 itir 27 27 S ON gt gt gt 1 2 4 Sp 4 Warning Note that 1 start stop contains the elements with indices i such as start lt i lt stop i ranging from start to stop 1 Therefore 1 start stop has stop start elements Slicing syntax 1 start stop stride All slicing parameters are optional A SEA 4 5 a gt RS Sn gt AZ SO Lists are mutable objects and can be modified gt gt gt 1 0 28 gt gt gt 1 ASS 4 Sl gt gt gt M24 P 8 gt gt gt 1 athe 27 3y S7 5 Note The elements of a list may have different types gt gt gt IN 2 A A gt gt
47. for j in range 2 N_max is_prime 2 j j False Skim through help np nonzero and print the prime numbers Follow up Move the above code into a script file named prime_sieve py Run it to check it works Convert the simple sieve to the sieve of Eratosthenes 1 Skip 3 which are already known to not be primes 2 The first number to cross out is j 3 1 The numpy array object 53 Python Scientific lecture notes Release 2011 1 Reminder Python scripts A very basic script file contains import numpy as np import matplotlib pyplot as plt lt if you use it any numpy using commands follow a np array 2 2 3 41 print a Run on command line python my seript 1I2 PY Or in Python shell wa LOS Sabia y Seane il Ay gt gt gt execfile 2_2_data_statistics py Exercise 1 1 Certain arrays Create the following arrays with correct data types errre errre ARP PH borre erie ee INTO O OUO ce GG G ee ey O O qO GD oy oO oe e a ee a ee Par on course 3 statements for each 53 amp 54 characters Exercise 1 2 Text data files Write a Python script that loads data from populations txt and drop the last column and the first 5 rows Save the smaller dataset to pop2 txt Exercise 1 3 Tiling Skim through the documentation for np t ile and use this function to construct the array 3 DO ANBA GI
48. from reservoir import Reservoir class ReservoirEvolution HasTraits reservoir Instance Reservoir name DelegatesTo reservoir inflows Array dtype np float64 shape None releass Array dtype np float64 shape None initial_stock Float stock Property depends_on inflows releases initial_stock month Property depends_on stock Traits View PHHIRIRARA RARA RA RARA RARA RARA RARA RARA RA RARA ARAR AAA RA DARA EEE traits_view View Item name Group 13 3 What are Traits 260 Python Scientific lecture notes Release 2011 1 ChacoPlotItem month stock show_label False width 500 resizable Tru Traits properties HHHRiIR RAMA AAA ARA RARA RARA RAM ARA RARA AAA dees de tee eet def _get_stock self won fixme should handle cases where we go over the max storage noe return self initial_stock self inflows self releases cumsum def _get_month self return np arange self stock size nig name main__ reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 initial_stock 10 TMLv des merry Su Eo a te lo Lo Wo O Se le Se 31 releases ts pcia Kar SO SS 2p il Sie Sil view ReservoirEvolution reservoir reservoir inflows inflows_ts releases releases_ts view configure_traits Pa
49. gt 1 San CIA gt gt gt 1111 1121 27 Shelioy 2 2 Basic types 11 Python Scientific lecture notes Release 2011 1 For collections of numerical data that all have the same type it is often more efficient to use the array type provided by the numpy module A NumPy array is a chunk of memory containing fixed sized items With NumPy arrays operations on elements can be faster because elements are regularly spaced in memory and more operations are perfomed through specialized C functions instead of Python loops Python offers a large panel of functions to modify lists or query them Here are a few examples for more details see http docs python org tutorial datastructures html more on lists Add and remove elements sso T fil 2 Ss gt gt gt l append 6 gt gt gt 1 ies een ae ce gt gt gt l pop 6 gt gt gt 1 eee ee oil gt gt gt l extend 6 gt gt gt 1 Pee ot Sos oe gt gt gt 1 1 2 gt gt gt l 71 4 5 Concatenate and repeat lists gt gt gt l Esr ie e e SS 2 E Sy Ey y 2 aly Sort r in place gt gt gt r sort gt gt gt r ES ON extend 1l in place Note Methods and Object Oriented Programming The notation r method r sort r append 3 1 pop is our first example of object oriented programming OOP Being a list the object r owns the method function that is called using the notation
50. gt gt 1 scipy lena SS AS OSO AZ AOS SO gt gt gt noisy 1 0 4x1 std np random random 1 shape A Gaussian filter smoothes the noise out and the edges as well gt gt gt gauss_denoised ndimage gaussian_filter noisy 2 Most local linear isotropic filters blur the image ndimage uniform_filter A median filter preserves better the edges gt gt gt med_denoised ndimage median_filter noisy 3 noisy Gaussian filter Median filter a y y F Median filter better result for straight boundaries low curvature gt gt gt im np zeros 20 20 gt gt gt mn Sl gt gt gt im ndimage distance_transform_bf im gt gt gt im noise im 0 2 np random randn im shape gt gt gt im_med ndimage median_filter im_noise 3 Other rank filter ndimage maximum_filter ndimage percentile_filter Other local non linear filters Wiener scipy signal wiener etc 12 4 Image filtering 232 Python Scientific lecture notes Release 2011 1 Median filter Error r i i Total variation TV denoising Find a new image so that the total variation of the image integral of the norm L1 of the gradient is minimized while being close to the measured image Non local filters gt gt gt from scikits image filter import tv_denoise gt gt gt from tv_denoise import tv_denoise gt gt gt tv_denoised tv_denoise noisy wei
51. gt gt a np arange 6 dtype np int8 reshape 3 2 gt gt gt b a T gt gt gt b strides lp 2 So far so good However gt gt gt ista andata FN x00 x01 x02 x03 x04 x057 gt gt gt b array I 2r Ally SEE pe nas gt gt gt c b reshape 3 2 gt gt gt Cc array 0 2 4 1 3 5 dtype int8 3 4 Under the hood 87 Python Scientific lecture notes Release 2011 1 Here there is no way to represent the array c given one stride and the block of memory for a Therefore the reshape operation needs to make a copy here 3 4 4 Summary e Numpy array block of memory indexing scheme data type description e Indexing strides byte_position np sum arr strides indices e Various tricks can you do by playing with the strides stuff for an advanced tutorial it is 3 4 Under the hood 88 CHAPTER 4 Getting help and finding documentation author Emmanuelle Gouillart Rather than knowing all functions in Numpy and Scipy it is important to find rapidly information throughout the documentation and the available help Here are some ways to get information e In Ipython help function opens the docstring of the function Only type the beginning of the func tion s name and use tab completion to display the matching functions In 204 help np v np vander np vdot np version np void0 np vstack np var np vectorize np void np ws plate In 204 help np vander
52. i tp i 0 Oly de 0 OF OF 10 OW Oli gt gt gt ndimage binary_erosion a cra LOT Or 0 107 107 O Olle OO or O O O0 Olly de OU O 1 0 Oly Os Up O o Op OF O15 da UW 0 AL 0 O Ol O 0 O 107 O Ol O O 0 07 107 0115 gt gt gt Erosion removes objects smaller than the structure area IO 0 0 0 0 0 01 OO O AO O O Olli OOO O O On Olly OF mn OF 0 0 10 O Ol den OU Of Of O Or Ol da WU 0 OF 0 O Oly de O OF OF 10 WO O11 Erosion Dilation Dilation maximum filter gt gt gt a np zeros 5 5 gt gt gt al2 2 2 gt gt gt a AO DE OS GC ny cea Opening Closing 12 4 Image filtering 234 Python Scientific lecture notes Release 2011 1 On Wn Wey O ORT Oe MOR pe een Ok O Can Way Oar Wap Ost OFA Os OE SOR 10a gt gt gt ndimage binary_dilation a astype a dtype array il Om on on omy ora Ok on le TOR Cm Of Wap a as gt Od Of Oe ee OR Oi Qa Way Oe Ox Wet Also works for grey valued images gt gt gt np random seed 2 gt gt gt x y 63 np random random 2 8 astype np int gt gt gt im x y np arange 8 SSS gt gt gt bigger_points ndimage grey_dilation im size 5 5 structure np ones 5 5 SSS gt gt gt square np zeros 16 16 gt gt gt square 4 4 4 4 1 gt gt gt dist ndimage distance_transform_bf square gt gt gt dilate_dist ndimage grey_dilation
53. lena 1x 4 1x 4 ly 4 ly 4 gt gt gt up lt gt down flip gt gt gt flip_ud_lena np flipud lena gt gt gt rotation gt gt gt rotate_lena ndimage rotate lena 45 gt gt gt rotate_lena_noreshape ndimage rotate lena 45 reshape False 12 4 Image filtering Local filters replace the value of pixels by a function of the values of neighboring pixels Neighbourhood square choose size disk or more complicated structuring element 19 1 12 maximal e a E 19 19 19 Ughbors 12 4 1 Blurring smoothing Gaussian filter from scipy ndimage gt gt gt lena scipy lena gt gt gt blurred_lena ndimage gaussian_filter lena sigma 3 gt gt gt very_blurred ndimage gaussian_filter lena sigma 5 Uniform filter gt gt gt local_mean ndimage uniform_filter lena size 11 A 12 4 Image filtering 231 Python Scientific lecture notes Release 2011 1 12 4 2 Sharpening Sharpen a blurred image gt gt gt lena scipy lena gt gt gt blurred_l ndimage gaussian_filter lena 3 increase the weight of edges by adding an approximation of the Laplacian gt gt gt filter_blurred_l ndimage gaussian_filter blurred_l 1 2 gt gt och phic 510 gt gt gt sharpened blurred_1 alpha blurred_1 filter blurred i 12 4 3 Denoising Noisy lena gt
54. numpy int32 gt with 6 stored elements in Compressed Sparse Row format gt gt gt gt mtx todense mara 0 0 ay Se l 3 611 11 2 Storage Schemes 215 Python Scientific lecture notes Release 2011 1 gt gt gt mtx data cra il 2 Sp te Bo Gil gt gt gt mtx indices eya LO 2 2p O Uy 21 gt gt gt mtx indptr array 0 25 de Bi e create using data indices indptr tuple gt gt gt data mp array 1l 2 37 gt gt gt indices np array 0 2 gt gt gt indptr np array 0 2 gt gt gt mtx sps csr_matrix data gt gt gt mtx todense mac cix i il O 2 LO Wy Sy a Se Sl vi 1 61 1 21 indices indptr shape 3 3 Compressed Sparse Column Format CSC e column oriented three NumPy arrays indices indptr data indices is array of row indices data is array of corresponding nonzero values x indptr points to column starts in indices and data x lengthisn_col 1 last item number of values length of both indices and data nonzero values of the i th column are data indptr i indptr i 1 with row indices indices indptr i indptr i 1 x Item i j can be accessed as data indptr 3 k where k is position of i in indices indptr j indptr j 1 subclass of _cs_matrix common CSR CSC functionality x subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector
55. py Write or copy and paste the following lines in a file called test py message Hello how ware you for word in message split print word Let us now execute the script interactively that is inside the Ipython interpreter This is maybe the most common use of scripts in scientific computing e in Ipython the syntax to execute a scriptis run script py For example In 1 trun test py Hello how are you In 2 message Out 2 Hello how are you The script has been executed Moreover the variables defined in the script such as message are now available inside the interpeter s namespace Other interpreters also offer the possibility to execute scripts e g execfile in the plain Python interpreter etc 2 6 Reusing code scripts and modules 25 Python Scientific lecture notes Release 2011 1 It is also possible In order to execute this script as a standalone program by executing the script inside a shell terminal Linux Mac console or cmd Windows console For example if we are in the same directory as the test py file we can execute this in a console epsilon sandbox python test py Hello how lare you Standalone scripts may also take command line arguments In file py import sys print sys argv python file py test arguments Mate py test i arguments Note Don t implement option parsing yourself Use modules such as optparse 2 6 2 Importing
56. w In 45 fp close Ta 46 unk txe In Osr Std os eu dia Out 46 True In 47 os remove junk txt In 48 7 junkie In os Mistari r os Curci Out 48 False os path path manipulations os path provides common operations on pathnames En 70 fo opem sia swe tn 71 fp crosen in 72 a os path abspath A JUNR CXE in 73 a out hisi Usens churns sEec scipy2000 scipyaZ 009 tutontall sounce Junk EAE In 74 os path split a CUINA I Users courcns src scrpy 200g serpy 2009 Storia sources Sins cis In 78 os path dirname a Out 78 Users cburns src scipy2009 scipy_2009 tutorial source In 79 os path basename a CUASI Us In 80 os path splitext os path basename a OME SON Junki ete sates In 84 cs path vexists qunk txt Out 84 True Ta 86 os path rstile Punt EXE Out 86 True mma vit osr patima dirr unk eE Out 87 False 2 8 Standard Library 34 Python Scientific lecture notes Release 2011 1 In 88 os path expanduser local Ours Users ecburns local in 92 cs path jorn Osepaeh expanduser local 7 bam Out 92 Users cburns local bin Running an external command a 8 os systems x conf py debug_file py demo2 py demo py demo pyc my_file py conf py demo2 py demo2 pyc demo py my_file py pi_wallis_image py
57. 2 6 Reusing code scripts and modules 30 Python Scientific lecture notes Release 2011 1 In 1 import scipy In 2 scipy file Cum usr Topy thonan S Gist packages lt cipy Imati pye In 3 import scipy version In 4 scipy version version Ome Pais 07 0 In 5 import scipy ndimage morphology In 6 from scipy ndimage import morphology In 17 morphology binary_dilation Type function Base Class lt type function gt String Form lt function binary_dilation at 0x9bedd84 gt Namespace Interactiv Tiles usr lib python2 6 dist packages scipy ndimage morphology py Definition morphology binary_dilation input structure None iterations 1 mask None output None border_value 0 origin 0 brute_force False DOCS Lang Multi dimensional binary dilation with the given structure An output array can optionally be provided The origin parameter controls the placement of the filter If no structuring element is provided an element is generated with a squared connectivity equal to one The dilation operation is repeated iterations times If iterations is less than 1 the dilation is repeated until the result does not change anymore If a mask is given only those elements with a true value at the corresponding mask element are modified at each iteration 2 6 7 Good practices Note Good practices Indentation no choice Indenting is compulsory in Python Every
58. 200 200 32 bit integer array so that PIL accepts it gt gt gt img Image frombuffer RGBA 200 200 data gt gt gt img save test png Q Check what happens if x is now modified and img saved again 8 3 3 The old buffer protocol import numpy as np import Image Let s make a sample image RGBA format 8 3 Interoperability features 178 Python Scientific lecture notes Release 2011 1 x np zeros 200 200 4 dtype np x PEO 254 red X 3 255 opaque data x view np int32 Check that img Mages r ronbu t Hers REC BANE 20 05 img save test png Modify the original data and save It turns out that PIL which knows happily shares the same data li 11 254 img save test2 png int8 you understand why this is OK 200 data again next to nothing about Numpy 8 3 4 Array interface protocol e Multidimensional buffers 8 3 Interoperability features 179 Python Scientific lecture notes Release 2011 1 e Data type information present e Numpy specific approach slowly deprecated but not going away e Not integrated in Python otherwise See Also Documentation http docs scipy org doc numpy reference arrays interface html Le A ar crac iil 21 18 20d gt gt gt X _ array_interface__ data 1716945527 Ealse ASS CAI A Aaa Teypestr s lt 14 strides N
59. 95238095 20166 66666667 42400 ip The sample standard deviations gt gt gt populations std axis 0 ddof 1 array Alls Seka ayy Goss 9990119057 3404 55577132 Which species has the highest population each year gt gt gt np argmax populations axis 1 ce 27 07 0 Lo i 2 Ap 27 25 20 ap Ue De We Lo 2 25 20 21 3 2 Numerical operations on arrays 59 Python Scientific lecture notes Release 2011 1 Example diffusion simulation using a random walk algorithm 1 2 1 2 DS 1 O 1 d t What is the typical distance from the origin of a random walker after t left or right jumps shuffled jumps Position cumulated jumps sum time time uf2faf2faf2 Ao 1fop110 lp gt gt gt n_stories 1000 number of walkers gt gt gt t_max 200 time during which we follow the walker We randomly choose all the steps 1 or 1 of the walk gt gt gt t np arange t_max gt gt gt steps 2 x np random random_integers 0 1 n stories t_max 1 gt gt gt np unique steps Verification all steps are 1 or 1 array 1 1 We build the walks by summing steps along the time gt gt gt positions np cumsum steps axis 1 axis 1 dimension of time gt gt gt sq distance positions x 2 We get the mean in the axis of the stories gt gt gt mean_sq distance np mean sq_distance axis 0 Plot the results gt gt
60. IS N be N ds N 4 DN us SEA 1 3 1 1 3 1 The numpy array object 54 Python Scientific lecture notes Release 2011 1 3 2 Numerical operations on arrays 3 2 1 Elementwise operations With scalars gt gt a Mp array O7 2 3 eee ll rs A gt gt gt 2xxa AY 4 a 41 All arithmetic operates elementwise gt gt gt b np ones 4 1 gt gt gt a b array Sis Oley oleae a e gt gt gt ax D ar Cavi Ang Der Sal gt gt gt j np arange 5 SA 4 arcay 2 S7 6 ba Ael Warning including multiplication gt gt gt c np ones 3 3 gt gt gt cx Cc arcay to Lay ey i deg ey hel tog top told NOT matrix multiplication Note Matrix multiplication gt gt gt C COE e ar ravi Sop Sop Salle lL Sore Vey weasel esi AS yal ely Comparisons gt fb O Ry A 22 Sip gt gt gt b np array 4 2 2 gt gt gt a 211 ea array False True False gt gt gt 4 gt b array False False True True dtype bool False dtype bool Logical operations gt gt fl paro il i Ol 0 dtype bool 0 dtype bool gt gt gt b np array 1 0 1 gt gt gt a b array True True True gt gt gt a amp b array True False False False dtype bo01 False dtype bo01 Note For arrays
61. In Python 2 7 and 3 x the list comprehension syntax was extended to dictionary and set comprehensions A set is created when the generator expression is enclosed in curly braces A dict is created when the generator expression contains pairs of the form key value gt gt gt i for i in range 3 ss O ii 22 22 gt axa for i Iin range 3 Oe 0 AS 2AT If you are stuck at some previous Python version the syntax is only a bit worse gt gt gt Set i for 1 in abc Bei ev et AEA gt gt gt dict a ord a form in abc es ASES ASE Shah Generator expression are fairly simple not much to say here Only one gotcha should be mentioned in old Pythons the index variable i would leak and in versions gt 3 this is fixed 7 1 3 Generators Generators A generator is a function that produces a sequence of results instead of a single value David Beazley A Curious Course on Coroutines and Concurrency A third way to create iterator objects is to call a generator function A generator is a function containing the keyword yield It must be noted that the mere presence of this keyword completely changes the nature of the function this yield statement doesn t have to be invoked or even reachable but causes the function to be marked as a generator When a normal function is called the instructions contained in the body start to be executed When a generator is called the execution stops before t
62. IrrigationArea def energy_production self release 1 7 Returns the energy production Wh for the given release m3 s det power 1000 x 9 81 self head release self efficiency return power 3600 traits_view View Item name Item max_storage Item max_release Item head Item efficiency Item irrigated_areas resizable Tru if name _ main_ upper_block IrrigationArea name Section C surface 2000 crop Wheat reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 irrigated_areas upper_block release 80 print Releasing m3 s produces kWh format release reservoir energy_production release Trait listeners can be used to listen to changes in the content of the list to e g keep track of the total crop surface on linked to a given reservoir from traits api import HasTraits Str Float Range Enum List Property from traitsui api import View Item class IrrigationArea HasTraits name Str surface Float desc Surface ha crop Enum Alfalfa Wheat Cotton class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 Max release Pilloat 10 dese Maxina lii release m3 s head Float 10 desc Hydraulic head m fficiency Range 0 1 irrigate
63. None traceback None which is equivalent to raise type value traceback at the point of the yield statement Unlike raise which immediately raises an exception from the current execution point throw first resumes the generator and only then raises the exception The word throw was picked because it is suggestive of putting the exception in another location and is associated with exceptions in other languages What happens when an exception is raised inside the generator It can be either raised explicitly or when executing some statements or it can be injected at the point of a yield statement by means of the throw method In either case such an exception propagates in the standard manner it can be intercepted by an except or finally clause or otherwise it causes the execution of the generator function to be aborted and propagates in the caller For completeness sake it s worth mentioning that generator iterators also have a close method which can be used to force a generator that would otherwise be able to provide more values to finish immediately It allows the generator ___de1___ method to destroy objects holding the state of generator Let s define a generator which just prints what is passed in through send and throw gt gt gt import itertools gt gt gt def g print start for i in itertools count prine yielding format q try ans yield i except GeneratorExit print e lo
64. Th 4 A A A AF A A A FA A 44 FF A A A Si 44 4 FAA A PA Aolo HA6a Scatter plots print x vs y diagrams We define x and y and make some point in y random x arange 10 y arange 10 NA a AE well S Now make a scatter plot scatter x y The three different values for y break out of the line BATES DO Ol Aea 5 8 Other Types of Plots 105 Python Scientific lecture notes Release 2011 1 Working with sparse matrices it is often of interest as how the matrix looks like in terms of sparsity We take an identity matrix as an example i identity 10 aL arcay iil 0 0 0 0 OF 0 0 0 Wil 0 I 07 0 0 Of OW 07 07 Ol wp 07 i 0 OF We OF Wi Wh el O O OW Lo We OF 07 0 0 Wil O OW WW O te 0 07 0 0 01 07 07 OW WO 0 Ip Op OW 07 Wil QO Oj 0 0 0 Of i GC 0 01 Ol O7 0 Wh Oe Wp We ik Wl ll OQ OF OW 0 O 10 107 Oi Lo Ol De O CU DO OW OF 0 0 07 ail Now we look at it more visually spy i POlO 16 8 Stem plots vertical lines at the given x location up to the specified y location Let s reuse x and y from our scatter see above stem x y loa AAN There is a function for creating date plots Let s define 10 dates starting at January 1st 2000 at 15 day intervals E tNolo 6a
65. True fill_value 1le 20 Note Streamlined and more seamless support for dealing with missing data in arrays is making its way into Numpy 1 7 Stay tuned 3 3 Moving on 81 Python Scientific lecture notes Release 2011 1 Example Masked statistics Canadian rangers were distracted when counting hares and lynxes in 1903 1910 and 1917 1918 and got the numbers are wrong Carrot farmers stayed alert though Compute the mean populations over time ignoring the invalid numbers gt gt gt data np loadtxt data populations txt gt gt gt populations np ma masked_array data 1 gt gt gt year datal 0 gt gt gt bad_years year gt 1903 amp year lt 1910 ee year gt 1917 amp year lt 1918 gt gt gt populations bad_years 0 np ma masked gt gt gt populations bad_years 1 np ma masked gt gt gt populations mean axis 0 masked_array data 40472 7272727 18627 2727273 42400 0 mask False False False fill_value 1e 20 gt gt gt populations std axis 0 masked_array data 21087 656489 15625 7998142 3322 50622558 mask False False False fill_value 1le 20 Note that Matplotlib knows about masked arrays gt gt gt Plt p ot year populations MOSA gt gt gt plt show 80000 70000 60000 50000 40000 A 30000 20000 10000 1900 1905 1910 1915 1920 3 3 5 Polynomials Numpy also
66. Zi array 1 3 dtype int16 See Also stride fakedims py Exercise array 1 2 3 4 dtype np int8 gt eiceey il 2 3 41 Ly 2 37 Al 1 2 3 411 dtype np int8 using only as_strided Hint byte_offset stride 0 index 0 stride 1 index 1 Spoiler Stride can also be 0 gt gt gt x np array 1 2 3 4 dtype np int8 gt gt gt y as_strided x strides 0 1 shape 3 4 gt gt gt y array MN 27 27 47 EA Sp e 1 2 3 411 dtype int8 gt gt gt y base base is x True 8 1 Life of ndarray 165 Python Scientific lecture notes Release 2011 1 Broadcasting e Doing something useful with it outer product of 1 2 3 4 and 5 6 7 gt gt gt x np array 1 2 3 4 dtype np int16 gt gt gt x2 as_strided x strides 0 1x2 shape 3 4 gt gt gt x2 array il 2e Sp ll Hi 2r Si ull ap 27 37 Sl adtype M6 22 gt y MP array 97 6 Mii adtype np into gt gt gt y2 as_strided y strides 1x2 0 shape 3 4 gt gt gt y2 arca TIS 5 O al l Ye Yo Te mio ciyos ime ka o gt o gt Oy fen gt gt gt x2 y array Ll IO 15 201 6 12 18 24 dp 14 21 2811 Cevss mc 16 Seems somehow familiar gt gt gt x np array 1 2 3 4 dtype np int16 gt gt gt y np array 5 6 7 dtype np int16 gt gt gt x n
67. a np zeros 5 5 dtype np int gt gt gt a 1 4 1 4 1 a 4 4 1 gt gt gt a array LIO 07 07 07 04 LO eas al al OZ 6 11 Image processing scipy ndimage 123 Python Scientific lecture notes Release 2011 1 gt gt gt Opening array 0 0 0 OF 0 r r r PRR O F 0 1 1 0 lo Olz ly Olg 07 LI removes small objects gt gt gt ndimage binary_opening a structure np ones 3 3 astype np int 0 0 01 ty Olz i Olly Le Oll 0 011 gt gt gt Opening can also smooth corners gt gt gt ndimage binary_opening a astype np int array 0 r 0 0 0 0 i r F SIS f 0 il iy I 0 0 01 0 01 Lp Olly 0 01 0 011 e Closing ndimage binary_closing Exercise Check that opening amounts to eroding then dilating An opening operation removes small structures while a closing operation fills small holes Such operation can therefore be used to clean an image gt gt gt a np zeros 50 gt a O OU 0 gt gt gt a 0 25xnp random standard_normal a shape gt gt gt mask a gt 0 gt gt gt opened_mask gt gt gt closed_mask 50 1 ndimage binary_opening mask ndimage binary_closing opened_mask mask opened_mask closed_mask nm Exercise Check that the area of the reconstructed square is smaller than the area of the in
68. be linear algebra computations In this case using the right function to solve the right problem is key For instance an eigenvalue problem with a symmetric matrix is easier to solve than with a general matrix Also most often you can avoid inverting a matrix and use a less costly and more numerically stable operation Know your computational linear algebra When in doubt explore scipy linalg and use St imeit to try out different alternatives on your data 10 3 Making code go faster 203 Python Scientific lecture notes Release 2011 1 10 4 Writing faster numerical code A complete discussion on advanced use of numpy is found in chapter Advanced Numpy page 156 or in the article The NumPy array a structure for efficient numerical computation by van der Walt et al Here we discuss only some commonly encountered tricks to make code faster e Vectorizing for loops Find tricks to avoid for loops using numpy arrays For this masks and indices arrays can be useful Broadcasting Use broadcasting page 166 to do operations on arrays as small as possible before combining them e In place operations In 1 a np zeros le7 In 2 Stimeit global a a Oxa 10 loops best of 3 111 ms per loop In 3 Stimeit global a a 0 10 loops best of 3 48 4 ms per loop note we need global a in the timeit so that it work as it is assigning to a and thus considers it as a local variable Be easy
69. can almost be exactly computed using the 2 other PCA finds the directions in which the data is not flat and it can reduce the dimensionality of the data by projecting on a subspace 16 4 Dimension Reduction with Principal Component Analysis 284 Python Scientific lecture notes Release 2011 1 gt gt gt from scikits learn import decomposition gt gt gt pca decomposition PCA n_components 2 gt gt gt pear Eit Gris data PCA copy True n_components 2 whiten False gt gt gt X pca transform iris data Now we can visualize the transformed iris dataset gt gt gt import pylab as pl gt gt gt pluscattrer i Oli Si ol ezis target d e 1 0 a o o Poo r ost Cog o See gt o So ae A Re e e o o et aes ete ey AA A A o 000a 0 5 2 o oi e e 1 0p e 1 5 j 2 0 4 3 2 1 0 1 2 3 4 5 PCA is not just useful for visualization of high dimensional datasets It can also be used as a preprocessing step to help speed up supervised methods that are not efficient with high dimensions 16 5 Putting it all together face recognition An example showcasing face recognition using Principal Component Analysis for dimension reduction and Sup port Vector Machines for classification 16 5 Putting it all together face recognition 285 Python Scientific lecture notes Release 2011 1 non Stripped down version of the face recognition ex
70. channels lt uZ Sample_rate lt u4 byte_rate lt u4 Mlcels clica EA flexible sized scalar type item size 4 little endian unsigned 32 bit integer 4 byte string more of the same Toits pers ano LU Mera aa Wisi a A a suo cite USE ater FUM vdata size p anne the sound data itself cannot be represented her 8 1 Life of ndarray 159 Python Scientific lecture notes Release 2011 1 it does not have a fixed siz 1 See Also wavreader py gt gt gt wav_header_dtypel format dtype S4 gt gt gt wav_header_dtype fields lt dictproxy object at 0x85e9704 gt gt gt gt wav_header_dtype fields format Apenas e The first element is the sub dtype in the structured data corresponding to the name format e The second one is its offset in bytes from the beginning of the item Note Mini exercise make a sparse dtype by using offsets and only some of the fields gt gt gt wav_header_dtype np dtype dict names format sample_rate data_id offsets offset_1 offset_2 offset_3 counted from start of structure in bytes formats list of dtypes for each of the fields and use that to read the sample rate and dat a_id as sub array gt gt gt f open test wav r gt gt gt wav_header np fromfile f dtype wav_header_
71. color g 3 using the set_something methods line set_marker o Lines have several properties as shown in the following table data_clipping label Property Value alpha alpha transparency on 0 1 scale antialiased True or False use antialised rendering color matplotlib color arg whether to use numeric to clip data string optionally used for legend linestyle one of linewidth float the line width in points marker oneof o svx gt lt ete markeredgewidth line width around the marker symbol markeredgecolor edge color if a marker is used markerfacecolor face color if a marker is used markersize size of the marker in points Symbol Description O gt lt TD5OU 5sU0NRQUOX lt CUVAS steps solid line dashed line dash dot line dotted line points pixels circle symbols triangle up symbols triangle down symbols triangle left symbols triangle right symbols square symbols plus symbols cross symbols diamond symbols thin diamond symbols tripod down symbols tripod up symbols tripod left symbols tripod right symbols hexagon symbols rotated hexagon symbols pentagon symbols vertical line symbols horizontal line symbols use gnuplot style steps kwarg only There are many line styles that can be specified with symbols Colors can be given in many ways one letter abbreviations gray scale intensity from 0 to 1 RGB in hex and tuple for
72. detailed explanation of the above issues can be found in David M Beazley s article Types and Objects in Python 2 4 Control Flow Controls the order in which the code is executed 2 4 1 if elif else In 1 if 2xx2 4 print Obvious Obvious Blocks are delimited by indentation Type the following lines in your Python interpreter and be careful to respect the indentation depth The Ipython shell automatically increases the indentation depth after a column sign to decrease the indentation depth go four spaces to the left with the Backspace key Press the Enter key twice to leave the logical block in 2 a 10 In 3 if a print 1 elif a print 2 else prine A lot 2 4 Control Flow 16 Python Scientific lecture notes Release 2011 1 A lot Indentation is compulsory in scripts as well As an exercise re type the previous lines with the same indentation in a script condition py and execute the script with run condition py in Ipython 2 4 2 for range Iterating with an index In 4 for i in range 4 print i CNS ES But most often it is more readable to iterate over values In 5 for word in cool powemtull readable o rine Pyehonm as 25 s word P Pyishon is cool Python is powerful Python is readable 2 4 3 while break continue Typical C style while loop Mandelbrot problem
73. dimensional Poisson problem with Dirichlet boundary conditions noe import scipy from scipy sparse linalg import lobpcg from pyamg import smoothed_aggregation_solver from pyamg gallery import poisson N 100 K 9 A poisson N N format csr create the AMG hierarchy ml smoothed_aggregation_solver A initial approximation to the K eigenvectors X scipy rand A shape 0 K preconditioner based on ml M ml aspreconditioner comput igenvalues and eigenvectors with LOBPCG W V lobpcg A X M M tol le 8 largest False plot the eigenvectors import pylab pylab figure figsize 9 9 for i in range K pylabrsubplot 3 3y E1 pylab title Eigenvector td i pylab pcolor V i reshape N N pylab axis equal Probar iiO pylab show 11 3 Linear System Solvers 223 Python Scientific lecture notes Release 2011 1 examples pyamg_with_lobpcg py e example by Nils Wagner examples lobpcg_sakurai py e output python examples lobpcg_sakurai py Results by LOBPCG for n 2500 0 06250083 0 06250028 0 06250007 Exact eigenvalues 0 06250005 0 0625002 0 06250044 Elapsed time 7 01 Eigenvalue distribution 10 107 10 10 10 10 lt 10 10 10 10 10 10 107 000000 10 10 10 10 10 Number i 11 4 Other Interesting Packages e PyAMG algebraic multigrid solvers http
74. e line_profiler http packages python org line_profiler e pyflakes http pypi python org pypi pyflakes e gdb for the C debugging part Chapters contents e Avoiding bugs page 190 Coding best practices to avoid getting in trouble page 190 pyflakes fast static analysis page 191 Running pyflakes on the current edited file page 191 x A type as go spell checker like integration page 192 e Debugging workflow page 192 e Using the Python debugger page 193 Invoking the debugger page 193 Postmortem page 193 Step by step execution page 195 Other ways of starting a debugger page 196 Debugger commands and interaction page 197 e Debugging segmentation faults using gdb page 197 9 1 Avoiding bugs 9 1 1 Coding best practices to avoid getting in trouble 190 Python Scientific lecture notes Release 2011 1 Brian Kernighan Everyone knows that debugging is twice as hard as writing a program in the first place So if you re as clever as you can be when you write it how will you ever debug it e We all write buggy code Accept it Deal with it e Write your code with testing and debugging in mind e Keep It Simple Stupid KISS What is the simplest thing that could possibly work e Don t Repeat Yourself DRY Every piece of knowledge must have a single unambiguous authoritative representation within a system Constants algorithms etc
75. gt a b erase O a o lil za le POA Oil 27217 30 Sip Sel We have already used broadcasting without knowing it gt gt gt a np ones 4 5 gt gt gt al0 2 we assign an array of dimension 0 to an array of dimension 1 arcrcay Ill Zon Zap Pe PAIT Z Pere A aller melee ee se dan ee aS i as i 1 il hel Se Py Os Broadcasting seems a bit magical but it is actually quite natural to use it when we want to solve a problem whose output data is an array with more dimensions than input data Example Let s construct an array of distances in miles between cities of Route 66 Chicago Springfield Saint Louis Tulsa Oklahoma City Amarillo Santa Fe Albuquerque Flagstaff and Los Angeles gt gt gt mileposts np array 0 UIs SUS Wee Si Divs AS ISA Ai 1913 2448 gt gt gt distance_array np abs mileposts mileposts np newaxis gt gt gt distance_array array O AS S05 15907 yal Lia way sd ALS 224 os 07 lb SS Gis Sev Wadi ist IAS A250 30387 LUST OF 4837 S687 Si a ata ed 2 AS 367 52S 2337 OO ss 9 Wa SOs A E EZ E Sil Gr 563 los 0 304 604 075 M Sa WS Oli BR 89 silat O S00 869 WSs UAVS TEA Si Ae Mies SS 0 500 0 G3 Wes Sl 1544 1346 12417 808 6737 369 69 OF soo Lo ToS A A Ow sisi TA SO O SO AA O Iie SS ise Oa aS5 011 3 2 Numerical operations on arrays 62 Python Scientific lecture notes Releas
76. gt gt gt mtx sps csc_matrix data indices indptr shape 3 gt gt gt mtx todense mara AO a MOROS L S 6 r 1 Block Compressed Row Format BSR e basically a CSR with dense sub matrices of fixed shape instead of scalar items block size R C must evenly divide the shape of the matrix M N three NumPy arrays indices indptr data indices is array of column indices for each block 3 data is array of corresponding nonzero values of shape nnz R C subclass of _cs_matrix common CSR CSC functionality x subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector products and other arithmetics sparsetools e constructor accepts dense matrix array sparse matrix 11 2 Storage Schemes 217 Python Scientific lecture notes Release 2011 1 shape tuple create empty matrix data ij tuple data indices indptr tuple e many arithmetic operations considerably more efficient than CSR for sparse matrices with dense sub matrices use like CSR vector valued finite element discretizations Examples e create empty BSR matrix with 1 1 block size like CSR gt gt gt mtx sps bsr_matrix 3 4 dtype np int8 gt gt gt mtx lt 3x4 sparse matrix of type lt type numpy int8 gt with 0 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt gt mt
77. gt gt gt s 0 gt gt gt for i in xrange 5 gt gt gt for j in xrange 5 gt gt gt Se se Sl apap ai ae by striding and using sum on the result gt gt gt y as_strided x shape 5 5 strides TODO TODO gt gt eye gt gt gt assert s s2 Solution gt gt gt y as_strided x shape 5 5 strides 5 5 5 5 x itemsize 5x5 1 x itemsize gt gt gt s2 y sum CPU cache effects Memory layout can affect performance In 1 np zeros 20000 In 2 y np zeros 20000x67 67 In 3 x shape y shape AAA rn 200007 In 4 Stimeit x sum 100000 loops best of 3 0 180 ms per loop In 5 Stimeit y sum 100000 loops best of 3 2 34 ms per loop 8 1 Life of ndarray 167 Python Scientific lecture notes Release 2011 1 In 6 x strides y strides 8 536 Smaller strides are faster X y Mm TtTtitt mT cache block size e CPU pulls data from main memory to its cache in blocks e If many array items consecutively operated on fit in a single block small stride fewer transfers needed gt faster See Also numexpr is designed to mitigate cache effects in array computing Example inplace operations caveat emptor e Sometimes gt gt gt a b is not the same as gt gt gt a COPY eso mn mnp array lt 2 e 411 gt gt gt x x transpose
78. gt gt gt y array 1 2 3 4 dtype int8 gt gt gt y data lt read only buffer for 0xa588ba8 size 4 offset 0 at 0xa55cd60 gt gt gt gt y base IS x True 3 4 Under the hood 85 Python Scientific lecture notes Release 2011 1 gt gt gt y flags C_CONTIGUOUS True F_CONTIGUOUS True OWNDATA False WRITEABLE False ALIGNED True UPDATEIFCOPY False The owndata and writeable flags indicate status of the memory block 3 4 3 Indexing scheme strides The question Zee oS o cual 2 Sil 4 5 6l 7 8 91 dtype np int8 gt gt gt SCL G data x01 x02 x03 x04 x05 x06 x07 x08 x09 At which byte in x data does the item x 1 2 begin The answer in Numpy e strides the number of bytes to jump to find the next element e stride per dimension gt gt gt x strides Ga gt gt gt byte_offset 3x1 1x2 He On METI Cle S2 gt gt gt x data byte_offset ON S10 gt gt gt 1 2 6 e simple flexible C and Fortran order gt gt gt xX mo eme 27 31 Ta Sip il 7 8 9 dtype np int16 order C gt gt gt x strides Gp 2 gt gt gt Ser x data r x01 x00 x02 x00 x03 x00 x04 x00 x05 x00 x06 x00 x07 x00 x08 x00 t x00 e Need to jump 6 bytes to find the next row e Need to jump 2 bytes to find the next column gt gt gt y np array x order F gt gt gt
79. gt gt waveform_1 np load data waveform_1 npy and visualize it gt gt gt import matplotlib pyplot as plt gt gt gt t np arange len waveform_1 gt gt gt plt plot t waveform_1 gt gt gt plt show 1 Mallet C and Bretar F Full Waveform Topographic Lidar State of the Art ISPRS Journal of Photogrammetry and Remote Sensing 64 1 pp 1 16 January 2009 http dx doi org 10 1016 j isprsjprs 2008 09 007 The data used for this tutorial are part of the demonstration data available for the FullAnalyze software and were kindly provided by the GIS DRAIX 6 12 Summary exercises on scientific computing 130 Python Scientific lecture notes Release 2011 1 30 25 20 15 Intensity bins 10 0 10 20 30 40 50 60 70 80 Time ns As you can notice this waveform is a 80 bin length signal with a single peak Fitting a waveform with a simple Gaussian model The signal is very simple and can be modeled as a single Gaussian function and an offset corresponding to the background noise To fit the signal with the function we must e define the model e propose an initial solution e call scipy optimize leastsq Model A Gaussian function defined by 2 B Aexp o can be defined in python by gt gt gt def model t coeffs return coeffs 0 coeffs 1 np exp t coeffs 2 coeffs 3 x 2 where e coeffs 0 is B noise e coef
80. gt regr score diabetes_X_test diabetes_y_test 07 5910835453 being the score very similar to linear regression Least Squares gt gt gt lin linear_model LinearRegression gt gt gt Tin fit diabetes Xx train diabetes y train eee ee eee on Pit intercept ey normalize False overwrite_X False gt gt gt lin score diabetes_X_test diabetes_y_test 0 5850753022 16 6 Linear model from regression to sparsity 287 Python Scientific lecture notes Release 2011 1 Different algorithms for a same problem Different algorithms can be used to solve the same mathematical problem For instance the Lasso object in the scikits learn solves the lasso regression using a coordinate descent method that is efficient on large datasets However the scikits learn also provides the LassoLARS object using the LARS which is very efficient for problems in which the weight vector estimated is very sparse that is problems with very few observations 16 7 Model selection choosing estimators and their parameters 16 7 1 Grid search and cross validated estimators Grid search The scikits learn provides an object that given data computes the score during the fit of an estimator on a param eter grid and chooses the parameters to maximize the cross validation score This object takes an estimator during the construction and exposes an estimator API gt gt gt from scikits learn imp
81. has Figure as title Figures are numbered starting from 1 as opposed to the normal Python way starting from 0 This is clearly MATLAB style There are several parameters that determine how the figure looks like Argument Default Description num 1 number of figure figsize figure figsize figure size in in inches width height dpi figure dpi resolution in dots per inch facecolor figure facecolor color of the drawing background edgecolor figure edgecolor color of edge around the drawing background frameon True draw figure frame or not The defaults can be specified in the resource file and will be used most of the time Only the number of the figure is frequently changed When you work with the GUI you can close a figure by clicking on the x in the upper right corner But you can close a figure programmatically by calling close Depending on the argument it closes 1 the current figure no argument 2 a specific figure figure number or figure instance as argument or 3 all figures all as argument As with other objects you can set figure properties also setp or with the set__something methods With subplot you can arrange plots in regular grid You need to specify the number of rows and columns and the number of the plot A plot with two rows and one column is created with subplot 211 and subplot 212 The result looks like this AE subplot 211 subplot 212
82. implemented but accepted for Python 3 3 Let s say we are writing a generator and we want to yield a number of values generated by a second generator a subgenerator If yielding of values is the only concern this can be performed without much difficulty using a loop such as subgen some_other_generator for v in subgen yield v However if the subgenerator is to interact properly with the caller in the case of calls to send throw and close things become considerably more difficult The yield statement has to be guarded by a try except finally structure similar to the one defined in the previous section to debug the generator function Such code is provided in PEP 380 here it suffices to say that new syntax to properly yield from a subgenerator is being introduced in Python 3 3 yield from some_other_generator This behaves like the explicit loop above repeatedly yielding values from some_other_generator until it is exhausted but also forwards send throw and close to the subgenerator 7 2 Decorators Summary This amazing feature appeared in the language almost apologetically and with concern that it might not be that useful Bruce Eckel An Introduction to Python Decorators Since a function or a class are objects they can be passed around Since they are mutable objects they can be modified The act of altering a function or class object after it has been constructed but before is
83. in x data does the item x 1 2 begin The answer in Numpy e strides the number of bytes to jump to find the next element e 1 stride per dimension gt gt gt x strides ere gt gt gt byte_offset 3x1 1x2 7 to find 1 27 gt gt gt x data byte_offset I Nex O16 gt 22 6 e simple flexible 8 1 Life of ndarray 163 Python Scientific lecture notes Release 2011 1 C and Fortran order 22 gt a an il 2 Sle 7 8 9 dtype np int16 order C gt gt gt x strides Op 2 gt gt gt str x data x01 x00 x02 x00 x03 x00 x04 x00 x05 x00 x06 x00 x07 x00 x08 x00 t x00 e Need to jump 6 bytes to find the next row e Need to jump 2 bytes to find the next column gt gt gt y np array x order F gt gt gt y strides 2 6 gt gt gt SUr y data x01 x00 x04 x00 x07 x00 x02 x00 x05 x00 x08 x00 x03 x00 x06 x00 t x00 e Need to jump 2 bytes to find the next row e Need to jump 6 bytes to find the next column e Similarly to higher dimensions C last dimensions vary fastest smaller strides F first dimensions vary fastest shape dj d2 dn strides 1 82 Sn j dj 1dj 2 dn x itemsize s d d2 dj 1 x itemsize Note Now we can understand the behavior of view gt gt gt y np array 1 3 2 4 dtype np uint8 transpose gt gt gt x y copy Transposition does not affect t
84. interpolate import UnivariateSpline gt gt gt quantile_func UnivariateSpline cprob sorted_max_speeds The quantile function is now going to be evaluated from the full range of probabilities gt gt gt nprob np linspace 0 1 le2 gt gt gt fitted_max_speeds quantile_func nprob 2 In the current model the maximum wind speed occurring every 50 years is defined as the upper 2 quantile As a result the cumulative probability value will be ii E O il 0 OZ So the storm wind speed occurring every 50 years can be guessed by gt gt gt fifty_wind quantile_func fifty_prob gt gt gt fifty_wind rro T 32797989825 The results are now gathered on a Matplotlib figure Cumulative probability Veg 32 98 m s 0 05 22 24 26 28 30 32 34 Annual wind speed maxima m s Figure 6 1 Solution Python source file 6 12 Summary exercises on scientific computing 127 Python Scientific lecture notes Release 2011 1 Exercise with the Gumbell distribution The interested readers are now invited to make an exercise by using the wind speeds measured over 21 years The measurement period is around 90 minutes the original period was around 10 minutes but the file size has been reduced for making the exercise setup easier The data are stored in numpy format inside the file examples sprog windspeeds npy Do not look at the source code for the plots until you have completed the
85. is invalidated Again Traits uses magic method names for the property e _get_XXX for the getter of the XXX Property trait e _set_XXX for the setter of the XXX Property trait from traits api import HasTraits Instance DelegatesTo Float Range from traits api import Property from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s won reservoir Instance Reservoir max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Property depends_on inflows release control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release Private traits eH Fete tees eset este AMARA ARA ARA AAA thes ed tse e eet _storage Float Traits property implementation ttt tt ttt tt th tt ttt tt tt tt ttt EE def _get_storage self new_storage self _storage self releas self inflows return min new_storage self max_storage def _set_storage self storage_value self _storage storage_value def _get_spillage self new_storage self _storage self releas sel
86. is bound to its name is called decorating There are two things hiding behind the name decorator one is the function which does the work of decorating i e performs the real work and the other one is the expression adhering to the decorator syntax i e an at symbol and the name of the decorating function Function can be decorated by using the decorator syntax for functions decorator def function 0 pass e A function is defined in the standard way O e An expression starting with placed before the function definition is the decorator The part after must be a simple expression usually this is just the name of a function or class This part is evaluated first and after the function defined below is ready the decorator is called with the newly defined function object as the single argument The value returned by the decorator is attached to the original name of the function 7 2 Decorators 145 Python Scientific lecture notes Release 2011 1 Decorators can be applied to functions and to classes For classes the semantics are identical the original class definition is used as an argument to call the decorator and whatever is returned is assigned under the original name Before the decorator syntax was implemented PEP 318 it was possible to achieve the same effect by assigning the function or class object to a temporary variable and then invoking the decorator explicitly and then assi
87. lasso alpha 0 0075421928471338063 These estimators are called similarly to their counterparts with CV appended to their name 16 7 Model selection choosing estimators and their parameters 288 Python Scientific lecture notes Release 2011 1 Exercise On the diabetes dataset find the optimal regularization parameter alpha 16 7 Model selection choosing estimators and their parameters 289 Index D diff 274 277 differentiation 274 dsolve 277 E equations algebraic 275 differential 277 integration 275 M Matrix 276 P Python Enhancement Proposals PEP 255 143 PEP 3118 178 PEP 3129 153 PEP 318 146 153 PEP 342 143 PEP 343 153 PEP 380 145 PEP 380 id13 145 PEP 8 148 S solve 275 290
88. lecture notes Release 2011 1 gt gt gt el ndimage generate_binary_structure 2 1 gt gt gt el array False True False True rue True False True False dtype bool gt gt gt el astype np int AO i 017 i il abl gt i Wil Erosion gt gt gt a np zeros 7 7 dtype np int gt gt gt aig o gt gt gt a aera MIO 0 OF O O Oj Ol Oe O o Lo Lo Of 01 OO On Do i i 0 Oil On Or AO LON Or 0p 1 ip 1 0r Oi O O O 0 0 9 O gt gt gt ndimage binary_erosion a astype a dtype anray lO 07 0 07 07 0r Olly On Op Oy 0 OF Oe Ol 07 O Lo O Ge Ol 0 O I Of 0 Ol On Of UD Lo O Ge Ol On 07 0r 07 0 07 Ol e O O O O OA gt gt gt Erosion removes objects smaller than the structure gt gt gt ndimage binary_erosion a structure np ones 5 5 astype a dtype array 110 Or 07 O O Of Ol O 0 0 0 0 0 Ol De Da 0 0 Ol O O On 0 0r 07 0 OO On De O 0 O OF Ol 0 07 0 0 O Ol Or O 0 0 O 9 OI Dilation gt gt gt a np zeros 5 5 gt gt gt aia 2l ol gt gt gt a cre I One Ose Der Wa Well On Oee Of Why Wisi Ope Oy eel Oe NORA Os Ws Go Os Well Os Ws Ose Ws Welt gt gt gt ndimage binary_dilation a astype a dtype array UL Os De Ose O Oslo One One Teo OSA Ong ean Wa a y One Ws a Os On Wap OWsp Oep Oop Wall e Opening gt gt gt
89. maxima 1802382379983002 SN OA IMSS S STOL Oe Si 20 1426 2 4961181804217221 6 7167361922608864 1 8023823799830032 TEO 2 LoS ici SOS AOS Sa iia 2161 gt gt gt ndimage find_objects labels 4 slice 30 48 None slice 30 48 None gt gt gt sl ndimage find_ objects labels 4 gt gt gt imshow sig s1 0 sig mask labels See the summary exercise on Image processing application counting bubbles and unmolten grains page 134 for a more advanced example 6 11 Image processing scipy ndimage 125 Python Scientific lecture notes Release 2011 1 6 12 Summary exercises on scientific computing The summary exercises use mainly Numpy Scipy and Matplotlib They first aim at providing real life examples on scientific computing with Python Once the groundwork is introduced the interested user is invited to try some exercises 6 12 1 Maximum wind speed prediction at the Sprog station The exercise goal is to predict the maximum wind speed occurring every 50 years even if no measure exists for such a period The available data are only measured over 21 years at the Sprogg meteorological station located in Denmark First the statistical steps will be given and then illustrated with functions from the scipy interpolate module At the end the interested readers are invited to compute results from raw data and in a slightly different approach Statistical approach The annual maxima are supposed to fit
90. non zero values e efficient for constructing sparse matrices incrementally e constructor accepts dense matrix array sparse matrix shape tuple create empty matrix e efficient O 1 access to individual elements e flexible slicing changing sparsity structure is efficient e can be efficiently converted to a coo_matrix once constructed e slow arithmetics for loops with dict iteritems e use when sparsity pattern is not known apriori or changes 11 2 Storage Schemes 212 Python Scientific lecture notes Release 2011 1 Examples e create a DOK matrix element by element gt gt gt mtx sps dok_matrix 5 5 dtype np float64 gt gt gt mtx lt 5x5 sparse matrix of type lt type numpy float64 gt with 0 stored elements in Dictionary Of Keys format gt gt gt gt for ir in range 5 gt gt gt for ic in range gt gt gt mex ere iel ION ir IS ale gt gt gt mtx lt 5x5 sparse matrix of type lt type numpy float64 gt with 25 stored elements in Dictionary Of Keys format gt gt gt gt mtx todense marcs 0n 7 il 0 og Log il 1 Oe PRORR gt POrRPRRER O RRRP z bere Ot OU e something is wrong let s start over gt gt gt mtx sps dok_matrix 5 5 dtype np float64 gt gt gt for ir in range 5 gt gt gt for ic in range 5 gt gt gt 2E Ge ae mts ta iel 10 gt gt gt mtx
91. nprarray iil 217 Bree Sao gt gt gt linalg det arr 200 gt gt gt arr np array 3 21 ae 6 411 gt gt gt linalg det arr 0 0 gt gt gt linalg det np ones 3 4 Traceback most recent call last ValueError expected square matrix The inv function computes the inverse of a square matrix gt gt gt ace Np array I 217 ee 3 411 gt gt gt larr linalg inv arr gt gt gt larr cuca lo y e lo Li Usain gt gt gt np allclose np dot arr iarr np eye 2 True Note that in case you use the matrix type the inverse is computed when requesting the I attribute gt gt gt ma np matrix arr copy False gt gt gt np allclose ma I iarr True Finally computing the inverse of a singular matrix its determinant is zero will raise LinAlgError gt gt gt arr Ne crea I 217 wees ae 21 gt gt gt linalg inv arr Traceback most recent call last sinAlgError singular matrix More advanced operations are available like singular value decomposition SVD gt gt gt arr np arange 12 reshape 3 4 1 gt gt gt uarr spec vharr linalg svd arr 6 6 Linear algebra operations scipy linalg 114 Python Scientific lecture notes Release 2011 1 The resulting array spectrum is gt gt gt spec array 2 54368356e 01 1 72261225e 00 Bs TAOS sis 191 For the recomposition
92. number of plots each of them shows both the source code and the resulting plot This is very useful for learning by example More standard documentation is also available amp matplotlib home search examples gallery docs modules index Click on any image to see full size image and source code m l a risa 2 Go Enter search terms or a module class or function name NN D Fi HERN FRB amp matplotlib some other wail oe Te l e Mayavi s website http code enthought com projects mayavi docs development html mayavi also has a very nice gallery of examples http code enthought com projects mayavi docs development html mayavi auto examples html in which one can browse for different visualization solutions 90 Python Scientific lecture notes Release 2011 1 Example gallery Mayavi v3 3 1 documentation previous next index Mlab functions gallery These are the examples of the mlab plotting functions They are copied out here for convenience Please refer to the corresponding section of the user guide for more information 3D Plotting functions for numpy arrays plot3d points3d imshow surf contour_surf mesh Next topic Boy example barchart triangular_mesh contour3d Finally two more technical possibilities are useful as well e In Ipython the magical function Spsearch search for objects matching patterns Th
93. optimize code for such effects Use compiled code The last resort once you are sure that all the high level optimizations have been explored is to transfer the hot spots i e the few lines or functions in which most of the time is spent to compiled code For compiled code the preferred option is to use Cython it is easy to transform exiting Python code in compiled code and with a good use of the numpy support yields efficient code on numpy arrays for instance by unrolling loops Warning For all the above profile and time your choices Don t base your optimization on theoretical considerations 10 4 Writing faster numerical code 205 CHAPTER 11 Sparse Matrices in SciPy author Robert Cimrman 11 1 Introduction dense matrix is e mathematical object e data structure for storing a 2D array of values important features e memory allocated once for all items usually a contiguous chunk think NumPy ndarray e fast access to individual items 11 1 1 Why Sparse Matrices e the memory that grows like nx 2 e small example double precision matrix gt gt gt import numpy as np gt gt gt import matplotlib pyplot as plt gt gt gt x np linspace 0 le6 10 gt gt gt plt plot x 8 0 x xx x2 le6 lw 5 gt gt gt plita x lapels Ize in gt gt gt plt ylabel memory MB gt gt gt plt show 11 1 2 Sparse Matrices vs Sparse Matrix Storage Schemes e
94. plt imread tiny_elephant png interpolation nearest gt gt gt plt show Numpy s own format gt gt gt np save pop npy data gt gt gt data3 np load pop npy Well known amp more obscure file formats HDF5 h5py PyTables e NetCDF scipy io netcdf_file netcdf4 python e Matlab scipy io loadmat scipy io savemat e MatrixMarket scipy io mmread scipy io mmread If somebody uses it there s probably also a Python library for it 3 1 9 Summary amp Exercises e Creating arrays array linspace arange zeros ones rand e Data types integers floats complex floats and strings e Simple plotting with Matplotlib plot x y e Indexing slicing and assignment into arrays slicing creates views e Reading data from files loadt xt savetxt et al 3 1 The numpy array object 52 Python Scientific lecture notes Release 2011 1 Worked example Prime number sieve 5 01234567 8 910111213 3 EECA ARRAIOA E 4 KEA EXELL VN Compute prime numbers in 0 99 with a sieve Construct a shape 100 boolean array is_prime filled with True in the beginning gt gt gt is_prime np ones 100 dtype bool Cross out 0 and which are not primes gt gt gt is_prime 2 0 For each integer 3 starting from 2 cross out its higher multiples gt gt gt N max int np sqrt len is_prime gt gt gt
95. products and other arithmetics sparsetools e constructor accepts dense matrix array sparse matrix data ij tuple shape tuple create empty matrix data indices indptr tuple e efficient column slicing column oriented operations e slow row slicing expensive changes to the sparsity structure use actual computations most linear solvers support this format 11 2 Storage Schemes 216 Python Scientific lecture notes Release 2011 1 Examples e create empty CSC matrix gt gt gt mtx sps csc_matrix 3 4 dtype np int8 gt gt gt mtx todense mario WO 0 01 IO O 0 0 0 0 0 0 dtype int8 create using data 13 tuple zo ey cra O 10 wp 2p 2 All gt gt COl mo carey 0 2 2 5 Oy ly 21 gt gt gt data Mp array 27 sip 5 Gill gt gt gt mtx sps csc_matrix data row col shape 3 3 gt gt gt mtx lt 3x3 sparse matrix of type lt type numpy int32 gt ll with 6 stored elements in Compressed Sparse Column format gt gt gt gt mtx todense mara O 2 IO O 8 r Sr 6l gt gt gt mez data y ESG gt gt gt mtx indices cre 0 2 27 De dy 21 gt gt gt mtx indptr array 10 2 3 6 id e create using data indices indptr tuple gt gt gt data np array 1 4 7 5 2 3 gt gt gt indices nprarcrcay IO 2 2 0 le 2 SS gt alcoi mena 10 2 SS Sl
96. range 1 s 2 2 granulo ndimage binary_opening data structure disk_structure n sum for n in sizes return granulo LES gt gt gt np random seed 1 gt gt gt n 10 gt gt gt 1 256 gt gt gt im a aio Mal D gt gt gt points l np random random 2 nxx2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im ndimage gaussian_filter im sigma 1 4 x n gt gt gt gt mask im gt im mean gt gt gt gt gt gt granulo granulometry mask sizes np arange 2 19 4 12 6 Measuring objects properties 242 Python Scientific lecture notes Release 2011 1 4 6 8 10 12 14 16 18 12 6 Measuring objects properties 243 CHAPTER 13 Traits author Didrik Pinte The Traits project allows you to simply add validation initialization delegation notification and a graphical user interface to Python object attributes In this tutorial we will explore the Traits toolset and learn how to dramatically reduce the amount of boilerplate code you write do rapid GUI application development and understand the ideas which underly other parts of the Enthought Tool Suite Traits and the Enthought Tool Suite are open source projects licensed under a BSD style license Intended Audience Intermediate to advanced Python programmers Requirements e Python 2 6 or 2 7 www python org e Either wxPython http www wxp
97. resize an array references or is referenced by another array in this way Use the resize function Some examples of real world use cases Case 2 a Calling legacy Fortran code Shape preserving functions with elementwise non Python routines For instance Fortran 2 a fortran _ module f90 subroutine some_function n a b integer n double precision dimension n intent in a double precision dimension n intent out b b ar I end subroutine some_function f2py c m fortran_module 2_a_fortran_module f90 import numpy as np import fortran_module def some_function input non Call a Fortran routine and preserve input shape nu input np asarray input fortran_module some_function takes 1 D arrays output fortran_module some_function input ravel return output reshape input shape print some_function np array 1 2 3 print Someseunculon npmdrsay THT 21r bs 411 gt 2 Si 44 2 Seay 4 Sel i 3 2 Numerical operations on arrays 66 Python Scientific lecture notes Release 2011 1 Case 2 b Block matrices and vectors and tensors Vector space quantum level amp spin T a Eo o E aj Y a G gt p bi we way In short for block matrices and vectors it can be useful to preserve the block structure In Numpy gt gt gt psi np zeros 2 2 dimensions level spin gt gt gt psi 0 1 lt psi_ 1 downarrow Linear o
98. set_ylim 0 5 O73 draw and get this AE A POlO 6 We can draw box and whisker plots boxplot arange 2 10 arange 1 5 We want to have the whiskers well within the plot and therefore increase the y axis ax gca ax set_ylim 0 12 draw Our plot looks like this 5 8 Other Types of Plots 102 Python Scientific lecture notes Release 2011 1 The range of the whiskers can be determined with the argument whis which defaults to 1 5 The range of the whiskers is between the most extreme data point within whis 75 25 of the data We can also do contour plots We define arrays for the x and y coordinates and also a 2D array for z Now we can make a simple contour plot We can also fill the area We just use numbers form 0 to 9 for the values v Now our plot area is filled 5 8 Other Types of Plots 103 Python Scientific lecture notes Release 2011 1 We can make histograms Let s get some normally distributed random numbers from numpy import numpy as N r numbers N random normal size 1000 Now we make a simple histogram hist r_numbers With 100 numbers our figure looks pretty good AGE tHolo 18 an Plots with logarithmic scales are easy loglog arange 1000 We set the mayor and minor gri
99. sites im im inputs hay m p output shape m p m n n p gt m p e This is called the signature of the generalized ufunc e The dimensions on which the g ufunc acts are core dimensions Status in Numpy e g ufuncs are in Numpy already new ones can be created with PyUFunc_FromFuncAndDataAndSignature e but we don t ship with public g ufuncs except for testing ATM gt gt gt import numpy core umath_tests as ut gt gt gt ut matrix_multiply signature k m n Ui n p 2 m p gt gt gt x np ones 10 2 4 gt gt gt y np ones 10 4 5 gt gt gt ut matrix_multiply x y shape LO 2 3 e the last two dimensions became core dimensions and are modified as per the signature e otherwise the g ufunc operates elementwise e matrix multiplication this way could be useful for operating on many small matrices at once Generalized ufunc loop Matrix multiplication m n n p gt m p void gufunc_loop void xargs int dimensions int steps void data char xinput_1 char args 0 these are as previously x char input_2 charx args 1 char output char args 2 int input_1_stride_m steps 3 strides for the core dimensions x int input_1_stride_n steps 4 are added after the non core int input 2 strides n steps 5 x steps x 7 8 int input_2_strides_p steps 6
100. sparse matrix is a matrix which is almost empty e storing all the zeros is wasteful gt store only nonzero items e think compression e pros huge memory savings e cons depends on actual storage scheme usually does not hold 206 Python Scientific lecture notes Release 2011 1 11 1 3 Typical Applications solution of partial differential equations PDEs the finite element method mechanical engineering electrotechnics physics e graph theory nonzero at i j means that node i is connected to node j 11 1 4 Prerequisites recent versions of numpy e scipy e matplotlib optional e ipython the enhancements come handy 11 1 5 Sparsity Structure Visualization e spy from matplotlib e example plots 11 1 Introduction 207 Python Scientific lecture notes Release 2011 1 11 2 Storage Schemes seven sparse matrix types in scipy sparse 1 csc_matrix Compressed Sparse Column format 2 csr_matrix Compressed Sparse Row format 3 bsr_matrix Block Sparse Row format 4 lil_ matrix List of Lists format 5 dok_matrix Dictionary of Keys format 6 coo_matrix COOrdinate format aka IJV triplet format 7 dia_matrix DIAgonal format each suitable for some tasks many employ sparsetools C module by Nathan Bell assume the following is imported gt gt gt import numpy as np gt gt gt import scipy sparse as sps gt gt gt import matplotlib pyplot as p
101. spillage irrigation 13 3 What are Traits A trait is a type definition that can be used for normal Python object attributes giving the attributes some additional characteristics e Standardization Initialization Validation Deferral e Notification e Visualization e Documentation A class can freely mix trait based attributes with normal Python attributes or can opt to allow the use of only a fixed or open set of trait attributes within the class Trait attributes defined by a class are automatically inherited by any subclass derived from the class The common way of creating a traits class is by extending from the HasTraits base class and defining class traits from traits api import HasTraits Str Float class Reservoir HasTraits name Str max_storage Float 13 3 What are Traits 246 Python Scientific lecture notes Release 2011 1 Warning For Traits 3 x users If using Traits 3 x you need to adapt the namespace of the traits packages e traits api should be enthought traits api e traitsui api should be enthought traits ul api Using a traits class like that is as simple as any other Python class Note that the trait value are passed using keyword arguments x servoir Reservoir name Lac de Vouglans max_storage 605 13 3 1 Initialisation All the traits do have a default value that initialise the variables For example the basic python types do have the followi
102. the screen Im 10 draw We can also add several lines to one plot In 1 x arange 100 In 2 linear arange 100 In 3 square v x v for v in arange 0 10 0 1 In 4 lines plot x linear x square Let s add a legend In 5 legend linear square Out 5 lt matplotlib legend Legend instance at 0x01BBC170 gt This does not look particularly nice We would rather like to have it at the left So we clean the old graph im Lepe elit and print it anew providing new line styles a green dotted line with crosses for the linear and a red dashed line with circles for the square graph ines ie anes solo ele ania eget Square ESO Now we add the legend at the upper left corner In 8 1 legend linear square loc upper left The result looks like this KANEC e a a 1 Plot a simple graph of a sinus function in the range 0 to 3 with a step size of 0 01 2 Make the line red Add diamond shaped markers with size of 5 3 Add a legend and a grid to the plot 5 4 Properties So far we have used properties for the lines There are three possibilities to set them 5 4 Properties 95 Python Scientific lecture notes Release 2011 1 1 as keyword arguments at creation time plot x 2 with the function setp setp line linear g x Square r 0
103. there s nothing to return raises the StopIteration exception An iterator object allows to loop just once It holds the state position of a single iteration or from the other side each loop over a sequence requires a single iterator object This means that we can iterate over the same sequence more than once concurrently Separating the iteration logic from the sequence allows us to have more than one way of iteration Calling the __iter___ method on a container to create an iterator object is the most straightforward way to get hold of an iterator The iter function does that for us saving a few keystrokes gt gt gt nums Ll 27 31 note that varies these are different objects gt gt gt iter nums lt listiterator object at gt gt gt gt nums iter_ SISNSIEALISSIECIEOIE ClOWSCt BNE 44 4 gt gt gt gt nums __reversed__ lt listreverseiterator object at gt gt gt gt it iter nums gt gt gt next it next obj simply calls obj next il gt gt gt Tt omext H gt gt gt next it 3 gt gt gt next it Traceback most recente cally ilast Fale lt stdine Jane i am lt moduile gt StopIteration When used in a loop StopIteration is swallowed and causes the loop to finish But with explicit invocation we can see that once the iterator is exhausted accessing it raises an exception Using the for in loop also uses th
104. tmp advnumpy ex zip In this section numpy will be imported as follows gt gt gt import numpy as np 156 Python Scientific lecture notes Release 2011 1 e Life of ndarray page 157 It s page 157 Block of memory page 158 Data types page 159 Indexing scheme strides page 163 Findings in dissection page 169 e Universal functions page 169 What they are page 169 Exercise building an ufunc from scratch page 170 Solution building an ufunc from scratch page 173 Generalized ufuncs page 176 e Interoperability features page 178 Sharing multidimensional typed data page 178 The old buffer protocol page 178 The old buffer protocol page 178 Array interface protocol page 179 The new buffer protocol PEP 3118 page 180 PEP 3118 details page 181 e Siblings chararray maskedarray matrix page 185 e Summary page 186 e Contributing to Numpy Scipy page 186 Why page 186 Reporting bugs page 187 Contributing to documentation page 188 Contributing features page 188 How to help in general page 189 8 1 Life of ndarray 8 1 1 It s ndarray block of memory indexing scheme data type descriptor e raw data e how to locate an element e how to interpret an element ndarray typedef struct PyArrayObject PyObject_HEAD 8 1 Life of ndarray 157 Python Scien
105. types If you don t know you need special data types then you probably don t Comparison on using float 32 instead of float 64 e Half the size in memory and on disk e Half the memory bandwidth required may be a bit faster in some operations In 1 a np zeros le6 dtype np float64 In 2 b np zeros le6 dtype np float32 In 3 Stimeit axa 1000 loops best of 3 1 78 ms per loop In 4 Stimeit bxb 1000 Loops best of 3 1 07 ms per loop 3 3 2 Structured data types Composite data types sensor_code 4 character string position float value float gt gt gt samples np zeros 6 dtype sensor_code S4 gt gt gt samples ndim position r float ovallues e But bigger rounding errors sometimes in surprising places i e don t use them unless you really need them Float ih 3 3 Moving on 76 Python Scientific lecture notes Release 2011 1 i gt gt gt samples shape 6 gt gt gt samples dtype names sensor_code position valuen gt gt gt Samples TACEREA l7 OSO BETA 1 OTN ATAU OSO CATENAS ES OS MP e 2 ro AA a a gt gt gt samples array I CALEA T EOE Ossie QC BETAS p i CADERA LD 0 35 ALFA 2 dtype sensor_code S4 OF OR CAO Or Ol Si O TA l2 07397 faasaina lt lt a Mavens See Field access works by indexing with field
106. wise operations manipulate data array directly fast NumPy machinery constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data offsets tuple no slicing no individual item access e use rather specialized solving PDEs by finite differences with an iterative solver 11 2 Storage Schemes 209 Python Scientific lecture notes Release 2011 1 Examples e create some DIA matrices gt gt gt data gt gt gt data array Ciri 1 1 gt gt gt offse gt gt gt mtx gt gt gt mtx np array 1 pn 37 4 r 2n Spo ES A BS l l 11 lt 4x4 sparse matrix of with 9 stored gt gt MEX E matrix gt gt gt data gt gt gt data array gt gt gt mtx odense 1 Of Sy Ll 25 0 O 25 Sip 0 0 3 np arange 12 reshape 3 4 i ES Sy Ur 71 OF Op sps dia_matrix data gt gt gt mtx data array gt gt gt mtx o a 0 gt gt gt print 0 0 du dE Cr 57 alt Cr Si 07 ES gt gt gt miei matriz NN TS GS Ir 27 3 Br 6 7 Q WO TAL ffsets F i 2il mtx il 2 8 4 5 6 7 ibal T2 odense ile Oval Din iy OFF 6 OF 0 Ol 41 01 a I a ral III type elements 4 8 2 Y 2 np array 0 sps dia_matrix data
107. write int a 3 In addition the type of a variable may change in the sense that at one point in time it can be equal to a value of a certain type and a second point in time it can be equal to a value of a different type b was first equal to an integer but it became equal to a string when it was assigned the value hello Operations on integers b 2 xa are coded natively in Python and so are some operations on strings such as additions and multiplications which amount respectively to concatenation and repetition 2 2 Basic types 2 2 1 Numerical types Integer variables gt gt gt 1 1 2 gt gt gt a 4 floats 2 1 First steps 9 Python Scientific lecture notes Release 2011 1 See Aull complex a native type in Python gt gt gt oy eat Ok Say gt gt gt a real eS gt gt gt a imag OS and booleans gt gt gt 3 gt 4 False gt gt gt test 3 gt 4 gt gt gt Lest False gt gt gt type test SEVIOS aa A Python shell can therefore replace your pocket calculator with the basic arithmetic operations x modulo natively implemented Soo Dd 20 gt gt gt 2xx10 1024 gt gt gt 8 3 2 Warning Integer division a al Trick use floats SA S e Scalar types int float complex bool gt gt gt type 1 lt type avian gt gt gt gt type 1 lt type float gt cee ss
108. y 2 eps wo y wo 2 y 0 with wo 2 k m being k the spring constant m the mass and eps c 2 m wo with c the damping coefficient For a computing example the parameters will be gt gt gt mass 0 5 kg gt gt gt kspring 4 N m gt gt gt cviscous 0 4 N s m so the system will be underdamped because gt gt gt eps cviscous 2 mass np sqrt kspring mass 2 Es s 1 true For the odeint solver the 2nd order equation needs to be transformed in a system of two first order equations for the vector Y y y It will be convenient to define nu 2 eps wo c mandom wo 2 k m gt gt gt nu_coef cviscous mass ll gt gt gt om_coef kspring mass Thus the function will calculate the velocity and acceleration by gt gt gt def calc_deri yvec time nuc omc return yvec 1 nuc x yvec 1 omc x yvec 0 gt gt gt time_vec np linspace 0 10 100 gt gt gt yarr odeint calc_deri 1 0 time_vec args nu_coef om_coef The final position and velocity are shown on the following Matplotlib figure 6 7 Numerical integration scipy integrate 116 Python Scientific lecture notes Release 2011 1 0 2 4 6 8 10 There is no Partial Differential Equations PDE solver in scipy Some PDE packages are written in Python such as fipy or SfePy 6 8 Fast Fourier transforms scipy fftpack The fftpack module allows to com
109. 011 1 gt gt gt radiusil radius2 radius3 radius4 lo 14 15 14 gt gt gt circlel x center1l 0 xx2 y centerl 1 2 lt radiuslx x2 gt gt gt circle2 x center2 0 2 y center2 1 2 lt radius2xx2 gt gt gt circle3 x center3 0 2 y center3 1 2 lt radius3x x 2 gt gt gt circle4 x center4 0 2 y center4 1 2 lt radius4 2 gt gt gt gt gt gt 4 circles gt gt gt img circlel circle2 circle3 circle4 gt gt gt mask img astype bool gt gt gt img img astype float gt gt gt gt gt img 1 0 2x np random randn img shape gt gt gt Convert the image into a graph with the value of the gradient on gt gt gt the gt gt gt edges gt gt gt graph image img_to_graph img mask mask gt gt gt gt gt gt Take a decreasing function of the gradient we take it weakly gt gt gt dependant from the gradient the segmentation is close to a voronoi gt gt gt graph data np exp graph data graph data std gt gt gt gt labels spectral_clustering graph k 4 mode arpack gt gt gt label_im np ones mask shape gt gt gt label_im mask labels 12 6 Measuring objects properties ndimage measurements Synthetic data gt gt gt n 10 gt gt gt 1 256 gt gt gt im np zeros 1 1 gt gt gt points lxnp random random 2
110. 1 gt gt gt J_ max np argmax a gt gt gt j Min np argmin a 3 2 Numerical operations on arrays 69 Python Scientific lecture notes Release 2011 1 gt gt gt 0 Jj max j_min 2 3 2 8 Summary amp Exercises Arithmetic etc are elementwise operations Basic linear algebra dot Reductions sum axis 1 std all any Broadcasting a np arange 4 a np newaxis a np newaxis Shape manipulation a ravel a reshape 2 2 Fancy indexing ala gt 3 a 2 3 Sorting data sort np sort np argsort np argmax Worked example Framing Lena Let s do some manipulations on numpy arrays by starting with the famous image of Lena http www cs cmu edu chuck lennapg scipy provides a 2D array of this image with the scipy lena function gt gt gt import scipy gt gt gt lena scipy lena Here are a few images we will be able to obtain with our manipulations use different colormaps crop the image change some parts of the image Let s use the imshow function of pylab to display the image In 3 import pylab as plt In 4 lena scipy lena In 5 plt imshow lena Lena is then displayed in false colors A colormap must be specified for her to be displayed in grey In 6 plt imshow lena plt cm gray In 7 7 or Tn 7 elt ag raya Create an array of the image with a narrower centering fo
111. 2 57 Tea aL Il gt gt gt l oprana CLG i ll Zl gt gt gt j np array 2 1 3 3 gt gt gt i array On DF lo 2110 gt gt gt j aa As Ill 37 ID gt gt gt alla l array Pap Sl Aa Ae 3 2 Numerical operations on arrays 68 Python Scientific lecture notes Release 2011 1 gt gt gt a 0 1 2 3 4 1 2 3 4 5 array 1 12 23 34 45 gt gt gt abE3 0 2 5H array 30 32 35 40 42 45 50 52 551 gt gt gt mask array 1 0 1 0 0 1 dtype bool gt gt gt a mask 2 array 2 22 52 We can even use fancy indexing and broadcasting at the same time gt gt gt a np arange 12 reshape 3 4 gt gt gt a Array O 1 27 31 A Sme Gr a Be 10 E cee a mpar cay IG ai il 2a gt gt gt ali 2 same as ali 2x np ones 2 2 dtype int 27 Ml 6 10 array 3 2 7 Sorting data Sorting along an axis Sel AS Sle il 2 11 1 gt gt gt b np sort a axis 1 gt gt gt b array l3 27 S1 Note Sorts each row separately In place sort gt gt gt a sort axis 1 gt gt gt a array Ss Wp Silly EA 20 Sorting with fancy indexing gt gt gt a np array 4 3 1 21 gt gt gt j np argsort a cua 1 Sr e 01 gt gt gt alj array l1 27 37 41 Finding minima and maxima gt gt gt a np array 4 3 1 2
112. 2 sp se ewes os eee SymPy is also able to solve boolean equations that is to decide if a certain boolean expression is satisfiable or not For this we use the function satisfiable Sisa ts tia ote Sy QUISAS True y Truel This tells us that x amp y is True whenever x and y are both True If an expression cannot be true i e no values of its arguments can make the expression True it will return False In 14 satisfiable x amp x Out 14 False 15 4 1 Exercises 1 Solve the system of equations x y 2 2 y 0 2 Are there boolean values x y that make x y amp y x true 15 5 Linear Algebra 15 5 1 Matrices Matrices are created as instances from the Matrix class gt gt gt from sympy import Matrix cea Matri x il Oily 0 i 1 1 0 Ore unlike a NumPy array you can also put Symbols in it x y 1 gt gt gt gt gt gt gt gt gt gt gt gt Ly ly Symbol Symbol Matrix x y 1 rx Pek XK ll gt gt gt Axx2 15 5 Linear Algebra 276 Python Scientific lecture notes Release 2011 1 an TN 2xx PI ay 15 5 2 Differential Equations SymPy is capable of solving some Ordinary Differential Equations sympy ode dsolve works like this AAA Cut 4 4 To Slk dsolve E x L ALEE K r E ae E Owe Si eS C2 cos Gs Keyword arguments can be given to this function in ord
113. 23 expand x y 3 Out 23 3 xx y 2 3 Y xx 2 x 3 y 3 Further options can be given in form on keywords In 28 expand x y complex True Out 28 Ixim x Ixim y re x re y In 30 expand cos x y trig True Out SOS coses cos y Ela Saa cya 15 2 2 Simplify Use simplify if you would like to transform an expression into a simpler form In 19 simplify x xx y x Oe SS al ar 15 2 Algebraic manipulations 273 Python Scientific lecture notes Release 2011 1 Simplification is a somewhat vague term and more precises alternatives to simplify exists powsimp simplifica tion of exponents trigsimp for trigonometric expressions logcombine radsimp together 15 2 3 Exercises 1 Calculate the expanded form of x y 2 Simplify the trigonometric expression sin x cos x 15 3 Calculus 15 3 1 Limits Limits are easy to use in SymPy they follow the syntax limit function variable point so to compute the limit of f x as x gt 0 you would issue limit f x 0 gt gt gt limit sin x x x 0 i you can also calculate the limit at infinity gt gt gt limit x x 00 oo gt gt gt limit x x 00 0 A O il 15 3 2 Differentiation You can differentiate any SymPy expression using diff func var Examples gt gt gt ditt sim x X Cosi gt gt gt ida ft sam 2x X 2 CO
114. 8 ax xaxis set_major_locator major_locator lm Po Axa Sens SOC Men O locator minor locaton In 10 ax xaxis set_major_formatter major_formatter Im LO draw After we redraw the figure our x axis should look like this es 00 2 00 4 00 6 00 8 00 5 6 Ticks 99 Python Scientific lecture notes Release 2011 1 1 Plot a graph with dates for one year with daily values at the x axis using the built in module datetime 2 Format the dates in such a way that only the first day of the month is shown 3 Display the dates with and without the year Show the month as number and as first three letters of the month name 5 7 Figures Subplots and Axes So far we have used implicit figure and axes creation This is handy for fast plots We can have more control over the display using figure subplot and axes explicitly A figure in matplot1lib means the whole window in the user interface Within this figure there can be subplots While subplot positions the plots in aregular grid axes allows free placement within the figure Both can be useful depending on your intention We ve already work with figures and subplots without explicitly calling them When we call plot matplotlib calls gca to get the current axes and gca in turn calls gcf to get the current figure If there is none it calls figure to make one strictly speaking to make a subplot 111 Let s look at the details A figure is the windows in the GUI that
115. 9 Similarly we can calculate the percentile 90 gt gt gt stats scoreatpercentile a 90 MAS OSITO IZ The percentile is an estimator of the CDF cumulative distribution function 6 5 3 Statistical tests A statistical test is a decision indicator For instance if we have 2 sets of observations that we assume are gener ated from Gaussian processes we can use a T test to decide whether the two sets of observations are significantly different 6 5 Statistics and random numbers scipy stats 113 Python Scientific lecture notes Release 2011 1 gt gt gt a np random normal 0 1 size 100 gt gt gt b np random normal 1 1 size 10 gt gt gt Stats Ctest Indla B 2 389876434401887 0 018586471712806949 The resulting output is composed of e The T statistic value it is a number the sign of which is proportional to the difference between the two random processes and the magnitude is related to the significance of this difference e the p value the probability of both process being identical If it is close to 1 the two process are almost certainly identical The closer it is to zero the more likely it is that the processes have different mean 6 6 Linear algebra operations scipy linalg First the linalg module provides standard linear algebra operations The det function computes the determinant of a square matrix gt gt gt from scipy import linalg gt gt gt aur
116. ES Be SHE BAS Be ed ow amp Ale 75 34 Under the hood s pes ee aoe a ee ee Ae ea ae ee Eee ok 85 Getting help and finding documentation 89 Matplotlib 93 Sil Introduction so ior ea e 2dn5 a5 Sepa dy he ESR ages oe eR PE ee Ee RE 93 35 2 IPython and the pylab mode lt lt 2654 240he 4 084245054004 bob ae eee eas 93 J SIMP POS 2 05 6 AR aK YE eee ee ee E Ae Ses EO Se ete Oe 93 S4 Properties mined A ERE SE EOE AS EME ESSE Ge eR ASE od amp EAS 95 33 gt E A SS Ee a 97 D310 VICKS pec io e di ee ib bh a eee be be 98 5 7 Figures Subplots and Axes 2 06 5 20ce a eB Se oe ee ed ee a E 100 5 8 Other Types Ot Plots secsec e Ga ae Oe ee a e ae ee Be a ea 101 39 Tbe Class Labra caia aha god Bee AA a arg Ea ea ke Pe ek eS 107 Scipy high level scientific computing 109 I 10 11 12 13 6 1 Scipy builds upon Numpy e sies e ea o oh a He SE ae Se Be ke a a 6 2 Fileimput outputi scipy 20 1 04 066646 eea OE we ee ea oe BS 6 3 Signal processing scipy signal s sses cepai a ee ek ee e a 64 Specialtunchonsscipy special vs usan Ma ere mad oe eee ee oe Oe 6 5 Statistics and random numbers scipy Stats sasea cara ma oo Eee e 6 6 Linear algebra operations SCiPyY LIDSUDO si aose eao a ee a a EE es 6 7 Numerical integration sSC IPY INTegrate e 6 8 F ast Fournier transforms scipy fftpack om its a a es 69 Interpolation scipy interpolater ni 6 oa e dra e a ORS 6 10 Optimization and fit secipy
117. It seeks to answer a problem of actual interest for numerical computing but it does not work Can you debug it Python source code to_debug py 9 4 Debugging segmentation faults using gdb 199 CHAPTER 10 Optimizing code author Ga l Varoquaux Donald Knuth Premature optimization is the root of all evil This chapter deals with strategies to make Python code go faster Prerequisites e line_profiler http packages python org line_profiler Chapters contents e Optimization workflow page 200 e Profiling Python code page 201 Timeit page 201 Profiler page 201 Line profiler page 202 e Making code go faster page 203 Algorithmic optimization page 203 x Example of the SVD page 203 e Writing faster numerical code page 204 10 1 Optimization workflow 1 Make it work write the code in a simple legible ways 2 Make it work reliably write automated test cases make really sure that your algorithm is right and that if you break it the tests will capture the breakage 3 Optimize the code by profiling simple use cases to find the bottlenecks and speeding up these bottleneck finding a better algorithm or implementation Keep in mind that a trade off should be found between profiling on a realistic example and the simplicity and speed of execution of the code For efficient work it is best to work with profiling runs lasting around 10s 200
118. Principal Component Analysis page 284 e Putting it all together face recognition page 285 e Linear model from regression to sparsity page 287 Sparse models page 287 e Model selection choosing estimators and their parameters page 288 Grid search and cross validated estimators page 288 First we will load some data to play with The data we will use is a very simple flower database known as the Iris dataset We have 150 observations of the iris flower specifying some measurements sepal length sepal width petal length and petal width together with its subtype Iris setosa Iris versicolor Iris virginica To load the dataset into a Python object gt gt gt from scikits learn import datasets gt gt gt iris datasets load_iris This data is stored in the data member whichis a n_samples n_features array gt gt gt iris data shape CESO TAY The class of each observation is stored in the target attribute of the dataset This is an integer 1D array of length n_samples gt gt gt iris target shape BIO gt gt gt import numpy as np 16 1 Loading an example dataset 279 Python Scientific lecture notes Release 2011 1 gt gt gt np unique iris target arean LO y 21 An example of reshaping data the digits dataset N On BWN O TA rrr 0123456 7 The digits dataset consists of 1797 images where each one is an 8x8 pixel ima
119. Python Scientific lecture notes Release 2011 1 EuroScipy tutorial team Editors Valentin Haenel Emmanuelle Gouillart Ga l Varoquaux http scipy lectures github com April 22 2012 2012 0 1 g042a4f5 Contents Getting started with Python for science 2 Scientific computing with tools and workflow 3 LU Why Python sace 65446 a RA OER Re Oe Ree He A 3 12 scientiic Python buildineiblocks 2 5 4 p eds oa h be oY RES Wee we PSS Od bo vad 5 1 3 The interactive workflow Python andatexteditor 2 00 6 The Python language 8 2A CPWStStepSin nk eine a Shea hal a eee a de a bat hoe 9 22 BASIC TYPES oes e a AA a Rs Rae a OS BR a ae Se 9 23 JASSionme nt Operator pros Bde a Pea ee Pe ee ee Re ea ek hee be aed 15 24 Control FOW bid oe oes 2h a eA eed Ai SS He ee RSS BS 16 25 Defining TUNCHONS se sa a es YER SO He eR A Re Be 20 2 6 Reusing code scripts and modules segir coe bh BA sd Aw 25 29 putand Output 545 84 4 4554 eee ae Oe ER Pa oem dee ep wee EO 32 2 8 Standard Library 5 6 ee ee ea ee eee ee ewe ee 33 29 Exception handling in Python 22 02 esk ope macia ee ee Oe 37 2 10 Object oriented programming OOP e esio erede bee we eee ee ee ee 39 NumPy creating and manipulating numerical data 41 3 1 hemumpy array object lt s c cat cent eeee bbe ae Dee SSeS se ch be Seuhi ase 42 3 2 Numerical Operations OM MTAYS os e e EE ew 6a Re ee A eee He EOS 55 3 3 MOVING OM santane 2 oe Eee wee eM eS CAS
120. S 2 x gt gt gt diff tan x x 1 tan x 2 You can check that it is correct by gt gt gt limit tan x y tan x y y 0 iL Ge an E a Higher derivatives can be calculated using the diff func var n method gt gt gt darr Sans x 1 2 COS 2 x gt gt sis xX 2 4xsin 2 x gt gt gt dirt sim 2xx x 3 8 Cos Zixx 15 3 Calculus 274 Python Scientific lecture notes Release 2011 1 15 3 3 Series expansion SymPy also knows how to compute the Taylor series of an expression at a point Use series expr var gt gt gt series cos x x 1 xxx2 2 xxx4 24 O xx 6 gt gt gt series 1 cos x x 1 xxx2 2 5xxxx x4 24 O xx x 6 15 3 4 Exercises 1 Calculate limx gt 0 sin x x 2 Calulate the derivative of log x for x 15 3 5 Integration SymPy has support for indefinite and definite integration of transcendental elementary and special functions via integrate facility which uses powerful extended Risch Norman algorithm and some heuristics and pattern matching You can integrate elementary functions gt gt gt integrate 6x xx x5 x Xxx 6 gt gt gt integrate sin x x TECOS gt gt gt integrate log x x SO E gt gt gt integrate 2 x sinh x x cosh 2 Also special functions are handled easily gt gt gt integrate exp x 2 erf x x pix 1 2 erf
121. Scientific lecture notes Release 2011 1 The actual ufunc declaration cdef PyUFuncGenericFunction loop_func 1 cdef char input_output_types 3 cdef void elementwise_funcs 1 loops tune 0 ePyUF une DPDDID input_output_types 0 input_output_types 1 input_output_types 2 NPY_CDOUBLE NPY_CDOUBLE NPY_CDOUBLE elementwise_funcs 0 lt void gt mandel_single_point mandel PyUFunc_FromFuncAndData Loop tune elementwise_funcs input_output_types 1 number of supported input types 2 number of input args 1 number of output args 0 identity element never mind this mandel function name mandel z Cc gt computes iterated z z c docstring 0 unused import numpy as np import mandel pla space im 0E 1000 y np linspace 1 4 1 4 1000 c x None 13jxy None z mandel mandel c c import matplotlib pyplot as plt pit imshow abs z 2 lt 1000 extent 1 playa plt show 1 5 1 0 0 5 0 0 0 5 oE ER ae Note Most of the boilerplate could be automated by these Cython modules http wiki cython org MarkLodato CreatingUfuncs 8 2 Universal functions 175 Python Scientific lecture notes Release 2011 1 Several accepied input types E g supporting both single and double precision versions cdef void mandel_single_point double complex z_in double complex c_in double complex z
122. True Mini exercise data re interpretation See Also view colors py You have RGBA data in an array gt gt gt x np zeros 10 10 4 dtype np int8 Seo sds 0d pa 2 gt gt na cee AS 4 ll where the last three dimensions are the R B and G and alpha channels How to make a 10 10 structured array with field names r g b a without copying data gt gt gt y gt gt gt assert y r 1 all gt gt gt assert y g 2 all gt gt gt assert y b 3 all gt gt gt assert y a 4 all Solution 8 1 Life of ndarray 162 Python Scientific lecture notes Release 2011 1 gt gt gt y x view gt gt gt y nprkarcayi Cry dtype np uint8 transpose gt gt gt x y copy gt gt gt x array ie 3 Atype uint8 gt gt gt y array 1 2 3 4 dtype uint8 gt gt gt x view np intl6 array SSI y 1027 dtype int16 gt gt gt 0x0201 0x0403 Ss MOAT gt gt gt y view np int16 array 769 1026 dtype int16 e What happened e we need to look into what x 0 1 actually means gt gt gt 0x0301 0x0402 769 1026 8 1 4 Indexing scheme strides Main point The question gt gt YA LS AS Slp 7 8 911 dtype np int8 gt gt gt str x data x01 x02 x03 x04 x05 x06 x07 x08 t At which byte
123. _out nogil cdef void mandel_single_point_singleprec float complex z_in float complex c_in float complex z_out nogil cdef PyUFuncGenericFunction loop_funcs 2 cdef char input_output_types 3x2 cdef void elementwise_funcs 1x2 lcop tunes 0 2 yUE UNS DDD input_output_types 0 NPY_CDOUBLE input_output_types 1 NPY_CDOUBLE input_output_types 2 NPY_CDOUBLE elementwise_funcs 0 lt void gt mandel_single_point loop_funcs 1 PyUFunc_FF_F input_output_types 3 NPY_CFLOAT input_output_types 4 NPY_CFLOAT input_output_types 5 NPY_CFLOAT elementwise_funcs 1 lt void gt mandel_single_point_singleprec mandel PyUFunc_FromFuncAndData Toop tune elementwise_funcs input_output_types 2 number of supported input types 2 number of input args 1 number of output args 0 identity element never mind this mandel function name mandel z Cc gt computes iterated z z c docstring 0 unused 8 2 4 Generalized ufuncs ufunc output elementwise_function input Both output and input can be a single array element only generalized ufunc output and input can be arrays with a fixed number of dimensions For example matrix trace sum of diag elements input shape n n output shape i e scalar Matrix product 8 2 Universal functions 176 Python Scientific lecture notes Release 2011 1 ma o
124. a normal probability density function However such function is not going to be estimated because it gives a probability from a wind speed maxima Finding the maximum wind speed occurring every 50 years requires the opposite approach the result needs to be found from a defined probability That is the quantile function role and the exercise goal will be to find it In the current model it is supposed that the maximum wind speed occurring every 50 years is defined as the upper 2 quantile By definition the quantile function is the inverse of the cumulative distribution function The latter describes the probability distribution of an annual maxima In the exercise the cumulative probability p_i for a given year i is defined as p_i 1 N 1 with N 21 the number of measured years Thus it will be possible to calculate the cumulative probability of every measured wind speed maxima From those experimental points the scipy interpolate module will be very useful for fitting the quantile function Finally the 50 years maxima is going to be evaluated from the cumulative probability of the 2 quantile Computing the cumulative probabilities The annual wind speeds maxima have already been computed and saved in the numpy format in the file xamples max speeds npy thus they will be loaded by using numpy gt gt gt import numpy as np gt gt gt max_speeds np load examples max speeds npy gt gt gt years_nb max_speeds shape 0
125. ager __exit__ In other words the context manager protocol defined in PEP 343 permits the extraction of the boring part of a try except finally structure into a separate class leaving only the interesting do_something block 1 The__enter__ method is called first It can return a value which will be assigned to var The as part is optional if it isn t present the value returned by __enter___is simply ignored 2 The block of code underneath with is executed Just like with t ry clauses it can either execute success fully to the end or it can break continue or return or it can throw an exception Either way after the block is finished the __exit__ method is called If an exception was thrown the information about the exception is passed to __exit__ which is described below in the next subsection In the normal case exceptions can be ignored just like ina finally clause and will be rethrown after__e x it__ is finished Let s say we want to make sure that a file is closed immediately after we are done writing to it gt gt gt class closing object def imit self obj self obj obj def _ enter_ self return self obj def _ _exit_ self xargs v self obj close gt gt gt with closing open tmp file w as f mete the contente Nal 7 3 Context managers 153 Python Scientific lecture notes Release 2011 1 Here we have made sure that the f close is called when t
126. aining samples closest to it in n dimensional space where n is the number of features in each sample 16 2 Classification 280 Python Scientific lecture notes Release 2011 1 Sepal width Sepal length The k nearest neighbors classifier internally uses an algorithm based on ball trees to represent the samples it is trained on KNN k nearest neighbors classification example gt gt gt Create and fit a nearest neighbor classifier gt gt gt from scikits learn import neighbors gt gt gt knn neighbors NeighborsClassifier gt gt nine asada tra ls So gel NeighborsClassifier algorithm auto classification _type knn_vote leaf_size 30 n_neighbors 5 radius 1 0 ceo kon predict CLOC OS Met array 0 Training set and testing set When experimenting with learning algorithms it is important not to test the prediction of an estimator on the data used to fit the estimator Indeed with the KNN estimator we would always get perfect prediction on the training set gt gt gt perm np random permutation iris target size gt gt gt iris data iris data perm gt gt gt iris target iris target perm gt gt gt knn es data T00 tris targec 200 NeighborsClassifier algorithm auto classification _type knn_vote leaf_size 30 n_neighbors 5 radius 1 0 gt gt gt knn score iris data 100 iris target 100 ORS SSS Orr Bonus question
127. al ain cange Nih print str_format format self storage self release self inflows self spillage print 1 379 mE name _ main_ projectA Reservoir name Project A max_storage 30 ll max_release Diy hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 25 state release 4 state inflows 0 ll state print_state state configure_traits PSA PA 4 Edit properties Name Project A Storage 21 0 Spillage O Inflows 0 0 Release 0 0 5 0 4 0 Some use cases need the delegation mechanism to be broken by the user when setting the value of the trait The PrototypeFrom trait implements this behaviour from traits api import HasTraits Str Float Range PrototypedFrom Instance class Turbine HasTraits 13 3 What are Traits 254 Python Scientific lecture notes Release 2011 1 turbine_type Str power Float 1 0 desc Maximal power delivered by the turbine Mw class Reservoir HasTraits name Str max_storage Float le6 desc Maximal storage hm3 max release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m fficiency Range 0 1 turbine Instance Turbine installed capacity PrototypedFrom turbine power if name main__ turbine Turbine turbine_type typel power 5 0 reservoir Reservoir
128. al namespace The variable x only exists within the function foo 2 5 5 Global variables Variables declared outside the function can be referenced within the function MA In 115 def adadx y IE return x y 2 5 Defining functions 22 Python Scientific lecture notes Release 2011 1 In 116 addx 10 Orere MAES eTa But these global variables cannot be modified within the function unless declared global in the function This doesn t work In 117 def setx y E x y E print Ox ls 2a ax En 118 secx 10 Se ls 1100 Ta 120 x Guwo S This works In 121 def setx y Toes global x Ts x y A Print is sa 7 x In 122 setx 10 s ake ILO Tn 123 Owe Ae O x 2 5 6 Variable number of parameters Special forms of parameters e args any number of positional arguments packed into a tuple e kwargs any number of keyword arguments packed into a dictionary In 35 def variable_args args xxkwargs eters print arge isi args OS print kwargs is kwargs In 36 variable_args one two Xl y 2 z 3 arge is Vons EWON kwargs irs Tiye E l az a 2 5 7 Docstrings Documentation about what the function does and it s parameters General convention In 67 def funcname params O Concise one line sentence describing the function ads Extended summary which can contain multiple paragraphs
129. algorithmic optimization are there ways to compute less or better For a high level view of the problem a good understanding of the maths behind the algorithm helps However it is not uncommon to find simple changes like moving computation or memory allocation outside a for loop that bring in big gains Example of the SVD In both examples above the SVD Singular Value Decomposition is what takes most of the time Indeed the computational cost of this algorithm is roughly n in the size of the input matrix However in both of these example we are not using all the output of the SVD but only the first few rows of its first return argument If we use the svd implementation of scipy we can ask for an incomplete version of the SVD Note that implementations of linear algebra in scipy are richer then those in numpy and should be preferred In 3 Stimeit np linalg svd data 1 loops best of 3 14 5 s per loop In 4 from scipy import linalg In 5 timeit linalg svd data loops best of 3s 14 2 s per loop In 6 timeit linalg svd data full_matrices False losas best oror 2905 me ger loop In 7 Stimeit np linalg svd data full_matrices False 1 loops best of 3 293 ms per loop Real incomplete SVDs e g computing only the first 10 eigenvectors can be computed with arpack available in scipy sparse linalg eigsh Computational linear algebra For certain algorithms many of the bottlenecks will
130. ame or points or mesh or fancymesh Default surface resolution The resolution of the glyph created For spheres for instance this is the number of divisions along theta and phi Must be an integer Default 8 scalars optional scalar data scale_factor scale factor of the glyphs used to represent the vertices in fancy_mesh mode Must be a float Default 0 05 scale_mode the scaling mode for the glyphs vector scalar or none transparent make the opacity of the actor depend on the scalar tube_radius radius of the tubes used to represent the lines in mesh mode If None simple lines are used tube_sides number of sides of the tubes used to represent the lines Must be an integer Default 6 vmax vmax is used to scale the colormap If None the max of the data will be used vmin vmin is used to scale the colormap If None the min of the data will be used Example In 1 import numpy as np in 22 a cheta op mead 09110 Np peinp pi H0 In 3 x rx xnp cos theta In 4 y r xnp sin theta in 512 2 sa e In 6 from enthought mayavi import mlab In 7 mlab mesh x y Zn colormap gist_earth extent 0 1 0 1 0 1 Out 7 lt enthought mayavi modules surface Surface object at Oxde6f08c gt 14 3 Figures and decorations 268 Python Scientific lecture notes Release 2011 1 nie ilies Out 8 mikaloemes Es y 2z extent OO O r representatio
131. amming OOP 40 CHAPTER 3 NumPy creating and manipulating numerical data authors Emmanuelle Gouillart Didrik Pinte Ga l Varoquaux and Pauli Virtanen Chapters contents e The numpy array object page 42 What are Numpy and numpy arrays page 42 Reference documentation page 42 Creating arrays page 43 Basic data types page 44 Basic visualization page 45 Indexing and slicing page 48 Copies and views page 50 Data files page 50 Summary amp Exercises page 52 e Numerical operations on arrays page 55 Elementwise operations page 55 Basic linear algebra page 56 Basic reductions page 57 Broadcasting page 61 Array shape manipulation page 64 Fancy indexing page 67 Sorting data page 69 Summary amp Exercises page 70 Conclusions page 74 e Moving on page 75 More data types page 75 Structured data types page 76 Fourier transforms page 77 Masked arrays page 80 Polynomials page 82 Summary amp Exercises page 84 Under the hood page 85 It s page 85 Block of memory page 85 Indexing scheme strides page 86 Summary page 88 41 Python Scientific lecture notes Release 2011 1 3 1 The numpy array object 3 1 1 What are Numpy and numpy arrays Python has e built in lists integers floating point e for numerics more is needed efficiency convenience Numpy i
132. ample by Olivier Grisel http scikit learn org dev auto_examples applications face _recognition html original shape of images 50 37 moe import numpy as np import pylab as pl from scikits learn import cross_val datasets decomposition svm load data lfw_people datasets fetch_lfw_people min_faces_per_person 70 resize 0 4 perm np random permutation lfw_people target size lfw_people data lfw_people data perm lfw_people target lfw_people target perm faces np reshape lfw_people data lfw_people target shape 0 1 train test iter cross_val StratifiedKFold 1fw_people target k 4 next X_train X_test faces train faces test y_train y_test lfw_people target train lfw_people target test dimension reduction pca decomposition RandomizedPCA n_components 150 whiten True OC cle priser 6 escala X_train_pca pca transform X_train X_test_pca pca transform X_test Classification clf svm SVC C 5 gamma 0 001 Cli Ee cerca polea y eranmi 16 5 Putting it all together face recognition 286 Python Scientific lecture notes Release 2011 1 predict on new images for i in range 10 print lfw_people target_names clf predict X_test_pca i 0 _ pl imshow X_test i reshape 50 37 cmap pl cm gray _ raw_input 16 6 Linear model from regression to sparsity Diabetes dataset The diabetes dataset co
133. an alias for manipulating matrix will first be defined gt gt gt asmat np asmatrix then the steps are gt gt gt sarr np zeros 3 4 gt gt gt sarr put 0 5 10 spec gt gt gt svd_mat asmat uarr asmat sarr asmat vharr gt gt gt np allclose svd_mat arr True SVD is commonly used in statistics or signal processing Many other standard decompositions QR LU Cholesky Schur as well as solvers for linear systems are available in scipy linalg 6 7 Numerical integration scipy integrate The most generic integration routine is scipy integrate quad gt gt gt from scipy integrate import quad gt gt gt res err quad np sin 0 np pi 2 gt gt gt np allclose res 1 True gt gt gt np allclose err 1 res True Others integration schemes are available with fixed_quad quadrature romberg scipy integrate also features routines for Ordinary differential equations ODE integration In particular scipy integrate odeint isa general purpose integrator using LSODA Livermore solver for ordinary dif ferential equations with automatic method switching for stiff and non stiff problems see the ODEPACK Fortran library for more details odeint solves first order ODE systems of the form Nichole adas al Wey Gey Up ano gt As an introduction let us solve the ODE dy dt 2y betweent 0 4 with the initial condition y t 0 1 Fi
134. and modules 28 Python Scientific lecture notes Release 2011 1 import sys def print_a MPSS oo peint on print sys argv i name main__ Importing it In 11 import demo2 b In 12 import demo2 Running it In 13 run demo2 b a 2 6 5 Scripts or modules How to organize your code Note Rule of thumb e Sets of instructions that are called several times should be written inside functions for better code reusability e Functions or other bits of code that are called from several scripts should be written inside a module so that only the module is imported in the different scripts do not copy and paste your functions in the different scripts Note How to import a module from a remote directory Many solutions exist depending mainly on your operating system When the import mymodule statement is executed the module mymodule is searched in a given list of directories This list includes a list of installation dependent default path e g usr 1ib python as well as the list of directories specified by the environment variable PYTHONPATH The list of directories searched by Python is given by the sys path variable In 1 import sys In 2 sys path Out IET Baa as Dan usr local include enthought traits 1 1 0 use ib pyc honz 67 usr lib python2 6 plat linux2 as Mao python2 6S DAEK usr lib python2 6 lib old usr lib p
135. anipulate them http www numpy org e Scipy high level data processing routines Optimization regression interpolation etc http www scipy org e Matplotlib 2 D visualization publication ready plots http matplotlib sourceforge net e Mayavi 3 D visualization 1 2 Scientific Python building blocks 5 Python Scientific lecture notes Release 2011 1 http code enthought com projects mayavi 1 3 The interactive workflow IPython and a text editor Interactive work to test and understand algorithm Python is a general purpose language As such there is not one blessed environement to work into and not only one way of using it Although this makes it harder for beginners to find their way it makes it possible for Python to be used to write programs in web servers or embedded devices Here we describe an interactive workflow with Python that is handy to explore and understand algorithms Note Reference document for this section IPython user manual http ipython org ipython doc dev index html 1 3 1 Command line interaction Start ipython In 1 print Hello world Hello world Getting help in 2 print Type builtin_function_or_method Base Class lt type builtin function or methodi gt Sicilia lowes lt buaihe in FUNC E LON prints Namespace Python builtin Docstring print value sep end n file
136. any objects will be needed the standard way to do this is something like def find_answers answers while True ans look_for_next_answer if ans is None break answers append ans return answers This is fine as long as the body of the loop is fairly compact Once it becomes more complicated as often happens in real code this becomes pretty unreadable We could simplify this by using yield statements but then the user would have to explicitly call List find_answers We can define a decorator which constructs the list for us def vectorized generator_func def wrapper xargs kwargsS return list generator_func args kwargs return functools update_wrapper wrapper generator_func Our function then becomes vectorized def find_answers while True ans look_for_next_answer if ans is None break yield ans 7 2 7 A plugin registration system This is a class decorator which doesn t modify the class but just puts it in a global registry It falls into the category of decorators returning the original object class WordProcessor object PLUGINS def process self text for plugin in self PLUGINS text plugin cleanup text return text classmethod def plugin cls plugin cls PLUGINS append plugin WordProcessor plugin class CleanMdashesExtension object def cleanup self text return text replace amp mdash u N em d
137. art of the language e classmethod causes a method to become a class method which means that it can be invoked without creating an instance of the class When a normal method is invoked the interpreter inserts the instance object as the first positional parameter self When a class method is invoked the class itself is given as the first parameter often called cls Class methods are still accessible through the class namespace so they don t pollute the module s names pace Class methods can be used to provide alternative constructors class Array object def Mt self data self data data classmethod def fromfile cls file data numpy load file return cls data This is cleaner then using a multitude of flags to__init__ e stat icmethod is applied to methods to make them static i e basically a normal function but acces sible through the class namespace This can be useful when the function is only needed inside this class its name would then be prefixed with _ or when we want the user to think of the method as connected to the class despite an implementation which doesn t require this e property is the pythonic answer to the problem of getters and setters A method decorated with property becomes a getter which is automatically called on attribute access gt gt gt class A object property def a self an important attribute ame return a value gt gt gt A a 7 2 D
138. ase 2011 1 2 8 2 shutil high level file operations The shut il provides useful file operations e shutil rmtree Recursively delete a directory tree e shutil move Recursively move a file or directory to another location e shutil copy Copy files or directories 2 8 3 glob Pattern matching on files The glob module provides convenient file pattern matching Find all files ending in t xt In 18 import glob In 19 qlobagleb x EE QUE ASIA nol a A Un nene ts 2 8 4 sys module system specific information System specific information related to the Python interpreter e Which version of python are you running and where is it installed In 117 sys platform Cueca Carwin In 118 sys version OME AS ES US Sa ADOS Oy says ss Wel GCC 4 0 1 Apple Computer Inc build 5363 in 119 sys prefix Out 119 Library Frameworks Python framework Versions 2 5 e List of command line arguments passed to a Python script In 100 sys argv Out LOO E Users cburns local bin ipython sys path is a list of strings that specifies the search path for modules Initialized from PYTHONPATH Ta 2d sys path Curie py Users cburns local bin Users cburns local lib python2 5 site packages grin 1 1 py2 5 egg Users cburns local lib python2 Users cburns local lib python2 Users cburns local lib python2 Users cburns local li
139. ash Here we use a decorator to decentralise the registration of plugins We call our decorator with a noun instead of a verb because we use it to declare that our class is a plugin for WordProcessor Method plugin simply appends the class to the list of plugins A word about the plugin itself it replaces HTML entity for em dash with a real Unicode em dash character It exploits the unicode literal notation to insert a character by using its name in the unicode database EM DASH 7 2 Decorators 152 Python Scientific lecture notes Release 2011 1 If the Unicode character was inserted directly it would be impossible to distinguish it from an en dash in the source of a program 7 2 8 More examples and reading PEP 318 function and method decorator syntax PEP 3129 class decorator syntax http wiki python org moin PythonDecoratorLibrary http docs python org dev library functools html e http pypi python org pypi decorator e Bruce Eckel Decorators I Introduction to Python Decorators Python Decorators II Decorator Arguments Python Decorators II A Decorator Based Build System 7 3 Context managers A context manager is an object with _ enter___ and ___exit__ methods which can be used in the with state ment with manager as var do_something var is in the simplest case equivalent to var manager __enter___ try do_something var finally man
140. ax set_ylabel calculated ax grid True 7 5 9 The Class Library 107 Python Scientific lecture notes Release 2011 1 Line set_marker 0 8 canvas FigureCanvasAgg fig 10 canvas print_figure oo png dpi 80 11 import Tkinter as Tk 12 Coot a 14 canvas2 FigureCanvasTkAgg fig master root 15 canvas2 show 16 canvas2 get_tk_widget pack side Tk TOP fill Tk Tk mainloop 18 from matplotlib import _pylab_helpers 19 import pylab 20 pylab_fig pylab figure 1 figsize figsize 21 figManager _pylab_helpers Gcf get_active 22 figManager canvas figure fig 23 pylab show 24 BOTE from matplotlib backends backend_agg import FigureCanvasAgg 9 from matplotlib backends backend_tkagg import FigureCanvasTkAgg 13 expand 1 17 Since we are not in the interactive pylab mode we need to import the class Figure explicitly 1 We set the size of our figure to be 8 by 5 inches 2 Now we initialize a new figure 3 and add a subplot to the figure 4 The 111 says one plot at position 1 1 just as in MATLAB We create a new plot with the numbers from 0 to 9 and at the same time get a reference to our line 5 We can add several things to our plot So we set a title and labels for the x and y axis 6 We also want to see the grid 7 and would like to have little filled circles as markers 8 There are many different backends for rendering o
141. ax_storage DelegatesTo reservoir max_release DelegatesTo reservoir min_release Float state attributes storage Property depends_on inflows release control attributes inflows Float desc Inflows hm3 release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release Traits view HHHRRRRARRARARRRARMARAARARAAARAARARARAA AAA ARA AAA ddd td tage eet traits_view View Group VGroup Item name Item storage Item spillage label State style readonly mn VGroup Item inflows Item release label Control Private traits FFHFEFFEREFEREEEREAEREEEEEEEREE ETAT EEE EEE EE EEE 13 3 What are Traits 253 Python Scientific lecture notes Release 2011 1 _storage Float Traits property implementation ttttt teeta et th tt ttt ttt ttt ttt EE def _get_storage self new_storage self _storag self releas self inflows return min new_storage self max_storage def _set_storage self storage_value self _storage storage_value def _get_spillage self new_storage self _storag self releas self inflows overflow new_storage self max_storage return max overflow 0 def print_state self print Storage tRelease tInflows tSpillage see formar NE sion tay fon
142. b python2 5 site packages argparse 0 8 0 py2 5 egg 5 site packages urwid 0 9 7 1 py2 5 egg 5 site packages yolk 0 4 1 py2 5 egg 5 site packages virtualenv 1 2 py2 5 egg 2 8 5 pickle easy persistence Useful to store arbitrary objects to a file Not safe or fast 2 8 Standard Library 36 Python Scientific lecture notes Release 2011 1 In 1 import pickle jin 2 ile None Stani In 3 prickle d mp ll Trie test PRI Zw In 4 pickle load file test pkl Out 4 1 None Stan Exercise Write a program to search your PYTHONPATH for the module site py path_site 2 9 Exception handling in Python It is highly unlikely that you haven t yet raised Exceptions if you have typed all the previous commands of the tutorial For example you may have raised an exception if you entered a command with a typo Exceptions are raised by different kinds of errors arising when executing Python code In your own code you may also catch errors or define custom error types 2 9 1 Exceptions Exceptions are raised by errors in Python e ZeroDivisionError integer division or modulo by zero In 2 a e TypeError unsupported operand type s for int and str Ta ie a eee AnA Ea als sad 81 KeyError 3 ta psik lie ES In 6 1 4 IndexError list index out of range Thy 1 foobar
143. bject at Oxcc3eldc gt 14 2 3 Elevation surface rael tao elig 14 2 3D plotting functions 264 Python Scientific lecture notes Release 2011 1 Enel Y p eee OOO Os KOOL OO a LO np sqrt x 2 yxx 2 In 11i 2 mps ee In 12 mlab surf z warp_scale auto Out 12 lt enthought mayavi modules surface Surface object at Oxcdb98fc gt 14 2 4 Arbitrary regular mesh tn 13 mob cito In 14 phi theta ne mgrid O np pislly 052 np pi 1151 In 15 x np sin phi np cos theta In 16 y np sin phi np sin theta in L7 2 npcos pha ta 18 mlab mesn x y Z In 19 mlab mesh x y z representation wireframe color 0 0 0 Out 19 lt enthought mayavi modules surface Surface object at Oxcel017c gt 14 2 3D plotting functions 265 Python Scientific lecture notes Release 2011 1 Note A surface is defined by points connected to form triangles or polygones In mlab func and mlab mesh the connectivity is implicity given by the layout of the arrays See also mlab triangular_mesh Our data is often more than points and values it needs some connectivity information 14 2 5 Volumetric data Ia 20 mab pelea Tta PUI x y Z2 Tole pe SS Kile a Spin eh In 22 values x xx 0 5 yey zxzx x2 0 In 23 mlab contour3d values Out 24 lt enthought mayavi modules iso_surface IsoSurface object at Oxcfe392c gt
144. c head m efficiency Range 0 1 traits_view View name max_storage max_release head efficiency title Reservoir resizable True def energy_production self release Returns the energy production Wh for the given release m3 s power 1000 x 9 81 self head release self efficiency return power 3600 main__ reservoir Reservoir name ProjJect A max_storage 30 13 3 What are Traits 249 Python Scientific lecture notes Release 2011 1 max_release 100 0 head 60 efficiency 0 8 reservoir configure_traits PES A eh Name Max storage 30 0 Max release 100 0 Head 60 0 Efficiency 0 0 13 3 5 Deferral Being able to defer the definition of a trait and its value to another object is a powerful feature of Traits from traits api import HasTraits Instance DelegatesTo Float Range from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage noe reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Range low min_storage high max_storage control attributes inflows Float desc Inflows hm3 release Range low min_releas
145. c listeners from reservoir import Reservoir from reservoir_state property import ReservoirState def wake_up_watchman_if_spillage new_value if new_value gt 0 print Wake up watchman Spilling hm3 format new_value TE name _ main _ 13 3 What are Traits 256 Python Scientific lecture notes Release 2011 1 projectA Reservoir name Project A max_storage 30 max_release LODE Ol hydraulic_head 60 efficiency 0 oe tate ReservoirStat reservoir projectA storage 10 register the dynamic listener state on_trait_change wake_up_watchman_if_spillage name spillage release 90 inflows 0 print_state ta a ta ta print s S Forcing spillage inflows 100 release 0 Cartes Cares print Why do we have two executions of the callback 2 The dynamic trait notification signatures are not the same as the static ones def wake_up_watchman pass def wake_up_watchman new pass def wake_up_watchman name new pass def wake_up_watchman object name new pass def wake_up_watchman object name old new pass Removing a dynamic listener can be done by e calling the remove_trait_listener method on the trait with the listener method as argument e calling the on_trait_change method with listener method and the keyword remove True e deleting the instance that holds the listener Listeners ca
146. ccessible from gt gt gt import scipy sparse linalg as spla gt gt gt spla _ all LinearOperator Tester arpack aslinearoperator bicg EDucgsiza 7 as as ese ome Csr matrix n dasolven eigen eigen_symmetric factorized gmres interface iS hie cake 0 lec Misco las Misc mantesi npe Ceme SUS As Eg Or NUS esti paca US es oler As varnings 11 3 1 Sparse Direct Solvers e default solver SuperLU 4 0 included in SciPy real and complex systems both single and double precision optional umfpack real and complex systems double precision only recommended for performance wrappers now live in scikits umfpack check out the new scikits suitesparse by Nathaniel Smith Examples e import the whole module and see its docstring gt gt gt import scipy sparse linalg dsolve as dsl gt gt gt help ds1l both superlu and umfpack can be used if the latter is installed as follows prepare a linear system gt gt gt import numpy as np gt gt gt import scipy sparse as sps gt gt gt mess Bos spaad HL 2 gS y Bil Sp Sp Es Yo M1 10 Milo 5p Y gt gt gt mtx todense matrix dae 57 0 Os Ove OO 2 En Wil WO OW Sy 3 Olly LO O Ore Sa Oy LO O OO Sill gt gt gt rhs np array il 2 S35 4 SI 11 3 Linear System Solvers 220 Python Scie
147. ch as winpdb Alternatively pudb is a good semi graphical debugger with a text user interface in the console 9 3 2 Debugger commands and interaction 1 list Lists the code at the current position u p Walk up the call stack d own Walk down the call stack n ext Execute the next line does not go down in new functions s tep Execute the next statement goes down in new functions bt Print the call stack a Print the local variables command Exectute the given Python command by opposition to pdb commands Warning Debugger commands are not Python code You cannot name the variables the way you want For instance if in you cannot override the variables in the current frame with the same name use different names then your local variable when typing code in the debugger 9 4 Debugging segmentation faults using gdb If you have a segmentation fault you cannot debug it with pdb as it crashes the Python interpreter before it can drop in the debugger Similarly if you have a bug in C code embedded in Python pdb is useless For this we turn to the gnu debugger gdb available on Linux Before we start with gdb let us add a few Python specific tools to it For this we add a few macros to our gbdinit The optimal choice of macro depends on your Python version and your gdb version I have added a simplified version in gdbinit but feel free to read Debugging WithGdb To debug with gdb the Python script seg
148. cision arithmetic That way some special constants like e pi oo Infinity are treated as symbols and can be evaluated with arbitrary precision gt gt gt pixx2 DLE 232 gt gt gt pi evalf 3a 1159265958979 gt gt gt pi exp 1 evalf 5 85987448204884 as you see evalf evaluates the expression to a floating point number There is also a class representing mathematical infinity called oo 15 1 First Steps with SymPy 272 Python Scientific lecture notes Release 2011 1 gt gt gt 00 gt 99999 True gt gt gt oo 1 oG 15 1 2 Exercises 1 Calculate v2 with 100 decimals 2 Calculate 1 2 1 3 in rational arithmetic 15 1 3 Symbols In contrast to other Computer Algebra Systems in SymPy you have to declare symbolic variables explicitly gt gt gt from sympy import x gt gt gt x Symbol x gt gt gt y Symbol y Then you can manipulate them gt gt gt XtYtxX y 2x X ZSS NO A2 x y 2 Symbols can now be manipulated using some of python operators arithmetic amp gt gt lt lt boolean 15 2 Algebraic manipulations SymPy is capable of performing powerful algebraic manipulations We ll take a look into some of the most frequently used expand and simplify 15 2 1 Expand Use this to expand an algebraic expression It will try to denest powers and multiplications In
149. commands block following a colon bears an additional indentation level with respect to the previous line with a colon One must therefore indent after def f or while At the end of such logical blocks one decreases the indentation depth and re increases it if a new block is entered etc Strict respect of indentation is the price to pay for getting rid of or characters that delineate logical blocks in other languages Improper indentation leads to errors such as IndentationError unexpected indent test py line 2 All this indentation business can be a bit confusing in the beginning However with the clear indentation and in the absence of extra characters the resulting code is very nice to read compared to other languages e Indentation depth Inside your text editor you may choose to indent with any positive number of spaces 1 2 3 4 However it is considered good practice to indent with 4 spaces You may configure your editor to map the Tab key to a 4 space indentation In Python x y the editor Scite is already configured this way Style guidelines 2 6 Reusing code scripts and modules 31 Python Scientific lecture notes Release 2011 1 Long lines you should not write very long lines that span over more than e g 80 characters Long lines can be broken with the character gt gt gt long_line Here is a very very long line that we break in two parts Space
150. contains polynomials in different bases 3 3 Moving on 82 Python Scientific lecture notes Release 2011 1 For example 31 2x 1 2 19 alles 2 gt gt gt p 0 gt gt gt hous ar sav Sd r 0S SaS gt gt gt p order 2 gt gt gt x np linspace 0 1 20 gt gt gt y np cos x 0 3 np random rand 20 gt gt gt p np polyld np polyfit x y 3 gt gt gt t np linspace 0 1 200 Sse oleole ey Wp O ty wey a gt gt gt plt show 1 3 1 2 1 1 1 0 0 9 0 8 b0 0 2 0 4 0 6 0 8 1 0 See http docs scipy org doc numpy reference routines polynomials poly 1d html for more More polynomials with more bases Numpy also has a more sophisticated polynomial interface which supports e g the Chebyshev basis 3a 22 1 gt gt gt p np polynomial Polynomial 1 2 3 coefs in different order gt gt gt p 0 10 gt gt gt PD roots array i1 AOS Ses 33833 gt gt gt p order 2 3 3 Moving on 83 Python Scientific lecture notes Release 2011 1 Example using polynomials in Chebyshev basis for polynomials in range 1 1 gt gt gt gt gt gt np linspace 1 1 2000 np cos x 0 3 np random rand 2000 np polynomial Chebyshev fit x y 90 MOE eins pacer ci mle 2 0101 SISO kp Wie te 5S Plot Er PE k tw s show 1 0 0 5 0 0 0 5 1 0 The Chebyshev polynomials have
151. d grid True grid True which minor Now we have loglog plot igx wS 10 10 tolo 6 If we want only one axis with a logarithmic scale we can use semilogx or semi logy Pie charts can also be created with a few lines data 500 700 300 lapels cats dogs other pie data labels labels The result looks as expected AE HO O aja Polar plots are also possible Let s define our r from 0 to 360 and our theta from 0 to 360 degrees We need to convert them to radians r arange 360 theta r 180 pi 5 8 Other Types of Plots 104 Python Scientific lecture notes Release 2011 1 Now plot in polar coordinates polar theta r We get a nice spiral AE DO Ol ea Plotting arrows in 2D plane can be achieved with quiver We define the x and y coordinates of the arrow shafts x y arange 10 The x and y components of the arrows are specified as 2D arrays u ones 10 10 v ones 10 10 u 4 4 3 a al Now we can plot the arrows cuasi E Wr UU Ww All arrows point to the upper right except two The one at the location 4 4 has 3 units in x direction and the other at location 1 1 has 1 unit in y direction alo 4A A A A A A A A A PAP A 4A A AA A 4 A 4 A 4 A A A 4A 4A AFA AA 4A 4 P
152. d add attributes e g add a docstring to a class A decorator might do something useful even without modifying the object for example register the decorated class in a global registry In the second case virtually anything is possible when a something different is substituted for the original function or class the new object can be completely different Nevertheless such behaviour is not the purpose of decorators they are intended to tweak the decorated object not do something unpredictable Therefore when a function is decorated by replacing it with a different function the new function usually calls the original function after doing some preparatory work Likewise when a class is decorated by replacing if with a new class the new class is usually derived from the original class When the purpose of the decorator is to do something every time like to log every call to a decorated function only the second type of decorators can be used On the other hand if the first type is sufficient it is better to use it because 1t is simpler 7 2 2 Decorators implemented as classes and as functions The only requirement on decorators is that they can be called with a single argument This means that decorators can be implemented as normal functions or as classes witha __cal1__ method or in theory even as lambda functions Let s compare the function and class approaches The decorator expression the part after can be either ju
153. d charx x PyUFunc_gg_g charx args args PyUFuncGenericFunction charx x object PyUFunc_FromFuncAndData PyUFuncGenericFunctions func oe MN char doc npy_intp dimensions npy_intp dimensions npy_intp dimensions npy_intp dimensions npy_intp dimensions npy_intp dimensions PyUFunc_ff_f_As_dd_d char args npy_intp dimensions npy_intp steps void func npy_intp dimensions npy_intp dimensions args npy_intp dimensions npy_intp steps volds func PyUFunc_FF_F_As_DD_D char args npy_intp dimensions npy_intpx steps voids func npy_intp voidx voidx x data npy_intp inte Dout e E steps void Func voids func voldx func volada fume steps voidx func void func void func voids func npy_intp dimensions npy_intpx npy_intpx steps npy_intpx steps npy_intp steps npy_intpx dimensions npy_intp npy_intpx steps npy_intpx steps npy_intpx steps voidx func walls FUNE npy_intpx steps npy_intp steps void void void Py Py Py U U U Func_DD_D char args Func_FF_F char args Func_GG_G charxx args npy_intp dimensions npy_intp dimensions npy_intp dimensions void func vord func voidx func SAN LES npy_intp steps npy_intpx steps Required module initialization import_array import_ufunc The actual ufunc declaration cdef PyUFuncGenericFunction loo
154. d_areas List IrrigationArea total_crop_surface Property depends_on irrigated_areas surface def _get_total_crop_surface self return sum iarea surface for iarea in self irrigated_areas 13 3 What are Traits 259 Python Scientific lecture notes Release 2011 1 def energy_production self release Returns the energy production Wh for the given release m3 s PET power 1000 9 81 self head release self efficiency return power 3600 traits_view View Item name Item max_storage Item max_release Item head Item efficiency Item irrigated_areas Item total_crop_surface resizable Tru I name main__ upper_block IrrigationArea name Section C surface 2000 crop Wheat reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 irrigated_areas upper_block release 80 print Releasing m3 s produces kWh format release reservoir energy_production release The next example shows how the Array trait can be used to feed a specialised TraitsUI Item the ChacoPlotltem import numpy as np from traits api import HasTraits Array Instance Float Property from traits api import DelegatesTo from traitsui api import View Item Group from chaco chaco_plot_editor import ChacoPlotItem
155. data coordinates eee ee In 3 tl text 5 5 Text in the middle figtext uses figure coordinates form 0 to 1 In MI t2 frgtext 0 8 0 8 Upper right text 5 5 Text 97 Python Scientific lecture notes Release 2011 1 xt in the middle 10 0 a matplotlib supports TeX mathematical expression So r pi will show up as T If you want to get more control over where the text goes you use annotations In 4 ax gca In 5 ax annotate Here is something special xy 1 1 We will write the text at the position 1 1 in terms of data There are many optional arguments that help to customize the position of the text The arguments text coords and xycoords specifies what x and y mean argument coordinate system figure points points from the lower left corner of the figure figure pixels pixels from the lower left corner of the figure figure fraction 0 0 is lower left of figure and 1 1 is upper right axes points points from lower left corner of axes axes pixels pixels from lower left corner of axes axes fraction 0 1 is lower left of axes and 1 1 is upper right data use the axes data coordinate system If we do not supply xycoords the text will be written at xy Furthermore we can use an arrow whose appearance can also be described in detail In 14 plot arange 10 Out 14 lt matplotlib lines Line2D instance at 0
156. dd the directory to your sys path each time you want to import from a module in this directory See http docs python org tutorial modules html for more information about modules 2 6 6 Packages A directory that contains many modules is called a package A package is a module with submodules which can have submodules themselves etc A special file called ___ init__ py which may be empty tells Python that the directory is a Python package from which modules can be imported sd 2116 usr lib python2 6 dist packages scipy ls OEA cluster io README txtQ stsci __config__ py LATEST txt setup pye __svn_version__ py Come sieve Lio setup pyc __svn_version__ pyc constants linalg setupscons py THANKS txtQ Eft pack linsolve setupscons pyc OCHANGE txtQ E O maxentropy signal version py Lainie Y misc sparse version pyc INSTALL txt ndimage spatial weave integrate odr special interpolate optimize stats sd 2116 usr lib python2 6 dist packages scipy cd ndimage ICTSO TA sd 2116 usr lib python2 6 dist packages scipy ndimage Is La SOF doccer py fourier pyc interpolation py morphology pyc setup pyc doccer pyc info py interpolation pyc _nd_image so setupscons py filters py info pyc measurements py _ni_support py setrescons Dye filters pyc __init__ py measurements pyc _ni_support pyc tests fourier py __init__ pyc morphology py setup py From Ipython
157. de ascii eee except UnicodeError e PTA if name Ga l O print OK Ga l NS else a raise e E return name In 16 filter_name Ga l OK Ga l Out 16 Ga xe3 xabl In 17 filter name St fan ascii codec can t decode byte Oxc3 in position 2 ordinal UnicodeDecodeError e Exceptions to pass messages between parts of the code In 17 def achilles_arrow x E if abs x 1 lt le 3 TO raise Stoplteration a A Ae son arate return x In 18 x 0 In 19 while True A try a x achilles_arrow x R O except Stoplteration OS break TR 20 x Out 2 017 09990234315 Use exceptions to notify certain conditions are met e g StopIteration or not e g custom error raising 2 10 Object oriented programming OOP Python supports object oriented programming OOP The goals of OOP are e to organize the code and e to re use code in similar contexts Here is a small example we create a Student class which is an object gathering several custom functions methods and variables attributes we will be able to use gt gt gt class Student object def __ init__ self name self name name set_age self age self age age set_major self major self major major def def 2 10 Object oriented programming OOP 39 not in range Python Scientific lecture notes Release 2011 1 gt gt gt anna S
158. declared with cdef Note also that this function receives xpointers to the data cdef double complex z z_in 0 cdef double complex c c_in 0 cdef int k the integer we use in the for loop TODO write the Mandelbrot iteration for one point here as you would write it in Python Say use 100 as the maximum number of iterations and 1000 as the cutoff for z realx x2 z imagx x2 TODO mandelbrot iteration should go here Return the answer for this point 2 out Il zZ 8 2 Universal functions 171 Python Scientific lecture notes Release 2011 1 Boilerplate Cython definitions The litany below is particularly long A sal asia cua ake DUSE paul ba Siebese but you don t really need to from the Numpy C headers cdef extern from numpy arrayobject h void import array ctypedef int npy_intp cdef enum NPY_TYPES NPY_DOUBLE NPY_CDOUBLE NPY_LONG cdef extern from numpy ufuncobject h void import_ufunc ctypedef void charx types int identity int ntypes char name List of pre defined loop functions void void void void void void void vord void void void void void PyUE une E As did charsx args PyUFunc_d_d char args PyUFunc_f_f charx x args PyUFunc_g_g charx xx args PyUFunc_F_F_As _D_D charxx args PyUFunc_F_F char args PyUFunc_D_D char args PyUFunc_G_G char args PyUFunc_ff_f charxx PyUFunc_dd_
159. dist size 3 3 structure np ones 3 3 gt gt gt a np zeros 5 5 dtype np int Opening erosion dilation gt gt gt a l 4 1 4 1 ala 4 2 gt gt gt a arca 0 Of 0 0 0 Or 0 0 ir li 1 0l or ee a O Oe WF O We a gt gt gt Opening removes small objects gt gt gt ndimage binary_opening a structure np ones 3 3 astype np int arravi LOP Of O WO Ol OG ky ile i Ol OF Le Te 1 oil Os wy ie 2 Ol On OF Oe UF Oi gt gt gt Opening can also smooth corners gt gt gt ndimage binary_opening a astype np int arca O O O WO 01 Ge O Lo O 017 O E il lo Ol Br O 17 10 Ol 0 07 07 107 UA A ii f Application remove noise gt gt gt square np zeros 32 32 gt gt gt square 10 10 10 10 1 gt gt gt np random seed 2 gt gt gt x y 32x np random random 2 20 astype np int gt gt gt square x y 1 gt gt gt 12 4 Image filtering 235 Python Scientific lecture notes Release 2011 1 gt gt gt open_square ndimage binary_opening square gt gt gt gt gt gt eroded_square ndimage binary_erosion square gt gt gt reconstruction ndimage binary_propagation eroded_square mask square Closing dilation erosion Many other mathematical morphology operations hit and miss transform tophat etc 12 5 Feature extraction 12 5 1 Edge detection Synthetic data g
160. dtype count 1 gt gt gt f close gt gt gt print wav_header DL CR LAC 2 ho WAVE emia ol I al OOOO OO 27 iG DM ati ae Yaa ISS gt gt gt wav_header sample_rate array 16000 dtype uint32 Let s try accessing the sub array gt gt gt wav_header data_id array ala AAA dtype S1 gt gt gt wav_header shape a gt gt gt wav_header data_id shape ip 27r 2 When accessing sub arrays the dimensions get added to the end Note There are existing modules such as wavfile audiolab etc for loading sound data Casting and re interpretation views casting e on assignment e on array construction on arithmetic 8 1 Life of ndarray 160 Python Scientific lecture notes Release 2011 1 e etc e and manually ast ype dt ype data re interpretation e manually view dtype Casting e Casting in arithmetic in nutshell only type not value of operands matters largest safe type able to represent both is picked scalars can lose to arrays in some situations e Casting in general copies data gt gt gt x np array 1 2 3 4 dtype np float gt gt gt x arcay ioe P Sig Ae gt gt gt y X astype np int8 gt gt gt y array 1 2 3 4 dtype int8 See y ar il array 127 37 47 517 Eye Uns gt gt gt y 256 array 1 2 3 4 dtype int8 gt gt gt y
161. e high max_release spillage Float desc Spillage hm3 def print_state self print Storage tRelease tInflows tSpillage ste format NE Join ter 26 for a in range ih print str_format format self storage self release self inflows self spillage print 79 if name _ main _ projectA Reservoir name Project A max_storage 30 max_release 100 0 hydraulic_head 60 efficiency 0 8 13 3 What are Traits 250 Python Scientific lecture notes Release 2011 1 state ReservoirState reservoir projectA state release 90 state inflows 0 state print_state print How do we update the current storage storage 10 A special trait allows to manage events and trigger function calls using the magic _xxxx_fired method from traits api import HasTraits Instance DelegatesTo Float Range Event from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s woe reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Range low min_storage high max_storage control attributes inflows Float de
162. e self inflows overflow new_storage self max_storage return max overflow 0 on_trait_change storage def print_state self print Storage tRelease tInflows tSpillage ste format Ne Join ewe fon al ain range an print str_format format self storage self release self inflows self spillage print 7 79 if name main projectA Reservoir name Project A max_storage 30 max_release 5 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 25 state release 4 state inflows 0 The patterns supported by the on_trait_change method and decorator are powerful The reader should look at the docstring of HasTraits on_trait_change for the details 13 3 7 Some more advanced traits The following example demonstrate the usage of the Enum and List traits from traits api import HasTraits Str Float Range Enum List from traitsui api import View Item class IrrigationArea HasTraits name Str surface Float desc Surface ha crop Enum Alfalfa Wheat Cotton class Reservoir HasTraits name Str max_storage Float 1le6 desc Maximal storage hm3 Max release Pilea 10 desce Maximal release Ims Siki 13 3 What are Traits 258 Python Scientific lecture notes Release 2011 1 head Float 10 desc Hydraulic head m fficiency Range 0 1 irrigated_areas List
163. e 2011 1 Good practices e Explicit variable names no need of a comment to explain what is in the variable e Style spaces after commas around etc A certain number of rules for writing beautiful code and more importantly using the same conven tions as everybody else are given in the Style Guide for Python Code and the Docstring Conventions page to manage help strings e Except some rare cases variable names and comments in English A lot of grid based or network based problems can also use broadcasting For instance if we want to compute the distance from the origin of points on a 10x10 grid we can do gt gt gt x y np arange 5 np arange 5 gt gt gt distance np sqrt x 2 y np newaxis s 2 gt gt gt distance array 0 7 We 2 pS poe l ale p A AMAS IDO Sn DA LOZA Iso Ail ES 2s pe 2200678 2c Ze4 27127 SIS A NAS 3 T 2 622766 37605551287 4224264 0697 5 Ie 4 z LOS 4747213595 5 7 5 6568542511 Or in color gt gt gt plt pcolor distance gt gt gt plescolorbar gt gt gt plt axis equal gt gt gt plt show lt again not needed in interactive Python Ol UY N Re Remark the numpy ogrid function allows to directly create vectors x and y of the previous example with two significant dimensions 3 2 Numerical operations on arrays 63 Python Scientific lecture notes Release 2011 1
164. e __iter__ method This allows us to transparently start the iteration over a sequence But if we already have the iterator we want to be able to use it in an for loop in the same way In order to achieve this iterators in addition to next are also required to have a method called ___ i ter___ which returns the iterator self Support for iteration is pervasive in Python all sequences and unordered containers in the standard library allow this The concept is also stretched to other things e g file objects support iteration over lines gt gt gt f open etc fstab gt gt gt f is E iter Ex e The file is an iterator itself and it s__iter__ method doesn t create a separate object only a single thread of sequential access is allowed 7 1 iterators generator expressions and generators 141 Python Scientific lecture notes Release 2011 1 7 1 2 Generator expressions A second way in which iterator objects are created is through generator expressions the basis for list compre hensions To increase clarity a generator expression must always be enclosed in parentheses or an expression If round parentheses are used then a generator iterator is created If rectangular parentheses are used the process is short circuited and we geta list gt gt gt i for i in nums lt generator object lt genexpr gt at 0x gt gt gt gt i for i in nums Rib a Sh gt gt gt list i for i in nums ite 2 Sl
165. e descriptor dt ype describes a single item in the array type scalar type of the data one of int8 int16 float64 et al fixed size str unicode void flexible size itemsize size of the data block byteorder byte order big endian gt little endian lt not applicable fields sub dtypes if it s a structured data type shape shape of the array if it s a sub array gt gt gt np dtype int type lt type nukpy mic 327 gt gt gt gt np dtype int itemsize 4 gt gt gt np dtype int byteorder et Example reading wav files The wav file header audio_fmt num_channels sample_rate byte_rate block_align bits_per_sample data_id data_size chunk_id RIFE chunk_size 4 byte unsigned little endian integer format WAVE fmt_id fmt fmt_size 4 byte unsigned little endian integer 2 byte unsigned little endian integer 2 byte unsigned little endian integer 4 byte unsigned little endian integer 4 byte unsigned little endian integer 2 byte unsigned little endian integer 2 byte unsigned little endian integer data 4 byte unsigned little endian integer e 44 byte block of raw data in the beginning of the file e followed by data_size bytes of actual sound data The wav file header as a Numpy structured data type gt gt gt wav_header_dtype np dtype Menu St A MENUS SA Mora SA MEE SN fme sizet SU AN audio Emed SI nun
166. e gaussian_filter im sigma 1 4 x n gt gt gt A gt gt gt mask im gt im mean astype np float gt gt gt A gt gt gt mask 0 1 im gt gt gt A gt gt gt img mask 0 2 np random randn mask shape gt gt gt A gt gt gt hist bin_edges np histogram img bins 60 gt gt gt bin_centers 0 5 bin_edges 1 bin_edges 1 gt gt gt A gt gt gt binary_img img gt 0 5 histogram 1 0 0 5 0 0 0 5 10 15 Automatic thresholding use Gaussian mixture model gt gt gt mask im gt im mean astype np float SSS gt gt gt mask 0 1 im SSS gt gt gt img mask 0 3 np random randn mask shape gt gt gt gt gt gt from scikits learn mixture import GMM gt gt gt classif GMM n_components 2 cvtype full gt gt gt classif fit img reshape img size 1 GMM cvtype full n_components 2 See gt gt gt classif means array TL Wetec as 1 0029 06 03 91 1 gt gt gt np sqrt classif covars ravel array I 0 350746317 0282253270 gt gt gt classif weights array lO 409897995 Ors OnO gt gt gt threshold np mean classif means gt gt gt binary_img img gt threshold 12 5 Feature extraction 237 Python Scientific lecture notes Release 2011 1 histogram Use mathematical morphology to clean up the result gt gt gt Remove small wh
167. e in double complex rz out nogil The Mandelbrot iteration Some points of note It s NOTx allowed to call any Python functions here The Ufunc loop runs with the Python Global Interpreter Lock released Hence the nogil And so all local variables must be declared with cdef Note also that this function receives xpointers to the data the traditional solution to passing complex variables around cdef double complex z z_in 0 cdef double complex c c_in 0 cdef int k the integer we use in the for loop Straightforward iteration for k in range 100 Ze a O if z realx 2 z 1mag 2 gt 1000 break Return the answer for this point Z out lol z Boilerplate Cython definitions You don t really need to read this part it just pulls in stuff from the Numpy C headers cdef extern from numpy arrayobject h void import_array ctypedef int npy_intp cdef enum NPY_TYPES NPY_CDOUBLE cdef extern from numpy ufuncobject h void import_ufunc ctypedef void PyUFuncGenericFunction char x npy_intpx npy_intp voidx object PyUFunc_FromFuncAndData PyUFuncGenericFunctionx func voidxx data chars types int ntypes int nin int nout int identity Chars name chars doc int C void PyUFunc_DD_D charx x npy_intpx npy_intpx void Required module initialization import_array import Urune 8 2 Universal functions 174 Python
168. eanst k 3 gt gt gt print kimeansi labels AO fh la lil OC t 2 2 2 2 2 gt gt gt print iris target 10 LOOM O OR Oe Ie ZO og Weed Virginica ol RE Ye o Versicolour Petal length Petal length Petal length Ground truth K means 3 clusters K means 8 clusters 16 3 Clustering grouping observations together 283 Python Scientific lecture notes Release 2011 1 Application to Image Compression Clustering can be seen as a way of choosing a small number of observations from the information For instance this can be used to posterize an image conversion of a continuous gradation of tone to several regions of fewer tones gt gt gt import scipy as sp gt gt gt lena sp lena astype np float32 gt gt gt X lena reshape 1 1 We need an n_sample n_feature array gt gt gt k_means cluster KMeans k 5 gt gt gt k_means fit X KMeans copy_x True init k means k 5 max_iter 300 n_init 10 random_state lt mtrand RandomState object at gt tol 0 0001 verbose 0 gt gt gt values k_means cluster_centers_ squeeze gt gt gt labels k_means labels_ gt gt gt lena_compressed np choose labels values gt gt gt lena_compressed shape lena shape The cloud of points spanned by the observations above is very flat in one direction so that one feature
169. ecorators 149 Python Scientific lecture notes Release 2011 1 lt property object at 0x gt gt gt gt A a a value In this example A a is an read only attribute It is also documented help A includes the docstring for attribute a taken from the getter method Defining a as a property allows it to be a calculated on the fly and has the side effect of making it read only because no setter is defined To have a setter and a getter two methods are required obviously Since Python 2 6 the following syntax is preferred class Rectangle object def __init__ self edge self edge edg property def area self Computed area Setting this updates the edge length to the proper valu now return self edgex x2 area setter def area self area self edge area x 0 5 The way that this works is that the property decorator replaces the getter method with a property object This object in turn has three methods getter setter and deleter which can be used as decorators Their job is to set the getter setter and deleter of the property object stored as attributes get fset and fdel The getter can be set like in the example above when creating the object When defining the setter we already have the property object under area and we add the setter to it by using the setter method All this happens when we are creating the class Afterwards when an instance of the class
170. ects Python_talks scipy_2009_tutorial source demo py 2 6 Reusing code scripts and modules 27 Python Scientific lecture notes Release 2011 1 Docstring A demo module Ta RS who demo In 6 whos Variable Type Data Info demo module lt module demo from demo py gt in 7 dir demo Quel 7 RATES y MECO A Pole e aT name_ _ package_ EI ins Line PELEA Aaa Jo In 8 demo demo __builtins__ demo imat demo __str__ demo __class__ demo __ name__ demo _ subclasshook__ demo __delattr__ demo new__ demo c demo dict__ demo __ package__ demo d demo __doc__ demo _ _reduce__ demo print a demo _ file _ demo _ reduce_ex demo print_b demo _ format___ demo __repr__ demo py demo __getattribute__ demo __setattr__ demo pyc demo __hash__ demo _ sizeof__ Importing objects from modules into the main namespace In 9 from demo import print_a print_b In 10 whos Variable Type Data Info demo module lt module demo from demo py gt print_a function lt function print_a at 0xb7421534 gt print p Comet ion lt function print b at 0xb74214c4 gt rn i s print 0 a Warning Module caching Modules are cached if you modify demo py and re import it in the old session you will get the old one Solution In 10 reload demo Loo Z O 2 6 4 _ main__ and module loading File demo2 py 2 6 Reusing code scripts
171. ed to power a loop the loop becomes very simple The code to initialise the state to decide if the loop is finished and to find the next value is extracted into a separate place This highlights the body of the loop the interesting part In addition it is possible to reuse the iterator code in other places 7 1 4 Bidirectional communication Each yield statement causes a value to be passed to the caller This is the reason for the introduction of generators by PEP 255 implemented in Python 2 2 But communication in the reverse direction is also useful One obvious way would be some external state either a global variable or a shared mutable object Direct communication is possible thanks to PEP 342 implemented in 2 5 Itis achieved by turning the previously boring yield statement into an expression When the generator resumes execution after a yield statement the caller can call a method on the generator object to either pass a value into the generator which then is returned by the yield statement or a different method to inject an exception into the generator 7 1 iterators generator expressions and generators 143 Python Scientific lecture notes Release 2011 1 The first of the new methods is send value whichis similar to next but passes value into the generator to be used for the value of the yield expression In fact g next and yg send None are equivalent The second of the new methods is throw type value
172. efficient construction of finite element matrices Examples create empty COO matrix gt gt gt mtx sps coo_matrix 3 4 dtype np int8 gt gt gt mtx todense maters GO O 0 01 PO OO 0 0 0 0 dtype int8 e create using data ij tuple gt gt gt cow mp array lO 3S 1 Oil ree oll ine creen On Sip ile 21 gt gt gt data np array 4 5 7 9 gt gt gt mtx sps coo_matrix data row col shape 4 4 gt gt gt mtx lt 4x4 sparse matrix of type lt type numpy int32 gt with 4 stored elements in COOrdinate format gt gt gt gt mtx todense matrix 4 0 A ls Ion Ve OF Chil Oe OO 0 0 g Smi O GO OWO e duplicates entries are summed together gt gt gt GOW Dp array LOr O Gp S 1 Or Oil See A ES i We 10 paso lea mpra rray il i il ail il al 1 gt gt gt mtx sps coo_matrix data row col shape 4 4 gt gt gt mtx todense mata ls or i 0l 0 2 0 O 0 0 0 O 0 0 0 1 e no slicing gt gt gt mtx 2 3 Traceback most recent call last File lt ipython console gt line 1 in lt module gt ypeError coo_matrix object is unsubscriptable 11 2 Storage Schemes 214 Python Scientific lecture notes Release 2011 1 Compressed Sparse Row Format CSR e row oriented three NumPy arrays indices indptr data indices is ar
173. en these in our examples on lists dictionaries strings etc 2 5 10 Exercises 2 5 Defining functions 24 Python Scientific lecture notes Release 2011 1 Exercise Quicksort Implement the quicksort algorithm as defined by wikipedia function quicksort array var list less greater if length array lt 2 return array select and remove a pivot value pivot from array for each x in array if x lt pivot 1 then append x to less else append x to greater return concatenate quicksort less pivot quicksort greater Exercise Fibonacci sequence Write a function that displays the n first terms of the Fibonacci sequence defined by eu 0 1 ul 1 e u_ nt2 u_ n 1 un 2 6 Reusing code scripts and modules For now we have typed all instructions in the interpreter For longer sets of instructions we need to change tack and write the code in text files using a text editor that we will call either scripts or modules Use your favorite text editor provided it offers syntax highlighting for Python or the editor that comes with the Scientific Python Suite you may be using e g Scite with Python x y 2 6 1 Scripts Let us first write a script that is a file with a sequence of instructions that are executed each time the script is called Instructions may be e g copied and pasted from the interpreter but take care to respect indentation rules The extension for Python files is
174. er to help if find the best possible resolution system For example if you know that it is a separable equations you can use keyword hint separable to force dsolve to resolve it as a separable equation In 6 dsolve sin x cos f x cos x sin f x f x diff x f x hint separable Out 6 log 1 sin f x 2 2 C1 log 1 sin x 2 2 15 5 3 Exercises 1 Solve the Bernoulli differential equation x f x diff x f x f x 2 Warning TODO correct this equation and convert to math directive 2 Solve the same equation using hint Bernoulli What do you observe 15 5 Linear Algebra 277 CHAPTER 16 scikit learn machine learning in Python author Fabian Pedregosa Gael Varoquaux Machine learning is a rapidly growing field with several machine learning frameworks available for Python ALN Y l rm my pS Py q VS machine learning py o e learning in Python Prerequisites e Numpy Scipy e Python e matplotlib e scikit learn http scikit learn org 278 Python Scientific lecture notes Release 2011 1 Chapters contents e Loading an example dataset page 279 Learning and Predicting page 280 e Classification page 280 k Nearest neighbors classifier page 280 Support vector machines SVMs for classification page 281 e Clustering grouping observations together page 283 K means clustering page 283 e Dimension Reduction with
175. er transform gt gt gt sig_fft np abs sample_freq gt freq 0 The resulting signal can be computed by the if ft function gt gt gt main_sig fftpack ifft sig_fft The result is shown on the Matplotlib figure Amplitude Time s 6 8 Fast Fourier transforms scipy fftpack 118 Python Scientific lecture notes Release 2011 1 6 9 Interpolation scipy interpolate The scipy interpolate is useful for fitting a function from experimental data and thus evaluating points where no measure exists The module is based on the FITPACK Fortran subroutines from the netlib project By imagining experimental data close to a sinus function gt gt gt measur d tim np linspace 0 1 10 gt gt gt noise np random random 10 2 1 x le 1 gt gt gt measures np sin 2 x np pi measured_time noise The interpld class can built a linear interpolation function gt gt gt from scipy interpolate import interpld gt gt gt linear_interp interpld measured_time measures Then the 1inear_interp instance needs to be evaluated on time of interest gt gt gt computed_time np linspace 0 1 50 gt gt gt linear_results linear_interp computed_time A cubic interpolation can also be selected by providing the kind optional keyword argument gt gt gt cubic_interp interpld measured_time measures kind cubic gt gt gt cubic_r
176. ers lists sourceforge net for plotting with matplotlib 92 CHAPTER 5 Matplotlib author Mike Miiller 5 1 Introduction matplotlib is probably the single most used Python package for 2D graphics It provides both a very quick way to visualize data from Python and publication quality figures in many formats We are going to explore matplotlib in interactive mode covering most common cases We also look at the class library which is provided with an object oriented interface 5 2 IPython and the pylab mode 5 2 1 IPython IPython is an enhanced interactive Python shell that has lots of interesting features including named inputs and outputs access to shell commands improved debugging and many more When we start it with the command line argument pylab pylab since IPython version 0 12 it allows interactive mat plot1lib sessions that has Matlab Mathematica like functionality 5 2 2 pylab py lab provides a procedural interface to the mat plot 1ib object oriented plotting library It is modeled closely after Matlab TM Therefore the majority of plotting commands in pylab has Matlab TM analogs with similar arguments Important commands are explained with interactive examples 5 3 Simple Plots Let s start an interactive session S ipython pylab This brings us to the Python prompt TENIA Oca An enhanced Interactive Python 2 gt Introduction to IPython s features o Smagic Information ab
177. eshape 6 2 2 gt gt gt mtx sps bsr_matrix data indices indptr shape 6 6 gt gt gt mtx todense meras A ip OW On 2 2 lp lp Of Op 27 Alle 0 0 0 0 3 31 0 0 0 0 3 31 a Ap Sr Sin O Sil 4 4 5 5 6 611 gt gt gt data array l J Lil De Ars ae ele 2a ld Sy alls 37 217 A A Alls Sy Sly Sola On Sl Sr S 11 2 3 Summary Table 11 1 Summary of storage schemes for matrix get fancy set fancy solvers note mat vector item get item set DIA sparsetools itera has data array specialized tive LIL via CSR yes yes yes yes itera arithmetics via CSR tive incremental construction DOK python yes one axis yes yes itera O 1 item access incremental only tive construction COO sparsetools itera has data array facilitates fast tive conversion CSR sparsetools yes yes slow any has data array fast row wise ops CSC sparsetools yes yes slow any has data array fast column wise ops BSR sparsetools spe has data array specialized cial ized 11 2 Storage Schemes 219 Python Scientific lecture notes Release 2011 1 11 3 Linear System Solvers e sparse matrix eigenvalue problem solvers live in scipy sparse linalg e the submodules dsolve direct factorization methods for solving linear systems isolve iterative methods for solving linear systems eigen sparse eigenvalue problem solvers e all solvers are a
178. esults cubic_interp computed_time The results are now gathered on the following Matplotlib figure e e measures linear interp cubic interp 139 0 2 0 4 0 6 0 8 1 0 scipy interpolate interp2d is similar to interpla but for 2 D arrays Note that for the interp family the computed time must stay within the measured time range See the summary exercise on Maximum wind speed prediction at the Sprog station page 126 for a more advance spline interpolation example 6 10 Optimization and fit scipy optimize Optimization is the problem of finding a numerical solution to a minimization or equality The scipy optimize module provides useful algorithms for function minimization scalar or multi dimensional curve fitting and root finding Example Minimizing a scalar function using different algorithms 6 9 Interpolation scipy interpolate 119 Python Scientific lecture notes Release 2011 1 Let s define the following function gt gt gt def f x return xxx2 10 np sin x and plot it gt gt gt x np arange 10 10 0 1 gt gt gt ple plot E x gt gt gt plt show 120 T T T 100 80 60 40 20 L 2079 5 0 5 10 This function has a global minimum around 1 3 and a local minimum around 3 8 6 10 1 Local convex optimization The general and efficient way to find a minimum for this function is to conduct a gradient descent startin
179. etc For more information on Mayavi http github enthought com mayavi mayavi See Also More in the Mayavi chapter page 263 3 1 6 Indexing and slicing The items of an array can be accessed and assigned to the same way as other Python sequences list tuple gt gt gt a np arange 10 gt gt gt a arsay lO ln 2 Sia Sin Or in Si Sl gt gt gt a 01 al21 al 1 0 274 2 Warning Indices begin at O like other Python sequences and C C In contrast in Fortran or Matlab indices begin at 1 For multidimensional arrays indexes are tuples of integers gt gt gt a np diag np arange 5 gt gt gt a can IO 0 0 0 Wil 10 A 0 0 Ol LO Or 2 O Ol On Ws 0 si O7 LO Of 0 0 211 gt gt gt ap 1 il gt gt gt a 2 1 10 third line second column gt gt gt a array OW OC UW OO Wil O lp On On Olly LO 1 2 0 Ol G S Ol 0p 0 41 gt gt gt a 1 ema LO y Ole O Only Note that e In 2D the first dimension corresponds to rows the second to columns e for multidimensional a a 0 is interpreted by taking all elements in the unspecified dimensions Slicing Arrays like other Python sequences can also be sliced gt gt gt a np arange 10 gt gt gt a array 0 i 2 8 Lo De Da Me Be SI gt gt gt a 2 9 3 start end step ase 3 81 3 1 The numpy array object 48 Python Scientific lec
180. ews of the array array 2 etc Adjust the shape of the array using reshape or flatten it with ravel Obtain a subset of the elements of an array and or modify their values with masks gt gt gt ala lt 0 0 e Know miscellaneous operations on arrays such as finding the mean or max array max array mean No need to retain everything but have the reflex to search in the documentation online docs help lookfor For advanced use master the indexing with arrays of integers as well as broadcasting Know more Numpy functions to handle various array operations 3 2 Numerical operations on arrays 74 Python Scientific lecture notes Release 2011 1 3 3 Moving on 3 3 1 More data types Casting Bigger type wins in mixed type operations s gt macia ll 2 21 dos array eee ol Assignment never changes the type gt gt gt ja gt mp ray il 2 31 gt gt gt a dtype dtype int64 gt gt gt a 0 1 9 gt gt gt a ay AZ 3 lt float is truncated to integer Forced casts gt gt gt da np array l r 1 2 6N gt gt gt b a astype int lt truncates to integer gt gt gt b arcay iL il ily Rounding AS O gt gt gt b np around a gt gt gt b array 1y 2al gt gt gt c np around a astype int gt gt gt c cy 2 A 21 still floating point Different data type sizes
181. execution contexts i e enable exploring inside function calls pdb US gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 35 2 34 noisy_img noisy_img 09 denoised_img local_mean noisy_img size size 36 l_var local_var noisy_img size size ipdb gt n gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 3 25 denoised_img local_mean noisy_img size size 5 36 l_var local_var noisy_img size size Si fOr i in range 3 e Step a few lines and explore the local variables ipdb gt n gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 37 36 l_var local_var noisy_img size size gt 37 for 1 an range 3 38 res noisy_img denoised_img Paba privar Seci 1579 SS sag BOM AOS SiS 50137 B63 A437 aa 346 262 4355 SNS Chloe AA S92 Ue 351 M35 S62 SS sas 2 5 Ee GS DAS SA AO ae CAS 203 OD MS WSS ISG sas Wess IAS ALSO ipdb gt print Ivar man 0 Oh dear nothing but integers and O variation Here is our bug we are doing integer arithmetic Raising exception on numerical errors When we run the wiener_filtering py file the following warnings are raised In 2 Srun wiener_filtering py Warning divide by zero encountered in divide Warning divide by zero encountered in divide Warning divide by zero encountered in divide We can tu
182. exercise e The first step will be to find the annual maxima by using numpy and plot them as a matplotlib bar figure 30 EH N N u o un Annual wind speed maxima m s HH o Year Figure 6 2 Solution Python source file The second step will be to use the Gumbell distribution on cumulative probabilities p_i defined as 1log log p_i for fitting a linear quantile function remember that you can define the degree of the UnivariateSpline Plotting the annual maxima versus the Gumbell distribution should give you the following figure e The last step will be to find 34 23 m s for the maximum wind speed occurring every 50 years 6 12 2 Non linear least squares curve fitting application to point extraction in topographical lidar data The goal of this exercise is to fit a model to some data The data used in this tutorial are lidar data and are described in details in the following introductory paragraph If you re impatient and want to practice now please skip it and go directly to Loading and visualization page 130 6 12 Summary exercises on scientific computing 128 Python Scientific lecture notes Release 2011 1 Gumbell cumulative probability Vig 34 23 m s n 20 25 30 35 40 45 Annual wind speed maxima m s Figure 6 3 Solution Python source file 6 12 Summary exercises on scientific computing 129 Python Scientific lecture notes Release 2011 1 Introduction Lidars systems are opt
183. f inflows overflow new_storage self max_storage return max overflow 0 def print_state self print Storage tRelease tInflows tSpillage stee format Ne o AZ Efor i an cange ANN print str_format format self storage self release self inflows self spillage print x 79 13 3 What are Traits 252 Python Scientific lecture notes Release 2011 1 if name main__ projectA Reservoir name Project A max_storage 30 max_release 5 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 25 state release 4 state inflows 0 state print_state Note Caching property Heavy computation or long running computation might be a problem when accessing a property where the inputs have not changed The cached_property decorator can be used to cache the value and only recompute them once invalidated Let s extend the TraitsUI introduction with the ReservoirState example from traits api import HasTraits Instance DelegatesTo Float Range Property from traitsui api import View Item Group VGroup from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example the release is considered in hm3 timestep and not in m3 s moe reservoir Instance Reservoir name DelegatesTo reservoir m
184. faster or to avoid this step algorithmic optimization Spending time on the rest of the code is useless 10 2 3 Line profiler The profiler is great it tells us which function takes most of the time but not where it is called For this we use the line_profiler in the source file we decorate a few functions that we want to inspect with profile no need to import it profile def test data np random random 5000 100 u S v linalg svd data pea np dot u 10 data results fastica pca T whiten False Then we run the script using the kernprof py program with switches and v kernprof py 1 v demo py Wrote profile results to demo py lprof Timer unit le 06 s File demo py Function test at line 5 Total time 14 2793 s Line IBLE SS Time Per Hit Time Line Contents 5 profile 6 def test 7 ll ANOS ALDOUS O 0 AL data np random random 5000 100 10 2 Profiling Python code 202 Python Scientific lecture notes Release 2011 1 8 1 14242163 14242163 0 9 67 u S v linalg svd data 9 i 102821028270 O pea np dot ui 10 rI data 10 il 7799 7799 0 0 dl results fastica pca T whiten False The SVD is taking all the time We need to optimise this line 10 3 Making code go faster Once we have identified the bottlenecks we need to make the corresponding code go faster 10 3 1 Algorithmic optimization The first thing to look for is
185. fault py we can run the script in gdb as follows E gdb python gdb run segfault py Starting program usr bin python segfault py Thread debugging using libthread_db enabled Program received signal SIGSEGV Segmentation fault _strided_byte_copy dst 0x8537478 360 343G outstrides 4 src 0x86c0690 lt Address 0x86c0690 out of bounds gt instrides 32 N 3 elsize 4 9 4 Debugging segmentation faults using gdb 197 N Python Scientific lecture notes Release 2011 1 at numpy core src multiarray ctors c 365 365 _FAST_MOVE Int32 gdb We get a segfault and gdb captures it for post mortem debugging in the C level stack not the Python call stack We can debug the C call stack using gdb s commands gdb up 1 0x004af4f5 in _copy_from_same_shape dest lt value optimized out gt src lt value optimized out gt myfunc 0x496780 lt _strided _byte_copy gt swap 0 lat numpy core src multiarray ctors c 748 1748 myfunc dit gt dataptr dest gt strides maxaxis As you can see right now we are in the C code of numpy We would like to know what is the Python code that triggers this segfault so we go up the stack until we hit the Python execution loop gdb up 8 Ox080ddd23 in call_function f Frame 0x8537lec for file home varoquau usr lib python2 6 site packages numpy core arrayprini A Pye DOn ceva lie S3750 3750 Python ceval c No such file or directory O EPOC ci
186. for getting started with Numpy and Scipy is addressed here To learn more about the language consider going through the excellent tutorial http docs python org tutorial Dedicated books are also available such as http diveintopython org python Python is a programming language as are C Fortran BASIC PHP etc Some specific features of Python are as follows an interpreted as opposed to compiled language Contrary to e g C or Fortran one does not compile Python code before executing it In addition Python can be used interactively many Python interpreters are available from which commands and scripts can be executed a free software released under an open source license Python can be used and distributed free of charge even for building commercial software multi platform Python is available for all major operating systems Windows Linux Unix MacOS X most likely your mobile phone OS etc a very readable language with clear non verbose syntax a language for which a large variety of high quality packages are available for various applications from web frameworks to scientific computing a language very easy to interface with other languages in particular C and C Some other features of the language are illustrated just below For example Python is an object oriented language with dynamic typing the same variable can contain objects of different types during the course of a program See http ww
187. for manipulating strings looking for patterns or format ting Due to lack of time this topic is not addressed here but the interested reader is referred to http docs python org library stdtypes html string methods and http docs python org library string html new string formatting e String substitution gt gt gt ARNES E sol a kloat si another string ss O As anio An integer 1 a float 0 100000 another string string gt gt gt i 102 gt gt gt filename processing_of_dataset_ 03d txt Si gt gt gt filename processing ot dataset TOZET Dictionaries A dictionary is basically an efficient table that maps keys to values It is an unordered container gt gt gt tel emmanuelle 5752 sebastian 5578 gt gt gt tel francis 5915 gt gt gt tel sebastian 5578 francis 5915 emmanuelle 5752 gt gt gt tel sebastian 5578 gt gt gt tel keys sebastian francis emmanuelle gt gt gt tel values 55787 59105 IDA gt gt gt francis in tel True It can be used to conveniently store and retrieve values associated with a name a string for a date a name etc See http docs python org tutorial datastructures html dictionaries for more information A dictionary can have keys resp values with different types 53 gt gt d S A 2 Se Pelor gt gt gt d Rava SO Or 2 More c
188. fs 1 is A amplitude 6 12 Summary exercises on scientific computing 131 Python Scientific lecture notes Release 2011 1 e coeffs 2 is y center e coeffs 3 is o width Initial solution An approximative initial solution that we can find from looking at the graph is for instance gt gt gt x0 np array l3 230 Lo Ll atype tloat Fit scipy optimize leastsq minimizes the sum of squares of the function given as an argument Basically the function to minimize is the residuals the difference between the data and the model gt gt gt def residuals coeffs y t return y model t coeffs So let s get our solution by calling scipy optimize leastsq with the following arguments e the function to minimize e an initial solution e the additional arguments to pass to the function gt gt gt from scipy optimize import leastsq gt gt gt x flag leastsq residuals x0 args waveform_1 t gt gt gt print x 2 70363341 27 82020742 15 47924562 SUIZA And visualize the solution gt gt gt ple plot t waveform lp t modellt x gt gt gt plt legend waveform model gt gt gt plt show Remark from scipy v0 8 and above you should rather use scipy optimize curve_fit which takes the model and the data as arguments so you don t need to define the residuals any more Going further e Try with a more complex waveform for instance data wa
189. g from a given initial point The BFGS algorithm is a good way of doing this gt gt gt optimize fmin_bfgs f 0 Optimization terminated successfully Current function value 7 945823 Iterations 5 Function evaluations 24 Gradient evaluations 8 array 1 30644003 This resolution takes 4 11ms on our computer The problem with this approach is that if the function has local minima is not convex the algorithm may find these local minima instead of the global minimum depending on the initial point If we don t know the neighborhood of the global minima to choose the initial point we need to resort to costlier global optimization 6 10 2 Global optimization To find the global minimum the simplest algorithm is the brute force algorithm in which the function is evaluated on each point of a given grid gt gt gt from scipy import optimize 22 gt peach 10 10 Weil 6 10 Optimization and fit scipy optimize 120 Python Scientific lecture notes Release 2011 1 gt gt gt optimize brute f grid array 1 30641113 This approach take 20 ms on our computer This simple algorithm becomes very slow as the size of the grid grows so you should use opt imize brent instead for scalar functions gt gt gt optimize brent f 1 3064400120612139 To find the local minimum let s add some constraints on the variable using optimize fminbound gt gt gt
190. ge representing a hand written digit gt gt gt digits datasets load_digits gt gt gt digits images shape GUYS tip E gt gt gt import pylab as pl gt gt gt pl imshow digits images 0 cmap pl cm gray_r lt matplotlib image AxesImage object at gt To use this dataset with the scikit we transform each 8x8 image into a vector of length 64 gt gt gt data digits images reshape digits images shape 0 W 16 1 1 Learning and Predicting Now that we ve got some data we would like to learn from it and predict on new one In scikit learn we learn from existing data by creating an estimator and calling its fit X Y method gt gt gt from scikits learn import svm gt gt gt clf svm LinearSVC gt gt gt cli fit rs data iris target learn form the data LinearSVC C 1 0 dual True fit_intercept True intercept_scaling 1 loss 12 multi_class False penalty 12 tol 0 0001 Once we have learned from the data we can use our model to predict the most likely outcome on unseen data cre he ola DO Sos dase Maa array 0 Note We can access the parameters of the model via its attributes ending with an underscore gt gt gt clf coef_ array MORSA 16 2 Classification 16 2 1 k Nearest neighbors classifier The simplest possible classifier is the nearest neighbor given a new observation take the label of the tr
191. ght 10 gt gt gt More denoising to the expense of fidelity to data gt gt gt tv_denoised tv_denoise noisy weight 50 The total variation filter tv_denoise is available in the scikits image doc http scikits image org docs dev api scikits image filter html tv denoise but for convenience we ve shipped it as a standalone module with this tutorial noisy TV denoising more TV denoising t F y i y 12 4 4 Mathematical morphology See http en wikipedia org wiki Mathematical_morphology Probe an image with a simple shape a structuring element and modify this image according to how the shape locally fits or misses the image Structuring element gt gt gt el ndimage generate_binary_structure 2 1 gt gt gt el array False True False True rue True False True False dtype bool gt gt gt el astype np int cia O A Olli 1 17 1y OF OO 12 4 Image filtering 233 Python Scientific lecture notes Release 2011 1 Erosion minimum filter Replace the value of a pixel by the minimal value covered by the structuring element gt gt gt a np zeros 7 7 dtype np int astype a dtype gt gt gt ndimage binary_erosion a structure np ones 5 5 astype a dtype gt gt gt alii 225 i gt gt gt a cra IO 0 0 07 0 0 01 OF We a ie O Wil On WF ip i 2 On Wil Or Or i ele Ore ON Os OD ie ip ib OF Ol da W
192. gning the return value to the name of the function This sound like more typing and it is and also the name of the decorated function dubbling as a temporary variable must be used at least three times which is prone to errors Nevertheless the example above is equivalent to def function 0 pass function decorator function 0 Decorators can be stacked the order of application is bottom to top or inside out The semantics are such that the originally defined function is used as an argument for the first decorator whatever is returned by the first decorator is used as an argument for the second decorator and whatever is returned by the last decorator is attached under the name of the original function The decorator syntax was chosen for its readability Since the decorator is specified before the header of the function it is obvious that its is not a part of the function body and its clear that it can only operate on the whole function Because the expression is prefixed with is stands out and is hard to miss in your face according to the PEP When more than one decorator is applied each one is placed on a separate line in an easy to read way 7 2 1 Replacing or tweaking the original object Decorators can either return the same function or class object or they can return a completely different object In the first case the decorator can exploit the fact that function and class objects are mutable an
193. gs return function args kwargs return _wrapper return _decorator gt gt gt replacing_decorator_with_args abc def function xargs xxkwargs prine inside Luneeilon jars kwargs ane return 14 defining the decorator doing decoration abc gt gt gt KUNCE wom IP ao inside wrapper iil C2 i inside aEbualciealere iil Ib J 14 The _wrapper function is defined to accept all positional and keyword arguments In general we cannot know what arguments the decorated function is supposed to accept so the wrapper function just passes everything to the wrapped function One unfortunate consequence is that the apparent argument list is misleading Compared to decorators defined as functions complex decorators defined as classes are simpler When an object is created the __ init __ method is only allowed to return None and the type of the created object cannot be changed This means that when a decorator is defined as a class it doesn t make much sense to use the argument less form the final decorated object would just be an instance of the decorating class returned by the constructor call which is not very useful Therefore it s enough to discuss class based decorators where arguments are given in the decorator expression and the decorator ___init__ method is used for decorator construction gt gt gt class decorator_class object def imat self arg this method is called in the decorator expression prin
194. gt gt gt al0 UA gt gt gt allij Tar gt gt gt a 1 oe Remember that negative indices correspond to counting from the right end Slicing gt gt gt a hello world gt gt gt a 3 6 3rd to 6th excluded elements elements 3 4 5 lo E gt gt gt a 2 10 2 Syntax a start stop step I Ike Ya gt gt gt a 3 every three characters from beginning to end Vlg seg dh Accents and special characters can also be handled in Unicode strings see http docs python org tutorial introduction html unicode strings A string is an immutable object and it is not possible to modify its contents One may however create new strings from the original one In 53 a hello world In 54 al2 z TypeError Traceback most recent call 2 2 Basic types 13 Python Scientific lecture notes Release 2011 1 last home gouillar travail sgr 2009 talks dakar_python cours gael essai source lt ipython console gt in lt module gt TypeError str object does not support item assignment in 55 are lacey aa A Out S51 Rezlo world in 56 a replace Ii 72r Out 56 hezzo worzd Strings have many useful methods such as a replace as seen above Remember the a object oriented notation and use tab completion or help str to search for new methods Note Python offers advanced possibilities
195. gt plt figure figsize 4 3 gt Plt potrie npesine mcanssqsdistance ge ty Me sorte SA gt gt gt plt xlabel r St gt gt gt plt ylabel r Asgrt llangle delta x 2 rangle gt gt gt plt show 16 0 50 100 150 200 3 2 Numerical operations on arrays 60 Python Scientific lecture notes Release 2011 1 The RMS distance grows as the square root of the time 3 2 4 Broadcasting e Basic operations on numpy arrays addition etc are elementwise e This works on arrays of the same size Nevertheless It s also possible to do operations on arrays of different sizes if Numpy can transform these arrays so that they all have the same size this conversion is called broadcasting The image below gives an example of broadcasting 8 8 sle YN NX NON Let s verify gt gt gt a gt gt gt a array np tile np arange 0 40 10 3 1 T 0 O 0 o oe ALO AN FA 2017 30 30 30 gt gt gt by np array Op i 2N gt gt gt a tb O al 10 11 12 20 217 221 S07 Sil S213 An useful trick gt gt gt a np arange 0 40 10 gt gt gt a shape 3 2 Numerical operations on arrays 61 Python Scientific lecture notes Release 2011 1 ara gt gt gt a al np newaxis adds a new axis gt 2D array gt gt gt a shape 27 y gt gt gt a array 0 WO 2011 SO gt gt
196. has been created the property object is special When the inter preter executes attribute access assignment or deletion the job is delegated to the methods of the property object To make everything crystal clear let s define a debug example gt gt gt class D object property def a self print getting 1 return 1 a setter def a self value print setting value a deleter def a self perne de Mecano gt gt gt D a lt property object at 0x gt gt gt gt D a CJSC lt funetton arat OA 45 gt gt gt gt D a fset lt tunet ron a at Ox gt gt gt gt D a fdel lt fuUnCE ron a at Ox gt gt gt d D f varies this is not the same gt gt gt id na getting 1 il gt gt gt d a 2 a Un eE uo 7 2 Decorators 150 Python Scientific lecture notes Release 2011 1 setting 2 gt gt gt del d a deleting gt gt gt d a getting 1 il Properties are a bit of a stretch for the decorator syntax One of the premises of the decorator syntax that the name is not duplicated is violated but nothing better has been invented so far It is just good style to use the same name for the getter setter and deleter methods Some newer examples include e functools lru_cache memoizes an arbitrary function maintaining a limited cache of argu ments answer pairs Python 3 2 e functools total_ordering is a class decorator which fills in missing
197. has value 1 Key c has value 1j Key b has value 1 2 2 4 Control Flow 19 Python Scientific lecture notes Release 2011 1 2 4 6 List Comprehensions In ie eee for i in range 4 Ome labels TO i Ly El Exercise Compute the decimals of Pi using the Wallis formula Co 4i a z i l 2 5 Defining functions 2 5 1 Function definition In 56 def test o print in test tuner son In Sve Cesco a cear PENSE on Warning Function blocks must be indented as other control flow blocks 2 5 2 Return statement Functions can optionally return values In 6 def disk_area radius return 3 14 x radius radius TO 8 disk areali S LOU Sil m0649999999999995 Note By default functions return None Note Note the syntax to define a function e the def keyword e is followed by the function s name then e the arguments of the function are given between brackets followed by a colon e the function body e and return object for optionally returning values 2 5 Defining functions 20 Python Scientific lecture notes Release 2011 1 2 5 3 Parameters Mandatory parameters positional arguments In 81 def double_it x Gari return x 2 In 82 double it 3 Ouse 6 ine 832 double tto TypeError Traceback most recent call last Users cburns src scipy2009 scipy_2009_tutorial source lt ipython console gt in lt
198. he closing example as a generator fcontextlib contextmanager def closing obj try yield obj finally obj close Let s rewrite the assert_raises example as a generator fcontextlib contextmanager def assert_raises type try yield except type return except Exception as value raise AssertionError wrong exception type else raise AssertionError exception expected Here we use a decorator to turn generator functions into context managers 7 3 Context managers 155 CHAPTER 8 Advanced Numpy author Pauli Virtanen Numpy is at the base of Python s scientific stack of tools Its purpose is simple implementing efficient operations on many items in a block of memory Understanding how it works in detail helps in making efficient use of its flexibility taking useful shortcuts and in building new work based on it This tutorial aims to cover e Anatomy of Numpy arrays and its consequences Tips and tricks e Universal functions what why and what to do if you want a new one e Integration with other tools Numpy offers several ways to wrap any data in an ndarray without unnecessary copies e Recently added features and what s in them for me PEP 3118 buffers generalized ufuncs Prerequisites e Numpy gt 1 2 preferably newer e Cython gt 0 12 for the Ufunc example e PIL used in a couple of examples e Source codes http pav iki fi
199. he first instruction in the body An invocation of a generator function creates a generator object adhering to the iterator protocol As with normal function invocations concurrent and recursive invocations are allowed When next is called the function is executed until the first yield Each encountered yield statement gives a value becomes the return value of next After executing the yield statement the execution of this function is suspended gt gt gt def f yield 1 yield 2 gt gt gt lt generator object f at 0x gt gt gt gt gen gt gt gt gen next al 7 1 iterators generator expressions and generators 142 Python Scientific lecture notes Release 2011 1 gt gt gt gen next 2 gt gt gt gen next Traceback most recent call last Poke lt stdine t Tane i anm lt module gt StopIteration Let s go over the life of the single invocation of the generator function gt gt gt def prine SS start T yield 3 prine O middle yield 4 print O e nasheed N gt gt gt gen f gt gt gt next gen ee 3 gt gt gt next gen SS middie 4 gt gt gt next gen SS EOS Traceback most recent call last StopIteration Contrary to a normal function where executing f would immediately cause the first print to be executed gen is assigned without executing any statements in the function body Only when gen nex
200. he memory layout of the data only strides gt gt gt x strides 42 gt gt gt y strides ae gt gt gt str x data x01 x02 x03 x04 gt gt gt str y data PXx01 x0S x02 x04 e the results are different when interpreted as 2 of int16 e copy creates new arrays in the C order by default Slicing with integers e Everything can be represented by changing only shape strides and possibly adjusting the data pointer e Never makes copies of the data 8 1 Life of ndarray 164 Python Scientific lecture notes Release 2011 1 gt gt gt X np array il 2 3 4 5 61 dtype np intz gt gt gt y ala gt gt gt y arsav iG 8 4 S 2p LI gt gt gt y strides ay ll gt gt gt y x 2 gt gt gt y __array_interface__ data 0 x __array_interface__ data 0 8 gt gt gt x np zeros 10 10 10 dtype np float gt gt gt x strides 800 80 8 32 gt a Sd strides LOOP 2407 22 Example fake dimensions with strides Stride manipulation gt gt gt from numpy lib stride tricks import as_strided gt gt gt help as_strided as_strided x shape None strides None Make an ndarray from the given array with the given shape and strides gt gt gt x np array 1 2 3 4 dtype np int16 gt gt gt as_strided x strides 2x2 shape 2 array 1 3 dtype int16 gt gt gt
201. he random process s PDF probability density function gt gt gt a np random normal size 1000 gt gt gt bins np arange 4 5 gt gt gt bins arcay ll or 2 le OF ils Biy SF 41 gt gt gt histogram np histogram a bins bins normed True 0 gt gt gt bins 0 5 bins 1 bins 1 gt gt gt bins tan MES 2 5 Lo SUD Wed Law 2557 Sol 6 4 Special functions scipy special 112 Python Scientific lecture notes Release 2011 1 gt gt gt from scipy import stats gt gt gt b stats norm pdf bins Tre l paolo bins ns to gal za 2l php lomas 19 0 45 T T 0 40 0 35 0 30 0 25 0 20 0 15 0 10 0 05 ore 4 2 0 2 4 6 If we know that the random process belongs to a given family of random processes such as normal processes we can do a maximum likelihood fit of the observations to estimate the parameters of the underlying distribution Here we fit a normal process to the observed data gt gt gt loc std stats norm fit a gt gt gt loc 0 003738964114102075 gt gt gt std 0 97450996668871193 6 5 2 Percentiles The median is the value with half of the observations below and half above gt gt gt np median a OSOS ASS 10292782019 It is also called the percentile 50 because 50 of the observation are below it gt gt gt stats scoreatpercentile a 50 0 007164557029278251
202. he with block is exited Since closing files is such a common operation the support for this is already present in the file class Ithas an___exit__ method which calls close and can be used as a context manager itself gt gt gt with open tmp file a as f f write more contents n The common use for try finally is releasing resources Various different cases are implemented similarly in the __enter__ phase the resource is acquired in the __exit__ phase it is released and the exception if thrown is propagated As with files there s often a natural operation to perform after the object has been used and it is most convenient to have the support built in With each release Python provides support in more places e all file like objects file automatically closed fileinput tempfile py gt 3 2 bz2 BZ2File gzip GzipFile tarfile TarFile zipfile ZipFile ftplib nntplib close connection py gt 3 2 or 3 3 e locks multiprocessing RLock lock and unlock multiprocessing Semaphore memoryview automatically release py gt 3 2 and 2 7 e decimal localcontext modify precision of computations temporarily e _winreg PyHKEY open and close hive key e warnings catch_warnings kill warnings temporarily e contextlib closing the same as the example above call close e parallel programming concurrent futures ThreadPoolExecutor invoke in parallel then kil
203. ical rangefinders that analyze property of scattered light to measure distances Most of them emit a short light impulsion towards a target and record the reflected signal This signal is then processed to extract the distance between the lidar system and the target Topographical lidar systems are such systems embedded in airborne platforms They measure distances between the platform and the Earth so as to deliver information on the Earth s topography see for more details In this tutorial the goal is to analyze the waveform recorded by the lidar system Such a signal contains peaks whose center and amplitude permit to compute the position and some characteristics of the hit target When the footprint of the laser beam is around 1m on the Earth surface the beam can hit multiple targets during the two way propagation for example the ground and the top of a tree or building The sum of the contributions of each target hit by the laser beam then produces a complex signal with multiple peaks each one containing information about one target One state of the art method to extract information from these data is to decompose them in a sum of Gaussian functions where each function represents the contribution of a target hit by the laser beam Therefore we use the scipy optimize module to fit a waveform to one or a sum of Gaussian functions Loading and visualization Load the first waveform using gt gt gt import numpy as np gt
204. ides 0 and 1 8 3 Interoperability features 181 Python Scientific lecture notes Release 2011 1 view gt shape 0 and 1 and view gt ndim Note if correct interpretation requires strides or shape you need to check flags for what was requested and raise appropriate errors The same if the buffer is not readable e view gt obj PyObject self Py_INCREF self return 0 static void myobject_releasebuffer PyMemoryViewObject self Py_buffer view if view gt shape free view gt shape view gt shape NULL if view gt strides free view gt strides view gt strides NULL Sample implementation of a custom data type that exposes an array interface And does nothing else Requires Python gt 3 1 al x Mini exercises make the array strided Change the data type define PY_SSIZE_T_CLEAN include lt Python h gt include structmember h typedef struct PyObject_HEAD int buffer 4 PyMyObjectObject stat ete myobject_getbuffer PyObject obj Py_buffer view int flags PyMyObjectObject x self PyMyObjectObjectx obj x Called when something requests that a MyObject type object provides a buffer interface x view gt buf self gt buffer 8 3 Interoperability features 182 Python Scientific lecture notes Release 2011 1
205. ific computing web server management serial port access etc Free and open source software widely spread with a vibrant community e Drawbacks less pleasant development environment than for example Matlab More geek oriented Not all the algorithms that can be found in more specialized software or toolboxes 1 1 Why Python 4 Python Scientific lecture notes Release 2011 1 1 2 Scientific Python building blocks e Python a generic and modern computing language Python language data types string int flow control data collections lists dictionaries pat terns etc Modules of the standard library A large number of specialized modules or applications written in Python web protocols web frame work etc and scientific computing Development tools automatic tests documentation generation Shell Konsole lt 2 gt EX s IPython uses TAB for name completion Hit TAB after the dot s s capitalize s expandtabs s s ljust s rjust s swapcase Functions are automatically parenthes saving typing TAB tab tab ab for name completion Hit tab after the dot Using as the first character the quotes are also automatic eplace tab TAB ace tab TAB ython uses TAB for name completion Hit TAB after the dot e IPython an advanced Python shell http ipython scipy org moin e Numpy provides powerful numerical arrays objects and routines to m
206. in the way shown below z char input_1 char args 0 char input_2 char args 1 char output charx args 2 int i for G 0 lt dimensions lOl Fri i xoutput elementwise_function input_1 input_2 input_1 steps 0 8 2 Universal functions 169 Python Scientific lecture notes Release 2011 1 input_2 steps 1 output steps 2 2 The Numpy part built by char types 3 types 0 NPY_BYTE fx BY De of Ems t Input arg s types 1 NPY_BYTE type of second input arg types 2 NPY_BYTE A type Of third TAUG arg 47 PyObject python_ufunc PyUFunc_FromFuncAndData urune loop NULL types 1 ntypes o s numa nips A 1 num_outputs x identity_element name docstring unused e A ufunc can also support multiple different input output type combinations can be made slightly easier 3 ufunc_loop is of very generic form and Numpy provides pre made ones PyUfunc_f _f float elementwise_func float input_1 PyUfunc_ff_ ffloat elementwise_func float input_1 float input_2 PyUfunc_d_d doub lementwise_func double input_1 PyUfunc_dd_ ddoubl lementwise_func double input_1 double input_2 PyUfunc_D_D elementwise_func npy_cdouble x input npy_cdoublex output PyUfunc_DD_Delementwise_func npy_cdouble inl npy_cdouble in2 npy_cdoublex out e Only elementwise_func needs to be sup
207. is is useful if for example one does not know the exact name of a function In 3 import numpy as np In 4 Spsearch np diagx np diag np diagflat np diagonal e numpy lookfor looks for keywords inside the docstrings of specified modules In 45 numpy lookfor convolution Search results for convolution numpy convolve Returns the discrete linear convolution of two one dimensional sequences numpy bartlett Return the Bartlett window numpy correlate Discrete linear correlation of two 1 dimensional sequences In 46 numpy lookfor remove module os Search results for remove os remove remove path os removedirs removedirs path OS emdr rmdir path os unlink unlink path os walk Directory tree generator e If everything listed above fails and Google doesn t have the answer don t despair Write to the mailing list suited to your problem you should have a quick answer if you describe your problem well Experts on scientific python often give very enlightening explanations on the mailing list Numpy discussion numpy discussion Oscipy org all about numpy arrays manipulating them in dexation questions etc 91 Python Scientific lecture notes Release 2011 1 SciPy Users List scipy user scipy org scientific computing with Python high level data process ing in particular with the scipy package matplotlib us
208. istical information page 230 Geometrical transformations page 231 e Image filtering page 231 Blurring smoothing page 231 Sharpening page 232 Denoising page 232 Mathematical morphology page 233 e Feature extraction page 236 Edge detection page 236 Segmentation page 237 e Measuring objects properties page 239 12 1 Opening and writing to image files Writing an array to a file from scipy import misc 1 misc lena misc imsave lena png 1 uses the Image module PIL Creating a numpy array from an image file gt gt gt lena misc imread lena png gt gt gt type lena lt type numpy ndarray gt gt gt gt lena shape lena dtype Gil 212 dtype Y Tame dtype is uint8 for 8 bit images 0 255 Opening raw files camera 3 D images 12 1 Opening and writing to image files 227 Python Scientific lecture notes Release 2011 1 gt gt gt l tofile lena raw Create raw file gt gt gt lena_from_raw np fromfile lena raw dtype np int64 gt gt gt lena_from_raw shape 202 14AT gt gt gt lena_from_raw shape 512 512 gt gt gt import os gt gt gt os remove lena raw Need to know the shape and dtype of the image how to separate data bytes For large data use np memmap for memory mapping gt gt gt lena_memmap np memmap lena ra
209. ite regions gt gt gt open_img ndimage binary_opening binary_img gt gt gt Remove small black hole gt gt gt close_img ndimage binary_closing open_img Exercise Check that reconstruction operations erosion propagation produce a better result than opening closing gt gt gt eroded_img ndimage binary_erosion binary_img gt gt gt reconstruct_img ndimage binary_propagation eroded_img gt gt gt mask binary_img gt gt gt tmp np logical_not reconstruct_img gt gt gt eroded_tmp ndimage binary_erosion tmp gt gt gt reconstruct_final gt gt gt np logical_not ndimage binary_propagation eroded_tmp mask tmp gt gt gt np abs mask close_img mean 0 014678955078125 gt gt gt np abs mask reconstruct_final mean 0 0042572021484375 Exercise Check how a first denoising step median filter total variation modifies the histogram and check that the resulting histogram based segmentation is more accurate e Graph based segmentation use spatial information gt gt gt from scikits learn feature_extraction import image gt gt gt from scikits learn cluster import spectral_clustering gt gt gt gt gt gt 1 100 a gt 3 Sin dic Ss 1m 1N SSS gt gt gt centerl gt gt gt center2 40 50 gt gt gt center3 gt gt gt center4 12 5 Feature extraction 238 Python Scientific lecture notes Release 2
210. itial square The opposite would occur if the closing step was performed before the opening For gray valued images eroding resp dilating amounts to replacing a pixel by the minimal resp maximal value among pixels covered by the structuring element centered on the pixel of interest gt gt gt a np zeros 7 7 dtype np int gt gt gt valle 67 0 16l gt gt gt al4 4 2 gt gt gt ad array C TOn OW Ol O 34 Sr Ops Shas Si 5 al2 3 6 11 Image processing scipy ndimage 124 Python Scientific lecture notes Release 2011 1 de Se Se 20 Sp sip Ole Oy Sy Bp Sp Ae Se Wil On Sir Sr Sh Ss 3r Ol OF or OF 107 O OR Oh gt gt gt ndimage grey_erosion a size 3 3 cia TO 0 0 0 0 O Ol O O 0 0 07 Oe Ol De Of If ip O Ol GC UW a a i OG Ol Or 0 3h 27 2r On Ol OP 0 0 0 10 107 01 O DO 0 0 0 0 0115 6 11 4 Measurements on images Let us first generate a nice synthetic binary image gt gt gt x y np indices 100 100 2 olg A A AOS a nd a eo 2 gt gt gt mask sig gt 1 Now we look for various information about the objects in the image gt gt gt labels nb ndimage label mask gt gt gt nb 8 gt gt gt areas ndimage sum mask labels xrange 1 labels max 1 gt gt gt areas NO MN 424107 278 0 4590 H00 5A904240 gt gt gt maxima ndimage maximum sig labels xrange 1 labels max 1 gt gt gt
211. l integration scipy integrate page 115 Fast Fourier transforms scipy fftpack page 117 Interpolation scipy interpolate page 119 Optimization and fit scipy optimize page 119 Local convex optimization page 120 Global optimization page 120 Image processing scipy ndimage page 121 Geometrical transformations on images page 121 Image filtering page 122 Mathematical morphology page 122 Measurements on images page 125 Summary exercises on scientific computing page 126 109 Python Scientific lecture notes Release 2011 1 Warning This tutorial is far from an introduction to numerical computing As enumerating the different submodules and functions in scipy would be very boring we concentrate instead on a few examples to give a general idea of how to use scipy for scientific computing To begin with gt gt gt import numpy as np gt gt gt import scipy scipy is mainly composed of task specific sub modules cluster fftpack integrate interpolate 10 linalg maxentropy ndimage odr optimize signal sparse spatial special stats Vector quantization Kmeans Fourier transform Integration routines Interpolation Data input and output Linear algebra routines Routines for fitting maximum entropy models n dimensional image package Orthogonal distance regression Optimization Signal processing Sparse matrices Spatial data structures and algo
212. l of the same shape giving the positions of the vertices of the surface The connectivity between these points is implied by the connectivity on the arrays For simple structures such as orthogonal grids prefer the surf function as it will create more efficient data structures Keyword arguments color the color of the vtk object Overides the colormap if any when specified This is specified as a triplet of float ranging from 0 to 1 eg 1 1 1 for white colormap type of colormap to use extent xmin xmax ymin ymax zmin zmax Default is the x y z arrays extents Use this to change the extent of the object created figure Figure to populate line_width The with of the lines if any used Must be a float Default 2 0 mask boolean mask array to suppress some data points mask_points If supplied only one out of mask_points data point is displayed This option is usefull to reduce the number of points displayed on large datasets Must be an integer or None mode the mode of the glyphs Must be 2darrow or 2dcircle or 2dcross or 2ddash or 2ddiamond or 2dhooked_arrow or 2dsquare or 2dthick_arrow or 2dthick_cross or 2dtriangle or 2dvertex or arrow or cone or cube or cylinder or point or sphere Default sphere name the name of the vtk object created representation the representation type used for the surface Must be surface or wirefr
213. l thread pool py gt 3 2 concurrent futures ProcessPoolExecutor invoke in parallel then kill process pool py gt 3 2 nogil solve the GIL problem temporarily cython only 7 3 1 Catching exceptions When an exception is thrown in the with block it is passed as arguments to ___exit__ Three arguments are used the same as returned by sys exc_info type value traceback When no exception is thrown None is used for all three arguments The context manager can swallow the exception by returning a true value from ___exit__ Exceptions can be easily ignored because if __exit__ doesn t use return and just falls of the end None is returned a false value and therefore the exception is rethrown after __e x it__ is finished The ability to catch exceptions opens interesting possibilities A classic example comes from unit tests we want to make sure that some code throws the right kind of exception class assert_raises object based on pytest and unittest TestCase def __init_ self type self type typ def _ _enter__ self pass def __exit_ self type value traceback if type is None raise AssertionError exception expected 7 3 Context managers 154 Python Scientific lecture notes Release 2011 1 if issubclass type self type return True swallow th xpected exception raise AssertionError wrong exception type with assert_raise
214. lgebraic manipulations bornas e Pe ee ee eS 13 3 Calculus 5 dra 2 Me a bad A EE EERE Oe eB eA ee 15 4 Equation solving sees cece ee eR RAO HERA RE eR OES AS 15 5 Linear Algebra es e443 cet te ee AEE EEE BARS Se ERE a rd 16 scikit learn machine learning in Python 16 1 Loading anexample dataset 0 ee eR Se ae ee A 16 2 Classification oe wi ee ae ee a BOAO Rew Bee ee be A 16 3 Clustering grouping observations together acro e ee ee ee ee 16 4 Dimension Reduction with Principal Component Analysis 16 5 Putting tall together fac recopmition c s s i mpane p idos sa td RA ees 16 6 Linear model from regression t sparsity lt s es 300 sms ses 16 7 Model selection choosing estimators and their parameters o o Index 263 263 264 267 270 271 272 273 274 275 276 278 279 280 283 284 285 287 288 290 Python Scientific lecture notes Release 2011 1 Contents 1 Part I Getting started with Python for science CHAPTER 1 Scientific computing with tools and workflow 1 1 authors Fernando Perez Emmanuelle Gouillart Ga l Varoquaux Why Python 1 1 1 The scientist s needs Get data simulation experiment control Manipulate and process data Visualize results to understand what we are doing Communicate on results produce figures for reports or publications write presentations 1 1 2 Specifications Rich c
215. lib ticker Locator You can make your own locator deriving from it Handling dates as ticks can be especially tricky Therefore matplotlib provides special locators in matplotlib dates Class Description MinuteLocator locate minutes HourLocator locate hours DayLocator locate specified days of the month WeekdayLocator locate days of the week e g MO TU MonthLocator locate months e g 10 for October YearLocator locate years that are multiples of base RRuleLocator locate using a matplotlib dates rrule Similarly to locators there are formatters Class Description NullFormatter no labels on the ticks FixedFormatter set the strings manually for the labels FuncFormatter user defined function sets the labels FormatStrFormatter use a sprintf format string IndexFormatter cycle through fixed strings by tick position ScalarFormatter default formatter for scalars autopick the fmt string LogFormatter formatter for log axes DateFormatter use an strftime string to format the date All of these formatters derive from the base class matplotlib ticker Formatter You can make your own formatter deriving from it Now we set our major locator to 2 and the minor locator to 1 We also format the numbers as decimals using the FormatStrFormatter In 5 major_locator MultipleLocator 2 In 6 major_formatter FormatStrFormatter In 7 minor_locator MultipleLocator 1 EA in
216. lizatig Chaco f Enable Kiva VERA AN a E ot i PRA al In this tutorial we will focus on Traits 13 2 Example Throughout this tutorial we will use an example based on a water resource management simple case We will try to model a dam and reservoir system The reservoir and the dams do have a set of parameters e Name e Minimal and maximal capacity of the reservoir hm3 e Height and length of the dam m e Catchment area km2 e Hydraulic head m 13 1 Introduction 245 Python Scientific lecture notes Release 2011 1 e Power of the turbines MW e Minimal and maximal release m3 s e Efficiency of the turbines The reservoir has a known behaviour One part is related to the energy production based on the water released A simple formula for approximating electric power production at a hydroelectric plant is P phrgk where e Pis Power in watts e pis the density of water 1000 kg m3 e his height in meters e r is flow rate in cubic meters per second e gis acceleration due to gravity of 9 8 m s2 e kis a coefficient of efficiency ranging from 0 to 1 Annual electric energy production depends on the available water supply In some installations the water flow rate can vary by a factor of 10 1 over the course of a year The second part of the behaviour is the state of the storage that depends on controlled and uncontrolled parameters storager41 storage inflows release
217. lt warning for NumPy users the multiplication with is the matrix multiplication dot product not part of NumPy passing a Sparse matrix object to NumPy functions expecting ndarray matrix does not work 11 2 1 Common Methods e all scipy sparse classes are subclasses of spmatrix default implementation of arithmetic operations x always converts to CSR subclasses override for efficiency shape data type set get nonzero indices format conversion interaction with NumPy toarray todense 11 2 Storage Schemes 208 Python Scientific lecture notes Release 2011 1 e attributes mtx A same as mtx toarray mtx T transpose same as mtx transpose mtx H Hermitian conjugate transpose mtx real real part of complex matrix mtx imag imaginary part of complex matrix mtx size the number of nonzeros same as self getnnz mtx shape the number of rows and columns tuple e data usually stored in NumPy arrays 11 2 2 Sparse Matrix Classes Diagonal Format DIA very simple scheme diagonals in dense NumPy array of shape n_diag length fixed length gt waste space a bit when far from main diagonal subclass of _data_mat rix sparse matrix classes with data attribute offset for each diagonal Ois the main diagonal negative offset below positive offset above fast matrix vector sparsetools fast and easy item
218. lt type 4 repeat 3 Zp offsets 3 diagonals 1 offsets axis 0 shape 4 4 Hump y sunt S210 gt 7 in DIAgonal format gt shape 4 4 e explanation with a scheme Offset r OW o 10 8 Md Bh 2 ol CES T e matrix vector multiplication 11 2 Storage Schemes 210 Python Scientific lecture notes Release 2011 1 gt gt gt vec np ones 4 gt gt gt vec annava he IEF IEY EETA gt gt gt mtx vec arre lea IRONT Dep MS gt gt gt mtx toarray vec array i le O Wolly See ESS o A le D Ous Gay Sap 0 1 ES On Hace 4 11 List of Lists Format LIL e row based linked list each row is a Python list sorted of column indices of non zero elements rows stored in a NumPy array dt ype np object non zero values data stored analogously e efficient for constructing sparse matrices incrementally e constructor accepts dense matrix array sparse matrix shape tuple create empty matrix e flexible slicing changing sparsity structure is efficient e slow arithmetics slow column slicing due to being row based e use when sparsity pattern is not known apriori or changes example reading a sparse matrix from a text file Examples e create an empty LIL matrix gt gt gt mtx sps lil_matrix 4 5 e prepare random data gt gt gt from numpy random import rand gt g
219. mat as well as any legal html color name The one letter abbreviations are very handy for quick work With following you can get quite a few things done 5 4 Properties 96 Python Scientific lecture notes Release 2011 1 Abbreviation Color sK Banm o blue green red cyan magenta yellow black white Other objects also have properties The following table list the text properties Property Value alpha alpha transparency on 0 1 scale color matplotlib color arg family set the font family eg sans serif cursive fantasy fontangle the font slant one of normal italic oblique horizontalalignment left right or center multialignment left right or center only for multiline strings name font name eg Sans Courier Helvetica position x y location variant font variant eg normal small caps rotation angle in degrees for rotated text size fontsize in points eg 8 10 12 style font style one of normal italic oblique text set the text string itself verticalalignment top bottom or center weight font weight e g normal bold heavy light 1 Apply different line styles to a plot Change line color and thickness as well as the size and the kind of the marker Experiment with different styles 5 5 Text We ve already used some commands to add text to our figure x label ylabel and title There are two functions to put text at a defined position text adds the text with
220. module gt TypeError double_it takes exactly 1 argument 0 given Optional parameters keyword or named arguments In 84 def double_it x 2 AE return x x 2 To 85 lobo late ounle sie Y In 86 doble it 3 out Ss 6 Keyword arguments allow you to specify default values Warning Default values are evaluated when the function is defined not when it is called In 124 bigx 10 In 125 def double_it x bigx erences return x 2 In 126 bigx le9 Now really big In 128 double_it Out 1281 20 More involved example implementing python s slicing In 98 def slicer seq start None stop None step None A Mtra tement basie pyrEhon SICrnge I AS return seq start stop step In 101 rhyme one fish two fish red fish blue fish split In 102 rhyme outi ozik a O Se as Ghee es a In 103 slicer rhyme OE MUNS Mone Llehy y Eno tits ren Lust ole rs In 104 slicer rhyme step 2 Qut 104 hs ene Ewo red blue 2 5 Defining functions 21 Python Scientific lecture notes Release 2011 1 In 105 slicer rhyme 1 step 2 QUO a Eush sessile pe eile Ie ee een In 106 slicer rhyme start 1 stop 4 step 2 Oe AOC is seein seals The order of the keyword arguments does not matter In 107 slicer rhyme step 2 start 1 stop 4 Cu
221. mple resample a signal to n points using FFT t np linspace 0 5 100 X np sin t pl plot t x linewidth 3 pli plot Ge 2 7 signal resample x 50 Tkon 6 3 Signal processing scipy signal 111 Python Scientific lecture notes Release 2011 1 1 5 1 0 0 5 0 0 0 5 1 0 1 55 Notice how on the side of the window the resampling is less accurate and has a rippling effect e Signal has many window function hamming bartlett blackman e Signal has filtering Gaussian median filter Wiener but we will discuss this in the image paragraph 6 4 Special functions scipy special Special functions are transcendal functions The docstring of the module is well written and we will not list them Frequently used ones are e Bessel function such as special jn nth integer order Bessel function e Elliptic function special ellipj for the Jacobian elliptic function e Gamma function special gamma also note special gammaln which will give the log of Gamma to a higher numerical precision e Erf the area under a Gaussian curve special erf 6 5 Statistics and random numbers scipy stats The module scipy stats contains statistical tools and probabilistic description of random processes Random number generators for various random process can be found in numpy random 6 5 1 Histogram and probability density function Given observations of a random process their histogram is an estimator of t
222. n wireframe line_width 1 color 0 5 0 5 0 5 lt enthought mayavi modules surface Surface object at 0xdd6a71c gt 14 3 3 Decorations Different items can be added to the figure to carry extra information such as a colorbar or a title tne 9s Out In Ou In Ou In Ou SE 10 10 pias aa B2 T21 mlab colorbar Out 7 orientation vertical lt tvtk_classes scalar_bar_actor ScalarBarActor object at 0xd897f8c gt mlab title polar mesh lt enthought mayavi modules text Text object at Oxd8ed38c gt mlab outline Out 7 lt enthought mayavi modules outline Outline object at Oxdd21b6c gt mlab axes Out 7 lt enthought mayavi modules axes Axes object at Oxd2e4bcc gt 14 3 Figures and decorations 269 Python Scientific lecture notes Release 2011 1 1 60e 16 0 841 1 00 0 694 1 00 0 546 0 399 0 251 0 103 sue Ei 0 192 Warning extent If we specified extents for a plotting object mlab outline and mlab axes don t get them by default 14 4 Interaction The quicket way to create beautiful visualization with Mayavi is probably to interactivly tweak the various settings Click on the Mayavi button in the scene and you can control properties of objects with dialogs Mayavi pipeline Mayavi Sce sgt acl Ged bel ad Pipeline gt Scalar LUT
223. n also be added to classes using the on_trait_change decorator from traits api import HasTraits from traits api import Property Instance DelegatesTo Float on_trait_change Range from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage For the simplicity of the example hm3 timestep and not in m3 s won the release is considered in reservoir Instance Reservoir max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storag control attributes inflows Float desc Inflows release Range low min_release Property depends_on inflows release hm3 high max_release 13 3 What are Traits 257 Python Scientific lecture notes Release 2011 1 spillage Property desc Spillage hm3 depends_on storage inflows release Private traits es detest eee eee Heese Hees eases eed ds thes d tg set _storage Float Traits property implementation th dteetet RA RADAR thet RARA RAARARA def _get_storage self new_storage self _storage self release self inflows return min new_storage self max_storage def _set_storage self storage_value self _storage storage_value def _get_spillage self new_storage self _storage self releas
224. name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 turbine turbine print installed capacity is initialised with turbine power print reservoir installed_capacity princ eo print updating the turbine power updates the installed capacity turbine power 10 print reservoir installed_capacity PELOS print setting the installed capacity breaks the link between turbine power print and the installed capacity trait reservoir installed_capacity 8 print turbine power reservoir installed_capacity 13 3 6 Notification Traits implements a Listener pattern For each trait a list of static and dynamic listeners can be fed with callbacks When the trait does change all the listeners are called Static listeners are defined using the _XXX_changed magic methods from traits api import HasTraits Instance DelegatesTo Float Range from reservoir import Reservoir class ReservoirState HasTraits Keeps track of the reservoir state given the initial storage woe reservoir Instance Reservoir min_storage Float max_storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir 13 3 What are Traits 255 Python Scientific lecture notes Release 2011 1 state attributes storage Range low min_storage high max_storage control attributes inflows Float desc
225. names gt gt gt samples sensor_code array CAA VERAS OTAU p MALA INIA NTO I dtype S4 gt gt gt samples value array i ORSS Orel ORS Ol Sioy A Osh gt gt gt samples 0 CALEA 130 O35 gt gt gt samples 0 sensor_code TAU gt gt gt samples 0 C TAU On 05 Multiple fields at once gt gt gt samples position value array O On35 1 0 Ooi 1 0 Ons o MI Aoi Weill OS OA dtype position lt f8 value lt f8 Fancy indexing works as usually gt gt gt samples samples sensor_code ALFA AAA I CALEA MOr 036 AA OS AAA AO A dtype sensor_code S4 position lt f8 value lt f8 Note There are a bunch of other syntaxes for constructing structured arrays see here and here 3 3 3 Fourier transforms Numpy contains 1 D 2 D and N D fast discrete Fourier transform routines which compute n 1 k A Y am exp 27 k 0 n 1 m 0 Full details of what for you can use such standard routines is beyond this tutorial Neverheless there they are if you need them gt gt gt a np exp 2j np pixnp arange 10 gt gt gt fa Dp Ete EEEa gt gt gt np set_printoptions suppress True print small number as 0 gt gt gt fa arsav LO 03 Dm DO OROI ORRORI OO Ty O 00 Oa 000 311
226. nd 4 F una tom sn O L gt Al array I 05597729 OSA SAA On o42 643 SN gt gt gt b np random randn 4 gaussian gt gt gt b array 2 56844807 0 06798064 0 36823781 0 86966886 gt gt gt c np random rand 3 3 gt gt gt Cc array 0 31976645 0 64807526 0 74770801 OS AS OO 7 OR Scol403 7 NOnOv 6oSissil OSA AS On A Ons SOS 28 oi gt gt gt d np random zipf 1 5 size 2 8 Zipf distribution s 1 5 gt gt gt d array i ls290 Aly Gy oF all ale iL Pile ike 5 ly S ili dl ae IA gt gt gt np random seed 1234 Setting the random seed gt gt gt np random rand 3 array Ol Saas OsG22oyy Wher 727 774 gt gt gt np random seed 1234 gt gt gt np random rand 5 arava O ASAS ORG 2208 iii Osetia Ws MSI Wo ial 3 1 4 Basic data types You probably noted the 1 and 1 above These are different data types gt gt gt a np array l1 2 31 gt gt gt a dtype dtype int64 gt gt gt bD np array il 27 23 gt gt gt b dtype 3 1 The numpy array object 44 Python Scientific lecture notes Release 2011 1 dtype float64 Much of the time you don t necessarily need to care but remember they are there You can also choose which one you want gt gt gt c np array 1 2 3 dtype float gt gt gt c dtype dtype float 64 The default data type is floating point
227. ndex of minimum gt gt gt X argmax index of maximum il e Logical operations gt gt gt np all True True False False gt gt gt np any True True False True Note Can be used for array comparisons gt gt gt a np zeros 100 100 gt gt gt MO cmay a Y 0 False gt gt gt MS a Ta True gt ES oaeen llo 2 Sy 21 22 gt 1 mp crea Ly Se 210 gt gt gt np array lo 4 4 31 gt gt gt a lt b amp b lt c all True e and many more best to learn as you go 3 2 Numerical operations on arrays 58 Python Scientific lecture notes Release 2011 1 Example data statistics Data in populations txt describes the populations of hares and lynxes and carrots in northern Canada during 20 years We can first plot the data gt gt gt data np loadtxt data populations txt gt gt gt year hares lynxes carrots data T trick columns to variables cee jolie seve a2 Wail Wea 081 gt gt gt plt plot year hares year lynxes year carrots 5 gt gt plt legend Hars lynx Canroe y OE AOS 05 gt gt gt plt show 80000 70000 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 1900 1905 1910 1915 1920 The mean populations over time gt gt gt populations data 1 gt gt gt populations mean axis 0 array 34080
228. ng trait equivalents Trait Python Type Built in Default Value Bool Boolean False Complex Complex number 0 0 Float Floating point number 0 0 Int Plain integer 0 Long Long integer OL Str String Unicode Unicode u A number of other predefined trait type do exist Array Enum Range Event Dict List Color Set Expression Code Callable Type Tuple etc Custom default values can be defined in the code from traits api import Hals rales sur Eloat class Reservoir HasTraits name Str max_storage Float 100 reservoir Reservoir name Lac de Vouglans Note Complex initialisation When a complex initialisation is required for a trait a XXX_default magic method can be implemented It will be lazily called when trying to access the XXX trait For example def _name default self vru Complex initialisation of the reservoir name return Undefined 13 3 2 Validation Every trait does validation when the user tries to set its content reservoir Reservoir name Lac de Vouglans max_storage 605 reservoir max_storage 230 TraitError Traceback most recent call last Users dpinte projects scipy lecture notes advanced traits lt ipython input 7 979bdff9974a gt in lt mod 13 3 What are Traits 247 Python Scientific lecture notes Release 2011 1 gt I reservolr max storage 230 Use
229. nsists of 10 physiological variables age sex weight blood pressure measure on 442 patients and an indication of disease progression after one year gt gt gt diabetes datasets load_diabetes gt gt gt diabetes _ X train diabetes data 20 gt gt gt diabetes X test diabetes data 20 gt gt gt diabetes_y_train diabetes target 20 gt gt gt diabetes_y_test diabetes target 20 The task at hand is to predict disease prediction from physiological variables 16 6 1 Sparse models To improve the conditioning of the problem uninformative variables mitigate the curse of dimensionality as a feature selection preprocessing etc it would be interesting to select only the informative features and set non informative ones to 0 This penalization approach called Lasso can set some coefficients to zero Such methods are called sparse method and sparsity can be seen as an application of Occam s razor prefer simpler models to complex ones gt gt gt from scikits learn import linear_model gt gt gt regr linear_model Lasso alpha 3 gt gt gt regr fit diabetes_X_train diabetes_y_train Lasso alpha 0 3 copy_X True fit_intercept True max_iter 1000 normalize False precompute auto tol 0 0001 gt gt gt regr coef_ very sparse coefficients array 0 r 0 497 34075682 199 17441034 Ol i 10 A IS GOAL SANS OF 7 ASO 9S7 9595 7 OF 1 gt gt
230. nt numpy _ version Check that the following is what you expect gt gt gt print numpy __file__ In case you have old broken Numpy installations lying around If unsure try to remove existing Numpy installations and reinstall 8 6 Contributing to Numpy Scipy 187 Python Scientific lecture notes Release 2011 1 8 6 3 Contributing to documentation 1 Documentation editor e http docs scipy org numpy e Registration Register an account Subscribe to scipy dev mailing list subscribers only Problem with mailing lists you get mail x But you can turn mail delivery off change your subscription options at the bottom of http mail scipy org mailman listinfo scipy dev Send a mail scipy dev mailing list ask for activation To scipy dev scipy org Ham I d like to edit Numpy Scipy docstrings My account is XXXXX Cheers Ne NGS e Check the style guide http docs scipy org numpy Don t be intimidated to fix a small thing just fix it e Edit 2 Edit sources and send patches as for bugs 3 Complain on the mailing list 8 6 4 Contributing features 0 Ask on mailing list if unsure where it should go 1 Write a patch add an enhancement ticket on the bug tracket 2 OR create a Git branch implementing the feature add enhancement ticket e Especially for big invasive additions e http projects scipy org numpy wiki GitMirror e http www spheredev o
231. ntific lecture notes Release 2011 1 solve as single precision real gt gt gt mtxl mtx astype np float32 gt gt gt x dsl spsolve mtxl rhs use_umfpack False gt gt gt print x 2 gt gt Print Veron 2 mtxl kx b solve as double precision real gt gt gt mtx2 mtx astype np float64 gt gt gt x dsl spsolve mtx2 rhs use_umfpack True gt gt gt print x PEINE Error 5 Mie cs E solve as single precision complex gt gt gt mtxl mtx astype np complex64 gt gt gt x dsl spsolve mtxl rhs use_umfpack False gt gt gt print x gt gt gt Prine Erro AO solve as double precision complex gt gt gt mtx2 mtx astype np complex128 gt gt gt x dsl spsolve mtx2 rhs use_umfpack True gt gt gt print x gt gt gt PLINE Macas V5 Mier E non Construct a 1000x1000 iil matrix and add some values to it convert it to CSR format and solve A x b for x and solve a linear system with a direct solver moe import numpy as np import scipy sparse as sps from matplotlib import pyplot as plt from scipy sparse linalg dsolve import linsolve rand np random rand mtx sps lil_matrix 1000 1000 dtype np float64 mtx 0 2 100 rand 100 me OO 200 A mts 0 010 mtx setdiag rand 1000 PERCEK plt spy mtx marker markersize 2 plt show mtx mtx tocsr rhs rand 1000 x linsol
232. ntific lecture notes Release 2011 1 Proposed solution 6 12 4 Example of solution for the image processing exercise unmolten grains in glass Det Spot Mag HFW 1 0mm 10 4 mm 15 0 kV SSD 5 8 41x 3 25 mm MV 15 05 2009 1280 C 1 Open the image file MV_HFV_012 jpg and display it Browse through the keyword arguments in the docstring of imshow to display the image with the right orientation origin in the bottom left corner and not the upper left corner as for standard arrays gt gt gt dat imread MV_HFV_012 jpq 2 Crop the image to remove the lower panel with measure information gt gt gt dat dat 60 3 Slightly filter the image with a median filter in order to refine its histogram Check how the histogram changes gt gt gt filtdat ndimage median_filter dat size 7 7 gt gt gt hi_dat np histogram dat bins np arange 256 gt gt gt hi_filtdat np histogram filtdat bins np arange 256 6 12 Summary exercises on scientific computing 135 Python Scientific lecture notes Release 2011 1 140000 120000 100000 80000 60000 40000 20000 0 50 100 150 200 250 4 Using the histogram of the filtered image determine thresholds that allow to define masks for sand pixels glass pixels and bubble pixels Other option homework write a function that determines automatically the thresholds from the minima of the histogram
233. nxx2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im ndimage gaussian_filter im sigma 1 4 n gt gt gt mask im gt im mean e Analysis of connected components Label connected components ndimage label 12 6 Measuring objects properties 239 Python Scientific lecture notes Release 2011 1 gt gt gt label_im nb_labels ndimage label mask gt gt gt nb_labels how many regions A3 gt gt gt plt imshow label_im lt matplotlib image AxesImage object at 0x6624d50 gt es Compute size mean_value etc of each region gt gt gt sizes ndimage sum mask label_im range nb_labels 1 gt gt gt mean_vals ndimage sum im label_im range 1 nb_labels 1 Clean up small connect components gt gt gt mask_size sizes lt 1000 gt gt gt remove_pixel mask_size label_im gt gt gt remove_pixel shap 2567 2516 gt gt gt label_im remove_pixel 0 gt gt gt plt imshow label_im Now reassign labels with np searchsorted gt gt gt labels np unigque label_im gt gt gt label_im np searchsorted labels label_im Find region of interest enclosing object gt gt gt slice_x slice_y ndimage find_objects label_im 4 0 gt gt gt roi im slice_x slice_y gt gt gt plt imshow roi 12 6 Measuring objects properties 240 Python Scientific lecture notes Release 2011 1
234. oadtxt pop2 txt Note If you have a complicated text file what you can try are e np genfromtxt e Using Python s I O functions and e g regexps for parsing Python is quite well suited for this Navigating the filesystem in Python shells Ipython In 1 pwd show current directory nome user stuff 2011 numpy tutorial In 2 cd ex home user stuff 2011 numpy tutorial ex Ta 3 Ts populations txt species txt Python here s yet one reason to use Ipython for interactive use gt gt gt import os gt gt gt os getcwd nome user stuff 2011 numpy tutorial gt gt gt Os Chdi Ex gt gt gt os getcwd nome user stuff 2011 numpy tutorial ex cer isha la a populations ezr species txt Images gt gt gt img plt imread data elephant png gt gt gt img shape img dtype C00 S00 Ss deuype floats 2 gt gt gt plt imshow img gt gt gt plt savefig plot png gt gt gt plt show gt gt gt plt imsave red_elephant img 0 cmap plt cm gray This saved only one channel of RGB gt gt gt plt imshow plt imread red_elephant png gt gt gt plt show Other libraries gt gt gt from scipy misc import imsave gt gt gt imsave tiny_elephant png img 6 6 3 1 The numpy array object 51 Python Scientific lecture notes Release 2011 1 gt gt gt plt imshow
235. objects from modules In 1 import os In 2 os Out 2 lt module os from jusr libl python2 6 los pya gt Iin 3 tos lts tato Ome pss conti pyr y Ubasnicwlay DCS nasiewe control Flow rst UMC rons ase y python_language rst dreus UNG rst EMS To rst exceptions rst UWOmkt WOW ese mnde ESES And also In 4 from os import listdir Importing shorthands In 5 import numpy as np Warning from os import x Do not do it e Makes the code harder to read and understand where do symbols come from e Makes it impossible to guess the functionality by the context and the name hint os name is the name of the OS and to profit usefully from tab completion Restricts the variable names you can use os name might override name or vise versa Creates possible name clashes between modules Makes the code impossible to statically check for undefined symbols 2 6 Reusing code scripts and modules 26 Python Scientific lecture notes Release 2011 1 Modules are thus a good way to organize code in a hierarchical way Actually all the scientific computing tools we are going to use are modules gt gt gt import numpy as np data arrays gt gt gt np linspace 0 10 6 array 0 Lor 4 Gap Bap 1061 gt gt gt import scipy scientific computing In Python x y software Ipython
236. ollection of already existing bricks corresponding to classical numerical methods or basic actions we don t want to re program the plotting of a curve a Fourier transform or a fitting algorithm Don t reinvent the wheel Easy to learn computer science neither is our job nor our education We want to be able to draw a curve smooth a signal do a Fourier transform in a few minutes Easy communication with collaborators students customers to make the code live within a labo or a company the code should be as readable as a book Thus the language should contain as few syntax symbols or unneeded routines that would divert the reader from the mathematical or scientific understanding of the code Efficient code that executes quickly But needless to say that a very fast code becomes useless 1f we spend too much time writing it So we need both a quick development time and a quick execution time A single environment language for everything if possible to avoid learning a new software for each new problem 1 1 3 Existing solutions Which solutions do the scientists use to work Python Scientific lecture notes Release 2011 1 Compiled languages C C Fortran etc e Advantages Very fast Very optimized compilers For heavy computations it s difficult to outperform these lan guages Some very optimized scientific libraries have been written for these languages Ex blas vector matrix operations e Drawbacks
237. olve real problems and that their use is as simple as possible Chapters contents e Iterators generator expressions and generators page 141 Iterators page 141 Generator expressions page 142 Generators page 142 Bidirectional communication page 143 Chaining generators page 145 e Decorators page 145 Replacing or tweaking the original object page 146 Decorators implemented as classes and as functions page 146 Copying the docstring and other attributes of the original function page 148 Examples in the standard library page 149 Deprecation of functions page 151 A while loop removing decorator page 152 A plugin registration system page 152 More examples and reading page 153 e Context managers page 153 Catching exceptions page 154 Using generators to define context managers page 155 140 Python Scientific lecture notes Release 2011 1 7 1 Iterators generator expressions and generators 7 1 1 Iterators Simplicity Duplication of effort is wasteful and replacing the various home grown approaches with a standard feature usually ends up making things more readable and interoperable as well Guido van Rossum Adding Optional Static Typing to Python An iterator is an object adhering to the iterator protocol basically this means that it has a next method which when called returns the next item in the sequence and when
238. on 3 Form the 2 d boolean mask indicating which points are in the set 4 Save the result to an image with gt gt gt import matplotlib pyplot as plt gt Olen imshow mask I extent SS dei gt gt gt plt gray gt gt gt plt savefig mandelbrot png 3 2 Numerical operations on arrays 73 Python Scientific lecture notes Release 2011 1 Exercise 2 5 Markov chain Pj I Markov chain transition matrix P and probability distribution on the states p 1 0 lt P 1 3 lt 1 probability to go from state i to state j 2 Transition rule Phew PY bota 3 all sum P axis 1 1 p sum 1 normalization Write a script that works with 5 states and Constructs a random matrix and normalizes each row so that it is a transition matrix e Starts from a random normalized probability distribution p and takes 50 steps gt p_50 e Computes the stationary distribution the eigenvector of P T with eigenvalue 1 numerically closest to 1 gt p_stationary Remember to normalize the eigenvector I didn t e Checks if p_50 and p_stationary are equal to tolerance le 5 Toolbox np random rand dot np linalg eig reductions abs argmin comparisons all np linalg norm etc 3 2 9 Conclusions What do you need to know to get started e Know how to create arrays array arange ones zeros Know the shape of the array with array shape then use slicing to obtain different vi
239. on and ODEs scipy integrate Enter search terms or a module e Interpolation scipy interpolate class or function name e Input and output scipy io e Linear algebra scipy linalg Maximum entropy models scipy naxentropy e Miscellaneous routines scipy misc E Find help lt Previous Next Highlight all Match case 3 Reached end of page continued from top 89 Python Scientific lecture notes Release 2011 1 e Numpy s and Scipy s documentation is enriched and updated on a regular basis by users on a wiki http docs scipy org numpy As a result some docstrings are clearer or more detailed on the wiki and you may want to read directly the documentation on the wiki instead of the official documentation website Note that anyone can create an account on the wiki and write better documentation this is an easy way to contribute to an open source project and improve the tools you are using Schr documentation edifor Back to Numpy Scipy documentation Numpy documentation editor scipy org editor Wiki Docstrings Changes Milestones Search Stats Patch Login scipy ndimage morphology binary_dilation View Log DifftoSVN Discussion Source Review status Being written SciPy Multi dimensional image processing mod scipy ndimage binary_dilation input structure None iterations 1 mask None output None border_value 0 origin 0 brute_force False Multi dimensional binary dilation with the given struct
240. on the memory use views and not copies Copying big arrays is as costly as making simple numerical operations on them In 1 a np zeros le7 In 2 timeit a copy 10 loops best of 3 124 ms per loop Tn 2 tame a I 10 loops best of 3 112 ms per Loop Beware of cache effects Memory access is cheaper when it is grouped accessing a big array in a continuous way is much faster than random access This implies amongst other things that smaller strides are faster see CPU cache effects page 167 ins Js e ms Zeros Geny ileal wrclew e In 2 Stimeit c sum axis 0 loops Pest of Ss 839 sper Loop In 3 Stimeit c sum axis 1 1 loops best of 3 188 ms per loop In 4 e strrdes Out 4 80000 8 This is the reason why Fortran ordering or C ordering may make a big difference on operations In 5 a np random rand 20 2 18 In 6 b ll np random rand 20 2xx18 Time tt mps dot b va T 1 loops best of 3 194 ms per loop In 8 c np ascontiguousarray a T 10 4 Writing faster numerical code 204 Python Scientific lecture notes Release 2011 1 in L9 tamer np dot by C 10 loops best of 3 84 2 ms per loop Note that copying the data to work around this effect may not be worth it Ta 10 Stimeit c np ascontiguousarray a T 10 loops best of 3 106 ms per loop Using numexpr can be useful to automatically
241. one Sacos a 20 2 vyersion 3 memory address of data is readonly data type descriptor same in another form strides or None if in C order He He ROE gt gt gt import Image gt gt gt img Image open test png gt gt gt img __array_interface__ data aan very long sic ing shape 200 200 4 Cevyipesieia a ial gt gt gt x np asarray img gt gt gt x shape 200 200 4 gt gt gt x dtype dtype uinte Note A more C friendly variant of the array interface is also defined 8 3 5 The new buffer protocol PEP 3118 e Multidimensional buffers e Data type information present e C level interface extensions to tp_as_buffer e Integrated into Python gt 2 6 e Replaces the old buffer interface in Python 3 e Next releases of Numpy gt 1 5 will also implement it fully e Demo in Python 3 Numpy dev version Python os M2 a A Apr Sr 20o SIS 0N A ICCC MATAS on iaa Type help copyright credits or license for more information gt gt gt import numpy as np gt gt gt np __version__ 2 0 0 dev8469 15dcfb e Memoryview object exposes some of the stuff Python side SS oe aaa 2 is 410 gt gt gt y memoryview x gt gt gt y format YY gt gt gt y itemsize 4 8 3 Interoperability features 180 Python Scientific lecture notes Release 2011 1 gt gt gt y ndim 2
242. ontainer types e Tuples 2 2 Basic types 14 Python Scientific lecture notes Release 2011 1 Tuples are basically immutable lists The elements of a tuple are written between parentheses or just separated by commas gt E 12315 52321 hellol l gt gt gt t 0 12345 gt gt gt E 12345 54321 hello gt gt gt u 0 2 e Sets unordered unique items SSS rs SN ss Ce aa A SS setli alr ea PASA gt gt gt Sellos Ca Ma Ser ce II A bag of Ipython tricks e Several Linux shell commands work in Ipython such as 1s pwd cd etc e To get help about objects functions etc type help object Just type help to get started e Use tab completion as much as possible while typing the beginning of an object s name variable function module press the Tab key and Ipython will complete the expression to match available names If many names are possible a list of names is displayed e History press the up resp down arrow to go through all previous resp next instructions starting with the expression on the left of the cursor put the cursor at the beginning of the line to go through all previous commands e You may log your session by using the Ipython magic command logstart Your instructions will be saved in a file that you can execute as a script in a different session In 1 Slogstart commands log Activating auto logging Current session sta
243. optaMiZe po siop wa ke ee eS ER ke eS 6 11 Image processing scipy ndimage s ecs cm css wee ee E 6 12 Summary exercises on scientific COMPU Advanced topics Advanced Python Constructs 7 1 Iterators generator expressions and generators 2 0 000 p ee eee ed WDCCOLAIOTS oca sd ee ee Oe ORE EAA Be eae Be BS 1 3 Context Managers s eb ae mo Eag ada ok RA Be Se BY kel e Bogs we Re de A Advanced Numpy Sal Mendieta acd bo ee Rm ee Ook Ge eee ee ee e a 3 2 Universal functions lt 254 04 205 22808 6b ds bd be ai e AS 8 3 Interoperability features a4 see 5 ER RRO HOES E A eee ew OS 8 4 Siblings chararray maskedarray MatriX o A e E a hao e A A oe eRe ke SO ea GE ee A A we Ge G 8 6 Contributing to Numpy Scipy ce cra eeur ee ee ee et Debugging code Oly Avoiding DUES e aeie e adie ad E Bee OS A Eee ee ee we 92 Debugging WOTK OW 4 205 aace 4 48 bebe eRe a ee ee ge ee eo 93 Usne the Pythondebugser i i686 bea RSG ES Sheet AA 9 4 Debugging segmentation faults using gdb o nemen e A Optimizing code 10 1 Optimization workflow voor circense ee ee ee bee 10 2 Brofiling Python COdS cosmos soro lS be a SE ee ae 10 3 Making code So faster s bs cote e RY et oe a Y A ee ee A 104 Writing faster numerical Code wc ria a PEAS BR ee 4s be BA LEE OR RD Sparse Matrices in SciPy 11T Introduction o sps e ba ee ES a RRS SEEDS Ree ee Re 11 2 Storage SCHEMES s oraso ae Oa e ae e
244. ordering methods __1t__ gt__ __le_ based on a single available one Python 2 7 7 2 5 Deprecation of functions Let s say we want to print a deprecation warning on stderr on the first invocation of a function we don t like anymore If we don t want to modify the function we can use a decorator class deprecated object Print a deprecation warning once on first use of the function gt gt gt deprecated doctesti SKIP der F EA pass gt E e doctest SKIP f is deprecated woe def __call_ self func self func func self count 0 return self _wrapper def _wrapper self xargs kwargs self count 1 if self count 1 print self func __name__ is deprecated return self func xargs kwargs It can also be implemented as a function def deprecated func Print a deprecation warning once on first use of the function gt gt gt deprecated doctest SKIP der 7 Bone pass gt gt gt El doctest SKIP f is deprecated woe count 0 def wrapper xargs kwargsS count 0 1 if count 0 1 print func __name_ is deprecated return func xargs kwargs return wrapper 7 2 Decorators 151 Python Scientific lecture notes Release 2011 1 7 2 6 A while loop removing decorator Let s say we have function which returns a lists of things and this list created by running a loop If we don t know how m
245. ort svm grid_search gt gt gt gammas np logspace 6 1 10 gt gt gt svc svm SVC gt gt gt clf grid_search GridSearchCV estimator svc param_grid dict gamma gammas n_jobs 1 gt gt gt Cif hike diotse datali L000 digas target E0 0 01 6 GridSearchCV cv None estimator SVC C 1 0 gt gt gt clf best_score 0 98899798001594419 gt gt gt clf best_estimator gamma 0 00059948425031894088 By default the GridSearchCV uses a 3 fold cross validation However if it detects that a classifier is passed rather than a regressor it uses a stratified 3 fold Cross validated estimators Cross validation to set a parameter can be done more efficiently on an algorithm by algorithm basis This is why for certain estimators the scikits learn exposes CV estimators that set their parameter automatically by cross validation gt gt gt from scikits learn import linear_model datasets gt gt gt lasso linear_model LassoCV gt gt gt diabetes datasets load_diabetes gt gt gt X diabetes diabetes data gt gt gt y_diabetes diabetes target gt gt gt lasso fit X_diabetes y_diabetes LassoCV alphas array 2 14804 2 00327 0 0023 0 00215 copy_X True cv None eps 0 001 fit_intercept True max_iter 1000 n_alphas 100 normalize False precompute auto tol 0 0001 verbose False gt gt gt The estimator chose automatically its lambda gt gt gt
246. our emacs binds FS to pyflakes 9 1 Avoiding bugs 191 Python Scientific lecture notes Release 2011 1 defun pyflakes thisfile interactive compile format pyflakes s buffer file name define minor mode pyflakes mode Toggle pyflakes mode With no argument this command toggles the mode Non null prefix argument turns on the mode Null prefix argument turns off the mode Gp mie alinalicakeull velts man 77 lhee indicators tem ene mode lumen Pyflakes 7 The minor mode bindings f5 pyflakes thisfile add hook python mode hook lambda pyflakes mode t A type as go spell checker like integration obs IB self log emissionprob obs e In vim Use the pyflakes vim plugin 1 download the zip file from http www vim org scripts script php script_id 2441 2 extract the files in vim ftplugin python 3 make sure your vimrc has filetype plugin indent on e In emacs Use the flymake mode with pyflakes documented on http www plope com Members chrism flymake mode add the following to your emacs file when load flymake t defun flymake pyflakes init letx temp file flymake init create temp buffer copy flymake create temp inplace Local toL file relative nam temp file file name directory buffer file name list pyflakes list local file add to list flymake allowed file name masks MAN pyWW flymake pyflakes init
247. out IPython s magic functions 93 Python Scientific lecture notes Release 2011 1 help gt Python s own help system object gt Details about object object also works prints more Welcome to pylab a matplotlib based Python environment For more information type help pylab lesa AS Now we can make our first really simple plot in e plot range r0 Out 1 lt matplotlib lines Line2D instance at 0x01AA26E8 gt mn Ma The numbers form 0 through 9 are plotted Figure 1 Now we can interactively add features to or plot In 2 xlabel measured Out 2 lt matplotlib text Text instance at 0x01A9D210 gt In 3 ylabel calculated Out 3 lt matplotlib text Text instance at 0x01A9D918 gt In 4 title Measured vs calculated Out 4 lt matplotlib text Text instance at 0x01A9DF80 gt an Poise erat rica TAMIG We get a reference to our plot in bol my piot aea and to our line we plotted which is the first in the plot In 7 line my_plot lines 0 Now we can set properties using set__something methods SNS set marker Con or the setp function 5 3 Simple Plots 94 Python Scientific lecture notes Release 2011 1 In 9 setp line color g Out 9 None To apply the new properties we need to redraw
248. p newaxis y np newaxis cia IS IO s 20 EG U2 ar 2i 7 14 21 28 dtype int16 e Internally array broadcasting is indeed implemented using 0 strides More tricks diagonals See Also stride diagonals py Challenge Pick diagonal entries of the matrix assume C memory order gt gt gt x np array 1 2 3 4 5 6 I e Mi Cese lme gt gt gt x_diag as_strided x shape 3 strides Pick the first super diagonal entries 2 6 And the sub diagonals Hint to the last two slicing first moves the point where striding starts from Solution Pick diagonals gt gt gt x_diag as_strided x shape 3 strides 3 1 x itemsize gt gt gt x_diag array ld Dip Si 8 1 Life of ndarray 166 Python Scientific lecture notes Release 2011 1 Slice first to adjust the data pointer gt gt gt as_strided x 0 1 shape 2 strides 3 1 x itemsize array 12 915 gt gt gt as_strided x 1 0 shape 2 strides 3 1 x itemsize array 4 8 Note gt gt gt y np diag x k 1 gt gt gt y array 2 61 However gt gt gt y flags owndata True It makes a copy Room for improvement bug xxx See Also stride diagonals py Challenge Compute the tensor trace gt gt gt x np arange 5x5x5x5 reshape 5 5 5 5
249. p_func 1 cdef char input_output_types 3 cdef void elementwise_funcs 1 Reminder some pr made Ufunc loops 4 PyULUnCa Ear ys MEU EU Cire ait Sey NEUE CAC Vie YUU CCA 4 4 4 float elementwise_func float input_1 float elementwise_func float input_l double elementwise_func double input_1 double elementwise_func double input_l float Input m2 ys double input_2 8 2 Universal functions 172 Python Scientific lecture notes Release 2011 1 elementwise_func complex_double xinput elementwise_func complex_double inl NB VUE wugYe IDL B Se yUEunes DED Ay complex_double xin2 VN complex_doublex complex_double complex_doublex The full list is above Type codes NPY_BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_IN NPY_LONG NPY_ULONG NPY_LONGLONG NPY_ULONGLONG NPY_FLOAT NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NP NPY_VOID NPY_TIMEDELTA NPY_OBJECT NPY_STRING Ly Ey NPY_UNICODE loop_func 0 TODO suitable PyUFunc_x input_output_types 0 TODO TODO fill in rest of input_output_types This thing is passed as the PyUFunc_ loop to let it know which function it should call elementwise_funcs 0 lt void gt mandel_single_point Construct the ufunc mandel PyUFunc_FromFuncAndData Toop tune elementwise_funcs
250. perators on such block vectors have similar block structure a har Ad gt har Eht gt Vi ha 0 e gt gt gt H np zeros 2 2 2 2 dimensions levell level2 Spini spin2 gt gt A H1 OS 21 gt gt gt V H 0 1 Doing the matrix product get rid of the block structure do the 4x4 matrix product then put it back Ay gt gt gt def mdot operator PSE return operator transpose 0 2 1 3 reshape 4 4 dot psi reshape 4 reshape 2 2 ILe reorder dimensions first to levell spinl level2 spin2and then reshape gt correct matrix product 3 2 6 Fancy indexing Numpy arrays can be indexed with slices but also with boolean or integer arrays masks This method is called fancy indexing Masks gt gt gt np random seed 3 gt gt gt a np random random_integers 0 20 15 gt gt gt a arcay O 3 8 0 19 10 iil 97 O 6G 0 20 de Tp weal gt gt gt a 3 0 array False True False rue False False False True False True rue False rue False False dtype bool gt gt gt mask a 3 0 gt gt gt extract_from_a a mask or a a 3 0 gt gt gt extract_from_a extract a sub array with the mask larray 3 0 9 6 0 121 Extracting a sub array using a mask produces a copy of this sub array not a view like slicing gt gt gt extract_from_a 0 1 gt gt gt a arcay CLIO S an Ol SO SO e Or O A aa
251. ping the code as simple as possible in order to be comprehensible and easily extensible SymPy is written entirely in Python and does not require any external libraries Sympy documentation and packages for installation can be found on http sympy org 271 Python Scientific lecture notes Release 2011 1 Chapters contents e First Steps with SymPy page 272 Using SymPy as a calculator page 272 Exercises page 273 Symbols page 273 e Algebraic manipulations page 273 Expand page 273 Simplify page 273 Exercises page 274 e Calculus page 274 Limits page 274 Differentiation page 274 Series expansion page 275 Exercises page 275 Integration page 275 Exercises page 275 e Equation solving page 275 Exercises page 276 e Linear Algebra page 276 Matrices page 276 Differential Equations page 277 Exercises page 277 15 1 First Steps with SymPy 15 1 1 Using SymPy as a calculator SymPy defines three numerical types Real Rational and Integer The Rational class represents a rational number as a pair of two Integers the numerator and the denominator so Rational 1 2 represents 1 2 Rational 5 2 5 2 and so on gt gt gt from sympy import x gt gt gt a Rational 1 2 gt gt gt a 12 gt gt gt ax2 dl SymPy uses mpmath in the background which makes it possible to perform computations using arbitrary pre
252. plied e except when your elementwise function is not in one of the above forms 8 2 2 Exercise building an ufunc from scratch The Mandelbrot fractal is defined by the iteration zez c where c x iy is a complex number This iteration is repeated if z stays finite no matter how long the iteration runs c belongs to the Mandelbrot set e Make ufunc called mandel z0 c that computes z Z0 for k in range iterations Z AZ t say 100 iterations or until z realx x2 z imag x 2 gt 1000 Use itto determine which c are in the Mandelbrot set e Our function is a simple one so make use of the PyUFunc_x helpers 8 2 Universal functions 170 Python Scientific lecture notes Release 2011 1 e Write it in Cython See Also mandel pyx mandelplot py Fix the parts marked by TODO Compile this file by Cython gt 0 12 required because of the complex vars cython mandel pyx python setup py build_ext i and try it out with in this directory gt gt gt import mandel gt gt gt mandel mandel 0 1 2j The elementwise function cdef void mandel_single_point double complex x z_in double complex c_in double complex z_out nogil The Mandelbrot iteration Some points of note It s NOT allowed to call any Python functions here The Ufunc loop runs with the Python Global Interpreter Lock released Hence the nogil And so all local variables must be
253. pute fast Fourier transforms As an illustration an input signal may look like gt gt gt time_step 0 1 gt gt gt period 5 gt gt gt time_vec np arange 0 20 time_step gt gt gt sig np sin 2 np pi period x time vec np cos 10 x np pi time_vec However the observer does not know the signal frequency only the sampling time step of the signal sig But the signal is supposed to come from a real function so the Fourier transform will be symmetric The fft freq function will generate the sampling frequencies and f ft will compute the fast Fourier transform gt gt gt from scipy import f tpack gt gt gt sample_freq fftpack fftfreq sig size d time_step gt gt gt sig_fft fftpack fft sig Nevertheless only the positive part will be used for finding the frequency because the resulting power is symmetric gt gt gt pidxs np where sample_freq gt 0 gt gt gt freqs sample_freq pidxs gt gt gt power np abs sig_fft pidxs 6 8 Fast Fourier transforms scipy fftpack 117 Python Scientific lecture notes Release 2011 1 100 Peak frequency 80 60 plower 40 20 0 050 100 150 200 250 300 350 400 45 0 1 2 3 4 5 Frequency Hz Thus the signal frequency can be found by gt gt gt freq freqs power argmax gt gt gt np allclose freg 1 period ue Now only the main signal component will be extracted from the Fouri
254. puting authors Adrien Chauve Andre Espaze Emmanuelle Gouillart Ga l Varoquaux Scipy The scipy package contains various toolboxes dedicated to common issues in scientific computing Its different submodules correspond to different applications such as interpolation integration optimization image processing statistics special functions etc scipy can be compared to other standard scientific computing libraries such as the GSL GNU Scientific Library for C and C or Matlab s toolboxes scipy is the core package for scientific routines in Python it is meant to operate efficiently on numpy arrays so that numpy and scipy work hand in hand Before implementing a routine if is worth checking if the desired data processing is not already imple mented in Scipy As non professional programmers scientists often tend to re invent the wheel which leads to buggy non optimal difficult to share and unmaintainable code By contrast Scipy s routines are optimized and tested and should therefore be used when possible Chapters contents Scipy builds upon Numpy page 110 File input output scipy io page 110 Signal processing scipy signal page 111 Special functions scipy special page 112 Statistics and random numbers scipy stats page 112 Histogram and probability density function page 112 Percentiles page 113 Statistical tests page 113 Linear algebra operations scipy linalg page 114 Numerica
255. python 3 1 The numpy array object 45 Python Scientific lecture notes Release 2011 1 9 0 0 5 2D arrays such as images gt gt gt image np random rand 30 gt gt gt plt imshow image gt gt gt plt gray gt gt gt plt show gt gt gt plt pcolor image gt gt gt plt hot gt gt gt plt colorbar gt gt gt plt show 2 0 2 5 3 0 3 1 The numpy array object 46 Python Scientific lecture notes Release 2011 1 See Also More in the matplotlib chapter page 93 3D plotting For 3D visualization we can use another package Mayavi A quick example start with relaunching iPython with these options ipython pylab wthread or ipython pylab wx in Python gt 0 10 MS 0000216 0143 0 286 040 14 657 0714 In 59 from enthought mayavi import mlab In 60 mlab figure get fences failed 1 params 6 val 0 Out 60 lt enthought mayavi core scene Scene object at 0xcb2677c gt In 61 mlab surf image Out 61 lt enthought mayavi modules surface Surface object at 0xd0862fc gt 3 1 The numpy array object 47 Python Scientific lecture notes Release 2011 1 In 62 mlab axes Out 62 lt enthought mayavi modules axes Axes object at 0xd07892c gt The mayavi mlab window that opens is interactive by clicking on the left mouse button you can rotate the image zoom with the mouse wheel
256. r example remove 30 pixels from all the borders of the image To check the result display this new array with imshow In 9 crop lena l nals0 30 30 30 We will now frame Lena s face with a black locket For this we need to 3 2 Numerical operations on arrays 70 Python Scientific lecture notes Release 2011 1 create a mask corresponding to the pixels we want to be black The mask is defined by this condition y 256 2 x 256 xx 2 tn L5 y ss Sp ogerd Orso 0s Sa af Se and y indrces om pixels In 16 y shape x shape Gui cs C sle2 a lp Sle y In 17 centerx centery 256 256 center of the image In 18 mask y centery 2 x centerx 2 gt 230 2 circle then assign the value O to the pixels of the image corresponding to the mask The syntax is extremely simple and intuitive In 19 lena mask 0 In 20 plt imshow lena Out 20 lt matplotlib image AxesImage object at 0xa36534c gt e Follow up copy all instructions of this exercise in a script called lena_locket py then execute this script in Python with run lena_locket py Change the circle to an ellipsoid Exercise 2 1 Matrix manipulations 1 Form the 2 D array without typing it in explicitly i al Z FY 12 SAS ibs 4 9 14 By alle ES and generate a new array containing its 2nd and 4th rows 2 Divide each column of the array gt gt g
257. r overwrite existing file e Append a file a e Read and Write r e Binary mode b Note Use for binary files especially on Windows 2 8 Standard Library Note Reference document for this section e The Python Standard Library documentation http docs python org library index html e Python Essential Reference David Beazley Addison Wesley Professional 2 8 1 os module operating system functionality A portable way of using operating system dependent functionality Directory and file manipulation Current directory In 17 os getcwd Outil Users cohurns sre seipy2009 seipy_ 2009 tutorral source List a directory In 31 stes aos cucuta Duc T Si index st swe y python_language rst swp view_array py swp Static _templates ipase ES sio y Conk Py y eonkrol E VOW CSE debugging rst Make a directory In 32 tos amd ume are in 33 punks San oo istare osL curdi Out 33 True Rename the directory 2 8 Standard Library 33 Python Scientific lecture notes Release 2011 1 In 36 os rename junkdir foodir TA PSr jokers Sino sale sico seua abr Out 37 False in 38 rood anos list dit oS curdi r Out 38 True tin 41 osi rmdir foodie In 42 oo dino sist dame Oscura ta Out 42 False Delete a file In 44 fp open junk txt
258. r print i User defined classes can customize those rules by overriding the special __nonzero__ method 2 4 Control Flow 18 Python Scientific lecture notes Release 2011 1 oeu gt gt gt message Hello how are you gt gt gt message split returns a list Hello how are you gt gt gt for word in message split print word Hello how are you Few languages in particular languages for scientific computing allow to loop over anything but integers indices With Python it is possible to loop exactly over the objects of interest without bothering with indices you often don t care about Warning Not safe to modify the sequence you are iterating over Keeping track of enumeration number Common task is to iterate over a sequence while keeping track of the item number e Could use while loop with a counter as above Or a for loop In 13 for i in range 0 len words Panera print i words ae 0 cool 1 powerful 2 readable e But Python provides enumerate for this gt gt gt words cool powerful readable gt gt gt for index item in enumerate words print index item ONeool 1 powerful 2 readable Looping over a dictionary Use iteritems Im PLSI aerate elo ales le In 15 for key val in d iteritems a print Key s has value s key val Key a
259. ray H TE C 4 Which years any of the populations is above 50000 Hint comparisons and np any 5 The top 2 years for each species when they had the lowest populations Hint argsort fancy indexing 6 Compare plot the change in hare population see help np gradient and the number of lynxes Check correlation see help np corrcoef all without for loops Exercise 2 3 Crude integral approximations Write a function f a b c that returns a ranges 0 1 x 0 1 x 0 1 c Form a 24x12x6 array containing its values in parameter 3 2 Numerical operations on arrays 72 Python Scientific lecture notes Release 2011 1 Approximate the 3 d integral 1 pl pl J f edad de o Jo Jo over this volume with the mean The exact result is In 2 gt 0 1931 what is your relative error Hints use elementwise operations and broadcasting You can make np ogrid give a number of points in given range with np ogrid 0 1 203 Reminder Python functions def f a DE return some_result Exercise 2 4 Mandelbrot set Write a script that computes the Mandelbrot fractal The Mandelbrot iteration IN max 50 some_threshold 50 A y for j in xrange N_max Z gee2 e Point x y belongs to the Mandelbrot set if c lt some_threshold Do this computation by 1 Construct a grid of c x 1j y values in range 2 1 x 1 5 1 5 2 Do the iterati
260. ray of column indices data is array of corresponding nonzero values x indptr points to row starts in indices and data x lengthisn_row 1 last item number of values length of both indices and data nonzero values of the i th row are data indptr i indptr i 1 with column indices indices indptr i indptr i 1 x Item i j can be accessed as data indptr i k where k is position of j in indices indptr i indptr it 1 subclass of _cs_matrix common CSR CSC functionality x subclass of _data_matrix sparse matrix classes with dat a attribute e fast matrix vector products and other arithmetics sparsetools e constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data ij tuple data indices indptr tuple e efficient row slicing row oriented operations e slow column slicing expensive changes to the sparsity structure e use actual computations most linear solvers support this format Examples e create empty CSR matrix gt gt gt mtx sps csr_matrix 3 4 dtype np int8 gt gt gt mtx todense mateix OO OO Ol 10 Of WO 0 10 OF 07 011 censet e create using data ij tuple gt gt y o cra O 10 Mi A gt gt gt col md ctra 0 2 2 Oy ly 21 gt gt gt Pela mos cuac a So e De Sl gt gt gt mtx sps csr_matrix data row col shape 3 3 gt gt gt mtx lt 3x3 sparse matrix of type lt type
261. rg wiki Git_for_the_lazy Clone numpy repository git clone origin svn http projects scipy org git numpy git numpy cd numpy Create a feature branch git checkout b name of my feature branch svn trunk lt edit stuff gt git commit a e Create account on http github com or anywhere 8 6 Contributing to Numpy Scipy 188 Python Scientific lecture notes Release 2011 1 e Create a new repository Github e Push your work to github git remote add github git github YOURUSERNAME YOURREPOSITORYNAME git git push github name of my feature branch 8 6 5 How to help in general e Bug fixes always welcome What irks you most Browse the tracker e Documentation work API docs improvements to docstrings Know some Scipy module well User guide Needs to be done eventually x Want to think Come up with a Table of Contents http scipy org Developer_Zone UG_Toc e Ask on communication channels numpy discussion list scipy dev list 8 6 Contributing to Numpy Scipy 189 CHAPTER 9 Debugging code author Ga l Varoquaux This tutorial explores tool to understand better your code base debugging to find and fix bugs It is not specific to the scientific Python community but the strategies that we will employ are tailored to its needs Prerequisites e Numpy e IPython nosetests http readthedocs org docs nose en latest
262. rical operations on arrays 56 Python Scientific lecture notes Release 2011 1 3 2 3 Basic reductions Computing sums gt gt gt x n p array L 2 3 11 gt gt gt np sum x 10 gt gt gt x sum 10 Sum by rows and by columns axis 1 1 2 3 axis O gt gt np array PL Wy 2 2 gt gt gt x array 1 1 2p 211 gt gt gt x sum axis 0 columns first dimension array LS Sill ASUS al sao STARS gt gt gt x sum axis 1 rows second dimension array 2 4 gt gt gt Sun e ase 27 4 Same idea in higher dimensions gt gt gt IS np andom ran 222 gt gt gt x sum axis 2 0 1 AGO OS FSIS SA OA SUI IS SOC LI SOS OS Other reductions works the same way and take axis e Statistics ree So ere li 25 si A gt gt gt y Parra Iil 27 317 lon 6 11N gt gt gt x mean IES IES gt gt gt np median x 1S gt gt gt np median y y axis 1 last axis ese Do Deal 3 2 Numerical operations on arrays 57 Python Scientific lecture notes Release 2011 1 gt gt gt x Std OR SZSMSiGT 7 S884 5 gt gt gt x std ddof 1 om 9S7427 L207 15633381 full population standard dev sample std with N 1 in divisor e Extrema gt gt gt np EY 3 2AN gt gt gt x min il gt gt gt x max 3 gt gt gt x argmin i
263. rithms Any special mathematical functions Statistics 6 1 Scipy builds upon Numpy Numpy is required for running Scipy but also for using it The most important type introduced to Python is the N dimensional array and it can be seen that Scipy uses the same gt gt gt scipy ndarray is np ndarray True Moreover most of the Scipy usual functions are provided by Numpy gt gt gt scipy cos is np cos True If you would like to know the objects used from Numpy have a look at the scipy __file__ 1 file On version 0 6 0 the whole Numpy namespace is imported by the line from numpy 6 2 File input output scipy io e Loading and saving matlab files gt gt gt from scipy import io gt gt gt struct io loadmat file mat struct_as_record True gt gt gt io savemat C rle mat struct See also Load text files np loadtxt np savetxt e Clever loading of text csv files 6 1 Scipy builds upon Numpy 110 Python Scientific lecture notes Release 2011 1 np genfromtxt np recfromcsv e Fast an efficient but numpy specific binary format np save np load 6 3 Signal processing scipy signal gt gt gt from scipy import signal e Detrend remove linear trend from signal t np linspace 0 5 100 x t np random normal size 100 pl plot t x linewidth 3 pl plot t signal detrend x linewidth 3 e Resa
264. rn these warnings in exception which enables us to do post mortem debugging on them and find our problem more quickly In 3 np seterr all raise SUR STA ads E A print OS i PEINE Udemm ignore Other ways of starting a debugger e Raising an exception as a poor man break point If you find it tedious to note the line number to set a break point you can simply raise an exception at the point that you want to inspect and use ipython s debug Note that in this case you cannot step or continue the execution e Debugging test failures using nosetests You can run nosetests pdb to drop in post mortem debugging on exceptions and nosetests pdb failure to inspect test failures using the debugger 9 3 Using the Python debugger 196 Python Scientific lecture notes Release 2011 1 In addition you can use the Python interface for the debugger in nose by installing the nose plugin ipdb plugin You can than pass ipdb and ipdb failure options to nosetests e Calling the debugger explicitly Insert the following line where you want to drop in the debugger import pdb pdb set_trace Warning When running nosetests the output is captured and thus it seems that the debugger does not work Simply run the nosetests with the s flag Graphical debuggers For stepping through code and inspecting variables you might find it more convenient to use a graphical debugger su
265. rs dpinte projects ets traits trait 166 wy SY raise TraitError objec gt 168 value 169 170 def arg_error self metho TraitError The max_storage trait of s trait_handlers pyc in error self object name value Es value name self full_info object name d arg_num object name value a Reservoir instance must be a float but a value of 23 13 3 3 Documentation By essence all the traits do provide documentation about the model itself The declarative approach to the creation of classes makes it self descriptive from traits api import HasTraits Str class Reservoir HasTraits name Ser max_storage Float 100 Float The desc metadata of the traits can be used to provide a more descriptive information about the trait from traits api import HasTraits Str class Reservoir HasTraits Float name Str max_storage Float 100 desc Maximal storage hm3 Let s now define the complete reservoir class from traits api import HasTraits Str Float Range class Reservoir HasTraits name Str max_storage Float 1le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m ls fficiency Range 0 def energy_production self release Returns the energy producti REE ee on Wh for the given releas m3 s po
266. rst the function computing the derivative of the position needs to be defined gt gt gt def calc_derivative ypos time counter_arr counter_arr 1 return 2xypos An extra argument counter_arr has been added to illustrate that the function may be called several times for a single time step until solver convergence The counter array is defined as gt gt gt counter np zeros 1 np uint16 The trajectory will now be computed gt gt gt from scipy integrate import odeint gt gt gt time_vec np linspace 0 4 40 gt gt gt yvec info odeint calc_derivative 1 time_vec args counter full_output True Thus the derivative function has been called more than 40 times 6 7 Numerical integration scipy integrate 115 Python Scientific lecture notes Release 2011 1 gt gt gt counter array 129 dtype uint16 and the cumulative iterations number for the 10 first convergences can be obtained by gt gt gt aonfol nie 210 array Sil S35 43 8 S83 Si SO 6S GS G2 chesjoe imme 32 The solver requires more iterations at start The final trajectory is seen on the Matplotlib figure 1 0 0 8 0 6 E 2 3 2 0 4 gt 0 2 0 05 10 15 20 25 30 35 4 0 Time s Another example with odeint will be a damped spring mass oscillator 2nd order oscillator The position of a mass attached to a spring obeys the 2nd order ODE
267. s Write well spaced code put whitespaces after commas around arithmetic operators etc gt gt gt a 1 yes gt gt gt a 1 too cramped A certain number of rules for writing beautiful code and more importantly using the same conventions as anybody else are given in the Style Guide for Python Code e Use meaningful object names 2 7 Input and Output To be exhaustive here are some information about input and output in Python Since we will use the Numpy methods to read and write files you may skip this chapter at first reading We write or read strings to from files other types must be converted to strings To write in a file gt gt gt f open workfile w opens the workfile file gt gt gt type f lt type file gt gt gt gt f write This as a test nand another test gt gt gt f close To read from a file in 1 open worktile E Il Ta altos f read In 3 print s Tars iis a CESE and another test In 4 close For more details http docs python org tutorial inputoutput html 2 7 1 Iterating over a file In 6 open workfile r In 7 for line in f print line This is a test and another test ine 8 f close 2 7 Input and Output 32 Python Scientific lecture notes Release 2011 1 File modes e Read only r e Write only w Note Create a new file o
268. s e extension package to Python for multidimensional arrays e closer to hardware efficiency e designed for scientific computation convenience For example An array containing e discretized time of an experiment simulation e signal recorded by a measurement device e pixels of an image gt gt gt import numpy as np gt gt gt a np array 0 1 2 3 gt gt gt a 3 1 2 Reference documentation e On the web http docs scipy org e Interactive help gt gt gt help np array array object dtype None copy True order None subok False ndmin 0 Create an array Parameters object array_like Examples gt gt mera il 2 Sil Greeny il 2 31 Looking for something gt gt gt np lookfor create array Search results for create array numpy array Create an array numpy memmap 3 1 The numpy array object 42 Python Scientific lecture notes Release 2011 1 Creat a memory map to an array stored in a binaryx file on disk gt gt gt help np lookfor 3 1 3 Creating arrays 1 D gt gt a A NO laps eye oly gt gt gt a arcay LOP ale 2p Si gt gt gt a ndim TL gt gt gt a shape Bo gt gt gt len a 4 2 D 3 D gt gt gt b np array TPO 1 2 7 iis 47 Sli gt gt gt b array IO 1 2 3p 4 21 gt gt gt b nidam 2 g
269. s KeyError MESS 7 3 2 Using generators to define context managers When discussing generators page 142 it was said that we prefer generators to iterators implemented as classes because they are shorter sweeter and the state is stored as local not instance variables On the other hand as described in Bidirectional communication page 143 the flow of data between the generator and its caller can be bidirectional This includes exceptions which can be thrown into the generator We would like to implement context managers as special generator functions In fact the generator protocol was designed to support this use case contextlib contextmanager def some_generator lt arguments gt SeLup CEY yield lt value gt mime Myg lt cleanup gt The contextlib contextmanager helper takes a generator and turns it into a context manager The gen erator has to obey some rules which are enforced by the wrapper function most importantly it must yield exactly once The part before the yield is executed from __enter_ the block of code protected by the con text manager is executed when the generator is suspended in yield and the rest is executed in __exit_ If an exception is thrown the interpreter hands it to the wrapper through __exit__ arguments and the wrapper function then throws it at the point of the yield statement Through the use of generators the context manager is shorter and simpler Let s rewrite t
270. s Release 2011 1 gt gt gt mean_bubble_size median_bubble_size MIST 1651 0 6 12 Summary exercises on scientific computing 138 Part II Advanced topics 139 CHAPTER 7 Advanced Python Constructs author Zbigniew Jedrzejewski Szmek This chapter is about some features of the Python language which can be considered advanced in the sense that not every language has them and also in the sense that they are more useful in more complicated programs or libraries but not in the sense of being particularly specialized or particularly complicated It is important to underline that this chapter is purely about the language itself about features supported through special syntax complemented by functionality of the Python stdlib which could not be implemented through clever external modules The process of developing the Python programming language its syntax is unique because it is very transparent proposed changes are evaluated from various angles and discussed on public mailing lists and the final decision takes into account the balance between the importance of envisioned use cases the burden of carrying more language features consistency with the rest of the syntax and whether the proposed variant is the easiest to read write and understand This process is formalised in Python Enhancement Proposals PEPs As a result features described in this chapter were added after it was shown that they indeed s
271. s like this AE tiolo Aea 1 Draw two figures one 5 by 5 one 10 by 10 inches 2 Add four subplots to one figure Add labels and ticks only to the outermost axes 3 Place a small plot in one bigger plot 5 8 Other Types of Plots So far we have used only line plots matplotlib offers many more types of plots We will have a brief look at some of them All functions have many optional arguments that are not shown here The function bar creates a new bar chart bar l 2 3 4 3 71 Now we have three bars starting at 1 2 and 3 with height of 4 3 7 respectively 5 8 Other Types of Plots 101 Python Scientific lecture notes Release 2011 1 ixj Yo HO O aja The default column width is 0 8 It can be changed with common methods by setting width As it can be color and bottom we can also set an error bar with yerr or xerr The function barh creates an vertical bar chart Using the same data cuda fil Ap Silly lp Si VI We get HO Ol ea We can also have discontinuous vertical bars with broken_barh We specify start and width of the range in y direction and all start width pairs in x direction yrange 2 il ran ges TO 0 57 CE 2 ale el broken_barh xranges yrange We changes the extension of the y axis to make plot look nicer ax gca ax
272. sc Inflows hm3 release Range low min_release high max_release spillage Float desc Spillage hm3 update_storage Event desc Updates the storage to the next time step TE def _update_storage_fired self update storage state new_storage self storage self releas self inflows self storage min new_storage self max_storage overflow new_storage self max_storage self spillage max overflow 0 def print_state self print Storage tRelease tInflows tSpillage Ste format NE Joan 427 26 for in range 4 1 print str_format format self storage self release self inflows self spillage preine name main__ projectA Reservoir name Project A max_storage 30 max_release 5 0 hydraulic_head 60 efficiency 0 8 state ReservoirState reservoir projectA storage 15 state release 5 state inflows 0 13 3 What are Traits 251 Python Scientific lecture notes Release 2011 1 release the maximum amount of water during 3 time steps state update_storage True state print_state state update_storage True state print_state state update_storage True state print_state Dependency between objects can be made automatic using the trait Property The depends_on attribute ex presses the dependency between the property and other traits When the other traits gets changed the property
273. se graphical toolkits for visualization GTK Qt gt gt gt import scikits image io as im_io gt gt gt im_io use_plugin gtk imshow gt gt gt im_io imshow 1 3 D visualization Mayavi See 3D plotting with Mayavi page 263 and Volumetric data page 266 e Image plane widgets e Isosurfaces 12 3 Basic manipulations Images are arrays use the whole numpy machinery 12 3 Basic manipulations 229 Python Scientific lecture notes Release 2011 1 gt gt gt gt gt gt 166 gt gt gt gt gt gt array 158 KESI MESA gt gt gt piae gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt lena scipy lena lena 0 40 A SCLLeCTaG Tena LLORA 3r 2023 15 6 Ss RS Sie Laa y E Sal lena 100 120 255 1x ly lena shape x Y npeogesd Opis mask X 1x 2 2 Masks lena mask 0 Fancy indexing lena range 400 Oy Ss Ea range 400 12 3 1 Statistical information gt gt gt lena scipy lena gt gt gt lena mean 124 04678344726562 gt gt gt lena max lena min 2457 25 12 3 Basic manipulations 230 Python Scientific lecture notes Release 2011 1 np histogram 12 3 2 Geometrical transformations gt gt gt lena scipy lena gt gt gt lx ly lena shape gt gt gt Cropping gt gt gt crop_lena
274. sing raise except Exception as e print yveld raised lr format e else print gt Tela returned ial gt fornat ans gt gt gt it g gt gt gt next it etorL yielding 10 gt gt gt it send 11 yield returned 11 Syateulelalsoe it dl gt gt gt it throw IndexError E yield raised IndexError ellas 2o 2 gt gt gt it close MOS Note next or__next_ In Python 2 x the iterator method to retrieve the next value is called next It is invoked implicitly through the global function next which means that it should be called __next__ Just like the global function iter calls iter__ This inconsistency is corrected in Python 3 x where it next becomes it __next__ For other generator methods send and throw the situation is more complicated because they are not called implicitly by the interpreter Nevertheless there s a proposed syntax extension to allow cont inue to take 7 1 iterators generator expressions and generators 144 Python Scientific lecture notes Release 2011 1 an argument which will be passed to send of the loop s iterator If this extension is accepted it s likely that gen send will become gen __send__ The last of generator methods close is pretty obviously named incorrectly because it is already invoked implicitly 7 1 5 Chaining generators Note This is a preview of PEP 380 not yet
275. some advantages in interpolation 3 3 6 Summary amp Exercises e There is a number of data types with different precisions In some special cases you may need to care about this Str uctured arrays contain data of a composite type Lumping pieces of data together this way has various possible uses Fourier transform routines are under np fft Masked arrays can be used for missing data Polynomials are available in various bases 3 3 Moving on 84 Python Scientific lecture notes Release 2011 1 3 4 Under the hood 3 4 1 It s ndarray block of memory indexing scheme data type descriptor e raw data how to locate an element e how to interpret an element 3 4 2 Block of memory ndarray gt gt gt x np array 1 2 3 4 dtype np int32 gt gt gt x data lt read write buffer for Oxa37bfd8 size 16 offset 0 at Oxa4eabe0 gt gt gt gt Str x data x01 x00 x00 x00 x02 x00 x00 x00 x03 x00 x00 x00 x04 x00 x00 x00 Memory address of the data gt gt gt x __array_interface__ data 0 15975576 Reminder two ndarrays may share the same memory gt gt gt x np array 1 2 3 4 25 gt y el gt gt gt x 0 9 gt gt gt y array Sr a SS 21 gt gt gt y base is x True Memory does not need to be owned by an ndarray gt gt gt x x01 x02 x03 x04 gt gt gt y np frombuffer x dtype np int8
276. st a name or a call The bare name approach is nice less to type looks cleaner etc but is only possible when no arguments are needed to customise the decorator Decorators written as functions can be used in those two cases gt gt gt def simple _decorator function print doing decoration return function gt gt gt simple _decorator def function print inside function doing decoration gt gt gt function inside function 7 2 Decorators 146 Python Scientific lecture notes Release 2011 1 gt gt gt def decorator_with_arguments arg print Vdeiining CHE decoratori def decorator function in this inner function arg is available too print doing decoration arg return function return _decorator gt gt gt decorator_with_arguments abc def function print inside function defining the decorator doing decoration abc gt gt gt function inside function The two trivial decorators above fall into the category of decorators which return the original function If they were to return a new function an extra level of nestedness would be required In the worst case three levels of nested functions gt gt gt def replacing decorator with args arg print Vdertinangaene decoraron def _decorator function in this inner function arg is available too print doing decoration 7 arg def _wrapper xargs x xkwargs print inside wrapper args kwar
277. sys stdout Prints the values to a stream or to sys stdout by default Optional keyword arguments file a file like object stream defaults to the current sys stdout sep string inserted between values default a space end string appended after the last value default a newline 1 3 The interactive workflow IPython and a text editor 6 Python Scientific lecture notes Release 2011 1 1 3 2 Elaboration of the algorithm in an editor Create a file my_file py in a text editor Under EPD you can use Scite available from the start menu Under Python x y you can use Spyder Under Ubuntu if you don t already have your favorite editor I would advise installing Stani s Python editor In the file add the following lines s Hello world print s Now you can run it in ipython and explore the resulting variables In 3 trun my_file py Hello word In 4 s Out 4 Hello word In 5 Swhos Variable Type Data Info s SEN Hello word From a script to functions e A script is not reusable functions are e Thinking in terms of functions helps breaking the problem in small blocks 1 3 The interactive workflow IPython and a text editor 7 CHAPTER 2 The Python language authors Chris Burns Christophe Combelles Emmanuelle Gouillart Ga l Varoquaux Python for scientific computing We introduce here the Python language Only the bare minimum necessary
278. t is invoked by next the statements up to the first yield are executed The second next prints middle and execution halts on the second yield The third next prints finished and falls of the end of the function Since no yield was reached an exception is raised What happens with the function after a yield when the control passes to the caller The state of each generator is stored in the generator object From the point of view of the generator function is looks almost as if it was running in a separate thread but this is just an illusion execution is strictly single threaded but the interpreter keeps and restores the state in between the requests for the next value Why are generators useful As noted in the parts about iterators a generator function is just a different way to create an iterator object Everything that can be done with yield statements could also be done with next methods Nevertheless using a function and having the interpreter perform its magic to create an iterator has advantages A function can be much shorter than the definition of a class with the required next and__iter__ methods What is more important it is easier for the author of the generator to understand the state which is kept in local variables as opposed to instance attributes which have to be used to pass data between consecutive invocations of next on an iterator object A broader question is why are iterators useful When an iterator is us
279. t a np arange 25 reshape 5 5 elementwise with the array b np array 1 5 10 15 20 Hint np newaxis 3 Harder one Generate a 10 x 3 array of random numbers in range 0 1 For each row pick the number closest to 0 5 e Use abs and argsort to find the column j closest for each row e Use fancy indexing to extract the numbers Hint a i j the array i must contain the row numbers corresponding to stuff in 3 Exercise 2 2 Data statistics The data in populations txt describes the populations of hares and lynxes and carrots in northern Canada during 20 years gt gt gt data np loadtxt data populations txt gt gt gt year hares lynxes carrots data T trick columns to variables 3 2 Numerical operations on arrays 71 Python Scientific lecture notes Release 2011 1 ESS le axsatid 2 Del 03 0 81 gt gt gt plt plot year hares year lynxes year carrots gt gt gt pilt legend C Hane lynx aro oe OS OE gt gt gt plt show 80000 70000 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 1900 1905 1910 1915 1920 Computes and print based on the data in populations txt 1 The mean and std of the populations of each species for the years in the period 2 Which year each species had the largest population 3 Which species has the largest population for each year Hint argsort amp fancy indexing of np ar
280. t gt data np round rand 2 3 gt gt gt data array MIO Os Taly OO leah e assign the data using fancy indexing eres Smee A al 27 Sill S data gt gt gt mtx lt 4x5 sparse matrix of type lt type numpy float64 gt with 2 stored elements in Linked List format gt gt gt gt print mtx Oz 2S i 0 ES 1O gt gt gt mtx todense macca U oy DO Mar Oily 11 2 Storage Schemes 211 Python Scientific lecture notes Release 2011 1 i Oar Ocy Wop Wap Well Ones Oye SOR eee TO Orn O Oey VOR EI gt gt gt mtx toarray array 0 0 0 1 0 O O Wo Welz Ar Won ay Op Oslo A On O O OA more slicing and indexing coos MEX Sps li cis LO te 2 Wil Sp OF 1 wil tp O 07 a gt gt gt mtx todense merci Ol i Aa 01 155 0 Ty 01 0 a Op Oy Ah gt gt gt print mtx 0 1 1 OPS 2 CIMON 3 EZ iji AO iL EST 1 gt gt MEXXI 27 lt 2x4 sparse matrix of type lt type numpy int32 gt with 4 stored elements in Linked List format gt gt gt gt mtx 2 todense matrix EO Ll 2 01 Sp Of de Ol gt gt gt mtx 1 2 0 2 todense marci en A gt gt gt mtx todense matraca CLO i 2 Oil 37 Op Ie Cl lo 0 0 dy Dictionary of Keys Format DOK subclass of Python dict keys are row column index tuples no duplicate entries allowed values are corresponding
281. t gt gt b shape 2 3 gt gt gt len b 2 gt gt gt c np array 11 211 31 4 gt gt gt c array 1 Lads 3 La gt gt gt c shape a 2 dl F 2 x 3 array returns the size of the first dimension In practice we rarely enter items one by one e Evenly spaced gt gt gt import numpy as np gt gt gt a np arange 10 0 n 1 C gt gt gt a array LOr di SU De a ip Sr Sn end exlusive step gt gt gt b np ia range lil 9 2 7 start gt gt gt b array d ere or by number of points gt gt gt c np linspace 0 1 6 gt gt gt c array O y Wat Ont Wa 0 8 start end num points is gt gt gt d np linspace 0 1 5 endpoint False gt gt gt d array O 02 O 010 Weil 3 1 The numpy array object 43 Python Scientific lecture notes Release 2011 1 e Common arrays gt gt gt a np ones 3 3 reminder 3 3 is a tuple gt gt gt a la than y Ba a al or E eS gt gt gt b np zeros 2 2 gt gt gt arre UE De Welly OMA gt gt gt c np eye 3 gt gt gt C cre Ill Meg Os Welly oca ley OR On On mN gt gt gt d mnp diag np arcay il 25 si 4 Sna gt gt gt d eye LT 0 0 0 0 Or Zo DD O Ol Op De 37 De Oil OO ay On Or Of OF Wp BE e Random numbers Mersenne Twister PRNG gt gt gt a np random ra
282. t gt gt im np zeros 256 256 gt gt gt im 64 64 64 64 1 SSS gt gt gt im ndimage rotate im 15 mode constant gt gt gt im ndimage gaussian_filter im 8 Use a gradient operator Sobel to find high intensity variations gt gt gt sx ndimage sobel im axis 0 mode constant gt gt gt sy ndimage sobel im axis 1 mode constant gt gt gt sob np hypot sx sy Sobel filter square Sobel x direction Sobel for noisy image Canny filter The Canny filter is available in the scikits image doc but for convenience we ve shipped it as a standalone module with this tutorial gt gt gt from scikits image filter import canny gt gt gt or use module shipped with tutorial gt gt gt im 0 1l np random random im shape gt gt gt edges canny im 1 0 4 0 2 not enough smoothing gt gt gt edges canny im 3 0 3 0 2 better parameters Several parameters need to be adjusted risk of overfitting 12 5 Feature extraction 236 Python Scientific lecture notes Release 2011 1 12 5 2 Segmentation e Histogram based segmentation no spatial information gt gt gt n 10 gt gt gt 1 256 gt gt gt im np zeros 1 1 gt gt gt np random seed 1 gt gt gt points 1lxnp random random 2 nx x 2 gt gt gt im points 0 astype np int points 1 astype np int 1 gt gt gt im ndimag
283. t ini decorator initi arg self arg arg def call self function this method is called to do the job print in decorator call self amg e return function gt gt gt deco_instance decorator_class foo in decorator init foo gt gt gt deco_instance 7 2 Decorators 147 Python Scientific lecture notes Release 2011 1 def function xargs xxkwargs prine in runet ion Ar args kwargs in decorator call foo gt gt gt function jin function Contrary to normal rules PEP 8 decorators written as classes behave more like functions and therefore their name often starts with a lowercase letter In reality it doesn t make much sense to create a new class just to have a decorator which returns the original function Objects are supposed to hold state and such decorators are more useful when the decorator returns a new object gt gt gt class replacing decorator _class object def init self arg this method is called in the decorator expression Print in decorator aint oy arg self arg arg def call self function this method is called to do the job print an decorator call t sel arg self function function return self _wrapper def _wrapper self xargs kwargs print in the wrapper args kwargs Rea return self function xargs kwargs gt gt gt deco_instance replacing_decorator_class foo in decorator init Eos gt gt gt deco_instance def f
284. te al Ge ae 11 3 Linear System Solvers os ana dee es BBD cig hk aa we ah ae dO eh BYR a AR 11 4 Other Interesting Packages c coco cd 6 eH RAE we ee ea Image manipulation and processing using Numpy and Scipy 12 1 Opening and writing toamage tiles sy wAc aya OG a Behe ye ye amp See 122 Displaying images sass pe oe rra ORR RRR aa ERE 12 3 Basie mampulations 4 2 4 6 4 4 3 44 oes be oe haw bau oe bbe PERS eee E 124 Image filterme sp ek SR Ee eh ee A 12 5 Feature extvacvion o ee Sente Bae Ae GRA DORA Dee Ee ae EE a ee 12 6 Measuring Objects Properties sips rara a Re ke SO Traits 1301 Introduction 24 4 2 6 5 hAwe a Ee ee ee ee ee oR Be Ae 13 2 Example osos woe Ryde ee ARES Bee AA ae ES feet ae Kie 133 Wihatare Traits eb e dito fds Bg ti do ban a oa a tow ds ar 139 140 141 145 153 156 157 169 178 185 186 186 190 190 192 193 197 200 200 201 203 204 206 206 208 220 224 226 227 228 229 231 236 239 244 245 245 246 WA Referentes ose soa eR Ge ea Ge a A Ge BP a de th BP A A a i 14 3D plotting with Mayavi 14 1 A simpleexample isis Wea Ob oe OL A ee ee A Ue E 14 2 SD plots MUnChONS 4 2 Di e Ace fa y BRS AA Gehan e Bid 14 3 Fisures and decorations ee pu mts Ge ba PRR eR ae ee SE EY oR J4 4 Interaction oi cis bie e eek bee A bee ee ese 15 Sympy Symbolic Mathematics in Python 15 1 First Steps withiSyMPy aos ge ee a dog we has ck Be ee Bode ale ae eee ae AS 15 2 A
285. te plus future input saved Filename commands log Mode backup Output logging False Raw input log False Timestamping False State active 2 3 Assignment operator Python library reference says Assignment statements are used to re bind names to values and to modify attributes or items of mutable objects In short it works as follows simple assignment 1 an expression on the right hand side is evaluated the corresponding object is created obtained 2 a name on the left hand side is assigned or bound to the r h s object Things to note e a single object can have several names bound to it m Ul a ely 2 311 In 2 b I w 2 3 Assignment operator 15 Python Scientific lecture notes Release 2011 1 Ea ik a Owell Mi 2p 3 In 4 b Orela l 2 3 In 5S a is 6 Out 5 True Ta Lele 6 na in F7 a Oueb ASE e to change a list in place use indexing slices ia Ali a i 2 3l In 3 a ome se lp 27 31 In 4 a a b c Creates another object agen ISi a Ome ode Mea r Me ren In 6 ida Out 6 138641676 In 7 a 1 2 3 Modifies object in place In 83 a iene ene i 2 2 Te Toe EA Out 9 138641676 Same as in Out 6 yours will differ the key concept here is mutable vs immutable mutable objects can be changed in place immutable objects cannot be modified once created A very good and
286. the current solution vector LinearOperator Class from scipy sparse linalg interface import LinearOperator common interface for performing matrix vector products e useful abstraction that enables using dense and sparse matrices within the solvers as well as matrix free solutions e has shape and matvec some optional parameters e example gt gt gt import numpy as np gt gt gt from scipy sparse linalg import LinearOperator gt gt gt def mv v return np array 2 v 0 3x v 1 1 gt gt gt A LinearOperator 2 2 matvec mv gt gt gt A lt 2x2 LinearOperator with unspecified dtype gt gt gt gt A matvec np ones 2 array 2h Sell gt gt gt A x np ones 2 array ll 2on Sell 11 3 Linear System Solvers 222 Python Scientific lecture notes Release 2011 1 A Few Notes on Preconditioning e problem specific e often hard to develop if not sure try ILU available in dsolve as spilu 11 3 3 Eigenvalue Problem Solvers The eigen module e arpack a collection of Fortran77 subroutines designed to solve large scale eigenvalue problems e lobpcg Locally Optimal Block Preconditioned Conjugate Gradient Method works very well in com bination with PyAMG example by Nathan Bell non Compute eigenvectors and eigenvalues using a preconditioned eigensolver In this example Smoothed Aggregation SA is used to precondition the LOBPCG eigensolver on a two
287. ti Ome Ad Es nye but it is good practice to use the same ordering as the function s definition Keyword arguments are a very convenient feature for defining functions with a variable number of arguments especially when default values are to be used in most calls to the function 2 5 4 Passed by value Can you modify the value of a variable inside a function Most languages C Java distinguish passing by value and passing by reference In Python such a distinction is somewhat artificial and it is a bit subtle whether your variables are going to be modified or not Fortunately there exist clear rules Parameters to functions are references to objects which are passed by value When you pass a variable to a function python passes the reference to the object to which the variable refers the value Not the variable itself If the value is immutable the function does not modify the caller s variable If the value is mutable the function may modify the caller s variable in place gt gt gt def try to modity y Z x 23 y append 42 z 99 new reference print x print y print z gt gt gt a 77 immutable variable gt gt gt b 99 mutable variable gt gt gt C 28 gt gt gt ey tonmodirty la ej E 23 RIS A2 SIS gt gt gt print a 77 gt gt gt print b OSA gt gt gt print c 28 II Functions have a local variable table Called a loc
288. tific lecture notes Release 2011 1 Block of memory char data Data type descriptor PyArray_Descr xdescr Indexing scheme int nd npy_intp dimensions npy_intp strides Other stuff PyObject xbase ant eelags PyObject x weakreflist PyArrayObject 8 1 2 Block of memory gt gt gt X np array I 2 3 4l dtype np Int32 gt gt gt x data lt read write buffer for Oxa37bfd8 size 16 offset 0 at Oxa4eabe0 gt gt gt gt str x data x01 x00 x00 x00 x02 x00 x00 x00 x03 x00 x00 x00 x04 x00 x00 x00 Memory address of the data gt gt gt x __array_interface__ data 0 ESTE Reminder two ndarrays may share the same memory gt gt gt x np array 1 2 3 4 Seo Wh E gt gt gt x 0 9 gt gt gt y eure IS 2 Sl Memory does not need to be owned by an ndarray gt gt gt x le Ay gt gt gt y np frombuffer x dtype np int8 gt gt gt y data lt read only buffer for 0xa588ba8 size 4 offset 0 at Oxa55cd60 gt gt gt gt y base is x True gt gt gt y flags C CONTTGEUGUS i TTrUS F_CONTIGUOUS True OWNDATA False WRITEABLE False ALIGNED True UPDATEIFCOPY False The owndata and writeable flags indicate status of the memory block 8 1 Life of ndarray 158 Python Scientific lecture notes Release 2011 1 8 1 3 Data types Th
289. tottime percall cumtime percall filename lineno function dl 14 457 14 457 14 479 14 479 decomp py 849 svd all 0 054 0 054 0 054 0 054 method random_sample of mtrand RandomState object dl 0 017 OSO 05021 0 021 function _base py 645 asarray_chkfinite 54 oromi 0 000 OO dat 0 000 numpy core _dotblas dot 2 0 005 0 002 0 005 0 002 method any of numpy ndarray objects 6 0 001 0 000 0 001 0000 weary soba Germe 6 0 001 0 000 0 001 OOOO teenid LSZ 14 OOO 0 000 oooi 0 000 numpy linalg lapack_lite dsyevd 19 0 001 0 000 0 001 0 000 twodim_base py 204 diag dl 0 001 0 001 0 008 oco0e iea py oe apar al 0 001 0 001 14 551 14 551 execfile 107 0 000 0 000 0 001 0 000 defmatrix py 239 __array_finalize__ 7 0 000 0 000 0 004 0 001 ica py 58 _sym decorrelation 7 0 000 0 000 0 002 0 000 linalg py 841 eigh Lye 0 000 0 000 0 000 0 000 isinstance 1 0 000 0 000 14 551 14 551 demo py 1 lt module gt 29 0 000 0 000 0 000 0 000 numeric py 180 asarray 35 0 000 0 000 0 000 0 000 defmatrix py 193 __new__ 35 0 000 0 000 0 001 0 000 defmatrix py 43 asmatrix 21 0 000 0 000 0 001 0 000 defmatrix py 287 __mul__ 41 0 000 0 000 0 000 0 000 numpy core multiarray zeros 28 0 000 0 000 0 000 0 000 method transpose of numpy ndarray objects dl 0 000 0 000 0 008 ORCS meras WA ase ea Clearly the svd in decomp py is what takes most of our time a k a the bottleneck We have to find a way to make this step go
290. tudent anna gt gt gt anna set_age 21 gt gt gt anna set_major physics In the previous example the Student class has init__ set_age and set_major methods Its at tributes are name age and major We can call these methods and attributes with the following notation classinstance method or classinstance attribute The __init__ constructor is a special method we call with MyClass init parameters if any Now suppose we want to create a new class MasterStudent with the same methods and attributes as the previous one but with an additional internship attribute We won t copy the previous class but inherit from it gt gt gt class MasterStudent Student internship mandatory from March to June gt gt gt james MasterStudent james gt gt gt james internship mandatory from March to June gt gt gt James set_age 23 gt gt gt jJames age 23 The MasterStudent class inherited from the Student attributes and methods Thanks to classes and object oriented programming we can organize code with different classes corresponding to different objects we encounter an Experiment class an Image class a Flow class etc with their own methods and attributes Then we can use inheritance to consider variations around a base class and re use code Ex from a Flow base class we can create derived StokesFlow TurbulentFlow PotentialFlow etc 2 10 Object oriented progr
291. ture notes Release 2011 1 Note that the last index is not included gt gt gt a A array 10 L 27 Si start end step isa slice object which represents the set of indexes range start end step A slice can be explicitly created gt SS O 2 gt gt gt a np arange 10 gt gt gt b np arange 1 20 2 gt gt gt a b array IO te 2 Sy Uy dy Sy Te Sp BI era lA Se Do Wy Dv Lily IS LS 17 pp gt asl Ds arar gs 5 Wi array SY dil si All three slice components are not required by default start is 0 end is the last and step is 1 gt gt gt a 1 3 ar lil 21 gt gt gt aillisa2 array Ol A 6 Sil gt gt gt ME array lo 4 9 On vn 3r 91 Of course it works with multidimensional arrays gt gt gt a np eye 5 gt gt gt a ene lAs Us Os Us Well Oay thee E We Wels Os Wes ise De Wall Ory MO NOR walk Wear Ory One Ok Ole CN A coll Sill A Sd and AEN moms 9 Es ia Columns array IL Oar Olin Lal Oa Mer Wasi All elements specified by a slice can be easily modified gt gt gt a 3 73 4 gt gt gt a array 7 o oly fey O a SS oo ff qa TA SS de ene Sr eS SS 1 A small illustrated summary of Numpy indexing and slicing 3 1 The numpy array object 49 Python Scientific lecture notes Release 2011 1
292. un ny Edit properties Name Plot Editor 13 4 References e ETS repositories http github com enthought 13 4 References 261 Python Scientific lecture notes Release 2011 1 e Traits manual http github enthought com traits traits_user_manual index html e Traits UI manual http github enthought com traitsui traitsui_user_manual index html e Mailing list enthought dev enthought com 13 4 References 262 CHAPTER 14 3D plotting with Mayavi author Ga l Varoquaux 14 1 A simple example Warning Start ipython wthread import numpy as np xp y op nmgr A TOLO 00n TOO 0 0 SS ea e A Sas from enthought mayavi import mlab mlab surf z warp_scale auto mlab outline mlab axes np mgrid 10 10 100j 10 10 100j creates an x y grid going from 10 to 10 with 100 steps in each directions 263 Python Scientific lecture notes Release 2011 1 14 2 3D plotting functions 14 2 1 Points In 1 import numpy as np In 2 from enthought mayavi import mlab In 3 x y z value np random random 4 40 In 4 mlab points3d x y z value Out 4 lt enthought mayavi modules glyph Glyph object at 0xc3c795c gt ef er Pe b 14 2 2 Lines ro S mlabiclEG In 6 t np linspace 0 20 200 ta 7 ico aloe so aa sae y mo cos e Omie E Out 7 lt enthought mayavi modules surface Surface o
293. unction args xkwargs print Vain unction t args kwargs in decorator call foo gt gt gt function lea in the wrapper 11 12 ab ebria A A A decorator like this can do pretty much anything since it can modify the original function object and mangle the arguments call the original function or not and afterwards mangle the return value 7 2 3 Copying the docstring and other attributes of the original function When a new function is returned by the decorator to replace the original function an unfortunate consequence is that the original function name the original docstring the original argument list are lost Those attributes of the original function can partially be transplanted to the new function by setting __doc___ the docstring module__ and __name___ the full name of the function and ___ annotations___ extra information about arguments and the return value of the function available in Python 3 This can be done automatically by using functools update_wrapper functools update_wrapper wrapper wrapped Update a wrapper function to look like the wrapped function gt gt gt import functools gt gt gt def better_replacing_decorator_with_args arg print defining the decorator def _decorator function print doing decoration arg def _wrapper args xxkwargs print inside wrapper args kwargs return function xargs kwargs 7 2 Decorators 148
294. ur figure We use the Anti Grain Geometry toolkit http www antigrain com to render our figure First we import the backend 9 then we create a new can vas that renders our figure 10 We save our figure in a png file with a resolution of 80 dpi 11 We can use several GUI toolkits directly So we import Tkinter 12 as well as the corresponding backend 13 Now we have to do some basic GUI programming work We make a root object for our GUI 14 and feed it together with our figure to the backend to get our canvas 15 We call the show method 1 6 pack our widget 17 and call the Tkinter mainloop to start the application 18 You should see GUI window with the figure on your screen After closing the screen the next part the script will be executed We would like to create a screen display just as we would use pylab Therefore we import a helper 19 and pylab itself 20 We create a normal figure with pylab 21 and get the corresponding figure manager 22 Now let s set our figure we created above to be the current figure 23 and let pylab show the result 24 The lower part of the figure might be cover by the toolbar If so please adjust the figsize for pylab accordingly 1 Use the object oriented API of matplotlib to create a png file with a plot of two lines one linear and square with a legend in it 5 9 The Class Library 108 CHAPTER 6 Scipy high level scientific com
295. uring element Parameters input array_like Binary array_like to be dilated Non zero True elements form the subset to be dilated structure array_like optional Structuring element used for the dilation Non zero elements are considered True If no structuring element is provided an element is generated with a square connectivity equal to one iterations int float optional The dilation is repeated iterations times one by default If iterations is less than 1 the dilation is repeated until the result does not change anymore mask array_like optional If a mask is given only those elements with a True value at the corresponding mask element are modified at each iteration output ndarray optional Array of the same shape as input into which the output is placed By default a new array is created origin int or tuple of ints optional Placement of the filter by default 0 border_value int cast to 0 or 1 Value at the border in the output array Returns out ndarray of bools Dilation of the input by the structuring element See Also Find help lt a Previous gt Next Highlight all Match case 3 Reached end of page continued from top e Scipy s cookbook http www scipy org Cookbook gives recipes on many common problems frequently en countered such as fitting data points solving ODE etc e Matplotlib s website http matplotlib sourceforge net features a very nice gallery with a large
296. ve spsolve mtx rhs print rezidual T np lanalge normi mes x rhs e examples direct_solve py 11 3 2 Iterative Solvers the isolve module contains the following solvers bicg BIConjugate Gradient 11 3 Linear System Solvers 221 Python Scientific lecture notes Release 2011 1 bicgstab BIConjugate Gradient STA Bilized cg Conjugate Gradient symmetric positive definite matrices only cgs Conjugate Gradient Squared gmres Generalized Minimal RESidual minres MINimum RESidual qmr Quasi Minimal Residual Common Parameters e mandatory A sparse matrix dense matrix LinearOperator The N by N matrix of the linear system b array matrix Right hand side of the linear system Has shape N or N 1 e optional x0 array matrix Starting guess for the solution tol float Relative tolerance to achieve before terminating maxiter integer Maximum number of iterations Iteration will stop after maxiter steps even if the specified tolerance has not been achieved M sparse matrix dense matrix LinearOperator Preconditioner for A The preconditioner should ap proximate the inverse of A Effective preconditioning dramatically improves the rate of convergence which implies that fewer iterations are needed to reach a given error tolerance callback function User supplied function to call after each iteration It is called as callback xk where xk is
297. veform_2 npy that contains three signif icant peaks You must adapt the model which is now a sum of Gaussian functions instead of only one Gaussian peak 6 12 Summary exercises on scientific computing 132 Python Scientific lecture notes Release 2011 1 25 20 15 10 Intensity bins 0 10 20 30 40 50 60 70 80 Time ns e In some cases writing an explicit function to compute the Jacobian is faster than letting least sq esti mate it numerically Create a function to compute the Jacobian of the residuals and use it as an input for leastsq e When we want to detect very small peaks in the signal or when the initial guess is too far from a good solution the result given by the algorithm is often not satisfying Adding constraints to the parameters of the model enables to overcome such limitations An example of a priori knowledge we can add is the sign of our variables which are all positive With the following initial solution gt gt gt x0 np array 3 50 20 1 dtype float compare the result of scipy optimize leastsq and what you can get with scipy optimize fmin_slsqp when adding boundary constraints 6 12 Summary exercises on scientific computing 133 Python Scientific lecture notes Release 2011 1 6 12 3 Image processing application counting bubbles and unmolten grains Det Spot Mag 10 4 mm 15 0 kV SSD 5 8 41x 3 25 mm MV 15 05 2009 1280 C Statement of the problem
298. vm SVC kernel patySyc svm SVC kernels rbf degree 3 gt gt gt gamma inverse of size of gt gt gt degree polynomial deyxred radial kernel Exercise Which of the kernels noted above has a better prediction performance on the digits dataset 16 2 Classification 282 Python Scientific lecture notes Release 2011 1 16 3 Clustering grouping observations together Given the iris dataset if we knew that there were 3 types of iris but did not have access to their labels we could try unsupervised learning we could cluster the observations into several groups by some criterion 16 3 1 K means clustering The simplest clustering algorithm is k means This divides a set into k clusters assigning each observation to a cluster so as to minimize the distance of that observation in n dimensional space to the cluster s mean the means are then recomputed This operation is run iteratively until the clusters converge for a maximum for max_iter rounds An alternative implementation of k means is available in SciPy s cluster package The scikit learn implementation differs from that by offering an object API and several additional features including smart initial ization gt gt gt from scikits learn import cluster datasets gt gt gt iris datasets load_iris gt gt gt k_means cluster KMeans k 3 gt gt gt k_means fit iris data KMeans copy_x True init k m
299. w dtype np int64 shape 512 512 data are read from the file and not loaded into memory Working on a list of image files gt gt gt for 1 in range 10 im np random random_integers 0 255 10000 reshape 100 100 o misc imsave random_ 02d png i im gt gt gt from glob import glob gt gt gt filelist glob randon PAg gt gt gt filelist sort 12 2 Displaying images Use matplot lib and imshow to display an image inside a matplotlib figure gt gt gt 1 scipy lena gt gt gt import matplotlib pyplot as plt gt gt gt plt imshow 1 cmap plt cm gray lt matplotlib image AxesImage object at 0x3c7f 710 gt Increase contrast by setting min and max values gt gt gt plt imshow 1 cmap plt cm gray vmin 30 vmax 200 lt matplotlib image AxesImage object at 0x33ef750 gt gt gt gt Remove axes and ticks gt Os O 0 3 Bl Dio 55 Draw contour lines gt Plenconeouc is O Z lt matplotlib contour ContourSet instance at 0x33f8c20 gt 100 200 300 12 2 Displaying images 228 Python Scientific lecture notes Release 2011 1 For fine inspection of intensity variations use interpolation nearest gt gt gt plt imshow 1 200 220 200 220 cmap plt cm gray gt gt gt plt imshow 1 200 220 200 220 cmap plt cm gray d interpolation nearest Other packages sometimes u
300. w python org about for more information about distinguishing features of Python Python Scientific lecture notes Release 2011 1 2 1 First steps Start the Ipython shell an enhanced interactive Python shell e by typing ipython from a Linux Mac terminal or from the Windows cmd shell e or by starting the program from a menu e g in the Python x y or EPD menu if you have installed one of these scientific Python suites If you don t have Ipython installed on your computer other Python shells are available such as the plain Python shell started by typing python in a terminal or the Idle interpreter However we advise to use the Ipython shell because of its enhanced features especially for interactive scientific computing Once you have started the interpreter type gt gt gt Print isllo wore Hello world The message Hello world is then displayed You just executed your first Python instruction congratulations To get yourself started type the following stack of instructions SMS gt gt gt b 2xa gt gt gt type b lt type int gt gt gt gt print b 6 gt gt gt axb S gt gt gt b hello gt gt gt type b ENDS sta gt gt gt 6b b hellohello gt gt gt 2xb hellohello Two variables a and b have been defined above Note that one does not declare the type of an variable before assigning its value In C conversely one should
301. wer 1000 x 9 81 self head release x self efficiency return power 3600 if name _ main_ reservoir Reservoir name Project A max_storage 30 max_release 100 0 head 60 efficiency 0 8 release 80 print Releasing m3 s produces kWh format 13 3 What are Traits 248 Python Scientific lecture notes Release 2011 1 release reservoir energy_production release 13 3 4 Visualisation The Traits library is also aware of user interfaces and can pop up a default view for the Reservoir class reservoirl Reservoir reservoirl edit_traits a rm os Edit properties Efficiency 0 0 Head 10 Hydraulic head 60 Max release 100 0 1 0 0 8 Max storage 30 0 Name Project A OK Cancel TraitsUI simplifies the way user interfaces are created Every trait on a HasTraits class has a default editor that will manage the way the trait is rendered to the screen e g the Range trait is displayed as a slider etc In the very same vein as the Traits declarative way of creating classes TraitsUl provides a declarative interface to build user interfaces code from traits api import HasTraits Str Float Range from traitsui api import View class Reservoir HasTraits if STE max_storage Float le6 desc Maximal storage hm3 max_release Float 10 desc Maximal release m3 s Float 10 desc Hydrauli
302. why did we use a random permutation 16 2 2 Support vector machines SVMs for classification Linear Support Vector Machines SVMs try to construct a hyperplane maximizing the margin between the two classes It selects a subset of the input called the support vectors which are the observations closest to the separating hyperplane 16 2 Classification 281 Python Scientific lecture notes Release 2011 1 gt gt gt from scikits learn import svm gt gt gt svc svm SVC kernel linear gt gt gt SVC fie risa data IriS Carget SVC C 1 0 cache_size 200 coef0 0 0 degree 3 gamma 0 0 probability False shrinking True tol 0 001 kernel linear There are several support vector machine implementations in scikit learn The most commonly used ones are svm SVC svm NuSVC and svm LinearSVC SVC stands for Support Vector Classifier there also exist SVMs for regression which are called SVR in scikit learn Excercise Train an svm SVC on the digits dataset Leave out the last 10 and test prediction performance on these observations Using kernels Classes are not always separable by a hyperplane so it would be desirable to have a decision function that is not linear but that may be for instance polynomial or exponential RBF kernel Radial Basis Func Polynomial kernel tion Linear kernel gt gt gt svc svm SVC kernel 2mpesve s
303. x 2 4 It is possible to compute definite integral gt gt gt integrated 1m 0 gt gt gt integrate sin x x 0 pi 2 il gt gt gt integrate cos x x pi 2 pi 2 2 Also improper integrals are supported as well gt gt gt integrate exp X x 0 0o il gt gt gt integrate exp Xxx x2 x 00 00 pix 1 2 15 3 6 Exercises 15 4 Equation solving SymPy is able to solve algebraic equations in one and several variables lm 4 solvetxx44 i x Ote 1118 ap il Sil 1 15 4 Equation solving 275 Python Scientific lecture notes Release 2011 1 As you can see it takes as first argument an expression that is supposed to be equaled to 0 It is able to solve a large part of polynomial equations and is also capable of solving multiple equations with respect to multiple variables giving a tuple as second argument iin IS solve lz r y 2 gt x ce Gry 15l We wi QUES EA AS It also has limited support for trascendental equations In 9 solve exp x 1 x CUASI Api Another alternative in the case of polynomial equations is factor factor returns the polynomial factorized into irreducible terms and is capable of computing the factorization over various domains im 101 E xd xa L im At Dector i Out 11 1 x xx 2 1 x Xxx2 im 121 Factor E modulus 5 Owe UA 8
304. x tp_is_gc x tp_bases EA tp cache x tp subclasses tp_weaklist tp_del x tp_version_tag 8 3 Interoperability features 184 Python Scientific lecture notes Release 2011 1 NULL NULL NULL NULL hi PyObject PyInit_myobject void PyObject m xd if PyType_Ready amp PyMyObject_Type lt 0 return NULL a ll PyModule_Create amp moduledef da PyModule_GetDict m Py_INCREF amp PyMyObject_Type PyDict_SetItemString d MyObject PyObject amp PyMyObject_Type return m 8 4 Siblings chararray maskedarray matrix chararray vectorized string operations A A Ae peas aya gt gt ls ip chararccay tar bbb cast dtype S5 gt gt gt xX upper chararray MAL ABBE SCE ir dtype S5 Note view has a second meaning it can make an ndarray an instance of a specialized ndarray subclass MaskedArray dealing with propagation of missing data e For floats one could use NaN s but masks work for all types Seo oe I inoue chee il A Sip i meske 0 2 0 11 gt gt gt x eel cry data 1 E mask Fals True Fals True fill_value 999999 gt gt gt y np smavacray 1 27 3 41 mask 0 1 1 T gt gt gt x y mass tara dare 2 DS mask Fals Tru Tru True fill_value 999999 e Masking versions of common functions
305. x todense mcr 0 WO OF 01 107 O 07 Cll 0 0 0 0 dtype int8 e create empty BSR matrix with 3 2 block size gt gt gt mtx sps bsr_matrix 3 4 blocksize 3 2 dtype np int8 gt gt gt mtx lt 3x4 sparse matrix of type lt type numpy int8 gt with 0 stored elements blocksize 3x2 in Block Sparse Row format gt gt gt gt mtx todense marc MO 0 0 Cll 10 0 0 Wl 0 0 0 0 dtype int8 abug e create using data ij tuple with 1 1 block size like CSR gt gt mow mpr arcay 10 0 1 2 2 21 gt gt gt col np array 0 2 2 O 17 21 Lees ela ao cua A Sip Le De Bl gt gt gt mtx sps bsr_matrix data row col shape 3 3 gt gt gt mtx lt 3x3 sparse matrix of type lt type numpy int32 gt with 6 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt gt mtx todense marc A 0 0 3 29 So SI gt gt gt mtx data array DI 101 AMG Sle Ala 511 11 2 Storage Schemes 218 Python Scientific lecture notes Release 2011 1 6 gt gt gt mtx indices array 10 2 27 Op Lo 21 gt gt gt mtx indptr array Or 2 3 Gl e create using data indices indptr tuple with 2 2 block size gt gt gt indptr np array 0 2 3 6 gt gt dinchices mo array IO 2 2 O i 21 gt gt gt data np array 1 2 3 4 5 6 repeat 4 r
306. x y execute the following imports at startup gt gt gt import numpy gt gt gt import numpy as np gt gt gt from pylab import x gt gt gt import scipy and it is not necessary to re import these modules 2 6 3 Creating modules If we want to write larger and better organized programs compared to simple scripts where some objects are defined variables functions classes and that we want to reuse several times we have to create our own modules Let us create a module demo contained in the file demo py A demo module def print_b nBr imes D perne al def print_a Prints as print a c 2 d 2 In this file we defined two functions print_a and print_b Suppose we want to call the print_a function from the interpreter We could execute the file as a script but since we just want to have access to the function print_a we are rather going to import it as a module The syntax is as follows In 1 import demo In 2 demo print_a a In 3 demo print_b b Importing the module gives access to its objects using the module object syntax Don t forget to put the module s name before the object s name otherwise Python won t recognize the instruction Introspection In 4 demo Type module Base Class lt type module gt Strang Horm lt module demo from demo py gt Namespace Interactiv File home varoquau Proj
307. x01BB15D0 gt 1 Mis ak geal In 16 ax annotate Here is something special xy 2 1 xytext 1 5 Out 16 lt matplotlib text Annotation instance at 0x01BB1648 gt In 17 ax annotate Here is something special xy 2 1 xytext 1 5 AE BOWPEO PS NE aces como 1 Annotate a line at two places with text Use green and red arrows and align it according to figure points and data 5 6 Ticks Well formatted ticks are an important part of publishing ready figures matplotlib provides a totally con figurable system for ticks There are tick locators to specify where ticks should appear and tick formatters to make ticks look like the way you want Major and minor ticks can be located and formatted independently from 5 6 Ticks 98 Python Scientific lecture notes Release 2011 1 each other Per default minor ticks are not shown i e there is only an empty list for them because it is as NullLocator see below There are several locators for different kind of requirements Class Description NullLocator no ticks IndexLocator locator for index plots e g where x range len y LinearLocator evenly spaced ticks from min to max LogLocator logarithmically ticks from min to max MultipleLocator ticks and range are a multiple of base either integer or float AutoLocator choose a MultipleLocator and dynamically reassign All of these locators derive from the base class matplot
308. x_er peime iise kero bse db Step by step execution Situation You believe a bug exists in a module but are not sure where For instance we are trying to debug wiener_filtering py Indeed the code runs but the filtering does not work well e Run the script with the debugger In 1 Srun d wiener_filtering py xxx Blank or comment xxx Blank or comment xxx Blank or comment Breakpoint 1 at home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_f NOTE Enter c at the ipdb gt prompt to start your script gt lt string gt 1 lt module gt Enter the wiener_filtering py file and set a break point at line 34 ipdb gt n gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 4 3 gt gt 4 amport numpy as np 5 import scipy as sp ipdb gt b 34 Breakpoint 2 at home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_f Continue execution to next breakpoint with c ont inue ipdb gt c gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 34 BS wae UA 34 noisy_img noisy_img 35 denoised_img local_mean noisy_img size size 9 3 Using the Python debugger 195 Python Scientific lecture notes Release 2011 1 e Step into code with n ext and s tep next jumps to the next statement in the current execution context while st ep will go across
309. y Click the Register link to get an account e Mailing lists scipy org Mailing Lists If you re unsure No replies in a week or so Just file a bug ticket Good bug report Title numpy random permutations fails for non integer arguments I m trying to generate random permutations using numpy random permutations When calling numpy random permutation with non integer arguments it fails with a cryptic error message gt gt gt np random permutation 12 array 107 Cre Ae Te Sie Bir OF Do Sp it aa 2 gt gt gt np random permutation long 12 Traceback most recent call last Hales Soil ibas dl im ot File mtrand pyx line 3311 in mtrand RandomState permutation File mtrand pyx line 3254 in mtrand RandomState shuffle ypeError len of unsized object This also happens with long arguments and so np random permutation X shape 0 where X is an array fails on 64 bit windows where shape is a tuple of longs It would be great if it could cast to integer or at least raise a proper error for non integer types I m using Numpy 1 4 1 built from the official tarball on Windows 64 with Visual studio 2008 on Python org 64 bit Python 0 What are you trying to do 1 Small code snippet reproducing the bug if possible e What actually happens e What you d expect 2 Platform Windows Linux OSX 32 64 bits x86 PPC 3 Version of Numpy Scipy gt gt gt pri
310. y strides 276 gt gt gt Str y data NO A OON CAN OON OTA OONO N OAOA ONO EN OON OA OON ENEON EN 00 e Need to jump 2 bytes to find the next row e Need to jump 6 bytes to find the next column e Similarly to higher dimensions 3 4 Under the hood 86 Python Scientific lecture notes Release 2011 1 C last dimensions vary fastest smaller strides F first dimensions vary fastest shape d1 do dn strides 81 82 Sn sF dj 1dj 2 dn x itemsize s7 dida d 1 x itemsize Slicing e Everything can be represented by changing only shape strides and possibly adjusting the data pointer e Never makes copies of the data gt gt gt x np array il 2 3 4 5 6l dtype npr intezi gt gt gt y ee gt gt gt y cute Sp 4 Se 2 1 gt gt gt y strides 4 gt gt gt y x 2 gt gt gt y __array_interface__ data 0 x __array_interface__ data 0 8 gt gt gt x np zeros 10 10 10 dtype np float gt gt gt x strides SOO BO te gt gt Sis e e r as telde ss LSO 2410 32 e Similarly transposes never make copies it just swaps strides gt gt gt x np zeros 10 10 10 dtype np float gt gt gt x strides S007 SiO E gt gt gt x T ystrides 8 80 800 Reshaping But not all reshaping operations can be represented by playing with strides gt
311. ython org or PyQt http www riverbankcomputing co uk software pyqt fhtro e Numpy and Scipy http www scipy org e Enthought Tool Suite 3 x or higher http code enthought com projects e All required software can be obtained by installing the EPD Free http www enthought com products epd php Tutorial content e Introduction page 245 e Example page 245 e What are Traits page 246 Initialisation page 247 Validation page 247 Documentation page 248 Visualisation page 249 Deferral page 250 Notification page 255 Some more advanced traits page 258 e References page 261 244 Python Scientific lecture notes Release 2011 1 13 1 Introduction The Enthought Tool Suite enable the construction of sophisticated application frameworks for data analysis 2D plotting and 3D visualization These powerful reusable components are released under liberal BSD style licenses The main packages are e Traits component based approach to build our applications e Kiva 2D primitives supporting path based rendering affine transforms alpha blending and more e Enable object based 2D drawing canvas e Chaco plotting toolkit for building complex interactive 2D plots e Mayavi 3D visualization of scientific data based on VTK e Envisage application plugin framework for building scriptable and extensible applications Mayavi _TVTK Traits VTK Envisage sgous
312. ython2 6 lib dynload ausr lib python2 6 dist packages usr lib pymodules python2 6 usr lib pymodules python2 6 gtk 2 0 usr lib python2 6 dist packages wx 2 8 gtk2 unicode usr local lib python2 6 dist packages ausr lib python2 6 dist packages 2 6 Reusing code scripts and modules 29 Python Scientific lecture notes Release 2011 1 usr lib pymodules python2 6 IPython Extensions u home gouillar ipython Modules must be located in the search path therefore you can e write your own modules within directories already defined in the search path e g ust local lib python2 6 dist packages You may use symbolic links on Linux to keep the code somewhere else e modify the environment variable PYTHONPATH to include the directories containing the user defined modules On Linux Unix add the following line to a file read by the shell at startup e g etc profile profile export PYTHONPATH SPYTHONPATH home emma user_defined_modules On Windows http support microsoft com kb 3 10519 explains how to handle environment variables e or modify the sys path variable itself within a Python script import sys new_path home emma user_defined_modules if new_path not in sys path sys path append new_path This method is not very robust however because it makes the code less portable user dependent path and because you have to a

Download Pdf Manuals

image

Related Search

Related Contents

Hi-Copy User's Guide Revision 2.1  Kate`s Draft Manual  hl-2-1300p_2007-12(7) [igg ief].qxp  ELECTRIC NAILGUN Model 32003  Corpus OTG - Université François Rabelais  HP StorageWorks Disk System 2100 (fld rck 0 drvs) User's Manual  BLDCモータ・ベクトル制御開発プラットフォーム(概要)  

Copyright © All rights reserved.
Failed to retrieve file