Home

Python Scientific lecture notes Release 2012.3 (EuroScipy - e

image

Contents

1. An ill conditionned quadratic function Note that as the quadratic approximation is exact the Newton method is blazing fast An ill conditionned non quadratic function 1 ELI A Here we are optimizing a Gaussian which is always below its quadratic approximation As a result the Newton method overshoots and leads to oscillations An ill conditionned very non quadratic function In scipy the Newton method for optimization is implemented in scipy optimize fmin nocg cg here refers to that fact that an inner operation the inversion of the Hessian is performed by conjugate gradient scipy optimize fmin tnc can be use for constraint problems although it is less versatile gt gt gt def f x Ihe rosenbrock function P return 25 40 I oo e se Dur Sc Os Zee gt gt gt def fprime x return Nosarray 27 5 1 BIST 42ex O0 gt Coll x0 ee2 I T 0 ese 2 e e gt gt gt optimize fmin ncg f 2 2 fprime fprime Optimization terminated successfully Current function value 0 000000 Iterations 10 Humctvon evaluar toms 12 Gradient evaluations 44 Hessian evaluations 0 array cla lt i Note that compared to a conjugate gradient above Newton s method has required less function evaluations but more gradient evaluations as it uses it to approximate the Hessian Let s compute the Hessian and pass it to the algorithm gt gt gt def hessian x Computed
2. data indices indptr tuple efficient column slicing column oriented operations slow row slicing expensive changes to the sparsity structure use actual computations most linear solvers support this format 11 2 Storage Schemes 217 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Examples create empty CSC matrix gt gt gt mtx Sparse csc matrix 3 4 dtype enp rnts9 gt gt gt mtx todense t merge q ccs OF NO XO DO xo cda TO LOS D 05 Olly deype ints create using data ij tuple 22 gt crow MP anran 10 0 1 2 2 uw gt gt gt COll mp array O 2 42 Oe ale Sq gt gt gt data np array ly 2 3 uw 3 61 gt gt gt mtx sparse csc_matrix data row col shape 3 3 gt gt gt mtx 3x3 sparse matrix of type type numpy into4 with 6 stored elements in Compressed Sparse Column format gt gt gt MEX T Odense 1 RIT O I SOG 2 LO zs Oy a 4 5 6 gt gt gt mtx data amaan a 24 37 6 gt gt gt mtx indices cuu HDR dedu cxcexdts pede SOR INEO Ide e dag 2 o Ol dtype aT 2 L e create using data indices indptr tuple gt gt gt data np earray l 4 5 2 3 6 poc Indices mp gccavebye eg 0 iy 12 pom congu Mp array 0 2 3 61 gt gt gt mtx Sparse csce Matrix data Indices xndptr shape 3 3 gt gt gt mtx todense Matra I 390 2217 Gye Ol s 2 o UNI Block Compr
3. 5 8 10 Arrow Plots Plotting arrows in 2D plane can be achieved with quiver We define the x and y coordinates of the arrow shafts gt gt gt x y np arange 10 The x and y components of the arrows are specified as 2D arrays gt gt gt u np ones 10 10 gt gt gt v np ones 10 10 gt gt gt u 4 4 3 gt gt gt ww ds S a Now we can plot the arrows gt gt gt Plequaver or yr Uy V 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 N N N N N N NN N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 7 7 Pd 7 7 A 7 N T 5 8 11 Scatter Plots Scatter plots print x vs y diagrams We define x and y and make some point in y random gt gt gt x np arange 10 gt gt gt y np arange 10 gt gt gt y 1 7 gt gt gt y 4 2 gt gt gt y 8 3 Now make a scatter plot gt gt gt Plwscauwer x y The three different values for y break out of the line 5 8 Other Types of Plots 101 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 5 8 12 Sparsity Pattern Plots 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 gt gt gt 1 np identity 10 gt gt gt i darrar Els Oy 09
4. Style guidelines 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 Spaces 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 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 27 Input and Output it Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt f open workfile w opens the workfile file gt gt gt type f type file gt gt gt fwrite This is ao test Anand another test gt gt gt f close To read from a file In 1 open workfile r In 2 s f read In 3 print s This is a test and another test In 4 f close For more details http docs python org tutorial inputoutput html 2 7 1 Iterati
5. gt gt gt lin fit diabetes X train diabetes y train LinearRegression gt gt gt lin score diabetes X test diabetes y test Qu OD T5902 dos 17 6 Linear model from regression to sparsity 303 Different algorithms for a same problem Different algorithms can be used to solve the same mathematical problem For instance the Lasso object in the sklearn solves the lasso regression using a coordinate descent method that is efficient on large datasets However the sklearn 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 17 7 Model selection choosing estimators and their parameters 17 7 1 Grid search and cross validated estimators Grid search The scikit learn provides an object that given data computes the score during the fit of an estimator on a parameter 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 sklearn import svm grid search gt gt gt gammes mnmp logspace t 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 CIT TIC cq e H peso duae stem s qp GridSearchCVv cv None estimator SVC C 1 0 gt gt gt Clivbest score Ope Sooo nee Ee g
6. strides Maxaxisi 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 Ho OOS ecl o n ea T EU elei Ge Erame O23 ee tor EH ss bino pyenonm2 6 silte packages mumpy core arrayprini at APvbhon cevaltcso50 2750 sel PVEROnN Ceva loe No such filo or dirt tO tn s Python ceval c gdp up 9 PyEval_EvalFrameEx f Erame 0x2537lec for file home varoguau usr lib pyehionzeo site packagcec mumay eore arrayprini at x Python ceval c 24T2 2412 am EE eC eee 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 hone arogo Wem Mle Ehon o ste packages MUMMY COmc cinco ENDE ussng trailing gdb This is numpy code we need to go up until we find code that we have written gdb up gdb up 34 0x080dc97a in PyEval EvalFrameEx f Prame xoA2r064c tor tite segtault py line 11 in print big_array smalil_array tnumpy ndarra S30 2 7 Python Cova l e No such Tile or dir tO unt Ete Some ees gdb pyframe Seqtreumle vp 12 Print brog array The corresponding code is def print big array small_ array big array make big array small array Prine big_arrayl 10 return big array Thus the segfault happens when printing big array 10 The reas
7. LinearLocator evenly spaced ticks from min to max logarithmically ticks from min to max MultipleLocator ticks and range are a multiple of base either integer or float choose a MultipleLocator and dynamically reassign All of these locators derive from the base class matplotlib ticker Locator You can make your own locator deriving from it locator for index plots e g where x range len y Handling dates as ticks can be especially tricky Therefore matplotlib provides special locators in matplotlib dates locate specified days of the month ocate months e g 10 for October locate years that are multiples of base 5 6 3 Tick Formatters Similarly to locators there are formatters 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 PormatotrbormatLter gt gt gt major locator pl M ltrpleLocator 2 gt gt gt Major formatter p1l FPormatcStrrormatter 45 27 gt gt gt minor locator pl Multiplekocator 1 gt gt ax xXaxIS Ser major locator major locator gt gt gt ax xaxis set_minor_locator minor_locator gt gt gt ax Xxaxis set major formatter major formatter gt gt gt pl draw After we redraw the figure our x axis should lo
8. Pul Eq Edel dte e l Ev cH ede lo r ddelitem r le r delslice r len Ta COG poaae 2 2 Basic types r append 3 1l pop isour first example of object oriented programming OOP Being a list the object r owns the method function that is called using the notation No further knowledge of OOP than understanding the notation is necessary for going through this tutorial Setattr Setitem Setslice S1Z2e0f Sa Sree subclasshook__ append 12 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 aC Olin eq MESE CXII UE poe v Getattribute ne extend Pintle poses noe we POP SIBI e getitem reduce ex x Gets lace TI shes repr reversed reverse bos esc e pe Dp eR B wo omw ea ma ley a D a E qe d SS CH E Strings Different string syntaxes simple double or triple quotes Hello how are you Har owes up PU elio tripling the quotes allows the how are vou the string to span more than one line panes eee waat e Tjo yui Pile lt ipython console line 1 Hip what o up SEE ror invalid e nta The newline character is n and the tab character is t Strings are collections like lists Hence they can be indexed and sliced using the same syntax and rules Indexing a hello Remember that negative indices correspond to counting from the right end Slicing h
9. Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt mtx data dra ci iude Sy 561p gt gt gt MUX indices array l0 2 2 0 1 2 dtypesint 2 gt gt gt Ioco cot array 0 25 Su Gilly dbyvpDeernm 32 e create using data indices indptr tuple gt gt gt data csimpesrics V p 2 3 cds o po indices e mp array iO 2p 2 Op lp 219 coc indptr Npsarray 0 99 gt gt gt mtx sparse csr_matrix data indices indptr shape 3 gt gt gt mtx todense macax i On n ere dese 47 55 6 Compressed Sparse Column Format CSC column oriented three NumPy arrays indices indptr data indices is array of row indices data is array of corresponding nonzero values indptr points to column starts in indices and data S x lengthisn col 1 last item number of values length of both indices and data x nonzero values of the i th column are data indptr i indptr it l with row indices indices indptr i indptr itl x item i j can be accessed as data indptr j k where k is position of i in indices andper Ia T sandper iade TT subclass of _cs_matrix common CSR CSC functionality subclass of data matrix sparse matrix classes with data attribute fast matrix vector products and other arithmetics sparsetools constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data ij tuple
10. Slicing Arrays like other Python sequences can also be sliced gt gt gt a np arange 10 oo SS dup don en oo SH dou TO dee Sp gt gt gt a 2 9 3 start end step xm D el Note that the last index is not included gt gt gt a 4 annar D 1 27 31 All three slice components are not required by default start is 0 end is the last and step is 1 gt gt gt rds n E Z gt gt gt als 2 S E E UO SO gt gt gt cse arra Le 42 OS secus S 3 1 The numpy array object 47 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 A small illustrated summary of Numpy indexing and slicing gt gt gt a 0 3 5 array 3 4 gt gt gt a 4 4 array 44 45 54 55 gt gt gt a 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 M T ap e Ec we dae Tx sey D gt gt Dp Salers le bD array 0 2 4 6 9 gt gt gt b 0 12 gt gt gt b arcas 12 2 4 o oi gt gt gt a 2000 array 0 le E Sm 4 D S E S 91 gt gt gt a np arange 10 gt gt gt b af 2 copy L force a cop
11. We want to have the whiskers well within the plot and therefore increase the y axis gt gt gt ax pl gca gt gt gt ax 3et ylim O 12 gt gt gt draw Our plot looks like this 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 5 8 5 Contour Plots We can also do contour plots We define arrays for the x and y coordinates Kags X y np arange 10 and also a 2D array for z gt gt gt z np ones 10 10 gt gt gt z 5 5 7 gt gt gt z 2 1 3 gt gt gt z 8 7 4 gt gt gt Z 5 8 Other Types of Plots 98 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 eee aL do Js ee ile hue 3hee o h c Gi hf Lals podes CODE xg Wie Ghee bos los thas tag heal lbs Sth lea ile shes they Ber they Weep deadly UT gic die dee dae Lan Las Ghee Goa a ET L I bsc ean Wer wer Joe gk Ee dha eae Rn INTER tlhe e e bsr T ban ban bamn EET e eder ken ke Per Ton Ta To Ear Dily dios be dioe ul ome hax a leg JE bey E Tep tee ber ten Bap Tap Tan Aan Tap Baly 1 1 1 1 Ta il il il il eel B S Now we can make a simple contour plot So ET CDU Our plot looks like this TIO eju We can also fill the area We just use numbers form 0 to 9 for the values v BU
12. an os ligro e OS Croa Out 42 False Delete a file In 44 fp open junk txt w In 45 fp close In 46 junk txt in os lvsidir os curdir Out 46 True In 47 os remove junk ext In 48 juni Cxe dinoosslv storT os curear Out 48 False 2 8 Standard Library 33 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 os path path manipulations os path provides common operations on pathnames In 70 fo openm gunibcbxctt w In 71 fp close Tn 721 In 73 Owe bya In 74 Gur lye In 78 Ou 1 eJ In 79 Oia Ope In 80 Out soul In 84 Eo ds In 86 Out oo In 87 Que 53s In 88 Out eal In 92 Ole koZ E a os path abspath junk txt a CdMIsers coubms shO scepveDOUD sciloy 2000 tuvertal source junk exc CS perm col a JUsers courns sre scipy0090 scipy 2000 ctuborialy sommes junk txt os path dirname a P Users courmis src scipy20097 scipy_2000_tutorial source os path basename a v p scis os path splitext os path basename a nk ge ale ae OS T wens rae qn wea True OSs Dab hale aiken quik E True OS Pathes dair REER gi cee False OS pati expanduser local 7JUsers courns local Je T OS Pacls On Os paelheexpandusern Local Vente c purns local pum Running an external command In 8 CO
13. dy 2 3 5 IS dtype inelo gt gt gt y np array 5 6 7 dtype np intl16 gt gt gt y2 as strided y strides 1 2 0 shape 3 4 gt gt gt y2 array llor gt r xe T rm e Te ea a Gp db lom 24 K array Cll Ss 10 se SD r 754 La 21 281 dtyoe i1nct 16 seems somehow familiar gt gt gt x np array ll 4 3 4 dtiypesnp ntl6 po y npsarray Ibp 6 7l devpsenpswmnle 2 x npsnewdousw amp ylen neas Ds abray 5 10 15 20 6 ees Wed Ty M4 21 281 dtyvpe rinti6 e Internally array broadcasting is indeed implemented using O strides More tricks diagonals See Also stride diagonals py Challenge Pick diagonal entries of the matrix assume C memory order 8 1 Life of ndarray 169 gt gt gt x No Array IIl 215 a o Sle dere en 37 poo X Gag as serided x Shape 37 7 Strides e Pick the first super diagonal entries 2 6 e 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 s dua arrey l 5 915 dtyvoe rint 32 Slice first to adjust the data pointer gt gt gt as strided x 0 1 shape 2 strides 3 1 x itemsize J array 2 6 dtype int32 gt gt gt as strideg x l 0 shape 2 strides 3 1 x 1temsize a
14. gt gt gt alidx arrcay ls her np gt gt gt b Python Scientific lecture notes Release 2012 3 EuroScipy 2012 arange 10 hy 4 AT arange 10 The image below illustrates various fancy indexing applications gt gt gt a 0 1 2 3 4 1 2 3 4 5 array 1 12 23 34 45 gt gt gt al3 0 2 5 array 30 32 35 40 42 45 50 52 55 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 page 58 at the same time gt gt gt a np arange 12 reshape 3 4 gt gt gt a array Li Op dy I 29 a CX E eee le Pee Ss deo SRI 2o cx mp array I0 1l dE 211 gt gt gt a i 2 same as a i 2 np ones 2 2 dtype int annan HRF os i 30 0 Ju 3 2 Numerical operations on arrays Section contents Elementwise operations page 51 Basic reductions page 53 Broadcasting page 58 Array shape manipulation page 61 Sorting data page 64 Some exercises page 65 Summary page 71 3 2 1 Elementwise operations With scalars 3 2 Numerical operations on arrays 51 gt gt gt a np array l 2 3 ww uw array Z 3 4 51 gt gt gt 2x xa ale teal way 4 Sy Led All arithmetic operates elementwise gt gt gt b np ones 4 1 gt gt gt a b arcrcay M i x MEy EIT gt gt gt ax b abiay 22 7 4 Oar Sa gt g
15. np array 1 3 min max argmin of Minimum argmax of maximum 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 np any a Q False gt gt gt np all a a True 3 2 Numerical operations on arrays 54 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 e and many more best to learn as you go 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 gt gt gt from matplotlib import pyplot as plt woo updates e 0 1 0 57 076l matplotlib axes Axes object at gt moo Olt plor ysar Meares year lynxes year Carrots mat plor lib lines Line D object ab xe 14 5 gt gt gt plt legendi Hare n iyn S arrot loc 1 2 05 0 3 lt matplotlib legend Legend object at gt 80000 70000 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 1500 1905 1910 1915 1920 The mean populations over time po PO Ula tions esae es gt gt gt populations mean axis 0 array 24020 95238095 201669560066007 42400
16. 5 8 Other Types of Plots 5 8 1 Many More So far we have used only line plots matplot1ib 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 5 8 2 Bar Charts The function bar creates a new bar chart gt gt gt a ZR a NC a Now we have three bars starting at 1 2 and 3 with height of 4 3 7 respectively 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 5 8 3 Horizontal Bar Charts The function barh creates an vertical bar chart Using the same data gt gt gt plos 225 9 ps os RB We get Broken Horizontal Bar Charts 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 5 8 Other Types of Plots 97 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt yrange 2 1 Seo lt tanges ClO 0 5 e e quo 3 gt gt gt pl broken_barh xranges yrange We changes the extension of the y axis to make plot look nicer gt gt gt ax pl gca oor Fax see a OS 07 5 gt gt gt pl draw and get this Figure ipi xi 5 8 4 Box and Whisker Plots We can draw box and whisker plots ploboxplot arange 2 10 arange 1 5
17. And visualize the solution 22 Plies plots uve nme we Medel a 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 waveform_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 11 Summary exercises on scientific computing 135 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Intensity bins 0 10 20 30 AQ 50 60 70 80 Time ns n some cases writing an explicit function to compute the Jacobian is faster than letting 1eastsq esti mate it numerically Create a function to compute the Jacobian of the residuals and use it as an input for leastsq 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 x0 np array 5 50 20 J dtvpoe rtlost compare the result of scipy optimize leastsq and
18. 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 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 1 2 3 note that varies these are different objects gt gt gt iter nums lt IvStaterator obqect aC ssa 2 numse iter lt listit erator Object at mes 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 1 gt gt gt it next z gt NeXT 10 2
19. Python Scientific lecture notes Release 2012 3 EuroScipy 2012 cdef PyUFuncGenericFunction loop func 1 cdek char cnpu t cousput tvbpesJ s cdef void xelementwise funcs 1 Reminder some pre made Ufunc loops 2222 2222222222 a s VU he ok S float elementwise func float input 1 p psu TER Sp tloat etementwrise crune rlost Input l float input 2 v xo Y MES UE CSI double elementwise func double input 1 t Py HE D ad ud double elementwise func double input 1 double input 2 s MSIE Ese s elementwise func complex double lt input complex double complex double TS ESSE HEIDI elementwise func complex double xinl complex double xin2 complex doublex 22222222222222 a t The full list is above Type codes F 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 T Toop funcool cssc TODO Suitable ES ROTE Taput OULD seyoes O S aa TODO TODOS FLE dem rest or vnpub coucpub tvDes This thing is passed as the data parameter for the generic t PYVUPUMe s loop to let t know vvhich runc Cion it should call elementwise funcs 0 lt void mandel single point a Construct che ace Le mandel PyUFunc_FromFuncAndData
20. Slicing gt gt gt penali 20623 R UMS cse MES EIN LT A Hugs o doe gies IE Suhel gt gt gt lene 100 120 255 SSS gt gt gt lx ly ena senhape sm X o scgrid o Iz 0 ly gt gt gt mask xdi ct qni c yd gt gt gt Masks gt gt gt lena mask U 12 3 Basic manipulations 231 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt Fancy indexing gt gt gt lena range 400 range 400 255 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 245 25 np histogram 12 3 2 Geometrical transformations lena scipy lena ix ly lena shape Cropping crop lena lenall 7 4 lx 4 ly 42 ly 4 Jj UD c MIDI milage flip ud lena np flipud lena rotation rotate lena ndimage rotate lena 45 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 12 4 Image filtering 232 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Neighbourhood square choose size disk or more complicated structuring element Ka ded 1 Wo NC DIIS 12 4 1 Blurring smoothing Gaussian filter from scipy ndimage gt gt gt from scipy import misc gt gt gt lena misc lena gt gt gt blurred_lena
21. T Hence the nogil ft And so all local variables must be declared with edet T Note also that Chis function receives EE to the data the traditional solution to passing complex variables around T cdef double complex z z_ in 0 cdef double complex c c in 0 edet int k wb the integer we use in the tor loop Straightforward iteration for k aim range 100y ge ug cb xe af vogue co oA SIR co 1000 break Return the answer for this point z out 7 Boilerplate Cythom definitions it T You dom t really need to read this part xt Just pulls an stuff from the Numpy C headers cdef extern from nu umpy arrayobject h void import array ctypedef int npy_intp cdef enum NPY TYP rs NPY_CDOUBLE edem extern from numpy ufuncobject hnh vord Impor Uuno Qi mede vold PyYUPFPUNCCGenNericFkFuUnctron Chars MPY onto NPY Ints Vows 8 2 Universal functions 177 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 object PyUFunc FromFuncAndData PyUFuncGenericFunction func voidx data Chars typos In nbyvpesy Spe nin me OU int identity Char name charte doc unt C void PyUkunc UD D char s Py 1ntO s upysimpuwe eds Required mod le initialization import avra import urune Hne setulae l ERN e ies a cdef PyUFuncGenericFunction loop func 1 cder char vnput cou tput tvpesls cdef void xelementwise_funcs 1 Loop liune 0 PyUFUnec ppp input
22. The sample standard deviations gt gt gt populations std axis 0 array 208597 90645909 16254 05915306971 332255062755 Which species has the highest population each year gt gt gt np argmax populations axis 1 array OI T Oye Wy dep Bb 2 ae ey 2p Za cop SUE Op We be d cp yv 3 2 Numerical operations on arrays 55 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 3 2 Numerical operations on arrays 56 Example diffusion simulation using a random walk algorithm AA 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 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 Steps 2 s no random random integers 0 1 mostories tomax 1 gt gt gt Np unique steps Verification all steps are I or 1 amaray L 1p LI 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 x2 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 gt plt figure figsize 4 3 matplotlib figure Pigure object at i cos plitplo
23. damenscvenstOols bg marcm l ror s tua deco med ross inputs npe Output strides for each array input 1 steps 0 iM input 2 steps 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 Only 1 D buffers No data type information C level interface PyBufferProcs tp as buffer in the type object But it s integrated into Python e g strings support it Mini exercise using PIL Python Imaging Library See Also 8 3 Interoperability features 181 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 pilbuffer py gt gt gt import Image gt gt gt data np zeros 200 200 4 dtype 2np int89 gt gt gt dabeles sl 1255 0 0 255 s Red gt gt gt In PIL RGBA images consist of 32 bit integers whose bytes are RR GG BB AA gt gt gt data data view np int32 squeeze gt gt gt img e mageecremburtcer RGBA 200 200 data gt gt gt img save test png Q Check what happens if data is now modified and img saved again
24. def decorator function prine doing decora lon arg def _wrapper xargs xxkwargs print inside wrapper args kwargs return function xargs xxkwargs 7 2 Decorators 151 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 return functools update wrapper wrapper function return decorator gt gt gt better replacing decorator with args abc def function extensive documentation print inside function T return 14 defining the decorator doing decoration abc 529 FUNCE TOM SMC mon wc thom at Ure a gt gt gt print function doc exbenoilve CocumentTat Lom 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 Exam
25. fprime fprime Optimization terminated successfully Current function value 0 000000 Teerabrvons 96 bPunction evaluations 24 Gradient evaluations 24 arra 1200000017 1 2000000226 L BFGS Limited memory BFGS Sits between BFGS and conjugate gradient in very high dimensions 250 the Hessian matrix is too costly to compute and invert L BFGS keeps a low rank version In addition the scipy version scipy optimize fmin l bfgs b includes box bounds gt gt gt def f x The rosenbrock function he return 5s cecus gt gt gt def fprime x return up arraday 2 5 L IRI s Ole Cll cropwe2 4 oc pp o sober 2 1 pO OODptdqaqdzegumarndlcbrgscbu E xe dporuime sptcme larray I 1 00000005 STIT TT I i467 I se to LT CONVERGENCE Note If you do not specify the gradient to the L BFGS solver you need to add approx_grad 1 13 2 4 Gradient less methods A shooting method the Powell algorithm Almost a gradient approach 13 2 A review of the different optimizers 253 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 An ill conditionned quadratic function Powell s method isn t too sensitive to local ill conditionning in low dimensions An ill conditionned very non quadratic function Simplex method the Nelder Mead The Nelder Mead algorithms is a generalization of dichotomy approaches to high dimensional spaces The al gorithm works by refining a simplex the generalization o
26. 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__ ex it__ 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 exit 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 type def enter self pass def exit self type value traceback if type is None raise AssertionError exception expected 7 3 Context managers 157 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 if issubclass type self type return True swallow the expected exception raise AssertionError wrong exception type with assert raises KeyError Dau Roe 7 3 2 Using generators to define
27. gt gt gt from scipy import optimize gt gt gt def f x return np exp x 7 w 2 gt gt gt x min optimize brent f It actually converges in 9 iterations gt gt gt x min 0 059 999090 Q9 TSO urs SO CIT x 2 L605 4 lt 6 10 Note Brents method can be used for optimization constraint to an intervale using Scipy optimize fminbound Note Inscipy 0 11 scipy optimize minimize scalar gives a generic interface to 1D scalar mini mization 13 2 2 Gradient based methods Some intuitions about gradient descent Here we focus on intuitions not code Code will follow Gradient descent basically consists consists in taking small steps in the direction of the gradient Table 13 1 Fixed step gradient descent A well conditionned quadratic function x Ce oc e An ill conditionned quadratic function The core problem of gradient methods on ill conditioned problems is that the gradient tends not to point in the direction of the minimum We can see that very anisotropic ill conditionned functions are harder to optimize 13 2 A review of the different optimizers 249 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Take home message conditioning number and preconditioning If you know natural scaling for your variables prescale them so that they behave similarly This is related to preconditioning Also it clearly can be advantageous to take bigger steps This i
28. gt gt gt next it Traceback most recent call last EeLle lt sedin mue 1 in mocule gt Stoplteration When used in a loop St op Iteration 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 the __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 iter which returns the iterator self Support for iteration 1s 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 fL _iter_ True 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 lterators generator expressions and generators 144 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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
29. meca Os Ih 2 Ody See 2957 ce o gt gt gt mex ee MTIR odense mac a koe a gt gt gt mtx todense meer ie ez 39 23 o bn Oe dy One Os OEIL Dictionary of Keys Format DOK subclass of Python dict keysare row column index tuples no duplicate entries allowed values are corresponding non zero values efficient for constructing sparse matrices incrementally constructor accepts dense matrix array sparse matrix 11 2 Storage Schemes 213 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 shape tuple create empty matrix efficient O 1 access to individual elements flexible slicing changing sparsity structure is efficient can be efficiently converted to a coo matrix once constructed slow arithmetics or loops with dict iteritems use when sparsity pattern is not known apriori or changes Examples create a DOK matrix element by element gt gt gt mtx sparse dok matrix 5 5 dtype np float64 gt gt gt mtx 5x5 sparse Matrix ot type tvpe numpy rloato4q with 0 stored elements in Dictionary Of Keys format gt gt gt for ir in range 5 for ic in range 5 mex lic xe e WO x ccu Je are gt gt gt mtx 5x5 Sperse Matrix ot type lt tyoe numpy tloatoZ2 with 20 stored elements in Dictionary Of Keys format gt gt gt mtx todense Mele ea 40 H o dee E R SRI Dar AOL LA TT benz Eee ugue tear
30. ndimage mean l labels rbin index np arange 1 rbin max 1 12 6 Measuring objects properties ndimage measurements 243 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Du gt gt gt ZUR gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt p gt gt gt Ium gt gt gt def disk structure n struct Mpezeros 2 m r be 2 xe m 5 x y np indices 2 xn t l 2an e c mask x n 2 y n x2 lt nexZ struct mask 1 return struct astype np bool def granulometry data sizes None s max data shape if sizes None sizes range 1 s 2 2 granulo ndimage binary_opening data structure disk structure n sum for n in sizes return granulo np random seed 1 n 10 1 256 im np zeros l 1 points Jano rendom random 2 m 2 imi cpounes 0 sestyoe notch points astyoe moeine e im ndimage gaussian filter im sigma 1 4 n mask im gt im mean granulo granulometry mask sizes np arange 2 19 4 5000 2 4 6 8 10 12 14 16 18 12 6 Measuring objects properties ndimage measurements 244 CHAPTER 13 Mathematical optimization finding minima of functions authors Ga l
31. wm print OK Ga l Nu ud else Bulle raise e UR ES return name In 16 filter _name Ga l OK Ga l Out 16 Ga xec3 xab1 In 17 filter name St fan UnicodeDecodeError ascii codec can t decode byte Oxc3 in position 2 ordinal not in range Exceptions to pass messages between parts of the code In 17 def achilles arrow x qi abog l1 bees raise StopIteration 1 1 x 2 return x In 18 In 19 while True try 2 9 Exception handling in Python 38 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 ere ete x achilles arrow x ee except StopIteration res break In 20 x Ou OO s 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 to organize the code and 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 def set age self age self age age def set major self major self major major gt gt gt anna Student anna gt gt gt anna set_age 21 gt gt gt anna set_major physics In the previous example th
32. 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 gt gt gt y array M Ice 2p Sy Of Teo 91 Now we can plot the dates at the x axis gt gt gt pl plot_date dates y 5 9 The Class Library So far we have used the py1ab 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 5 9 1 The Figure Class The class Figure lives in the module matplotlib figure Its constructor takes these arguments figsize None dpi None facecolor None edgecolor 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 None edgecolor None frameon True 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 py1ab to set properties of the figure Figure also implements get something methods such as get_axes or get_facecolor to get properties of the figure 5 9 The Class Library 103 Python Scienti
33. 1 90 30e3 4e3 48300 oer Gime Mod s 48200 L202 URN NEC SS 41500 gt gt gt data np loadtxt data populations txt gt gt gt data array Uli T200 20000 4000 48300 gt 1901 7 47200 7 Cl00 o2 E 23022 O00 Quee S Ss gt gt gt npsosaevetoxti popz txt data poo data2 np loadt xi pop2 tE Note If you have a complicated text file what you can try are np genfromtxt 3 4 Advanced operations 0 5 1 0 80 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Using Python s I O functions and e g regexps for parsing Python is quite well suited for this Navigating the filesystem with python In 1 pwd show current directory Lone user sturt 20 l numpy eutorial in 2 cd ex home user stuff 2011 numpy tutorial ex In 3 1s populations txt Species Ext Images Using Matplotlib gt gt gt img plt imread data elephant png gt gt gt img shape img dtype CODO 200 3 de pen N 9 231 gt gt gt plt imshow img lt matplotlib image AxesImage object at gt soc plt savosfig ploc png 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 lt matplotlib image AxesImage object at gt Other libraries gt gt gt from scipy misc import imsave gt gt gt AMSavel tiny elephani gong 7 amg Ves 67 246 gt
34. 195 10 Wy FOL 0 1095 014 GOs e dO 105 uO 20 MO Oe E iu oc Odes bos JO dU 10 MOV ds JO OM 2 a POL XO ho dro s ep OR x0 nds Res vil X 105 the HO win dos Aus ONG RO ao WO 10k vb lege JOE UE duoc Ou ROR HOE 205 00s Ol WO A 0 a0 aL Oe uo 30 Oe C 104 UD Nos UT ja 0 MODs ODE 308 v 0 WO Oy ues Oly OG Or Ge OE fL 107 OF WO Oy DIETO Now we look at it more visually 55 pli pyi NIG 1609 5 8 13 Stem Plots 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 gt gt gt pl stem x y f Powe OOOO T OII e 5 8 14 Date Plots There is a function for creating date plots Let s define 10 dates starting at January Ist 2000 at 15 day intervals 5 8 Other Types of Plots 102 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt import datetime gt gt gt dl datetime datetime 2000 1 1 gt gt gt delta datetime timedelta 15 gt gt gt dates dl x delta for x in range l gt gt gt dates datetime datetime 2000 1 1 0 0 datetime datetime 2000 1 16 0 0 datetime datetime 2000 1 31 0 0 datetime datetime 2000 2 15 0 0 datetime datetime 2000 3 1 0 0 datetime datetime 2000 3 16 0 0 datetime datetime 2000 3 31 0 0 datetime datetime 2000 4 15
35. 3 2 Numerical operations on arrays 69 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise Mandelbrot set 1 5 1 0 0 5 0 0 0 5 2 0 1 1 0 0 5 0 0 0 5 1 0 Write a script that computes the Mandelbrot fractal The Mandelbrot iteration N max 50 some threshold 50 Qr Sos T for j in xrange N_max Z Z A2 Cc 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 iteration 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 gt gt plic rmshtowimask r extent 2 2 Ll L 5 d4 5 matplotlib image AxesImage object at gt poc pde gt gt gt plt savefig mandelbrot png 3 2 Numerical operations on arrays 70 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise Markov chain Pj l J Markov chain transition matrix P and probability distribution on the states p 1 0 lt P i j lt 1 probability to go from state i to state j 2 Transition rule Phew P void 3 all sum P axis 1 1 p sum 1 normalization Write a script that works with 5 states and e Constructs a random matrix and normalizes each row so that it 1s a transition matrix e Starts from a random normalized probability distri
36. 3D Plotting functions for numpy arrays Table Of Contents plot3d points3d imshow Example gallery Miab functions gallery Advanced mlab examples Interactive examples Advanced visualization examples Misc examples Previous topic surf Miscellaneous Next topic Boy example This Page Show Source Quick search barchart triangular_mesh contour3d Enter search terms or a module e class or function name gt Finally two more technical possibilities are useful as well n Ipython the magical function psearch search for objects matching patterns This is useful if for example one does not know the exact name of a function 3 import numpy as np 4 psearch np diag diag 2 ELE G Gon l e numpy lookfor looks for keywords inside the docstrings of specified modules in 45 mnumpy lockror convolution Search results for convolution nu mpyconmvodlwe Returns the discrete linear convolution of two one dimensional sequences wame panre ie mE Return the Bartlett window numpy correlate Discrete linear correlation of two l dimensional sequences In 46 numpy lookfor remove module os Search results for remove os remove remove path removedirs removedirs path a ENEL E rmdir patch wL Lak unlink path walk Directory tree generator f everything listed above fails and Google does
37. 60000 Hare 50000 Lynx Carrot 40000 30000 20000 10000 1500 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 npo ebravtltBHt gt 60513 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 3 2 Numerical operations on arrays 68 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise Crude integral approximations Write a function f a b c that returns a ranges 0 1 x 0 1 x 0 1 Approximate the 3 d integral c Form a 24x12x6 array containing its values in parameter i ow gd J a c da db dc 0 Jo Jo over this volume with the mean The exact result is In 2 i z 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 207 Reminder Python functions def f a b c return some result
38. 8 3 3 The old buffer protocol import numpy as np import Image Let s make a sample image RGBA format x np zeros 200 200 4 dtype np int8 Zlieri 0 254 red x 3 255 opaque data x view np int32 Check that you understand why this is OK img Image frombuffer RGBA 200 200 data ImgsSsave test png Modify the original data and save again P IS turns OUL that Pity which knows next CTO Morning eDOoNt Nompy happily shares the same data scc 4 img save test2 png 8 3 Interoperability features 182 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 8 3 4 Array interface protocol Multidimensional buffers e Data type information present e Numpy specific approach slowly deprecated but not going away Not integrated in Python otherwise See Also Documentation http docs scipy org doc numpy reference arrays interface html gt gt gt x np array l 21 137 411 gt gt gt x array interface data 1716094552 False Pdea raa pue veda i e S s Txx4t strides None Reape 2p wine lane som t memory address of data is readonly data type descriptor same in another form Se SF cHe ck Strides or None at zn C order gt gt gt import Image gt gt gt img Image open data test png gt gt gt img array interface data me shapbe s 200 200 4 Mey pester aly gt gt
39. 90 state inflows state print state print How do we update the current storage 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 n m 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_release high max_release spillage Float desc Spillage hm3 update storage Event desc Updates the storage to the next time step def update storage fired self update storage state new storage self storage self release 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 Str format Ye a foin 4 7e2f for in range 4 i print str_format format self storage self release self inflows self s
40. Deferral Notification Visualization 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 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Warning For Traits 3 x users If using Traits 3 x you need to adapt the namespace of the traits packages traits api should be enthought traits api traitsui api should be enthought traits ui 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 reservoir Reservoir name Lac de Vouglans max storage 005 14 3 1 Initialisation All the traits do have a default value that initialise the variables For example the basic python types do have the following trait equivalents Python Type Boolean Complex number Floating point number 000 OO Plain integer ong integer Siring Unicode u A number of other predefined trait type do exist Array Enum Range Event Dict List Color Set Expression Code Callable
41. Invoking the debugger page 194 Postmortem page 194 Step by step execution page 196 Other ways of starting a debugger page 197 Debugger commands and interaction page 198 Debugging segmentation faults using gdb page 198 9 1 Avoiding bugs 9 1 1 Coding best practices to avoid getting in trouble 191 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 We all write buggy code Accept it Deal with it Write your code with testing and debugging in mind Keep It Simple Stupid KISS What is the simplest thing that could possibly work Don t Repeat Yourself DRY Every piece of knowledge must have a single unambiguous authoritative representation within a system Constants algorithms etc Try to limit interdependencies of your code Loose Coupling 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 Fast simple Detects syntax errors missing imports typos on names Integrating pyflakes in your editor is highly recommended it does yield prod
42. LI i i w 0 E 0 Oz U Ur 0 TOn 7 eg 070 Up O Dy U Bo Oye U U 0 U U U Iu 305 m po 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 I LO D OF e Dilation e e y E T av 0 Oley Cg OO gt gt gt ndimage binary_dilation a era y ii 0 7 1 astype a dtype S eo x E o Oe 4 Ci Cory NN Cb S Cac n i 1 Opening dtype np int 4 gt gt gt Opening removes small objects gt gt gt ndimage binary_opening a structure np ones 3 3 astype np int array l0 9 OON 0 1 0 Oy XE pubis Op SE 0 O 011 gt gt gt Opening can also smooth corners 1 il r dr O 0 gt gt gt ndimage binary opening a astype np int 0 i IL 1 6 10 Image processing scipy ndimage 126 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 operations can therefore be used to clean an image gt gt gt a np zeros 50 50 oo alos o OL I gt gt gt a 0 25xnp random standard_normal a sh
43. 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 abray Llo 07 0 S lake e els Wa wey Bee ls Sen oy woe IE gt gt gt y array IO S CON CON CONUS PPR SS INS SS COCOON However in practice this is rarely needed 3 2 4 Array shape manipulation Flattening gt gt gt a np array Ill 2 gt 14 9 911 gt gt gt a ravel abrbay il 2 Sy a 1 gt gt gt a T 3 s 1 19 gt gt gt a M rave LT array OMH de 2 S 37 61 Higher dimensions last dimensions ravel out first Reshaping The inverse operation to flattening gt gt gt a shape am gt gt gt b a ravel gt gt gt b reshape 2 3 cura d s Ae b CI 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 array I y Jdhe due 9 Ay r U 5 Or tp to oy RIDE Zo EB Log Uo ig Bo ROLL DN A Ze d 227 620 217 AO uS OG dle ee SX 3 2 Numerical operations on arrays 61 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt b a reshape 6 1 unspecified 1 value is inferred Views and copies ndarray reshape may return a view cf help np reshape not a copy gt gt gt qn gt gt gt
44. Property depends_on inflows release y control attributes inflows Float desc Inflows hm3 f release Range low min_release high max_release spillage Property desc Spillage hm3 depends_on storage inflows release Ht Private traits A E a a a aS _storage Float Traits property implementation ffteeeeeeeeeeeeeeeeeee ete tte tt tH ET 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 release self inflows overflow new storage self max storage return max overflow 0 def print state self print Storage tRelease tInflows tSpillage Str format VE join HIE 27 22 for in rauge 4 1 print str_format format self storage self release self inflows self spillage print 79 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 if name main projectA Reservoir name Project A max storage 30 max release 5 hydraulic head 600 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 h
45. True True Truel False True False dtype bool gt gt gt el astype np int abiray 107 1 Oly ile er DI Or ee OA Erosion minimum filter Replace the value of a pixel by the minimal value covered by the structuring element np zeros 7 7 dtype np int t ao 4 a a 0 gt gt gt ndimage binar array ITO O O oOo OG b g x O E SQ O O Qa a2 e a eo oa aS a el S LL astype a dtype l G A A LA OOM O LA LA LA HHP OO KA A A Ad Ad d y d Ad Ad d Ad Ad Ad Sa oS 2 Oo I U U D U U U OF OF OF gt gt gt Erosion removes objects smaller than the structure U U 0 U U U gt gt gt ndimage binary_erosion a structure np ones 5 5 astype a dtype array T10 0 0 07 0 0 0 Or 07 Uy 0 0 y Ol 12 4 Image filtering 235 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 OU R e eH rs ee Que LUE reU loea grs SO O or Oe O O O Or O OO O Or U KR 0 Qr 0 0 OTT Erosion Dilation Opening Closing Dilation maximum filter gt gt gt a np zeros 5 5 gt gt gt a 2 2 1 gt gt gt a annon Os O
46. Using the Python debugger uuu mom Room ono oROROECOR Om E GR OPOXOROROROEOROH CR OXGROR B Fuo ORO Debugging segmentation faults using gdb 4 og wed eee ee Gee eee He eK RE RE He Optimizing code 10 1 10 2 10 3 10 4 Optimization workflow zx woo kom x 3 REESE RE EERE RHE Re X X 4d XR Profiling Python code uxo Xgek xke 33 Roe 39 0RORergE Xo 49 x 3x5 X 9 38 x3 Making code go fast f aa a odemg hee FERRO XO RD EEEHEEERES SEH HERES Writing Taster numerical code a se sa s 44 wow 999 Rw OR S SEO uU wy FOE REDE bod we x Sparse Matrices in SciPy 11 1 11 2 11 3 11 4 MOUE os s ea raraged aeaaea AT TOO SCUCMICS e s a os aoe ia eee ee bee SPE a ae a RE E ORR EE AE Se d Lincaroysten SOIWEIS 4 saoo wed um m 9 S X BOY x ana aasa EE EEE GRE Ee 3 Other Interesting Packages 2 222 9 s oso ooo om Ee EH XR X 4 3 doo Image manipulation and processing using Numpy and Scipy 12 1 12 2 1 9 12 4 125 12 6 Opening and writing to image files 2 22 sec bene Xx IE ee ee eee ey E RGEORGROR OY Displaying WAGES s e eke he eae en Oe EP BRS Rom Sm WR REG wor GREE Weg Basie manipuladon 44 66 ebm 43 99 SEER HED SHEE 79 3 309 RO SE EEE ES a rase ANNAE os wa 91 5 302 99 3 9 B3 943 93 4 2 55 93 Wb due e oe RAUS SS P lg niic cc K AE eS ee ES Measuring objects properties ndimage measurements Mathematical optimization finding minima of functions 13 1 152 Knowing your problem 4565 62868
47. am 0 2 mpsrandom randon im shape im med ndimage median filter im noise 3 Original image Noisy image Median filter Error Other rank filter ndimage maximum_filter ndimage percentile_filter Other local non linear filters Wiener scipy signal wiener etc Non local filters 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 from scikits image filter import tv denoise from tv denoise import tv denoise tv denoised tv denoise noisy weight 10 More denoising to the expense of fidelity to data 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 12 4 Image filtering 234 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 TV denoising more TV denoising 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
48. 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 solve real problems and that their use is as simple as possible Chapters contents terators generator expressions and generators page 144 terators page 144 Generator expressions page 145 Generators page 145 Bidirectional communication page 146 Chaining generators page 148 Decorators page 148 Replacing or tweaking the original object page 149 Decorators implemented as classes and as functions page 149 Copying the docstring and other attributes of the original function page 151 Examples in the standard library page 152 Deprecation of functions page 154 Awhile loop removing decorator page 155 A plugin registration system page 155 More examples and reading page 156 Context managers page 156 Catching exceptions page 157 Using generators to define context managers page 158 143 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 7 1 lterators generator expressions and generators 7 1 1 lterators Simplicity
49. b ST ln So S 71 or by number of points gt gt gt c np lanspace 0 6 Start end Num POINTE gt gt gt C array IO sse 20227 Ose 30s hs gt gt gt d np linspace 0 endpoint False gt gt gt d arma d a Common arrays 3 1 The numpy array object 42 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt a np ones 3 3 reminder 3 3 is a tuple gt gt gt a arca Iii du ipee Hl calle ley vile elec set alles Ie Blige TUS gt gt gt b mnp zeros 2 2 gt gt gt b array AMI DNO Oly E Ong O0 pp gt gt gt c np eye 3 gt gt gt C array i ia rer Oly IP dE See O Ate des ales mood mpdiag Parray ik 25 9 AN gt gt gt d array IL y 0 0l I E H e ROS Oy Se Whe BS 2L OL IE 7 e np random random numbers Mersenne Twister PRNG gt gt gt a np random rand 4 a UD ROm cm me ad gt gt gt a array PLC ONE penu Doe LO 7393 gt gt gt b np random randn 4 Gaussian gt gt gt b cur Wns 544600 usus SE 1 796642115 gt gt gt np random seed 1234 Setting the random seed Exercise Array creation Create the following arrays with correct data types ii l 1 Oss 5 Wn Par on course 3 statements for each Hint Individual array elements can be accessed similarly to a list e g a 1 ora 1 2 Hint Examine the docstring for diag Exercise Tiling for array creation Skim through the
50. gt X np asarray img gt gt gt x shape 200 200 4 gt gt gt x dtype dtype uint8 Note A more C friendly variant of the array interface is also defined 8 3 Interoperability features 183 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 8 4 Array siblings chararray maskedarray matrix 8 4 1 chararray vectorized string operations gt gt gt X np arrdgyv clta DO 2 ccc view np chararray Do Seo T Chararvay ate bbb eee dtype S5 gt gt gt x upper charrar ra Clk US BEBO G A dtype S5 Note view hasa second meaning it can make an ndarray an instance of a specialized ndarray subclass 8 4 2 masked array missing data Masked arrays are arrays that may have missing or invalid entries For example suppose we have an array where the fourth entry 1s invalid gt gt gt x mnp array i 2 E I One way to describe this is to create a masked array gt gt gt mx np ma masked_array x mask 0 0 0 1 0 gt gt gt mx Imasked array data 127 5 mask False False False True False fill value 999999 Masked mean ignores masked data gt gt gt mx mean De gt gt gt np mean mx Z9 The masked array returns a view to the original array gt gt gt mx 1 9 gt gt gt x array ihe 9 3 99 Sx The mask You can modify the mask by assigning gt gt g
51. gt gt from scipy import ndimage Image processing routines may be sorted according to the category of processing they perform 6 10 1 Geometrical transformations on images Changing orientation resolution gt gt gt from scipy import misc gt gt gt lena misc 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 502 50 gt gt gt zoomed_lena ndimage zoom lena 2 gt gt gt zoomed_lena shape 1024 1024 In 35 subplot 151 Our Sols makelorvlibwaxwes AxesSubpilloe object au 9 100 gt In 36 pl imshow shifted_lena cmap cm gray Out 36 lt matplotlib image AxesImage object at 0x9593f6c gt in 373 eas Core EE Ts ke queues deb O S Ds In 39 etc 6 10 2 Image filtering from scipy import misc lena misc lena import numpy as np noisy lena np copy lena astype np float noisy lena lena std 0 5 np random standard_normal lena shape blurred_lena ndimage gaussian_filter noisy_lena sigma 3 median_lena ndimage median_filter blurred_lena size 5 from scipy import signal wiener_lena signal wiener blurred lena 5 5 6 10 Image processing scipy ndimage 124 noisy lena Gaussian filter median filter Wiener filter y Ww P k f pai gt Many other filters
52. lena misc 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 cmap plt cm gray Create an array of the image with a narrower centering for 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 Penal30 30 302 30 e We will now frame Lena s face with a black locket For this we need to create a mask corre sponding to the pixels we want to be black The mask is defined by this condition y 256 2 x 250 427 In 15 v s npsogrvdl0 95125 0 5512 4 x and y indices OF pixels In 16 v shape x shape OMe ded du oz S259 2 In 17 centerx centery 256 256 center of the image In PIS moek ly cemtery 22 Ww x Cemrerx 2 gt 250942 4 circle then we 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 U 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 1ena locket py then execute this script in IPython with run lena locket py Change the circle to an ellipsoid 3 2 Numerical operations on arrays 66 Python Sc
53. 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 12 4 2 Sharpening Sharpen a blurred image gt gt gt from scipy import misc gt gt gt lena misc 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 1 1 gt gt gt alpha 30 gt gt gt sharpened blurred_l alpha blurred 1 filter blurred 1 12 4 3 Denoising Noisy lena from scipy import misc l misc lena qp AED ME RACIO MD noisy 1 0 4 1l std np random random l shape A Gaussian filter smoothes the noise out and the edges as well 12 4 Image filtering 233 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 d Lu b Ed E amp ts y e U Zai A N Lya 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 T e Median filter better result for straight boundaries low curvature im Np zeros 20 207 iml Ss eu ll im ndimage distance_transform_bf im im noise
54. reveals that there is a second root around 2 5 We find the exact value of it by adjusting our initial guess gt gt gt COOUZ optimrize rtsolve f 2 5 gt gt gt root array 2 47948183 Curve fitting Suppose we have data sampled from f with some noise gt gt gt xdata np linspace 10 10 num 20 gt gt gt ydata f xdata np random randn xdata size Now if we know the functional form of the function from which the samples were drawn x 2 sin x in this case but not the amplitudes of the terms we can find those by least squares curve fitting First we have to define the function to fit gt gt gt def fF2 x a D return axx 2 b np sin x Then we can use scipy optimize curve fit to find a and b gt gt gt guess 2 2 gt gt gt params params Covariance optimize curve_fit f2 xdata ydata guess gt gt gt params array T 0 99923477 tous Now we have found the minima and roots of and used curve fitting on it we put all those resuls together in a single plot 6 5 Optimization and fit scipy optimize 115 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 120 f x l 100 Curve fit result e e Minima 80 v v Roots 60 E Yet 40 20 U 20 10 Note In Scipy gt 0 11 unified interfaces to all minimization and root finding algorithms are available scipy optimize minimize scipy optimize minimize scalar and scipy optimize root Th
55. x array ols 1027 dtre inclo gt gt gt 0x0201 0x0403 95 EDD 9x01 0x02 W 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 07205925 dtyvpestme32 gt gt gt 0x04030201 07305985 Note e view makes views does not copy or alter the memory block only changes the dtype and adjusts array shape gt gt gt x 1 5 gt gt gt y array 326193 diyoe inrs2 gt gt gt y base is x 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 ints8 Soe SI ce uU se som cbe ep e umi gt gt gt x He d S 2 see 3 H 4 8 1 Life of ndarray 165 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 SACS Lu 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 px vue Sem ee ape das H T p FO a um fpe Eee 0 Warning Another array taking exactly 4 bytes of memory gt gt gt y mpvarray ll 315 2 4 ly seypeenpeuint9o treanspo
56. xargs xxkwargs print in the wrapper args kwargs NS return selt tunctrion args kwargs gt gt gt deco instance replacing decorator class foo in decoravor Xue LOO gt gt gt ideco instance def function xargs xkKwargs print in runccron args kwards in decorator call foo Pe FUNG Eom ly 12 im tite wrapper 11 12 1 ia cunctLom 11 12 1 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
57. 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 efficiency Range 0 1 turbine Instance Turbine installed capacity PrototypedFrom turbine power if name main turbine Turbine turbine type typel power 5 0 reservoir Reservoir 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 prine CS 215 print updating the turbine power updates the installed capacity turbine power 10 print reservoir installed capacity print I5 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 14 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 H
58. 2012 3 EuroScipy 2012 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 scipy is composed of task specific sub modules Signal processing Statistics They all depend on numpy but are mostly independent of each other The standard way of importing Numpy and these Scipy modules is gt gt gt import numpy as np gt gt gt from scipy import stats same for other sub modules The main scipy namespace mostly contains functions that are really numpy functions try scipy cos is np cos Those are exposed for historical reasons only there s usually no reason to use import scipy in your code 6 1 File input output scipy io Loading and saving matlab files gt gt gt from scipy import io as spio gt gt gt a np ones 3 3 gt gt gt spio savemat file mat a al savemat expects a dictionary gt gt gt data spio loadmat file mat struct as record True gt gt gt data a arcar MIN ell TRE T RUM is diss Gb LC C ltl ae Reading images gt gt gt from scipy import misc gt gt gt misc imread fname png gt gt gt Matplotlib also has a similar function gt gt gt import matplotlib pyplot as plt gt gt gt plt imread ftname png
59. 6 Contributing to Numpy Scipy 188 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Problem with mailing lists you get mail 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 cevescipy ord Hai ird like to edit Numey Seipy docstrindgo My accoume ds XXXXX Cheers IS T 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 Especially for big invasive additions s http projects scipy org numpy wiki GitMirror s http www spheredev org wiki Git for the lazy Clone numpy repository gie redo cuisse I sva Ua tege ce bL odo S EST SCENES ome Cit mm scummy cd numpy Create a feature branch git checkout b name of my feature branch ss E CE lt edit stuff gt Gite Commit c Create account on http github com or anywhere Create a new repository Github Push your work to github git remote add github gitG8github YOURUSERNAME YOURREPOSITORYNAME git git push
60. Axes Edit LUT properties mm Mayavi Sce Pm S z z ww 1 606 16 aaoo Y 2 j pana Launch LUT editor Z v Z PolyDataNormals B v 99 Colors and legends Show legend aia m Surface Y 0 00 X Title Number of labels 0 1 004421 299 17 Shadow Use default name 4 0 192 Data name sc alars 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 15 4 Interaction 287 CHAPTER 16 Sympy Symbolic Mathematics in Python author Fabian Pedregosa Objectives 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 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 keeping 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
61. EERE 55 9529 40 3 2 Numerical operations on arrays ek e so9 9 OE eee X RO RO OE amp XO OX 9 BOR OX X ok OE ews o1 2 Mo oreelabDorate Oba S x udo oov oq Rh uade cedere dee qo Behe Snow Dee 71 34 Advanced operations zs ssas mE RR 053 ROROR O3 EO 39h RR OY d R EO XO DEE ESS 78 Getting help and finding documentation 83 Matplotlib 87 NMENI Un 20465 68 oe 2 G CHR Ewe eae eee net eee ee bees eee wo ae 87 5 2 IPyihon and the pylab mode uu so ss 542685 de 8 94x93 4 x 94x ERE OS 87 Ir AMPF 6 2 45 5 n655 4 2G F 646 SEER SHEE SEE SSH 9425 HK 4E 87 Ob x s sa T cbt Oo eee ee Eee Ce tee a bees eh ee be eek eb eee ee 90 SS Ta 2g ke tO R ee EM DOSER ERG EDGE ER OR GRRE EEO Ree EEE hee 92 NiO CENEES pee bea ee eRe eS 4d s m X RP EC EN Moy ROS ESTER HE S E 9 ok wx E o yo XR S 93 5 7 Figures Subplots and Axes ac4 koe Se be demo ok Rom p RR HELGE E SHEE EES 95 23 5 Other Types OL Plots one eee hee pd EO dioba 9 ROR Geer ee Gee oe Ee EUR om 0x5 97 209 STBEC IAS ET lt 1 lt uas ox 9 Exo 3 x Eee e Ee Heure Pe ER EROS EG He ee 103 Scipy high level scientific computing 106 II 10 11 12 13 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 File input output SCA DT uL ses e dom ssw EERE HER irid oom REAR EMRE EAS Special fonctions SCipy SoeCial eR RR sess isadega yS eee eikin eee x Linear algebra operations scipy tinal rs scsi x oROROE O9 Xo oq eO y ardas Fast Fourier transtorms SCIDVATTLDOGRE 6 909 9x o ew dare
62. EG 9 EE Ra e ols Optimization and fit scipy optimize cas teen RR x ox os E 5394 RO x OX Ov EAS Statistics and random numbers scipy stats aooaa oea e e e e Interpolation scipy interpolate usmod E wem Cee xES45esv9ck4uGom s Numerical integration scipy integrate iei woo xXx 9o 99 SEER ee HR eS Signal processus SLO Signal uos eae am mp he ue 3L Eye 6 soe d doses ES wee 6 10 Image processing scipy ndimage eue woe ox eee wehbe RE ERE RE EEG 6 11 Summary exercises on scientific computing 84 Re Kus REESE Ee x x o3 3 Advanced topics Advanced Python Constructs T 7 2 7 9 terators generator expressions and generators 7 2232 bw 9 y no EORR OX xU Decors aou uos um bet S UsexUhos dem eom xd due oe wee due MU Bode um UR ouod PU oe ae COD X MANIS caw ee ca woe PEERS EEO EES Wurde woe died Ew X x d s Advanced Numpy 8 1 5 2 8 3 8 4 8 5 8 6 Lise On ndamay 2 Gee dnb eng He Bea RORORIR OR BES Ee ERS ESE GbE ee ES Universal DUDCUONSS 5 s Gee cee ee babe e 0 9 UR 3C3 8 9 we UXOR d X e E eee ees Interoperability features lt uos Xon om wed db eS DE Oe Ue RG BES REESE He BESS ETE Oe Array siblings chararray maskedarray matrix Oa 662k eee he ee ee Re Re ee ae eee a a Se ba we Contributing to Numpy Scipy 2 ee Debugging code 9 1 ie 9 3 9 4 OIE DUZS zs ota ee ee pU SEU UN UR RUPEM eI AR WEE RUE RES ee Debugging workflow abe eee AEE EES REESE o X AU XE OEGR Oh x E Ww
63. Edit page Reference Quick search e Clustering package scipy cluster e Constants scipy constants e Fourier transforms scipy fftpack Go e Integration 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 e Maximum entropy models scipy maxentropy e Miscellaneous routines scipy misc G Find help Previous Next Highlight all Match case TZ Reached end of page continued from top 83 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Scipy documentation editor 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_dil
64. EuroScipy 2012 Chapter contents Life of ndarray page 160 It s page 160 Block of memory page 161 Data types page 162 Indexing scheme strides page 166 Findings in dissection page 172 Universal functions page 172 What they are page 172 Exercise building an ufunc from scratch page 173 Solution building an ufunc from scratch page 177 Generalized ufuncs page 180 Interoperability features page 181 Sharing multidimensional typed data page 181 The old buffer protocol page 181 The old buffer protocol page 182 Array interface protocol page 183 Array siblings chararray maskedarray matrix page 184 chararray vectorized string operations page 184 masked_array missing data page 184 recarray purely convenience page 187 matrix convenience page 187 Summary page 187 Contributing to Numpy Scipy page 187 Why page 187 Reporting bugs page 187 Contributing to documentation page 188 Contributing features page 189 How to help in general page 190 8 1 Life of ndarray 8 1 1 It s ndarray block of memory indexing scheme data type descriptor raw data how to locate an element how to interpret an element 8 1 Life of ndarray 160 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 array scalar iie ET ndarray typedef struct PyArrayObject PyODjecc HEAD Block of m
65. IIl conditioned quadratic Rosenbrock 10 Nu e dim 2 O dim 32 9 Well conditioned Gaussian o dim B e dim 128 B Well conditioned quadratic m 4 Cd dugato gradient Powell Nelder mead L BFGS BFGS Conjugate gradient BFGS L BFGS Newton wf wf wf W Hessian 13 3 Practical guide to optimization with scipy 255 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Without knowledge of the gradient e n general prefer BFGS scipy optimize fmin_bfgs or L BFGS scipy optimize fmin 1 bfgs b even if you have to approximate nu merically gradients On well conditioned problems Powell scipy optimize fmin powell and Nelder Mead scipy optimize fmin both gradient free methods work well in high dimension but they collapse for ill conditioned problems With knowledge of the gradient e BFGS scipy optimize fmin bfgs OT L BFGS scipy optimize fmin l bfgs b Computational overhead of BFGS is larger than that L BFGS itself larger than that of conjugate gradient On the other side BFGS usually needs less function evaluations than CG Thus conjugate gradient method is better than BFGS at optimizing computationally cheap functions With the Hessian e If you can compute the Hessian prefer the Newton method sclpy optimize tfmin negct If you have noisy measurements Use Nelder Mead scipy optimize fmin Or Powell scipy optimize fmin powell 13 3 2 Making your optimizer faste
66. Jene EIN elementwise funcs input output E 1 Number Of supported input types TODO Number Of input args TODO t Mumber or output args 0 identity element never mind this mandel function name mandel z c gt cemnpures ww C ur docstring 0 unused Reminder some pre made Ufunc loops PyUfunc f f float elementwise func float input 1 PyUfunc ffj ffloat elementwise func float input 1 float input 2 PyUfunc_d_d double elementwise func double input 1 PyUfunc_dd Mouble elementwise func double input 1 double input 2 PyUfunc D D elementwise func complex double x input complex doubles oup o e rir mere i PyUfunc DD DBlementwise func complex double xinl complex double xin2 ws NN Type codes 8 2 Universal functions 176 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 NE G2 I NES EE INE Ue ie EINE ESI ED SEDES HIC En NEY UPPER Ne uti NEY ONG ESCRITO NESCIUS ORIG NETT bp DOUBTS NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NPY DATETIME NEY TIMED LA NE OBR NEY STRING NE YES ODE NEY VOLD 8 2 3 Solution building an ufunc from scratch The elementwise function cdef void mandel single point double complex x z_in double complex xc in double complex z out umog ls The Mandelbrot iteration T T Some points of note T r E Ss Le allowed to call any Python functions here T The Ufunc loop runs with the Python Global Interpreter Lock released
67. Measuring objects properties ndimage measurements page 240 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 import pylab as pl pl imshow 1 Creating a numpy array from an image file gt gt gt lena misc imread lena png oe pecu EyYDS Numpy adarray gt gt gt lena shape lena dtype od ol dee Manes dtype is uint8 for 8 bit images 0 255 12 1 Opening and writing to image files 228 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Opening raw files camera 3 D images 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 262144 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 data are read from the file and not loaded into memory Working on a list of image files gt gt gt for i in range 10 im unporandomrandom integers 0 255 10000 reshape 100 00 eus miscsimsave random 02d png Ss i m gt gt gt from glob import glob poc fileli e Glob randone phg ao Tile lisrt sort 12 2 Displaying images Use matplotlib and
68. Se 2 4xsin 2 gt x CI SSINTIN 2 ce mes 25 163 Calculus gg Python Scientific lecture notes Release 2012 3 EuroScipy 2012 16 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 xX EEE eee A deua uie series 1 cos x x E x 2 2 5b x x x4 24 O x 6 16 3 4 Exercises 1 Calculate lim x gt 0 sin a x 2 Calulate the derivative of log x for x 16 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 integrate 6 x x 5 X Maro gt gt gt integrate sin x x COS X gt gt gt integrate log x x xe Log x gt gt gt integrate 2 x sinh x x esi a ix Also special functions are handled easily gt gt gt integrate exp x 2 xerf x x Diet ly Ay Cerna 27 4 It is possible to compute definite integral gt gt gt integrate x 3 x 1 1 ne cx Ime eCgrarc siii lt y qo P v5 29 1 gt gt gt Integrate lcos x x Eom A p72 p Also improper integrals are supported as well gt gt gt integrate exp x x 0 oo 1 gt gt gt integrate exp x xx2 x oo
69. See also e Load text files numpy loadtxt numpy savetxt e Clever loading of text csv files numpy genfromtxt numpy recfromcsv e Fast and efficient but numpy specific binary format numpy save numpy load 6 1 File input output scipy io 107 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 6 2 Special functions scipy special Special functions are transcendental functions The docstring of the scipy special module is well written so we won t list all functions here Frequently used ones are Bessel function such as scipy special jn nth integer order Bessel function Elliptic function scipy special ellipj for the Jacobian elliptic function Gamma function scipy special gamma also note scipy special gammaln which will give the log of Gamma to a higher numerical precision Erf the area under a Gaussian curve scipy special erf 6 3 Linear algebra operations scipy linalg The scipy linalg module provides standard linear algebra operations relying on an underlying efficient implementation BLAS LAPACK 6 2 The scipy linalg det function computes the determinant of a square matrix gt gt gt from scipy import linalg aoe Ale Heverray i ils Zi S o D gt gt gt linalg det arr 2 0 poo drr npeerteyd b 2 Ms Por an gt gt gt linalg det arr O20 gt gt gt linalg det np ones 3 4 Traceback most recent call last ValueError expe
70. 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 ndentation no choice Indenting is compulsory in Python Every 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 orwhile 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 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
71. 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 2 wc icheva ong our seripesod mede In 3 X renp costtheta In 4 y r np sin theta In 5 24 noesinir 7 In 6 from enthought mayavi import mlab in 7 milab mesh x y x CoOlormap Gist earch extent e or 1 0 de d Out 7 lt enthought mayavi modules surface Surface object at Oxde6f08c gt 15 3 Figures and decorations 285 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 miabemesh x vy uw excenz 0 1 0 hb Oe Ly representation wireframe line width 1 color 0 5 0 5 0 5 enthought mayavi modules surface Surface objec
72. Varoquaux Mathematical optimization deals with the problem of finding numerically minimums or maximums or zeros of a function In this context the function is called cost function or objective function or energy Here we are interested in using scipy optimize for black box optimization we do not rely on the mathe matical expression of the function that we are optimizing Note that this expression can often be used for more efficient non black box optimization Prerequisites e Numpy Scipy s Python e matplotlib References Mathematical optimization is very mathematical If you want performance it really pays to read the books Convex Optimization by Boyd and Vandenberghe pdf available free online e Numerical Optimization by Nocedal and Wright Detailed reference on gradient descent methods Practical Methods of Optimization by Fletcher good at hand waving explainations 245 Chapters contents Knowing your problem page 246 Convex versus non convex optimization page 247 Smooth and non smooth problems page 247 Noisy versus exact cost functions page 248 Constraints page 248 A review of the different optimizers page 248 Getting started 1D optimization page 248 Gradient based methods page 249 Some intuitions about gradient descent page 249 Conjugate gradient descent page 250 Newton and quasi newton methods page 251 Newton methods using the Hessian 2nd differ
73. a eigen ie ol Sue Lop daly L27 13 14 AS7 26 TX X8 Ze cU I G0 od 82 y 3457 d npozcerosctiss52 a T reshape 3 2 To understand see the section on the memory layout of an array page 75 below Dimension shuffling a np arange 4 3 2 reshape 4 a shape oy 124 S RT 2 1 b a transpose 1 2 0 b shape ze Io 1 20 Also creates a view Resizing Size of an array can be changed with ndarray resize gt gt gt a np arange 4 SS er resi a gt gt gt a array E However it must not be referred to somewhere else gt gt gt b a Soe a resize 4 ValueError cannot resize an array references or is referenced by another array in this way Use the resize function 3 2 Numerical operations on arrays 62 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 double precision dimension n intent out b a 1 end subroutine some_function We can use f2py to wrap this fortran code in Python f2py c m fortran module Zo dp FORT Yan module rt9uU import numpy as np import fortran module def some function input m fP Call a Fortran routine and preserve input s
74. a loop gt gt gt a 1 0 2 4 gt gt gt for element in a if element continue print 1 element 2 4 4 Conditional Expressions if object 2 4 Control Flow 17 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Evaluates to False e any number equal to zero 0 0 0 0 0 an empty container list tuple set dictionary e False None Evaluates to True everything else a b Tests equality with logics gt gt gt True a is b Tests identity both sides are the same object gt gt gt lis 1l False gt gt gt da gt gt gt p gt gt gt a is D True a in b For any collection b b contains a gt gt gt b 1 2 3 gt gt gt 2 in b True gt gt gt 5 an b False If b is a dictionary this tests that a is a key of b 2 4 5 Advanced iteration Iterate over any sequence You can iterate over any sequence string list keys in a dictionary lines in a file gt gt gt vowels aeiouy gt gt gt for i in powerful if i in vowels print i gt gt gt message Hello how are you gt gt gt message split returns a list Hel lho 7 now s are your gt gt gt for word in message split print word Hello how ake you Few languages in particular languages for scientific computing allow to loop over anything but integers indices With Python it is possible to loop exac
75. and packages for installation can be found on http sympy org 288 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Chapters contents e First Steps with SymPy page 289 Using SymPy as a calculator page 289 Exercises page 290 Symbols page 290 Algebraic manipulations page 290 Expand page 290 Simplify page 290 Exercises page 291 Calculus page 291 Limits page 291 Differentiation page 291 Series expansion page 292 Exercises page 292 Integration page 292 Exercises page 292 Equation solving page 292 Exercises page 293 Linear Algebra page 293 Matrices page 293 Differential Equations page 294 Exercises page 294 16 1 First Steps with SymPy 16 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 from sympy import a Rational 1 2 gt gt gt a E gt gt gt ax2 i SymPy uses mpmath in the background which makes it possible to perform computations using arbitrary precision 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 SR o EE gt gt gt pi evalf I
76. computing 132 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 6 11 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 133 Introduction Lidars systems are optical 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
77. coo matrix 3 4 dtype np int8 gt gt gt MEX T Odense IDSE TET HE E c qs DSI HO Oly Lom 2059 ds Dl uite spe 3 TO create using data ij tuple gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt row merana 0 2 i 01 pol mer array 0 3 y 3 data np array 4 5 7 9 mtx sparse coo matrix datay row col shape 4 mtx lt 4x4 sparse matrix of type type numpy into4 Wiech 4 stOreo elements C COOrcinarce od Tc gt gt gt mtx todenset Morrux U O e KOy T S 805 Oye 205 ws 5 usc Ge O siu duplicates entries are summed together poo OW Movarray il 0 Jd T ge oO pos Col TNovarray 0 xx 1 5 1 8 20 gt gt gt data Merarray id s e es Sh Sod gt gt gt mtx sparse coo matrix data row col shape 4 gt gt gt mtx todense Meise lise d wal HO e OF Oy Ls IL Oe TONS 0 Oe PO IE no slicing gt gt gt Mie Sle Ol Traceback most recent call last TvpeRhrror coo matrix object 11 2 Storage Schemes 2 21 215 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Compressed Sparse Row Format CSR row oriented three NumPy arrays indices indptr data indices is array of column indices data is array of corresponding nonzero values indptr points to row starts in indices and data x lengthisn row 1 lastitem number of values length of both indice
78. data by creating an estimator and calling its fit X Y method gt gt gt from sklearn import svm gt gt gt clf svm LinearsSvVvCc gt gt Cli strc aris data iris tarcrget 7 learn from the data EON 0d Once we have learned from the data we can use our model to predict the most likely outcome on unseen data gt gt gt clf predict 5 0 3 6 1 3 0 25 parra EIT di eame Note We can access the parameters of the model via its attributes ending with an underscore gt gt gt CLE COGE array peccemus b 17 2 Classification 17 2 1 k Nearest neighbors classifier The simplest possible classifier is the nearest neighbor given a new observation take the label of the training samples closest to it in n dimensional space where n is the number of features in each sample 17 2 Classification 297 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 sklearn import neighbors gt gt gt knn neighbors KNeighborsClassifier gt gt gt bon rat urasccderde sess EE L KNerxghborsClassqrTrer x s oo enn pres esu ud quem x so res array LULU Training set and testing set When exp
79. dist packages secipy gt ad ndimage 17 07 sde Meon Uee bilby p than 5 Acla ete packages Scu elim dcr Mis EE doccer pyG Ltourretf pyc Tou empooOlgeTOnepyd morphine logy py setup pyc doccer pyc Hig on OAs interpolation pyc _nd_image so setupscons py filters pye nbospye measurements py SEI SUITS oe ie oa SEMIS SOS filters pyc init py8 measurements pyc Eus out C tests fourrer y tite Skye momol oy o AC setup py From Ipython In 1 import scipy zn 2 oinp Tile OuctI2 s 5 ust lub python 67 dist packagesy scipy init yc In 3 import scipy version In 4 scipy version version One iA Cees In 5 import scipy ndimage morphology In 6 from scipy ndimage import morphology in 17 morphology binary_dilation Type function Base Class type function gt Duas Form function binary dilation at 0x9bedd84 gt Namespace Interactive File just lib python 6 dast packages scipy noimage morpno logy py Definition morphology binary dilalion input structure None iterations 1 mask None output None border value 0 origin 0 brute force False DOG Su I TIS Multi dimensional binary dilation with the given structure 2 6 Reusing code scripts and modules 30 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
80. 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 For the Sprog 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 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 npslanspace 0 1 leZ 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 6 11 Summary exercises on scientific computing 129 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 eee the eno i OA 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 S259 FS OSes Dat The results are now gathered on a Matplotlib figure 1 0 0 8 2 S 0 6 e E i 2 4A m 5 0 4 L 0 2 Vig 32 98 m s 0 85 22 24 26 28 30 32 34 Annual wind speed maxima m s Figure 6 1 Solution Python source file Exercise with the Gumbell distribution The interested reade
81. full_matrices False i boops best of 3 295 ms per loop In 7 timeit np linalg svd data full matrices False i loops best of 32 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 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 timeit to try out different alternatives on your data 10 3 Making code go faster 204 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 10 4 Writing faster numerical code A complete discussion on advanced use of numpy is found in chapter Advanced Numpy page 159 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 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 58 to do operations on arrays as small
82. function and an offset corresponding to the background noise To fit the signal with the function we must define the model propose an initial solution e call scipy optimize leastsq Model A Gaussian function defined by u B Aexp c can be defined in python by gt gt gt def model t coeffs return coolio T9 v eoeT rs mevexe cu E OSstrsieeemts5 2 where e coeffs 0 is B noise e coeffs 1 is A amplitude 6 11 Summary exercises on scientific computing 134 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 e coeffs 2 is u center 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 35 320 15 1 dtype 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 tros dual s coetts y yy T return y model t coeffs So let s get our solution by calling scipy optimize leastsq with the following arguments the function to minimize an initial solution 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 LL 2 70563941 297252020742 Jix51924990 a U 56056226
83. github name of my reature branch 8 6 Contributing to Numpy Scipy 189 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 8 6 5 How to help in general Bug fixes always welcome What irks you most Browse the tracker Documentation work API docs improvements to docstrings Know some Scipy module well User guide Needs to be done eventually Want to think Come up with a Table of Contents http scipy org Developer Zone UG Toc Ask on communication channels numpy discussion lit Scipy dev list 8 6 Contributing to Numpy Scipy 190 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 Python nosetests http readthedocs org docs nose en latest line profiler http packages python org line_profiler s pyflakes http pypi python org pypi pyflakes e gdb for the C debugging part Chapters contents Avoiding bugs page 191 Coding best practices to avoid getting in trouble page 191 pyflakes fast static analysis page 192 Running pyflakes on the current edited file page 192 A type as go spell checker like integration page 193 Debugging workflow page 193 Using the Python debugger page 194
84. gt data curas e 2 see All i 2 T 7 8 Lr LO dia T BE gt gt gt mtx sparse dia_matrix data offsets shape 4 4 gt gt gt mtx data SIE P or Sy 4 sr or po el Co Oe IE Za E gt gt gt mtx offsets array d Sie uium o gt gt gt print mtx 07 0 1 lie Ls 2 27 2 3 597 3 4 MEET 2 27 1 6 95 123 7 One 325 LI ec TZ gt gt gt mtx todense R E iil L Oe ET QT o se O Uu doe EIT Ur 0p Fa pg explanation with a scheme offset row 2 9 1 10 0 1 11 11 2 Storage Schemes 211 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 1 5 2 12 2 6 3 3 7 4 8 matrix vector multiplication gt gt gt vec np ones 4 gt gt gt vec array L IE I Talg gt gt gt mtx vec array L257 lap eur d 1 gt gt gt mtx toarray vec cana cto AMICI D Vn es On A s Pe Duo quse P 0s On ore Oy etr Oe pe 4 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 efficient for constructing sparse matrices incrementally constructor accepts dense matrix array sparse matrix shape tuple create empty matrix flexible slicing changing sparsity structure is efficient slow arithmetics slow column s
85. gt gt arr2 x Chacaray a p o dtype S1 gt gt gt arrzZ y arcay L 21 8 4 4 matrix convenience e always 2 D e x is the matrix product not the elementwise one no Pe MaE RI HHE E Olly 07 UY ree e Era dee 22 o as meri CI 29s 3 egg 8 5 Summary Anatomy of the ndarray data dtype strides Universal functions elementwise operations how to make new ones Ndarray subclasses Various buffer interfaces for integration with other tools 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 I have this fancy code Would you like to have it Td like to help What can I do 8 6 2 Reporting bugs Bug tracker prefer this http projects scipy org numpy http projects scipy org scipy Click the Register link to get an account e Mailing lists scipy org Mailing_ Lists If you re unsure 8 5 Summary 187 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Noreplies 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 zi falls with l
86. 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 array AU 452 AZadap Z2T785 159 10s i195 424 gt gt gt maxima ndimage maximum sig labels xrange 1 labels max 1 gt gt gt maxima arrar T 1 25028 11205352 LR ut lI e407 Oy 249611919 Gu momo 9 190230299 ouod 545195407971 gt gt gt ndimage find_objects labels 4 slice 30L 48L None slice 30Lh 48h None gt gt gt sl ndimage find_objects labels 4 gt gt gt import pylab as pl gt gt gt pl amohow sane bsp lt matplotlib image AxesImage object at gt sig mask labels See the summary exercise on mage processing application counting bubbles and unmolten grains page 137 for a more advanced example 6 11 Summary exercises on scientific computing The summary exercises use mainly Numpy Scipy and Matplotlib They provide some real life examples of scientific computing with Python Now that the basics of working with Numpy and Scipy have been introduced the interested user is invited to try these exercises 6 11 1 Maximum wind speed prediction at the Sprogo 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 availabl
87. in scipy ndimage filters and scipy signal can be applied to images Exercise Compare histograms for the different filtered images 6 10 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 Elementary mathematical morphology operations use a structuring element in order to modify other geometrical Structures Let us first generate a structuring element gt gt gt el ndimage generate_binary_structure 2 1 Dos abiay False True False True True True False Irue False dtype bool gt gt gt el astype np int array I9 1 0l IT MII SS ER e Erosion np zeros 7 7 dtype np int 2 5 6 10 Image processing scipy ndimage 125 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 _ 0 gt gt gt ndimage binar array TO OY l e SU N xe O S astype a dtype _ GO A KA A OL O A LA LA LA ba bes es es es d y d d d d A
88. in the decorator expression and the decorator ___init__ method is used for decorator construction gt gt gt class decorator class object def init__ self arg this method is called in the decorator expression print in decorator Init p arg self arg arg def call self function this method is called to do the job Prine in decorator call selicverc ee return function gt gt gt deco instance decorator _class foo in seeCoracer inie soo gt gt gt deco instance 7 2 Decorators 150 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 def function args kwargs print inocpumnoctren args Kwarge SI deceorgtor cali coo 2o fpumetlomt jp SEMIS NL TS 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 ino cecoraror imt arg self arg arg def call self function this method is called to do the job print in decorator call seli arg Self function function return self wrapper def _wrapper self
89. initial point we need to resort to costlier 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 grid 10 dO 0 1 gt gt gt xmin global optimize brute f grid poc xmin global arise LOS 9 5 For larger grid sizes scipy optimize brute becomes quite slow scipy optimize anneal provides an alternative using simulated annealing More efficient algorithms for different classes of global opti mization problems exist but this is out of the scope of scipy Some useful packages for global optimization are OpenOpt IPOPT PyGMO and PyEvolve To find the local minimum lets constraint the variable to the interval 0 10 using 6 5 Optimization and fit scipy optimize 114 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 scipy optimize fminbound pos omin local e pue hmaimoound tt Oy T0 gt gt gt xmin_local Boo 467 le Note Finding minima of function is discussed in more details in the advanced chapter Mathematical optimiza tion finding minima of functions page 245 Finding the roots of a scalar function To find a root ie a point where f x 0 of the function f above we can use for example Scipy optimize fsolve seo FOOL Optimize solve 1 our initial guess is 1 gt gt gt root S Olan Note that only one root is found Inspecting the plot of
90. int dimensions int xsteps void xdata int68 output elementwise function int8 input 1 int8 input 2 8 2 Universal functions 172 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Ihis runctron must compute the ufunc Tor Many values at once in the way shown below char input 1 charx args 0 char input 2 char args 1 char output char args 2 int i for L Ua lt dimencions lOl em 4 xoutput elementwise_function input_l input_2 DN P cp iMput 2 steps i output steps 2 2 The Numpy part built by char types 3 types EE Jw type of First nput arg 7 tcI sce E ETE fe type of second input arg w yee NE BITE fe type of third input arg wy PyObject xpython_ufunc PyUFunc_FromFuncAndData ufunc_loop NURE types Ll Vw Yes 2 sb ume mper sce Ll 7 num outputs 7 identity_element name docstring unused e A ufunc can also support multiple different input output type combinations Making it 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 double elementwise func double input 1 PyUfunc dd ddouble elementwise func double input 1 double input 2 PyUfunc D D elementwise func npy cdouble xinput npy cdoublex output PyUfunc DD Delementwise func npy
91. its subtype Iris setosa Iris versicolor Iris virginica To load the dataset into a Python object gt gt gt from sklearn import datasets gt gt gt iris datasets load_iris This data is stored in the dat a member which isa n samples n features array gt gt gt iris data shape T5055 4 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 150 gt gt gt import numpy as np gt gt gt Np unique iris target array ily ly 299 17 1 Loading an example dataset 296 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 An example of reshaping data the digits dataset SN OW BP WN KF CO 012 3 4 5 6 7 The digits dataset consists of 1797 images where each one is an 8x8 pixel image representing a hand written digit digits datasets load_digits gt gt gt digits images shape LOT gre o gt gt gt import pylab as pl gt gt gt pimnenowidigsbssameoes IJ cmap ol 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 1 17 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
92. linspace 0 1 50 gt gt gt linear results linear interp computed time A cubic interpolation can also be selected by providing the k ind optional keyword argument gt gt gt cubic_interp interpld measured_time measures kind cubic gt gt gt cubic results cubic interp computed time The results are now gathered on the following Matplotlib figure 6 7 Interpolation scipy interpolate 119 e e measures linear interp cubic interp 1 35 0 2 0 4 0 6 0 8 1 0 scipy interpolate interp2d is similar to scipy interpolate interpld but for 2 D arrays Note that for the interp family the computed time must stay within the measured time range See the sum mary exercise on Maximum wind speed prediction at the Sprog station page 128 for a more advance spline interpolation example 6 8 Numerical integration scipy integrate The most generic integration routine is scipy integrate quad gt gt gt from scipy integrate import quad no ares err usc npe m sap gt gt gt np allclose res 1 True gt gt gt no allelose err iL res Tete Others integration schemes are available with ixed_quad quadrature romberg scipy integrate also features routines for integrating Ordinary Differential Equations ODE In particular scipy integrate odeint isa general purpose integrator using LSODA Livermore Solver for Ordinary Differential equations with Automatic method switching f
93. ls Users cburns local lib python2 5 site packages Lustr locat Ttiib python2 5 site peckages ZLabrarvv Rrameworks Python tramewowvk VersTons 2 57l11b5 7p50ython2 2 8 2 shutil high level file operations The shutil provides useful file operations shutil rmtree Recursively delete a directory tree shutil move Recursively move a file or directory to another location shutil copy Copy files or directories 2 0 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 Ta E OR e e RH e SEU SEK Glee Wi Doly araili G a ankta HE ew male C 2 8 4 sys module system specific information System specific information related to the Python interpreter 2 8 Standard Library 35 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Which version of python are you running and where is it installed zn Plt Gvsscbat ror Quse gu vec cudq In 118 sys version Dur DHT T2 bls tees C0 oll Ei 22 uS 07 2s7255 XB GCC 22021 Apple Computer Inc build 5363 In 119 sys prerix Out 119 library Frameworks Python framework Versicons 2 5 e List of command line arguments passed to a Python script In 100 sys argv Our 100i I Users cburns local Cin T sys path is a list of strings that specifies the search path for modules Initialized from PYTHONPATH In 121 sys path Qu ues 2 L
94. mandatory compilation steps verbose syntax amp 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 Drawbacks Base language is quite poor and can become restrictive for advanced users Not free Other scripting 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 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 out language allowing to write very readable and well structured code we code what we think Many libraries for other tasks than scientific computing web server management serial port access et
95. means that decorators can be implemented as normal functions or as classes witha ca11 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 just 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 det function print inside function doing decoration gt gt gt funmetionm imside Faunce aon 7 2 Decorators 149 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt def decorator_with_arguments arg print defining the decorator def decorator function in this inner function arg is available too Princ doing decoration y arg return function return _decorator gt gt gt decorator_with_arguments abc def F umnct ion print inside function defining the decorator Going 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
96. mult minimization The scipy optimize module provides useful algorithms for function d ing d root find ing an 1 curve fitt 1mensiona RE S gt gt gt from scipy import opt lon f a scalar funct inimum o Finding the m Let s define the following function gt gt gt def f x X s n return x 2 10 np It and plot no arcange 10 gt gt gt X E gt gt gt DIE plor x gt gt gt plt show 113 imize opt scipy ion and fi imizat 6 5 Opt Python Scientific lecture notes Release 2012 3 EuroScipy 2012 120 100 80 60 40 20 RL 5 y 5 10 This function has a global minimum around 1 3 and a local minimum around 3 8 The general and efficient way to find a minimum for this function is to conduct a gradient descent starting from a given initial point The BFGS algorithm is a good way of doing this 2o Optimize min bros iy 0 Optimization terminated successfully Current function value 7 945823 Iterations 5 Function evaluations 24 Gradient evaluations 8 ara 092421995 A possible issue with this approach is that if the function has local minima the algorithm may find these local minima instead of the global minimum depending on the initial point gt gt gt optimize fmin bfgs f 3 disp 0 SDRap 1595595746969 If we don t know the neighborhood of the global minimum to choose the
97. np sort a axis 1 gt gt gt b array Lbs 4 51 Note Sorts each row separately In place sort gt gt gt a sort axis 1 gt gt gt a array Uber sd 0 Lgs alge x2 de Sorting with fancy indexing sso a mp rvarray 4 37 d 21 gt gt gt j np argsort a gt gt gt j dima M ES a 3 2 Numerical operations on arrays 64 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 ey pgs S22 5 rd Finding minima and maxima gt gt gt a np array 4 3 1 2 gt gt gt J max np argmax a 2x jJ min Np argmin a gt gt gt J max J m n S 3 2 6 Some exercises 3 2 Numerical operations on arrays 65 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 from scipy import misc gt gt gt lena misc lena Note In older versions of scipy you will find lena under 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 ie Er X 1 d P UTR U i 5 ame x I g s lt l 97 pou 4 1 Let s use the imshow function of pylab to display the image In 3 import pylab as plt In 4
98. oo 1 pases T 16 3 6 Exercises 16 4 Equation solving SymPy is able to solve algebraic equations in one and several variables he IP SU VENERE S MES eere scc ME bosses om 292 164 Equation solving 292 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 In 8 solve x 5 y 2 3xx 6xy 15 Ix yl fuu Velie eyes a Eccc It also has limited support for trascendental equations im S isolve expisx sex GUESS SEA 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 rm 10 E er ee Zee ra LL racco E Ouel Lills GC TS ee ee In DES Facco i modulus 3 Quebec ee a ae 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 S SESS E etc qae x38 oy Once sq 1x trus y rrue This tells us that x amp y is True whenever x and y are both True If an expression cannot be true 1 e no values of its arguments can make the expression True it will return Fals
99. our GUI 14 and feed it together with our figure to the backend to get our canvas 15 We call the show method 16 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 py lab itself 20 We create a normal figure with pylab 21 and get the corresponding figure manager 422 Now let s set our figure we created above to be the current figure 23 and let pylab show the result 424 The lower part of the figure might be cover by the toolbar If so please adjust the figsize for pylab accordingly Exercises 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 105 CHAPTER 6 Scipy high level scientific computing authors Adrien Chauve Andre Espaze Emmanuelle Gouillart Ga l Varoquaux Ralf Gommers 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
100. ourput pes NPY CDOUBLE input output type NE C DOUBLE input OULDUE_ Ee ypes 2 NPY C DOUBTE elementwise funcs 0 void x mandel single point mandel PyUFunc FromFuncAndData Jeene E sore elementwise funcs input output ypes T mumber of supported amour types 2 dt mumoer of input args 1 number of output args 0 identity element never mind this mandel function name mandel z c gt comnp tes iterated z242 7 C t docstring 0 unused import numpy as np import mandel i No d gps deu A000 y no linspace 1 4 1 4 1000 eoe o None Liey ls None z mandel mandel c c import matplotlib pyplot as plt Plitainohowla bS arses lt 10005 e tent n 7 0 6 ly ay jede E pit show 8 2 Universal functions 178 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Note Most of the boilerplate could be automated by these Cython modules http wiki cython org MarkLodato CreatingUfuncs Several accepted input types E g supporting both single and double precision versions cdef void mandel single point double complex z in double complex xc in double complex out mogul cdef void mandel single point singleprec float complex z_in float Complex C in float complex z out cdef PyUFuncGenericFunction loop funcs 2 cdef omar unp ct outpuc teypes s 2 cdef void xelementwise funcs 1 x2 loop tunes PyUFunc_DD_D SITUE e ORE Dos eae S
101. 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 dynamic 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 if name m mariot Python Scientific lecture notes Release 2012 3 EuroScipy 2012 projectA Reservoir name Project A max_storage 30 max release 100 0 hydraulic head 600 efficiency 0 8 State ReservoirState reservoir projectA storage 10 register the dynamic listener state on_trait_change wake_up_watchman_if_spillage name spillage state release 90 Stare Nimi Vows 0 state print_state print Forcing spillage state inflows 100 state release U print Why do we have two executions of the callback 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 wak
102. position and velocity are shown on the following Matplotlib figure 0 2 4 6 8 10 There is no Partial Differential Equations PDE solver in Scipy Some Python packages for solving PDE s are available such as fipy or SfePy 6 9 Signal processing scipy signal gt gt gt from scipy import signal e scipy signal detrend remove linear trend from signal t np lrmspacet t0 5 100 x t np random normal size 100 pl plot t x linewidth 3 pl plot t signal detrend x linewidth 3 6 9 Signal processing scipy signal 122 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 e scipy signal resample resample a signal to n points using FFT t np linspace 0 5 100 x np sin t plL plot t x Lin width 3 papusoeg cule Signal resample oy 90 eo 1 5 1 0 0 5 0 0 0 5 1 0 1 5 Notice how on the side of the window the resampling is less accurate and has a rippling effect e Signal has many window function scipy signal hamming scipy signal bartlett scipy signal blackman Ht e Signal has filtering median filter scipy signal medfilt Wiener scipy signal wiener but we will discuss this in the image paragraph 6 10 Image processing scipy ndimage The submodule dedicated to image processing in scipy is scipy ndimage 6 10 Image processing scipy ndimage 123 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt
103. self new storage self storage self release self inflows return min new storage self max storage Set storage self storage value self storage storage value get spillage self new storage self storage self release self inflows overflow new storage self max storage return max overflow 0 Qon trait change storage def print state self print Storage tRelease tInflows tSpillage Str format e CNET join 4s T7 2f for zn raungec4 1 print str format format self storage self release self inflows self spillage print T s 79 if name S main PA 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 14 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 ALrslra wWwaedct Corton class Reservoir HasTraits name Str max storage Float le6 desc Maximal storage hm3 max release Float 10 desc Ma
104. significantly different 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 ttest_ind a b S37 5622 nO ec y Os O02 77 S65 so The resulting output is composed of 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 the p value the probability of both processes 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 means 6 7 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 sine function gt gt gt measured time np linspace 0 1 10 gt gt gt nonse np random random l0 2 1 1le 1 gt gt gt measures np sin 2 np pi measured time noise The scipy interpolate interpid class can build a linear interpolation function gt gt gt from scipy interpolate import interpld gt gt gt linear interp interpld measured time measures Thenthe scipy interpolate linear interp instance needs to be evaluated at the time of interest pue computed time np
105. sub array gt gt gt np dtype int type lt Eype nmumpysvmuos gt gt gt gt np dtype int itemsize 8 gt gt gt np dtype int byteorder f Example reading wav files The wav file header 11 4 byte unsigned little endian integer 11 bits_per_sample 2 byte unsigned little endian integer 4 byte unsigned little endian integer e 44 byte block of raw data in the beginning of the file 4 byte unsigned little endian integer block_align 2 byte unsigned little endian integer 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 teppkcqde sic wy flexible sized scalar type item size 4 8 1 Life of ndarray 162 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 vehunk size hem little endian unsigned 32 bit integer CE Ow Mee goa Nay 4 byte string ETE Cs me Vim size Hut occ tity Ment uum channels eut more of the same sample rate lt u4 byte rate u4 Clock EE eg aM pits per sample 402 datas quM o DI ae SU Saree quse gue data size aad E La the sound data itself cannot be represented here it does not have a fixed size See Also wavreader py gt gt gt wav header dtype format dtype Mor E gt gt gt wav header dtype fields lt Cictproxy ODOL XE um gt gt gt wav
106. 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 assigned to a variable an item in a list or any collection passed as an argument to another function In 38 va variable args In 39 va three x 1 y 2 args is three kwargo ao qo ot I ee ae alle 2 5 9 Methods Methods are functions attached to objects You ve seen these in our examples on lists dictionaries strings etc 2 5 10 Exercises Exercise Quicksort Implement the quicksort algorithm as defined by wikipedia t nctiom guicksort array var list less greater it Jengen array lt Z recurn aly select and remove a pivot value pivot from array Lor Gach x in array Tf x lt pivot t L them 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 uo 1 ul 1 eu nt2 u n l1 un 2 6 Reusing code scripts and modules For now we have typed all instructions in the interpret
107. 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 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 Ecke
108. 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 date nploadsxt dats populations t b 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 year gt 1917 amp year lt 1918 gt gt gt means and and l means or 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 720 gt gt gt populations std axis 0 masked array data 21087 056469 TIE ooo a 3322 5062255 8l mask False False False le 20 fill value Note that Matplotlib knows about masked arrays gt gt gt ile LOE year POPUL tions oe ma R T R lineon Line D Ob ect uode uu sonal 80000 70000 60000 50000 40000 A 30000 20000 10000 1800 1905 1910 1915 1920 8 4 Array siblings chararray maskedarray matrix 186 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 8 4 3 recarray purely convenience pos Aer Darry ila ar ye Lu p gs ey oe HE se 7 vue opc Em p gt gt gt arr2 arr view np recarray gt
109. when the 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 fa as f fawrieTek mor Con ent m 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 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 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 parallel programming concurrent futures ThreadPoolExecutor invoke in parallel then kill thread pool py
110. widget pack side Tk TOP fill Tk BOTH expand 1 17 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 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 5 9 The Class Library 104 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 our 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 1 3 Now we have to do some basic GUI programming work We make a root object for
111. with sympy return uper us ds a ee Oe cesse cass HaHa iu yn gt gt gt optimize fmin ncg f 2 2 fprime fprime fhess hessian Optimization terminated successfully Current function value 0 000000 keerati ons 10 Function evaluatrons 12 Gradient evaluations 10 Se eaU AL TSE tome U array l TD Note At very high dimension the inversion of the Hessian can be costly and unstable large scale gt 250 Note Newton optimizers should not to be confused with Newton s root finding method based on the same principles scipy optimize newton 13 2 A review of the different optimizers 252 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Quasi Newton methods approximating the Hessian on the fly BFGS BFGS Broyden Fletcher Goldfarb Shanno algorithm refines at each step an approximation of the Hes sian An ill conditionned quadratic function ee On a exactly quadratic function BFGS is not as fast as Newton s method but still very fast An ill conditionned non quadratic function Here BFGS does better than Newton as its empirical estimate of the curvature is better than that given by the Hessian An ill conditionned very non quadratic function gt gt gt def f x The rosenbrock function zx return 5c 1 9005 95352 x 1 gt gt gt def fprime x return unp arravecc2e5swL lec ssepe sp seo s2 0 xc soho 2 gt gt gt optimize fmin bfgs f 2 2
112. 04 help np v np vander np vdot np version np voLd0 np vstack Mp Vad np vectorize np void Diy Sp late In 204 help np vander 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 Z SciPy Release 0 8 dev Date February 11 2010 SciPy pronounced Sigh Pie is open source software for mathematics science and engineering e SciPy Tutorial o Introduction o Basic functions in Numpy and top level scipy o Special functions scipy special Table Of Contents SciPy o Integration scipy integrate Reference o Optimization optimize E o K i H Next topic Interpolation scipy interpolate 3 o Signal Processing signal cxi PUT Lu o Linear Algebra This Page o Statistics o Multi dimensional image processing ndimage o File IO scipy io Resources o Weave e Release Notes Show Source Scipy org website
113. 121 subplot 122 46 6 H 78a 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 10 10 0 8 0 8 osf subplot 221 BST subplot 222 04 04 0 2 0 2 945 02 0 4 0 6 0 8 10 955 0 2 0 4 0 6 0 8 1 0 1 0 1 0 0 8 0 8 6 subplot 223 st subplot 224 0 4 0 4 0 2 0 2 946 02 0 4 0 6 0 8 10 995 0 2 04 0 6 0 8 10 OII 1809 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 5 7 4 Axes 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 aoe pl plot x lt matplot irb lines LineZD instance ae 2 45 Poe a Pleaxes 0 2 Usd 0225 395295 hoe Dl plow k matplot iib lines LineZD instance ab se The result looks like this KAEN e a EE 5 7 Figures Subplots and Axes 96 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercises 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
114. 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 matrix sparse matrix classes with data attribute offset for each diagonal is the main diagonal negative offset 2 below positive offset above fast matrix vector sparsetools fast and easy item 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 11 2 Storage Schemes 210 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 use rather specialized solving PDEs by finite differences with an iterative solver Examples e create some DIA matrices gt gt gt data np array ll 2 3 4 l repeat 3 axis 0 gt gt gt data drray l 2 3 4 pales ee E E pee 4 EUMD gt gt gt offsets np array 0 1 2 gt gt gt mtx sparse dia_matrix data offsets shape 4 4 gt gt gt mtx lt 4x4 sparse matrix of type type numpy into4 with 9 stored elements 3 diagonals in DIAgonal format gt gt gt mtx todense ene asec I Or oO T doe ore E 5 ee Dos wae ss pd LO Oy 3 41 gt gt gt data np arange 12 reshape 3 4 1 gt gt
115. 2 3 Elevation surface in 8 meb eTii O 15 2 3D plotting functions 281 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 In 9 nome asc NO OE WP OLOR PES IE OCTO 0M In 10 r TI 2 f y2 In 11 z np E In 12 mlab surf z warp_scale auto QuigLt2 s senthoughs mayeavimodules surface Surtdcoe Object at Oxcob99efce 15 2 4 Arbitrary regular mesh in Tish mhe cE O in Hid phig theta np moridlO np pi llI R senp sq el wg In r5 pup sept Mo cos theta In 16 np sin phi No Simtehewa In 17 Ap Cos PRT In 18 mlab mesh x yp 2 In 19 mlab mesh x y 2 representation wireframe color 0 0 0 Out 19 enthought mayavi modules surface Surface object at O0xcel017c 15 2 3D plotting functions 202 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 15 2 5 Volumetric data in 20 millab vei In 2l1 55 595 zx mpenmcsrbd 9995958942 Se E 95545941 tn Wael wssusso B S EE cm E in 23 mlab contour3d values Our 24 enthought mayavi modules iso surface TIsoSurftace object at xcte3929 This function works with a regular orth
116. 20 gt gt gt y No cos x 0 3 np random rand 20 gt gt gt gt Mp polyldinp ooOlyirait ie 3y gt gt gt t np linspace 0 1 200 Poe plbsp lou c yy Ow ty o bu esty lt matplotlib lines Line2D object at 1 3 1 2 1 1 1 0 0 9 0 8 0 6 0 0 2 0 4 See http docs scipy org doc numpy reference routines polynomials poly 1 d html for more More polynomials with more bases Numpy also has a more sophisticated polynomial interface which supports e g the Chebyshev basis 3r 2x2 1 gt gt gt p np polynomzal Polvnomraltl 1 gt gt gt p 0 SE 0 sow proots array pL A 0 59 595159 9 59 gt gt gt p degree In general polynomials do not always expose 2 Example using polynomials in Chebyshev basis for polynomials in range 1 gt gt gt c npe ane Pace a d 2000 gt gt gt y mypcos x VU h random rand 2000 gt gt gt p np polynomial Chebyshev fit x gt gt gt t np inspace l 1 200 LE A E a se tes 3 4 Advanced operations lt matplotlib lines Line2D object at dcosrs Ja Mit rerene order 79 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 lt matplotlib limes LineZD obyect at ex zo plte plotte puts Meet E lt matplot irb limes LineZzD ODJECE Sb x 1 0 0 5 0 0 The Chebyshev polynomials have some advantages in interpolation 3 4 2 Loading data files Text files Example populations txt
117. 4456585 4 4H ee hE eSEK EO Eee GS A review of the different optimizers 55 6 2 ou x xoxo C9 BSS ome m ER OH Ro o OS 13 3 Practical guide to optimization with SCIDY sooo Romo ox RR REOR 8446665 ee o 8 13 4 Special case non linear least squares 4 ono o 88 062448 wo OES OS EC OR OE E s 142 143 144 148 156 159 160 172 181 184 187 187 191 191 193 194 198 201 201 202 204 205 207 207 209 22 222 227 228 229 231 232 231 240 245 246 248 255 257 13 5 Optimization with constraints 2 222 ee 259 14 Traits 261 INE IC r 262 I4 TOS 2022 30 9 09 349 2 908 9 REOR OG EM ee WB EE eee wR Ee Ww s m 262 l43 Whatare fals A e sers deo 3o ee ei dia ERE ORES ORO XL ESE ER 263 ihe References wax o 39 3 0 9n XR RGEOGe AX Oe Oe Gow oe eh owe 4 3 Xo ow s 278 15 3D plotting with Mayavi 280 IS JosmpieccaDi domos 9 x4 Bebe 49 RES EEO EO us xo d S wu d 280 15 2 3D plotting functions s sss resad srid Eee REE ob ERE 9 x ox 281 15 3 Figures and decorations 2 nue x xoxo RE KEE BASSO EHS Eo RSE HEHE X 43 284 IK TMa GON 224 eee eee ewes OS ee oO REG RIEN Eee 208 ee POR P5 eS 287 16 Sympy Symbolic Mathematics in Python 288 tol Fist Steps WIN SYMTyY Geta dee ea Be Che Erts orae EAGER YE X ok XO X HH 289 16 2 Algebraic manipulations a ae ee tke EORCE ORE ERE EUR OR RUE b BEES 290 LO CONSE uuu use qns Ce tytn Ea RU ee ee eee ee ee HE
118. 51615L Floating point numbers Los s Ap linio np Tloat eps SH R G H 9c EE gt gt gt MP E R H A H 44004 92750313 11 gt gt gt np float32 1e 8 np float32 1 1 Prue gt gt gt np float64 1e 8 np float64 1 1 False Complex floating point numbers Smaller data types If you don t know you need special data types then you probably don t Comparison on using float32 instead of float 64 Half the size in memory and on disk Half the memory bandwidth required may be a bit faster in some operations In 1 a np zeros 1e6 dtype np float64 In 2 b np zeros le6 dtype np float32 In 3 timeit axa 1000 Loops best of 55 1 76 ms per loop In 4 timeit b b 1000 loops best of 3 1207 ms per loop But bigger rounding errors sometimes in surprising places i e don t use them unless you really need them 3 3 2 Structured data types sensor code 4 character string position float E value o 3 3 More elaborate arrays 73 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt samples np zeros 6 dtype sensor code S4 postition ElLoOac value Eloat gt gt gt samples ndim 1i gt gt gt samples shape 6 gt gt gt samples dtype names sensor code position gt gt gt samples ALFA TAN Dr CREE la A pup rom gt gt gt samples anres HAC 120 Wade EIE a AGE A
119. 7 cese peel test function 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 x radius In 8 disk_area 1 5 Out 8 7 0649999999999995 Note By default functions return None Note Note the syntax to define a function the def keyword is followed by the function s name then the arguments of the function are given between brackets followed by a colon the function body and return object for optionally returning values 2 5 3 Parameters Mandatory parameters positional arguments In 81 def double it x ce dr return x gt 2 In 82 double it 3 Out SZ 59 6 In 83 double it TypeError Traceback most recemt call last j Users courns sre scipy2009 7 scipy_2009_ctuterialy source lt ipyehom Console gt ian lt medule TypeError double_it takes exactly 1 argument 0 given 2 5 Defining functions 20 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Optional parameters keyword or named arguments In 84 def double_it x 2 Dabo return x x 2 In 85 double wt xU os es 4 In 86 double_it 3 Ou a6 6 Keyword arguments allow you to specify default values In 124 bigx 10 In 125 def double_it x bigx S return x gt 2 In 126 bigx 1e9 Now really big In 128 double_it Ou 12612 20 M
120. Boo 50 5 15 gt gt gt a T arca II 0 7 pee oris Note Linear algebra The sub module np linalg implements basic linear algebra such as solving linear systems singular value decomposition etc However it is not guaranteed to be compiled using efficient routines and thus we recommend the use of scipy linalg as detailed in section Linear algebra operations scipy linalg page 108 Exercise Generate arrays 2 0 2 1 2 2 2 3 2xx4 anda j 2 3j 3 2 2 Basic reductions Computing sums gt gt gt x NMowarray Il 2 gt 37 up gt gt gt np sum ix 10 gt gt gt x sum 10 axis O Sum by rows and by columns Do x wonpaarravcrpbe Xe ume ur gt gt gt x arrar lik ails fa 249 gt gt gt x sum axis 0 columns first dimension arrar L eT See H Ee ls Aci 3 2 Numerical operations on arrays 53 ae gt gt gt x sum axis 1 rows second dimension array GE 4 Doo sou s Sc 2 4 same idea in higher dimensions gt gt gt ox onp random andt 2 gt gt gt x sum axis 2 0 1 1 14764 ooo sq ss ITA OSS 4 Other reductions works the same way and take axis e Statistics gt gt gt x np array l gt gt gt y np array l gt gt gt x mean ES gt gt gt np median x 9 gt gt gt np median y v axis l 7 last exis abray 2a EX gt gt gt x std full population standard dev 0 82915619758884995 e Extrema
121. DES Uu Op o LJ SEE Je 2 X Oy n aN ates B ey oO RR p ds S BGH Sni da e eie 4A tenth Pu AREE brite hood OQ ny G aeos POSSE 705 t us End b H O SER SO SU EaR Betis LKA KA HT sess ae Bees TRT orem Renton gees HGH HHH err Y ao DO 0 26 iQ ose Sous DEG deste eb BGH HH U THA TT Yw Ns Y 24 2s SEE nrn 3 gt In 1c Noise ier Transform the Fast Four ise using to clean up the no 1S exercise We alm th 2 Load the e D lt z D e G e c O 2 z D Ke N lt i9 ea Ban z en c c oN ne e e c O oN e i D gt oF D Mem oO CO gt lt fa d lt imrea image using p transform 1er image ing fftpack and plot the spectrum Four ion in scipy transform to see the result 1er Four Inverse frequency part of the spectrum so set some of those components to zero use array slicing of the image Do you have any trouble visualising the spectrum If so why 4 The spectrum consists of high and low frequency components The noise is contained in the high 3 Find and use the 2 D FFT funct Apply the 5 imize opt scipy ion and fi imizat 6 5 Opt solution to a minimization or equality ing a numerica Optimization is the problem of find 1 scalar or
122. DON NIE TEC TOUS SIBI Hm OO RISO Ule eae ev NPY oe bOUERIE Input vote pute vibes rl NPY SCpOuUrRIE elementwise funcs 0 void mandel single point TLoopecrumeslli hscEyUbunc cEE E SED HC NOME DU se es se NPY E LOAT input output types 4 NPY_CFLOAT input output types lo NEY SCE LOA elementwise funcs 1 lt void gt mandel_single_point_singleprec mandel PyUFunc_FromFuncAndData Leop EN elementwise funcs LAE OUTPUT EPES 2 d NUMber ot supported input types SEE 2 4p number oi input args T number Or outpuL args OF identity element never mind this mandel function name mandell z computes iterated zz Wut F sdoeserang 8 2 Universal functions noce 179 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 OS 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 l e scalar fu my gt Matrix product input 1 shape m n input Shapes n vp output shape m p i Mr ar P e my 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 e new ones can be cr
123. E 00 x 1 250096E 00 ze A990 0E 01 arra InZa009622 0249902781 Warning The above problem is known as the Lasso problem in statistics and there exists very efficient solvers for it for instance in scikit learn In general do not use generic solvers when specific ones exist Lagrange multipliers If you are ready to do a bit of math many constrained optimization problems can be converted to non constrained optimization problems using a mathematical trick known as Lagrange multipliers 13 5 Optimization with constraints 260 CHAPTER 14 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 Either wxPython http www wxpython org or PyQt http www riverbankcomputing co uk software pyqt tro Numpy and Scipy http www scipy org Enthought Tool Suite 3 x or higher http code enthought com projects Al required software ca
124. E EX o pl Cont ouri co oro e Now our plot area is filled o xl 5 8 6 Histograms We can make histograms Let s get some normally distributed random numbers from numpy r numbers np random normal size 1000 Now we make a simple histogram gt gt gt pl hist r_ numbers With 100 numbers our figure looks pretty good 5 8 Other Types of Plots 99 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 5 8 7 Loglog Plots Plots with logarithmic scales are easy gt gt gt Dili toglog npsarange 1000 We set the major and minor grid gt gt gt pl grid True gt gt gt plogrid True whrich mrnor Now we have loglog plot o xl If we want only one axis with a logarithmic scale we can use semilogx or semilogy 5 8 8 Pie Charts Pie charts can also be created with a few lines gt gt gt data 500 700 300 s labels I ears ddOJgS Other gt gt gt pl pie data labels labels The result looks as expected 5 8 9 Polar Plots 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 gt gt gt r np arange 360 gt gt gt theta r 180 pi Now plot in polar coordinates o gt Dipolar ue E We get a nice spiral 5 8 Other Types of Plots 100 Python Scientific lecture notes Release 2012 3 EuroScipy 2012
125. I gt gt gt a durs 0l 10 20 90913 gt gt gt ad b array px Ue s EOS H Ars 20 dur mons ese Seale HIE We have already used broadcasting without knowing it gt gt gt a np ones 4 5 gt gt gt a 0 2 we assign an array of dimension 0 to an array of dimension 1 gt gt gt a guru Wb Z2 ep Sey een o ud EK dhens wea fre wiley hee odes files Iles ikar Dels S 1 iL al BEN le LS LA BR c 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 mu wleposts Npeakray II p 199 209 7364 ol 117571475 1544 P 1913 2448 gt gt gt distance array np abs mileposts mileposts np newaxis gt gt gt distance array ende vC Oj 198 9035 753567 99 1175 1475 1544 1913 l 198 OF MOS S38 OUO Ds AU L346 1715 L035 LOS Or 483 S66 ule iy L241 1610 7367 SL 433 Oe geo 4395 739 Xe 117 Ll erl Gls Boe aaa iS On 304 604 Olp 1042 I Ie gee 977 clay 439 304 O 3007 369 739 Lavo erp Lilie Feo 04 300 OF 69 Wc 15443 13467 1241 390904 Gia 269 69 0a 267 Pode ile Velo X Hd Fase 2507 Sco OF 24495 229507 E SED
126. I 295 3 2 Numerical operations on arrays 59 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 27 Kh E h L o ps eme Do dista ee e E A 242 a Vie Ap mewa xis es 2 gt gt gt distance array LLL 0 d 5 D4 ce 4 lg Ee tlk EL dea eo 2 22 9050 Ie 8 eye Co Xa 7 EE e ly eae p 2eesS60GTSS 2320472 712 EI 4247713595 S p 191922 7060 35055702902 ue M OCC E i 4 p Ste Osea AAT Lx p 54955900942 9 in Color poc Pto pce hor dietadan e doctest ELLIPSIS Met plot ip scollhectiome PoOlyColleceion obe ae du gt gt gt mde colorbar t doctest ELLIPSIS mat plot lib c olorbdbar C oLorbar Anstance ae 5 zee plt a o equal 0 07 200 0 Ero UI UJ NJ I Remark the numpy ogrid function allows to directly create vectors x and y of the previous example with two significant dimensions gt gt gt x y inp OGrid 0 5 rs gt gt gt x Y array LOT p RET 3 pes src E e uc Oy estia gt gt gt x shape y shape Cor NE qu cU 3 2 Numerical operations on arrays 60 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt distance np sqrt x 2 y 2 So np ogrid is very useful as soon as we have to handle computations on a grid
127. Into 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 Summary e Numpy array block of memory indexing scheme data type description ndexing strides byte position np sum arr strides indices Various tricks can you do by playing with the strides stuff for an advanced tutorial it is While it is off topic in a chapter on numpy let s take a moment to recall good coding practice which really do pay off in the long run Good practices 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 Except some rare cases variable names and comments in English 3 4 Advanced operations Section contents Polynomials page 78 Loading data files page 80 3 4 1 Polynomials Numpy also contains polynomials in different bases For example 32 2x 1 23 p Npspelylai s 2 Ln gt gt gt p 0 Su gt gt gt p roots array Ula 2X irs S15 o e es e 2x R Z 3 4 Advanced operations 78 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 l gt gt gt x np lamspace 0 1
128. NE OY os system ls debug_file py demo2 py demo py demo pyc my frle py CONES demo2 py demo2 pyc demo py my file py pi wallis image py Walking a directory os path walk generates a list of filenames in a directory tree In 10 for dirpath dirnames filenames in os walk os curdir for fp in filenames print os potnobospucth ip 7Ulsev sv courns src sciley2009 sciey_ 2009 05091193 19 8 01016 eV cries stt o SwO Users courns src scapyZ00U97 serpy 2009 tutorial source view array SWE J Users churns src scipyZ009 scipy 2009 tutorial so rce basic types ral j Users courns src scipoy20090 scioy 2009 tutorial source cour Py 2 8 Standard Library 34 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 j Users courns sre scioy20090 scipoy 2009_cuterial source control flow rst Environment variables In 9 import os In 11 oS environ keys Out LL s L esr FSLDIR TERM PROGRAM VERSION AHS TEREMOANA USER HOME PATH VS hs SHELL EDITOR WORKON HOME ey FHONPATH In 12 os environ PYTHONPATH Quid Zils 23 User coir iay sree leat gseses Coulda a ey nuu CO e Users cburns local lib python2 5 site packages just local lib python2 5 site packages Library Frameworks Python framework Versions 2 5 lib python2 In 16 os getenv PYTHONPATH Our TMG 2e Users churns src urlls Usetre duutus src mito
129. NGSerPS cbuUBHS bocal bsm Users courns 7 local lib pyehon2 5 site packages qrim l 1 py2 5 eq0 Users cburns local lib python2 5 site packages argparse 0 8 0 py2 5 eg9g Users couirns local lib pyehon2 5 site packages urwid 0 9 7 l py2 5 600 Users courns Vocal lib ovyehon2 5 site packages yolk 4 1 py2 J5 cq0 Users cburns local lib python2 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 In 1 import pickle In 2 1 1 None Stan In 31 picklevdump l tale testopkl Ta In 4 pickle load file test pkl Out 42 Tl None Stan Exercise Write a program to search your PY THONPATH 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 Exception handling in Python 36 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 2 9 1 Exceptions Exceptions are raised by errors in Python ZeroDrivisronEbError integer division or modulo by zero TypeError unsupported op
130. Python Scientific lecture notes Release 2012 3 EuroScipy 2012 EuroScipy tutorial team Editors Valentin Haenel Emmanuelle Gouillart Ga l Varoquaux http scipy lectures github com August 24 2012 2012 2 77 g24cebec Contents Getting started with Python for science 2 Scientific computing with tools and workflow 3 Lh Mi PY 2 223 022 93 2 4 99 95 4 952414 9499 83924 49926 3 1 2 Scientific Python building blocks 46m 9E 9 399 3 mo x ORO yx Uy Y o9 9s 5 1 3 The interactive workflow Python and atexteditor 0 000000 ee eee 6 The Python language 8 ZI 3PIESDOPODS 2368 9 3 93 ew oe OR Ea ER ROO Ro Rr m QR P 3 XXE 9 2 2 DCOP E pb ooo 4 3 99 3 9933 9 92394 4 e bn T Sw CES EEA 94599 3 9 2 3 Assignment operator amp 40x ook wok Ro bOR X ROW e 4e RR ROW vee Heenan bon ees 15 24 Control Flow sss xo eA oa eo x 343 Eee EG RESO ee SRR ee ES S 16 2 5 Tennis TUBOLIODS nom eon be 4 R3ekoRBox edeG ox 93 e KR HE EEE 3 43x R33 20 2 6 Reusing code scripts and modules 2 4 54 9 9 9 X 37x ev RO 9 Xo eee ES OH 24 2 6a ss eA KEKE EEO ERE BEE EEREE EEE EMG 3l 2 5 E rt h tcs B LIDA v uou ee a ee we RJ de m a EE Be EEE Pee EEE RE S X HH 22 2 9 Exception handling in Python 2 ees ok ised bee 9 Ro UR OEC OR US be UR EUR EORR ER 36 2 10 Object oriented programming OOP dw XR RORURORX o XO EERE EES SEE EY 30 NumPy creating and manipulating numerical data 40 3 1 The numpy array object 2132 9 on eS Bo oe eee ES
131. S Ge RR 291 16 4 Equation solving 2 246646 566 Xs x X R39 Sw bo S ROROR RE wm 0 OE Re Eee Re ES 292 105 Ikea ACOs gee he kee eee hE ee RS EARS EER EEE ESE BERS Ewe ES 293 17 scikit learn machine learning in Python 295 17 1 Loading an example dabiSeL 3 anu ish bee 9x 93 he RR RRR EE RO 909 x 3 x 3e 206 252 e e Lu 99 eee Oe 909 EERE 9 5 99 9 9 2 89 Ee EORR A E S S S 297 17 5 Clustering grouping observations together lt 4 6266624 o o Room oS Xo RR dee ES 300 17 4 Dimension Reduction with Principal Component Analysis css 301 17 5 Putting it all together Tace recognition i229 6 ibe eee ox ER ew be we xc WOW ox Re 4o ox 302 17 6 Linear model from regression to sparsity 2222s 303 17 7 Model selection choosing estimators and their parameters ls 304 Index 306 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Contents 1 Part I Getting started with Python for science 1 1 CHAPTER 1 Scientific computing with tools and workflow authors Fernando Perez Emmanuelle Gouillart Ga l Varoquaux Why Python 1 1 1 The scientists needs Get data simulation experiment control Manipulate and process data Visualize results to understand what we are doing Communicate results produce figures for reports or publications write presentations 1 1 2 Specifications Rich collection of already existing bricks corresponding to classical numerical methods or
132. S R e 2655560979 gt gt gt pi cexp 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 16 1 First Steps with SymPy 289 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt oo gt 99999 True z gt OO r l OO 16 1 2 Exercises 1 Calculate v2 with 100 decimals 2 Calculate 1 2 1 3 in rational arithmetic 16 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 U x gt gt gt y Symbol y Then you can manipulate them gt gt gt x y x y 2X gt gt gt xty eZ oC ey ea Z Symbols can now be manipulated using some of python operators arithmetic amp gt gt lt lt boolean 16 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 16 2 1 Expand Use this to expand an algebraic expression It will try to denest powers and multiplications im 23 expand cxt Ole PAS S Saxtyxn Bayer Beers SS yas Further options can be given in form on keywords In 28 expand xty complex True Outil Tims simy F rols F rey im a Ol exeand Cos Cos we Or
133. 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 File input output scipy io page 107 Special functions scipy special page 108 Linear algebra operations scipy linalg page 108 Fast Fourier transforms scipy fftpack page 109 Optimization and fit scipy optimize page 113 Statistics and random numbers scipy stats page 117 Histogram and probability density function page 117 Percentiles page 118 Statistical tests page 119 Interpolation scipy interpolate page 119 Numerical integration scipy integrate page 120 Signal processing scipy signal page 122 Image processing scipy ndimage page 123 Geometrical transformations on images page 124 Image filtering page 124 Mathematical morphology page 125 Measurements on images page 128 Summary exercises on scientific computing page 128 106 Python Scientific lecture notes Release
134. Type Tuple etc Custom default values can be defined in the code from traits api import HasTraits Str Float 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 Complex initialisation of the reservoir name return Undefined 14 3 2 Validation Every trait does validation when the user tries to set its content reservoir Reservoir name Lac de Vouglans max_storage 605 reservotr max storage 2307 Dead eit ret Traceback most recent call last cu dipinte PONE Sele ecciesie theatres ipyvenen impue o Fearn 90 7 4a gt in mod 14 3 What are Traits 264 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 T reservoir max storage 230 Users eliam eom occ etis pacis Aia ci ipao handlers pyc in Curren sels object mame value 166 vu 167 raise TraitError object name self full_info object name value Weis value 18539 170 def arg error self method arg num object name value TraitError The max storage trait of a Reservoir instance must be a float but a value of 23 14 3 3 Documentation By essence all the traits do provide docu
135. UPUMG I Tf As dd oic oes cs amps v dimensions Roy pte void PyUkune d d chars args Noy into dimensions Mpy ini pw stems void PYUPUME f 3 Chars args Mpy inip dimensions Npy Intp steps void PyUkunc g g ed argo R Into dimensionar Ny IM p Steps void PyUP Unc F As D D cChar args npy intp dimensions Npy 1ntpk vord PyUPunc_P_F chars args npycrtHtpe dimensions Npy_intp Steps void PyYUPUne D D chars args Mpy imap dimensions MPY 1inNtpP senes vord PyUFUmMC C G ehe x argo NPY inti dimensions NEY IN P siens steps voidx func void func voL nba void func steps voidx func void func void func void func yod EP VUE UNC fi tome Clicih eine ene ae ne dimensions Noy ne Gteos vourd func void PyUPUMC ff f chars args Noy intp dimensions Npy Eh steps vorid PyUkunc dd d char args np into dimensions noy Intp Steps vold PyUkunc 659 E RE argo Npy intpp dimensions NPY Intp Steps void func Node Eme void func void PVUP Une FE E As DDO D charss args Mpy Intp dimensions Apy Iie Steps Vald func vord PyUF une DD D charss drogs Mpy Into dimensions Npy IR E qo creps void PyYUP UMC FF F Char eros Noy inte dimensions Noy 1Intp Sens void PyUkunc GG G chare args npyewncep dimensions Moy Intps Steps Required module initialization import array IMPOL UTU he actual Eune declarar iion 8 2 Universal functions voL rune void func VoL rUn 175
136. Y Ge the Se We Si 3X ABEA 9 dtype sensor_code S4 D EA K ER T 19s op he TAUS ex 9s WO Sup Nc Cpu rr xvn rosse i il Field access works by indexing with field names gt gt gt Samples sensor code arra D AeA S PEIA onn C Re A aa dtype S4 gt gt gt samples value array One Oddo Wels SOUL Onli ONES gt gt gt samples 0 CREBRA NND Oe a gt gt gt samples sensor code TAU gt gt gt samples C TAU Multiple fields at once gt gt gt samples position value array I dus One iy beue POLI ICE Oe 30529 WS OO ee Aou ud Cezanne deype posicion tO value festa Fancy indexing works as usual gt gt gt samples samples sensor code ALFA arram DW SEA v dis s T s BEN s 0 ud dtype sensor code ody posi ion SEO y value lt 237 Note There are a bunch of other syntaxes for constructing structured arrays see here and here 3 3 3 maskedarray dealing with propagation of missing data For floats one could use NaN s but masks work for all types gt gt gt x np ma array 1 2 3 4 gt gt gt x Maskedarray data LL 3 mask False True False True fill value 999999 ao uw Np mM array tus 3 d E A gt gt gt X y masked_array data par a ae L mask False True True True fill_value 999999 3 3 More elaborate array
137. a Lau euer Cake Oe TAL e 1 1 EE 0 11 slicing and indexing soc oie ele L O20 xoc Mexi Leo lt 1x2 sparse matrix of type type numpy floato4 with 1 stored elements in Dictionary Of Keys format pos camxLT Ls3 2600densew macax uo dee Do poc mcs dq lpcelcodenmscet Traceback most recent call last NotImplementedError fancy indexing supported over one axis only Coordinate Format COO also known as the ijv or triplet format three NumPy arrays row col data data i isvalueat row i col i position permits duplicate entries subclass of data matrix sparse matrix classes with data attribute fast format for constructing sparse matrices constructor accepts dense matrix array 11 2 Storage Schemes 214 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 sparse matrix shape tuple create empty matrix data ij tuple very fast conversion to and from CSR CSC formats fast matrix vector sparsetools fast and easy item wise operations manipulate data array directly fast NumPy machinery no slicing no arithmetics directly use Examples facilitates fast conversion among sparse formats when converting to other format usually CSR or CSC duplicate entries are summed together facilitates efficient construction of finite element matrices create empty COO matrix gt gt gt mtx sparse
138. a and we add the setter to it by using the set ter method All this happens when we are creating the class Afterwards when an instance of the class 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 print deleting gt gt gt D a property object st Ux gt gt gt D a fget lt function a at Ox oo gt gt gt D a fset e EIT Wem a sort e S S a gt gt gt D a fdel EUNICE qon a at Ox coo Y gt gt gt d D X Varies this is not the same a function gt gt gt d a getting 1 il gt gt gt da 2 7 2 Decorators 153 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Sem tal mone gt gt gt del d a deleting gt gt gt d a getting 1 1 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 maintainin
139. akes the code fail every time 2 Divide and Conquer Once you have a failing test case isolate the failing code Which module Which function 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 Walk up and down the call stack nspect values of variables Modify values of variables e Set breakpoints 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 debug and drop into the debugger 9 3 Using the Py
140. ally flat minimum Consider the function exp 1 1 x 2 yx x2 This function admits a minimum in 0 0 Starting from an initialization at 1 1 try to get within le 8 of this minimum point 13 4 Special case non linear least squares 13 4 1 Minimizing the norm of a vector function Least square problems minimizing the norm of a vector function have a specific structure that can be used in the Levenberg Marquardt algorithm implemented in scipy optimize leastsq Lets try to minimize the norm of the following vectorial function gt gt gt def f x return Np arcran x np earctam np luvumspaee 0 1 Lent zx X0 Mp Zeros 10 13 4 Special case non linear least squares 257 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt optimize leastsq f x0 larray I 0 EET EU Le DERE 222225 n 35999 257 ORMAZA PES sisi eis 0 66666667 OVIT he 0 88888889 Jj Jes 2 This took 67 function evaluations check it with full_output 1 What if we compute the norm ourselves and use a good generic optimizer BFGS gt gt gt def g x return np sum f x 2 gt gt gt optimize rtmaon orgs g 0 Optimization terminated successfully Current uncer won value 0 000000 Tteractronss uv Function evaluations 144 Gradient evaluations 12 array L r 599992778 09 T LI TR obe ZAMBIE ie D 09551 91425071 4 44449794e 01 52 5556049s
141. ape gt gt gt mask a gt 0 5 gt gt gt opened_mask ndimage binary_opening mask gt gt gt closed_mask ndimage binary_closing opened_mask opened_mask closed_mask Exercise Check that the area of the reconstructed square is smaller than the area of the initial 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 e126 Lise 3 gt gt gt a 4 4 2 a 2 3 1 gt gt gt a array bI cu 07 07 0 NO Ol OG Se une Ser Joey i30 Oe Oe s 2p ie idus See LON GOs 2 2 EIR Dus Sip Bes en de 23s MO lc cb ESL re edu xS MO Be Oe 8a 10 OUS 105 ORIS gt gt gt ndimage grey_erosion a size 3 3 array 10 Or 07 07 07 0 0l Oe xp ME Oe Qo 102 wOuli Ore YO Du WR Ah 30 7 Yale POs Oe ite Ge he Ae Ce Oy Oe oe er Me OOS Due Or E 105 Oy 195 ALS Oss HO Orr Oe MOL Nc nO 6 10 Image processing scipy ndimage 127 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 6 10 4 Measurements on images Let us first generate a nice synthetic binary image po x y npzandices 100 200 o cnt Me sin Zee x SO emp Sa A mio ex R cy Ore uu
142. as Matlab Mathematica like functionality 5 2 2 pylab pylab provides a procedural interface to the matplot1ib object oriented plotting library It is modeled closely after Matlab TM Therefore the majority of plotting commands in py1ab has Matlab TM analogs with similar arguments Important commands are explained with interactive examples 5 3 Simple Plots Let s start an interactive session ipython pylab This brings us to the IPython prompt IPython 0 9 1 An enhanced Interactive Python 7 gt lnt Te E RT Hee H TPyvthorn s Ted ures THE DIET about EE HT ITE 87 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 help gt Python s own help System objecto gt Details about objects 7obyeet also works 7 prints mores Welcome to pylab a matplotlib based Python environment For more information type help pylab TA Eds Now we can make our first really simple plot In 1 import numpy as np In 2 import pylab as pl In 3 phopbot 0nprangswq9 SEL E pemacploucirb irmeshtnezD instance at OxOTAA2GOES Note In the following we use the Python prompt gt gt gt rather than the Python one as it facilitates copy pasting in IPython using the doctest mode Now we can interactively add features to or plot gt gt gt pl xlabel measured lt matplotlib text Text instance at sas gt gt gt pl ylabel calculated AMakphetlibweext T
143. as possible before combining them n place operations In 1 a np zeros 1e7 In 2 timeit global aj a Oxa IQ loops best of 5 lll ms per loop In 3 timeit global a a U 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 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 1e 7 In 2 smerst ascopy 10 loops best of 3 124 ms per loop In 3 timeit a 1 10 Loops best Of 53 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 171 In 1 c np zeros le4 1e4 order C In 2 timeit c sum axis 0 L loops best or 3 3 89 5 per loop In 3 timeit c sum axis 1 L loops best of 3 199 ms per loop In 4 c strides Out 4 69000079 This is the reason why Fortran ordering or C ordering may make a big difference on operations In I5 a np random rand 20 sse deo In 6 b np random rand 20 2 x18 Einb Sewraemc cWp ootw be cm l loops best of 3 194 ms per loop In 8 c np ascontiguousarray a T 10 4 Writing faster numerical code 205 Python Sci
144. asTraits Keeps track of the reservoir state given the initial storage n m reservoir Instance Reservoir min storage Float max storage DelegatesTo reservoir min release Float max_release DelegatesTo reservoir Python Scientific lecture notes Release 2012 3 EuroScipy 2012 state attributes storage Range low min_storage high max_storage control attributes inflows Float desc Inflows Tamsil release Range low min_release high max_release spillage Float desc Spillage hm3 def print_state self print Storage tRelease tInflows tSpillage Str format E voim I 44 426 for 2 an range 4 print str_format format self storage self release self inflows self spillage Print x 79 Prr Traits listeners FFFFTEEERETEEEREEE EERE EEE EET ET ETE EE HE EE def _release_changed self new COV et the release is higher than zero warn all the inhabitants orf the valley m 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 600 efficiency 0 8 State ReservoirState reservoir projectA storage 10 state release 90 state inflows U state print_state The static trait notification signatures can be e def _release_changed self pass e def release changed self new
145. at we have our first data arrays we are going to visualize them Start by launching IPython in pylab mode S Gi python poy ds Matplotlib is a 2D plotting package We can import its functions as below gt gt gt import matplotlib pyplot as plt the tidy way 1D plotting 3 1 The numpy array object 44 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt x np linspace 0 3 20 gt gt gt y NoO lanspace 0 9 20 oor puitoplou y f line plot matplotiib l ines Line2D obgyect at 2247 poc Plo PLOC we ftot F dot pol matplot lib lrnces LIine D cb ieee a is gt gt gt plt show shows the plot not needed with Ipython 3 0 0 5 1 0 1 5 2 0 2 5 3 0 2D arrays such as images gt gt gt image np random rand 30 30 gt gt gt plt imshow image cmap plt cm gray lt matplotlib image AxesImage object at gt o plt colorbar MakClOEn lio colombo odoris Instance us oum gt gt gt plt show 3 1 The numpy array object 45 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 20 25 See Also More in the matplotlib chapter page 87 3D plotting For 3D visualization we can use another package Mayavi A quick example start by relaunching iPython with these options ipython pylab wx or ipython pylab wthread in Python lt 0 10 E oo QLB 0248 KE D 14 Bs 0714 0457 In 58 from mayavi import mlab In 61 m
146. ate cache effects in array computing Example inplace operations caveat emptor s Sometimes gt gt gt a b is not the same as gt gt gt a b copy 23 gt x np array IL ul gt gt gt x x transpose gt gt gt X 0 uu pr e DUIS 8 1 Life of ndarray 171 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt y Nipsarray il gt gt gt y y scopy gt gt gt y e x and x 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 5 Findings in dissection 16480 E Me scalar Te Ol l ndarray e memory block may be shared base data 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 di ag CPU cache coherence 8 2 Universal functions 8 2 1 What they are Ufunc performs and elementwise operation on all elements of an array Examples e Automatically support broadcasting casting The author of an ufunc only has to supply the elementwise operation Numpy takes care of the rest The elementwise operation needs to be implemented in C or e g Cython Parts of an Ufunc Provided by user void ufunc loop void xargs
147. ation input structure None iterations 1 mask None output None border_value 0 origin 0 brute_force False Multi dimensional binary dilation with the given structuring 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 Previous Next Highlight all Match case Reached end of page continued from top e Scipy s cookbook http www scipy org Cookbook gives recipes on many common problems freque
148. ations together 300 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 17 4 Dimension Reduction with Principal Component Analysis The cloud of points spanned by the observations above is very flat in one direction so that one feature 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 gt gt gt from sklearn import decomposition gt gt gt pca decomposition PCA n components 2 puc occ In L Ss EIS a PCA copy True n components 2 whiten False gt gt X pea Lranstorm iris data Now we can visualize the transformed iris dataset gt gt gt import pylab as pl gt gt gt Die searcer x 20 5 Kiley jg Giris tare lt Matplorlibn collections Collection ob ject ab 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 17 4 Dimension Reduction with Principal Component Analysis 301 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 17 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 mt Stripped down version of the face recognition example by Olivier Grisel htt
149. ave not changed The Q 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 mm reservoir Instance Reservoir mO name DelegatesTo reservoir max 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 fg fTf ST fSydTZsTSESTSTSSTSSTESTSTPSTSSTSSTSTSSTSSTSTTSTPSTPST ST Y traits view View Group VGroup Item name Item storage Item spillage label State style readonly VGroup Item inflows Item release label Control fffT Private traits fTESTRESREERREEEERESETEERRHREEEE EREHREEEESAEETESTTTRRESASSTSTTTTT Python Scientific lecture notes Rel
150. ax release 100 0 head 60 efficiency O0 reservoir configure_traits gt mm Reservoir Max storage 30 0 Max release 100 0 Head 60 0 Efficiency 0 0 14 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 mnm 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 monta ce Tour SS inflows Float desc inflows hm3 release Range low min release high max release spillage Float desc Spillage hm3 def print_state self print Storage tRelease tInflows tSpillage Ar format VE Jorn 4 7s2rr for cin tanga ERIE print str format format self storage self release self inflows self spillage Print x 79 l if name main projectA Reservoir name Project A max storage 30 max release 100 0 hydraulic head 60 efficiency 0 8 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 State ReservoirState reservoir projectA storage 10 state release
151. 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 is neither 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 lab or a com pany the code should be as readable as a book Thus the language should contain as few syntax symbols or unneeded routines as possible 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 if 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 scientists use to work Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Example BLAS vector matrix operations e Drawbacks Painful usage no interactivity during development
152. 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 examples 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 intro summary exercises examples max speeds npy gt gt gt years nb max speeds shape 0 Following the cumulative probability definition p i from the previous section the corresponding values will be gt gt gt cprob np atange years nb dtypesup tloat3s2 1 years nb 1 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 AII 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
153. 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 segfault py we can run the script in gdb as follows Doe CHE wien gdb run segfault py Starting Program Usr bim pyemon seqtrault py Thread debugging using libthread_db enabled Program received signal SIGSEGV Segmentation fault strided byte copy dst 0xs537473 360 3436 cutstrides 4 src 0x86c0690 Address 0x86c0690 out of bounds instrides 32 N 3 elsize 4 9 4 Debugging segmentation faults using gdb 198 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 at un Acme mmus tos 5 365 EASY SMO Ving INES 2 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 OxO0O04af4f5 in _copy_from_same_shape dest value optimized out src values optimized out myfunc 0x496780 strided byte copy Swap 0 at numMpy COre src muilisarray crors c 748 748 my unc dits dara pti Cest
154. bject at gt Now reassign labels with np searchsorted gt gt gt labels np unique 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 a gt plr Nm Sb rom matplotlib image AxesImage object at gt 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 from scipy import misc gt gt gt 1 misc lena gt gt gt Gx Sy l shape po X uo Pog idl Osx Qs gt gt gt regions sy 6 x X 4 Y 6 note that we use broadcasting 12 6 Measuring objects properties ndimage measurements 241 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 12 6 Measuring objects properties ndimage measurements 242 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt block mean ndimage mean l labels regions index np arange l As pPegaons me t17 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 168 Non regularly spaced blocks radial mean SX sy l shape Xe POO oie ous oc Ursy Tomo sca RS Poin 20 ae ae max neas tpe mp int radial mean
155. bution p and takes 50 steps gt p_50 e Computes the stationary distribution the eigenvector of P T with eigenvalue 1 numeri cally 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 no random rand dot no linalg eig reductions abs argmin comparisons all np linalg norm etc 3 2 7 Summary What do you need to know to get started Know how to create arrays array arange ones zeros Know the shape of the array with array shape then use slicing to obtain different views 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 soc ala 0l 0 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 3 More elaborate arrays 3 3 More elaborate arrays T1 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Section contents More data types page 72 e Structured data types page 73 e maskedarray dealing with propagation of missing data pa
156. c 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 2012 3 EuroScipy 2012 1 2 Scientific Python building blocks Unlike Matlab Scilab or R Python does not come with a pre bundled set of modules for scientific computing Below are the basic building blocks that can be combined to obtain a scientific computing environment e Python a generic and modern computing language Python language data types st ring 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 testing documentation generation Bi Shell Konsole lt 2 gt gz Python an advanced Python shell http ipython scipy org moin e Numpy provides powerful numerical arrays objects and routines to manipulate them http www numpy org e Scipy high level data processing routines Optimization regression interpolation etc http www scipy org 0 54 0 53 0 52 0 51 e Matplotlib 2 D visualization publication ready plots http matplot
157. cdouble inl npy cdouble in2 npy cdoublex out e Only elementwise func needs to be supplied 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 zz c 8 2 Universal functions 173 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 where c x ty 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 ZU for k in range iterations V AK Oe SC say 100 iterations or until z real 2 z imag x 2 gt 1000 Use it to determine which c are in the Mandelbrot set e Our function is a simple one so make use of the PyUFunc x helpers Write it in Cython See Also mandel pyx mandelplot py T T T w E E E E E R Sis Fix the parts marked by TODO Compile this file by Cython gt 0 12 required because of the complex vars cython mandel pyx Python setupspy build et i and Ery ae cuc with imn Chie 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 rn double complex z ut nogi T The Mandelbrot iteration T T Some points of note T t E Ss GL 0 allowed to call any Python functio
158. 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 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 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 the current solution vector LinearOperator Class from scipy sparse linalg interface import LinearOperator common interface for performing matrix vector products 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 example gt gt gt impo
159. cision complex gt gt gt mtxl mtx astype np complex64 gt gt gt x dsolve spsolve mtxl rhs use_umfpack False gt gt gt print x LO CaO Ou eA reorua e Le OOs gt gt gt print Error y mexi se 6 PhS Pru oU L 4Q EO QuFO 3g O 0 7 Oet0 7 04047 solve as double precision complex gt gt gt mtx2 mtx astype np complex128 gt gt gt x dsolve spsolve mtx2 rhs use_umfpack True gt gt gt print x LOG 00u J 21 0402 55 0 9 1 550 7 Ors UT Jui gt gt gt print Errors gt mios we co rhs IE ES E EO HIE R Op HEE E E E E I Bsp M m Construct a 1000x1000 I 1 matrix and add some values to zt convert if to CSR format and solve A x b for x and solve a linear system with a direct solver import numpy as np import scipy sparse as sps from matplotlib import pyplot as plt from scipy sparse linalg dsolve import linsolve rand Mp random rand mtx sps lil matrix 1000 1000 dtype np float64 mes lO sos and 100 mex EDS OO amex LO BLEI MEX Setdvag rand 1000 IE sc plt spy mtx marker markersize 2 puc sow mtx mtx tocsr rand 1000 linsoilve spsolve mex rhs print rezidusl moe linalo norm mex o rhs examples direct_solve py 11 3 Linear System Solvers 222 11 3 2 Iterative Solvers e the isolve module contains the following solvers bicg BIConjugate Gradient bicgstab BIConjugate Gradient STABilized
160. co plot editor import ChacoPlotItem from reservoir import Reservoir class ReservoirEvolution HasTraits reservoir Instance Reservoir name DelegatesTo reservoir inflows Array dtype np float604 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 teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eee eee HH HH HEA THE traits view View item 7 name l Group Python Scientific lecture notes Release 2012 3 EuroScipy 2012 chacobPlotitemt t Monch stock show Label Pelse E widen 500 resizable True Traits properties 9T STq3qTTT PTTSTTTTHTTTScrPEPTSTTSSSPRSPETESTPSygqsrpSsPySS3y get stock self i m m fixme should handle cases where we go over the max storage wee return self initial stock self inflows self releases cumsum get month selt return np arange self stock size if name aes as matu 7 3 reservoir Reservoir name Project A max storage 30 max release 100 0 head 60 efficiency 0 8 qe dal sroek 10 ini Lows ts Np array Oez releases_ts np array 4 view ReservoirEvolution reservoir reservoir inflows inflows_ts releases releases ts view configure_traits Edit properties Name Project A Plot Editar stock c ob nm o M te el ete ERES Le
161. context managers When discussing generators page 145 it was said that we prefer generators to iterators implemented as classes because they are shorter sweeter and the state 1s stored as local not instance variables On the other hand as described in Bidirectional communication page 146 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 Qcontextlib contextmanager def some generator arguments S up try yield lt value gt finally 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 the closing example as a generator contextlib contextmanager def closing
162. cted square matrix The scipy linalg inv function computes the inverse of a square matrix gt gt gt arr ne varray il 2 opo gt gt gt larr linalg inv arr gt gt gt iarr array EE Wee Hi be diese mts sc nu gt gt gt NP alie loce np dot learn apace D True Finally computing the inverse of a singular matrix its determinant is zero will raise LinAlgError p arr np array lis Zl slong ion Tu gt gt gt donee inv ert Traceback most recent call last fimetokr ror Ssimguilar matrix More advanced operations are available for example singular value decomposition SVD poc dtr Mp arange 9 reshape 3 3 tape diag il 0 11 gt gt gt uarr spec vharr linalg svd arr The resulting array spectrum is Special functions scipy special 108 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt spec array 42059909544 0245294236 0 29654967 The original matrix can be re composed by matrix multiplication of the outputs of svd with np dot gt gt gt sarr np diag spec gt gt gt svd_mat uarr dot sarr dot vharr gt gt gt np allclose svd_mat arr True SVD is commonly used in statistics and signal processing Many other standard decompositions QR LU Cholesky Schur as well as solvers for linear systems are available in scipy linalg 6 4 Fast Fourier transforms scipy fftpack The scipy fftpack module allows to compu
163. ction on arithmetic etc and manually ast ype dtype data re interpretation manually view dtype Casting Note Exact rules see documentation http docs scipy org doc numpy reference ufuncs html casting rules 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 Casting in general copies data gt gt gt x np array ll 2 3 4 dtype np tloat gt gt gt X mmc 1 ales 2 yg C Pe ZR gt gt gt y x astype np int89 gt gt gt y array l 2 3 lg vdEVDOCEDEO gt gt gt y Fk Ul array l2 3 4 5 dtype inte 5 wn du 5 6 array Ll 2 3 4 dbypDe 1nto pou 256 0 T L p Zu S xe aoe yy mpeatrayvtl2581 sdetvpesmpsrnus2 array 2577 255 2597 260 Aves 22 Casting on setitem dtype of the array is not changed on item assignment gt gt gt y y 15 gt gt gt y array 2 3 4 5 dtype int8 Re interpretation viewing 8 1 Data block in memory 4 bytes 6x01 1 9x92 ii 0x03 1 0x04 Life of ndarray 164 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 4 of uint8 OR 4 of int8 OR 2 of intl6 OR 1 of 1nt32 OR 1 of float32 OR How to switch from one to another 1 Switch the dtype 2 x Np array 2 3 4l dtiype np uintse gt gt gt x dtype 12 gt gt gt
164. dam and reservoir system The reservoir and the dams do have a set of parameters e Name Minimal and maximal capacity of the reservoir hm3 Height and length of the dam m e Catchment area km2 Hydraulic head m 14 1 Introduction 262 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 P is 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 g is acceleration due to gravity of 9 8 m s2 e k is 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 storage 41 storage inflows release spillage irrigation 14 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 s Standardization Initialization Validation
165. db m gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 4 3 l gt 4 import numpy as np gt Import Seip y Jac Sp ipdb 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 c gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 34 38 NR noisy_img noisy_img 35 denoised_img local_mean noisy_img size size Using the Python debugger 196 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 e Step into code with n ext and s tep next jumps to the next statement in the current execution context while step will go across execution contexts i e enable exploring inside function calls ere lees gt home varoquau dev scipy lecture notes advanced debugging optimizing wiener filtering py 3 2 34 noisy img noisy img gt 35 denoised_img local_mean noisy_img size size 36 l var local_var noisy_img size size upeb m gt home varoquau dev scipy lecture notes advanced debugging optimizing wiener filtering py 3 35 denoised_img local_mean noisy_img size size gt 36 l_var local_var noisy_img size size 37 for i in rangel e Step a few lines and explore the local variables ipdb n gt home varoquau dev scipy lecture notes advanced debugging_optimizing wiener_filtering py 37 36 l var
166. directories This list includes a list of installation dependent default path e g usr lib python as well as the list of directories specified by the environment variable PY THONPATH The list of directories searched by Python is given by the sys path variable In 1 import sys In 2 syscpatcth OE 1 E Lodo som s YJ usr local unetbt ds ent cughtotravres h 0 ust Lib ey eda e dd o gt use Mle ey t hon O plaoe Tinu y usr lib pytnon2 60 11b tk sie ea a Oe d a 7 Idus ip py nona 6 7 Lib dyrnilead Lots lib eytnon2 6 dist packages Pos lib epymodules python 6 dace Ae pymocules b meo erts deste Lorem do python T dirscopacieages wxezeoegtiez undcosde uste local lab pythonZ 6 7 dist packages user libp pythnon2 6 dist packages usc Lib epymodules pytnon2 6 1Python Bxtensions 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 usr local lib python2 6 dist packages You may use symbolic links on Linux to keep the code somewhere else 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 suppor
167. do_something var finally manager 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 issimply 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 in a finally clause and will berethrown after exit 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 init__ self obj self obj obj def enter self return self obj def exit self xargs ee selt obj elose gt gt gt with closing open tmp file w as f fowrite thecconmtentsin 7 3 Context managers 156 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Here we have made sure that the close is called
168. documentation for np tile and use this function to construct the array 3 L 3 iL 3 1 The numpy array object 43 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 3 1 4 Basic data types You may have noticed that in some instances array elements are displayed with a trailing dot e g 2 vs 2 This is due to a difference in the data type used 2 a Np array 1 2 3 gt gt gt a dtype diyoe umtod eer b np array ley ag wd gt gt gt b dtype dtype floato4 Different data types allow us to store data more compactly in memory but most of the time we simply work with floating point numbers Note that in the example above NumPy auto detects the data type from the input You can explicitly specify which data type you want gt gt gt c np array 1 2 3 dbype rloat gt gt gt c dtype dtype floato4 The default data type is floating point gt gt gt a np ones 3 3 gt gt gt a dtype dtype floato4 There are also other types Complex eee gd Me ereay il 2y Ec Ono la gt gt gt d dtype dtype complex128 Bool gt gt gt e np array True False False True gt gt gt e dtype dtype l bool Strings gt gt gt i mp array UP BonJour r Helio aallon gt gt gt dtype lt SEriIngds COnNnLaining max 7 letters diype S Much more int32 int64 3 1 5 Basic visualization Now th
169. e In 14 satisfiable x amp x Out 14 False 16 4 1 Exercises 1 Solve the system of equations x y 2 2 x y 0 2 Are there boolean values x y that make x y amp y x true 16 5 Linear Algebra 16 5 1 Matrices Matrices are created as instances from the Matrix class gt gt gt from sympy import Matrix sc Mabie EIE n To 1 0 0 1 unlike a NumPy array you can also put Symbols in it gt gt gt x Svmboli x gt gt gt y Symbol y oc Ace E IIE H aL gt gt gt A 1 x y BE gt gt gt Axx 16 5 Linear Algebra 293 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 pu A Yy 2x Dos ce sese 16 5 2 Differential Equations SymPy is capable of solving some Ordinary Differential Equations sympy ode dsolve works like this Ts Poe Gx ss clie se ae er espe OEC ria 15 elsi as ME S 0LEE E A S r IE C PEE OUT T Bl Cl xsin x C2xcos x Keyword arguments can be given to this function in order 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 16 5 3 Exercises 1 Solve the Bernoulli differential equation x f x dif
170. e 01 6 66672149e 01 DRE DUO 8 88882036e 01 1000010266400 BFGS needs more function calls and gives a less precise result Note leastsq is interesting compared to BFGS only if the dimensionality of the output vector is large and larger than the number of parameters to optimize Warning If the function is linear this is a linear algebra problem and should be solved with scapy lrinalg lstsqti 13 4 2 Curve fitting 1 0 0 5 0 0 13 0 5 1 0 1 5 2 0 2 5 3 0 Least square problems occur often when fitting a non linear to data While it is possible to construct our optimization problem ourselves scipy provides a helper function for this purpose scipy optimize curve_fit gt gt gt def f t omega phi return np cos omega x t phi pow cc eounpuicbnnmepacet0 Ov 50 gt gt gt y qox Ls dy t 2 ene candom normal size 50 s ODULI mTAescurwve fu T v array Unique T 3926600949 anwey Ul 00007994 ou OO O 0000 05560 0 UE 3 OR 13 4 Special case non linear least squares 258 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise Do the same with omega 3 What is the difficulty 13 5 Optimization with constraints 13 5 1 Box bounds Box bounds correspond to limiting each of the individual parameters of the optimization Note that some problems that are not originally written as box bounds can be rewritten as such be a change of variables vd 5 e sci
171. e ee Ee Ine D c Po Ix C W E e 14 4 References ETS repositories http github com enthought 144 References 8g Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 144 References ig CHAPTER 15 3D plotting with Mayavi author Ga l Varoquaux 15 1 A simple example Warning Start ipython wthread import numpy as np x y sunmeemepeacr ors dos os xs r eIDpeSqEt xe duce 2 e E BY ae from enthought mayavi import mlab mlab surf z warp cscele auto mlLab out Lime 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 Mayavi Scene 1 x i m m rz S 280 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 15 2 3D plotting functions 15 2 1 Points import numpy as np from enthought mayavi import mlab X Y Z value mo random random 4 40 imeem ours d yy By ede lt lt enthoughte mayayvi modules glyph CGlyph co eck at 05395079959 15 2 2 Lines in 5 mlab cli In 6 t np bLrnspesce U 20 200 Xn ib sila plore sa nies s une i imo meos OT Ere Out 7 lt enthought mayavi modules surface Surface object at Oxcc3eldc gt 15
172. e Oe Onan Onl 4 ose Orge Org dee X das ex dios es Is prs OS Osp dO Waly du Wey City hee Ol gt gt gt ndimage binary dilation a astype a dtype Buda c I oov Osa 3 ag O he Oea Ot la Oleg Oal k Oaa ikea lege Chuge OL Chew s chm Diy Oy peers U U 0 SES a La A Lo A Also works for grey valued images gt gt gt np random seed 2 gt gt gt x y 634npD random random 2 9 mstype mos nt gt gt gt im x y np arange 8 gt gt gt bigger_points ndimage grey_dilation im size 5 5 structure np ones 5 5 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 dist size 3 3 structure np ones 3 3 gt gt gt a mp zeros 5 5 dtype np int gt gt gt a 1 4 1 4 1 al4 4 1 gt gt gt a arkay oO 07 07 07 0l ST O LOS O Opening erosion dilation 12 4 Image filtering 236 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Kor d de ie Ol OG 407 OD dE d gt gt gt Opening removes small objects gt gt gt ndimage binary opening a structure np ones 3 3 astype np int array lL lO 0 0 07 ss Oe al aes O RO lee Sie ole Ons Or al vee ies COMI COE FO Oe 20 OI gt gt gt Opening can also smooth corners gt gt gt ndimage binary_opening a astype np int arca iO a
173. e 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 omes o y inom Maren to Wutic gt gt gt jJames set_age 23 gt gt gt james age Zo 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 programming OOP 39 CHAPTER 3 NumPy creating and manipulating numerical data authors Emmanuelle Gouillart Didrik Pinte Ga l Var
174. e conversion gt gt gt float 1 l 0 2 2 Basic types 10 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 aoe iS quce c e gt gt gt type l lt ityoe List gt Indexing accessing individual objects contained in the list gt gt gt 1 2 2 Counting from the end with negative indices poo 5 gt gt gt be 4 Warning Indexing starts at 0 as in C notat 1 as in Fortran or Matlab Slicing obtaining sublists of regularly spaced elements gt gt gt bo ee i9 Wi eu posts D Warning Note that 1 start stop contains the elements with indices i suchas 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 soc 1132 4 9 zsm L3 Pe Ze 2 gt gt gt is TM M 2 Lists are mutable objects and can be modified gt gt gt gg 28 gt gt gt 1 Luc 27 op 47 5 gt gt gt 1 2 4 3 8 gt gt gt 1 2o cM S PN XS Note The elements of a list may have different types gt gt gt l 2 hello gt gt gt 1 lou xe eso aoe qu EA 2 hello 2 2 Basic types 11 Python Scient
175. e data are only measured over 21 years at the Sprog 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 6 11 Summary exercises on scientific computing 128 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Statistical approach The annual maxima are supposed to fit 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 i 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
176. e 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 demosgprwnmbcs 0 a Pn 9 4 demos 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 type module Strang Form module demo from demo py gt Namespace Interactive File home varoquau Projects Python talks scipy 2009 tutorial source demo py DOG St UTE s A demo module In 5 who demo In 6 whos Variable Type Data Info demo module lt modu ule demo irom T Cell aon gt In IT7T die demo Qut 7 s V b ilrings 4 D doe ee DT eae name T package ro ra CEPI rint o 2 6 Reusing code scripts and modules 27 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 In 8 demo demo builtins gt Inle a Ser demo class name c s lbeclasshook demo delattr new e demo dict package el demo doce reduce so demo file reduce ex vo geris D demo format repr SE demo getattribute Setattr TOME demo hash m JSP Zeof Importing objects from modules into the main namespace In 9 from demo i
177. e notes Release 2012 3 EuroScipy 2012 gt gt gt Classaterit amg reshape Cumg size 177 GMM gt gt gt classif means SE a ilk Ones Sikes al SOO STS HS eu TP gt gt gt np sgrt classif Covars ravel array 0 3930746910262 2392 1 gt gt gt classif weights_ arca 0 40989799 0 590102011 gt gt gt threshold np mean classif means_ gt gt gt binary img img gt threshold histogram Use mathematical morphology to clean up the result gt gt gt Remove small white 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 mask binary img gt gt gt tmp mp logical nol reconst rucrt 11mg gt gt gt eroded_tmp ndimage binary_erosion tmp gt gt gt reconstruct final np logical_not ndimage binary propagation eroded tmp mask t gt gt gt np abs mask close img mean 0 014679955079125 gt gt gt np abs mask reconstruct trinal mean 0 0042572021484375 Exercise Check how a first denoising step median filter total variation modifies the histogram and check that the resulting histogram based segme
178. e 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 asingle object can have several names bound to it In 1 a In 2 b Q re NO GO 2 3 Assignment operator 15 In 3l Our Les In 4 Out 4 In 5 Ome os In 6 In 7 eue LUE Python Scientific lecture notes Release 2012 3 EuroScipy 2012 a ils eee al IS bibe ce S a is b True Ib e T E a ENS USE e S to change a list in place use indexing slices In 1 In 31 Out he In 4 Tn 5 Our lols In 6 couce In 7 In 8 eur In 9 Quies a 1 2 3 a W a 9 a a b c Creates another object a pror le Seer mad a 138641676 al l 1 2 3 Modifies object in place a 2 o id a 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 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 fn BL Obvious if 2 x 2 4 print Obvrous Bloc
179. e up watchman object name old new pass Removing a dynamic listener can be done by calling the remove trait listener method on the trait with the listener method as argument calling the on trait change method with listener method and the keyword remove True deleting the instance that holds the listener Listeners can also be added to classes using the on trait change decorator from traits api import HasTraits Instance DelegatesTo Float Range from traits api import Property on trait change 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 noc in m3 s n m m reservoir Instance Reservoir 05 max storage DelegatesTo reservoir min_release Float max_release DelegatesTo reservoir state attributes storage Property depends_on inflows release Xx controd Bal ributes int lows Float desc Intlows Tams release Range low min_release high max_release Python Scientific lecture notes Release 2012 3 EuroScipy 2012 spillage Property desc Spillage hm3 depends_on storage inflows release Private traits FHFFETEFEEEEEEEEEEE EERE EEE EET EEE EE HEE EEE _storage Float Traits property implementation ffeeeeeeeeeeeeeeeeeeeeeeeee tt erE def _get_storage
180. e will be removed from the Fourier transformed signal gt gt gt sig fft np abs sample freq gt freq 0 The resulting filtered signal can be computed by the scipy fftpack ifft function po Mains lG fftpack arfi srg Pt The result can be viewed with gt gt gt plt figure o plbcploc uime vec Sid gt gt gt plt plot time_vec main_sig linewidth 3 gt gt gt plt xlabel Time s gt gt gt plt ylabel Amplitude Ui Il FUN l 1 f Amplitude i 10 Time s numpy fft Numpy also has an implementation of FFT numpy ft However in general the scipy one should be preferred as it uses more efficient underlying implementations 6 4 Fast Fourier transforms scipy fftpack 110 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Worked example Crude periodicity finding 80000 70000 60000 50000 40000 L 30000 20000 10000 1500 1905 1910 1915 1920 300000 250000 200000 150000 100000 50000 Ul 10 15 20 6 4 Fast Fourier transforms scipy fftpack 111 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Worked example Gaussian image blur Convolution Ties fa K t t fo t 6 4 Fast Fourier transforms scipy fftpack 112 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 ing image Exercise Denoise moon land m o RHH y
181. ease 2012 3 EuroScipy 2012 _storage Float Traits property implementation ffeeeeeeeeeeeeeeeHeeeeeee eee eH HF def _get_storage self new storage self storage self release self inflows return min new storage self max storage Set storage self storage value self storage storage value get spillage self new storage self storage self release self inflows overflow new storage self max storage return max overflow 0 print_state self print Storage tRelease tInflows tSpillage Str formae XE soi 13 feet Eor in range print str_format format self storage self release self inflows self spillage print gt 79 if name Sas main projectA Reservoir name Project A max_storage 30 max release 5 hydraulic_head 60 efficiency 0 8 ReservoirState reservoir projectA storage 25 release 4 inflows U Orine stave YCONELGUre ira Ss P a P a Edit properties Name Project A Storage 21 0 Spillage U Inflows 0 0 Release 0 0 J 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 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 turbine type Str power Float
182. eated 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 Doc ute THE ossa doa ol signature OUI o Ml fo gt gt gt x np ones 10 2 4 gt gt gt y np ones 10 4 5 2o Ub Matrix multiply x y shape LO 27 gt the last two dimensions became core dimensions and are modified as per the signature otherwise the g ufunc operates elementwise matrix multiplication this way could be useful for operating on many small matrices at once Generalized ufunc loop Matrix multiplication m n n p m p 8 2 Universal functions 180 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 void gufunc loop void xxargs int dimensions int xsteps void xdata char input 1 char args 0 these are as previously char input 2 char args 1 char output char args 2 int input l stride m steps 3 strides for the core dimensions 7 we ere cadded atter the noa core w int input 1 stride n steps 4 4 T e int input_2_strides_n steps 5 6 EE 8 int input 2 strides p steps int output strides n steps int output strides p steps int m dimension 1 core dimensions are added after int n dimension 2 the main dimension order as in int p dimension l 74 signature 47 int i fort 1 0 uc
183. ec L0 5 This works In 121 def setx y global x x y Prine xe 0 In 122 setx 10 x is 10 In 123 x Out ZS ie 10 2 5 6 Variable number of parameters Special forms of parameters 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 xargs xkwargs print args icf afgs print kwargs is kwargs In 36 variable args one two x 1 y 2 z 3 args G one wot kwargo bs dq 197 5 1 05 aoe s shes ONE 2 5 7 Docstrings Documentation about what the function does and it s parameters General convention In 67 def funcname params Concise one line sentence describing the function Extended summary which can contain multiple paragraphs function body pass In 68 funcname Type function Base Class CyPo func Cion gt Scie Form function funcname at Oxeaa0f0 gt Namespace Interactive File PRE Coumms src scipy20097 python console Definition funcname params Does EE Concise one line sentence describing the function Extended summary which can contain multiple paragraphs Note Docstring guidelines 2 5 Defining functions 23 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 For the sake of standardization the Docstring Conventions webpage documents the semantics and conventions associated with Python docstrings Also
184. ello world 6 3rd to 6th excluded elements elements 3 4 5 10 21 ut Syntax als arto op Seep 3 every three characters from beginning to end 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 a 2 Tvpel tror Traceback most recent call 2 2 Basic types 13 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 lastr home gouillar travail sgr 2009 talks dakar python cours gael essai source ipython console in lt module gt TypeError str object does not support item assignment In Tool a mep laces dq LS uis Owe lssi fherzlo world In 56 a replace 1 727 Qut SC T wortzdl 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 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 poo An in
185. emory char data Data type descriptor PyArray Descr xdescr Indexing scheme int nd npy intpp dimensioms npy intp xstrides Zu Other Stuff 7 PyObject xbase int flags PyObject xweakreflist PyArrayOb ject 8 1 2 Block of memory gt gt gt x NOvarray lly 2 3 2l dtypesnp rnt32 gt gt gt x data lt read write buffer for size 16 offset 0 at gt gt gt str x data PASSO T OOA 00 cO voz x00 100 DD OO x00 x00 909 sod 00x00 200 Memory address of the data gt gt gt Xa iray c nberrdgoe data ro 64803824 Reminder two ndarrays may share the same memory PIZA gt gt gt y np frombuffer x dtype np int8 gt gt gt y data lt read only buffer for Size 4 offset 0 art 8 1 Life of ndarray 161 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt y base is x True porc y i lags CSCONTIGUOUS True E CONTIGUOUS 3 True OWNDATAT False WRITEABLE False ACTCNED True UE DS iC OF 2 Baloe The owndata and writeable flags indicate status of the memory block 8 1 3 Data types The 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 size of the data block byte order big endian gt little endian lt not applicable sub dtypes if it s a structured data type shape of the array if it s a
186. ential page 251 Quasi Newton methods approximating the Hessian on the fly page 253 Gradient less methods page 253 A shooting method the Powell algorithm page 253 Simplex method the Nelder Mead page 254 Global optimizers page 255 Brute force a grid search page 255 Simulated annealing page 255 Practical guide to optimization with scipy page 255 Choosing a method page 255 Making your optimizer faster page 256 Computing gradients page 256 Synthetic exercices page 257 Special case non linear least squares page 257 Minimizing the norm of a vector function page 257 Curve fitting page 258 e Optimization with constraints page 259 Box bounds page 259 General constraints page 259 13 1 Knowing your problem Not all optimization problems are equal Knowing your problem enables you to choose the right tool Dimensionality of the problem The scale of an optimization problem is pretty much set by the dimensionality of the problem i e the number of scalar variables on which the search is performed 13 1 Knowing your problem 246 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 13 1 1 Convex versus non convex optimization A convex function A non convex function f is above all its tangents equivalently for two point A B f C lies below the segment f A f B if A C B Optimizing convex functions is easy Optimiz
187. entific lecture notes Release 2012 3 EuroScipy 2012 In 9 ceimeit np dot bD C lO beoops best Or 3 94 2 ms per Loop Note that copying the data to work around this effect may not be worth it In 10 timert c np ascontiguousarray a 7 10 loops best of 3 106 ms per loop Using numexpr can be useful to automatically 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 206 CHAPTER 11 Sparse Matrices in SciPy author Robert Cimrman 11 1 Introduction dense matrix is mathematical object 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 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 imp
188. er 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 2 6 Reusing code scripts and modules 24 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 py Write or copy and paste the following lines in a file called test py message Hello how are you for word in message split print word Let us now execute the script interactively that is inside the python interpreter This is maybe the most common use of scripts in scientific computing in Ipython the syntax to execute a script is run script py For example In 1 chum vest spy 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 p
189. er sca cds c cia E 1 LO262 102882 0 oP pea Mo Cort lul 10 lt 1 Gara 10 1 7799 7799 0 Du 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 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 stwmnevs np linalg svd data l loops best of 3 14 5 s per loop In 4 from scipy import linalg In 5 lt timeit buxnabosswdidauvsa l loops best of 32 1442s per loop In 6 Stimeit linalg svd data
190. erand type s for int in 3 DES uu BS ABI qo hs Pode ok IndexError list index out of range in 7 l ftoobar AvclrTDULeErrPrOPS list object has no attribute foobar Different types of exceptions for different errors 2 9 2 Catching exceptions try except while True x int raw_input Please enter a number break except ValueError print Ihat was no valid number Try again Please enter a number a That was no valid number Try again Please enter a number 1 In 9 x Ow 1 try finally int raw_input Please enter a number finally print Ihank you for your input Please enter a number a Thank yon for your nput 2 9 Exception handling in Python 37 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 ValueRrrori invalic literal tor int with base 10 Important for resource management e g closing a file Easier to ask for forgiveness than for permission In I1 def print sorted collection try collection sorri except AttributeError pass print collection In t2 Prime sorted Ile 09 219 bp 27 2 In 13 print sorted sery le 37 277 See ik 47 ol In i4 prirntc sorted 132 132 2 9 3 Raising exceptions e Capturing and reraising an exception In 15 def filter name name Lond try UNIT name name encode ascii TE except UnicodeError e bau if name Ga l
191. erimenting 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 kon E a data 100 aree s fee incre lt BEH KNeighborsClassifier Boo keri Sscore irio daca lV002 suawssbardgebvpJ e Ono aoe Bonus question why did we use a random permutation 17 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 17 2 Classification 298 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt from sklearn import svm gt gt gt svc svm SVC kernel linear pou SVC ie D Seddi ey dS varger SVC 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 da
192. ers of the underlying distribution Here we fit a normal process to the observed data gt gt gt loc std stats norm fit a 2 OC 0452 56 7 d cis gt gt gt std Oe De 03 S S66 Oe Exercise Probability distributions Generate 1000 random variates from a gamma distribution with a shape parameter of 1 then plot a histogram from those samples Can you plot the pdf on top it should match Extra the distributions have a number of useful methods Explore them by reading the docstring or by using Python tab completion Can you find the shape parameter of 1 back by using the it method on your random variates 6 6 2 Percentiles The median is the value with half of the observations below and half above gt gt gt np median a O 5307 R 4n ar 6 6 Statistics and random numbers scipy stats 118 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 It is also called the percentile 50 because 50 of the observation are below it gt gt gt stats scoreatpercentile a 50 iO similarly we can calculate the percentile 90 gt gt gt stats scoreatpercentile a 90 I M The percentile is an estimator of the CDF cumulative distribution function 6 6 3 Statistical tests A statistical test is a decision indicator For instance if we have two sets of observations that we assume are gen erated from Gaussian processes we can use a T test to decide whether the two sets of observations are
193. essed Row Format BSR 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 19 array of column indices for each block data is array of corresponding nonzero values of shape nnz R C subclass of cs matrix common CSR CSC functionality subclass of data matrix sparse matrix classes with data attribute fast matrix vector products and other arithmetics sparsetools constructor accepts dense matrix array sparse matrix 11 2 Storage Schemes 218 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 create empty BSR matrix with 1 1 block size like CSR gt gt gt mtx sparse bsr matrix 3 4 dtype np int8 gt gt gt mtx 2x4 Sparse Matrix of type stvpe numpy ince gt with 0 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt mtx todense iene a sec Oe 07 07 0 BOs Oy tO Ol 0 0 O 0 dtype int89 create empty BSR matrix with 3 2 block size gt gt gt MEX sparse bsr matrix 3 4 bl
194. ext instance ut xw poo pl t itle Measured ve Calculated ELLIPSIS smatplotlasb text Text instance at 252 gt gt gt pl grid True We get a reference to our plot gt gt gt my plot pl gca and to our line we plotted which is the first in the plot gt gt gt line my plot lines 0 Now we can set properties using set something methods 5 3 Simple Plots 88 gt gt gt line set marker o or the setp function gt gt gt setp line color g None To apply the new properties we need to redraw the screen gt gt gt draw We can also add several lines to one plot gt gt gt x Novarange 100 gt gt gt linear np arange 100 gt gt gt square woos ow fOr v n npearange 0 2100 071 gt gt gt lines pl plot x linear x square Let s add a legend gt gt gt pl legend linear square lt matplotlib legend Legend instance at This does not look particularly nice We would rather like to have it at the left So we clean the old graph clf stands for clear figure gt gt gt pl clf 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 lines piot x lrnest uit x Square 0 Now we add the legend at the upper left corner gt gt gt 1 legend linear square loc upper left The result
195. ey allow comparing various algorithms easily through the method keyword You can find algorithms with the same functionalities for multi dimensional problems in scipy optimize Exercise Curve fitting of temperature data The temperature extremes in Alaska for each month starting in January are given by in degrees Celcius mare dre NE RET DAE CODES TR TA crue EIE imc xod MeL ee deu o EET Eu eke 5257 ceo exu esi Plot these temperature extremes 2 Define a function that can describe min and max temperatures Hint this function has to have a period of 1 year Hint include a time offset Fit this function to the data with scipy optimize curve fit Plot the result Is the fit reasonable If not why Is the time offset for min and max temperatures the same within the fit accuracy 6 5 Optimization and fit scipy optimize 116 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise 2 D minimization Six hump Camelback function The six hump camelback function 4 a f x y 4 212 ze zy Ay Ay has multiple global and local minima Find the global minima of this function Hints e Variables can be restricted to 2 lt x lt 2and 1 lt y lt 1 e Usenumpy meshgrid andplt imshow to find visually the regions e Usescipy optimize fmin bfgs or another multi dimensional minimizer How many global minima are there and what is the function value at those points What happens fo
196. f intervals and triangles to high dimensional spaces to bracket the minimum Strong points it is robust to noise as it does not rely on computing gradients Thus it can work on functions that are not locally smooth such as experimental data points as long as they display a large scale bell shape behavior However it is slower than gradient based methods on smooth non noisy functions iterations function calls Error on f x e ejo 50 100 150 200 250 An ill conditionned non quadratic function iterations function calls Error on f x e PC 150 200 250 An ill conditionned very non quadratic function In scipy scipy optimize fmin implements the Nelder Mead approach gt gt gt def f x The rosenbrock function ae returm 5 wie e xL nO 2 e2 Pow ODESmuze Lmarmet Ee 2 Optimization terminated successfully Current funcrtvon value 0 000000 Iterations 46 puncectron cevaludstqonse 9T eabray 02999985685 0 99996682 13 2 A review of the different optimizers 254 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 13 2 5 Global optimizers If your problem does not admit a unique local minimum which can be hard to test unless the function is convex and you do not have prior information to initialize the optimization close to the solution you may need a global optimizer Brute force a grid search scipy optimize brute evaluates the function on a given g
197. f 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 16 5 Linear Algebra 294 CHAPTER 17 scikit learn machine learning in Python author Fabian Pedregosa Gael Varoquaux machine learning in Python Prerequisites e Numpy Scipy e Python e matplotlib e scikit learn http scikit learn org Chapters contents Loading an example dataset page 296 Learning and Predicting page 297 Classification page 297 k Nearest neighbors classifier page 297 Support vector machines SVMs for classification page 298 Clustering grouping observations together page 300 K means clustering page 300 Dimension Reduction with Principal Component Analysis page 301 Putting it all together face recognition page 302 Linear model from regression to sparsity page 303 Sparse models page 303 Model selection choosing estimators and their parameters page 304 Grid search and cross validated estimators page 304 295 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 17 1 Loading an example dataset 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
198. fault a newline 1 3 2 Elaboration of the algorithm in an editor Create a file my file py in a text editor Under EPD Enthought Python Distribution 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 we 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 orun my tile py Folio wore In 4 s Our lA Het ito word In 5 whos Variable Type Data Info IS SIEG Hello word 1 3 The interactive workflow IPython and a text editor 6 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 From a script to functions While it is tempting to work only with scripts that is a file full of instructions following each other do plan to progressively evolve the script to a set of functions A script is not reusable functions are 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 necessar
199. fering an object API and several additional features including smart initial ization gt gt gt from sklearn import cluster datasets gt gt gt iris datasets load rris gt gt gt k_means cluster KMeans k 3 gt gt gt k_means fit iris data KMeans copy x True init k meanst k 3 gt gt gt print k_means labels_ 10 ale wake ale i Se ee E OOO O E ee EO NONI gt gt gt Prine ets R L 50 POr O SOS DOSE SIE ele ay ale i os ee e D et A o o SES seme irginica Mr A Versicolour Petal length Petal length Petal length Ground truth K means 3 clusters K means 8 clusters 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 from scipy import misc gt gt gt lena misc lena astype np float32 gt gt gt lena reshape 1 1 We need an n sample n feature array gt gt gt k means cluster KMeans n clusters 5 gt gt gt k means fit X KMeans 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 Raw image K means quantization 17 3 Clustering grouping observ
200. fic lecture notes Release 2012 3 EuroScipy 2012 5 9 2 The Classes Axes and Subplot 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 5 9 3 Other Classes Other classes such as text Legend or Ticker are setup very similarly They can be understood mostly by comparing to the py lab interface 5 9 4 Example Let s look at an example for using the object oriented API file matplotdsvT5 005 Dy from matplotlib figure import Figure 1 figsize 8 5 2 fig Figure figsize figsize 3 ax fig add subplot T 4 line ax plot range 10 0 5 ax ser cele Pli tec vich OO anwerrace 6 ax set_xlabel measured ax set_ylabel calculated ax grid True 7 line Sere marken CoO 8 from matplotlib backends backend_agg import FigureCanvasAgg 9 canvas FigureCanvasAgg fig 10 Calves Prilnp fig re oso t spi 11 import Tkinter as Tk 12 from matplotlib backends backend_tkagg import FigureCanvasTkAgg 13 TOOR eed 14 canvas2 FigureCanvasTkAgg fig master root 15 canvas2 show 16 canvas2 get tk
201. g Cryptic error messages gt gt gt Np random permutacion 12 array Uem dob cl IU 2 ihe ty CER Se 2 OR Sil poo MO random permuceac wom 2 Meaceback most recent call ast File lt stdin gt line 1 in lt module gt File Tmtrand pyx line 23311 in mtrand Randomstate permutation File mtrangspysx Line 3254 if mtrand Randomotate lt shuff le TypeError 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 Nunmpy 124 1 buile from ene oficial tarball om Windows 64 with visual ctudia 0007 on Pyemon org osse Python 0 What are you trying to do 1 Small code snippet reproducing the bug if possible What actually happens What you d expect 2 Platform Windows Linux OSX 32 64 bits x86 PPC 3 Version of Numpy Scipy gt gt print Mp version 2 Check that the following is what you expect gt gt gt print Op Tile oe In case you have old broken Numpy installations lying around If unsure try to remove existing Numpy installations and reinstall 8 6 3 Contributing to documentation 1 Documentation editor s http docs scipy org numpy Registration Register an account Subscribe to scipy dev mailing list subscribers only 8
202. g a limited cache of argu ments answer pairs Python 3 2 e functools total_ordering is a class decorator which fills in missing ordering methods __1t__ gt le based on a single available one Python 2 7 9 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 WEN Print a deprecation warning once on I2rst use or Lhe runction gt gt gt deprecated doctest SKIP def f EE pass Se ary docbtestt SKIP f is deprecated def Gall self func self func func self count 0 return self _wrapper def _wrapper self xargs kwargs self count t 1 if self count print self func name is deprecated return self func xargs xxkwargs It can also be implemented as a function def deprecated func VEEP PLUG a deprecation warning ODCO On Tirst uso of ele funct10nD gt gt gt deprecated 7 doc ebli FoIP def f S pass ps E f doctest SKIP f is deprecated count 0 def wrapper xargs x xkwargs count 0 1 1f count 0 1 print func name is deprecated return func xargs xxkwargs return wrapper 7 2 Decorators 154 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 7 2 6 A while loop removing decorator Let s say we have function which retur
203. ge 74 Under the hood the memory layout of a numpy array page 75 3 3 1 More data types Casting Bigger type wins in mixed type operations gt gt gt wo acca 2 gt r 1 5 array oy oy A 9 Assignment never changes the type gt gt gt a np array ll 2 31 gt gt gt a dtype dtype int64 gt gt gt a 0 1 9 lt float is truncated to integer gt gt gt a array ity 2 31 Forced casts so og e T h a lls p bez de gt gt gt b a astype int lt truncates to integer gt gt gt b array ely e cu Rounding DO a me peor ay Ma 555 Joe sy x30 Sc gt gt gt b np around a gt gt gt b 5 LII Tloating Point array l lag oe age Cay Gay Gail gt gt gt C NOvaround a sastype ine gt gt gt C apray L dE 2p Ay 41 Different data type sizes Integers signed 32 bits same as int on 32 bit platform 64 bits same as int on 64 bit platform gt gt gt np array 1 dtype int dtype diype imte4 gt gt gt Dp AIO ip eines eMax Beto te ob 2147483647 2147483647 gt gt gt np iinfo np int64 max 2 603 1 22283 T A20 99992 To 0 SE S3 203868047 15607 i Unsigned integers 3 3 More elaborate arrays 72 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 BOO T E qns o DICIT A Nico Ze k 42949672957 4294967295 2o eh iinfo np Uint H max od c 1 18446744073709551615L 184467440737095
204. graph data std 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 256 gt gt gt im Np zeros 1 17 gt gt gt points l np random random 2 n3s 2 12 6 Measuring objects properties ndimage measurements 240 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt Am pointslOl astype np int l Xposmesiil sesuvbpetuUnpstne e gt gt gt im ndimage gaussian_filter im sigma 1 4 n gt gt gt mask im gt im mean Analysis of connected components Label connected components ndimage label gt gt gt label_im nb_labels ndimage label mask gt gt gt nb_labels how many regions PES gt gt gt plt imshow label im matplotlib image AxesImage object at gt 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 shape 25167 256 gt gt gt label_im remove_pixel 0 gt gt gt plt imshow label_im lt matplotlib image AxesImage o
205. gt data R a MIL Lie Bd LL s 25 T4 Ee 45 e cdd bsec eem 4 4 lS de y Sy 6 6 ov 61d C C IA L SR ae 11 2 Storage Schemes itera has data array specialized ie fe itera arithmetics via CSR itera fancy solvers note set itera tive O 1 item access incremental construction has data array facilitates fast conversion any has data array fast row wise ops has data array fast column wise ops has data array specialized 220 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 11 3 Linear System Solvers sparse matrix eigenvalue problem solvers live in scipy sparse linalg 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 accessible from gt gt gt import scipy sparse linalg as spla gt gt gt Sola all LnearOperator j Tester arpack aslinearoperator bvcg R 4 eg 4 COs CSc matr e v Cor matri a T e Ote eigen eigen symmetric factorized gmres interface solys upepdgpdve a 7 lLomces X3umsolbwe T Polbpog J9cu amp o ou gam ga TE aT ST a spa s spi a spec lye fd test umf pack use solver utils p warnings 11 3 1 Sparse Direct Solvers default solver SuperLU 4 0 included in SciPy real and complex systems both
206. gt def replacing decorator with args arg print defining the decorator def decorator function in this inner function arg is available too Prine doing decoracion ard def _wrapper xargs xkKwargs print inside wrapper args kwargs return function xargs xxkwargs return wrapper return decorator gt gt gt replacing_decorator_with_args abc def function args kwargs print inside function y args kwargs T return 14 defining the decorator doing decoration abc sso EVNET BT T L 12 inside wrapper 11 127 1 Lases runccion T 12 4a 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
207. gt gt gt next gen middle 4 gt gt gt next gen Finished Traceback most recent call last GLtoprIteratron 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 next 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 me
208. gt gt plt imshow plt imread tiny elephant png interpolation nearest lt matplotlib image AxesImage object at gt 34 Advanced operations 0 8 amp 8 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 s Ae HET FL 10 20 30 40 Numpy s own format Numpy has its own binary format not portable but with efficient I O gt gt gt data np ones 3 3 gt gt gt np save pop npy data gt gt gt dabas npeload pop npv Well known amp more obscure file formats s 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 Exercise 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 34 Advanced operations 8 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 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 2
209. gt t 12345 7 54921 hello 223g 0 2 e Sets unordered unique items Sos RE Seta T oy a gt gt gt G seeli a p Cl cfe 1 gt gt Soditrerence Seber n a AHN A bag of Ipython tricks Several Linux shell commands work in Ipython such as 1s pwd cd etc To get help about objects functions etc type help object Just type help to get started 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 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 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 slegstart commands log Activating auto logging Current Session state plus future input saved Filename 1 commands log Mode PACKUP Output Jogging s 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 mutabl
210. hape W n 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 scue tuncrron tnpodrkray tLlb 27 211 print ome function nee M ES 4 2 ue ow Ls Ws 255 b uL dud 3 2 Numerical operations on arrays 63 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Case 2 b Block matrices and vectors and tensors Vector space quantum level amp spin P led tal Y Wo V1 Uil We wey 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 psu lO E 7 lt psl 41 downarrow Oa 0 Linear operators on such block vectors have similar block structure hi V HJ n e N gt gt gt H np zeros 2 2 2 1 7 dimensions levell Jevelz spini spinz gt gt gt M S HO R ess 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 v v Hw gt gt gt def mdot operator psi return operator transpose 0 2 1 3 reshape 4 4 dot psi reshape 4 reshape 2 2 Le reorder dimensions first to levell spinl level2 spin2 and then reshape gt correct matrix product 3 2 5 Sorting data Sorting along an axis soo a Mp array i 47 38 5 dev xb gt gt gt b
211. hat 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 the 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 poo der f yield 1 yield 2 gt gt gt SEN generator object t st Oxea gt gt gt gen gt gt gt gen next 1 7 1 Iterators generator expressions and generators 145 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt gen next 2 gt gt gt gen next Traceback most recent call last prio Vasrdine line d in module StoplIteration Let s go over the life of the single invocation of the generator function gt gt gt def f Print T R yield 3 print middle yield 4 print finished gt gt gt gen f gt gt gt next gen Seat 3
212. header dtype fields format ci ype sd o The first element is the sub dtype in the structured data corresponding to the name format The second one is its offset in bytes from the beginning of the item Exercise 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 b formats list of dtypes for each of the fields and use that to read the sample rate and data id as sub array gt gt gt f open data test wav r gt gt gt wav header np fromfile f dtype wav header dtype count 1 gt gt gt f close gt gt gt print wav header Le C RIRE PA qr WAVE Serie d e lob 1 1 COCO E DIDI 27 16 Ae H Iig 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 ML alge ac Loto qus dtype S1 gt gt gt wav header shape 1 gt gt gt wav header data id shape or za 229 When accessing sub arrays the dimensions get added to the end 8 1 Life of ndarray 163 Note There are existing modules such as wavfile audiolab etc for loading sound data Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Casting and re interpretation views casting on assignment on array constru
213. hod 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 S HEL for i an tertools counc s print yieloing 1 1 try ans yield i except GeneratorExit print ciosiug e raise except Exception as e print ysold raised r e else print yield returned s ans gt gt gt it g gt gt gt next it TIE IE yielding 0 0 gt gt gt it Sendi CMH yield returned 11 Vielding J gt gt gt it throw IndexError yield raised IndexError cuv ie ldi ng 2 E TET Los ing 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 continue to take 7 1 Iterators generator expressions and generators 147 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 an argument which will be passed to send of the loop s iterator If
214. hould 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 he 110 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 gt gt gt 1 1 2 gt gt gt 2 4 Floats 2 1 First steps 9 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt Co Ziel Complex Booleans gt gt gt 3 5 False gt gt gt test 3 gt 4 gt gt gt Lest False gt gt gt type test Eype bool A Python shell can therefore replace your pocket calculator with the basic arithmetic operations x modulo natively implemented Do quet CEN gt gt gt 2xx10 WORA gt gt gt 8 2 2 Warning Integer division gt gt gt 3 2 AL Trick use floats e Scalar types int float complex bool gt gt gt type 1 dtype ani gt gt gt gt type l type float gt gt gt ype s 0J type complex gt P a eC gt gt gt type a type int gt Typ
215. ientific lecture notes Release 2012 3 EuroScipy 2012 Exercise Array manipulations 1 Form the 2 D array without typing it in explicitly T1 12 Hs di SO spel OUR IES and generate a new array containing its 2nd and 4th rows 2 Divide each column of the array gt gt gt a np arange 25 reshape 5 5 elementwise with the array b np array 1 5 10 15 20 Hint np newaxis 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 Use abs and argsort to find the column 7 closest for each row Use fancy indexing to extract the numbers Hint a i j the array i must contain the row numbers corresponding to stuff in j 3 2 Numerical operations on arrays 67 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise 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 loadixi data populations txt gt gt gt year hares lynxes carrots data T trick columns to variables wo Ole axess 10 2 Odor 0 57 Uus lt matplotlib axes Axes object at gt gt gt gt plt plot year hares year lynxes year carrots p mat plot lib lines Line D bqeck b siop 44 4 gt gt gt plr legend Hare Lynx Carrer j locs 1 0397 0457 lt matplotlib legend Legend object at gt 80000 70000
216. ific lecture notes Release 2012 3 EuroScipy 2012 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 px ud m cuc cur ados Jol gt gt gt l append 60 gt gt gt 1 Len We Sys eui o D gt gt gt l pop 6 gt gt gt 1 so fae c E Ue gt gt gt l extend 6 71 extend l gt gt gt 1 Ln Te sous AC eee ae ofall gt gt gt HI gt gt gt 1 ET A A US Reverse 1 gt gt gt r prr gt gt gt r oy 4 y Ar ds Concatenate and repeat lists Doc cp Gb l Sue wb See vue ie iz op Se EY P M 2 Ea uus ders E ble aO wc eZ Fi Sort r in place gt gt gt qpoccd c gt gt gt r ile eee ey pr ais in place Note Methods and Object Oriented Programming The notation r method r sort Note Discovering methods In IPython tab completion press tab In 28 rt ilc ie adda La Class Co imul em Du COnEains
217. ig rue Que qq 39 cos x cos y ssim ses VA 16 2 2 Simplify Use simplify if you would like to transform an expression into a simpler form In 19 simplify x x y x Out Sos 16 2 Algebraic manipulations 290 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 16 2 3 Exercises 1 Calculate the expanded form of x y 2 Simplify the trigonometric expression sin x cos x 16 3 Calculus 16 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 sc ine Ce ity xe r0 E you can also calculate the limit at infinity Doe xmas sep of OO OO gt gt gt limit 1 x 00 0 gt gt gt limit x x x 0 al 16 3 2 Differentiation You can differentiate any SymPy expression using diff func var Examples soc sii Six oe Coe x gt gt gt Gift sini 22x 2 x 2eCOS 2 x gt gt gt Giff tan x x 1 tan x 2 You can check that it is correct by gt gt gt limit tan xty tan x y y 0 a eam Higher derivatives can be calculated using the diff func var n method o gt E sme v s 1 2eCOs x s gt s diri Sin 23
218. imshow to display an image inside amatplotlib figure gt gt gt scipy lena gt gt gt import matplotlib pyplot as plt 23 gt Dit amshow CL cmap plt cm gray matplotlib image AxesImage object at O0Ox3c7 710 gt Increase contrast by setting min and max values gt gt gt plt imshow l cmap plt cm gray vmin 30 vmax 200 matplotlib image AxesImage object at 0Ox33ef750 gt gt gt Remove axes and ticks oc pibus QT Dus ds ls up Draw contour lines mono dtocontenet i Wee 5225 Smatplobliboconeogurcconbtousset Instance au UxssieeZ0 For fine inspection of intensity variations use interpolation nearest so plit amshow 200 2207 400270 7 cmap Pli cm gray gt gt gt Dit vamshow 2003220 200 220 cmap plt cm gray Interpolation Nearest Other packages sometimes use graphical toolkits for visualization GTK Qt gt gt gt import scikits image io as im_io zo LOSS Plugin gek y ums gt gt gt im io imshow 1 12 2 Displaying images 229 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 100 200 300 400 12 2 Displaying images 230 3 D visualization Mayavi See 3D plotting with Mayavi page 280 and Volumetric data page 283 mage plane widgets sosurfaces 12 3 Basic manipulations Images are arrays use the whole numpy machinery gt gt gt lena scipy lena gt gt gt lena 0 40 166 gt gt gt
219. ing non convex functions can be very hard Note A convex function provably has only one minimum no local minimums 13 1 2 Smooth and non smooth problems A smooth function A non smooth function The gradient is defined everywhere and is a continuous function Optimizing smooth functions is easier 13 1 Knowing your problem 247 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 13 1 3 Noisy versus exact cost functions Noisy blue and non noisy green functions Noisy gradients Many optimization methods rely on gradients of the objective function If the gradient function is not given they are computed numerically which induces errors In such situation even if the objective function is not noisy 13 1 4 Constraints Optimizations under constraints Here lt x lt 1 lt Ta lt 1 13 2 A review of the different optimizers 13 2 1 Getting started 1D optimization Use scipy optimize brent to minimize ID functions It combines a bracketing strategy with a parabolic approximation 13 2 A review of the different optimizers 248 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Brent s method on a quadratic function it converges in 3 iterations as the quadratic approximation is then exact Brent s method on a non convex function note that the fact that the optimizer avoided the local minimum is a matter of luck
220. ipy 2012 alpha transparency on 0 1 scale matplotlib color arg set the font family eg sans serif cursive fantasy the font slant one of normal italic oblique left right or center left right or center only for multiline strings font name eg Sans Courier Helvetica position x y location Exercise Property Value C 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 x1abel ylabel andtitle There are two functions to put text at a defined position text adds the text with data coordinates gt gt gt Pl plore me arrange 10 gt gt gt tl piltextio 5 Text in the middle figtext uses figure coordinates form 0 to 1 ao qp pligte 0 0 029 Upper right Cest xt in the middle JOIE ala matplotlib supports TeX mathematical expression So r pis will show up as T If you want to get more control over where the text goes you use annotations 5 5 Text 92 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt ax pl gca gt gt gt ax annotate Here is something special xy 1 1 maEploblib bexc Anmotatuaon instance ab sse 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 te
221. 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 assigning 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 fu nction e pass EUNCE Lom decor tor ECL vom 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 Rep
222. jugate gradient are named with cg in scipy The simple conjugate gradient method to minimize a function is scipy optimize fmin cg Error on f x An ill conditionned very non quadratic function gt gt gt def f x The rosenbrock function return 2521 Seo oll sO RE uA a gt gt QOpsmasce tnmgneeg crx up 21 Optimization terminated successfully Current function value 0 000000 iterations 1 gt Em ome oue Lome b Gradient evaluations 30 arrevop 09999809697 0999978551 These methods need the gradient of the function They can compute it but will perform better if you can pass them the gradient gt gt gt def fprime x return npr array C 2A T DIRI RNR E TOT etse T m epo 2 gt gt gt optimize fmin_cg f 2 2 fprime fprime Optimization terminated successfully Current functvon value 0000000 Iterations 13 punctrohn evaluations 30 Gradient evaluations 30 array lL 0 99999199 0 99997536 Note that the function has only been evaluated 30 times compared to 120 without the gradient 13 2 3 Newton and quasi newton methods Newton methods using the Hessian 2nd differential Newton methods use a local quadratic approximation to compute the jump direction For this purpose they rely on the 2 first derivative of the function the gradient and the Hessian 13 2 A review of the different optimizers 251 Python Scientific lecture notes Release 2012 3 EuroScipy 2012
223. ks 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 in 3 elif a print 2 else 2 4 Control Flow TE 10 if a print 1 print A lot 16 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 xu Ud 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 gt gt gt for i in range 4 print i But most often it is more readable to iterate over values gt gt gt for word in cool powerful readable prine Python 15 95 word Python ia cool Python is powerful Python is readable 2 4 3 while break continue Typical C style while loop Mandelbrot problem gt gt gt z 1 1j gt gt gt while abs z lt 100 Aa 2 cto r d gt gt gt Z 15203523 More advanced features break out of enclosing for while loop gt gt gt 7 1 1J gt gt gt while abs z lt 100 if z imag break Z z x2 1 continue the next iteration of
224. l 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 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 Qdecorator e def function 0 pass e A function is defined in the standard way 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 148 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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
225. lab surf image Our 61 lt enthought mayavi modules surface Surface object at x In 62 mlab axes Out 62 enthought mayavi modules axes Axes object at gt The mayavi mlab window that opens is interactive by clicking on the left mouse button you can rotate the image 3 1 The numpy array object 46 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 zoom with the mouse wheel etc For more information on Mayavi http github enthought com mayavi mayavi See Also More in the Mayavi chapter page 280 3 1 6 Indexing and slicing The items of an array can be accessed and assigned to the same way as other Python sequences e g lists gt gt gt a np arange 10 gt gt gt a array lOr d 2 Oo 4 57 Ge lt 1 Sy 91 scc Olly ale sla Oy 27 9 Warning Indices begin at 0 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 3 gt gt gt a array 7o 0 01 gu dis Oth Dis Or al oec scr DIEI I gt gt gt alZ ll 10 7 third line second column gt gt gt a arbay 0 OF Ol or e Ol BOG dO 2A gt gt gt all array 10 1 307 Note that n 2D the first dimension corresponds to rows the second to columns e for multidimensional a a O is interpreted by taking all elements in the unspecified dimensions
226. lacing 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 and 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 it 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
227. lain Python interpreter etc 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 Sosmilomy enclose python Est Heo how anme your Standalone scripts may also take command line arguments In file py import sys print sys argv D python Iile py tesil arguments Arile py e test o arguments l Note Don t implement option parsing yourself Use modules such as optparse 2 6 2 Importing objects from modules 2 6 Reusing code scripts and modules 25 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 In 1 import os in L s Out 2 module os from usz Lib python2 6 los eyd gt In s os lrsedir 7 Out 3 lt Meonk ao t pasic types rot oombrol riow LEST Yume ba ost Coychon language rst ta eis qug Sb CPi ee Se y exceptions rst morke ow eSti index rst And also In 4 from os import listdir Importing shorthands In 5 import numpy as np Warning from os import Do not do it e Makes the code harder to read and understand where do symbols come from 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 completio
228. 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 import numpy as np gt 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 wavetorm 1 poco sposta deo gt gt gt plt show 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 11 Summary exercises on scientific computing 133 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Intensity bins 0 10 20 30 AQ 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
229. last dimensions vary fastest smaller strides F first dimensions vary fastest shape dj ds dn strides 51 2 Sn se 3410 19 05 x itemsize s did d 1 x itemsize Slicing e Everything can be represented by changing only shape strides and possibly adjusting the data pointer Never makes copies of the data 2 x Mpaarray IL 2r or Ar Oy Gly dtype np int2 59 we x iss gt gt gt y array lO 5 47 32 2 Wie scltspesumts2 gt gt gt y strides 4 gt gt gt y x 2 gt gt gt wu er ray Aneerrace data IOl x array interidca daca ITO 8 gt gt gt x np zeros 10 10 10 dtype np float gt gt gt o traces 8007 29 0 8 z N ee eS Ii M IT L600 240 32 e Similarly transposes never make copies it just swaps strides x np zeros 10 10 10 dtype np tloat gt gt gt x strides S007 SO 3 gt gt gt xI strides 37 9 0 S00 Reshaping But not all reshaping operations can be represented by playing with strides z 3g a a a a a T T a gt gt gt b a T gt gt gt b strides 172 3 3 More elaborate arrays 77 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 So far so good However gt gt gt str a data de QUID E O12 02 o 012 05 gt gt gt b array MOr 2 4X Ll 24 Suse they pe T mice gt gt gt c b reshape 3 2 gt gt gt C array l n 2 4 Jy 3 5 dtype
230. lib sourceforge net e Mayavi 3 D visualization http code enthought com projects mayavi 1 2 Scientific Python building blocks 5 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 1 3 The interactive workflow IPython and a text editor Interactive work to test and understand algorithms In this section we describe an interactive workflow with IPython that is handy to explore and understand algorithms Python is a general purpose language As such there is not one blessed environment to work in 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 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 ISE denm DT OE LONN or method Base Class type bucdcgsm t nctron or cmechod Set EC OE lt bu uilt in CUNCE LOM pul Namespace Python utm DOCS ring print value sep end n file 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 de
231. licing due to being row based use when sparsity pattern is not known apriori or changes example reading a sparse matrix from a text file Examples create an empty LIL matrix gt gt gt mtx sparse lil matrix 4 75 prepare random data gt gt gt from numpy random import rand gt gt gt data np round rand 2 3 gt gt gt data arbe ob Loy os IERS cue Ose E e assign the data using fancy indexing 11 2 Storage Schemes 212 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Soe queo a bab 25 D wea gt gt gt mtx 4x5 sparse matrix of type type numpy float64 gt with 5 stored elements in Linked List format gt gt gt print mtx 0 E IO COs 22 dT d COTS WO CI IRO ds 39 IO gt gt gt mtx todense Ineredogecq 0s lea LRF E ori ORT NR O E OR gs De DN hu Sues EOT Ory Ue Uem rus gt gt gt mtx toarray arrar M 0s TRN TES eine Osa MO Iei Ds wc TE ET Oty Wiery S L eee 0 OF OR Ore ee eo more slicing and indexing poor me Sparse idl mak repeti is 2h 2 Oily iw O bs SO ile 905 0 a gt gt gt MEX Lodense EEE cn she IAEI DU Olp Oye Oy aes al gt gt gt print mtx Oa il CON 2 2 eTa 3 1155 2 1 2p ae il 27 2 1 gt gt gt mts 2 2x4 sparse matrix of type type numpy into4 with 4 stored elements in Linked List format gt gt gt mex lez Alsetodenseq
232. local_var noisy_img size size 37 Tor L wm range 3 SIS res noisy img denoised img ipdb gt prine dl var IG O5 S372 SSG kesr oO a7 S19 5013 969 2397 coup S846 262 23554 5979 A10 344 dc 392 604 S377 D 55 B62 08 c i79 d 99 1992 L 545 892 290 456 248 69 1953 P 66 Yeo 736 nw besa 17257940P ipdb gt print T var main U 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 run wilener IilLering py Warning divide by zero encountered in divide Warning divide by zero encountered in divide Warning divide by zero encountered in divide We can turn 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 Out Si a davide s prin anvedid tprimt overs prime under t iemore Other ways of starting a debugger 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 Debugging test failures using nosetests You can run nosetests pdb to drop in post mortem debugging on exceptions a
233. looks like this L Figure linear square x 92 7 y 54 3 5 3 Simple Plots 89 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercises 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 1 as keyword arguments at creation time plot x linear g x square r o 2 with the function setp setp line color g 3 using the set something methods 1ine set marker o Lines have several properties as shown in the following table oneof o svx gt lt ete There are many line styles that can be specified with symbols 5 4 Properties 90 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Symbol steps use gnuplot style steps kwarg only Colors can be given in many ways one letter abbreviations gray scale intensity from 0 to 1 RGB in hex and tuple format 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 bo bue g fern cr md Other objects also have properties The following table list the text properties 5 4 Properties 91 Python Scientific lecture notes Release 2012 3 EuroSc
234. 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 gt gt gt mean bubble size median bubble size 6 11 Summary exercises on scientific computing 140 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Choa cr oy HE 6 11 Summary exercises on scientific computing 141 Part Il Advanced topics 142 CHAPTER 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
235. mentation about the model itself The declarative approach to the creation of classes makes it self descriptive from traits api import HasTraits Str Float class Reservoir HasTraits name Str max storage Float 100 The desc metadata of the traits can be used to provide a more descriptive information about the trait from traits api import HasTraits Str Float class Reservoir HasTraits 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 bloat lep desc Maximal storage hm3 max release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m efficiency Range 0 1 def energy production self release Returns the energy production Wh for the given release m3 s FX power 1000 x 9 81 self head release x self efficiency return power x 3600 1f name main ae 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 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 release reservoir energy_production release 14 3 4 Visualisation The Traits library is also aware of user interfaces and can pop up a default vie
236. mport print a print b In 10 whos Variable Data Into module demo from demo py gt f nction lt f nct ion print da at 0xb 421554 print b PUACELON lt f unction print b at Uxb 4214c4 gt In li printa 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 pou Z U U 3 55 fc cd uUu J O 2 6 4 main and module loading File demo2 py import sys def print a VEI s print 7a print sys argv if name X Print a Importing it In 11 import demo2 b In 12 import demo2 Running it In 13 run demo2 b a 2 6 Reusing code scripts and modules 28 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 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
237. n Restricts the variable names you can use os name might override name or vise versa Creates possible name clashes between modules e Makes the code impossible to statically check for undefined symbols 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 2 2 NO linspace 0 7 105 6 arrer T pose p d E o Bop 10 1 gt gt gt import scipy scientific computing In Python x y software Ipython 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 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 2 6 Reusing code scripts and modules 26 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 A demo module def print_b VEIInbs ba print b def print a Meri ties a N 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 th
238. n be obtained by installing the EPD http www enthought com products epd php Tutorial content Introduction page 262 Example page 262 e What are Traits page 263 Initialisation page 264 Validation page 264 Documentation page 265 Visualisation page 266 Deferral page 267 Notification page 272 Some more advanced traits page 275 References page 278 261 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 14 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 Kiva 2D primitives supporting path based rendering affine transforms alpha blending and more Enable object based 2D drawing canvas Chaco plotting toolkit for building complex interactive 2D plots Mayavi 3D visualization of scientific data based on VTK Envisage application plugin framework for building scriptable and extensible applications Mayavi TVTK VTK Traits Chaco Enable Kiva lt T Ijs ri ird del ul EIUS EAT In this tutorial we will focus on Traits 14 2 Example Throughout this tutorial we will use an example based on a water resource management simple case We will try to model a
239. n 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 scipy org all about numpy arrays manipulating them in dexation questions etc 85 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 SciPy Users List scipy user scipy org scientific computing with Python high level data process ing in particular with the scipy package matplotlib users Q lists sourceforge net for plotting with matplotlib 86 CHAPTER 5 Matplotlib author Mike M ller 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 Python 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 Python version 0 12 it allows interactive matplotlib sessions that h
240. nd nosetests pdb failure to inspect test failures using the debugger 9 3 Using the Python debugger 197 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 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 such 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 a PrinttheTocal variables O O OoOo 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
241. ng over a file In 6 open workrile 214 in 7 for Line in f print line This ioe esis emen o ete Sect IH 8 fasc bosse File modes Read only r Write only w Note Create a new file or overwrite existing file Append a file a Read and Write r Binary mode b Note Use for binary files especially on Windows 2 8 Standard Library Note Reference document for this section The Python Standard Library documentation http docs python org library index html Python Essential Reference David Beazley Addison Wesley Professional 2 8 Standard Library 32 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 2 8 1 os module operating system functionality A portable way of using operating system dependent functionality Directory and file manipulation Current directory List a directory In 3112 os listdir os curdir Cu S index rSkaswo 5 opythonmeldneusecrssueewo NV ew array Oy oW y joer aee templates pasic types rat COME Oy S COMmceol S B CSC debugging rst Make a directory In 32 os mkdvr junkdr r in 33 Junkcur an oslis dir os cu urdir Out Sal True Rename the directory In 36 os rensme unkdrf toodrr In I37 3nkcur an os listailn oS curdam Out 37 False in sol S an os ligro Eq osobu cl Ome Ses True In fails os rmdir food in 427i gt rooodsi
242. noisy image 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 2 Segmentation Histogram based segmentation no spatial information gt gt gt n 10 gt gt gt 1 256 22 gt 1m np Zeros UL 17 gt gt gt np random seed 1 gt gt gt points lenpsrandom random 2 mx Z gt gt gt im points 0 astyre mpc ine wWpounmcsiil sesucvepeunpsti m I gt gt gt im ndimage gaussian_filter im sigma 1 4 xxn gt gt gt mask im gt im mean astype np float gt gt gt mask 0 1 im gt gt gt img Mask 0 2 noerandom rancdm mask ysnape 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 binary_img img gt 0 5 histogram 1 0 0 5 Automatic thresholding use Gaussian mixture model gt gt gt mask im gt im mean astype np float gt gt gt mask 0 1 im gt gt 1m9 Mask t OF s np random rand mask shape gt gt gt from sklearn mixture import GMM gt gt gt classif GMM n_components 2 12 5 Feature extraction Python Scientific lectur
243. ns a lists of things and this list created by running a loop If we don t know how many 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 x kwargs return list generator_func xargs xxkwargs 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 det pluginiclse plugin cls PLUGINS append plugin QWordProcessor plugin class CleanMdashesExtension object def cleanup self tex
244. ns hore T The Ufunc loop runs with the Python Global Interpreter Lock released Hence the nogil T f e And So all local variables must be declared with edet T Note also Lhat this function receives poilinters Co the dala T cdef double complex z z in 0 cdef double complex c c in 0 eder ane ve S the ankeger we use an the ror loop 8 2 Universal functions 174 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 CULOLE for ZarealxxZ a Se de db db db de TODO mandelbrot iteration should go here Return the answer for this point z outil Z i Boilerplate Cython definitions t The litany below is particularly long b t you don t really need to ft read this part ut Just pulls in stuts from the Numpy C headers cdef extern from nu umpy arrayobject h yord import array ctypedef int npy_intp cdef enum NPY TYPES NEY DOUBLE NPY CDOUBLE NPY LONG ces extern from aumey Urunmcoejece aii vord ES bI EN CN Ceypeder void w wBREvUbRumncGenecrobRunectscon cohear e Noy wnbpey py INLE oce obgect PyUPusc PromPuncAndDsate PyUFUuncGenecroFunctrone func sOxd w data Chars types rint ntypes une Nin vns nout int tden ity hare mame Claire CoG asit C List of pre defined loop functions vord Py
245. ntation is more accurate Graph based segmentation use spatial information 12 5 Feature extraction 239 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt from sklearn feature_extraction import image gt gt gt from sklearn cluster import spectral_clustering soc 1 100 gt gt gt x y Nprindices l 17 gt gt gt centerl 28 24 gt gt gt center2 40 50 gt gt gt center3 67 58 gt gt gt center4 24 70 Pee qeadasusl macrus2 radiuss radius4 16 14 15 14 gt gt gt Circle x centerlL O ee2 y eenterl ll 2 lt radiusl x2 gt gt Circle x center2 0 2 t ly ocenter2z Lt ye 2 lt radius 4x2 gt gt gt circle3 x center3 0 2 y center3 1 2 radius3x 2 gt gt gt circle4 x center4 0 2 y center4 1 2 lt radius4 x 2 gt gt gt 4 circles img Ccirolel circle2 circles circle4 gt gt gt mask img astype bool gt gt gt img img astype float gt gt gt img t 0 24npo random randn imog shape gt gt gt Convert the image into a graph with the value of the gradient on gt gt gt the edges gt gt gt graph image img_to_graph img mask mask 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
246. ntly 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 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 I home search examples gallery docs modules index Click on any image to see full size image and source code um mre Go F R CE D 2 Enter search terms or a module j L class or function name 4 T H i m B A N ee 00000 0 107 70 i c AMT OC gt zs na j V s l x Vu g l eooe i D z festes Ez ee EL lt b a a amp matplotlib x x some other mere Rrape n gem a Ol y I y 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 84 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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
247. obj try yield obj finally Obj sc lose Let s rewrite the assert_raises example as a generator Qcontextlib contextmanager def assert_raises type Ery 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 158 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 Anatomy of Numpy arrays and its consequences Tips and tricks Universal functions what why and what to do if you want a new one Integration with other tools Numpy offers several ways to wrap any data in an ndarray without unnecessary copies Recently added features and what s in them for me PEP 3118 buffers generalized ufuncs Prerequisites s Numpy gt 1 2 preferably newer Cython gt 0 12 for the Ufunc example PIL used in a couple of examples In this section numpy will be imported as follows gt gt gt import numpy as np 159 Python Scientific lecture notes Release 2012 3
248. ocessing 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 numpy basic array manipulation e scipy scipy ndimage submodule dedicated to image processing n dimensional images See http docs scipy org doc scipy reference tutorial ndimage html gt gt gt from scipy import ndimage e afew examples use specialized toolkits working with np array Scikit Image scikit learn Common tasks in image processing Input Output displaying images Basic manipulations cropping flipping rotating mage filtering denoising sharpening mage segmentation labeling pixels corresponding to different objects Classification More powerful and complete modules OpenCV Python bindings CellProfiler TK with Python bindings many more 227 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Chapters contents e Opening and writing to image files page 228 Displaying images page 229 e Basic manipulations page 231 Statistical information page 232 Geometrical transformations page 232 Image filtering page 232 Blurring smoothing page 233 Sharpening page 233 Denoising page 233 Mathematical morphology page 235 Feature extraction page 237 Edge detection page 237 Segmentation page 238
249. ocks rze 5 2 dtypeenp irnbs gt gt gt mtx lt 3x4 sparse matrix of type type numpy int8 with 0 stored elements blocksize 3x2 in Block Sparse Row format gt gt gt mtx todense Merci VOR 0 kOe 0l Lee Os O DR E L0 0 0 du dtype inte a bug e create using data ij tuple with 1 1 block size like CSR poc row No abray TOP T 2 aD gt gt gt xol mp array qM 2 42 xs d gt gt gt data np array lly 22 3 4 3 9 gt gt gt mtx sparse bsr_matrix data row col shape 3 3 gt gt gt mtx 3x3 sparse matrix ot type lt type numpy Tnbpo4t with 6 stored elements blocksize 1x1 in Block Sparse Row format gt gt gt gt mtx todense Maree Ill x Z POTO 4 5 6 gt gt gt mtx data array T 1 11 2 Storage Schemes 219 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 ike 1 gt gt gt mtx indices abray 0 2 2595 5 d oo Me H arra d Di 2 dtype int32 6 dtype rnt32 e create using data indices indptr tuple with 2 2 block size ooo I0dpEtr mparray Op 27 de gt gt indices mMo erray Oy 27 225 107 Up 2 data Mpe array 2 3 4 5 6 repeat 4 reshape 6 2 2 gt gt gt mtx sparse bsr_matrix data indices indptr shape 6 6 gt gt gt mtx todenset EEE TE T T DL HIL Lg 15 Qr D Zr Za LL cu SOL 05 Ss 34 Lies OD We Oe Be sS 4 4 D Dy 6 oly aA Se eG SN gt gt
250. ogonal grid In 25 s mlab pipeline scalar field values 15 2 3D plotting functions 283 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 15 3 Figures and decorations 15 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 I 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 15 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 15 3 Figures and decorations 284 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Example docstring mlab mesh Plots a surface using grid spaced data supplied as 2D arrays Function signatures meos S Zr poa X y Zare 2D arrays all of the same shape giving the positions of the vertices of the surface The c
251. ok like this a LLL 8 00 2 00 4 00 6 00 8 00 5 6 Ticks 94 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercises 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 5 1 The Hierarchy 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 matplotlib means the whole window in the user interface Within this figure there can be subplots While subplot positions the plots in a regular 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 5 7 2 Figures A figure is the windows in the GUI that has Figure T as title Figures are numbered starting from 1 as opposed to the normal Python way starting from 0 This is clearly MATLAB style There a
252. olution yvec for the trajectory can now be plotted 1 0 0 8 d Oo y position m I 0 2 000 05 10 15 20 25 30 35 40 Time s Another example with scipy integrate 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 y 2 eps wo y wo 2 y 0 with wo 2 k m with k the spring constant m the mass and eps c 2 m wo with c the damping coefficient For this example we choose the parameters as gt gt gt mass 0 5 kg gt gt gt kspring 4 N m gt cviscous 0 4 is eS m so the system will be underdamped because gt gt gt eps Cviscous 7 2 x mass np osqrt kspring mass gt gt gt Cpo l rrue 6 8 Numerical integration scipy integrate 121 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 For the scipy integrate 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 TE NM VM eo SESS 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 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
253. omatically called on attribute access gt gt gt class A object property def a self an important attribute return a value gt gt gt A a 7 2 Decorators 152 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Sproperty object at Oxia 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 edge property def area self Computed area Setting this updates the edge length to the proper value weg return self edgex x 2 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 get ter 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 fget 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 are
254. on 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 199 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Wrap up exercise The following script is well documented and hopefully legible 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 200 CHAPTER 10 Optimizing code Donald Knuth Premature optimization is the root of all evil author Ga l Varoquaux This chapter deals with strategies to make Python code go faster Prerequisites line profiler http packages python org line_profiler Chapters contents Optimization workflow page 201 Profiling Python code page 202 Timeit page 202 Profiler page 202 Line profiler page 203 Making code go faster page 204 Algorithmic optimization page 204 Example of the SVD page 204 Writing faster numerical code page 205 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 b
255. onnectivity 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 wireframe or points or mesh or fancymesh Default surface resolution
256. oquaux and Pauli Virtanen This chapter gives an overview of Numpy the core tool for performant numerical computing with Python 3 1 The numpy array object Section contents What are Numpy and numpy arrays page 40 Reference documentation page 41 Creating arrays page 42 Basic data types page 44 Basic visualization page 44 Indexing and slicing page 47 Copies and views page 48 Adding Axes page 49 Fancy indexing page 50 3 1 1 What are Numpy and numpy arrays Python has built in containers lists costless insertion and append dictionaries fast lookup high level number objects integers floating point Numpy is extension package to Python for multi dimensional arrays closer to hardware efficiency designed for scientific computation convenience 40 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt import numpy as np gt gt gt a MoOrerray 0 1 2 3 gt gt gt a arcay ro b 27 31 For example An array containing values of an experiment simulation at discrete time steps signal recorded by a measurement device e g sound wave pixels of an image grey level or colour 3 D data measured at different X Y Z positions e g MRI scan Why it is useful Memory efficient container that provides fast numerical operations In 1 1 range 1000 In 2 timeit 1 2 for i in 1 LOOO loops best ort 3 403 us per loop In 3 a np a
257. 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 1 for i in nums Ea gt gt gt list 1 for 3 ain nums dey 2 X 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 sert I0 xs 2N gt gt gt 1 1 x 2 for i in range 3 Ones OM esc ek cio ud If you are stuck at some previous Python version the syntax is only a bit worse gt gt gt set i for i in abc Sos Ru Mer 1S 10 poc Over ly Ord EOF i cmo 3590 B cee eg ee Poy lt E 25 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 t
258. or stiff and non stiff problems see the ODEPACK Fortran library for more details odeint solves first order ODE systems of the form dy dt rhs yl y2 t0 As an introduction let us solve the ODEdy dt 2ybetweent 0 4 with the initial condition y t O 1 First the function computing the derivative of the position needs to be defined gt gt gt def calc derivative ypos time counter arr counter arr 1l 6 8 Numerical integration scipy integrate 120 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 return 2 x ypos 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 dtype np urntl65 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 which was the number of time steps gt gt gt counter array 129 dtype uint1i19 and the cumulative number of iterations for each of the 10 first time steps can be obtained by soos aod mctu ts array lol 309 454 49 Dor 57 59 9554 05 99 udtvpe ulito2 Note that the solver requires more iterations for the first time step The s
259. ore involved example implementing python s slicing In 98 def slicer seq start None stop None step None rane TEM WDDIeNmentgasccU m chnon si ET ere return seq start stop step In 101 rhyme one fish two fish red fish blue fish split In 102 rhyme Owe qe i one Lers 2 Eno gt vg qne ren Wish alia In 103 slicer rhyme Out IOs ies Mene p Aeh a AEN E redy rris Eet In 104 slicer rhyme step 2 Oue boan i one k Eve y rad bie In 105 slicer rhyme 1 step 2 OCOS I E cH T e E T rst Tent In 106 slicer rhyme start 1 stop 4 step 2 Cure NOG Me esp esu gt MP eae inne The order of the keyword arguments does not matter In 107 slicer rhyme step 2 start 1 stop 4 Ou pop es iepsospt 9 NH but it is good practice to use the same ordering as the function s definition d et Fien 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 Defining functions 21 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Fortuna
260. ort numpy as np gt gt gt import matplotlib pyplot as plt gt gt gt x np linspace 0 1e6 10 psc plep 5 Due A2 vr leb ESO matploridblsxpnessbosne2D o5jeet at s gt gt gt plhtowlabelq suxze n lt matploclibtext Text object at 22 gt gt gt plt ylabel memory MB matplotlib uext Text ODJECE at 2 22 11 1 2 Sparse Matrices vs Sparse Matrix Storage Schemes sparse matrix is a matrix which is almost empty storing all the zeros is wasteful gt store only nonzero items think compression pros huge memory savings 207 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 cons depends on actual storage scheme usually does not hold 11 1 3 Typical Applications solution of partial differential equations PDEs the finite element method mechanical engineering electrotechnics physics graph theory nonzero at i jJ means that node i is connected to node j 11 1 4 Prerequisites recent versions of e numpy SCipy e matplotlib optional ipython the enhancements come handy 11 1 5 Sparsity Structure Visualization e Spy frommatplotlib example plots 11 1 Introduction 208 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 11 2 Storage Schemes seven sparse matrix types in scipy sparse 1 csc matrix Compressed Sparse Column format csr matrix Compressed Sparse Row format bsr matrix Block Spar
261. p scikit learn org dev auto examples applications face recognition html original shape of images 50 37 wee import numpy as np import pylab as pl from sklearn 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 lfw_people target k 4 next x train X test faces itrain taces test y_train y_test lfw_people target train lfw_people target test dimension reduction pca decomposition RandomizedPCA n components 150 whiten True posset xX Troin x train poa DIOS tr NS aT o train X_test_pca pca transform X_test 17 5 Putting it all together face recognition 302 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Clacar cacon clf svm SVC C 5 gamma 0 001 CLE EN EAOCCTESE SES Coa 7G rain predict on new images for s in range 10 print lfw people target names clf predict X test pca i 01 1 _ pl imshow X test i reshape 50 37 cmap pl cm gray _ raw input 17 6 Linear model from regression to sparsity Diabetes dataset The diabetes dataset consists of 10 physiological variables age se
262. pillage print s 79 if name 1 main projectA Reservoir name Project A max_storage 30 max release 5 0 hydraulic head 600 efficiency 0 8 State ReservoirState reservoir projectA storage 15 state release 5 U state inflows Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 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 mmm reservoir Instance Reservoir max storage DelegatesTo reservoir Float DelegatesTo reservoir min_release max_release state attributes storage
263. ples 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 part 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 init self data self data data classmethod def fromfile cls file data numpy load file return cls data This is cleaner then using a multitude offlagsto init e staticmethod 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 aut
264. py optimize fminbound for 1D optimization e scipy optimize fmin_l_bfgs_b aquasi Newton page 253 method with bound constraints gt gt gt def f x return np sqgrt Oocl0 3 2 4 XxX Ll 2 2 poc OCDeEIMize min l Oros bh Cr Mesarcray I0 ru opi oxegqitodsi pounds las dose io e emm ME Sq oq ees O0S4I eos mtd 0 wack SSNMORRGOHENCBSNORM OF PROJFE 13 5 2 General constraints Equality and inequality constraints specified as functions f x Oandg x 0 e scipy optimize fmin slsqp Sequential least square programming equality and inequality con O Lx straints gt gt gt def f x return nowsdru x 0 3 2 4 x1 2 e2Z gt gt gt def constraint x return np atleast_1d 1 5 no sum np abs x 13 5 Optimization with constraints 259 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt dQpUcTImizecrnmmimssdtseptr moyarray 95 0l steqcons conctraimmt 1 Optimization terminated successfully Exit mode 0 Current function value 2474973735504 Iterations 5 Runctsxon evaluacronst 20 Gradient evaluations 5 array 1 25004696 0 424995304 e scipy optimize fmin_cobyla Constraints optimization by linear approximation inequality constraints only gt gt gt Op imize fmin ccobvd ts nNp array I0 Oly cOms Gonscrainc Normal rercurcn fromosubroutuene COBY LA NFVALS aye F 2 474874E 00 MAXCV 0 000000
265. python mode hook lambda pyflakes mode t A type as go spell checker like integration f obs B self log emissionprob obs 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 In emacs Use the flymake mode with pyflakes documented http www plope com Members chrism flymake mode add the following to your emacs file on when load flymake t defun flymake pyflakes init letx temp file flymake init create temp buffer copy flymake create temp inplace local file file relative name temp file file name directory buffer file name list syilakes Tist local friley add to list flymake allowed file name masks LM t tillymake pytilakes amaic add hook tand rile hook tlymake rind r1 le heok 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 193 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 1 Make it fail reliably Find a test case that m
266. r Choose the right method see above do compute analytically the gradient and Hessian if you can Use preconditionning when possible Choose your initialization points wisely For instance if you are running many similar optimizations warm restart one with the results of another Relax the tolerance if you don t need precision 13 3 3 Computing gradients Computing gradients and even more Hessians is very tedious but worth the effort Symbolic computation with Sympy page 288 may come in handy Warning A very common source of optimization not converging well is human error in the computation of the gradient You can use scipy optimize check grad to check that your gradient is correct It returns the norm of the different between the gradient given and a gradient computed numerically ss Optiumtze cheok oradi Prines Z 21 2 584 eo ONOLOS625e6 07 See also scipy optimize approx fprime to find your errors 13 3 Practical guide to optimization with scipy 256 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 13 3 4 Synthetic exercices Exercice A simple quadratic function Optimize the following function using K 0 as a starting point np random seed 0 K np random normal size 100 100 def f x return nunp sum np dot K x 1 2Z 4 no sum x Z x2 Time your approach Find the fastest approach Why is BFGS not working well Exercice A loc
267. r an initial guess of x y 0 0 See the summary exercise on Non linear least squares curve fitting application to point extraction in topograph ical lidar data page 133 for another more advanced example 6 6 Statistics and random numbers scipy stats The module scipy stats contains statistical tools and probabilistic descriptions of random processes Random number generators for various random process can be found in numpy random 6 6 1 Histogram and probability density function Given observations of a random process their histogram is an estimator of the random process s PDF probability density function 6 6 Statistics and random numbers scipy stats 117 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt a np random normal size 1000 gt gt gt bins np arange 4 5 gt gt gt bins array l 4r 2 i 0 lf 2 SF 41 gt gt gt histogram np histogram a bins bins normed True 0 gt gt gt bins 0 5 oins ble bine 2 1 gt gt gt bins arrar Neto son cR iM so ween ae gt gt gt from scipy import stats 23 gt p Stats norm pdr bins T NORM sg ug ccc lon In Il pli plot bidms histogram In 2 pl plot bsms D 0 45 0 40 0 35 0 30 0 25 0 20 0 15 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 paramet
268. range 1000 In 4 timeit axx2 100000 Loops best or 39 12527 us per Loop 3 1 2 Reference documentation On the web http docs scipy org Interactive help gt gt gt Nelo np array Help on built in function array in module numpy core multiarray array ooa array object dtype None copy True order None subok False in 5 np array Seri POL burcin rumen ceu acd DOCS CE array object dtype None copy True order None subok False ndmin 0 Looking for something gt gt gt np lookfor create array Search results for create array numpy array Create an array numpy memmap Create a memory map to an array stored in a x binary file on disk In 6 np cons NO conealenats np Con 3 1 The numpy array object 41 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 np conjugate np convolve 3 1 3 Creating arrays 1 D a np array o gt gt gt a array lO hs 27 2l gt gt gt a ndim 1 gt gt gt a shape 47 gt gt gt len a 2 D 3 D 2 0 Soca rray y recurs the Guze wor Che Pirse oI mens Ton L3 LATT gt gt gt c shape x er 0 In practice we rarely enter items one by one Evenly spaced gt gt gt import numpy as np gt gt gt a np arange 10 O n i gt gt gt a ausge des Aue Sy ia oe Gy Ou Se gt gt gt b np arange 1 9 2 start end exlusive step gt gt gt
269. rators 146 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 The first of the new methods is send value which is similar to next but passes value into the generator to be used for the value of the yield expression In fact g next and g send None are equivalent The second of the new methods is throw type value 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 del met
270. re several parameters that determine how the figure looks like dpi figure facecolor color of the drawing background figure edgecolor color of edge around the drawing background draw figure fame or act 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 a11 as argument As with other objects you can set figure properties also setp or with the set something methods 5 7 3 Subplots 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 5 7 Figures Subplots and Axes 95 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 _fowet ee ipi xi subplot 211 10 0 8 0 6 0 4 0 2 og 1 0 0 8 0 6 0 4 0 2 og Bd subplot 212 o 02 04 06 08 Lo 10 0 1809 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 6 0 6 subplot
271. release Float 10 desc Maximal release m3 s head Float 10 desc Hydraulic head m efficiency Range 0 1 irrigated 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 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 def energy_production self release Returns the energy production Wh for the given release m3 s power 1000 x 9 81 self head release x self efficiency return power x 3600 traits view View teem name Item max_storage Item max_release Item head Item efficiency Item irrigated areas reem total crop surtace y resizable True 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 The next example shows how the Array trait can be used to feed a specialised TraitsUI Item the ChacoPlotItem 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 cha
272. rid of parameters and returns the parameters corresponding to the minimum value The parameters are specified with ranges given to numpy mgrid By default 20 steps are taken in each direction gt gt gt def f x The rosenbrock function ud return 5o d cu ND 2 ascii je LO I poc Oot imi 7 eMbENDGUE Wiha Zi NE 2 array T 00001416 dass od 5 Simulated annealing Simulated annealing does random jumps around the starting point to explore its vicinity progressively narrowing the jumps around the minimum points it finds Its output depends on the random number generator In scipy it is implemented in scipy optimize anneal gt gt gt def f x Ihe rosenbrock function return 5 sO ccce nu a po Optimize canneal r 2 2 Warning Cooled to SUS7 63e30 ab 20 278 77642 93464712523 Dut this is not the smallest point found pauses i R L E Ibo eod boom 5 It is a very popular algorithm but it is not very reliable Note For function of continuous parameters as studied here a strategy based on grid search for rough exploration and running optimizers like the Nelder Mead or gradient based methods many times with different starting points should often be preferred to heuristic methods such as simulated annealing 13 3 Practical guide to optimization with scipy 13 3 1 Choosing a method 10 10 3 S s 101 U c 9 3 2 10 Ill conditioned Gaussian 2e
273. rides Zp gt gt gt sir y data Se OO vedo 00 S OI senec 0S 00 coo 00 03 00 06 x00 Ve x00 Need to jump 2 bytes to find the next row 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 di ds dn strides 51 S2 Sn C 2 dj41d 42 dn X itemsize s d d dj 1 x itemsize Note Now we can understand the behavior of view gt gt gt y np array IlL 217 12 41l datype np uintS transpose aa y copy Transposition does not affect the memory layout of the data only strides gt gt gt x strides 27 b gt gt gt y strides Ce 2 gt gt gt str x data PSU T e R 04 soo Sur y ddaka EXO 4050 02 04 8 1 Life of ndarray 167 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Never makes copies of the data oo lt MOvarray Lbs 24 34 3 3 dle dtype np into gt gt gt y cepe gt gt gt y array O 75 4 ice coedbvpeseqmto2 gt gt gt y obri des 4 gt gt gt y x 2 gt gt ve array Interface data TIOJ x array interface data TTO 8 g
274. rray 4 8 dtype int32 Note gt gt gt y np diag x k l gt gt gt y array 2r 6l dtype int32 However gt gt gt y flags owndata True It makes a copy See Also stride diagonals py Challenge Compute the tensor trace gt gt gt x np arange 5 5 o 5 reshape 5 5 9 29 gt gt gt s 0 gt gt gt for i in xrange 5 for j in xrange 5 cU CUN im spain le by striding and using sum on the result gt gt gt y as strided x shape 5 5 strides TODO TODO gt gt gt S2 gt gt gt assert s s2 Solution gt gt gt y as strided x shape 5 5 strides 5 5 5 5 x itemsize ee 5 5 DLyex items ize gt gt gt s2 y sum 8 1 Life of ndarray 170 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 CPU cache effects Memory layout can affect performance ra Pe x np zeros 20000 In 2 y nps Zeros 12000040 I c5 In 3 x shape y shape E ET o 2000077 In 4 timeit x sum 100000 loops best or 0 180 ms per loop In 5 timeit y sum 200000 loops best of 3 2 34 ms per loop In 6 x lt strides y vss nes Core 59 Smaller strides are faster cache block size 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 gt fewer transfers needed faster See Also numexpr is designed to mitig
275. rror pyff1 module E e mall snippet to raise c IndexEPror t Pdb continue Traceback most recent call last Poe ust Iie pyehome E pdioeey bine 2 OG ann mien pdb r unsceriQipe mavnpyftile Bankes Aso lato Ae va bio o apice S2 mer 2S Gen miniseries self run statement Etle usr inle Ehon Bdo py Meio OO Eh exec cmd in globals locals File otri y deme d in modulo File index_error py line 8 in lt module gt index error File index error py line 5 in index error pue Lori tenter IndexError list index out of range Uncaught exception Entering post mortem debugging Running cont or step will restart the program gt home varoquau dev scipy lecture notes advanced debugging optimizing index error pyW5 index er gt print disp pberne st Pdb 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 s Run the script with the debugger 9 3 In 1 run d wiener_filtering py mee Blank or Comment ia Blank Or Xe Oe nid 4 Blank or Comment Breakpoint 1 at home varoquau dev scipy lecture notes advanced debugging optimizing wiener f NOTES Enter o0 at the apdb gt prompt to Stare your Scrip gt Sstring 1 module Enter the wiener filtering py file and set a break point at line 34 ip
276. rs 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 exercise The first step will be to find the annual maxima by using numpy and plot them as a matplotlib bar figure 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 The last step will be to find 34 23 m s for the maximum wind speed occurring every 50 years 6 11 Summary exercises on scientific computing 130 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 30 Annual wind speed maxima m s Year Figure 6 2 Solution Python source file 6 11 Summary exercises on scientific computing 131 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Gumbell cumulative probability Veg 34 23 m s 50 25 30 35 40 45 Annual wind speed maxima m s Figure 6 3 Solution Python source file 6 11 Summary exercises on scientific
277. rt numpy as np gt gt gt from scipy sparse linalg import LinearOperator gt gt gt def mv v return up array Ll2 v 10 3 v 1 gt gt gt A LinearOperator 2 2 matvec mv gt gt gt A 2x2 LinearOperator with unspecified dtype gt gt gt gt A matvec np ones 2 anna 2 4 Sealy gt gt gt A x np ones 2 Bursa 2p 32l 11 3 Linear System Solvers 223 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 A Few Notes on Preconditioning problem specific often hard to develop if not sure try ILU available in dsolve as spilu 11 3 3 Eigenvalue Problem Solvers The eigen module arpack acollection 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 m Compute eigenvectors and eigenvalues using a preconditioned eigensolver In this example Smoothed Aggregation SA is used to precondition the LOBPCG eigensolver on a two dimensional Poisson problem with Dirichlet Dougndary cong tions wee 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 rormat csr create the AMG hierarchy ml smoothed_aggregation_solver A initial approximation to the K eigenvec
278. s 74 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Masking versions of common functions oo gt fe meroqre i Ze E Masked array data 1 0 s 1241421356237 mask False True False Truel fill_value le 20 Note There are other useful array siblings page 184 3 3 4 Under the hood the memory layout of a numpy array A numpy array is block of memory indexing scheme data type descriptor raw data how to locate an element e how to interpret an element d bad scalar l ndarray Block of memory gt gt gt x RoOvgarray lly 2 237 dtypeoeenp rnmnbs2 gt gt gt x data lt read write buffer for size 16 offset O at gt gt gt gt str x data ES SON sc O10 6 x OI S00 se x 00 V0 00 ecc 010 00 04 00 sese Memory address of the data gt gt gt x array interface data 0 USTs E 16 Reminder two ndarrays may share the same memory gt gt gt mMpsarray hee 4 gt gt gt z gt gt gt gt gt gt array 0 27 3 41 gt gt gt y base is x True Memory does not need to be owned by an ndarray 3 3 More elaborate arrays 75 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt x x01 x02 x03 x04 gt gt gt y np frombuffer x dtype np int89 gt gt gt y arrayv Lb 2 3 4ly dtype inte gt gt gt y data lt read only Duffer for ve S120 4 Offset Q at ues gt gt gt
279. s and data x nonzero values of the i th row are data indptr i indptr i 1 with column indices indices indptr i indptr itl item i j can be accessed as data indptr i k where k is position of j in rndices andpcer a tindptr TL aE 11T subclass of _cs_matrix common CSR CSC functionality subclass of _data_matrix sparse matrix classes with data attribute e fast matrix vector products and other arithmetics sparsetools constructor accepts dense matrix array sparse matrix shape tuple create empty matrix data ij tuple data indices indptr tuple efficient row slicing row oriented operations slow column slicing expensive changes to the sparsity structure use actual computations most linear solvers support this format Examples create empty CSR matrix gt gt gt mtx sparse csr matrix 3 4 dtype np int89 gt gt gt mtx todense t Metis IOs OF aS ED POs Oe era Ti Oy Up 07 Ollie dbypesxmbe create using data ij tuple gt gt gt oW Mp anra IOP Op by 2 327 ET boo Col eco ui 2a Oe nen gt gt gt data np array il 2 3 4 9 61 gt gt gt mtx sparse csr matrix data row col shape 3 3 gt gt gt mtx 3x3 sparse matrix of type type numpy into4 with 6 stored elements in Compressed Sparse Row format gt gt gt mtx todense Metric G xc Oye Oy 3 aie 25 O 1 11 2 Storage Schemes 216
280. s and their parameters 305 D diff 291 294 differentiation 291 dsolve 294 equations algebraic 292 differential 294 integration 292 M Matrix 293 D Python Enhancement Proposals PEP 255 146 PEP 3118 181 PEP 3129 156 PEP 318 149 156 PEP 342 146 PEP 343 156 PEP 380 148 PEP 380 id13 148 PEP 8 151 o solve 292 Index 306
281. s done in gradient descent code using a line search Table 13 2 Adaptive step gradient descent iterations function calls S O 20 40 60 80 100 120 140 A well conditionned quadratic function iterations function calls Error on f x o L 5 E s O 200 400 600 800 100012001400 An ill conditionned quadratic function Error on f x An ill conditionned non quadratic function An ill conditionned very non quadratic function The more a function looks like a quadratic function elliptic iso curves the easier it is to optimize Conjugate gradient descent The gradient descent algorithms above are toys not to be used on real problems As can be seen from the above experiments one of the problems of the simple gradient descent algorithms is that it tends to oscillate across a valley each time following the direction of the gradient that makes it cross the valley 13 2 A review of the different optimizers 250 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 The conjugate gradient solves this problem by adding a friction term each step depends on the two last values of the gradient and sharp turns are reduced Table 13 3 Conjugate gradient descent iterations function calls Error on f x e 40 60 80 100 120 An ill conditionned non quadratic function iterations n function calls a e dl Methods based on con
282. s tnupoesqrhtimeauncscsdlistomce Gs be Nps ET fet lt matplotl ib lines Line2D Object db se matplorpiwcb lrnes hvrne2D Obyece at sr s plIt xlabel r USsts mactplotlrb textoelText ODject at 2 gt gt gt Dilteylabelir Ss vegrty 15note delta x 2 Xrsngle e e mat pror babere ce rp qM Io o pui pra g B LOQ 150 Z ULU 3 2 Numerical operations on arfays 57 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 The RMS distance grows as the square root of the time 3 2 3 Broadcasting Basic operations on numpy arrays addition etc are elementwise 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 oF Go c M G 2 E Let s verify gt gt gt aqa np tile np arange 0 AO S qq DOS 22 gt d Bu OF 89 MF M Ou EE EQ aCe 20n 20l L350 307 Sd gt gt gt b np array 0 1 21 gt gt gt a b array MU P LOF site kel EG ee a PSO 9p C An useful trick gt gt gt a np arange 0 40 10 gt gt gt a shape 3 2 Numerical operations on arrays 58 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt a a np newaxis adds a new axis gt 2D array gt gt gt a shape LLL
283. se Row format dok matrix Dictionary of Keys format 2 3 4 lil matrix List of Lists format 5 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 plt 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 Storage Schemes 209 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 11 2 1 Common Methods all scipy sparse classes are subclasses of spmat rix default implementation of arithmetic operations always converts to CSR subclasses override for efficiency shape data type set get nonzero indices format conversion interaction with NumPy toarray todense 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
284. sect gt gt gt X y copy gt gt gt x anran Vii 2 i 3 4 dtype uint89 gt gt gt y array Iii 21 3 4 dtype uint8 gt gt gt X view np intl6 array Cll Sle 1027 ae recur E gt gt gt 0x0201 0x0403 ones OE gt gt gt y view np intl6 array Eb 769 0026 divibe umt 1 What happened We need to look into what x 0 1 actually means so 05 030 959007 RS EET 8 1 4 Indexing scheme strides Main point The question gt gt gt x np drray IIl 2 e DE 911 de ype np i ie gt gt gt str x data ALOLLA x03 49125 015 V4 1 ono 6D Ore etd At which byte in x data does the item x 1 2 begin The answer in Numpy strides the number of bytes to jump to find the next element stride per dimension 8 1 Life of ndarray 166 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt x strides oy cul gt gt gt byte offset 3 1l 4 1 2 p To dung gru gt gt gt x data byte offset ESO IGN poc 22 6 e simple flexible C and Fortran order 2D ox gt Mpaarray id 2 ub Ecc 29 pe LIy o f 91 deypesenpscntlo order C pos x SEP ides 6 2 gt gt gt str x data XO NS X2c02 VseQ 0 x03 x00 x04 x00 x05 x00 1x06 voc O0 Noe 0 7 N3e0 0 x08 x00 E x00 Need to jump 6 bytes to find the next row Need to jump 2 bytes to find the next column gt gt gt y np array x order F gt gt gt VY st
285. ses on scientific computing 138 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 140000 120000 100000 80000 60000 40000 20000 U 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 gt gt gt void filtdat lt 50 gt gt gt sand np logical and filtdat gt 30 filtdat ed 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 VOLO saSt ype Npainmt 2Zeoglass astype Np int 34sSaendsast ype Np and 6 11 Summary exercises on scientific computing 139 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 sumor 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 41 gt gt gt mask sand areas gt 100 gt gt gt remove small sand
286. 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 from scipy sparse linalg import dsolve gt gt gt help dsolve both superlu and umfpack can be used if the latter is installed as follows prepare a linear system import numpy as np gt gt gt from scipy import sparse oe MEX Sparse spetvsaosl bl 2 3 4 uh Pe 2 8 Se S JL doo pomo EOdemSe mateix TL de nhe UD Ul Ly 27 8 UF 63 D0US un Sm Su Sy L 0p UU Op 4p 10l Oe Oy Oe Oy S orhs Mp array ll 2 3 4 E 11 3 Linear System Solvers 221 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 solve as single precision real gt gt gt mtxl mtx astype np float32 gt gt gt x dsolve spsolve mtxl rhs use_umfpack False gt gt gt print x S ER e A i gt D s Wg gt gt gt print Errore Mlk oe ee Iae NS D On OF Ore solve as double precision real gt gt gt mtx2 mtx astype np floato4 gt gt gt x dsolve spsolve mtx2 rhs use_umfpack True gt gt gt print x 106 SZ H S 2125 5 T gt gt gt print VRrror y nox x eis Ero TOF 0 OL 0 om solve as single pre
287. t return text replace amp mdash u N em dash 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 155 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 e PEP 3129 class decorator syntax s http wiki python org moin PythonDecoratorLibrary s http docs python org dev library functools html s http pypi python org pypi decorator Bruce Eckel Decorators I Introduction to Python Decorators Python Decorators II Decorator Arguments Python Decorators III 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
288. t gt J np arange 5 voc 2 ue Wy g crane cu Uk ap Gay Oy ls 28l Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Warning Array multiplication is not matrix multiplication gt gt gt c np ones 3 3 gt gt gt C 9 arro ue ds d ds dee iex x i aes Hier CHE Note Matrix multiplication po codot e gus UNS Dus 5 I r c SUC up E elo Comparisons sew 3o Mpsarray i 2 9 se D peara 4 2 2 gt gt gt a pb array Ealse True False gt gt gt a gt D array False False True Logical operations e R a d ur gt gt gt E e R E ll 0 35 o gt Pe Logillcalver a is array True True True oe Mp hog hea leanne D array True False False Shape mismatches gt gt gt a np arange 4 SS ECT array Chg al Ze oA o gt ae Meeerray iL 21 Traceback File Ie T IE W ValueError shape mismatch line i in 3 2 Numerical operations on arrays 7 NOT matrix mullet plication True dtype bool False dtype bool 0 0 dtype bool dtype bool False dtype bool False dtype bool most recent call last lt module gt objects cannot be broadcast to a single shape 52 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Broadcast We ll return to that ater page 58 Transposition gt gt gt a np triu np ones 3 3 1 see help np triu gt gt gt a gus Wa ieee LR x0 Oo Led Or
289. t gt gt clf best_estimator gamma 0 0005994984250519594089 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 scikit learn exposes CV estimators that set their parameter automatically by cross validation gt gt gt from sklearn 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 ZION DIDES T Q 00253 4 Q 002151 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 lasso alpha OPO ss MEE These estimators are called similarly to their counterparts with CV appended to their name 17 7 Model selection choosing estimators and their parameters 304 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Exercise On the diabetes dataset find the optimal regularization parameter alpha 17 7 Model selection choosing estimator
290. t gt gt x np zeros 10 10 10 dtype np float Doc x Strides 800 60 8 scm cs p dcr te pee tope S 1600 2407 32 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 intl6 gt gt gt as strided x strides 2 2 shape 2 J array l 3 dtypesrnti6 gt gt gt oue e array l 3 dtbype untio See Also stride fakedims py Exercise array lly 2 3 ig VpDe npeaerbe array ill x I S 27 o 4 se 27 or eddy sdivpecnposmto using only as strided 8 1 Life of ndarray 168 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Hint byte offset stride 0 xindex 0 stride 1l index 1 Spoiler Stride can also be 0 gt gt gt x np array 1 2 3 4 dtype np ints8 gt gt gt y as_strided x strides 0 1 shape 3 4 gt gt gt y eu TI ap too Aly do 1 x sdubs dy 2 3 41l cdUuvpeeume gt gt gt y base base is x True Broadcasting e Doing something useful with it outer product of 1 2 3 4 and 5 6 7 gt gt gt x Npearray ill 2 gt r 4 Gr ype np eine 6 gt gt gt x2 as strided x strides 0 1 2 shape 3 4 gt gt gt x2 mra 0 D lt 2 Saye ly ele TEN
291. t mx 1 np ma masked gt gt gt mx masked array data 1 3 5 mask False True False True False till valus 999999 The mask is cleared on assignment 8 4 Array siblings chararray maskedarray matrix 184 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt mx 1 9 gt gt gt mx masked array data 1 9 3 5 mask False False False True False fill value 999999 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 array i L oy Sy le 99 The mask can also be cleared gt gt gt mx mask np ma nomask gt gt gt mx massedcarray daeta 1 9 3 99 5 mask False False False False False fill value 999999 Domain aware functions The masked array package also contains domain aware functions See p Maas log np cerae ail uve oS Sn maskcedharray data 030 0 609314718056 III 1y mask False False True True False True fill value le 20 Note Streamlined and more seamless support for dealing with missing data in arrays is making its way into Numpy 1 7 Stay tuned 8 4 Array siblings chararray maskedarray matrix 185 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Example Masked statistics Canadian rangers
292. t at Oxdd6a71c 15 3 3 Decorations Different items can be added to the figure to carry extra information such as a colorbar or a title In 9 Qurt le s In 10 Ou LO s In 11 Aedes Tn Plas Oue 3s miab colorbar Out 7 Crientataon vertical lt tvtk Classes scalar Dar a tor ScalarBarhcror Ob jece at RE rec mlab title polar mesh lt enthought mayavi modules text Text object at Oxd8ed38c gt mlab outline Out 7 enthought maysvrimodules outlrine Oubisne object at 0xdd21b6c mlab axes Out 7 enthought mayavi modules axes Axes object at Oxd2e4bcc gt 15 3 Figures and decorations 286 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 l 60e 16 0 847 0 399 0 25 D 0442 B 192 Warning extent If we specified extents for a plotting object mlab outline and mlab axes don t get them by default 15 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 EM EX 234 Q6 GD Pipeline Scalar LUT Vector LUT ModuleManager v Mayavi Scene 1 LUT Look Up Table Manager GridSource v 4 PolyDataNormals Lut mode v Colors and legends m Surface Number of colors 256 Outline Reverse lut
293. t microsoft com kb 310519 explains how to handle environment variables 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 add the directory to your sys path each time you want to import from a module in this directory 2 6 Reusing code scripts and modules 29 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 suolo 1s cw aie Python CAd Se Sc SCIO SS cede db 17 07 cl stor TO README txt cum cic CONCI yO LATEST t e Set py __svn_version__ py S eve bale setup pyc oV t yeistore pe CORSO linalg setupscons py THANKS txt fftpack linsolve setupscons pyc TOCHANGE txt Sie dee e IC maxentropy signal version py ee io ls mas OC Ius sparse renes eu Ich INSTALL txt ndimage spatial weave integrate odr special interpolate optimize Eds Sole pao usr lib python 6
294. taset 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 Polynomial kernel RBF kernel Radial Basis Func tion E Svm SVC kerneldg rbf icd degree 3 gt gt gt gamma inverse of size of gt gt gt degree polynomial de xe radial kernel Exercise Which of the kernels noted above has a better prediction performance on the digits dataset 17 2 Classification 299 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 17 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 17 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 of
295. te fast Fourier transforms As an illustration a noisy input signal may look like gt gt gt time step 0 02 gt gt gt period 5 gt gt gt time vec np arange 0 20 time step o gt Sig nowsim 02 np Period a time vec FA 0 5 np random randn time vec size The observer doesn t know the signal frequency only the sampling time step of the signal sig The signal is supposed to come from a real function so the Fourier transform will be sym metric The scipy fftpack fftfreq function will generate the sampling frequencies and scipy fftpack fft will compute the fast Fourier transform gt gt gt from scipy import fftpack gt gt gt sample freq fftpack fftfreq sig size d time step oo SUG ctr ctetpack tbstsetdg Because the resulting power is symmetric only the positive part of the spectrum needs to be used for finding the frequency 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 100 Peak frequency 80 60 plower 40 20 0 050 100 150 200 250 300 350 400 45 0 l 2 3 4 5 Frequency Hz 6 4 Fast Fourier transforms scipy fftpack 109 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 The signal frequency can be found by gt gt gt freq freqs power argmax gt gt gt Np eliclose treq 1 pericd f check that correct freq is found True Now the high frequency nois
296. teger ci cd float T another strig 2S a 1 Oi string il cAn Gino I Jie a floats NOSTRO OE another Stringi String gt gt gt 1 102 gt gt gt filename processing or dataset cs tSc oi gt gt gt filename tprocessing of dataset i gt xtc 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 tell francis 5915 gt gt gt qol DosebascraH s Sore trance 5915 emmanuelle 5952 gt gt gt tel sebastian Sa 1s gt gt gt tel keys sebastian francis emmanuelle gt gt gt tel values POSTO SOS SUUS 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 d a 1 b 2 3 hello gt gt gt d Far d 32 hello b y 9 More container types Tuples 2 2 Basic types 14 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Tuples are basically immutable lists The elements of a tuple are written between parentheses or just separated by commas o t 12345 54321 hello gt gt gt t 0 12345 gt gt
297. tely 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 modiry x v 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 199 mutable variable Dx cow 282 gt gt gt Lry to modify las BD lt 25 DOS 421 S gt gt gt print a I gt gt gt print b E gt gt gt print c Ae Functions have a local variable table Called a local 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 in 114 x gt 5 In 115 def addx y har ee a return x y raire addx 10 Qua eos 15 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 EE x y pter au print x 15 2 2 x In 118 setx 10 Sa dc ORO 25 Defining functions sss Python Scientific lecture notes Release 2012 3 EuroScipy 2012 In 120 x Qu
298. the script In Pil serum t demo py IPython CPU timings estimated User T4 5 92 9 3S System D SIGNO S and profile it In 2 run o demo py gl Fume wom calls in 14 551 CPU seconds 10 2 Profiling Python code 202 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Ordered by internal time ncalls tottime percall cumtime percall ji 14 457 14 457 14 479 14 479 il 0 054 0 054 0 054 0 054 1 0017 SIH SI O07 T 54 OOT 0 000 Oe Oba C2000 2 O20 0S C002 0 005 9 501005 6 020 0 02000 OQ OO 090010 6 OC ON 0 000 SIT DOON 14 Oe BUES 0 000 Oral 0 000 1 9 Dass 0 000 QUOI 909010118 1 90 1000 dl OLOO DIO AG 0 008 1 O20 0d 0 001 14429551 14 551 190007 O20 OC 0 000 0 002 BO n CE COG 0 000 0 004 Des 7 0 000 0 000 0 002 0 000 172 0 000 BE 000 IO 02000 il OOO C2000 1 252 5 51 14 551 29 DO 02000 02000 0 000 35 O20 00 QUID Ou 0910 0 sE OC 00 0 000 0 001 0 01010 Za DOO O OON D OOT C7000 41 0 000 0 000 0 000 0500 0 PAS 0 000 0 000 COG 2080 1 O20 0 0 OS 01010 0 7 00 8 0 008 filename lineno function decomp py 849 svd method random sample of function base py 645 asarray chkfinite mino EEE cUgDEDd SLS clot method any ea py 190 pui ame ore DA LZ numpy linalg lapack_lite dsyevd mtrand Random tate object or numpysndarray objects twodim_base py 204 diag Cele pss O Care Dans execfile defmatrix py 239 array finalize SR 329 sym decorrelation linalg p
299. thods 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 used 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 1s 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 It is 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 gene
300. thon debugger 194 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 incdexError Traceback most recent call last home varoquau dev scipy lecture notes advanced debugging optimizing index error py in lt module gt 6 7 if name __main__ 8 index error 2 IW home usse cuc dc ccs lecture notes advanced debugging optimizing inde error py In index erro 3 det andex error lt 4 lst isi toober gt 5 print lstllen lsc 6 7 if name main IndexError list index out of range In 2 debug gt home varoquau dev scipy lecture notes advanced debugging optimizing index error py 5 index err 4 ise lis foober 5 puces gae nS 6 ISE Sel TISE i i omodd snippet to raise an TNAEXError a E 3 def index error 4 Ilse List foobar gt 5 print lst len lst 6 7 if name main 8 index error E ipdb len lst 6 Jupe prine Esc den dst T de Lire gui Tote S 9 3 Using the Python debugger 195 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Post mortem debugging without IPython In some situations you cannot use Python 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 S python em pdb and ex error py gt home varoquau dev scipy lecture notes advanced debugging_optimizing index_e
301. 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 sumor np bincount to compute the grain sizes 8 Compute the mean size of bubbles Proposed solution gt gt gt import numpy as np gt gt gt import pylab as pl gt gt gt from scipy import ndimage 6 11 Summary exercises on scientific computing 137 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 6 11 4 Example of solution for the image processing exercise unmolten grains in glass Det Spot Mag 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 plor mreead data MV HFV 012 Jpg 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 h histogram filtdat bins np arange 256 6 11 Summary exerci
302. tly over the objects of interest without bothering with indices you often don t care about l User defined classes can customize those rules by overriding the special nonzero__ method 2 4 Control Flow 18 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 Could use while loop with a counter as above Or a for loop gt gt gt for i in range 0 len words print i words i D Cool 1 powerful 2 readable 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 0 cool 1 powerful 2 readable Looping over a dictionary Use iteritems Donee xo ae A T ee Se exo gt gt gt for key val in d iteritems print Key s has value s key val Key a has value 1 Key c has value 1j Key b has value 1 2 2 4 6 List Comprehensions gt gt gt ix x2 for i in range 4 D qe 4 9 Exercise Compute the decimals of Pi using the Wallis formula CO E Ai gt a lu 2 4 Control Flow 19 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 2 5 Defining functions 2 5 1 Function definition In 56 def test Daeg eee Print tn test function In 5
303. tors X scipy rand A shape 0 K preconditioner based on ml M ml aspreconditioner compute eigenvalues and eigenvectors with LOBPCG W V lobpcg A X M M tol le 8 largest False plot the eigenvectors import pylab pylab rrgure fiogstze 9 9 for i in range K Py labes UbPIlot 37 3 ait 1b pylab trcle BErgenvector lt d z 1 pylab pcolor V 1i reshape N N pylab axis equal Py habeas off pylab show 11 3 Linear System Solvers 224 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 examples pyamg with lobpcg py example by Nils Wagner examples lobpcg sakurai py output python examples lobpcg sakurai py Results by LOBPCG for n 2500 39 20 5 2 5 90 5 3 006250022 G206250007 0 Exact eigenvalues O p HE SE X010 IESUS 225 007 0 06250044 Elapsed time 7 01 Eigenvalue distribution 1012 10H 1901 10 10 10 10 lt 10 10 10 10 10 10 101 E amp Bheehke 10 10 10 10 10 Number 11 4 Other Interesting Packages PyYAMG algebraic multigrid solvers http code google com p pyamg Pysparse own sparse matrix classes matrix and eigenvalue problem solvers 11 4 Other Interesting Packages 10 225 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 http pysparse sourceforge net 11 4 Other Interesting Packages 226 CHAPTER 12 Image manipulation and pr
304. tp www python org about for more information about distinguishing features of Python Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 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 Hello world 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 gt gt gt a 3 gt gt gt b 2a gt gt gt type b type int gt gt gt print b 6 gt gt gt axb 18 gt gt gt b hello gt gt gt type bd lt Ly De Str gt Dom w 35 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 s
305. uctivity gains Running pyflakes on the current edited file You can bind a key to run pyflakes in the current buffer In kate Menu settings gt configure kate n plugins enable external tools n external Tools add pyflakes kdialog title pyl lakes sfitename msgbox 5 pyrtlakes t1bensme e 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 Ct rl Shift V is binded to a pyflakes report e In vim In your vimrc binds F5 to pyf lakes Q autocmd FileType python let amp mp echo Hass running ss pyflakes dutocmd FileType tex mp rst python imap lt Esc gt 15 C O make M autocmd FileType tex mp rst python map lt Esc gt 15 make M autocmd FileType tex mp rst python set autowrite In emacs In your emacs binds F5 to pyflakes 9 1 Avoiding bugs 192 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 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 xs The inicial value miL pe LELE hom eNe mode dues Pyflakes The minor mode bindings ue ES gt e qoydlbakescchasts 0e add hook
306. w for the Reservoir class reservoirl Reservoir reservoirl edit traits P a AU F um Edit properties Efficiency 0 07 4 1 0 0 8 Head 10 Hydraulic head 60 Max release 100 0 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 TraitsUI 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 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 efficiency Range 0 1 traits view View name max storage max release head efficiency title Reservoir resizable True energy_production self release Returns the energy production Wh for the given release m3 s Ton power 1000 x 9 81 x self head release x self efficiency return power x 3600 Tf name eas mnaan s reservoir Reservoir name Project A max storage 30 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 m
307. what you can get with scipy optimize fmin slsqp when adding boundary constraints 6 11 Summary exercises on scientific computing 136 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 6 11 3 Image processing application counting bubbles and unmolten grains Det Spot Mag 10 4 mm 15 0 kY SSD 5 8 41x 3 25 mm MV 15 05 2009 1280 C Statement of the problem 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
308. x 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 17 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 sklearn import linear model gt gt gt regr Linear model lasso alpia 3 gt gt gt regr fit diabetes X train diabetes y train Lasso gt gt gt regr coef_ very sparse coefficients array Oe P O 3497 34075682 199 17441034 s p CUIUBO 9929194255 D 430 9379595 Oe 1 gt gt gt regr score diabetes X test diabetes y test 055108354532 being the score very similar to linear regression Least Squares gt gt gt lin linear_model LinearRegression
309. ximal release m3 s Python Scientific lecture notes Release 2012 3 EuroScipy 2012 head Float 10 desc Hydraulic head m efficiency Range 0 1 irrigated areas List IrrigationArea def energy_production self release Returns the energy production Wh for the given release m3 s power 1000 x 9 81 self head release x self efficiency return power x 3600 traits view View Item name ieem max storage Item max_release Item head Item efficiency Item irrigated areas resizable True if name moan ae 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 1 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 erop e Enum Alitalia Wheat Corton class Reservoir HasTraits name Str max storage bloat lep desc Maximal storage hm3 max
310. xing with the np newaxis object allows us to add an axis to an array 3 1 The numpy array object 49 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt z Mpsarray 1 gt gt gt 7 gt gt gt z np newaxis array TIL 2p Si 3 1 9 Fancy indexing Numpy arrays can be indexed with slices but also with boolean or integer arrays masks This method is called fancy indexing It creates copies not views Using boolean masks gt gt gt np random seed 3 gt gt gt a np random random_integers 0 20 15 gt gt gt a array LOr o Orto 3s db 9 10 O 207 dx Wu uq gt gt gt a 3 arkay Balise True False True False False False True False True True False True False False dtype bool gt gt gt mask a 0 gt gt gt extract from a a mask or a a 3 0 wor CXL Pact E rome extract a sub array with the mask amie a O F soe O a gt gt gt a np arange 10 gt gt gt a array 0 Indexing can be done with an array of integers where the same index is repeated several time oo uA Sy ey ay H TI Ones 2 gt 2g de ey ass Pyenon wer array UZ Si 2 A 2 New values can be assigned with this kind of indexing 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 3 1 The numpy array object 50 gt gt gt a np gt gt gt idx Np array 3
311. xt The arguments text coords and xycoords specifies what x and y mean 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 so Pli SR ip arange 10 gt gt gt mat plot lines TERIS ID instance ae sas gt gt gt ax pl gca gt gt gt ax annotate Here is something special xy matplotlibotextsoAnnoetagtgom 3nstanee at a2 gt 2 1 xytext 1 5 gt gt gt ax annotate Here is something special xy 2 1 xytext 1 5 di aArrOWDrODS Lacecolor 2 xr lt matplotlib text Annotation instance ub se Exercise Annotate a line at two places with text Use green and red arrows and align it according to figure points and data 9 6 Ticks 5 6 1 Where and What 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 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 5 6 2 Tick Locators There are several locators for different kind of requirements 5 6 Ticks 93 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 NullLocaior
312. y gt gt gt b 0 12 gt gt gt al array LU bw ay oe ae ege Op Ue ex Gi This behavior can be surprising at first sight but it allows to save both memory and time 3 1 The numpy array object 48 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 Warning The transpose is a view As aresult a matrix cannot be made symmetric in place gt gt gt a np ones 100 100 gt gt gt a t a T gt gt gt a arca i 22 215 Dogs Jung DT DP E T ks U T L T uw wy 2 dem eee de ills bh oie oli ES ie ee eID Worked example Prime number sieve 0123456 7 8 910111213 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 1 which are not primes gt gt gt 1g primel 2 0 For each integer j starting from 2 cross out its higher multiples gt gt gt N max int np sgrt len is_prime gt gt gt 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 j which are already known to not be primes 2 The first number to cross out is j 3 1 8 Adding Axes Inde
313. y 7 07 0f ROG Oe the 10 uU Gi ca calle alte Orla Ose SOL sls Oe Hes Om tO Oe 50 19 T Application remove noise 2 square mnpozeres 32 32 som squesrsepH equ 20 cq 1 gt gt gt np random seed 2 Doux y snp random random 2 20 vescvpedtnp mme gt gt gt sguarelx yl 1 gt gt gt open square ndimage binary_opening Square 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 gt gt gt um Np yzZeros 256 256 gt gt gt im 64 64 64 64 1 SS 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 l mode constant gt gt gt sob np hypot sx sy 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 12 5 Feature extraction 237 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 square Sobel x direction Sobel filter Sobel for
314. y 841 eigh isinstance demo py 1 lt module gt numeric py 180 asarray defmatrix py 193 __new__ dermatrriux py 43 asmacirix defmatrix py 20 mul nu umpy Core multidrray zeros method transpose Gk anume darra cljects ICa pPyY 97 fastic 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 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 Qprofile def test data np random random 5000 3 00 r Sy V linalg syd data poa MEE dTor E T SI aaea results fastica pca T whiten False Then we run the script using the kernprof py program with switches and v kernprol py L v demo 5 Wrote profile results to demo py lprof lime Ode bes06 46 File demo py Bunctuoons test st line 5 More domes 4 27 S us Isa Wer er HLCS Mme Rer AIE 5 6 7 1 19015 19015 0 10 2 Profiling Python code Time Line Contents profile def test DNE data np random random 5000 100 203 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 8 7 SI IE 9 E27 due 99 7 r
315. y Dase iS x True moo y flags C CONTIGUOUS True Fa ONT TEC RLE I S 2 True OWNDATA alse WRITEABLE False ALIGNED gt IRUS DED AE COP D The owndata and writeable flags indicate status of the memory block Indexing scheme strides The question gt gt gt x np array T eZ s br Gl A Lie suc Billy i yvpesmnpsemt e gt gt gt str x data EXO NS 2S 04 05 0G D Oe ET At which byte in x data does the item x 1 2 begin The answer in Numpy strides the number of bytes to jump to find the next element stride per dimension gt gt gt X Strides oe 1 gt gt gt pte otfset Bel a 1 2 m CO T 2 gt gt gt x data byte offset 254054 moo T kegel 6 s simple flexible C and Fortran order gt gt gt x Nnp array II 2 15 AS Gl iy oy Sho Gdeype nplimiloe order C gt gt gt o strsdes 6 2 gt gt gt str x data PX Ol x00 x02 500 x03 00 x04 x00 IDE 00 06 x00 07 00 08 ve Osa Need to jump 6 bytes to find the next row Need to jump 2 bytes to find the next column 3 3 More elaborate arrays 76 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 gt gt gt y np array x order F gt gt gt v S SIS C gt gt gt Str y data RSS T sep ose IDI Se OI A S OIIO LT DHUA DHE A IDI S UTS 00 205 00 A DHS 00 E OD Need to jump 2 bytes to find the next row Need to jump 6 bytes to find the next column e Similarly to higher dimensions C
316. y 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 e 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 e a language very easy to interface with other languages in particular C and C e 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 ht
317. y 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 201 Python Scientific lecture notes Release 2012 3 EuroScipy 2012 10 2 Profiling Python code No optimization without measuring Measure profiling timing You ll have surprises the fastest code is not always what you think 10 2 1 Timeit In IPython use timeit 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 timeit a x 2 100000 loops best of 3 S273 us per loop In 4 Seamer a xx 2 1 L000 loops best or 3 154 us per loop TAS cccmedte se ca 100000 loops best Ot 3 5256 Us per loop Use this to guide your choice between strategies Note For long running calls using t ime instead of t imeit itis 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 Up Sy wv linalg svd data pea ME dor tus cata results fastica pca T whiten False test In Python we can time

Download Pdf Manuals

image

Related Search

Related Contents

MODE D`EMPLOI WEB-TDFC    User Reference Manual  realisation d`un espace numerique d`accompagnement    1/3” CCD Polestar Verdadera Dia/Noche Impermeable  fiche technique Slobber Mash site internet ECOLIT  Piezon Master Surgery® Instruments & Systems  LD MEI1000 - Quadrifonia  Direct Detect User Guide - The University of Sydney  

Copyright © All rights reserved.
Failed to retrieve file