Advanced IPython session

  • IPython magics for development
  • Integration in IPython
  • Parallelization in IPython (in separate notebook)
  • Advanced Display Capabilities (in separate notebook)
In [ ]:
from IPython.display import Image
Image(url='http://imgs.xkcd.com/comics/new_pet.png')

Comic: XKCD: New Pet

Where can I find more of this advanced information?

We have an IPython in-depth tutorial that you can download. And you can watch it all online:

In [ ]:
from IPython.display import YouTubeVideo
YouTubeVideo('xe_ATRmw0KM')  # Part 1/3, the whole 3-part thing is 3 hours
In [ ]:
YouTubeVideo('A8VbS-YX2Lo') # Part 2/3
In [ ]:
YouTubeVideo('4tJKZWWRs6s') # Part 3/3

1. Development goodies

Documentation and source

In [ ]:
%matplotlib inline
from matplotlib.pyplot import plot
In [ ]:
plot?
In [ ]:
%pdoc plot
In [ ]:
%psource plot

The IPython kernel/client model

In [ ]:
%connect_info
In [ ]:
%qtconsole

Loading from the web

Finding useful examples in the matplotlib gallery.

2. Integration

R (statistical programming package)

In [ ]:
# Might need to install R and rpy2
%load_ext rpy2.ipython
In [ ]:
import numpy as np
X = np.arange(100)
Y = 200.*X + 100.
In [ ]:
%%R -i X,Y -o XYcoeff
XYlm = lm(Y~X)
print(summary(XYlm))
XYcoeff = coef(XYlm)
par(mfrow=c(2,2))
plot(XYlm)
In [ ]:
print(XYcoeff)

Octave

In [ ]:
# Might need to install Octave and oct2py
%load_ext octavemagic
In [ ]:
%%octave -s 600,200 -f png

subplot(121);
[x,y] = meshgrid(0:0.1:3);
r = sin(x-0.5).^2 + cos(y-0.5).^2;
surf(x,y,r);

subplot(122);
sombrero()

Cython (C and Python mashup)

In [ ]:
%load_ext cython
In [ ]:
def f(x):
    return x**2-x

def integrate_f(a, b, N):
    s = 0
    dx = (b-a)/N
    for i in range(N):
        s += f(a+i*dx)
    return s * dx
In [ ]:
%%cython

cdef double f(double x):
    return x**2-x

def cyintegrate_f(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0
    dx = (b-a)/N
    for i in range(N):
        s += f(a+i*dx)
    return s * dx
In [ ]:
a, b, N = 0, 2, 10000
%timeit integrate_f(a, b, N)
%timeit cyintegrate_f(a, b, N)

Ruby

In [ ]:
%%ruby
s = "Yo IPython, Ruby's here 4 real!"
s.split(" ").each do |word| 
    puts word if word.to_i.to_s == word
end