-
Michiel Cottaar authoredMichiel Cottaar authored
Jupyter notebook and IPython
Our main interaction with python so far has been through the Jupyter notebook. These notebooks are extremely popular these days within the python scientific community, however they support many more languages, such as R and octave (and even matlab with the right plugin). They allow for interactive analysis of your data interspersed by explanatory notes (including LaTeX) with inline plotting. However, they can not be called as scripts on the command line or be imported from other python code, which makes them rather stand-alone. This makes them more useful for analysis that needs to be reproducible, but does not need to be replicated on different datasets (e.g., making a plot for a paper).
For more ad-hoc analysis it can be useful to just use the command line (i.e., a REPL).
We strongly recommend to use the IPython (available as ipython
in the terminal after you install ipython
using pip
or conda
) rather than default python REPL (available through python
or fslpython
)
Both Ipython and the jupyter notebook offer a whole range of magic commands, which all start with a %
sign.
- A magic command starting with a single
%
sign will only affect the single line. - A magic command starting with two '%' signs will affect the whole block of code.
Note that the normal python interpreter will not understand these magic commands, so you will have to take them out when writing a python script or library.
Here we will discuss some of the many features available to you in Ipython and the Jupyter notebook
Getting help
To get the documentation for any object or method simply append a question mark
import string
string.capwords?
Alternatively you can put two questions marks to get the complete code for the method or object class
import string
string.capwords??
Both Ipython and Jupyter also come with autocomplete, which is available at any time by pressing
Running shell commands
Commands starting with a !
will be sent to the shell rather than the python interpreter.
!fslstats ${FSLDIR}/data/standard/FMRIB58_FA_1mm.nii.gz -r
Running other programming languages
In the notebook you can include a whole code block using another language by using %%<language>
(for many languages you will have to install a toolkit first, just google your favorite language besides python)
%%bash
for filename in `ls *.md` ; do
head -n 1 ${filename}
done
Timing code
We can time a line of code with %time
or a whole code block using %%time
.
To get the time needed to calculate the sine of a million random numbers:
import numpy as np
numbers = np.random.rand(int(1e6))
%time np.sin(numbers)
For very fast evaluation, you might need to run it multiple times to get an accurate estimate. The %timeit
(or %%timeit
for a code block) takes care of this for you.
import numpy as np
numbers = np.random.rand(10)
%timeit np.sin(numbers)
Debugging
Despite your best efforts in many cases some error will crop up
import numpy as np
def total(a_list):
# create local copy befor changing the input
local_list = list(a_list)
total = 0.
while len(local_list) > 0:
total += local_list.pop(1) # returns element at index=1 and removes it
return total
print(total([2, 3, 4]))
You can always open a debugger at the location of the last error by using the %debug
magic command. You can find a list of commands available in the debugger here
%debug
Try to check the value of a_list
and local_list
from within the debugger.
If you always want to enter the debugger when an error is raised you can call %pdb on
at any time (call %pdf off
to rever this)
Enabling plotting
By far the most popular scientific plotting library is matplotlib.
You can enable plotting in Ipython or the jupyter notebook using %matplotlib <backend>
, where backend is the system that will be used to display the plots.
When failing to provide a backend it will simply use the default (which is usually fine).
- In the jupyter notebook use the
nbagg
backend for interactive plots or theinline
backend for non-interactive plots - Otherwise on Mac OSx use the
macosx
backend