Commit e66c9754 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

ENH: filetree section

parent f80d0658
......@@ -32,7 +32,11 @@
" * [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",
"* [The `FileTree`](#the-filetree)\n",
" * [Describing your data](#describing-your-data)\n",
" * [Using the `FileTree`](#using-the-filetree)\n",
" * [Building a processing pipeline with `FileTree`](#building-a-processing-pipeline-with-filetree)\n",
" * [The `FileTreeQuery`](#the-filetreequery)\n",
"* [Calling shell commands](#calling-shell-commands)\n",
" * [The `runfsl` function](#the-runfsl-function)\n",
" * [Submitting to the cluster](#submitting-to-the-cluster)\n",
......@@ -129,13 +133,24 @@
"metadata": {},
"outputs": [],
"source": [
"import shlex\n",
"import IPython.display as display\n",
"from fsleyes.render import main\n",
"\n",
"def render(cmdline):\n",
"\n",
" import shlex\n",
" import IPython.display as display\n",
"\n",
" prefix = '-of screenshot.png -hl -c 2 '\n",
" main(shlex.split(prefix + cmdline))\n",
"\n",
" try:\n",
" from fsleyes.render import main\n",
" main(shlex.split(prefix + cmdline))\n",
"\n",
" except ImportError:\n",
" # fall-back for macOS - we have to run\n",
" # FSLeyes render in a separate process\n",
" from fsl.utils.run import runfsl\n",
" prefix = 'render ' + prefix\n",
" runfsl(prefix + cmdline, env={})\n",
"\n",
" return display.Image('screenshot.png')"
]
},
......@@ -381,7 +396,7 @@
" class uses `dcm2niix` to load NIfTI images contained within a DICOM\n",
" directory<sup>*</sup>.\n",
"* The\n",
" [`fsl.data.mghimahe.MGHImage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.mghimage.html)\n",
" [`fsl.data.mghimage.MGHImage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.mghimage.html)\n",
" class can be used too load `.mgh`/`.mgz` images (they are converted into\n",
" NIfTI images).\n",
"* The\n",
......@@ -407,11 +422,12 @@
" and\n",
" [`fsl.data.vtk`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.vtk.html)\n",
" modules contain functionality form loading surface data from GIfTI,\n",
" freesurfer, and VTK files respectively.\n",
" freesurfer, and ASCII VTK files respectively.\n",
"\n",
"\n",
"> <sup>*</sup>You must make sure that `dcm2niix` is installed on your system\n",
"> in order to use this class.\n",
"> <sup>*</sup>You must make sure that\n",
"> [`dcm2niix`](https://github.com/rordenlab/dcm2niix/) is installed on your\n",
"> system in order to use this class.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"nifti-coordinate-systems\"></a>\n",
......@@ -592,6 +608,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"> Below we will use the\n",
"> [`fsl.transform.flirt.fromFlirt`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.flirt.html#fsl.transform.flirt.fromFlirt)\n",
"> function, which does all of the above for us.\n",
"\n",
"\n",
"So we've now got some voxel coordinates from our functional data, and an\n",
"affine to transform into MNI world coordinates. The rest is easy:"
]
......@@ -791,6 +812,10 @@
"> # \"twod\" applies the -2D flag\n",
"> flirt('source.nii.gz', 'ref.nii.gz', omat='src2ref.mat', twod=True)\n",
"> ```\n",
">\n",
"> Some of the `fsl.wrappers` functions also support aliases which may make\n",
"> your code more readable. For example, when calling `bet`, you can use either\n",
"> `m=True` or `mask=True` to apply the `-m` command line flag.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"in-memory-images\"></a>\n",
......@@ -830,7 +855,7 @@
"\n",
"\n",
"By using the special `fsl.wrappers.LOAD` symbol, you can also have any output\n",
"files produced by the tool automatically loaded:"
"files produced by the tool automatically loaded into memory for you:"
]
},
{
......@@ -840,7 +865,11 @@
"outputs": [],
"source": [
"cropped = Image('bighead_cropped')\n",
"betted = bet(cropped, LOAD)['output']\n",
"\n",
"# The loaded result is called \"output\",\n",
"# because that is the name of the\n",
"# argument in the bet wrapper function.\n",
"betted = bet(cropped, LOAD).output\n",
"\n",
"fig = ortho(cropped.data, (80, 112, 85), cmap=plt.cm.gray)\n",
"fig = ortho(betted .data, (80, 112, 85), cmap=plt.cm.inferno, fig=fig)"
......@@ -851,8 +880,7 @@
"metadata": {},
"source": [
"You can use the `LOAD` symbol for any output argument - any output files which\n",
"are loaded will be returned in a dictionary, with the argument name used as\n",
"the key:"
"are loaded will be available through the return value of the wrapper function:"
]
},
{
......@@ -869,9 +897,10 @@
"\n",
"aligned = flirt(tstat1, std2mm, applyxfm=True, init=func2std, out=LOAD)\n",
"\n",
"print(aligned)\n",
"\n",
"aligned = aligned['out'].data\n",
"# Here the resampled tstat image\n",
"# is called \"out\", because that\n",
"# is the name of the flirt argument.\n",
"aligned = aligned.out.data\n",
"aligned[aligned < 1] = 0\n",
"\n",
"fig = ortho(std2mm .data, (45, 54, 45), cmap=plt.cm.gray)\n",
......@@ -884,7 +913,7 @@
"source": [
"For tools like `bet` and `fast`, which expect an output *prefix* or\n",
"*basename*, you can just set the prefix to `LOAD` - all output files with that\n",
"prefix will be available in the returned dictionary:"
"prefix will be available in the object that is returned:"
]
},
{
......@@ -896,11 +925,9 @@
"img = Image('bighead_cropped')\n",
"betted = bet(img, LOAD, f=0.3, m=True)\n",
"\n",
"print(betted)\n",
"\n",
"fig = ortho(img .data, (80, 112, 85), cmap=plt.cm.gray)\n",
"fig = ortho(betted['output'] .data, (80, 112, 85), cmap=plt.cm.inferno, fig=fig)\n",
"fig = ortho(betted['output_mask'].data, (80, 112, 85), cmap=plt.cm.summer, fig=fig, alpha=0.5)"
"fig = ortho(img .data, (80, 112, 85), cmap=plt.cm.gray)\n",
"fig = ortho(betted.output .data, (80, 112, 85), cmap=plt.cm.inferno, fig=fig)\n",
"fig = ortho(betted.output_mask.data, (80, 112, 85), cmap=plt.cm.summer, fig=fig, alpha=0.5)"
]
},
{
......@@ -966,12 +993,280 @@
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"the-filetree\"></a>\n",
"## The `filetree`\n",
"## The `FileTree`\n",
"\n",
"\n",
"The\n",
"[`fsl.utils.filetree`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.filetree.html)\n",
"library provides functionality which allows you to work with *structured data\n",
"directories*, such as HCP or BIDS datasets. You can use `filetree` for both\n",
"reading and for creating datasets.\n",
"\n",
"\n",
"This practical gives a very brief introduction to the `filetree` library -\n",
"refer to the [full\n",
"documentation](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.filetree.html)\n",
"to get a feel for how powerful it can be.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"describing-your-data\"></a>\n",
"### Describing your data\n",
"\n",
"\n",
"To introduce `filetree`, we'll begin with a small example. Imagine that we\n",
"have a dataset which looks like this:\n",
"\n",
"\n",
"> ```\n",
"> mydata\n",
"> ├── sub_A\n",
"> │   ├── ses_1\n",
"> │   │   └── T1w.nii.gz\n",
"> │   ├── ses_2\n",
"> │   │   └── T1w.nii.gz\n",
"> │   └── T2w.nii.gz\n",
"> ├── sub_B\n",
"> │   ├── ses_1\n",
"> │   │   └── T1w.nii.gz\n",
"> │   ├── ses_2\n",
"> │   │   └── T1w.nii.gz\n",
"> │   └── T2w.nii.gz\n",
"> └── sub_C\n",
"> ├── ses_1\n",
"> │   └── T1w.nii.gz\n",
"> ├── ses_2\n",
"> │   └── T1w.nii.gz\n",
"> └── T2w.nii.gz\n",
"> ```\n",
"\n",
"\n",
"(Run the code cell below to create a dummy data set with the above structure):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"for sub in A B C; do\n",
" subdir=mydata/sub_$sub/\n",
" mkdir -p $subdir\n",
" cp $FSLDIR/data/standard/MNI152_T1_2mm.nii.gz $subdir/T2w.nii.gz\n",
" for ses in 1 2; do\n",
" sesdir=$subdir/ses_$ses/\n",
" mkdir $sesdir\n",
" cp $FSLDIR/data/standard/MNI152_T1_2mm.nii.gz $sesdir/T1w.nii.gz\n",
" done\n",
"done"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use `filetree` with this dataset, we must first describe its structure - we\n",
"do this by creating a `.tree` file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile mydata.tree\n",
"sub_{subject}\n",
" T2w.nii.gz\n",
" ses_{session}\n",
" T1w.nii.gz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A `.tree` file is simply a description of the structure of your data\n",
"directory - it describes the *file types* (also known as *templates*) which\n",
"are present in the dataset (`T1w` and `T2w`), and the *variables* which are\n",
"implicitly present in the structure of the dataset (`subject` and `session`).\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"using-the-filetree\"></a>\n",
"### Using the `FileTree`\n",
"\n",
"\n",
"Now that we have a `.tree` file which describe our data, we can create a\n",
"`FileTree` to work with it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.filetree import FileTree\n",
"\n",
"tree = FileTree.read('mydata.tree', 'mydata')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can list all of the T1 images via the `FileTree.get_all` method. The\n",
"`glob_vars='all'` option tells the `FileTree` to fill in the `T1w` template\n",
"with all possible combinations of variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for t1file in tree.get_all('T1w', glob_vars='all'):\n",
" fvars = tree.extract_variables('T1w', t1file)\n",
" print(t1file, fvars)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `FileTree.update` method allows you to \"fill in\" variable values; it\n",
"returns a new `FileTree` object which can be used on a selection of the\n",
"data set:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"treeA = tree.update(subject='A')\n",
"for t1file in treeA.get_all('T1w', glob_vars='all'):\n",
" fvars = treeA.extract_variables('T1w', t1file)\n",
" print(t1file, fvars)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"building-a-processing-pipeline-with-filetree\"></a>\n",
"### Building a processing pipeline with `FileTree`\n",
"\n",
"\n",
"todo\n",
"Let's say we want to run BET on all of our T1 images. Let's start by modifying\n",
"our `.tree` definition to include the BET outputs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile mydata.tree\n",
"sub_{subject}\n",
" T2w.nii.gz\n",
" ses_{session}\n",
" T1w.nii.gz\n",
" T1w_brain.nii.gz\n",
" T1w_brain_mask.nii.gz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use the `FileTree` to generate the relevant file names for us.\n",
"Here we'll use the `FileTree.get_all_trees` method to create a sub-tree for\n",
"each subject and each session:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.wrappers import bet\n",
"tree = FileTree.read('mydata.tree', 'mydata')\n",
"for subtree in tree.get_all_trees('T1w', glob_vars='all'):\n",
" t1file = subtree.get('T1w')\n",
" t1brain = subtree.get('T1w_brain')\n",
" print('Running BET: {} -> {} ...'.format(t1file, t1brain))\n",
" bet(t1file, t1brain, mask=True)\n",
"print('Done!')\n",
"\n",
"example = tree.update(subject='A', session='1')\n",
"render('{} {} -ot mask -ol -w 2 -mc 0 1 0'.format(\n",
" example.get('T1w'),\n",
" example.get('T1w_brain_mask')))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"the-filetreequery\"></a>\n",
"### The `FileTreeQuery`\n",
"\n",
"\n",
"The `filetree` module contains another class called the\n",
"[`FileTreeQuery`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.filetree.query.html),\n",
"which provides an interface that is more convenient if you are reading data\n",
"from large datasets with many different file types and variables.\n",
"\n",
"\n",
"When you create a `FileTreeQuery`, it scans the entire data directory and\n",
"identifies all of the values that are present for each variable defined in the\n",
"`.tree` file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.filetree import FileTreeQuery\n",
"tree = FileTree.read('mydata.tree', 'mydata')\n",
"query = FileTreeQuery(tree)\n",
"print('T1w variables:', query.variables('T1w'))\n",
"print('T2w variables:', query.variables('T2w'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `FileTreeQuery.query` method will return the paths to all existing files\n",
"which match a set of variable values:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('All T1w images for subject A')\n",
"for template in query.templates:\n",
" print(' ', template)\n",
" for file in query.query(template, subject='A'):\n",
" print(file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"calling-shell-commands\"></a>\n",
"## Calling shell commands\n",
"\n",
......@@ -1487,8 +1782,8 @@
"frontal = lblatlas.get(name='Frontal Pole').data\n",
"frontal = np.ma.masked_where(frontal < 1, frontal)\n",
"\n",
"fig = ortho(std2mm, (45, 54, 45), cmap=plt.cm.gray)\n",
"fig = ortho(frontal, (45, 54, 45), cmap=plt.cm.winter, fig=fig)"
"fig = ortho(std2mm.data, (45, 54, 45), cmap=plt.cm.gray)\n",
"fig = ortho(frontal, (45, 54, 45), cmap=plt.cm.winter, fig=fig)"
]
},
{
......@@ -1510,8 +1805,8 @@
"frontal = probatlas.get(name='Frontal Pole')\n",
"frontal = np.ma.masked_where(frontal < 1, frontal)\n",
"\n",
"fig = ortho(std2mm, (45, 54, 45), cmap=plt.cm.gray)\n",
"fig = ortho(frontal, (45, 54, 45), cmap=plt.cm.inferno, fig=fig)"
"fig = ortho(std2mm.data, (45, 54, 45), cmap=plt.cm.gray)\n",
"fig = ortho(frontal, (45, 54, 45), cmap=plt.cm.inferno, fig=fig)"
]
},
{
......
%% 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)
* [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)
* [The `FileTree`](#the-filetree)
* [Describing your data](#describing-your-data)
* [Using the `FileTree`](#using-the-filetree)
* [Building a processing pipeline with `FileTree`](#building-a-processing-pipeline-with-filetree)
* [The `FileTreeQuery`](#the-filetreequery)
* [Calling shell commands](#calling-shell-commands)
* [The `runfsl` function](#the-runfsl-function)
* [Submitting to the cluster](#submitting-to-the-cluster)
* [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):
import shlex
import IPython.display as display
prefix = '-of screenshot.png -hl -c 2 '
main(shlex.split(prefix + cmdline))
try:
from fsleyes.render import main
main(shlex.split(prefix + cmdline))
except ImportError:
# fall-back for macOS - we have to run
# FSLeyes render in a separate process
from fsl.utils.run import runfsl
prefix = 'render ' + prefix
runfsl(prefix + cmdline, env={})
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)
[`fsl.data.mghimage.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.
freesurfer, and ASCII VTK files respectively.
> <sup>*</sup>You must make sure that `dcm2niix` is installed on your system
> in order to use this class.
> <sup>*</sup>You must make sure that
> [`dcm2niix`](https://github.com/rordenlab/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:
> Below we will use the
> [`fsl.transform.flirt.fromFlirt`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.flirt.html#fsl.transform.flirt.fromFlirt)
> function, which does all of the above for us.
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="fsl-wrapper-functions"></a>
## FSL wrapper functions
The