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