100 \begin{frame}<beamer> |
130 \begin{frame}<beamer> |
101 \frametitle{Outline} |
131 \frametitle{Outline} |
102 \tableofcontents[currentsection,currentsubsection] |
132 \tableofcontents[currentsection,currentsubsection] |
103 \end{frame} |
133 \end{frame} |
104 } |
134 } |
105 |
|
106 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
107 % the following command: |
|
108 %\beamerdefaultoverlayspecification{<+->} |
|
109 |
|
110 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
111 |
|
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
113 % DOCUMENT STARTS |
136 % DOCUMENT STARTS |
114 \begin{document} |
137 \begin{document} |
115 |
138 |
116 \begin{frame} |
139 \begin{frame} |
117 \titlepage |
140 \maketitle |
118 \end{frame} |
141 \end{frame} |
119 |
142 |
120 \begin{frame} |
143 \begin{frame} |
121 \frametitle{Outline} |
144 \frametitle{Outline} |
122 \tableofcontents |
145 \tableofcontents |
123 % You might wish to add the option [pausesections] |
146 % You might wish to add the option [pausesections] |
124 \end{frame} |
147 \end{frame} |
125 |
148 |
126 \section{Functions} |
149 \section{3D Data Visualization} |
127 \subsection{Default arguments} |
150 |
128 \begin{frame}[fragile] |
151 \begin{frame} |
129 \frametitle{Functions: default arguments} |
152 \frametitle{What is visualization?} |
|
153 \Large |
|
154 \begin{center} |
|
155 Visual representation of data |
|
156 \end{center} |
|
157 \end{frame} |
|
158 |
|
159 |
|
160 %% \begin{frame} |
|
161 %% \frametitle{Is this new?} |
|
162 %% \begin{center} |
|
163 %% We have moved from: |
|
164 %% \end{center} |
|
165 %% \begin{columns} |
|
166 %% \column{} |
|
167 %% \hspace*{-1in} |
|
168 %% \includegraphics[width=1.75in,height=1.75in, interpolate=true]{data/3832} |
|
169 %% \column{}\hspace*{-0.25in} |
|
170 %% To |
|
171 %% \column{} |
|
172 %% \hspace*{-1in} |
|
173 %% \includegraphics[width=1.75in, height=1.75in, interpolate=true]{data/torus} |
|
174 %% \end{columns} |
|
175 %% \end{frame} |
|
176 |
|
177 \begin{frame} |
|
178 \frametitle{3D visualization} |
|
179 \Large |
|
180 \begin{center} |
|
181 Harder but important |
|
182 \end{center} |
|
183 \end{frame} |
|
184 |
|
185 \begin{frame} |
|
186 \frametitle{Is this Graphics?} |
|
187 \Large |
|
188 \begin{center} |
|
189 Visualization is about data! |
|
190 \end{center} |
|
191 \end{frame} |
|
192 |
|
193 \begin{frame} |
|
194 \frametitle{Examples: trajectory in space} |
|
195 \Large |
|
196 \begin{center} |
|
197 \pgfimage[width=2.5in]{MEDIA/m2/mlab/plot3d_ex} |
|
198 \end{center} |
|
199 \end{frame} |
|
200 |
|
201 \begin{frame} |
|
202 \frametitle{Examples: Fire in a room} |
|
203 \Large |
|
204 \begin{center} |
|
205 Demo of data |
|
206 \end{center} |
|
207 \inctime{10} |
|
208 \end{frame} |
|
209 |
|
210 \section{Tools available} |
|
211 |
|
212 \subsection{mlab} |
|
213 |
|
214 \begin{frame} |
|
215 {Overview} |
|
216 \Large |
|
217 \begin{itemize} |
|
218 \item Simple |
|
219 \item Convenient |
|
220 \item Full-featured |
|
221 \end{itemize} |
|
222 \end{frame} |
|
223 |
|
224 \begin{frame}[fragile] |
|
225 |
|
226 \frametitle{Getting started} |
|
227 \myemph{\Large Vanilla:} |
|
228 \begin{lstlisting}[language=bash] |
|
229 $ ipython -wthread |
|
230 \end{lstlisting} |
|
231 \myemph{\Large with Pylab:} |
|
232 \begin{lstlisting}[language=bash] |
|
233 $ ipython -pylab -wthread |
|
234 \end{lstlisting} |
|
235 \end{frame} |
|
236 |
|
237 \begin{frame}[fragile] |
|
238 \frametitle{Using mlab} |
|
239 |
|
240 \begin{lstlisting} |
|
241 In []:from enthought.mayavi import mlab |
|
242 \end{lstlisting} |
|
243 |
|
244 \vspace*{0.5in} |
|
245 |
|
246 \myemph{\Large Try these} |
|
247 |
|
248 \vspace*{0.25in} |
|
249 |
|
250 \begin{lstlisting} |
|
251 In []: mlab.test_<TAB> |
|
252 In []: mlab.test_contour3d() |
|
253 In []: mlab.test_contour3d?? |
|
254 \end{lstlisting} |
|
255 \end{frame} |
|
256 |
|
257 \begin{frame} |
|
258 {Exploring the view} |
|
259 \begin{columns} |
|
260 \column{0.6\textwidth} |
|
261 \pgfimage[width=3in]{MEDIA/m2/contour3d} |
|
262 \column{0.4\textwidth} |
|
263 \begin{itemize} |
|
264 \item Mouse |
|
265 \item Keyboard |
|
266 \item Toolbar |
|
267 \item Mayavi icon\pgfimage[width=0.2in]{MEDIA/m2/m2_icon} |
|
268 \end{itemize} |
|
269 \end{columns} |
|
270 \end{frame} |
|
271 |
|
272 \begin{frame}[fragile] |
|
273 \frametitle{Clearing the plot area} |
|
274 \PythonCode{In []: mlab.clf()} |
|
275 \end{frame} |
|
276 |
|
277 \begin{frame}[fragile] |
|
278 \frametitle{\mlab\ plotting functions} |
|
279 \begin{columns} |
|
280 \column{0.25\textwidth} |
|
281 \myemph{\Large 0D data} |
|
282 \column{0.5\textwidth} |
|
283 \pgfimage[width=2in]{MEDIA/m2/mlab/points3d_ex} |
|
284 \end{columns} |
|
285 |
|
286 \begin{lstlisting} |
|
287 In []: t = linspace(0, 2*pi, 50) |
|
288 In []: u = cos(t) * pi |
|
289 In []: x, y, z = sin(u), cos(u), sin(t) |
|
290 \end{lstlisting} |
|
291 \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}} |
|
292 \end{frame} |
|
293 |
|
294 \begin{frame} |
|
295 \begin{columns} |
|
296 \column{0.25\textwidth} |
|
297 \myemph{\Large 1D data} |
|
298 \column{0.5\textwidth} |
|
299 \pgfimage[width=2.5in]{MEDIA/m2/mlab/plot3d_ex} |
|
300 \end{columns} |
|
301 \PythonCode{In []: mlab.clf()} |
|
302 \emphbar{\PythonCode{In []: mlab.plot3d(x, y, z, t)}} |
|
303 |
|
304 Plots lines between the points |
|
305 |
|
306 \end{frame} |
|
307 |
|
308 \begin{frame}[fragile] |
|
309 \begin{columns} |
|
310 \column{0.25\textwidth} |
|
311 \myemph{\Large 2D data} |
|
312 \column{0.5\textwidth} |
|
313 \pgfimage[width=2in]{MEDIA/m2/mlab/surf_ex} |
|
314 \end{columns} |
|
315 \begin{lstlisting} |
|
316 In []: x, y = mgrid[-3:3:100j,-3:3:100j] |
|
317 In []: z = sin(x*x + y*y) |
|
318 \end{lstlisting} |
|
319 |
|
320 \emphbar{\PythonCode{In []: mlab.surf(x, y, z)}} |
|
321 |
|
322 \alert{Assumes the points are rectilinear} |
|
323 |
|
324 \end{frame} |
|
325 |
|
326 \begin{frame}[fragile] |
|
327 \frametitle{mgrid} |
130 \begin{lstlisting} |
328 \begin{lstlisting} |
131 In []: greet = 'hello world' |
329 In []: mgrid[0:3,0:3] |
132 |
330 Out[]: |
133 In []: greet.split() |
331 array([[[0, 0, 0], |
134 Out[]: ['hello', 'world'] |
332 [1, 1, 1], |
135 |
333 [2, 2, 2]], |
136 In []: line = 'Rossum, Guido, 54, 46, 55' |
334 |
137 |
335 [[0, 1, 2], |
138 In []: line.split(',') |
336 [0, 1, 2], |
139 Out[]: ['Rossum', ' Guido', ' 54', |
337 [0, 1, 2]]]) |
140 ' 46', ' 55'] |
338 |
141 \end{lstlisting} |
339 In []: mgrid[-1:1:5j] |
142 \end{frame} |
340 Out[]: array([-1., -0.5, 0., 0.5, 1.]) |
143 |
341 \end{lstlisting} |
144 \begin{frame}[fragile] |
342 \end{frame} |
145 \frametitle{Functions: default arguments \ldots} |
343 |
146 \small |
344 \begin{frame}[fragile] |
|
345 \frametitle{Example} |
147 \begin{lstlisting} |
346 \begin{lstlisting} |
148 def ask_ok(prompt, complaint='Yes or no!'): |
347 In []: x, y = mgrid[-1:1:5j, -1:1:5j] |
149 while True: |
348 In []: z = x*x + y*y |
150 ok = raw_input(prompt) |
349 |
151 if ok in ('y', 'ye', 'yes'): |
350 In []: z |
152 return True |
351 Out[]: |
153 if ok in ('n', 'no', 'nop', |
352 array([[ 2. , 1.25, 1. , 1.25, 2. ], |
154 'nope'): |
353 [ 1.25, 0.5 , 0.25, 0.5 , 1.25], |
155 return False |
354 [ 1. , 0.25, 0. , 0.25, 1. ], |
156 print complaint |
355 [ 1.25, 0.5 , 0.25, 0.5 , 1.25], |
157 |
356 [ 2. , 1.25, 1. , 1.25, 2. ]]) |
158 ask_ok('?') |
|
159 ask_ok('?', '[Y/N]') |
|
160 \end{lstlisting} |
|
161 \end{frame} |
|
162 |
|
163 \subsection{Keyword arguments} |
|
164 \begin{frame}[fragile] |
|
165 \frametitle{Functions: Keyword arguments} |
|
166 We have seen the following |
|
167 \begin{lstlisting} |
|
168 In []: legend(['sin(2y)'], |
|
169 loc='center') |
|
170 In []: plot(y, sin(y), 'g', |
|
171 linewidth=2) |
|
172 In []: annotate('local max', |
|
173 xy=(1.5, 1)) |
|
174 In []: pie(science.values(), |
|
175 labels=science.keys()) |
|
176 \end{lstlisting} |
|
177 \end{frame} |
|
178 |
|
179 \begin{frame}[fragile] |
|
180 \frametitle{Functions: keyword arguments \ldots} |
|
181 \small |
|
182 \begin{lstlisting} |
|
183 def ask_ok(prompt, complaint='Yes or no!'): |
|
184 while True: |
|
185 ok = raw_input(prompt) |
|
186 if ok in ('y', 'ye', 'yes'): |
|
187 return True |
|
188 if ok in ('n', 'no', 'nop', |
|
189 'nope'): |
|
190 return False |
|
191 print complaint |
|
192 |
|
193 ask_ok(prompt='?') |
|
194 ask_ok(prompt='?', complaint='[y/n]') |
|
195 ask_ok(complaint='[y/n]', prompt='?') |
|
196 \end{lstlisting} |
357 \end{lstlisting} |
197 \end{frame} |
358 \end{frame} |
198 |
359 |
199 \subsection{Built-in functions} |
360 \begin{frame}[fragile] |
200 \begin{frame} |
361 \myemph{\Large 2D data: \texttt{mlab.mesh}} |
201 {Before writing a function} |
362 \vspace*{0.25in} |
|
363 |
|
364 \emphbar{\PythonCode{In []: mlab.mesh(x, y, z)}} |
|
365 |
|
366 \alert{Points needn't be regular} |
|
367 |
|
368 \vspace*{0.25in} |
|
369 \begin{lstlisting} |
|
370 In []: phi, theta = mgrid[0:pi:20j, |
|
371 ... 0:2*pi:20j] |
|
372 In []: x = sin(phi)*cos(theta) |
|
373 In []: y = sin(phi)*sin(theta) |
|
374 In []: z = cos(phi) |
|
375 In []: mlab.mesh(x, y, z, |
|
376 ... representation= |
|
377 ... 'wireframe') |
|
378 \end{lstlisting} |
|
379 |
|
380 \end{frame} |
|
381 |
|
382 \begin{frame}[fragile] |
|
383 |
|
384 \begin{columns} |
|
385 \column{0.25\textwidth} |
|
386 \myemph{\Large 3D data} |
|
387 \column{0.5\textwidth} |
|
388 \pgfimage[width=1.5in]{MEDIA/m2/mlab/contour3d}\\ |
|
389 \end{columns} |
|
390 \begin{lstlisting} |
|
391 In []: x, y, z = mgrid[-5:5:64j, |
|
392 ... -5:5:64j, |
|
393 ... -5:5:64j] |
|
394 In []: mlab.contour3d(x*x*0.5 + y*y + |
|
395 z*z*2) |
|
396 \end{lstlisting} |
|
397 \end{frame} |
|
398 |
|
399 \begin{frame}[fragile] |
|
400 |
|
401 \myemph{\Large 3D vector data: \PythonCode{mlab.quiver3d}} |
|
402 \vspace*{0.25in} |
|
403 |
|
404 \pgfimage[width=2in]{MEDIA/m2/mlab/quiver3d_ex}\\ |
|
405 |
|
406 \begin{lstlisting} |
|
407 In []: mlab.test_quiver3d() |
|
408 \end{lstlisting} |
|
409 |
|
410 \emphbar{\PythonCode{obj = mlab.quiver3d(x, y, z, u, v, w)}} |
|
411 \inctime{20} |
|
412 \end{frame} |
|
413 |
|
414 |
|
415 \subsection{Mayavi2} |
|
416 |
|
417 \begin{frame} |
|
418 \frametitle{Introduction to Mayavi} |
202 \begin{itemize} |
419 \begin{itemize} |
203 \item Variety of builtin functions are available |
420 \item Most scientists not interested in details of visualization |
204 \item \typ{abs, any, all, len, max, min} |
421 \item Visualization of data files with a nice UI |
205 \item \typ{pow, range, sum, type} |
422 \item Interactive visualization of data (think Matlab) |
206 \item Refer here: |
423 \item Embedding visualizations in applications |
207 \url{http://docs.python.org/library/functions.html} |
424 \item Customization |
208 \end{itemize} |
|
209 \inctime{10} |
|
210 \end{frame} |
|
211 |
|
212 \subsection{Exercises} |
|
213 \begin{frame}{Problem set 3: Problem 3.1} |
|
214 Write a function to return the gcd of two numbers. |
|
215 \end{frame} |
|
216 |
|
217 \begin{frame}{Problem 3.2} |
|
218 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\ |
|
219 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive. |
|
220 \end{frame} |
|
221 |
|
222 \begin{frame}{Problem 3.3} |
|
223 Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).} |
|
224 \inctime{15} |
|
225 \end{frame} |
|
226 |
|
227 \section{Modules} |
|
228 \begin{frame}[fragile] |
|
229 \frametitle{\typ{from} \ldots \typ{import} magic} |
|
230 \begin{lstlisting} |
|
231 from scipy.interpolate import splrep |
|
232 from scipy.interpolate import splev |
|
233 |
|
234 from scipy.integrate import quad |
|
235 from scipy.integrate import odeint |
|
236 |
|
237 from scipy.optimize import fsolve |
|
238 \end{lstlisting} |
|
239 \emphbar{All the above statements import one function into your namespace} |
|
240 \end{frame} |
|
241 |
|
242 \begin{frame}[fragile] |
|
243 \frametitle{Running scripts from command line} |
|
244 \small |
|
245 \begin{itemize} |
|
246 \item Start cmd |
|
247 \item cd to Desktop |
|
248 \item python sine\_plot.py |
|
249 \end{itemize} |
425 \end{itemize} |
250 \pause |
426 \pause |
|
427 \begin{block}{The Goal} |
|
428 Provide a \alert{flexible} library/app for all of these needs! |
|
429 \end{block} |
|
430 \end{frame} |
|
431 |
|
432 \begin{frame} |
|
433 {Overview of features} |
|
434 \vspace*{-0.3in} |
|
435 \begin{center} |
|
436 \hspace*{-0.2in}\pgfimage[width=5in]{MEDIA/m2/m2_app3_3} |
|
437 \end{center} |
|
438 \end{frame} |
|
439 |
|
440 |
|
441 \begin{frame} |
|
442 \frametitle{Mayavi in applications} |
|
443 \vspace*{-0.3in} |
|
444 \begin{center} |
|
445 \hspace*{-0.2in}\pgfimage[width=4.5in]{MEDIA/m2/m2_envisage} |
|
446 \end{center} |
|
447 \end{frame} |
|
448 |
|
449 \begin{frame} |
|
450 \frametitle{Live in your dialogs} |
|
451 \vspace*{0.1in} |
|
452 \begin{center} |
|
453 \hspace*{-0.2in}\pgfimage[width=2.5in]{MEDIA/m2/mlab_tui} |
|
454 \end{center} |
|
455 \end{frame} |
|
456 |
|
457 \begin{frame} |
|
458 {Exploring the documentation} |
|
459 \begin{center} |
|
460 \pgfimage[width=4in]{MEDIA/m2/m2_ug_doc} |
|
461 \end{center} |
|
462 \end{frame} |
|
463 |
|
464 |
|
465 \begin{frame} |
|
466 \frametitle{Summary} |
|
467 \begin{itemize} |
|
468 \item \url{http://code.enthought.com/projects/mayavi} |
|
469 \item Uses VTK (\url{www.vtk.org}) |
|
470 \item BSD license |
|
471 \item Linux, win32 and Mac OS X |
|
472 \item Highly scriptable |
|
473 \item Embed in Traits UIs (wxPython and PyQt4) |
|
474 \item Envisage Plugins |
|
475 \item Debian/Ubuntu/Fedora |
|
476 \item \alert{Pythonic} |
|
477 \end{itemize} |
|
478 |
|
479 \inctime{10} |
|
480 |
|
481 \end{frame} |
|
482 |
|
483 \begin{frame} |
|
484 {Getting hands dirty!} |
|
485 |
|
486 \begin{block}{Motivational problem} |
|
487 Atmospheric data of temperature over the surface of the earth. |
|
488 Let temperature ($T$) vary linearly with height ($z$): |
|
489 \begin{center} |
|
490 $T = 288.15 - 6.5z$ |
|
491 \end{center} |
|
492 \end{block} |
|
493 \end{frame} |
|
494 |
|
495 \begin{frame}[fragile] |
|
496 \frametitle{Simple solution} |
|
497 |
|
498 \begin{lstlisting} |
|
499 lat = linspace(-89, 89, 37) |
|
500 lon = linspace(0, 360, 37) |
|
501 z = linspace(0, 100, 11) |
|
502 \end{lstlisting} |
|
503 \pause |
|
504 \begin{lstlisting} |
|
505 x, y, z = mgrid[0:360:37j,-89:89:37j, |
|
506 0:100:11j] |
|
507 t = 288.15 - 6.5*z |
|
508 mlab.contour3d(x, y, z, t) |
|
509 mlab.outline() |
|
510 mlab.colorbar() |
|
511 \end{lstlisting} |
|
512 \end{frame} |
|
513 |
|
514 \begin{frame}[fragile] |
|
515 \frametitle{Exercise: Lorenz equation} |
|
516 \begin{columns} |
|
517 \column{0.25\textwidth} |
|
518 \begin{eqnarray*} |
|
519 \frac{d x}{dt} &=& s (y-x)\\ |
|
520 \frac{d y}{d t} &=& rx -y -xz\\ |
|
521 \frac{d z}{d t} &=& xy - bz\\ |
|
522 \end{eqnarray*} |
|
523 \column{0.25\textwidth} |
|
524 Let $s=10,$ |
|
525 $r=28,$ |
|
526 $b=8./3.$ |
|
527 \end{columns} |
|
528 \structure{\Large Region of interest} |
251 \begin{lstlisting} |
529 \begin{lstlisting} |
252 Traceback (most recent call last): |
530 x, y, z = mgrid[-50:50:20j,-50:50:20j, |
253 File "sine_plot.py", line 1, in <module> |
531 -10:60:20j] |
254 x = linspace(-5*pi, 5*pi, 500) |
|
255 NameError: name 'linspace' is not defined |
|
256 \end{lstlisting} |
532 \end{lstlisting} |
257 \end{frame} |
533 \inctime{20} |
258 |
534 |
259 \begin{frame}[fragile] |
535 \end{frame} |
260 \frametitle{Remedy} |
536 \begin{frame}[fragile] |
261 \emphbar{Adding what lines to sine\_plot.py makes this program work?} |
537 \frametitle{Solution} |
262 \begin{lstlisting} |
538 \begin{lstlisting} |
263 from scipy import * |
539 def lorenz(x,y,z,s=10.,r=28.,b=8./3.): |
|
540 u = s*(y-x) |
|
541 v = r*x-y-x*z |
|
542 w = x*y-b*z |
|
543 return u,v,w |
|
544 x,y,z = mgrid [-50:50:20j,-50:50:20j, |
|
545 -10:60:20j ] |
|
546 u,v,w = lorenz( x , y , z ) |
|
547 # Your plot here |
|
548 # |
|
549 mlab.show() |
|
550 |
264 \end{lstlisting} |
551 \end{lstlisting} |
265 \alert{Now run python sine\_plot.py again!} |
552 \end{frame} |
266 \pause |
553 |
267 \begin{lstlisting} |
554 \begin{frame} |
268 Traceback (most recent call last): |
555 \frametitle{We have covered:} |
269 File "sine_plot.py", line 4, in <module> |
|
270 plot(x, x, 'b') |
|
271 NameError: name 'plot' is not defined |
|
272 \end{lstlisting} |
|
273 \end{frame} |
|
274 |
|
275 \begin{frame}[fragile] |
|
276 \frametitle{Remedy \ldots} |
|
277 \emphbar{What should we add now?} |
|
278 \begin{lstlisting} |
|
279 from pylab import * |
|
280 \end{lstlisting} |
|
281 \alert{Now run python sine\_plot.py again!!} |
|
282 \end{frame} |
|
283 |
|
284 \begin{frame}[fragile] |
|
285 \frametitle{Modules} |
|
286 \begin{itemize} |
556 \begin{itemize} |
287 \item The \kwrd{import} keyword ``loads'' a module |
557 \item Need of visualization. |
288 \item One can also use: |
558 \item Using mlab to create 3 D plots. |
289 \begin{lstlisting} |
559 \item Mayavi Toolkit. |
290 In []: from scipy import * |
|
291 In []: from scipy import linspace |
|
292 \end{lstlisting} |
|
293 \item What is the difference? |
|
294 \item \alert{Use the former only in interactive mode} |
|
295 \end{itemize} |
|
296 \end{frame} |
|
297 |
|
298 \begin{frame}[fragile] |
|
299 \frametitle{Package hierarchies} |
|
300 \begin{lstlisting} |
|
301 from scipy.interpolate import splev |
|
302 |
|
303 from scipy.integrate import quad |
|
304 |
|
305 from scipy.optimize import fsolve |
|
306 \end{lstlisting} |
|
307 \end{frame} |
|
308 |
|
309 \begin{frame}[fragile] |
|
310 \frametitle{from \ldots import in a conventional way!} |
|
311 \small |
|
312 \begin{lstlisting} |
|
313 from scipy import linspace, pi, sin |
|
314 from pylab import plot, legend, annotate |
|
315 from pylab import xlim, ylim |
|
316 |
|
317 x = linspace(-5*pi, 5*pi, 500) |
|
318 plot(x, x, 'b') |
|
319 plot(x, -x, 'b') |
|
320 plot(x, sin(x), 'g', linewidth=2) |
|
321 plot(x, x*sin(x), 'r', linewidth=3) |
|
322 legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
|
323 annotate('origin', xy = (0, 0)) |
|
324 xlim(-5*pi, 5*pi) |
|
325 ylim(-5*pi, 5*pi) |
|
326 \end{lstlisting} |
|
327 \end{frame} |
|
328 |
|
329 \begin{frame}[fragile] |
|
330 \frametitle{from \ldots import in a conventional way!} |
|
331 \small |
|
332 \begin{lstlisting} |
|
333 import scipy |
|
334 import pylab |
|
335 |
|
336 x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500) |
|
337 pylab.plot(x, x, 'b') |
|
338 pylab.plot(x, -x, 'b') |
|
339 pylab.plot(x, scipy.sin(x), 'g', linewidth=2) |
|
340 pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3) |
|
341 pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
|
342 pylab.annotate('origin', xy = (0, 0)) |
|
343 pylab.xlim(-5*scipy.pi, 5*scipy.pi) |
|
344 pylab.ylim(-5*scipy.pi, 5*scipy.pi) |
|
345 \end{lstlisting} |
|
346 \end{frame} |
|
347 |
|
348 \begin{frame} |
|
349 \frametitle{Modules: Standard library} |
|
350 \begin{itemize} |
|
351 \item Very powerful, ``Batteries included'' |
|
352 \item Some standard modules: |
|
353 \begin{itemize} |
|
354 \item Math: \typ{math}, \typ{random} |
|
355 \item Internet access: \typ{urllib2}, \typ{smtplib} |
|
356 \item System, Command line arguments: \typ{sys} |
|
357 \item Operating system interface: \typ{os} |
|
358 \item Regular expressions: \typ{re} |
|
359 \item Compression: \typ{gzip}, \typ{zipfile}, and \typ{tarfile} |
|
360 \item And a whole lot more! |
|
361 \end{itemize} |
|
362 \item Check out the Python Library reference: |
|
363 \url{http://docs.python.org/library/} |
|
364 \end{itemize} |
560 \end{itemize} |
365 \inctime{5} |
|
366 \end{frame} |
|
367 |
|
368 \begin{frame}[fragile] |
|
369 \frametitle{Modules of special interest} |
|
370 \begin{description}[matplotlibfor2d] |
|
371 \item[\typ{pylab}] Easy, interactive, 2D plotting |
|
372 |
|
373 \item[\typ{scipy}] arrays, statistics, optimization, integration, linear |
|
374 algebra, Fourier transforms, signal and image processing, |
|
375 genetic algorithms, ODE solvers, special functions, and more |
|
376 |
|
377 \item[\typ{Mayavi}] Easy, interactive, 3D plotting |
|
378 \end{description} |
|
379 \end{frame} |
|
380 |
|
381 \section{Objects} |
|
382 \begin{frame}{Everything is an Object!} |
|
383 \begin{itemize} |
|
384 \item \typ{int} |
|
385 \item \typ{float} |
|
386 \item \typ{str} |
|
387 \item \typ{list} |
|
388 \item \typ{tuple} |
|
389 \item \typ{string} |
|
390 \item \typ{dictionary} |
|
391 \item \typ{function} |
|
392 \item User defined class is also an object! |
|
393 \end{itemize} |
|
394 \end {frame} |
|
395 |
|
396 \begin{frame}[fragile] |
|
397 \frametitle{Using Objects} |
|
398 \begin{itemize} |
|
399 \item Creating Objects |
|
400 \begin{itemize} |
|
401 \item Initialization |
|
402 \end{itemize} |
|
403 \begin{lstlisting} |
|
404 In []: a = str() |
|
405 |
|
406 In []: b = "Hello World" |
|
407 \end{lstlisting} |
|
408 \item Object Manipulation |
|
409 \begin{itemize} |
|
410 \item Object methods |
|
411 \item ``.'' operator |
|
412 \end{itemize} |
|
413 \begin{lstlisting} |
|
414 In []: "Hello World".split() |
|
415 Out[]: ['Hello', 'World'] |
|
416 \end{lstlisting} |
|
417 \end{itemize} |
|
418 \end{frame} |
|
419 |
|
420 \begin{frame}[fragile] |
|
421 \frametitle{Objects provide consistency} |
|
422 \small |
|
423 \begin{lstlisting} |
|
424 for element in (1, 2, 3): |
|
425 print element |
|
426 for key in {'one':1, 'two':2}: |
|
427 print key |
|
428 for char in "123": |
|
429 print char |
|
430 for line in open("myfile.txt"): |
|
431 print line |
|
432 for line in urllib2.urlopen('http://site.com'): |
|
433 print line |
|
434 \end{lstlisting} |
|
435 \inctime{10} |
|
436 \end{frame} |
|
437 |
|
438 \begin{frame} |
|
439 \frametitle{What did we learn?} |
|
440 \begin{itemize} |
|
441 \item Functions: Default and Keyword arguments |
|
442 \item Modules |
|
443 \item Objects |
|
444 \end{itemize} |
|
445 \end{frame} |
561 \end{frame} |
446 |
562 |
447 \end{document} |
563 \end{document} |
|
564 |