Commit 295d2e89 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

WIP fslpy

parent a011cc1d
......@@ -28,12 +28,14 @@
" * [Loading other file types](#loading-other-file-types)\n",
" * [NIfTI coordinate systems](#nifti-coordinate-systems)\n",
" * [Image processing](#image-processing)\n",
"* [The `filetree`](#the-filetree)\n",
"* [Calling shell commands](#calling-shell-commands)\n",
"* [FSL wrapper functions](#fsl-wrapper-functions)\n",
" * [In-memory images](#in-memory-images)\n",
" * [Loading outputs into Python](#loading-outputs-into-python)\n",
" * [The `fslmaths` wrapper](#the-fslmaths-wrapper)\n",
"* [The `filetree`](#the-filetree)\n",
"* [Calling shell commands](#calling-shell-commands)\n",
" * [`runfsl` and `submit`](#runfsl-and-submit)\n",
" * [Redirecting output](#redirecting-output)\n",
"* [FSL atlases](#fsl-atlases)\n",
" * [Querying atlases](#querying-atlases)\n",
" * [Loading atlas images](#loading-atlas-images)\n",
......@@ -698,173 +700,6 @@
"non-linear (FNIRT) transformations.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"the-filetree\"></a>\n",
"## The `filetree`\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"calling-shell-commands\"></a>\n",
"## Calling shell commands\n",
"\n",
"\n",
"The\n",
"[`fsl.utils.run`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.run.html)\n",
"module provides the `run` and `runfsl` functions, which are wrappers around\n",
"the built-in [`subprocess`\n",
"library](https://docs.python.org/3/library/subprocess.html).\n",
"\n",
"\n",
"The defsault behaviour of `run` is to return the standard output of the\n",
"command:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.run import run\n",
"\n",
"# You can pass the command\n",
"# and its arguments as a single\n",
"# string, or as a sequence\n",
"print('Lines in this notebook:', run('wc -l 08_fslpy.md'))\n",
"print('Lines in this notebook:', run(['wc', '-l', '08_fslpy.md']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But you can use the `stdout`, `stderr` and `exitcode` arguments to control the\n",
"return value. Let's create a little script to demonstrate the options:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile mycmd\n",
"#!/usr/bin/env bash\n",
"exitcode=$1\n",
"\n",
"echo \"Standard output!\"\n",
"echo \"Standard error :(\" >&2\n",
"\n",
"exit $exitcode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And let's not forget to make it executable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!chmod a+x mycmd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('run(\"./mycmd 0\"): ',\n",
" run(\"./mycmd 0\"))\n",
"print('run(\"./mycmd 0\", stdout=False): ',\n",
" run(\"./mycmd 0\", stdout=False))\n",
"print('run(\"./mycmd 0\", exitcode=True):',\n",
" run(\"./mycmd 0\", exitcode=True))\n",
"print('run(\"./mycmd 0\", stdout=False, exitcode=True):',\n",
" run(\"./mycmd 0\", stdout=False, exitcode=True))\n",
"print('run(\"./mycmd 0\", stdout=True, stderr=True): ',\n",
" run(\"./mycmd 0\", stdout=True, stderr=True))\n",
"print('run(\"./mycmd 0\", stdout=True, stderr=True, exitcode=True):',\n",
" run(\"./mycmd 0\", stdout=True, stderr=True, exitcode=True))\n",
"\n",
"print('run(\"./mycmd 1\", exitcode=True):',\n",
" run(\"./mycmd 1\", exitcode=True))\n",
"print('run(\"./mycmd 1\", stdout=False, exitcode=True):',\n",
" run(\"./mycmd 1\", stdout=False, exitcode=True))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the command returns a non-0 exit code, the default behaviour (if you don't\n",
"set `exitcode=True`) is for an `Exception` to be raised:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('run(\"./mycmd 99\")', run(\"./mycmd 99\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `log` option allows for more fine-grained control over what is done with\n",
"the standard output and error streams:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"# Use 'tee' to redirect the stdout/stderr\n",
"# of the command to the stdout/stderr of\n",
"# the calling command (your python script):\n",
"print('Teeing:')\n",
"run('./mycmd 0', log={'tee' : True})\n",
"\n",
"# sleep a tiny bit, otherwise the outputs\n",
"# from the command above might get interspersed\n",
"# with the print statements below\n",
"time.sleep(0.5)\n",
"\n",
"# Use 'stdout'/'stderr' to redirect\n",
"# the stdout/stderr to files:\n",
"with open('stdout.log', 'wt') as o, \\\n",
" open('stderr.log', 'wt') as e:\n",
" run('./mycmd 0', log={'stdout' : o, 'stderr' : e})\n",
"print('\\nRedirected stdout:')\n",
"!cat stdout.log\n",
"print('\\nRedirected stderr:')\n",
"!cat stderr.log\n",
"\n",
"# Use 'cmd' to log the command to a file\n",
"# (useful for pipeline logging!)\n",
"with open('commands.log', 'wt') as cmdlog:\n",
" run('./mycmd 0', log={'cmd' : cmdlog})\n",
" run('wc -l 08_fslpy.md', log={'cmd' : cmdlog})\n",
"\n",
"print('\\nCommand log:')\n",
"!cat commands.log"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"fsl-wrapper-functions\"></a>\n",
"## FSL wrapper functions\n",
"\n",
......@@ -1089,7 +924,7 @@
"metadata": {},
"outputs": [],
"source": [
"from wrappers import fslmaths\n",
"from fsl.wrappers import fslmaths\n",
"fslmaths('bighead_cropped') \\\n",
" .mas( 'bighead_cropped_brain_mask') \\\n",
" .run( 'bighead_cropped_brain')\n",
......@@ -1121,6 +956,239 @@
"fig = ortho(erodedbrain.data, (80, 112, 85), cmap=plt.cm.inferno, fig=fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"the-filetree\"></a>\n",
"## The `filetree`\n",
"\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"calling-shell-commands\"></a>\n",
"## Calling shell commands\n",
"\n",
"\n",
"The\n",
"[`fsl.utils.run`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.run.html)\n",
"module provides the `run` and `runfsl` functions, which are wrappers around\n",
"the built-in [`subprocess`\n",
"library](https://docs.python.org/3/library/subprocess.html).\n",
"\n",
"\n",
"The default behaviour of `run` is to return the standard output of the\n",
"command:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.run import run\n",
"\n",
"# You can pass the command\n",
"# and its arguments as a single\n",
"# string, or as a sequence\n",
"print('Lines in this notebook:', run('wc -l 08_fslpy.md').strip())\n",
"print('Words in this notebook:', run(['wc', '-w', '08_fslpy.md']).strip())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But you can control what `run` returns, depending on your needs. Let's create\n",
"a little script to demonstrate the options:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile mycmd\n",
"#!/usr/bin/env bash\n",
"exitcode=$1\n",
"\n",
"echo \"Standard output!\"\n",
"echo \"Standard error :(\" >&2\n",
"\n",
"exit $exitcode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And let's not forget to make it executable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!chmod a+x mycmd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use the `stdout`, `stderr` and `exitcode` arguments to control the\n",
"return value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('run(\"./mycmd 0\"): ',\n",
" run(\"./mycmd 0\").strip())\n",
"print('run(\"./mycmd 0\", stdout=False): ',\n",
" run(\"./mycmd 0\", stdout=False))\n",
"print('run(\"./mycmd 0\", exitcode=True):',\n",
" run(\"./mycmd 0\", exitcode=True))\n",
"print('run(\"./mycmd 0\", stdout=False, exitcode=True):',\n",
" run(\"./mycmd 0\", stdout=False, exitcode=True))\n",
"print('run(\"./mycmd 0\", stderr=True): ',\n",
" run(\"./mycmd 0\", stderr=True))\n",
"print('run(\"./mycmd 0\", stdout=False, stderr=True): ',\n",
" run(\"./mycmd 0\", stdout=False, stderr=True).strip())\n",
"print('run(\"./mycmd 0\", stderr=True, exitcode=True):',\n",
" run(\"./mycmd 0\", stderr=True, exitcode=True))\n",
"\n",
"print('run(\"./mycmd 1\", exitcode=True):',\n",
" run(\"./mycmd 1\", exitcode=True))\n",
"print('run(\"./mycmd 1\", stdout=False, exitcode=True):',\n",
" run(\"./mycmd 1\", stdout=False, exitcode=True))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So if only one of `stdout`, `stderr`, or `exitcode` is `True`, `run` will only\n",
"return the corresponding value. Otherwise `run` will return a tuple which\n",
"contains the requested outputs.\n",
"\n",
"\n",
"If you run a command which returns a non-0 exit code, the default behaviour\n",
"(if you don't set `exitcode=True`) is for a `RuntimeError` to be raised:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"run(\"./mycmd 99\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"runfsl-and-submit\"></a>\n",
"### `runfsl` and `submit`\n",
"\n",
"\n",
"The `runfsl` function is a wrapper around `run` which simply makes sure that\n",
"the command you are calling is inside the `$FSLDIR/bin/` directory. It has the\n",
"same usage as the `run` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.run import runfsl\n",
"runfsl('fslroi 08_fslpy/bighead_cropped bighead_slices 0 -1 0 -1 90 5')\n",
"runfsl('fast -o bighead_fast bighead_slices')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"redirecting-output\"></a>\n",
"### Redirecting output\n",
"\n",
"\n",
"The `log` option, accepted by both `run` and `fslrun`, allows for more\n",
"fine-grained control over what is done with the standard output and error\n",
"streams.\n",
"\n",
"\n",
"You can use `'tee'` to redirect the standard output and error streams of the\n",
"command to the standard output and error streams of the calling command (your\n",
"python script):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('Teeing:')\n",
"_ = run('./mycmd 0', log={'tee' : True})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or you can use `'stdout'` and `'stderr'` to redirect the standard output and\n",
"error streams of the command to files:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('stdout.log', 'wt') as o, \\\n",
" open('stderr.log', 'wt') as e:\n",
" run('./mycmd 0', log={'stdout' : o, 'stderr' : e})\n",
"print('\\nRedirected stdout:')\n",
"!cat stdout.log\n",
"print('\\nRedirected stderr:')\n",
"!cat stderr.log"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, you can use `'cmd'` to log the command itself to a file (useful for\n",
"pipeline logging):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('commands.log', 'wt') as cmdlog:\n",
" run('./mycmd 0', log={'cmd' : cmdlog})\n",
" run('wc -l 08_fslpy.md', log={'cmd' : cmdlog})\n",
"\n",
"print('\\nCommand log:')\n",
"!cat commands.log"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
%% Cell type:markdown id: tags:
# `fslpy`
[`fslpy`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/) is a
Python library which is built into FSL, and contains a range of functionality
for working with neuroimaging data from Python.
This practical highlights some of the most useful features provided by
`fslpy`. You may find `fslpy` useful if you are writing Python code to
perform analyses and image processing in conjunction with FSL.
> **Note**: `fslpy` is distinct from `fslpython` - `fslpython` is the Python
> environment that is baked into FSL. `fslpy` is a Python library which is
> installed into the `fslpython` environment.
* [The `Image` class, and other data types](#the-image-class-and-other-data-types)
* [Creating images](#creating-images)
* [Working with image data](#working-with-image-data)
* [Loading other file types](#loading-other-file-types)
* [NIfTI coordinate systems](#nifti-coordinate-systems)
* [Image processing](#image-processing)
* [The `filetree`](#the-filetree)
* [Calling shell commands](#calling-shell-commands)
* [FSL wrapper functions](#fsl-wrapper-functions)
* [In-memory images](#in-memory-images)
* [Loading outputs into Python](#loading-outputs-into-python)
* [The `fslmaths` wrapper](#the-fslmaths-wrapper)
* [The `filetree`](#the-filetree)
* [Calling shell commands](#calling-shell-commands)
* [`runfsl` and `submit`](#runfsl-and-submit)
* [Redirecting output](#redirecting-output)
* [FSL atlases](#fsl-atlases)
* [Querying atlases](#querying-atlases)
* [Loading atlas images](#loading-atlas-images)
* [Working with atlases](#working-with-atlases)
Let's start with some standard imports and environment set-up:
%% Cell type:code id: tags:
```
%matplotlib inline
import matplotlib.pyplot as plt
import os
import os.path as op
import nibabel as nib
import numpy as np
import warnings
warnings.filterwarnings("ignore")
```
%% Cell type:markdown id: tags:
And a little function that we can use to generate a simple orthographic plot:
%% Cell type:code id: tags:
```
def ortho(data, voxel, fig=None, **kwargs):
"""Simple orthographic plot of a 3D array using matplotlib.
:arg data: 3D numpy array
:arg voxel: XYZ coordinates for each slice
:arg fig: Existing figure and axes for overlay plotting
All other arguments are passed through to the `imshow` function.
:returns: The figure and axes (which can be passed back in as the
`fig` argument to plot overlays).
"""
data = np.asanyarray(data, dtype=np.float)
data[data <= 0] = np.nan
x, y, z = voxel
xslice = np.flipud(data[x, :, :].T)
yslice = np.flipud(data[:, y, :].T)
zslice = np.flipud(data[:, :, z].T)
if fig is None:
fig = plt.figure()
xax = fig.add_subplot(1, 3, 1)
yax = fig.add_subplot(1, 3, 2)
zax = fig.add_subplot(1, 3, 3)
else:
fig, xax, yax, zax = fig
xax.imshow(xslice, **kwargs)
yax.imshow(yslice, **kwargs)
zax.imshow(zslice, **kwargs)
for ax in (xax, yax, zax):
ax.set_xticks([])
ax.set_yticks([])
fig.tight_layout(pad=0)
return (fig, xax, yax, zax)
```
%% Cell type:markdown id: tags:
And another function which uses FSLeyes for more complex plots:
%% Cell type:code id: tags:
```
import shlex
import IPython.display as display
from fsleyes.render import main
def render(cmdline):
prefix = '-of screenshot.png -hl -c 2 '
main(shlex.split(prefix + cmdline))
return display.Image('screenshot.png')
```
%% Cell type:markdown id: tags:
<a class="anchor" id="the-image-class-and-other-data-types"></a>
## The `Image` class, and other data types
The
[`fsl.data.image`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.image.html#fsl.data.image.Image)
module provides the `Image` class, which sits on top of `nibabel` and contains
some handy functionality if you need to work with coordinate transformations,
or do some FSL-specific processing. The `Image` class provides features such
as:
- Support for NIFTI1, NIFTI2, and ANALYZE image files
- Access to affine transformations between the voxel, FSL and world coordinate
systems
- Ability to load metadata from BIDS sidecar files
Some simple image processing routines are also provided - these are covered
[below](#image-processing).
<a class="anchor" id="creating-images"></a>
### Creating images
It's easy to create an `Image` - you can create one from a file name:
%% Cell type:code id: tags:
```
from fsl.data.image import Image
stddir = op.expandvars('${FSLDIR}/data/standard/')
# load a FSL image - the file
# suffix is optional, just like
# in real FSL-land!
img = Image(op.join(stddir, 'MNI152_T1_1mm'))
print(img)
```
%% Cell type:markdown id: tags:
You can create an `Image` from an existing `nibabel` image:
%% Cell type:code id: tags:
```
# load a nibabel image, and
# convert it into an FSL image
nibimg = nib.load(op.join(stddir, 'MNI152_T1_1mm.nii.gz'))
img = Image(nibimg)
```
%% Cell type:markdown id: tags:
Or you can create an `Image` from a `numpy` array:
%% Cell type:code id: tags:
```
data = np.zeros((100, 100, 100))
img = Image(data, xform=np.eye(4))
```
%% Cell type:markdown id: tags:
You can save an image to file via the `save` method:
%% Cell type:code id: tags:
```
img.save('empty.nii.gz')
```
%% Cell type:markdown id: tags:
`Image` objects have all of the attributes you might expect:
%% Cell type:code id: tags:
```
stddir = op.expandvars('${FSLDIR}/data/standard/')
std1mm = Image(op.join(stddir, 'MNI152_T1_1mm'))
print('name: ', std1mm.name)
print('file: ', std1mm.dataSource)
print('NIfTI version:', std1mm.niftiVersion)
print('ndim: ', std1mm.ndim)
print('shape: ', std1mm.shape)
print('dtype: ', std1mm.dtype)
print('nvals: ', std1mm.nvals)
print('pixdim: ', std1mm.pixdim)
```
%% Cell type:markdown id: tags:
and a number of useful methods:
%% Cell type:code id: tags:
```
std2mm = Image(op.join(stddir, 'MNI152_T1_2mm'))
mask2mm = Image(op.join(stddir, 'MNI152_T1_2mm_brain_mask'))
print(std1mm.sameSpace(std2mm))
print(std2mm.sameSpace(mask2mm))
print(std2mm.getAffine('voxel', 'world'))
```
%% Cell type:markdown id: tags:
An `Image` object is a high-level wrapper around a `nibabel` image object -
you can always work directly with the `nibabel` object via the `nibImage`
attribute:
%% Cell type:code id: tags:
```
print(std2mm)
print(std2mm.nibImage)
```
%% Cell type:markdown id: tags:
<a class="anchor" id="working-with-image-data"></a>
### Working with image data
You can get the image data as a `numpy` array via the `data` attribute:
%% Cell type:code id: tags:
```
data = std2mm.data
print(data.min(), data.max())
ortho(data, (45, 54, 45))
```
%% Cell type:markdown id: tags:
> Note that `Image.data` will give you the data in its underlying type, unlike
> the `nibabel.get_fdata` method, which up-casts image data to floating-point.
You can also read and write data directly via the `Image` object:
%% Cell type:code id: tags:
```
slc = std2mm[:, :, 45]
std2mm[0:10, :, :] *= 2
```
%% Cell type:markdown id: tags:
Doing so has some advantages that may or may not be useful, depending on your
use-case:
- The image data will be kept on disk - only the parts that you access will
be loaded into RAM (you will also need to pass`loadData=False` when creating
the `Image` to achieve this).
- The `Image` object will keep track of modifications to the data - this can
be queried via the `saveState` attribute.
<a class="anchor" id="loading-other-file-types"></a>
### Loading other file types
The
[`fsl.data`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.html#module-fsl.data)
package has a number of other classes for working with different types of FSL
and neuroimaging data. Most of these are higher-level wrappers around the
corresponding `nibabel` types:
* The
[`fsl.data.bitmap.Bitmap`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.bitmap.html)
class can be used to load a bitmap image (e.g. `jpg, `png`, etc) and
convert it to a NIfTI image.
* The
[`fsl.data.dicom.DicomImage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.dicom.html)
class uses `dcm2niix` to load NIfTI images contained within a DICOM
directory<sup>*</sup>.
* The
[`fsl.data.mghimahe.MGHImage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.mghimage.html)
class can be used too load `.mgh`/`.mgz` images (they are converted into
NIfTI images).
* The
[`fsl.data.dtifit`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.dtifit.html)
module contains functions for loading and working with the output of the
FSL `dtifit` tool.
* The
[`fsl.data.featanalysis`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.featanalysis.html),
[`fsl.data.featimage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.featimage.html),
and
[`fsl.data.featdesign`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.featdesign.html)
modules contain classes and functions for loading data from FEAT
directories.
* Similarly, the
[`fsl.data.melodicanalysis`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.melodicanalysis.html)
and
[`fsl.data.melodicimage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.melodicimage.html)
modules contain classes and functions for loading data from MELODIC
directories.
* The
[`fsl.data.gifti`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.gifti.html),
[`fsl.data.freesurfer`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.freesurfer.html),
and
[`fsl.data.vtk`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.vtk.html)
modules contain functionality form loading surface data from GIfTI,
freesurfer, and VTK files respectively.
> <sup>*</sup>You must make sure that `dcm2niix` is installed on your system
> in order to use this class.
<a class="anchor" id="nifti-coordinate-systems"></a>
### NIfTI coordinate systems
The `Image.getAffine` method gives you access to affine transformations which
can be used to convert coordinates between the different coordinate systems
associated with a NIfTI image. Have some MNI coordinates you'd like to convert
to voxels? Easy!
%% Cell type:code id: tags:
```
stddir = op.expandvars('${FSLDIR}/data/standard/')
std2mm = Image(op.join(stddir, 'MNI152_T1_2mm'))
mnicoords = np.array([[0, 0, 0],
[0, -18, 18]])
world2vox = std2mm.getAffine('world', 'voxel')
vox2world = std2mm.getAffine('voxel', 'world')
# Apply the world->voxel
# affine to the coordinates
voxcoords = (np.dot(world2vox[:3, :3], mnicoords.T)).T + world2vox[:3, 3]
# The code above is a bit fiddly, so
# instead of figuring it out, you can
# just use the transform() function:
from fsl.transform.affine import transform
voxcoords = transform(mnicoords, world2vox)
# just to double check, let's transform
# those voxel coordinates back into world
# coordinates
backtomni = transform(voxcoords, vox2world)
for m, v, b in zip(mnicoords, voxcoords, backtomni):
print(m, '->', v, '->', b)
```
%% Cell type:markdown id: tags:
> The `Image.getAffine` method can give you transformation matrices
> between any of these coordinate systems:
>
> - `'voxel'`: Image data voxel coordinates
> - `'world'`: mm coordinates, defined by the sform/qform of an image
> - `'fsl'`: The FSL coordinate system, used internally by many FSL tools
> (e.g. FLIRT)
Oh, that example was too easy I hear you say? Try this one on for size. Let's
say we have run FEAT on some task fMRI data, and want to get the MNI
coordinates of the voxel with peak activation.
> This is what people used to use `Featquery` for, back in the un-enlightened
> days.
Let's start by identifying the voxel with the biggest t-statistic:
%% Cell type:code id: tags:
```
featdir = op.join('08_fslpy', 'fmri.feat')
tstat1 = Image(op.join(featdir, 'stats', 'tstat1')).data
# Recall from the numpy practical that
# argmax gives us a 1D index into a
# flattened view of the array. We can
# use the unravel_index function to
# convert it into a 3D index.
peakvox = np.abs(tstat1).argmax()
peakvox = np.unravel_index(peakvox, tstat1.shape)
print('Peak voxel coordinates for tstat1:', peakvox, tstat1[peakvox])
```
%% Cell type:markdown id: tags:
Now that we've got the voxel coordinates in functional space, we need to
transform them into MNI space. FEAT provides a transformation which goes
directly from functional to standard space, in the `reg` directory:
%% Cell type:code id: tags:
```
func2std = np.loadtxt(op.join(featdir, 'reg', 'example_func2standard.mat'))
```
%% Cell type:markdown id: tags:
But ... wait a minute ... this is a FLIRT matrix! We can't just plug voxel
coordinates into a FLIRT matrix and expect to get sensible results, because
FLIRT works in an internal FSL coordinate system, which is not quite
`'voxel'`, and not quite `'world'`. So we need to do a little more work.
Let's start by loading our functional image, and the MNI152 template (the
source and reference images of our FLIRT matrix):
%% Cell type:code id: tags:
```
func = Image(op.join(featdir, 'reg', 'example_func'))
std = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))
```
%% Cell type:markdown id: tags:
Now we can use them to get affines which convert between all of the different
coordinate systems - we're going to combine them into a single uber-affine,
which transforms our functional-space voxels into MNI world coordinates via:
1. functional voxels -> FLIRT source space
2. FLIRT source space -> FLIRT reference space
3. FLIRT referece space -> MNI world coordinates
%% Cell type:code id: tags:
```
vox2fsl = func.getAffine('voxel', 'fsl')
fsl2mni = std .getAffine('fsl', 'world')
```
%% Cell type:markdown id: tags:
Combining two affines into one is just a simple dot-product. There is a
`concat()` function which does this for us, for any number of affines:
%% Cell type:code id: tags:
```
from fsl.transform.affine import concat
# To combine affines together, we
# have to list them in reverse -
# linear algebra is *weird*.
funcvox2mni = concat(fsl2mni, func2std, vox2fsl)
```
%% Cell type:markdown id: tags:
So we've now got some voxel coordinates from our functional data, and an
affine to transform into MNI world coordinates. The rest is easy:
%% Cell type:code id: tags:
```
mnicoords = transform(peakvox, funcvox2mni)
mnivoxels = transform(mnicoords, std.getAffine('world', 'voxel'))
mnivoxels = [int(round(v)) for v in mnivoxels]
print('Peak activation (MNI coordinates):', mnicoords)
print('Peak activation (MNI voxels): ', mnivoxels)
```
%% Cell type:markdown id: tags:
> Note that in the above example we are only applying a linear transformation
> into MNI space - in reality you would also want to apply your non-linear
> structural-to-standard transformation too. But this is left as [an exercise
> for the
> reader](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.fnirt.html).
<a class="anchor" id="image-processing"></a>
### Image processing
Now, it's all well and good to look at t-statistic values and voxel
coordinates and so on and so forth, but let's spice things up a bit and look
at some images. Let's display our peak activation location in MNI space. To do
this, we're going to resample our functional image into MNI space, so we can
overlay it on the MNI template. This can be done using some handy functions
from the
[`fsl.utils.image.resample`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.image.resample.html)
module:
%% Cell type:code id: tags:
```
from fsl.transform.flirt import fromFlirt
from fsl.utils.image.resample import resampleToReference
featdir = op.join(op.join('08_fslpy', 'fmri.feat'))
tstat1 = Image(op.join(featdir, 'stats', 'tstat1'))
std = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))
# Load the func2standard FLIRT matrix, and adjust it
# so that it transforms from functional *world*
# coordinates into standard *world* coordinates -
# this is what is expected by the resampleToReference
# function, used below
func2std = np.loadtxt(op.join(featdir, 'reg', 'example_func2standard.mat'))
func2std = fromFlirt(func2std, tstat1, std, 'world', 'world')
# All of the functions in the resample module
# return a numpy array containing the resampled
# data, and an adjusted voxel-to-world affine
# transformation. But when using the
# resampleToReference function, the affine will
# be the same as the MNI152 2mm affine, so we
# can ignore it.
std_tstat1 = resampleToReference(tstat1, std, func2std)[0]
std_tstat1 = Image(std_tstat1, header=std.header)
```
%% Cell type:markdown id: tags:
Now that we have our t-statistic image in MNI152 space, we can plot it in
standard space using `matplotlib`:
%% Cell type:code id: tags:
```
stddir = op.expandvars('${FSLDIR}/data/standard/')
std2mm = Image(op.join(stddir, 'MNI152_T1_2mm'))
std_tstat1 = std_tstat1.data
std_tstat1[std_tstat1 < 3] = 0
fig = ortho(std2mm.data, mnivoxels, cmap=plt.cm.gray)
fig = ortho(std_tstat1, mnivoxels, cmap=plt.cm.inferno, fig=fig)
```
%% Cell type:markdown id: tags:
There are a few other useful functions tucked away in the
[fsl.utils.image](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.image.html)
package, with more to be added in the future. The [`fsl.transform`]() package
also contains a wealth of functionality for working with linear (FLIRT) and
non-linear (FNIRT) transformations.
<a class="anchor" id="the-filetree"></a>
## The `filetree`
<a class="anchor" id="calling-shell-commands"></a>
## Calling shell commands
The
[`fsl.utils.run`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.run.html)
module provides the `run` and `runfsl` functions, which are wrappers around
the built-in [`subprocess`
library](https://docs.python.org/3/library/subprocess.html).
The defsault behaviour of `run` is to return the standard output of the
command:
%% Cell type:code id: tags:
```
from fsl.utils.run import run
# You can pass the command
# and its arguments as a single
# string, or as a sequence
print('Lines in this notebook:', run('wc -l 08_fslpy.md'))
print('Lines in this notebook:', run(['wc', '-l', '08_fslpy.md']))
```
%% Cell type:markdown id: tags:
But you can use the `stdout`, `stderr` and `exitcode` arguments to control the
return value. Let's create a little script to demonstrate the options:
%% Cell type:code id: tags:
```
%%writefile mycmd
#!/usr/bin/env bash
exitcode=$1
echo "Standard output!"
echo "Standard error :(" >&2
exit $exitcode
```
%% Cell type:markdown id: tags:
And let's not forget to make it executable:
%% Cell type:code id: tags:
```
!chmod a+x mycmd
```
%% Cell type:code id: tags:
```
print('run("./mycmd 0"): ',
run("./mycmd 0"))
print('run("./mycmd 0", stdout=False): ',
run("./mycmd 0", stdout=False))
print('run("./mycmd 0", exitcode=True):',
run("./mycmd 0", exitcode=True))
print('run("./mycmd 0", stdout=False, exitcode=True):',
run("./mycmd 0", stdout=False, exitcode=True))
print('run("./mycmd 0", stdout=True, stderr=True): ',
run("./mycmd 0", stdout=True, stderr=True))
print('run("./mycmd 0", stdout=True, stderr=True, exitcode=True):',
run("./mycmd 0", stdout=True, stderr=True, exitcode=True))