IPython (Interactive Python) is an enhanced Python shell which provides a more robust and productive development environment for users. There are several key features that set it apart from the standard Python shell.
In IPython, all your inputs and outputs are saved. There are two variables named In
and Out
which are assigned as you work with your results. All outputs are saved automatically to variables of the form _N
, where N
is the prompt number, and inputs to _iN
. This allows you to recover quickly the result of a prior computation by referring to its number even if you forgot to store it as a variable.
import numpy as np
np.sin(4)**2
_1
_i1
_1 / 4.
All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end.
import time, sys
for i in range(8):
print(i)
time.sleep(0.5)
If you want details regarding the properties and functionality of any Python objects currently loaded into IPython, you can use the ?
to reveal any details that are available:
some_dict = {}
some_dict?
If available, additional detail is provided with two question marks, including the source code of the object itself.
from numpy.linalg import cholesky
cholesky??
This syntax can also be used to search namespaces with wildcards (*).
%matplotlib inline
import pylab as plt
plt.*plot*?
Because IPython allows for introspection, it is able to afford the user the ability to tab-complete commands that have been partially typed. This is done by pressing the <tab>
key at any point during the process of typing a command.
Place your cursor after the partially-completed command below and press tab:
np.ar
In IPython, you can type ls
to see your files or cd
to change directories, just like you would at a regular system prompt:
ls ../data
Virtually any system command can be accessed by prepending !
, which passes any subsequent command directly to the OS.
!touch test.txt
You can even use Python variables in commands sent to the OS:
file_type = 'csv'
!ls ../data/*$file_type
The output of a system command using the exclamation point syntax can be assigned to a Python variable.
data_files = !ls ../data/microbiome/
data_files
Over time, the IPython project grew to include several components, including:
As each component has evolved, several had grown to the point that they warrented projects of their own. For example, pieces like the notebook and protocol are not even specific to Python. As the result, the IPython team created Project Jupyter, which is the new home of language-agnostic projects that began as part of IPython, such as the notebook in which you are reading this text.
The HTML notebook that is part of the Jupyter project supports interactive data visualization and easy high-performance parallel computing.
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
def f(x):
return (x-3)*(x-5)*(x-7)+85
import numpy as np
x = np.linspace(0, 10, 200)
y = f(x)
p = plt.plot(x,y)
The notebook lets you document your workflow using either HTML or Markdown, providing a complete and self-contained record of a computation that can be exported to various formats and shared.
The Jupyter Notebook consists of three interacting components:
.ipynb
extension.The Notebook can be used by starting the Notebook server with the command:
$ jupyter notebook
This opens a Jupyter notebook dashboard that acts as a home page for your Jupyter instance. It displays the notebooks and other files in your current directory.
The notebook web application provides a rich computing environment for data science work. For example, you can embed images, videos, or entire websites into notebooks:
from IPython.display import HTML
HTML("<iframe src=http://fonnesbeck.github.io/Bios8366 width=700 height=350></iframe>")
from IPython.display import YouTubeVideo
YouTubeVideo("GExKsQ-OU78")
The next-generation web interface for Jupyter notebooks is called JupyterLab. JupyterLab is closer to an integrated development enviroment (IDE) in its design, including things like custom components, terminals, and text editors in a constomizable, panel-based interface. It is quickly becoming the default Jupyter front-end.
JupyterLab can be invoked from the command line via:
$ jupyter lab
Due to its extensibility, Jupyter can also be run from a variety of third-party apps, including the popular Visual Studio Code (VSCode) editor.
Use %load
to add remote code
%load http://matplotlib.org/mpl_examples/shapes_and_collections/scatter_demo.py
Mathjax ia a javascript implementation $\alpha$ of LaTeX that allows equations to be embedded into HTML. For example, this markup:
"""$$ \int_{a}^{b} f(x)\, dx \approx \frac{1}{2} \sum_{k=1}^{N} \left( x_{k} - x_{k-1} \right) \left( f(x_{k}) + f(x_{k-1}) \right). $$"""
becomes this:
$$ \int_{a}^{b} f(x)\, dx \approx \frac{1}{2} \sum_{k=1}^{N} \left( x_{k} - x_{k-1} \right) \left( f(x_{k}) + f(x_{k-1}) \right). $$SymPy is a Python library for symbolic mathematics. It supports:
# Never import like this!
from sympy import *
import warnings
warnings.filterwarnings('ignore')
init_printing()
x, y = symbols("x y")
eq = ((x+y)**2 * (x+1))
eq
expand(eq)
(1/cos(x)).series(x, 0, 6)
limit((sin(x)-x)/x**3, x, 0)
diff(cos(x**2)**2 / (1+x), x)
Jupyter has a set of predefined ‘magic functions’ that you can call with a command line style syntax. These include:
%run
%edit
%debug
%timeit
%paste
%load_ext
%lsmagic
Timing the execution of code; the timeit
magic exists both in line and cell form:
%timeit np.linalg.eigvals(np.random.rand(100,100))
%%timeit a = np.random.rand(100, 100)
np.linalg.eigvals(a)
IPython also creates aliases for a few common interpreters, such as bash, ruby, perl, etc.
These are all equivalent to %%script <name>
%%ruby
puts "Hello from Ruby #{RUBY_VERSION}"
%%bash
echo "hello from $BASH"
IPython has an rmagic
extension that contains a some magic functions for working with R via rpy2. This extension can be loaded using the %load_ext
magic as follows:
%load_ext rpy2.ipython
If the above generates an error, it is likely that you do not have the rpy2
module installed. You can install this now via:
!pip install rpy2
x,y = np.arange(10), np.random.normal(size=10)
%R print(lm(rnorm(10)~rnorm(10)))
%%R -i x,y -o XYcoef
lm.fit <- lm(y~x)
par(mfrow=c(2,2))
print(summary(lm.fit))
plot(lm.fit)
XYcoef <- coef(lm.fit)
XYcoef
In addition to MathJax support, you may declare a LaTeX cell using the %latex
magic:
%%latex
\begin{align}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}
Jupyter also enables objects to declare a JavaScript representation. At first, this may seem odd as output is inherently visual and JavaScript is a programming language. However, this opens the door for rich output that leverages the full power of JavaScript and associated libraries such as D3 for output.
%%javascript
alert("Hello world!");
In Jupyter, one can convert an .ipynb
notebook document file into various static formats via the nbconvert
tool. Currently, nbconvert is a command line tool, run as a script using Jupyter.
!jupyter nbconvert --to html Section0_1-IPython_and_Jupyter.ipynb
Currently, nbconvert
supports HTML (default), LaTeX, Markdown, reStructuredText, Python and HTML5 slides for presentations. Some types can be post-processed, such as LaTeX to PDF (this requires Pandoc to be installed, however).
!jupyter nbconvert --to pdf Section2_1-Introduction-to-Pandas.ipynb
A very useful online service is the Jupyter Notebook Viewer which allows you to display your notebook as a static HTML page, which is useful for sharing with others:
%%html
<iframe src=https://nbviewer.org/2352771 width=700 height=300></iframe>
Also, GitHub supports the rendering of Jupyter Notebooks stored on its repositories.
reproducing conclusions from a single experiment based on the measurements from that experiment
The most basic form of reproducibility is a complete description of the data and associated analyses (including code!) so the results can be exactly reproduced by others.
Reproducing calculations can be onerous, even with one's own work!
Scientific data are becoming larger and more complex, making simple descriptions inadequate for reproducibility. As a result, most modern research is irreproducible without tremendous effort.
Reproducible research is not yet part of the culture of science in general, or scientific computing in particular.
There are a number of steps to scientific endeavors that involve computing:
Many of the standard tools impose barriers between one or more of these steps. This can make it difficult to iterate, reproduce work.
The Jupyter notebook eliminates or reduces these barriers to reproducibility.
IPython Notebook Viewer Displays static HTML versions of notebooks, and includes a gallery of notebook examples.
NotebookCloud A service that allows you to launch and control IPython Notebook servers on Amazon EC2 from your browser.
A Reference-Free Algorithm for Computational Normalization of Shotgun Sequencing Data A landmark example of reproducible research in genomics: Git repo, iPython notebook, data and scripts.
Jacques Ravel and K Eric Wommack. 2014. All Hail Reproducibility in Microbiome Research. Microbiome, 2:8.
Benjamin Ragan-Kelley et al.. 2013. Collaborative cloud-enabled tools allow rapid, reproducible biological insights. The ISME Journal, 7, 461–464; doi:10.1038/ismej.2012.123;