day2/session6.tex
changeset 297 a835affb1447
parent 296 2d08c45681a1
child 300 f87f2a310abe
equal deleted inserted replaced
296:2d08c45681a1 297:a835affb1447
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     2 % Tutorial slides on Python.
       
     3 %
       
     4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
       
     5 % Copyright (c) 2005-2009, Prabhu Ramachandran
       
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     7 
       
     8 \documentclass[compress,14pt]{beamer}
       
     9 % \documentclass[handout]{beamer}
       
    10 % \usepackage{pgfpages}
       
    11 % \pgfpagesuselayout{4 on 1}[a4paper,border, shrink=5mm,landscape]
       
    12 \usepackage{tikz}
       
    13 \newcommand{\hyperlinkmovie}{}
       
    14 %\usepackage{movie15}
       
    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>
       
    24 {
       
    25   \usetheme{Warsaw}
       
    26   %\usetheme{Boadilla}
       
    27   %\usetheme{default}
       
    28   \useoutertheme{infolines}
       
    29   \setbeamercovered{transparent}
       
    30 }
       
    31 
       
    32 % To remove navigation symbols
       
    33 \setbeamertemplate{navigation symbols}{}
       
    34 
       
    35 \usepackage{amsmath}
       
    36 \usepackage[english]{babel}
       
    37 \usepackage[latin1]{inputenc}
       
    38 \usepackage{times}
       
    39 \usepackage[T1]{fontenc}
       
    40 
       
    41 % Taken from Fernando's slides.
       
    42 \usepackage{ae,aecompl}
       
    43 \usepackage{mathpazo,courier,euler}
       
    44 \usepackage[scaled=.95]{helvet}
       
    45 \usepackage{pgf}
       
    46 
       
    47 \definecolor{darkgreen}{rgb}{0,0.5,0}
       
    48 
       
    49 \usepackage{listings}
       
    50 \lstset{language=Python,
       
    51     basicstyle=\ttfamily\bfseries,
       
    52     commentstyle=\color{red}\itshape,
       
    53   stringstyle=\color{darkgreen},
       
    54   showstringspaces=false,
       
    55   keywordstyle=\color{blue}\bfseries}
       
    56 
       
    57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    58 % My Macros
       
    59 \setbeamercolor{postit}{bg=yellow,fg=black}
       
    60 \setbeamercolor{emphbar}{bg=blue!20, fg=black}
       
    61 \newcommand{\emphbar}[1]
       
    62 {\begin{beamercolorbox}[rounded=true]{emphbar} 
       
    63       {#1}
       
    64  \end{beamercolorbox}
       
    65 }
       
    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}
       
    79 \setcounter{time}{0}
       
    80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}}
       
    81 
       
    82 \newcommand\BackgroundPicture[1]{%
       
    83   \setbeamertemplate{background}{%
       
    84       \parbox[c][\paperheight]{\paperwidth}{%
       
    85       \vfill \hfill
       
    86  \hfill \vfill
       
    87 }}}
       
    88 
       
    89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    90 % Configuring the theme
       
    91 %\setbeamercolor{normal text}{fg=white}
       
    92 %\setbeamercolor{background canvas}{bg=black}
       
    93 
       
    94 
       
    95 
       
    96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    97 % Title page
       
    98 \title[3D Plotting]{3D data Visualization}
       
    99 
       
   100 \author[FOSSEE] {FOSSEE}
       
   101 
       
   102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
       
   103 \date[] {8 November, 2009\\Day 2, Session 6}
       
   104 
       
   105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   106 
       
   107 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
       
   108 %\logo{\pgfuseimage{iitblogo}}
       
   109 
       
   110 \AtBeginSection[]
       
   111 {
       
   112   \begin{frame}<beamer>
       
   113     \frametitle{Outline}      
       
   114     \tableofcontents[currentsection,currentsubsection]
       
   115   \end{frame}
       
   116 }
       
   117 
       
   118 %% Delete this, if you do not want the table of contents to pop up at
       
   119 %% the beginning of each subsection:
       
   120 \AtBeginSubsection[]
       
   121 {
       
   122   \begin{frame}<beamer>
       
   123     \frametitle{Outline}
       
   124     \tableofcontents[currentsection,currentsubsection]
       
   125   \end{frame}
       
   126 }
       
   127 
       
   128 \AtBeginSection[]
       
   129 {
       
   130   \begin{frame}<beamer>
       
   131     \frametitle{Outline}
       
   132     \tableofcontents[currentsection,currentsubsection]
       
   133   \end{frame}
       
   134 }
       
   135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   136 % DOCUMENT STARTS
       
   137 \begin{document}
       
   138 
       
   139 \begin{frame}
       
   140   \maketitle
       
   141 \end{frame}
       
   142 
       
   143 \begin{frame}
       
   144   \frametitle{Outline}
       
   145   \tableofcontents
       
   146   % You might wish to add the option [pausesections]
       
   147 \end{frame}
       
   148 
       
   149 \section{3D Data Visualization}
       
   150 
       
   151 \begin{frame}
       
   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{\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}
       
   555 \end{frame}
       
   556 
       
   557 \end{document}
       
   558