day2/session3.tex
changeset 288 c4e25269a86c
parent 263 8a4a1e5aec85
child 294 f05b1c457120
child 297 a835affb1447
equal deleted inserted replaced
287:d4ad532525a2 288:c4e25269a86c
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     2 % Tutorial slides on Python.
     2 %Tutorial slides on Python.
     3 %
     3 %
     4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
     4 % Author: FOSSEE 
     5 % Copyright (c) 2005-2009, Prabhu Ramachandran
     5 % Copyright (c) 2009, FOSSEE, IIT Bombay
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     7 
     7 
     8 \documentclass[compress,14pt]{beamer}
     8 \documentclass[14pt,compress]{beamer}
     9 % \documentclass[handout]{beamer}
     9 %\documentclass[draft]{beamer}
    10 % \usepackage{pgfpages}
    10 %\documentclass[compress,handout]{beamer}
    11 % \pgfpagesuselayout{4 on 1}[a4paper,border, shrink=5mm,landscape]
    11 %\usepackage{pgfpages} 
    12 \usepackage{tikz}
    12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
    13 \newcommand{\hyperlinkmovie}{}
    13 
    14 %\usepackage{movie15}
    14 % Modified from: generic-ornate-15min-45min.de.tex
    15 
       
    16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    17 % Note that in presentation mode 
       
    18 % \paperwidth  364.19536pt
       
    19 % \paperheight 273.14662pt
       
    20 % h/w = 0.888
       
    21 
       
    22 
       
    23 \mode<presentation>
    15 \mode<presentation>
    24 {
    16 {
    25   \usetheme{Warsaw}
    17   \usetheme{Warsaw}
    26   %\usetheme{Boadilla}
       
    27   %\usetheme{default}
       
    28   \useoutertheme{infolines}
    18   \useoutertheme{infolines}
    29   \setbeamercovered{transparent}
    19   \setbeamercovered{transparent}
    30 }
    20 }
    31 
    21 
    32 % To remove navigation symbols
       
    33 \setbeamertemplate{navigation symbols}{}
       
    34 
       
    35 \usepackage{amsmath}
       
    36 \usepackage[english]{babel}
    22 \usepackage[english]{babel}
    37 \usepackage[latin1]{inputenc}
    23 \usepackage[latin1]{inputenc}
    38 \usepackage{times}
    24 %\usepackage{times}
    39 \usepackage[T1]{fontenc}
    25 \usepackage[T1]{fontenc}
    40 
    26 
    41 % Taken from Fernando's slides.
    27 % Taken from Fernando's slides.
    42 \usepackage{ae,aecompl}
    28 \usepackage{ae,aecompl}
    43 \usepackage{mathpazo,courier,euler}
    29 \usepackage{mathpazo,courier,euler}
    44 \usepackage[scaled=.95]{helvet}
    30 \usepackage[scaled=.95]{helvet}
    45 \usepackage{pgf}
       
    46 
    31 
    47 \definecolor{darkgreen}{rgb}{0,0.5,0}
    32 \definecolor{darkgreen}{rgb}{0,0.5,0}
    48 
    33 
    49 \usepackage{listings}
    34 \usepackage{listings}
    50 \lstset{language=Python,
    35 \lstset{language=Python,
    53   stringstyle=\color{darkgreen},
    38   stringstyle=\color{darkgreen},
    54   showstringspaces=false,
    39   showstringspaces=false,
    55   keywordstyle=\color{blue}\bfseries}
    40   keywordstyle=\color{blue}\bfseries}
    56 
    41 
    57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    58 % My Macros
    43 % Macros
    59 \setbeamercolor{postit}{bg=yellow,fg=black}
       
    60 \setbeamercolor{emphbar}{bg=blue!20, fg=black}
    44 \setbeamercolor{emphbar}{bg=blue!20, fg=black}
    61 \newcommand{\emphbar}[1]
    45 \newcommand{\emphbar}[1]
    62 {\begin{beamercolorbox}[rounded=true]{emphbar} 
    46 {\begin{beamercolorbox}[rounded=true]{emphbar} 
    63       {#1}
    47       {#1}
    64  \end{beamercolorbox}
    48  \end{beamercolorbox}
    65 }
    49 }
    66 %{\centerline{\fcolorbox{gray!50} {blue!10}{
       
    67 %\begin{minipage}{0.9\linewidth}
       
    68 %    {#1} 
       
    69 %\end{minipage}
       
    70 %    }}}
       
    71 
       
    72 \newcommand{\myemph}[1]{\structure{\emph{#1}}}
       
    73 \newcommand{\PythonCode}[1]{\lstinline{#1}}
       
    74 
       
    75 \newcommand{\tvtk}{\texttt{tvtk}}
       
    76 \newcommand{\mlab}{\texttt{mlab}}
       
    77 
       
    78 \newcounter{time}
    50 \newcounter{time}
    79 \setcounter{time}{0}
    51 \setcounter{time}{0}
    80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    81 
    53 
    82 \newcommand\BackgroundPicture[1]{%
    54 \newcommand{\typ}[1]{\texttt{#1}}
    83   \setbeamertemplate{background}{%
    55 
    84       \parbox[c][\paperheight]{\paperwidth}{%
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    85       \vfill \hfill
    57 
    86  \hfill \vfill
    58 %%% This is from Fernando's setup.
    87 }}}
    59 % \usepackage{color}
    88 
    60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
    89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    61 % % Use and configure listings package for nicely formatted code
    90 % Configuring the theme
    62 % \usepackage{listings}
    91 %\setbeamercolor{normal text}{fg=white}
    63 % \lstset{
    92 %\setbeamercolor{background canvas}{bg=black}
    64 %    language=Python,
    93 
    65 %    basicstyle=\small\ttfamily,
       
    66 %    commentstyle=\ttfamily\color{blue},
       
    67 %    stringstyle=\ttfamily\color{orange},
       
    68 %    showstringspaces=false,
       
    69 %    breaklines=true,
       
    70 %    postbreak = \space\dots
       
    71 % }
    94 
    72 
    95 
    73 
    96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    97 % Title page
    75 % Title page
    98 \title[3D Plotting]{3D data Visualization}
    76 \title[Basic Python]{Python language: Data structures and functions}
    99 
    77 
   100 \author[FOSSEE] {FOSSEE}
    78 \author[FOSSEE Team] {The FOSSEE Group}
   101 
    79 
   102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
   103 \date[] {1 November, 2009\\Day 2, Session 5}
    81 \date[] {8 November, 2009\\Day 2, Session 3}
   104 
       
   105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   106 
    83 
   107 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
   108 %\logo{\pgfuseimage{iitblogo}}
    85 %\logo{\pgfuseimage{iitmlogo}}
   109 
    86 
   110 \AtBeginSection[]
       
   111 {
       
   112   \begin{frame}<beamer>
       
   113     \frametitle{Outline}      
       
   114     \tableofcontents[currentsection,currentsubsection]
       
   115   \end{frame}
       
   116 }
       
   117 
    87 
   118 %% Delete this, if you do not want the table of contents to pop up at
    88 %% Delete this, if you do not want the table of contents to pop up at
   119 %% the beginning of each subsection:
    89 %% the beginning of each subsection:
   120 \AtBeginSubsection[]
    90 \AtBeginSubsection[]
   121 {
    91 {
   130   \begin{frame}<beamer>
   100   \begin{frame}<beamer>
   131     \frametitle{Outline}
   101     \frametitle{Outline}
   132     \tableofcontents[currentsection,currentsubsection]
   102     \tableofcontents[currentsection,currentsubsection]
   133   \end{frame}
   103   \end{frame}
   134 }
   104 }
       
   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 
   135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   136 % DOCUMENT STARTS
   113 % DOCUMENT STARTS
   137 \begin{document}
   114 \begin{document}
   138 
   115 
   139 \begin{frame}
   116 \begin{frame}
   140   \maketitle
   117   \titlepage
   141 \end{frame}
   118 \end{frame}
   142 
   119 
   143 \begin{frame}
   120 \begin{frame}
   144   \frametitle{Outline}
   121   \frametitle{Outline}
   145   \tableofcontents
   122   \tableofcontents
   146   % You might wish to add the option [pausesections]
   123   % You might wish to add the option [pausesections]
   147 \end{frame}
   124 \end{frame}
   148 
   125 
   149 \section{3D Data Visualization}
   126 \section{Functions}
   150 
   127 \subsection{Default arguments}
       
   128 \begin{frame}[fragile]
       
   129   \frametitle{Functions: default arguments}
       
   130   \begin{lstlisting}
       
   131 In []: greet = 'hello world'
       
   132 
       
   133 In []: greet.split()
       
   134 Out[]: ['hello', 'world']
       
   135 
       
   136 In []: line = 'Rossum, Guido, 54, 46, 55'
       
   137 
       
   138 In []: line.split(',')
       
   139 Out[]: ['Rossum', ' Guido', ' 54',
       
   140                         ' 46', ' 55']
       
   141   \end{lstlisting}
       
   142 \end{frame}
       
   143 
       
   144 \begin{frame}[fragile]
       
   145   \frametitle{Functions: default arguments \ldots}
       
   146   \small
       
   147   \begin{lstlisting}
       
   148 def ask_ok(prompt, complaint='Yes or no!'):
       
   149     while True:
       
   150         ok = raw_input(prompt)
       
   151         if ok in ('y', 'ye', 'yes'):
       
   152             return True
       
   153         if ok in ('n', 'no', 'nop',
       
   154                             'nope'):
       
   155             return False
       
   156         print complaint
       
   157 
       
   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}
       
   197 \end{frame}
       
   198 
       
   199 \subsection{Built-in functions}
   151 \begin{frame}
   200 \begin{frame}
   152     \frametitle{What is visualization?}
   201   {Before writing a function}
   153     \Large
   202   \begin{itemize}
   154     \begin{center}
   203       \item Variety of builtin functions are available
   155     Visual representation of data
   204       \item \typ{abs, any, all, len, max, min}
   156     \end{center}
   205       \item \typ{pow, range, sum, type}
   157 \end{frame}
   206       \item Refer here:
   158 
   207           \url{http://docs.python.org/library/functions.html}
   159 
   208   \end{itemize}
   160 %% \begin{frame}
   209   \inctime{10} 
   161 %%     \frametitle{Is this new?}    
   210 \end{frame}
   162 %%     \begin{center}
   211 
   163 %%     We have moved from:
   212 \subsection{Exercises}
   164 %%     \end{center}
   213 \begin{frame}{Problem set 3: Problem 3.1}
   165 %%     \begin{columns}
   214   Write a function to return the gcd of two numbers.
   166 %%     \column{}
   215 \end{frame}
   167 %%     \hspace*{-1in}    
   216 
   168 %%     \includegraphics[width=1.75in,height=1.75in, interpolate=true]{data/3832}      
   217 \begin{frame}{Problem 3.2}
   169 %%     \column{}\hspace*{-0.25in}
   218 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
   170 %%     To
   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.
   171 %%     \column{}
   220 \end{frame}
   172 %%     \hspace*{-1in}
   221 
   173 %%     \includegraphics[width=1.75in, height=1.75in, interpolate=true]{data/torus}  
   222 \begin{frame}{Problem 3.3}
   174 %%     \end{columns}
   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).}
   175 %% \end{frame}
   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}
       
   250   \pause
       
   251   \begin{lstlisting}
       
   252 Traceback (most recent call last):
       
   253   File "sine_plot.py", line 1, in <module>
       
   254     x = linspace(-5*pi, 5*pi, 500)
       
   255 NameError: name 'linspace' is not defined
       
   256   \end{lstlisting}
       
   257 \end{frame}
       
   258 
       
   259 \begin{frame}[fragile]
       
   260   \frametitle{Remedy}
       
   261   \emphbar{Adding what lines to sine\_plot.py makes this program work?}
       
   262   \begin{lstlisting}
       
   263 from scipy import *
       
   264   \end{lstlisting}
       
   265 \alert{Now run python sine\_plot.py again!}
       
   266   \pause
       
   267   \begin{lstlisting}
       
   268 Traceback (most recent call last):
       
   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}
       
   287     \item The \kwrd{import} keyword ``loads'' a module
       
   288     \item One can also use:
       
   289       \begin{lstlisting}
       
   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}
   176 
   347 
   177 \begin{frame}
   348 \begin{frame}
   178     \frametitle{3D visualization}
   349   \frametitle{Modules: Standard library}
   179     \Large
   350   \begin{itemize}
   180     \begin{center}
   351   \item Very powerful, ``Batteries included''
   181         Harder but important
   352   \item Some standard modules:
   182     \end{center}
   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}
       
   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}
   183 \end{frame}
   436 \end{frame}
   184 
   437 
   185 \begin{frame}
   438 \begin{frame}
   186     \frametitle{Is this Graphics?}
   439   \frametitle{What did we learn?}
   187     \Large
   440   \begin{itemize}
   188     \begin{center}
   441     \item Functions: Default and Keyword arguments
   189         Visualization is about data!
   442     \item Modules
   190     \end{center}
   443     \item Objects
   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{\mlab\ plotting functions}
       
   274     \begin{columns}
       
   275         \column{0.25\textwidth}
       
   276         \myemph{\Large 0D data}
       
   277         \column{0.5\textwidth}
       
   278     \pgfimage[width=2in]{MEDIA/m2/mlab/points3d_ex}
       
   279     \end{columns}
       
   280 
       
   281     \begin{lstlisting}
       
   282 In []: t = linspace(0, 2*pi, 50)
       
   283 In []: u = cos(t) * pi
       
   284 In []: x, y, z = sin(u), cos(u), sin(t)
       
   285     \end{lstlisting}
       
   286     \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}}
       
   287 \end{frame}
       
   288 
       
   289 \begin{frame}
       
   290   \begin{columns}
       
   291         \column{0.25\textwidth}
       
   292         \myemph{\Large 1D data}
       
   293         \column{0.5\textwidth}
       
   294         \pgfimage[width=2.5in]{MEDIA/m2/mlab/plot3d_ex}
       
   295   \end{columns}
       
   296   \emphbar{\PythonCode{In []: mlab.plot3d(x, y, z, t)}}
       
   297 
       
   298     Plots lines between the points
       
   299     
       
   300 \end{frame}
       
   301 
       
   302 \begin{frame}[fragile]
       
   303     \begin{columns}
       
   304         \column{0.25\textwidth}
       
   305         \myemph{\Large 2D data}
       
   306         \column{0.5\textwidth}
       
   307         \pgfimage[width=2in]{MEDIA/m2/mlab/surf_ex}
       
   308     \end{columns}            
       
   309     \begin{lstlisting}
       
   310 In []: x, y = mgrid[-3:3:100j,-3:3:100j]
       
   311 In []: z = sin(x*x + y*y)
       
   312     \end{lstlisting}
       
   313 
       
   314     \emphbar{\PythonCode{In []: mlab.surf(x, y, z)}}
       
   315 
       
   316     \alert{Assumes the points are rectilinear}
       
   317 
       
   318 \end{frame}
       
   319 
       
   320 \begin{frame}[fragile]
       
   321   \frametitle{mgrid}
       
   322   \begin{lstlisting}
       
   323 In []: mgrid[0:3,0:3]
       
   324 Out[]: 
       
   325 array([[[0, 0, 0],
       
   326         [1, 1, 1],
       
   327         [2, 2, 2]],
       
   328 
       
   329        [[0, 1, 2],
       
   330         [0, 1, 2],
       
   331         [0, 1, 2]]])
       
   332 
       
   333 In []: mgrid[-1:1:5j]
       
   334 Out[]: array([-1., -0.5,  0.,  0.5,  1.])
       
   335 \end{lstlisting}
       
   336 \end{frame}
       
   337 
       
   338 \begin{frame}[fragile]
       
   339   \frametitle{Example}
       
   340   \begin{lstlisting}
       
   341 In []: x, y = mgrid[-1:1:5j, -1:1:5j]
       
   342 In []: z = x*x + y*y
       
   343 
       
   344 In []: z
       
   345 Out[]: 
       
   346 array([[ 2.  , 1.25, 1.  , 1.25, 2.  ],
       
   347        [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       
   348        [ 1.  , 0.25, 0.  , 0.25, 1.  ],
       
   349        [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       
   350        [ 2.  , 1.25, 1.  , 1.25, 2.  ]])
       
   351 \end{lstlisting}
       
   352 \end{frame}
       
   353 
       
   354 \begin{frame}[fragile]
       
   355     \myemph{\Large 2D data: \texttt{mlab.mesh}}
       
   356     \vspace*{0.25in}
       
   357 
       
   358     \emphbar{\PythonCode{In []: mlab.mesh(x, y, z)}}
       
   359 
       
   360     \alert{Points needn't be regular}
       
   361 
       
   362     \vspace*{0.25in}
       
   363 \begin{lstlisting}
       
   364 In []: phi, theta = mgrid[0:pi:20j, 
       
   365 ...                         0:2*pi:20j]
       
   366 In []: x = sin(phi)*cos(theta)
       
   367 In []: y = sin(phi)*sin(theta)
       
   368 In []: z = cos(phi)
       
   369 In []: mlab.mesh(x, y, z, 
       
   370 ...           representation=
       
   371 ...           'wireframe')
       
   372 \end{lstlisting}
       
   373 
       
   374 \end{frame}
       
   375 
       
   376 \begin{frame}[fragile]
       
   377 
       
   378   \begin{columns}
       
   379         \column{0.25\textwidth}
       
   380         \myemph{\Large 3D data}
       
   381         \column{0.5\textwidth}
       
   382         \pgfimage[width=1.5in]{MEDIA/m2/mlab/contour3d}\\        
       
   383     \end{columns}
       
   384 \begin{lstlisting}
       
   385 In []: x, y, z = mgrid[-5:5:64j, 
       
   386 ...                -5:5:64j, 
       
   387 ...                -5:5:64j]
       
   388 In []: mlab.contour3d(x*x*0.5 + y*y + 
       
   389                    z*z*2)
       
   390 \end{lstlisting}
       
   391 \end{frame}
       
   392 
       
   393 \begin{frame}[fragile]
       
   394 
       
   395     \myemph{\Large 3D vector data: \PythonCode{mlab.quiver3d}}
       
   396     \vspace*{0.25in}
       
   397 
       
   398     \pgfimage[width=2in]{MEDIA/m2/mlab/quiver3d_ex}\\
       
   399     
       
   400 \begin{lstlisting}
       
   401 In []: mlab.test_quiver3d()
       
   402 \end{lstlisting}
       
   403 
       
   404 \emphbar{\PythonCode{obj = mlab.quiver3d(x, y, z, u, v, w)}}
       
   405 \inctime{20}
       
   406 \end{frame}
       
   407 
       
   408 
       
   409 \subsection{Mayavi2}
       
   410 
       
   411 \begin{frame}
       
   412   \frametitle{Introduction to Mayavi}
       
   413   \begin{itemize}
       
   414   \item Most scientists not interested in details of visualization
       
   415   \item Visualization of data files with a nice UI
       
   416   \item Interactive visualization of data (think Matlab)
       
   417   \item Embedding visualizations in applications
       
   418   \item Customization
       
   419   \end{itemize}
       
   420   \pause
       
   421   \begin{block}{The Goal}
       
   422       Provide a \alert{flexible} library/app for all of these needs!
       
   423   \end{block}
       
   424 \end{frame}
       
   425 
       
   426 \begin{frame}
       
   427     {Overview of features}
       
   428       \vspace*{-0.3in}
       
   429   \begin{center}    
       
   430     \hspace*{-0.2in}\pgfimage[width=5in]{MEDIA/m2/m2_app3_3}
       
   431   \end{center}    
       
   432 \end{frame}
       
   433 
       
   434 
       
   435 \begin{frame}
       
   436     \frametitle{Mayavi in applications}
       
   437       \vspace*{-0.3in}
       
   438   \begin{center}    
       
   439     \hspace*{-0.2in}\pgfimage[width=4.5in]{MEDIA/m2/m2_envisage}
       
   440   \end{center}
       
   441 \end{frame}
       
   442 
       
   443 \begin{frame}
       
   444     \frametitle{Live in your dialogs}
       
   445       \vspace*{0.1in}
       
   446   \begin{center}    
       
   447     \hspace*{-0.2in}\pgfimage[width=2.5in]{MEDIA/m2/mlab_tui}
       
   448   \end{center}
       
   449 \end{frame}
       
   450 
       
   451 \begin{frame}
       
   452     {Exploring the documentation}
       
   453     \begin{center}
       
   454     \pgfimage[width=4in]{MEDIA/m2/m2_ug_doc}
       
   455     \end{center}
       
   456 \end{frame}
       
   457 
       
   458 
       
   459 \begin{frame}
       
   460   \frametitle{Summary}
       
   461       \begin{itemize}
       
   462           \item \url{http://code.enthought.com/projects/mayavi}
       
   463           \item Uses VTK (\url{www.vtk.org})
       
   464           \item BSD license
       
   465           \item Linux, win32 and Mac OS X
       
   466           \item Highly scriptable
       
   467           \item Embed in Traits UIs (wxPython and PyQt4)
       
   468           \item Envisage Plugins
       
   469           \item Debian/Ubuntu/Fedora
       
   470           \item \alert{Pythonic}
       
   471       \end{itemize}
       
   472     
       
   473       \inctime{10}
       
   474 
       
   475 \end{frame}
       
   476 
       
   477 \begin{frame}
       
   478     {Getting hands dirty!}
       
   479 
       
   480         \begin{block}{Motivational problem}
       
   481         Atmospheric data of temperature over the surface of the earth.
       
   482         Let temperature ($T$) vary linearly with height ($z$):
       
   483         \begin{center}            
       
   484         $T = 288.15 - 6.5z$
       
   485         \end{center}
       
   486         \end{block}
       
   487 \end{frame}
       
   488 
       
   489 \begin{frame}[fragile]
       
   490     \frametitle{Simple solution}
       
   491 
       
   492     \begin{lstlisting}
       
   493 lat = linspace(-89, 89, 37)
       
   494 lon = linspace(0, 360, 37)
       
   495 z = linspace(0, 100, 11)
       
   496     \end{lstlisting}
       
   497 \pause
       
   498     \begin{lstlisting}
       
   499 x, y, z = mgrid[0:360:37j,-89:89:37j,
       
   500                 0:100:11j]
       
   501 t = 288.15 - 6.5*z
       
   502 mlab.contour3d(x, y, z, t)
       
   503 mlab.outline()
       
   504 mlab.colorbar()
       
   505     \end{lstlisting}
       
   506 \end{frame}
       
   507 
       
   508 \begin{frame}[fragile]
       
   509     \frametitle{Exercise: Lorenz equation}
       
   510     \begin{columns}
       
   511         \column{0.25\textwidth}
       
   512         \begin{eqnarray*}
       
   513         \frac{d x}{dt} &=& s (y-x)\\
       
   514         \frac{d y}{d t} &=& rx -y -xz\\
       
   515         \frac{d z}{d t} &=& xy - bz\\
       
   516         \end{eqnarray*}
       
   517         \column{0.25\textwidth}
       
   518         Let $s=10,$
       
   519         $r=28,$ 
       
   520         $b=8./3.$
       
   521     \end{columns}
       
   522     \structure{\Large Region of interest}
       
   523   \begin{lstlisting}
       
   524 x, y, z = mgrid[-50:50:20j,-50:50:20j,
       
   525                 -10:60:20j]
       
   526   \end{lstlisting}
       
   527 \inctime{20}
       
   528 
       
   529 \end{frame}
       
   530 \begin{frame}[fragile]
       
   531     \frametitle{Solution}
       
   532   \begin{lstlisting}
       
   533 def lorenz(x,y,z,s=10.,r=28.,b=8./3.):
       
   534     u = s*(y-x)
       
   535     v = r*x-y-x*z
       
   536     w = x*y-b*z
       
   537     return u,v,w
       
   538 x,y,z = mgrid [-50:50:20j,-50:50:20j,
       
   539                     -10:60:20j ]
       
   540 u,v,w = lorenz( x , y , z )
       
   541 # Your plot here
       
   542 #
       
   543 mlab.show()
       
   544 
       
   545   \end{lstlisting}
       
   546 \end{frame}
       
   547 
       
   548 \begin{frame}
       
   549   \frametitle{We have covered:}
       
   550   \begin{itemize}
       
   551   \item Need of visualization.
       
   552   \item Using mlab to create 3 D plots.
       
   553   \item Mayavi Toolkit.
       
   554   \end{itemize}
   444   \end{itemize}
   555 \end{frame}
   445 \end{frame}
   556 
   446 
   557 \end{document}
   447 \end{document}
   558