#!/usr/bin/env python # coding: utf-8 # # 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](http://xkcd.com/413/) # # Where can I find more of this advanced information? # # We have an [IPython in-depth tutorial](https://github.com/ipython/ipython-in-depth) 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[ ]: get_ipython().run_line_magic('matplotlib', 'inline') from matplotlib.pyplot import plot # In[ ]: get_ipython().run_line_magic('pinfo', 'plot') # In[ ]: get_ipython().run_line_magic('pdoc', 'plot') # In[ ]: get_ipython().run_line_magic('psource', 'plot') # ### The IPython kernel/client model # In[ ]: get_ipython().run_line_magic('connect_info', '') # In[ ]: get_ipython().run_line_magic('qtconsole', '') # ### Loading from the web # # Finding useful examples in the [matplotlib gallery](http://matplotlib.org/gallery.html). # ## 2. Integration # # ### R (statistical programming package) # In[ ]: # Might need to install R and rpy2 get_ipython().run_line_magic('load_ext', 'rpy2.ipython') # In[ ]: import numpy as np X = np.arange(100) Y = 200.*X + 100. # In[ ]: get_ipython().run_cell_magic('R', '-i X,Y -o XYcoeff', 'XYlm = lm(Y~X)\nprint(summary(XYlm))\nXYcoeff = coef(XYlm)\npar(mfrow=c(2,2))\nplot(XYlm)\n') # In[ ]: print(XYcoeff) # ### Octave # In[ ]: # Might need to install Octave and oct2py get_ipython().run_line_magic('load_ext', 'octavemagic') # In[ ]: get_ipython().run_cell_magic('octave', '-s 600,200 -f png', '\nsubplot(121);\n[x,y] = meshgrid(0:0.1:3);\nr = sin(x-0.5).^2 + cos(y-0.5).^2;\nsurf(x,y,r);\n\nsubplot(122);\nsombrero()\n') # ### Cython (C and Python mashup) # In[ ]: get_ipython().run_line_magic('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[ ]: get_ipython().run_cell_magic('cython', '', '\ncdef double f(double x):\n return x**2-x\n\ndef cyintegrate_f(double a, double b, int N):\n cdef int i\n cdef double s, dx\n s = 0\n dx = (b-a)/N\n for i in range(N):\n s += f(a+i*dx)\n return s * dx\n') # In[ ]: a, b, N = 0, 2, 10000 get_ipython().run_line_magic('timeit', 'integrate_f(a, b, N)') get_ipython().run_line_magic('timeit', 'cyintegrate_f(a, b, N)') # ### Ruby # In[ ]: get_ipython().run_cell_magic('ruby', '', 's = "Yo IPython, Ruby\'s here 4 real!"\ns.split(" ").each do |word| \n puts word if word.to_i.to_s == word\nend\n')