diff --git a/getting_started/07_jupyter.ipynb b/getting_started/07_jupyter.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..77c8df4c0500f3792a1ae02f225e89351ba8d79c --- /dev/null +++ b/getting_started/07_jupyter.ipynb @@ -0,0 +1,357 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Jupyter notebook and IPython\n", + "Our main interaction with python so far has been through the [Jupyter notebook](http://jupyter.org/).\n", + "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](https://github.com/Calysto/matlab_kernel)).\n", + "They allow for interactive analysis of your data interspersed by explanatory notes (including LaTeX) with inline plotting.\n", + "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.\n", + "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).\n", + "\n", + "For more ad-hoc analysis it can be useful to just use the command line (i.e., a REPL).\n", + "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`)\n", + "\n", + "Both Ipython and the jupyter notebook offer a whole range of magic commands, which all start with a `%` sign.\n", + "* A magic command starting with a single `%` sign will only affect the single line.\n", + "* A magic command starting with two '%' signs will affect the whole block of code.\n", + "\n", + "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.\n", + "\n", + "Here we will discuss some of the many features available to you in Ipython and the Jupyter notebook\n", + "\n", + "---\n", + "\n", + "## Getting help\n", + "To get the documentation for any object or method simply append a question mark" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "string.capwords?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Alternatively you can put two questions marks to get the complete code for the method or object class" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "string.capwords??" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Both Ipython and Jupyter also come with autocomplete, which is available at any time by pressing <tab>\n", + "\n", + "---\n", + "\n", + "## Running shell commands\n", + "Commands starting with a `!` will be sent to the shell rather than the python interpreter." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!fslstats ${FSLDIR}/data/standard/FMRIB58_FA_1mm.nii.gz -r" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Running other programming languages\n", + "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)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "for filename in `ls *.md` ; do\n", + " head -n 1 ${filename}\n", + "done" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Timing code\n", + "We can time a line of code with `%time` or a whole code block using `%%time`.\n", + "To get the time needed to calculate the sine of a million random numbers:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "numbers = np.random.rand(int(1e6))\n", + "%time np.sin(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "numbers = np.random.rand(10)\n", + "%timeit np.sin(numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Debugging\n", + "Despite your best efforts in many cases some error will crop up" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "def total(a_list):\n", + " # create local copy befor changing the input\n", + " local_list = list(a_list)\n", + " total = 0.\n", + " while len(local_list) > 0:\n", + " total += local_list.pop(1) # returns element at index=1 and removes it\n", + " return total\n", + "\n", + "print(total([2, 3, 4]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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](http://www.georgejhunt.com/olpc/pydebug/pydebug/ipdb.html)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%debug" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Try to check the value of `a_list` and `local_list` from within the debugger.\n", + "\n", + "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)\n", + "\n", + "---\n", + "\n", + "## Enabling plotting\n", + "By far the most popular scientific plotting library is [matplotlib](https://matplotlib.org/).\n", + "You can enable plotting in Ipython or the jupyter notebook using `%matplotlib <backend>`, where [backend](https://matplotlib.org/faq/usage_faq.html#what-is-a-backend) is the system that will be used to display the plots.\n", + "When failing to provide a backend it will simply use the default (which is usually fine).\n", + "* In the jupyter notebook use the `nbagg` backend for interactive plots or the `inline` backend for non-interactive plots\n", + "* Otherwise on Mac OSx use the `macosx` backend" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib nbagg" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> Keep in mind that as soon as you have started plotting you can no longer change your backend without restarting python.\n", + "\n", + "To do the equivalent in a python script would look like" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib as mpl\n", + "mpl.use(<backend>)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For interactive use it can be handy to have all the `numpy` numeric functions and `matplotlib` plotting functions directly available without importing them explicitly.\n", + "This can be achieved using the `%pylab <backend>` magic command." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%pylab nbagg" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is equivalent in python code to:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib as mpl\n", + "mpl.use(<backend>)\n", + "from matplotlib.pylab import *" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I start most of my notebooks or terminals with the `%pylab` command, because afterwards I can just do stuff like:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = linspace(0, pi, 301)\n", + "y = sin(x)\n", + "plot(x, y, 'r-')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Exporting code from the Jupyter notebook\n", + "If you have a code cell in the jupyter notebook, that you want to convert into a script, you can use the %%writefile" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%writefile script_from_notebook.py\n", + "# a bunch of imports\n", + "import numpy as np\n", + "from datetime import datetime\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Any additional code cells need to contain the `-a` flag to stop jupyter from overwriting the original code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%writefile -a script_from_notebook.py\n", + "\n", + "print('today is ', datetime.now())\n", + "print('sin(3) is ', np.sin(3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now run this script" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!python script_from_notebook.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## Exporting code from the Ipython terminal\n", + "You can access the full history of your session using `%history`.\n", + "To save the history to a file use `%history -f <filename>`\n", + "You will probably have to clean a lot of erroneous commands you typed from that file before you are able to run it as a script." + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/getting_started/07_jupyter.md b/getting_started/07_jupyter.md new file mode 100644 index 0000000000000000000000000000000000000000..1a9090a254fda5ce3d0da46bf6e44301f09536e5 --- /dev/null +++ b/getting_started/07_jupyter.md @@ -0,0 +1,169 @@ +# Jupyter notebook and IPython +Our main interaction with python so far has been through the [Jupyter notebook](http://jupyter.org/). +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](https://github.com/Calysto/matlab_kernel)). +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 <tab> + +--- + +## 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](http://www.georgejhunt.com/olpc/pydebug/pydebug/ipdb.html) +``` +%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](https://matplotlib.org/). +You can enable plotting in Ipython or the jupyter notebook using `%matplotlib <backend>`, where [backend](https://matplotlib.org/faq/usage_faq.html#what-is-a-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 the `inline` backend for non-interactive plots +* Otherwise on Mac OSx use the `macosx` backend +``` +%matplotlib nbagg +``` +> Keep in mind that as soon as you have started plotting you can no longer change your backend without restarting python. + +To do the equivalent in a python script would look like +``` +import matplotlib as mpl +mpl.use(<backend>) +``` + +For interactive use it can be handy to have all the `numpy` numeric functions and `matplotlib` plotting functions directly available without importing them explicitly. +This can be achieved using the `%pylab <backend>` magic command. +``` +%pylab nbagg +``` + +This is equivalent in python code to: +``` +import matplotlib as mpl +mpl.use(<backend>) +from matplotlib.pylab import * +``` + +I start most of my notebooks or terminals with the `%pylab` command, because afterwards I can just do stuff like: +``` +x = linspace(0, pi, 301) +y = sin(x) +plot(x, y, 'r-') +``` + +--- + +## Exporting code from the Jupyter notebook +If you have a code cell in the jupyter notebook, that you want to convert into a script, you can use the %%writefile + +``` +%%writefile script_from_notebook.py +# a bunch of imports +import numpy as np +from datetime import datetime + +``` + +Any additional code cells need to contain the `-a` flag to stop jupyter from overwriting the original code +``` +%%writefile -a script_from_notebook.py + +print('today is ', datetime.now()) +print('sin(3) is ', np.sin(3)) +``` + +We can now run this script +``` +!python script_from_notebook.py +``` + +--- + +## Exporting code from the Ipython terminal +You can access the full history of your session using `%history`. +To save the history to a file use `%history -f <filename>` +You will probably have to clean a lot of erroneous commands you typed from that file before you are able to run it as a script. + diff --git a/getting_started/README.md b/getting_started/README.md index fe3b5f8aa6f044c91406d3e88d9a9b50d2a01753..f250beae1f7ac2a2f544f56d3929cd6b26ae6b87 100644 --- a/getting_started/README.md +++ b/getting_started/README.md @@ -9,6 +9,5 @@ This directory contains the "Getting started" practical. 4. Numpy (PM) 5. Nifti images (MJ) 6. Image manipulation (MC) -7. Moving from the Jupyter notebook to a text editor and terminal (MC) +7. Jupyter notebook and Ipython(MC) 8. Writing a callable script (MJ) -9. Debugging (MC) diff --git a/getting_started/basics.ipynb b/getting_started/basics.ipynb index 6a2266f2c052aae71d4339e8accfa75a6ab9d159..4cd6d7951303564e791df3d80c95220afd2ce159 100644 --- a/getting_started/basics.ipynb +++ b/getting_started/basics.ipynb @@ -483,13 +483,13 @@ { "ename": "IndexError", "evalue": "list index out of range", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-78-f4cf4536701c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" - ] + ], + "output_type": "error" } ], "source": [ @@ -504,13 +504,13 @@ { "ename": "IndexError", "evalue": "list index out of range", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-79-52d95fbe5286>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" - ] + ], + "output_type": "error" } ], "source": [ @@ -647,13 +647,13 @@ { "ename": "TypeError", "evalue": "list indices must be integers or slices, not list", - "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-84-aad7915ae3d8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not list" - ] + ], + "output_type": "error" } ], "source": [ diff --git a/getting_started/scripts.ipynb b/getting_started/scripts.ipynb index 1d3d705d5b636c281ce97e6a527431c69075847d..7b87ec6388573c9cebb864bdbea98da654593e9c 100644 --- a/getting_started/scripts.ipynb +++ b/getting_started/scripts.ipynb @@ -18,7 +18,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#!/usr/bin/env python" @@ -36,7 +38,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#!/usr/bin/env fslpython" @@ -56,7 +60,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import subprocess as sp\n", @@ -73,7 +79,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "spobj = sp.run(['ls'], stdout = sp.PIPE)" @@ -89,7 +97,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "spobj = sp.run('ls -la'.split(), stdout = sp.PIPE)\n", @@ -109,7 +119,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import os\n", @@ -131,7 +143,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "commands = \"\"\"\n", @@ -162,7 +176,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import sys\n", @@ -184,10 +200,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Usage: bash <input image> <output image>\n" + ] + } + ], "source": [ + "%%bash\n", "#!/bin/bash\n", "if [ $# -lt 2 ] ; then\n", " echo \"Usage: $0 <input image> <output image>\"\n", @@ -213,9 +238,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m<ipython-input-2-f7378930c369>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mspobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfsldir\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m'/bin/fslstats'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutfile\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'-V'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstdout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPIPE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0msout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mspobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mvol_vox\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0mvol_mm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Volumes are: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvol_vox\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m' in voxels and '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvol_mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m' in mm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ], + "output_type": "error" + } + ], "source": [ "#!/usr/bin/env fslpython\n", "import os, sys\n", @@ -235,9 +272,55 @@ "vol_mm = float(sout.split()[1])\n", "print('Volumes are: ', vol_vox, ' in voxels and ', vol_mm, ' in mm')" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.2" + }, + "toc": { + "colors": { + "hover_highlight": "#DAA520", + "running_highlight": "#FF0000", + "selected_highlight": "#FFD700" + }, + "moveMenuLeft": true, + "nav_menu": { + "height": "105px", + "width": "252px" + }, + "navigate_menu": true, + "number_sections": true, + "sideBar": true, + "threshold": 4.0, + "toc_cell": false, + "toc_section_display": "block", + "toc_window_display": false + } + }, "nbformat": 4, "nbformat_minor": 2 }