From: <fer...@us...> - 2008-07-07 00:18:00
|
Revision: 5713 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5713&view=rev Author: fer_perez Date: 2008-07-06 17:17:57 -0700 (Sun, 06 Jul 2008) Log Message: ----------- Add agenda for 2008 SciPy tutorial, plus misc. updates to scripts. Modified Paths: -------------- trunk/py4science/examples/fft_imdenoise.py trunk/py4science/examples/glass_dots1.py trunk/py4science/examples/lotka_volterra.py trunk/py4science/examples/numpy_wrap/f2py/example3/Makefile trunk/py4science/examples/skel/fft_imdenoise_skel.py trunk/py4science/examples/skel/fortran_wrap/Makefile trunk/py4science/examples/skel/fortran_wrap/fib3.f trunk/py4science/examples/skel/glass_dots1_skel.py trunk/py4science/examples/stock_demo.py trunk/py4science/examples/stock_records.py trunk/py4science/examples/weave_blitz.py trunk/py4science/examples/weave_examples_simple.py Added Paths: ----------- trunk/py4science/classes/0808_scipy_agenda.txt Added: trunk/py4science/classes/0808_scipy_agenda.txt =================================================================== --- trunk/py4science/classes/0808_scipy_agenda.txt (rev 0) +++ trunk/py4science/classes/0808_scipy_agenda.txt 2008-07-07 00:17:57 UTC (rev 5713) @@ -0,0 +1,94 @@ +========================================================= + Introduction to Scientific Computing in Python - Agenda +========================================================= + +.. contents:: +.. + 1 Introduction and resources + 2 Day 1 +.. + +Introduction and resources +========================== + +While the tutorial will begin with very basic concepts, we will assume that +attendees have given the free online `Python tutorial`_ a very decent read, and +will have installed on their systems all the prerequisite tools. + +.. _`Python tutorial`: http://docs.python.org/tut + +In addition, the following are good sources of information for the tools we'll +be using (all are linked from the main `SciPy documentation`_ page): + + * The `STSci tutorial`_ on interactive data analysis. + * The tentative `NumPy tutorial`_. + * The list of NumPy `functions with examples`_. + * The SciPy community cookbook_. + +.. _`SciPy documentation`: http://www.scipy.org/Documentation +.. _`STSci tutorial`: http://www.scipy.org/wikis/topical_software/Tutorial +.. _`NumPy tutorial`: http://www.scipy.org/Tentative_NumPy_Tutorial +.. _`functions with examples`: http://www.scipy.org/Numpy_Example_List_With_Doc +.. _`cookbook`: http://www.scipy.org/Cookbook + + +Initials indicate who presents what: + + * MD: Michael Droetboom + * PG: Perry Greenfield + * FP: Fernando Perez + + +Day 1 +===== + +* Python for scientific computing: A high-level overview of the topic of Python + in a scientific context ( simple 30 minute talk). + +* Workflow, guided by a simple examples and students typing along. Will show + basics of everyday workflow as we cover the core concepts. + + * Basic scalar types: strings and numbers (int, float, complex). Exercise: + Walli's infinte product formula for Pi. + + * Basic collections: lists and dicts (mention tuples and sets). Exercise: + word frequency counting. + + * Quick review of control flow: if, for, range, while, break, continue. + + * Defining functions. Arguments and docstrings. + + * Reusing your code: every script is a module, '__main__' (notes on module + loading and reloading) + + * Exceptions: a core concept in Python, you really can't use the language + without them. + + * Debugging your programs: + * Ye olde print statement. + * %debug in ipython. + * %run -d in ipython. + * winpdb - a free, cross-platform GUI debugger. + + * Testing your code: reproducible research from the start. Making a habit + out of having auto-validated code. + +* Introduction to NumPy arrays. + * Memory model. + * The dtype concept. + * Creating arrays. + * Basic operations: arithmetic and slicing. + * Indexing modes. Views vs. copies. + * Functions that operate on arrays: the builtins and making your own. + * Saving and reloading arrays on disk. + + Exercises: Trapezoid rule integration. Image denoising using FFTs. + +* Working with data + * Reading files. + * Simple text parsing. + * CSV files. + * Matplotlib's data loader. + + +* Python packages and modules, the very basics: __init__.py and $PYTHONPATH. Modified: trunk/py4science/examples/fft_imdenoise.py =================================================================== --- trunk/py4science/examples/fft_imdenoise.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/fft_imdenoise.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -3,14 +3,13 @@ import sys -import numpy as N -import pylab as P -import scipy as S +import numpy as np +from matplotlib import pyplot as plt def mag_phase(F): """Return magnitude and phase components of spectrum F.""" - return (N.absolute(F), N.angle(F)) + return (np.absolute(F), np.angle(F)) def plot_spectrum(F, amplify=1000): """Normalise, amplify and plot an amplitude spectrum.""" @@ -19,19 +18,19 @@ M[M > 1] = 1 print M.shape, M.dtype - P.imshow(M, P.cm.Blues) + plt.imshow(M, plt.cm.Blues) try: # Read in original image, convert to floating point for further # manipulation; imread returns a MxNx4 RGBA image. Since the # image is grayscale, just extrac the 1st channel - im = P.imread('data/moonlanding.png').astype(float)[:,:,0] + im = plt.imread('data/moonlanding.png').astype(float)[:,:,0] except: print "Could not open image." sys.exit(-1) # Compute the 2d FFT of the input image -F = N.fft.fft2(im) +F = np.fft.fft2(im) # Now, make a copy of the original spectrum and truncate coefficients. keep_fraction = 0.1 @@ -52,27 +51,27 @@ # Reconstruct the denoised image from the filtered spectrum, keep only the real # part for display -im_new = N.fft.ifft2(ff).real +im_new = np.fft.ifft2(ff).real # Show the results -P.figure() +plt.figure() -P.subplot(221) -P.title('Original image') -P.imshow(im, P.cm.gray) +plt.subplot(221) +plt.title('Original image') +plt.imshow(im, plt.cm.gray) -P.subplot(222) -P.title('Fourier transform') +plt.subplot(222) +plt.title('Fourier transform') plot_spectrum(F) -P.subplot(224) -P.title('Filtered Spectrum') +plt.subplot(224) +plt.title('Filtered Spectrum') plot_spectrum(ff) -P.subplot(223) -P.title('Reconstructed Image') -P.imshow(im_new, P.cm.gray) +plt.subplot(223) +plt.title('Reconstructed Image') +plt.imshow(im_new, plt.cm.gray) -P.savefig('fft_imdenoise.png', dpi=150) -P.savefig('fft_imdenoise.eps') -P.show() +plt.savefig('fft_imdenoise.png', dpi=150) +plt.savefig('fft_imdenoise.eps') +plt.show() Modified: trunk/py4science/examples/glass_dots1.py =================================================================== --- trunk/py4science/examples/glass_dots1.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/glass_dots1.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -5,17 +5,12 @@ See L. Glass. 'Moire effect from random dots' Nature 223, 578580 (1969). """ +import cmath from numpy import cos, sin, pi, matrix import numpy as npy import numpy.linalg as linalg from pylab import figure, show -def csqrt(x): - """ - sqrt func that handles returns sqrt(x)j for x<0 - """ - if x<0: return complex(0, npy.sqrt(abs(x))) - else: return npy.sqrt(x) def myeig(M): """ @@ -34,12 +29,13 @@ tau = a+d # the trace delta = a*d-b*c # the determinant - lambda1 = (tau + csqrt(tau**2 - 4*delta))/2. - lambda2 = (tau - csqrt(tau**2 - 4*delta))/2. + lambda1 = (tau + cmath.sqrt(tau**2 - 4*delta))/2. + lambda2 = (tau - cmath.sqrt(tau**2 - 4*delta))/2. return lambda1, lambda2 # 2000 random x,y points in the interval[-0.5 ... 0.5] -X1 = matrix(npy.random.rand(2,2000))-0.5 +X1 = matrix(npy.random.rand(2,2000) + )-0.5 name = 'saddle' sx, sy, angle = 1.05, 0.95, 0. Modified: trunk/py4science/examples/lotka_volterra.py =================================================================== --- trunk/py4science/examples/lotka_volterra.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/lotka_volterra.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -46,7 +46,7 @@ p.figure() -p.plot(r, f) +p.plot(r, f, color='red') p.xlabel('rabbits') p.ylabel('foxes') p.title('phase plane') @@ -65,8 +65,8 @@ dR = dr(R, F) dF = df(R, F) -p.contour(R, F, dR, levels=[0], linewidths=3, colors='black') -p.contour(R, F, dF, levels=[0], linewidths=3, colors='black') +p.contour(R, F, dR, levels=[0], linewidths=3, colors='blue') +p.contour(R, F, dF, levels=[0], linewidths=3, colors='green') p.ylabel('foxes') p.title('trajectory, direction field and null clines') Modified: trunk/py4science/examples/numpy_wrap/f2py/example3/Makefile =================================================================== --- trunk/py4science/examples/numpy_wrap/f2py/example3/Makefile 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/numpy_wrap/f2py/example3/Makefile 2008-07-07 00:17:57 UTC (rev 5713) @@ -1,10 +1,7 @@ -#F2PY=f2py +F2PY=f2py -F2PY=f2py2.4 -#F2PY=/usr/local/txpython/local/Frameworks/Python.framework/Versions/2.5/bin/f2py - clean: rm -f fib3.pyf example.so Modified: trunk/py4science/examples/skel/fft_imdenoise_skel.py =================================================================== --- trunk/py4science/examples/skel/fft_imdenoise_skel.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/skel/fft_imdenoise_skel.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -36,7 +36,7 @@ # channel from the MxNx4 RGBA matrix to represent the grayscale # intensities -F = # Compute the 2d FFT of the input image. Look for a 2-d FFT in N.dft +F = # Compute the 2d FFT of the input image. Look for a 2-d FFT in N.fft. # Define the fraction of coefficients (in each direction) we keep keep_fraction = 0.1 Modified: trunk/py4science/examples/skel/fortran_wrap/Makefile =================================================================== --- trunk/py4science/examples/skel/fortran_wrap/Makefile 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/skel/fortran_wrap/Makefile 2008-07-07 00:17:57 UTC (rev 5713) @@ -1,10 +1,7 @@ -#F2PY=f2py +F2PY=f2py -F2PY=f2py2.4 -#F2PY=/usr/local/txpython/local/Frameworks/Python.framework/Versions/2.5/bin/f2py - clean: rm -f fib3.pyf example.so Modified: trunk/py4science/examples/skel/fortran_wrap/fib3.f =================================================================== --- trunk/py4science/examples/skel/fortran_wrap/fib3.f 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/skel/fortran_wrap/fib3.f 2008-07-07 00:17:57 UTC (rev 5713) @@ -19,7 +19,7 @@ ENDDO END - SUBROUTINE CUMSUM(X, Y, N) +C SUBROUTINE CUMSUM(X, Y, N) C C COMPUTE THE CUMULATIVE SUM OF X C Modified: trunk/py4science/examples/skel/glass_dots1_skel.py =================================================================== --- trunk/py4science/examples/skel/glass_dots1_skel.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/skel/glass_dots1_skel.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -5,15 +5,12 @@ See L. Glass. 'Moire effect from random dots' Nature 223, 578580 (1969). """ +import cmath # provides complex math functions from numpy import cos, sin, pi, matrix import numpy as npy import numpy.linalg as linalg from pylab import figure, show -def csqrt(x): - 'sqrt func that handles returns sqrt(x)j for x<0' - XXX - def myeig(M): """ compute eigen values and eigenvectors analytically Modified: trunk/py4science/examples/stock_demo.py =================================================================== --- trunk/py4science/examples/stock_demo.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/stock_demo.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -38,5 +38,4 @@ # <demo> stop # Now, make a slightly modified version of the file with cleaner formatting. # We'll use this later... -mlab.rec2csv(r,'dap/myserver/data/sample.csv', - formatd={'date':mlab.FormatString()}) +# Modified: trunk/py4science/examples/stock_records.py =================================================================== --- trunk/py4science/examples/stock_records.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/stock_records.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -40,7 +40,7 @@ tickers = 'SPY', 'QQQQ', 'INTC', 'MSFT', 'YHOO', 'GOOG', 'GE', 'WMT', 'AAPL' # we want to compute returns since 2003, so define the start date -startdate = datetime.datetime(2003,1,1) +startdate = datetime.date(2003,1,1) # we'll store a list of each return and ticker for analysis later data = [] # a list of (return, ticker) for each stock Modified: trunk/py4science/examples/weave_blitz.py =================================================================== --- trunk/py4science/examples/weave_blitz.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/weave_blitz.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -5,12 +5,11 @@ import sys, time -import numpy -from numpy import zeros +import numpy as np from scipy import weave -from pylab import subplot, plot, show, legend, xlabel, ylabel, title +from pylab import figure,subplot, plot, show, legend, xlabel, ylabel, title -rand = numpy.random.rand +rand = np.random.rand Nadds = 12 Nevals = 10 @@ -45,7 +44,7 @@ # can disrupt timings if useWeave: # only weave needs to predefine result array - result= zeros(shape,dtype=float) + result= np.empty(shape,dtype=float) times[0] = now() for j in evalRng: blitz(s) @@ -67,9 +66,10 @@ nn, tn = repeat_nadds(Nadds, Nevals, useWeave=False) # plot weave versus Numeric +figure() ax = subplot(111) plot(nw, tw, 'go', nn, tn, 'bs') -legend( ('Weave', 'Numeric') ) +legend( ('Blitz', 'Numpy') ) xlabel('num adds') ylabel('time (s)') title('numpy vs weave; repeated adds, shape: %s' % (shape,)) Modified: trunk/py4science/examples/weave_examples_simple.py =================================================================== --- trunk/py4science/examples/weave_examples_simple.py 2008-07-04 19:20:43 UTC (rev 5712) +++ trunk/py4science/examples/weave_examples_simple.py 2008-07-07 00:17:57 UTC (rev 5713) @@ -1,9 +1,8 @@ #!/usr/bin/env python """Some simple examples of weave.inline use""" +import numpy as np from scipy.weave import inline,converters -import numpy as N -from pylab import rand #----------------------------------------------------------------------------- # Returning a scalar quantity computed from an array. @@ -37,16 +36,49 @@ inline(code,['num','mat','nrow','ncol'], type_converters = converters.blitz) -def main(): - zz = N.zeros([10,10]) +def prod(m, v): + #C++ version + nrows, ncolumns = m.shape + assert v.ndim==1 and ncolumns==v.shape[0],"Shape mismatch in prod" + + res = np.zeros(nrows, float) + code = r""" + for (int i=0; i<nrows; i++) + { + for (int j=0; j<ncolumns; j++) + { + res(i) += m(i,j)*v(j); + } + } + """ + err = inline(code,['nrows', 'ncolumns', 'res', 'm', 'v'], verbose=2, + type_converters=converters.blitz) + return res + + +if __name__=='__main__': + print 'zz is all zeros' + zz = np.zeros([10,10]) print 'tr(zz)=',trace(zz) - oo = N.ones([4,4],N.float) + print 'oo is all ones' + oo = np.ones([4,4],float) print 'tr(oo)=',trace(oo) - aa = rand(128,128) + print 'aa is random' + aa = np.random.rand(128,128) print 'tr(aa)=',trace(aa) + print 'tr(aa)=',np.trace(aa),' (via numpy)' + + print + print 'Modify oo in place:' print 'oo:',oo in_place_mult(3,oo) print '3*oo:',oo -if __name__=='__main__': - main() + print + print 'Simple matrix-vector multiply' + nr,nc = 20,10 + m = np.random.rand(nr,nc) + v = np.random.rand(nc) + mv = prod(m,v) + mvd = np.dot(m,v) + print 'Mat*vec error:',np.linalg.norm(mv-mvd) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |