Notebook is a wonderful environment to write your research notes. You can merge comments and code in a single document, pass this to your colleagues and let them check what you did.
Starting is super easy. It comes with the anaconda distribution. So just start with:
jupiter notebook
It will open the browser you chose as default and you will be able to open an existent notebook (files ending in *.ipynb as ipython notebook) or create a new one by opening the menu File -> New Notebook.
You can define a header by adding a pound sign (#) or two (##) to obtain different sizes in boldface.
# H1
## H2
### H3
#### H4
##### H5
###### H6
Italic: * asterisks * or _underscores_
Bold: ** asterisks ** or __ underscores __
asterisks or underscores
Combined: ** asterisks and __ underscores __ **
asterisks and underscores
Strikethrough: ~~ scratch this ~~
~~ scratch this ~~
Each of these symbols can be escaped using a backslash before it, such a \# or \*.
Let's learn with an example:
1. First ordered list item
2. Another item
⋅⋅* Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
⋅⋅1. Ordered sub-list
4. And another item.
⋅⋅⋅You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
⋅⋅⋅To have a line break without a paragraph, you will need to use two trailing spaces.⋅⋅ ⋅⋅⋅Note that this line is separate, but within the same paragraph.⋅⋅ ⋅⋅⋅(This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)
* Unordered list can use asterisks
- Or minuses
+ Or pluses
This is the result:
⋅⋅⋅You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).
⋅⋅⋅To have a line break without a paragraph, you will need to use two trailing spaces.⋅⋅ ⋅⋅⋅Note that this line is separate, but within the same paragraph.⋅⋅ ⋅⋅⋅(This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)
Mathematical formulae can be used by surrounding a typical Latex expression with \$ ... \$:
$ \int x^2 dx = x^3/3 $
You can add a link with [ linked name ] (http://...)
or simply by writing the link inside angle brackets:
or simply with http at the beginning:
You can write highlighted code surrounding it with ```python .... ```
print('hello world')
or the same with bash:
for i in (a b c);
do
echo $i
done
You can insert images with: ![alt](...)
To add an hovering title: ![alt text](... "some text"):
Tables are created using pipes "|"
Alignment inside the cells can be written using:
For instance:
| Column 1 | Column 2 | Column 3 |
| :- | :-: | :- |
| column | center | left |
will give:
Column 1 | Column 2 | Column 3 |
---|---|---|
column | center | left |
Command | Definition |
---|---|
Esc m | change to markdown cell |
Esc c | change to code cell |
Shift Enter | run cell |
Ctrl Enter | run cell in place |
Alt Enter | run cell and open new cell |
Esc Enter | go back to edit mode |
Ctrl Shift - | split a cell |
Shift + | merge the cell below |
Esc dd | delete the current cell |
Esc a | insert new cell above |
Esc b | insert new cell below |
Enter | switch to edit mode |
In [1]:
! pwd
In [2]:
names = !ls *.py
names[:3]
Out[2]:
See the source of python functions/classes with question marks (? or ??) The information appears in a pop-up window
In [3]:
%pycat?
%load
Use %load http://matplotlib.org/mpl_examples/pylab_examples/contour_demo.py to load the content of a python script.
%%writefile
Overwrite pythoncode.py
In [23]:
%%writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
%run
to run an external code or another notebook
In [27]:
%run ./foo.py
%load code
to load code directly into a cell
In [5]:
%lsmagic
Out[5]:
You absolutely need to know how to make appear plots in a page using: % matplolib inline
In [59]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# The semicolon at the end avoid to print the function
plt.hist(np.linspace(0, 1, 1000)**1.5);
%bash to run cell with bash
In [1]:
%%bash
for i in a b c;
do
echo $i
done
%%latex to render cell content in LaTeX
In [2]:
%%latex
\begin{align}
a = \frac{1}{2} && b = \frac{1}{3} && c = \frac{1}{4}\\
a && b && c \\
1 && 2 && 3
\end{align}
In [2]:
%load_ext rpy2.ipython
In [5]:
# example of R ...
%R X=c(1,4,5,7); sd(X); mean(X)
Out[5]:
Mixing Python and R
In [14]:
import numpy as np
import pylab
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
# Push variables in rpy2
%Rpush X Y
v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y)
# Compute the fit with R and save coefficients in Python
b = %R lm(Y~X)$coef
# Or compute variables in rpy2 and then pull them from the rpy2 space
#%R a=resid(lm(Y-X))
#%Rpull a
#print a, b
In [20]:
b = %R a=resid(lm(Y~X))
%Rpull a
print "a", a
print "b", b
We can also directly write in R
In [15]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
%time times a script
In [11]:
%time {1 for i in xrange(10*1000000)}
Out[11]:
%%timeit does several times the same operation and computes the average time
In [12]:
%%timeit
x = range(10000)
max(x)
To limit the number of repetitions use "-n number"
In [13]:
%%timeit -n 100
x = range(10000)
max(x)
To run profilers (processing and memory) you have to install further packages:
conda install line_profiler
conda install memory_profiler
%prun profiles a script by checking how much time each functions takes to run
In [30]:
from numpy.random import randn
def add_and_sum(x, y):
added = x + y
summed = added.sum(axis=1)
return summed
x = randn(3000, 3000)
y = randn(3000, 3000)
%prun add_and_sum(x, y)
In [34]:
!python -m cProfile foo.py
In [36]:
!python -m cProfile -o foo.out foo.py
In [37]:
import pstats
stats = pstats.Stats('foo.out')
stats.print_stats()
Out[37]:
%memit Profiling the memory usage
In [41]:
%load_ext memory_profiler
%memit range(1000000)
%memit list(range(1000000))
%mprun for profiling a code (testmem in the code mymem.py)
In [53]:
import numpy as np
def testmem():
a = np.arange(1000000)
b = list(range(1000000))
del(a)
del(b)
%mprun -f testmem testmem()
This works only on an external file. Results appear in another window. So, first let's write a code externally and then profile it.
In [64]:
%%writefile mymem.py
#mymem.py
import numpy as np
def testmem():
a = np.arange(1000000)
b = list(range(1000000))
del(a)
del(b)
testmem()
In [65]:
from mymem import testmem
%mprun -f testmem testmem()
In [67]:
from pythoncode import some_useless_slow_function, append_if_not_exists
In [68]:
%prun some_useless_slow_function()
In [69]:
%load_ext memory_profiler
%mprun -f append_if_not_exists some_useless_slow_function()
The line profiler doesn't currently work. We use the following work-around.
In [70]:
import line_profiler
lp = line_profiler.LineProfiler()
lp.add_function(some_useless_slow_function)
lp.runctx('some_useless_slow_function()', locals=locals(), globals=globals())
lp.print_stats()
In [72]:
import multiprocessing
print multiprocessing.cpu_count(), 'xeon cores'
In [4]:
from ipywidgets import widgets
In [5]:
from IPython.display import display
text = widgets.Text()
display(text)
def handle_submit(sender):
print(text.value)
text.on_submit(handle_submit)
In [6]:
button = widgets.Button(description="Click me !")
display(button)
def on_button_clicked(b):
print ("Yes, you clicked me !")
button.on_click(on_button_clicked)
In [8]:
from IPython.display import display
from ipywidgets import FloatProgress
import numpy as np
f = FloatProgress(min=0,max=1000)
f.value=0
display(f)
for i in range(1000):
a = np.exp(np.arange(100.))
f.value += 1
In [9]:
from ipywidgets import interact
def f(x):
print(x)
interact(f,x=10)
To create a checkbox:
In [15]:
interact(f, x=True);
To pass a string:
In [16]:
interact(f, x='Hi there!');
As a decorator of a function:
In [17]:
@interact(x=True, y=1.0)
def g(x, y):
return (x, y)
Finally, we can bind the input of one widget to the output of another one.
In [10]:
outputText = widgets.Text()
outputText
In [11]:
inputText = widgets.Text()
def makeUpperCase(sender):
outputText.value = inputText.value.upper()
inputText.on_submit(makeUpperCase)
inputText
Interactive visualization:
In [12]:
#from IPython.html.widgets import *
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
t = np.arange(0.0,1.0,0.01)
def pltsin(f):
plt.plot(t,np.sin(2*np.pi*t*f))
plt.show()
interact(pltsin, f=(1,10,0.1))
In [ ]: