08_fslpy.md 7.39 KB
Newer Older
1
2
# `fslpy`

3
4
5
# THIS IS A WORK IN PROGRESS - DO NOT READ


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
`fslpy` is a Python library which is built into FSL, and contains a range of
functionality for working with neuroimaging data in an FSL context.

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)
* [FSL atlases](#fsl-atlases)
* [The `filetree`](#the-filetree)
* [NIfTI coordinate systems](#nifti-coordinate-systems)
* [Image processing](#image-processing)
* [FSL wrapper functions](#fsl-wrapper-functions)


<a class="anchor" id="the-image-class-and-other-data-types"></a>
## The `Image` class, and other data types


The `fsl.data.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).


### Creating images

It's easy to create an `Image` - you can create one from a file name:

```
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'))
```

You can crearte an `Image` from an existing `nibabel` image:

```
# 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)
``

Or you can create an `Image` from a `numpy` array:

```
data = np.zeros((100, 100, 100))
img = Image(data, xform=np.eye(4))
```





<a class="anchor" id="fsl-atlases"></a>
## FSL atlases

<a class="anchor" id="the-filetree"></a>
## The `filetree`

<a class="anchor" id="nifti-coordinate-systems"></a>
## NIfTI coordinate systems

<a class="anchor" id="image-processing"></a>
## Image processing

<a class="anchor" id="fsl-wrapper-functions"></a>
## FSL wrapper functions



<a class="anchor" id="nifti-coordinate-systems"></a>
## NIfTI coordinate systems



The `getAffine` method gives you access to affine transformations which can be
used to convert coordinates between the different coordinate systems
associated with an image. Have some MNI coordinates you'd like to convert to
voxels? Easy!

```
mnicoords = np.array([[0,   0,  0],
                      [0, -18, 18]])

world2vox = img.getAffine('world', 'voxel')
vox2world = img.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)
```

> 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:

```
featdir = op.join(op.join('05_nifti', 'fmri.feat'))

# The Image.data attribute returns a
# numpy array containing, well, the
# image data.
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])
```


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:

```
func2std = np.loadtxt(op.join(featdir, 'reg', 'example_func2standard.mat'))
```

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):

```
func = Image(op.join(featdir, 'reg', 'example_func'))
std  = Image(op.expandvars(op.join('$FSLDIR', 'data', 'standard', 'MNI152_T1_2mm')))
```


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


```
vox2fsl = func.getAffine('voxel', 'fsl')
fsl2mni = std .getAffine('fsl',   'world')
```

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:

```
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)
```

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:

```
mnicoords = transform(peakvox, funcvox2mni)
print('Peak activation (MNI coordinates):', mnicoords)
```


> 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 ;).


<a class="anchor" id="image-processing"></a>
## Image processing

Now, it's all well and good to look at t-statistric 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

```
from IPython.display import Image as Screenshot
!fsleyes render -of screenshot.png -std
```


### (Advanced) Transform coordinates with nonlinear warpfields


have to use your own dataset