day2/session3.tex
changeset 298 df494695e061
parent 294 f05b1c457120
child 300 f87f2a310abe
equal deleted inserted replaced
294:f05b1c457120 298:df494695e061
     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]{\lstinline{#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: Functions, modules and objects}
    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[] {8 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   \begin{lstlisting}
       
   147 In []: def welcome(greet, name="World"):
       
   148   ....     print greet, name
       
   149 
       
   150 In []: welcome("Hello")
       
   151 Hello World
       
   152 
       
   153 In []: welcome("Hi", "Guido")
       
   154 Hi Guido
       
   155   \end{lstlisting}
       
   156 \end{frame} 
       
   157 
       
   158 \subsection{Keyword arguments}
       
   159 \begin{frame}[fragile]
       
   160   \frametitle{Functions: Keyword arguments}
       
   161 We have seen the following
       
   162 \begin{lstlisting}
       
   163 In []: legend(['sin(2y)'], 
       
   164                  loc = 'center')
       
   165 
       
   166 In []: plot(y, sin(y), 'g',
       
   167                  linewidth = 2)
       
   168 
       
   169 In []: annotate('local max',
       
   170                  xy = (1.5, 1))
       
   171 
       
   172 In []: pie(science.values(),
       
   173             labels = science.keys())
       
   174   \end{lstlisting}
       
   175 \end{frame}
       
   176 
       
   177 \begin{frame}[fragile]
       
   178   \frametitle{Functions: keyword arguments \ldots}
       
   179   \begin{lstlisting}
       
   180 In []: def welcome(greet, name="World"):
       
   181   ....     print greet, name
       
   182 
       
   183 In []: welcome("Hello", "James")
       
   184 Hello James
       
   185 
       
   186 In []: welcome("Hi", name="Guido")
       
   187 Hi Guido
       
   188 
       
   189 In []: welcome(name="Guido", greet="Hey")
       
   190 Hey Guido
       
   191   \end{lstlisting}
       
   192 \end{frame}
       
   193 
       
   194 \subsection{Built-in functions}
   151 \begin{frame}
   195 \begin{frame}
   152     \frametitle{What is visualization?}
   196   {Before writing a function}
   153     \Large
   197   \begin{itemize}
   154     \begin{center}
   198       \item Variety of built-in functions are available
   155     Visual representation of data
   199       \item \typ{abs, any, all, len, max, min}
   156     \end{center}
   200       \item \typ{pow, range, sum, type}
   157 \end{frame}
   201       \item Refer here:
   158 
   202           \url{http://docs.python.org/library/functions.html}
   159 
   203   \end{itemize}
   160 %% \begin{frame}
   204   \inctime{10} 
   161 %%     \frametitle{Is this new?}    
   205 \end{frame}
   162 %%     \begin{center}
   206 
   163 %%     We have moved from:
   207 \subsection{Exercises}
   164 %%     \end{center}
   208 \begin{frame}{Problem set 3: Problem 3.1}
   165 %%     \begin{columns}
   209   Write a function to return the gcd of two numbers.
   166 %%     \column{}
   210 \end{frame}
   167 %%     \hspace*{-1in}    
   211 
   168 %%     \includegraphics[width=1.75in,height=1.75in, interpolate=true]{data/3832}      
   212 \begin{frame}{Problem 3.2}
   169 %%     \column{}\hspace*{-0.25in}
   213 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
   170 %%     To
   214 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{}
   215 \end{frame}
   172 %%     \hspace*{-1in}
   216 
   173 %%     \includegraphics[width=1.75in, height=1.75in, interpolate=true]{data/torus}  
   217 \begin{frame}{Problem 3.3}
   174 %%     \end{columns}
   218   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}
   219 
       
   220 \inctime{15}
       
   221 \end{frame}
       
   222 
       
   223 \section{Modules}
       
   224 \begin{frame}[fragile]
       
   225   \frametitle{\texttt{from} \ldots \texttt{import} magic}
       
   226   \begin{lstlisting}
       
   227 from scipy.integrate import odeint
       
   228 
       
   229 from scipy.optimize import fsolve
       
   230   \end{lstlisting}
       
   231 \emphbar{Above statements import a function to our namespace}
       
   232 \end{frame}
       
   233 
       
   234 \begin{frame}[fragile]
       
   235   \frametitle{Running scripts from command line}
       
   236   \small
       
   237   \begin{itemize}
       
   238     \item Fire up a terminal
       
   239     \item python four\_plot.py
       
   240   \end{itemize}
       
   241   \pause
       
   242   \begin{lstlisting}
       
   243 Traceback (most recent call last):
       
   244   File "four_plot.py", line 1, in <module>
       
   245     x = linspace(-5*pi, 5*pi, 500)
       
   246 NameError: name 'linspace' is not defined
       
   247   \end{lstlisting}
       
   248 \end{frame}
       
   249 
       
   250 \begin{frame}[fragile]
       
   251   \frametitle{Remedy}
       
   252   \begin{lstlisting}
       
   253 from scipy import *
       
   254   \end{lstlisting}
       
   255 \alert{Now run python four\_plot.py again!}
       
   256   \pause
       
   257   \begin{lstlisting}
       
   258 Traceback (most recent call last):
       
   259   File "four_plot.py", line 4, in <module>
       
   260     plot(x, x, 'b')
       
   261 NameError: name 'plot' is not defined
       
   262   \end{lstlisting}
       
   263 \end{frame}
       
   264 
       
   265 \begin{frame}[fragile]
       
   266   \frametitle{Remedy \ldots}
       
   267   \begin{lstlisting}
       
   268 from pylab import *
       
   269   \end{lstlisting}
       
   270 \alert{Now run python four\_plot.py again!!}
       
   271 \end{frame}
       
   272 
       
   273 \begin{frame}[fragile]
       
   274   \frametitle{Modules}
       
   275   \begin{itemize}
       
   276     \item The \kwrd{import} keyword ``loads'' a module
       
   277     \item One can also use:
       
   278       \begin{lstlisting}
       
   279 In []: from scipy import *
       
   280 In []: from scipy import linspace
       
   281       \end{lstlisting}    
       
   282     \item What is the difference?
       
   283     \item \alert{Use the former only in interactive mode}
       
   284     \end{itemize}
       
   285 \end{frame}
       
   286 
       
   287 \begin{frame}[fragile]
       
   288   \frametitle{Package hierarchies}
       
   289   \begin{lstlisting}
       
   290 from scipy.integrate import odeint
       
   291 
       
   292 from scipy.optimize import fsolve
       
   293   \end{lstlisting}
       
   294 \end{frame}
       
   295 
       
   296 \begin{frame}[fragile]
       
   297   \frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
       
   298   \small
       
   299   \begin{lstlisting}
       
   300 from scipy import linspace, pi, sin
       
   301 from pylab import plot, legend, annotate
       
   302 from pylab import xlim, ylim
       
   303 
       
   304 x = linspace(-5*pi, 5*pi, 500)
       
   305 plot(x, x, 'b')
       
   306 plot(x, -x, 'b')
       
   307 plot(x, sin(x), 'g', linewidth=2)
       
   308 plot(x, x*sin(x), 'r', linewidth=3)
       
   309 legend(['x', '-x', 'sin(x)', 'xsin(x)'])
       
   310 annotate('origin', xy = (0, 0))
       
   311 xlim(-5*pi, 5*pi)
       
   312 ylim(-5*pi, 5*pi)
       
   313   \end{lstlisting}
       
   314 \end{frame}
       
   315 
       
   316 \begin{frame}[fragile]
       
   317   \frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
       
   318   \small
       
   319   \begin{lstlisting}
       
   320 import scipy
       
   321 import pylab
       
   322 
       
   323 x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
       
   324 pylab.plot(x, x, 'b')
       
   325 pylab.plot(x, -x, 'b')
       
   326 pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
       
   327 pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
       
   328 pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
       
   329 pylab.annotate('origin', xy = (0, 0))
       
   330 pylab.xlim(-5*scipy.pi, 5*scipy.pi)
       
   331 pylab.ylim(-5*scipy.pi, 5*scipy.pi)
       
   332   \end{lstlisting}
       
   333 \end{frame}
   176 
   334 
   177 \begin{frame}
   335 \begin{frame}
   178     \frametitle{3D visualization}
   336   \frametitle{Modules: Standard library}
   179     \Large
   337   \begin{itemize}
   180     \begin{center}
   338   \item Very powerful, ``Batteries included''
   181         Harder but important
   339   \item Some standard modules:
   182     \end{center}
   340     \begin{itemize}
       
   341     \item Math: \typ{math}, \typ{random}
       
   342     \item Internet access: \typ{urllib2}, \typ{smtplib}
       
   343     \item System, Command line arguments: \typ{sys}
       
   344     \item Operating system interface: \typ{os}
       
   345     \item Regular expressions: \typ{re}
       
   346     \item Compression: \typ{gzip}, \typ{zipfile}, and \typ{tarfile}
       
   347     \item And a whole lot more!
       
   348     \end{itemize}
       
   349   \item Check out the Python Library reference:
       
   350     \url{http://docs.python.org/library/}
       
   351   \end{itemize}
       
   352 \inctime{5}
       
   353 \end{frame}
       
   354 
       
   355 \begin{frame}[fragile]
       
   356   \frametitle{Modules of special interest}
       
   357   \begin{description}[matplotlibfor2d]
       
   358     \item[\texttt{pylab}] Easy, interactive, 2D plotting
       
   359 
       
   360     \item[\texttt{scipy}] arrays, statistics, optimization, integration, linear
       
   361             algebra, Fourier transforms, signal and image processing,
       
   362             genetic algorithms, ODE solvers, special functions, and more
       
   363 
       
   364     \item[\texttt{Mayavi}] Easy, interactive, 3D plotting
       
   365   \end{description}
       
   366 \end{frame}
       
   367 
       
   368 \section{Objects}
       
   369 \begin{frame}{Everything is an Object!}
       
   370   \begin{itemize}
       
   371     \item \typ{int}
       
   372     \item \typ{float}
       
   373     \item \typ{str}
       
   374     \item \typ{list}
       
   375     \item \typ{tuple}
       
   376     \item \typ{string}
       
   377     \item \typ{dictionary}
       
   378     \item \typ{function}
       
   379     \item User defined class is also an object!
       
   380   \end{itemize}
       
   381 \end {frame}
       
   382 
       
   383 \begin{frame}[fragile]
       
   384 \frametitle{Using Objects}
       
   385   \begin{itemize}
       
   386     \item Creating Objects
       
   387     \begin{itemize}
       
   388       \item Initialization
       
   389     \end{itemize}
       
   390     \begin{lstlisting}
       
   391 In []: a = str()
       
   392 
       
   393 In []: b = "Hello World"
       
   394     \end{lstlisting}
       
   395     \item Object Manipulation
       
   396     \begin{itemize}
       
   397       \item Object methods
       
   398       \item ``.'' operator
       
   399     \end{itemize}
       
   400   \begin{lstlisting}
       
   401 In []: "Hello World".split()
       
   402 Out[]: ['Hello', 'World']
       
   403     \end{lstlisting}
       
   404   \end{itemize}
       
   405 \end{frame}
       
   406 
       
   407 \begin{frame}[fragile]
       
   408   \frametitle{Objects provide consistency}
       
   409   \small
       
   410   \begin{lstlisting}
       
   411 for element in (1, 2, 3):
       
   412     print element
       
   413 for key in {'one':1, 'two':2}:
       
   414     print key
       
   415 for char in "123":
       
   416     print char
       
   417 for line in open("myfile.txt"):
       
   418     print line
       
   419 for line in urllib2.urlopen('http://site.com'):
       
   420     print line
       
   421   \end{lstlisting}
       
   422   \inctime{10}
   183 \end{frame}
   423 \end{frame}
   184 
   424 
   185 \begin{frame}
   425 \begin{frame}
   186     \frametitle{Is this Graphics?}
   426   \frametitle{What did we learn?}
   187     \Large
   427   \begin{itemize}
   188     \begin{center}
   428     \item Functions: Default and Keyword arguments
   189         Visualization is about data!
   429     \item Modules
   190     \end{center}
   430     \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{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}
       
   328   \begin{lstlisting}
       
   329 In []: mgrid[0:3,0:3]
       
   330 Out[]: 
       
   331 array([[[0, 0, 0],
       
   332         [1, 1, 1],
       
   333         [2, 2, 2]],
       
   334 
       
   335        [[0, 1, 2],
       
   336         [0, 1, 2],
       
   337         [0, 1, 2]]])
       
   338 
       
   339 In []: mgrid[-1:1:5j]
       
   340 Out[]: array([-1., -0.5,  0.,  0.5,  1.])
       
   341 \end{lstlisting}
       
   342 \end{frame}
       
   343 
       
   344 \begin{frame}[fragile]
       
   345   \frametitle{Example}
       
   346   \begin{lstlisting}
       
   347 In []: x, y = mgrid[-1:1:5j, -1:1:5j]
       
   348 In []: z = x*x + y*y
       
   349 
       
   350 In []: z
       
   351 Out[]: 
       
   352 array([[ 2.  , 1.25, 1.  , 1.25, 2.  ],
       
   353        [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       
   354        [ 1.  , 0.25, 0.  , 0.25, 1.  ],
       
   355        [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       
   356        [ 2.  , 1.25, 1.  , 1.25, 2.  ]])
       
   357 \end{lstlisting}
       
   358 \end{frame}
       
   359 
       
   360 \begin{frame}[fragile]
       
   361     \myemph{\Large 2D data: \texttt{mlab.mesh}}
       
   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}
       
   419   \begin{itemize}
       
   420   \item Most scientists not interested in details of visualization
       
   421   \item Visualization of data files with a nice UI
       
   422   \item Interactive visualization of data (think Matlab)
       
   423   \item Embedding visualizations in applications
       
   424   \item Customization
       
   425   \end{itemize}
       
   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}
       
   529   \begin{lstlisting}
       
   530 x, y, z = mgrid[-50:50:20j,-50:50:20j,
       
   531                 -10:60:20j]
       
   532   \end{lstlisting}
       
   533 \inctime{20}
       
   534 
       
   535 \end{frame}
       
   536 \begin{frame}[fragile]
       
   537     \frametitle{Solution}
       
   538   \begin{lstlisting}
       
   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 
       
   551   \end{lstlisting}
       
   552 \end{frame}
       
   553 
       
   554 \begin{frame}
       
   555   \frametitle{We have covered:}
       
   556   \begin{itemize}
       
   557   \item Need of visualization.
       
   558   \item Using mlab to create 3 D plots.
       
   559   \item Mayavi Toolkit.
       
   560   \end{itemize}
   431   \end{itemize}
   561 \end{frame}
   432 \end{frame}
   562 
   433 
   563 \end{document}
   434 \end{document}
   564