Commit 15d75bb5 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'enh/transform' into 'master'

fslpy tweaks

See merge request !29
parents 24882ffa 30e8c661
......@@ -21,17 +21,12 @@
"perform analyses and image processing in conjunction with FSL.\n",
"\n",
"\n",
"> **Note**: `fslpy` is distinct from `fslpython` - `fslpython` is the Python\n",
"> environment that is baked into FSL. `fslpy` is a Python library which is\n",
"> installed into the `fslpython` environment.\n",
"\n",
"\n",
"* [The `Image` class, and other data types](#the-image-class-and-other-data-types)\n",
" * [Creating images](#creating-images)\n",
" * [Working with image data](#working-with-image-data)\n",
" * [Loading other file types](#loading-other-file-types)\n",
" * [NIfTI coordinate systems](#nifti-coordinate-systems)\n",
" * [Image processing](#image-processing)\n",
" * [Transformations and resampling](#transformations-and-resampling)\n",
"* [FSL wrapper functions](#fsl-wrapper-functions)\n",
" * [In-memory images](#in-memory-images)\n",
" * [Loading outputs into Python](#loading-outputs-into-python)\n",
......@@ -51,6 +46,11 @@
" * [Working with atlases](#working-with-atlases)\n",
"\n",
"\n",
"> **Note**: `fslpy` is distinct from `fslpython` - `fslpython` is the Python\n",
"> environment that is baked into FSL. `fslpy` is a Python library which is\n",
"> installed into the `fslpython` environment.\n",
"\n",
"\n",
"Let's start with some standard imports and environment set-up:"
]
},
......@@ -67,7 +67,8 @@
"import nibabel as nib\n",
"import numpy as np\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
"warnings.filterwarnings(\"ignore\")\n",
"np.set_printoptions(suppress=True, precision=4)"
]
},
{
......@@ -97,6 +98,8 @@
" `fig` argument to plot overlays).\n",
" \"\"\"\n",
"\n",
" voxel = [int(round(v)) for v in voxel]\n",
"\n",
" data = np.asanyarray(data, dtype=np.float)\n",
" data[data <= 0] = np.nan\n",
"\n",
......@@ -189,6 +192,14 @@
"- Ability to load metadata from BIDS sidecar files\n",
"\n",
"\n",
"> The `Image` class behaves differently to the `nibabel.Nifti1Image`. For\n",
"> example, when you create an `Image` object, the default behaviour is to load\n",
"> the image data into memory. This is configurable however; take a look at\n",
"> [the\n",
"> documentation](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.image.html#fsl.data.image.Image)\n",
"> to explore all of the options.\n",
"\n",
"\n",
"Some simple image processing routines are also provided - these are covered\n",
"[below](#image-processing).\n",
"\n",
......@@ -213,8 +224,8 @@
"# load a FSL image - the file\n",
"# suffix is optional, just like\n",
"# in real FSL-land!\n",
"img = Image(op.join(stddir, 'MNI152_T1_1mm'))\n",
"print(img)"
"std1mm = Image(op.join(stddir, 'MNI152_T1_1mm'))\n",
"print(std1mm)"
]
},
{
......@@ -233,7 +244,7 @@
"# load a nibabel image, and\n",
"# convert it into an FSL image\n",
"nibimg = nib.load(op.join(stddir, 'MNI152_T1_1mm.nii.gz'))\n",
"img = Image(nibimg)"
"std1mm = Image(nibimg)"
]
},
{
......@@ -249,10 +260,28 @@
"metadata": {},
"outputs": [],
"source": [
"data = np.zeros((100, 100, 100))\n",
"data = np.zeros((182, 218, 182))\n",
"img = Image(data, xform=np.eye(4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have generated some data from another `Image` (or from a\n",
"`nibabel.Nifti1Image`) you can use the `header` option to set\n",
"the header information on the new image:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img = Image(data, header=std1mm.header)"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -266,7 +295,8 @@
"metadata": {},
"outputs": [],
"source": [
"img.save('empty.nii.gz')"
"img.save('empty')\n",
"!ls"
]
},
{
......@@ -403,7 +433,7 @@
"\n",
"* The\n",
" [`fsl.data.bitmap.Bitmap`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.bitmap.html)\n",
" class can be used to load a bitmap image (e.g. `jpg, `png`, etc) and\n",
" class can be used to load a bitmap image (e.g. `jpg`, `png`, etc) and\n",
" convert it to a NIfTI image.\n",
"* The\n",
" [`fsl.data.dicom.DicomImage`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.dicom.html)\n",
......@@ -471,12 +501,27 @@
"\n",
"# Apply the world->voxel\n",
"# affine to the coordinates\n",
"voxcoords = (np.dot(world2vox[:3, :3], mnicoords.T)).T + world2vox[:3, 3]\n",
"\n",
"# The code above is a bit fiddly, so\n",
"# instead of figuring it out, you can\n",
"# just use the transform() function:\n",
"voxcoords = (np.dot(world2vox[:3, :3], mnicoords.T)).T + world2vox[:3, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The code above is a bit fiddly, so instead of figuring it out, you can just\n",
"use the\n",
"[`affine.transform`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.affine.html#fsl.transform.affine.transform)\n",
"function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.affine import transform\n",
"\n",
"voxcoords = transform(mnicoords, world2vox)\n",
"\n",
"# just to double check, let's transform\n",
......@@ -615,14 +660,15 @@
"# To combine affines together, we\n",
"# have to list them in reverse -\n",
"# linear algebra is *weird*.\n",
"funcvox2mni = concat(fsl2mni, func2std, vox2fsl)"
"funcvox2mni = concat(fsl2mni, func2std, vox2fsl)\n",
"print(funcvox2mni)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Below we will use the\n",
"> In the next section 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",
......@@ -648,15 +694,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"> Note that in the above example we are only applying a linear transformation\n",
"> into MNI space - in reality you would also want to apply your non-linear\n",
"> structural-to-standard transformation too. But this is left as [an exercise\n",
"> for the\n",
"> reader](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.fnirt.html).\n",
"Note that in the above example we are only applying a linear transformation\n",
"into MNI space - in reality you would also want to apply your non-linear\n",
"structural-to-standard transformation too. This is covered in the next\n",
"section.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"image-processing\"></a>\n",
"### Image processing\n",
"<a class=\"anchor\" id=\"transformations-and-resampling\"></a>\n",
"### Transformations and resampling\n",
"\n",
"\n",
"Now, it's all well and good to look at t-statistic values and voxel\n",
......@@ -665,8 +710,13 @@
"this, we're going to resample our functional image into MNI space, so we can\n",
"overlay it on the MNI template. This can be done using some handy functions\n",
"from the\n",
"[`fsl.transform.flirt`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.flirt.html)\n",
"and\n",
"[`fsl.utils.image.resample`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.image.resample.html)\n",
"module:"
"modules.\n",
"\n",
"\n",
"Let's make sure we've got our source and reference images loaded:"
]
},
{
......@@ -675,28 +725,51 @@
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.flirt import fromFlirt\n",
"from fsl.utils.image.resample import resampleToReference\n",
"\n",
"featdir = op.join(op.join('08_fslpy', 'fmri.feat'))\n",
"tstat1 = Image(op.join(featdir, 'stats', 'tstat1'))\n",
"std = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))\n",
"std = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll load the `example_func2standard` FLIRT matrix, and adjust it so that\n",
"it transforms from functional *world* coordinates into standard *world*\n",
"coordinates - this is what is expected by the `resampleToReference` function,\n",
"used below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.flirt import fromFlirt\n",
"\n",
"# Load the func2standard FLIRT matrix, and adjust it\n",
"# so that it transforms from functional *world*\n",
"# coordinates into standard *world* coordinates -\n",
"# this is what is expected by the resampleToReference\n",
"# function, used below\n",
"func2std = np.loadtxt(op.join(featdir, 'reg', 'example_func2standard.mat'))\n",
"func2std = fromFlirt(func2std, tstat1, std, 'world', 'world')\n",
"\n",
"# All of the functions in the resample module\n",
"# return a numpy array containing the resampled\n",
"# data, and an adjusted voxel-to-world affine\n",
"# transformation. But when using the\n",
"# resampleToReference function, the affine will\n",
"# be the same as the MNI152 2mm affine, so we\n",
"# can ignore it.\n",
"func2std = fromFlirt(func2std, tstat1, std, 'world', 'world')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use `resampleToReference` to resample our functional data into\n",
"MNI152 space. This function returns a `numpy` array containing the resampled\n",
"data, and an adjusted voxel-to-world affine transformation. But in this case,\n",
"we know that the data will be aligned to MNI152, so we can ignore the affine:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.utils.image.resample import resampleToReference\n",
"\n",
"std_tstat1 = resampleToReference(tstat1, std, func2std)[0]\n",
"std_tstat1 = Image(std_tstat1, header=std.header)"
]
......@@ -725,15 +798,198 @@
"fig = ortho(std_tstat1, mnivoxels, cmap=plt.cm.inferno, fig=fig, cursor=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the example above, we resampled some data from functional space into\n",
"standard space using a linear transformation. But we all know that this is not\n",
"how things work in the real world - linear transformations are for kids. The\n",
"real world is full of lions and tigers and bears and warp fields.\n",
"\n",
"\n",
"The\n",
"[`fsl.transform.fnirt`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.fnirt.html#fsl.transform.fnirt.fromFnirt)\n",
"and\n",
"[`fsl.transform.nonlinear`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.nonlinear.html)\n",
"modules contain classes and functions for working with FNIRT-style warp fields\n",
"(modules for working with lions, tigers, and bears are still under\n",
"development).\n",
"\n",
"\n",
"Let's imagine that we have defined an ROI in MNI152 space, and we want to\n",
"project it into the space of our functional data. We can do this by combining\n",
"the nonlinear structural to standard registration produced by FNIRT with the\n",
"linear functional to structural registration generated by FLIRT. First of\n",
"all, we'll load images from each of the functional, structural, and standard\n",
"spaces:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"featdir = op.join('08_fslpy', 'fmri.feat')\n",
"func = Image(op.join(featdir, 'reg', 'example_func'))\n",
"struc = Image(op.join(featdir, 'reg', 'highres'))\n",
"std = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's say we have obtained our seed location in MNI152 coordinates. Let's\n",
"convert them to MNI152 voxels just to double check:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seedmni = [-48, -74, -9]\n",
"seedmnivox = transform(seedmni, std.getAffine('world', 'voxel'))\n",
"ortho(std.data, seedmnivox, cursor=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we'll load the FNIRT warp field, which encodes a nonlinear transformation\n",
"from structural space to standard space. FNIRT warp fields are often stored as\n",
"*coefficient* fields to reduce the file size, but in order to use it, we must\n",
"convert the coefficient field into a *deformation* (a.k.a. *displacement*)\n",
"field. This takes a few seconds:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.fnirt import readFnirt\n",
"from fsl.transform.nonlinear import coefficientFieldToDeformationField\n",
"\n",
"struc2std = readFnirt(op.join(featdir, 'reg', 'highres2standard_warp'), struc, std)\n",
"struc2std = coefficientFieldToDeformationField(struc2std)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll also load our FLIRT functional to structural transformation, adjust it\n",
"so that it transforms between voxel coordinate systems instead of the FSL\n",
"coordinate system, and invert so it can transform from structural voxels to\n",
"functional voxels:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.affine import invert\n",
"func2struc = np.loadtxt(op.join(featdir, 'reg', 'example_func2highres.mat'))\n",
"func2struc = fromFlirt(func2struc, func, struc, 'voxel', 'voxel')\n",
"struc2func = invert(func2struc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can transform our seed coordinates from MNI152 space into functional\n",
"space in two stages. First, we'll use our deformation field to transform from\n",
"MNI152 space into structural space:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seedstruc = struc2std.transform(seedmni, 'world', 'voxel')\n",
"seedfunc = transform(seedstruc, struc2func)\n",
"\n",
"print('Seed location in MNI coordinates: ', seedmni)\n",
"print('Seed location in functional voxels:', seedfunc)\n",
"ortho(func.data, seedfunc, cursor=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> FNIRT warp fields kind of work backwards - we can use them to transform\n",
"> reference coordinates into source coordinates, but would need to invert the\n",
"> warp field using `invwarp` if we wanted to transform from source coordinates\n",
"> into referemce coordinates.\n",
"\n",
"\n",
"Of course, we can also use our deformation field to resample an image from\n",
"structural space into MNI152 space. The `applyDeformation` function takes an\n",
"`Image` and a `DeformationField`, and returns a `numpy` array containing the\n",
"resampled data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fsl.transform.nonlinear import applyDeformation\n",
"\n",
"strucmni = applyDeformation(struc, struc2std)\n",
"\n",
"# remove low-valued voxels,\n",
"# just for visualisation below\n",
"strucmni[strucmni < 1] = 0\n",
"\n",
"fig = ortho(std.data, [45, 54, 45], cmap=plt.cm.gray)\n",
"fig = ortho(strucmni, [45, 54, 45], fig=fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `premat` option to `applyDeformation` can be used to specify our linear\n",
"functional to structural transformation, and hence resample a functional image\n",
"into MNI152 space:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tstatmni = applyDeformation(tstat1, struc2std, premat=func2struc)\n",
"tstatmni[tstatmni < 3] = 0\n",
"\n",
"fig = ortho(std.data, [45, 54, 45], cmap=plt.cm.gray)\n",
"fig = ortho(tstatmni, [45, 54, 45], fig=fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a few other useful functions tucked away in the\n",
"[fsl.utils.image](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.image.html)\n",
"package, with more to be added in the future. The [`fsl.transform`]() package\n",
"also contains a wealth of functionality for working with linear (FLIRT) and\n",
"non-linear (FNIRT) transformations.\n",
"[`fsl.utils.image`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.utils.image.html)\n",
"and\n",
"[`fsl.transform`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.transform.html)\n",
"packages, with more to be added in the future.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"fsl-wrapper-functions\"></a>\n",
......@@ -761,7 +1017,7 @@
"metadata": {},
"outputs": [],
"source": [
"from fsl.wrappers import bet, robustfov, LOAD\n",
"from fsl.wrappers import robustfov\n",
"\n",
"robustfov('08_fslpy/bighead', 'bighead_cropped')\n",
"\n",
......@@ -805,6 +1061,8 @@
"metadata": {},
"outputs": [],
"source": [
"from fsl.wrappers import bet\n",
"\n",
"bet('bighead_cropped', 'bighead_cropped_brain', f=0.3, m=True, s=True)\n",
"\n",
"render('bighead_cropped -b 40 '\n",
......@@ -878,6 +1136,8 @@
"metadata": {},
"outputs": [],
"source": [
"from fsl.wrappers import LOAD\n",
"\n",
"cropped = Image('bighead_cropped')\n",
"\n",
"# The loaded result is called \"output\",\n",
......
%% Cell type:markdown id: tags:
# `fslpy`
**Important:** Portions of this practical require `fslpy` 2.9.0, due to be
released with FSL 6.0.4, in Spring 2020.
[`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 FSL and 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)
* [Transformations and resampling](#transformations-and-resampling)
* [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)
* [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)
> **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.
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")
np.set_printoptions(suppress=True, precision=4)
```
%% 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, cursor=False, **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
:arg cursor: Show a cursor at the `voxel`
All other arguments are passed through to the `imshow` function.
:returns: The figure and orthogaxes (which can be passed back in as the
`fig` argument to plot overlays).
"""
voxel = [int(round(v)) for v in voxel]
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)
if cursor:
cargs = {'color' : (0, 1, 0), 'linewidth' : 1}
xax.axvline( y, **cargs)
xax.axhline(data.shape[2] - z, **cargs)
yax.axvline( x, **cargs)
yax.axhline(data.shape[2] - z, **cargs)
zax.axvline( x, **cargs)
zax.axhline(data.shape[1] - y, **cargs)
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:
```
def render(cmdline):
import shlex
import IPython.display as display
prefix = '-of screenshot.png -hl -c 2 '
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
> The `Image` class behaves differently to the `nibabel.Nifti1Image`. For
> example, when you create an `Image` object, the default behaviour is to load
> the image data into memory. This is configurable however; take a look at
> [the
> documentation](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.image.html#fsl.data.image.Image)
> to explore all of the options.
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)
std1mm = Image(op.join(stddir, 'MNI152_T1_1mm'))
print(std1mm)
```
%% 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)
std1mm = 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))
data = np.zeros((182, 218, 182))
img = Image(data, xform=np.eye(4))
```
%% Cell type:markdown id: tags:
If you have generated some data from another `Image` (or from a
`nibabel.Nifti1Image`) you can use the `header` option to set
the header information on the new image:
%% Cell type:code id: tags:
```
img = Image(data, header=std1mm.header)
```
%% 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')
img.save('empty')
!ls
```
%% 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: