IPython Notebook Tutorial

James had the suggestion that I should give an IPython notebook talk/tutorial. I thought of a few things I could mention:

Organisation

  • Examples that do not need to see the editing

    • Code and markdown cells
    • Intro to markdown?
    • Nice magic commands
      • config magic for e.g. increasing resolution of background plots, though this does increase download time
      • autoreloading through
        • %load_ext autoreload
        • %autoreload 1
        • %aimport blah
      • qtconsole for embedded console
    • Hints and tips
      • seaborn?
        • also matplotlib styles
      • seeding the rng for reproducible plots
    • nbagg backend
    • Background jobs?
  • Examples that need to view the editing

    • Directory navigation
    • Cell modes
      • Keyboard shortcuts
    • Help system
    • Interactivity
  • Examples that need the command line

    • Rendering to a report
      • Include ignoring code cells
    • Running from the command line?
    • Styling
    • Creating a presentation
      • reveal.js < 3
    • IPython 3

IPython notebook cannot be run from the default installation it seems, so perhaps an introduction to the Anaconda python distribution?

Running a notebook

Usual setup

Usually the beginning of a notebook is the same. The following code sets up embedded matplotlib plots (saved as pngs):


In [75]:
%matplotlib inline

Increase the resolution of the pngs created (mostly for OSX retina screens):


In [76]:
%config InlineBackend.figure_format = 'retina' # or svg for vector plots

Some imports


In [77]:
import matplotlib
import IPython

In [78]:
import matplotlib.pyplot as plt
import numpy as np
from astropy.io import fits as pyfits

In [79]:
def reset_matplotlib():
    matplotlib.rc_file_defaults()
    ipython = IPython.get_ipython()
    ipython.magic('matplotlib inline')
    ipython.magic('config InlineBackend.figure_format = "retina"')
    matplotlib.rc('figure', figsize=(8, 6))

Plots!

This is what IPython is really good for


In [80]:
reset_matplotlib()

In [81]:
x = np.arange(10)
y = x ** 2
plt.plot(x, y)


Out[81]:
[<matplotlib.lines.Line2D at 0x10a525208>]

The nbagg backend


In [82]:
%matplotlib inline

Markdown!

This is some markdown. Let's look at this link, or this list:

  • a
  • b
  • c

and what about some $\LaTeX$?

$$a = \sum_i^N x_i^2$$

This is some markdown. Let's look at [this link](http://example.com), or this list:

* a
* b
* c

and what about some $\LaTeX$?

$$a = \sum_i^N x_i^2$$

Cells and keyboard shortcuts

Cells have different modes:

  • Insert mode
  • Command mode

Keyboard shortcuts

  • jk to change slide
  • Escape: insert => command
  • Enter: command => insert
  • Shift-tab: cycle through help info

Command mode shortcuts

  • h for help
  • Ctrl/Cmd-Enter: run the current cell
  • Shift-Enter: run the current cell and switch to the next one
  • s to save the notebook

Magic commands!

  • %: line magic
  • %%: cell magic
  • arguments

Run a shell command


In [83]:
%%sh

pwd
whoami
uptime
uname -a


/Users/simon/work/Other/IPython-Notebook-Tutorial
simon
 08:47am  up  21:18,  1 user,  load average: 1.77, 1.52, 1.45
Darwin mbp15.local 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64 i386 MacBookPro11,3 Darwin

Get the results of shell output


In [84]:
files = !ls
print(files) # A list!
print(len(files))


['IPythonNotebookTutorial.ipynb', 'IPythonNotebookTutorial.slides.html', 'LICENSE', 'Makefile', 'README.md', 'c-example', 'embedding-example', 'ipython3-example', 'nbagg-backend', 'profiling', 'qtconsole', 'report-example', 'requirements.conda.txt', 'reveal.js', 'runipy-example', 'running-a-notebook', 'this', 'venv']
18

Compiling C example


In [85]:
!find c-example -type f -delete

In [86]:
%%file c-example/main.c

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 0;
}


Writing c-example/main.c

In [87]:
%%file c-example/Makefile

all: a.out
    
a.out: main.c
	gcc $< -o $@


Writing c-example/Makefile

In [88]:
!make -C c-example && ./c-example/a.out


gcc main.c -o a.out
Hello world!

Qt Console

Open an embedded console for interactive play, with %qtconsole:

qtconsole example

Timing


In [89]:
%time a = 10


CPU times: user 3 µs, sys: 2 µs, total: 5 µs
Wall time: 10 µs

In [90]:
%timeit a = 10


10000000 loops, best of 3: 23 ns per loop

In [91]:
%%timeit

a = np.arange(1024)
b = np.random.randint(50, size=1024)
c = a + b


100000 loops, best of 3: 16.9 µs per loop

Profiling

Profile cell running with %prun

profiling example

Autocall

Run commands without brackets! (sometimes...)


In [92]:
%autocall 1


Automatic calling is: Smart

In [93]:
str 10


Out[93]:
'10'

In [94]:
%autocall 0


Automatic calling is: OFF

Misc


In [95]:
%%latex

$a = 10$


$a = 10$

In [96]:
%%ruby

puts "Hello world"


Hello world

In [97]:
%load_ext cythonmagic


The cythonmagic extension is already loaded. To reload it, use:
  %reload_ext cythonmagic

In [98]:
%%cython

cdef int a = 10
cdef int b = 20

def f(int c):
    return a * b * c

In [99]:
f(5)


Out[99]:
1000

Figure styling

  • matplotlib includes custom easy to use styles
  • plt.style.use(...)

In [100]:
reset_matplotlib()

In [101]:
def plot_colour_example():
    L = 6
    x = np.linspace(0, L)
    ncolors = len(plt.rcParams['axes.color_cycle'])
    shift = np.linspace(0, L, 5, endpoint=False)
    for s in shift:
        plt.plot(x, np.sin(x + s), 'o-')
    plt.xlabel('x-axis')
    plt.ylabel('y-axis')
    plt.title('title')

In [102]:
plot_colour_example()



In [103]:
print(plt.style.available)
plt.style.use('dark_background')
plot_colour_example()


['ggplot', 'dark_background', 'grayscale', 'bmh', 'fivethirtyeight']

In [104]:
reset_matplotlib()

Seaborn is a wrapper around matplotlib that improves the aesthetics. For example

Seaborn!

  • Wrapper around matplotlib
  • Contains ways to customise aesthetics
    • includes default dimensions for papers, talks, posters
    • multiple colour schemes

Before seaborn:


In [105]:
reset_matplotlib()

In [106]:
# Set up some data
x = np.arange(10)
y = x ** 2

In [107]:
plt.plot(x, y)


Out[107]:
[<matplotlib.lines.Line2D at 0x114d190b8>]

After seaborn:


In [108]:
import seaborn as sns

sns.set()

In [109]:
plt.plot(x, y)


Out[109]:
[<matplotlib.lines.Line2D at 0x113889208>]

Nice distribution plotting


In [110]:
with sns.axes_style('white'):
    x = np.random.normal(2., 5., size=1000)
    y = np.random.exponential(5., size=1000)
    sns.jointplot(x, y, kind='kde', stat_func=None)


Seaborn has many colour schemes


In [111]:
from IPython.display import IFrame
seaborn_page = IFrame('http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html', width=700, height=450)

In [112]:
seaborn_page


Out[112]:

Example notebooks

Command line usage

Downsides

Not everything with IPython is perfect...

  • Cannot use vim :(
  • Overwriting earlier variables
    • everything in global namespace