%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Tutorial slides on Python.
%
% Author: FOSSEE
% Copyright (c) 2009-2016, FOSSEE, IIT Bombay
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[14pt,compress]{beamer}
%\documentclass[draft]{beamer}
%\documentclass[compress,handout]{beamer}
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

% Modified from: generic-ornate-15min-45min.de.tex
\mode<presentation>
{
  \usetheme{Warsaw}
  \useoutertheme{infolines}
  \setbeamercovered{transparent}
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
%\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{pgf}

% Taken from Fernando's slides.
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}
\usepackage{amsmath}

\definecolor{darkgreen}{rgb}{0,0.5,0}

\usepackage{listings}
\lstset{language=Python,
    basicstyle=\ttfamily\bfseries,
    commentstyle=\color{red}\itshape,
  stringstyle=\color{darkgreen},
  showstringspaces=false,
  keywordstyle=\color{blue}\bfseries}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\setbeamercolor{emphbar}{bg=blue!20, fg=black}
\newcommand{\emphbar}[1]
{\begin{beamercolorbox}[rounded=true]{emphbar}
      {#1}
 \end{beamercolorbox}
}

\newcommand{\myemph}[1]{\structure{\emph{#1}}}
\newcommand{\PythonCode}[1]{\lstinline{#1}}

\newcounter{time}
\setcounter{time}{0}
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}

\newcommand{\typ}[1]{\lstinline{#1}}

\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }

%%% This is from Fernando's setup.
% \usepackage{color}
% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
% % Use and configure listings package for nicely formatted code
% \usepackage{listings}
% \lstset{
%    language=Python,
%    basicstyle=\small\ttfamily,
%    commentstyle=\ttfamily\color{blue},
%    stringstyle=\ttfamily\color{orange},
%    showstringspaces=false,
%    breaklines=true,
%    postbreak = \space\dots
% }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
\title[Mayavi mlab]{Introductory Scientific Computing with
Python}
\subtitle{Simple 3D plots with Mayavi's mlab}

\author[Prabhu] {FOSSEE}

\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {
Mumbai
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
%\logo{\pgfuseimage{iitmlogo}}


%% Delete this, if you do not want the table of contents to pop up at
%% the beginning of each subsection:
\AtBeginSubsection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

\AtBeginSection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:
%\beamerdefaultoverlayspecification{<+->}

%\includeonlyframes{current,current1,current2,current3,current4,current5,current6}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT STARTS
\begin{document}

\begin{frame}
  \maketitle
\end{frame}

\section{Introduction to Mayavi}

\begin{frame}
  \frametitle{Mayavi}
  \begin{itemize}
  \item 3D visualization library
  \item Application
  \item Support for different data formats
  \item Embeddable, extensible
  \item Cross-platform
  \item Fully scriptable
  \end{itemize}
\end{frame}

\begin{frame}
    {Overview of features}
      \vspace*{-0.3in}
  \begin{center}
    \hspace*{-0.2in}\pgfimage[width=5in]{mlab_data/m2_app3_3}
  \end{center}
\end{frame}


\begin{frame}
    \frametitle{Mayavi in applications}
      \vspace*{-0.3in}
  \begin{center}
    \hspace*{-0.2in}\pgfimage[width=4.5in]{mlab_data/m2_envisage}
  \end{center}
\end{frame}

\begin{frame}
    \frametitle{Live in your dialogs}
      \vspace*{0.1in}
  \begin{center}
    \hspace*{-0.2in}\pgfimage[width=2.5in]{mlab_data/mlab_tui}
  \end{center}
\end{frame}



\begin{frame}
  \frametitle{Installation}
  \begin{itemize}
  \item Suggest using a Python distribution
  \item Canopy's package manager: simply install mayavi
  \item \typ{conda install -c menpo mayavi}
  \item \typ{edm install mayavi}
  \end{itemize}
\end{frame}

\section{Getting started with mlab}

\begin{frame}
  {Overview}
  \begin{itemize}
  \item Simple
  \item Convenient
  \item Full-featured
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Getting started}

 \myemph{With \typ{jupyter}:}
\begin{lstlisting}[language=bash]
    $ jupyter console
    In [1]: %gui qt
\end{lstlisting} %$
 \vspace*{0.15in}

  \myemph{Vanilla:}
\begin{lstlisting}[language=bash]
    $ ipython --gui=qt
\end{lstlisting} %$
 \vspace*{0.15in}

\myemph{Or:}
\begin{lstlisting}[language=bash]
    $ ipython
    In [1]: %gui qt
\end{lstlisting} %$

\end{frame}

\begin{frame}[fragile]
  \frametitle{Using notebooks}

  Make sure you have the following code first:

\begin{lstlisting}
    In [1]: from mayavi import mlab
    In [2]: mlab.init_notebook()
\end{lstlisting}

  \begin{itemize}
    \item Can also use \typ{\%gui qt}
    \item Plots will pop-up
  \end{itemize}

\end{frame}

\section{Basic Mayavi: \typ{mlab}}

\begin{frame}[fragile]
\frametitle{Basic 3D visualization}
\begin{itemize}
    \item Simple one liners for 3D visualization
    \item Must import mlab.
\end{itemize}
\begin{lstlisting}
In []: from mayavi import mlab
\end{lstlisting}
Ready to go!
\end{frame}

\begin{frame}[fragile]
    \frametitle{Simple example}

    \begin{lstlisting}
In []: mlab.test_<TAB>

# Let us start with this:
In []: mlab.test_contour3d()
\end{lstlisting}
Explore the UI.
\end{frame}

\begin{frame}
    {Exploring the view}
    \begin{columns}
        \column{0.6\textwidth}
    \pgfimage[width=3in]{mlab_data/mlab_contour_window}
        \column{0.4\textwidth}
        \begin{itemize}
            \item Mouse
            \item Keyboard
            \item Toolbar
            \item Mayavi icon\pgfimage[width=0.2in]{mlab_data/m2_icon}
        \end{itemize}
    \end{columns}
\end{frame}

\begin{frame}[fragile]
    \frametitle{\typ{mlab} plotting functions}
    \begin{columns}
        \column{0.25\textwidth}
        \myemph{\Large 0D data}
        \column{0.5\textwidth}
    \pgfimage[width=2in]{mlab_data/points3d_ex}
    \end{columns}

    \begin{lstlisting}
In []: t = linspace(0, 2*pi, 50)
In []: u = cos(t) * pi
In []: x, y, z = sin(u), cos(u), sin(t)
    \end{lstlisting}
    \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}}
\end{frame}

\begin{frame}[plain, fragile]
    \frametitle{Changing how things look}

    \begin{block}{Clearing the view}
    \PythonCode{>>> mlab.clf()}
    \end{block}

    \pause

    \begin{block}{IPython is your friend!}
    \PythonCode{>>> mlab.points3d?}

    \begin{itemize}

        \item Extra argument: Scalars

        \item Keyword arguments

        \item UI
    \end{itemize}
    \end{block}
\pause
    \begin{lstlisting}
In []: mlab.points3d(x, y, z, t,
                     scale_mode='none')
    \end{lstlisting}
\end{frame}


\begin{frame}[fragile]
  \begin{columns}
        \column{0.25\textwidth}
        \myemph{\Large 1D data}
        \column{0.5\textwidth}
        \pgfimage[width=2.5in]{mlab_data/plot3d_ex}
  \end{columns}
  \emphbar{\PythonCode{In []: mlab.plot3d(x, y, z, t)}}

    Plots lines between the points

\end{frame}

\begin{frame}[fragile]
    \begin{columns}
        \column{0.25\textwidth}
        \myemph{\Large 2D data}
        \column{0.5\textwidth}
        \pgfimage[width=2in]{mlab_data/surf_ex}
    \end{columns}
    \begin{lstlisting}
In []: x, y = mgrid[-3:3:100j,-3:3:100j]
In []: z = sin(x*x + y*y)
    \end{lstlisting}

    \emphbar{\PythonCode{In []: mlab.surf(x, y, z)}}

    \alert{Assumes the points are rectilinear}

\end{frame}

\begin{frame}[fragile]
  \frametitle{mgrid example}
  \begin{lstlisting}
In []: mgrid[0:3,0:3]
Out[]:
array([[[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]],

       [[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]]])

In []: mgrid[-1:1:5j]
Out[]: array([-1., -0.5,  0.,  0.5,  1.])
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{mgrid example}
  \begin{lstlisting}
In []: x, y = mgrid[-1:1:5j, -1:1:5j]
In []: z = x*x + y*y

In []: z
Out[]:
array([[ 2.  , 1.25, 1.  , 1.25, 2.  ],
       [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       [ 1.  , 0.25, 0.  , 0.25, 1.  ],
       [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
       [ 2.  , 1.25, 1.  , 1.25, 2.  ]])
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
    \myemph{\Large 2D data: \texttt{mlab.mesh}}
    \vspace*{0.25in}

    \emphbar{\PythonCode{In []: mlab.mesh(x, y, z)}}

    \alert{Points needn't be regular}

    \vspace*{0.25in}
\begin{lstlisting}
In []: phi, theta = mgrid[0:pi:20j,
...                         0:2*pi:20j]
In []: x = sin(phi)*cos(theta)
In []: y = sin(phi)*sin(theta)
In []: z = cos(phi)
In []: mlab.mesh(x, y, z,
...           representation=
...           'wireframe')
\end{lstlisting}

\end{frame}


\begin{frame}[fragile]

        \myemph{\Large 3D data}
    \vspace*{0.25in}

    \pgfimage[width=1.5in]{mlab_data/contour3d}\\

\begin{lstlisting}
In []: x, y, z = mgrid[-5:5:64j,
  ...:                 -5:5:64j,
  ...:                 -5:5:64j]
In []: mlab.contour3d(x*x*0.5 + y*y +
                   z*z*2)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]

    \myemph{\Large 3D vector data: \PythonCode{mlab.quiver3d}}
    \vspace*{0.25in}

    \pgfimage[width=2in]{mlab_data/quiver3d_ex}\\

\begin{lstlisting}
In []: mlab.test_quiver3d()
\end{lstlisting}

\emphbar{\PythonCode{obj = mlab.quiver3d(x, y, z, u, v, w)}}
\end{frame}


\begin{frame}[fragile]
    {Other utility functions}
    \begin{itemize}
        \item \PythonCode{gcf}: get current figure
            \pause
        \item \PythonCode{savefig}, \PythonCode{figure}
           \pause
        \item \PythonCode{axes}, \PythonCode{outline}
            \pause
        \item \PythonCode{title}, \PythonCode{xlabel, ylabel, zlabel}
            \pause
        \item \PythonCode{colorbar}, \PythonCode{scalarbar},
            \PythonCode{vectorbar}
            \pause
        \item \PythonCode{show}: Standalone mlab scripts
            \pause
        \item Others, see UG
    \end{itemize}
\end{frame}


\begin{frame}
    {Exploring the documentation}
    \begin{center}
    \pgfimage[width=4in]{mlab_data/m2_ug_doc}
    \end{center}
\end{frame}

\begin{frame}[fragile]
    \frametitle{Explore the examples}

    \myemph{\Large Try these}

\begin{lstlisting}
In []: mlab.clf()
In []: mlab.test_points3d()
In []: mlab.clf()
In []: mlab.test_plot3d()
In []: mlab.clf()
\end{lstlisting}
    Explore these later
\end{frame}


\begin{frame}
  \frametitle{Summary}
  \begin{itemize}
  \item Quick introduction to Mayavi's mlab
  \item Documentation: \url{github.enthought.com/mayavi/mayavi}
  \end{itemize}

\end{frame}

\end{document}


\begin{frame}
    {An exercise}

        \begin{block}{Exercise}
        Atmospheric data of temperature over the surface of the earth.
        Let temperature ($T$) vary linearly with height ($z$):
        \begin{center}
        $T = 288.15 - 6.5z$
        \end{center}
        \end{block}
\end{frame}

\begin{frame}[fragile]
    \frametitle{Simple solution}

    \begin{lstlisting}
lat = linspace(-89, 89, 37)
lon = linspace(0, 360, 37)
z = linspace(0, 100, 11)
    \end{lstlisting}
\pause
    \begin{lstlisting}
x, y, z = mgrid[0:360:37j,-89:89:37j,
                0:100:11j]
t = 288.15 - 6.5*z
mlab.contour3d(x, y, z, t)
mlab.outline()
mlab.colorbar()
    \end{lstlisting}
\end{frame}