day2/session4.tex
changeset 288 c4e25269a86c
parent 263 8a4a1e5aec85
child 289 884d42eff66d
equal deleted inserted replaced
287:d4ad532525a2 288:c4e25269a86c
    72 \newcommand{\myemph}[1]{\structure{\emph{#1}}}
    72 \newcommand{\myemph}[1]{\structure{\emph{#1}}}
    73 \newcommand{\PythonCode}[1]{\lstinline{#1}}
    73 \newcommand{\PythonCode}[1]{\lstinline{#1}}
    74 
    74 
    75 \newcommand{\tvtk}{\texttt{tvtk}}
    75 \newcommand{\tvtk}{\texttt{tvtk}}
    76 \newcommand{\mlab}{\texttt{mlab}}
    76 \newcommand{\mlab}{\texttt{mlab}}
    77 \newcommand{\typ}[1]{\lstinline{#1}}
    77 
    78 \newcounter{time}
    78 \newcounter{time}
    79 \setcounter{time}{0}
    79 \setcounter{time}{0}
    80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}}
    80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}}
    81 
    81 
    82 \newcommand\BackgroundPicture[1]{%
    82 \newcommand\BackgroundPicture[1]{%
    93 
    93 
    94 
    94 
    95 
    95 
    96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    97 % Title page
    97 % Title page
    98 \title[Python Development]{Python Development}
    98 \title[3D Plotting]{3D data Visualization}
    99 
    99 
   100 \author[FOSSEE] {FOSSEE}
   100 \author[FOSSEE] {FOSSEE}
   101 
   101 
   102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
   102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
   103 \date[] {1 November, 2009\\Day 2, Session 3}
   103 \date[] {1 November, 2009\\Day 2, Session 5}
       
   104 
   104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   105 
   106 
   106 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
   107 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
   107 %\logo{\pgfuseimage{iitblogo}}
   108 %\logo{\pgfuseimage{iitblogo}}
   108 
   109 
   109 \AtBeginSection[]
   110 \AtBeginSection[]
   110 {
   111 {
   111   \begin{frame}<beamer>
   112   \begin{frame}<beamer>
   112     \frametitle{Outline}
   113     \frametitle{Outline}      
   113       \Large
       
   114     \tableofcontents[currentsection,currentsubsection]
   114     \tableofcontents[currentsection,currentsubsection]
   115   \end{frame}
   115   \end{frame}
   116 }
   116 }
   117 
   117 
   118 %% 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
   138 
   138 
   139 \begin{frame}
   139 \begin{frame}
   140   \maketitle
   140   \maketitle
   141 \end{frame}
   141 \end{frame}
   142 
   142 
   143 \section{Tests: Getting started}
   143 \begin{frame}
   144 \begin{frame}[fragile] 
   144   \frametitle{Outline}
   145   \frametitle{gcd revisited!}
   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}
   146   \begin{itemize}
   413   \begin{itemize}
   147   \item Open gcd.py
   414   \item Most scientists not interested in details of visualization
   148   \end{itemize}  
   415   \item Visualization of data files with a nice UI
   149 \begin{lstlisting}
   416   \item Interactive visualization of data (think Matlab)
   150     def gcd(a, b):
   417   \item Embedding visualizations in applications
   151         if a % b == 0: 
   418   \item Customization
   152             return b
   419   \end{itemize}
   153         return gcd(b, a%b)
   420   \pause
   154 
   421   \begin{block}{The Goal}
   155     print gcd(15, 65)
   422       Provide a \alert{flexible} library/app for all of these needs!
   156     print gcd(16, 76)
   423   \end{block}
   157 \end{lstlisting}
   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:}
   158   \begin{itemize}
   550   \begin{itemize}
   159   \item python gcd.py
   551   \item Need of visualization.
       
   552   \item Using mlab to create 3 D plots.
       
   553   \item Mayavi Toolkit.
   160   \end{itemize}
   554   \end{itemize}
   161 \end{frame}
   555 \end{frame}
   162 
   556 
   163 \begin{frame}[fragile] 
       
   164   \frametitle{Find lcm using our gcd module}
       
   165   \begin{itemize}
       
   166   \item Open lcm.py  
       
   167   \item $lcm = \frac{a*b}{gcd(a,b)}$
       
   168   \end{itemize}  
       
   169 \begin{lstlisting}
       
   170     from gcd import gcd    
       
   171     def lcm(a, b):
       
   172         return (a * b) / gcd(a, b)
       
   173     
       
   174     print lcm(14, 56)
       
   175 \end{lstlisting}
       
   176   \begin{itemize}
       
   177   \item python lcm.py
       
   178   \end{itemize}
       
   179   \begin{lstlisting}
       
   180 5
       
   181 4
       
   182 56
       
   183   \end{lstlisting}    
       
   184 \end{frame}
       
   185 
       
   186 \begin{frame}[fragile] 
       
   187   \frametitle{Writing stand-alone module}  
       
   188 Edit gcd.py file to:
       
   189 \begin{lstlisting}
       
   190     def gcd(a, b):
       
   191         if a % b == 0: 
       
   192             return b
       
   193         return gcd(b, a%b)
       
   194 
       
   195     if __name__ == "__main__":        
       
   196         print gcd(15, 65)
       
   197         print gcd(16, 76)
       
   198 \end{lstlisting}
       
   199   \begin{itemize}
       
   200   \item python gcd.py
       
   201   \item python lcm.py
       
   202   \end{itemize}
       
   203 \end{frame}
       
   204 
       
   205 \begin{frame}[fragile]
       
   206   \frametitle{More use of main}
       
   207   For automating tests.
       
   208   \begin{lstlisting}
       
   209 if __name__ == '__main__':
       
   210     for line in open('numbers.txt'):
       
   211         numbers = line.split()
       
   212         x = int(numbers[0])
       
   213         y = int(numbers[1])
       
   214         result = (int(numbers[2]))
       
   215         assert gcd(x, y) == result
       
   216   \end{lstlisting}  
       
   217 \end{frame}
       
   218 
       
   219 \section{Coding Style}
       
   220 \begin{frame}{Readability and Consistency}
       
   221     \begin{itemize}
       
   222         \item Readability Counts!\\Code is read more often than its written.
       
   223         \item Consistency!
       
   224         \item Know when to be inconsistent.
       
   225       \end{itemize}
       
   226 \end{frame}
       
   227 
       
   228 \begin{frame}[fragile] \frametitle{A question of good style}
       
   229   \begin{lstlisting}
       
   230     amount = 12.68
       
   231     denom = 0.05
       
   232     nCoins = round(amount/denom)
       
   233     rAmount = nCoins * denom
       
   234   \end{lstlisting}
       
   235   \pause
       
   236   \begin{block}{Style Rule \#1}
       
   237     Naming is 80\% of programming
       
   238   \end{block}
       
   239 \end{frame}
       
   240 
       
   241 \begin{frame}[fragile]
       
   242   \frametitle{Code Layout}
       
   243   \begin{itemize}
       
   244         \item Indentation
       
   245         \item Tabs or Spaces??
       
   246         \item Maximum Line Length
       
   247         \item Blank Lines
       
   248         \item Encodings
       
   249    \end{itemize}
       
   250 \end{frame}
       
   251 
       
   252 \begin{frame}{Whitespaces in Expressions}
       
   253   \begin{itemize}
       
   254         \item When to use extraneous whitespaces??
       
   255         \item When to avoid extra whitespaces??
       
   256         \item Use one statement per line
       
   257    \end{itemize}
       
   258 \end{frame}
       
   259 
       
   260 \begin{frame}{Comments}
       
   261   \begin{itemize}
       
   262         \item No comments better than contradicting comments
       
   263         \item Block comments
       
   264         \item Inline comments
       
   265    \end{itemize}
       
   266 \end{frame}
       
   267 
       
   268 \begin{frame}{Docstrings}
       
   269   \begin{itemize}
       
   270         \item When to write docstrings?
       
   271         \item Ending the docstrings
       
   272         \item One liner docstrings
       
   273    \end{itemize}
       
   274 More information at PEP8: http://www.python.org/dev/peps/pep-0008/
       
   275 \inctime{5}
       
   276 \end{frame}
       
   277 
       
   278 \section{Debugging}
       
   279 \subsection{Errors and Exceptions}
       
   280 \begin{frame}[fragile]
       
   281  \frametitle{Errors}
       
   282  \begin{lstlisting}
       
   283 In []: while True print 'Hello world'
       
   284  \end{lstlisting}
       
   285 \pause
       
   286   \begin{lstlisting}
       
   287   File "<stdin>", line 1, in ?
       
   288     while True print 'Hello world'
       
   289                    ^
       
   290 SyntaxError: invalid syntax
       
   291 \end{lstlisting}
       
   292 \end{frame}
       
   293 
       
   294 \begin{frame}[fragile]
       
   295  \frametitle{Exceptions}
       
   296  \begin{lstlisting}
       
   297 In []: print spam
       
   298 \end{lstlisting}
       
   299 \pause
       
   300 \begin{lstlisting}
       
   301 Traceback (most recent call last):
       
   302   File "<stdin>", line 1, in <module>
       
   303 NameError: name 'spam' is not defined
       
   304 \end{lstlisting}
       
   305 \end{frame}
       
   306 
       
   307 \begin{frame}[fragile]
       
   308  \frametitle{Exceptions}
       
   309  \begin{lstlisting}
       
   310 In []: 1 / 0
       
   311 \end{lstlisting}
       
   312 \pause
       
   313 \begin{lstlisting}
       
   314 Traceback (most recent call last):
       
   315   File "<stdin>", line 1, in <module>
       
   316 ZeroDivisionError: integer division 
       
   317 or modulo by zero
       
   318 \end{lstlisting}
       
   319 \end{frame}
       
   320 
       
   321 \begin{frame}[fragile]
       
   322   \frametitle{Handling Exceptions}
       
   323   Python uses \typ{try} and \typ{except} clause.
       
   324   %%Revisiting the raw\_input
       
   325   \begin{lstlisting}
       
   326 a = raw_input('Enter number(Q to quit):')
       
   327 try:
       
   328     num = int(a)
       
   329     print num
       
   330 except:
       
   331     if a == 'Q':
       
   332         print 'Exiting...'
       
   333     else:
       
   334         print 'Wrong input!'      
       
   335   \end{lstlisting}
       
   336   
       
   337   
       
   338 \end{frame}
       
   339 
       
   340 %% \begin{frame}[fragile]
       
   341 %%   \frametitle{Solving it with \typ{try} and \typ{except}}
       
   342 %% \vspace{-0.2in}
       
   343 %%   \begin{lstlisting}
       
   344 %% highest = 0
       
   345 %% for record in open('sslc1.txt'):
       
   346 %%     fields = record.split(';')
       
   347 %%     try:
       
   348 %%         total = 0
       
   349 %%         for score_str in fields[3:8]:
       
   350 %%             score = int(score_str)
       
   351 %%             total += score
       
   352 %%         if total > highest:
       
   353 %%             highest = total
       
   354 %%     except:        
       
   355 %%         pass
       
   356 %% print highest
       
   357 %%   \end{lstlisting}
       
   358 %% \end{frame}
       
   359 \subsection{Strategy}
       
   360 \begin{frame}[fragile]
       
   361     \frametitle{Debugging effectively}
       
   362     \begin{itemize}
       
   363         \item \typ{print} based strategy
       
   364         \item Process:
       
   365     \end{itemize}
       
   366 \begin{center}
       
   367 \pgfimage[interpolate=true,width=5cm,height=5cm]{DebugginDiagram.png}
       
   368 \end{center}
       
   369 \end{frame}
       
   370 
       
   371 \begin{frame}[fragile]
       
   372     \frametitle{Debugging effectively}
       
   373     \begin{itemize}
       
   374       \item Using \typ{\%debug} in IPython
       
   375     \end{itemize}
       
   376 \end{frame}
       
   377 
       
   378 \begin{frame}[fragile]
       
   379 \frametitle{Debugging in IPython}
       
   380 \small
       
   381 \begin{lstlisting}
       
   382 In []: import mymodule
       
   383 In []: mymodule.test()
       
   384 ---------------------------------------------
       
   385 NameError   Traceback (most recent call last)
       
   386 <ipython console> in <module>()
       
   387 mymodule.py in test()
       
   388       1 def test():
       
   389 ----> 2     print spam
       
   390 NameError: global name 'spam' is not defined
       
   391 
       
   392 In []: %debug
       
   393 > mymodule.py(2)test()
       
   394       0     print spam
       
   395 ipdb> 
       
   396 \end{lstlisting}
       
   397 \inctime{15} 
       
   398 \end{frame}
       
   399 
       
   400 \subsection{Exercise}
       
   401 \begin{frame}[fragile]
       
   402 \frametitle{Debugging: Exercise}
       
   403 \small
       
   404 \begin{lstlisting}
       
   405 science = {}
       
   406 
       
   407 for record in open('sslc1.txt'):
       
   408     fields = record.split(';')
       
   409     region_code = fields[0].strip()
       
   410 
       
   411     score_str = fields[6].strip()
       
   412     score = int(score_str) if score_str != 'AA' 
       
   413                            else 0
       
   414 
       
   415     if score > 90:
       
   416         science[region_code] += 1
       
   417 
       
   418 pie(science.values(), labels=science.keys())
       
   419 savefig('science.png')
       
   420 \end{lstlisting}
       
   421 \inctime{10}
       
   422 \end{frame}
       
   423 
       
   424 %% \begin{frame}
       
   425 %%     \frametitle{Testing}
       
   426    
       
   427 %%     \begin{itemize}
       
   428 %%         \item Writing tests is really simple!
       
   429 
       
   430 %%         \item Using nose.
       
   431 
       
   432 %%         \item Example!
       
   433 %%     \end{itemize}
       
   434 %% \end{frame}
       
   435 
       
   436 \section{Test Driven Approach}
       
   437 \begin{frame}
       
   438     \frametitle{Need for Testing!}
       
   439    
       
   440     \begin{itemize}
       
   441         \item Quality
       
   442         \item Regression
       
   443         \item Documentation
       
   444     \end{itemize}
       
   445     %% \vspace*{0.25in}
       
   446     %% \emphbar{It is to assure that section of code is working as it is supposed to work}
       
   447 \end{frame}
       
   448 
       
   449 \begin{frame}[fragile]
       
   450     \frametitle{Example}
       
   451     \begin{block}{Problem Statement}
       
   452       Write a function to check whether a given input
       
   453       string is a palindrome.
       
   454     \end{block}
       
   455 \end{frame}
       
   456 
       
   457 \begin{frame}[fragile]
       
   458     \frametitle{Function: palindrome.py}
       
   459 \begin{lstlisting}    
       
   460 def is_palindrome(input_str):
       
   461   return input_str == input_str[::-1]
       
   462 \end{lstlisting}    
       
   463 \end{frame}
       
   464 
       
   465 \begin{frame}[fragile]
       
   466     \frametitle{Test for the palindrome: palindrome.py}
       
   467 \begin{lstlisting}    
       
   468 def test_function_normal_words():
       
   469   input = "noon"
       
   470   assert is_palindrome(input) == True
       
   471 
       
   472 if __name__ == "main'':
       
   473   test_function_normal_words()
       
   474 \end{lstlisting}    
       
   475 \end{frame}
       
   476 
       
   477 \begin{frame}[fragile]
       
   478     \frametitle{Running the tests.}
       
   479 \begin{lstlisting}    
       
   480 $ nosetests palindrome.py 
       
   481 .
       
   482 ----------------------------------------------
       
   483 Ran 1 test in 0.001s
       
   484 
       
   485 OK
       
   486 \end{lstlisting}    
       
   487 \end{frame}
       
   488 
       
   489 \begin{frame}[fragile]
       
   490     \frametitle{Exercise: Including new tests.}
       
   491 \begin{lstlisting}    
       
   492 def test_function_ignore_cases_words():
       
   493   input = "Noon"
       
   494   assert is_palindrome(input) == True
       
   495 \end{lstlisting}
       
   496      \vspace*{0.25in}
       
   497      Check\\
       
   498      \PythonCode{$ nosetests palindrome.py} \\
       
   499      \begin{block}{Task}
       
   500      Tweak the code to pass this test.
       
   501      \end{block}
       
   502 \end{frame}
       
   503 
       
   504 %\begin{frame}[fragile]
       
   505 %    \frametitle{Lets write some test!}
       
   506 %\begin{lstlisting}    
       
   507 %#for form of equation y=mx+c
       
   508 %#given m and c for two equation,
       
   509 %#finding the intersection point.
       
   510 %def intersect(m1,c1,m2,c2):
       
   511 %    x = (c2-c1)/(m1-m2)
       
   512 %    y = m1*x+c1
       
   513 %    return (x,y)
       
   514 %\end{lstlisting}
       
   515 %
       
   516 %Create a simple test for this
       
   517 %
       
   518 %function which will make it fail.
       
   519 %
       
   520 %\inctime{15} 
       
   521 %\end{frame}
       
   522 %
       
   523 
       
   524 %% \begin{frame}[fragile]
       
   525 %%     \frametitle{Exercise}
       
   526 %%     Based on Euclid's algorithm:
       
   527 %%     \begin{center}
       
   528 %%     $gcd(a,b)=gcd(b,b\%a)$
       
   529 %%     \end{center}
       
   530 %%     gcd function can be written as:
       
   531 %%     \begin{lstlisting}
       
   532 %%     def gcd(a, b):
       
   533 %%       if a%b == 0: return b
       
   534 %%       return gcd(b, a%b)
       
   535 %%     \end{lstlisting}
       
   536 %%     \vspace*{-0.15in}
       
   537 %%     \begin{block}{Task}
       
   538 %%       \begin{itemize}
       
   539 %%       \item Write at least 
       
   540 %%         two tests for above mentioned function.
       
   541 %%       \item Write a non recursive implementation
       
   542 %%       of gcd(), and test it using already 
       
   543 %%       written tests.
       
   544 %%       \end{itemize}
       
   545 %%     \end{block}
       
   546     
       
   547 %% \inctime{15} 
       
   548 %% \end{frame}
       
   549 
       
   550 \begin{frame}
       
   551   \frametitle{Summary}
       
   552 We have coverd:
       
   553   \begin{itemize}
       
   554   \item Following and Resolving Error Messages.
       
   555   \item Exceptions.
       
   556   \item Handling exceptions
       
   557   \item Approach for Debugging.
       
   558   \item Writting and running tests.
       
   559   \end{itemize}
       
   560 \end{frame}
       
   561 
       
   562 \end{document}
   557 \end{document}
       
   558