File: motion_correction.py

package info (click to toggle)
dipy 1.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,144 kB
  • sloc: python: 92,240; makefile: 272; pascal: 183; sh: 162; ansic: 106
file content (67 lines) | stat: -rw-r--r-- 2,482 bytes parent folder | download | duplicates (2)
1
2
3
4
5
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
"""

=================================================
Between-volumes Motion Correction on DWI datasets
=================================================

During a dMRI acquisition, the subject motion inevitable. This motion implies
a misalignment between N volumes on a dMRI dataset. A common way to solve this
issue is to perform a registration on each acquired volume to a
reference b = 0 :footcite:p:`Jenkinson2001`.

This preprocessing is a highly recommended step that should be executed before
any dMRI dataset analysis.


Let's import some essential functions.
"""

from dipy.align import motion_correction
from dipy.core.gradients import gradient_table
from dipy.data import get_fnames
from dipy.io.gradients import read_bvals_bvecs
from dipy.io.image import load_nifti, save_nifti

###############################################################################
# We choose one of the data from the datasets in dipy_. However, you can
# replace the following line with the path of your image.

dwi_fname, dwi_bval_fname, dwi_bvec_fname = get_fnames(name="sherbrooke_3shell")

###############################################################################
# We load the image and the affine of the image. The affine is the
# transformation matrix which maps image coordinates to world (mm) coordinates.
# We also load the b-values and b-vectors.

data, affine = load_nifti(dwi_fname)
bvals, bvecs = read_bvals_bvecs(dwi_bval_fname, dwi_bvec_fname)

###############################################################################
# This data has 193 volumes. For this demo purpose, we decide to reduce the
# number of volumes to 3. However, we do not recommend to perform a motion
# correction with less than 10 volumes.

data_small = data[..., :3]
bvals_small = bvals[:3]
bvecs_small = bvecs[:3]
gtab = gradient_table(bvals_small, bvecs=bvecs_small)

###############################################################################
# Start motion correction of our reduced DWI dataset(between-volumes motion
# correction).

data_corrected, reg_affines = motion_correction(data_small, gtab, affine=affine)

###############################################################################
# Save our DWI dataset corrected to a new Nifti file.

save_nifti(
    "motion_correction.nii.gz", data_corrected.get_fdata(), data_corrected.affine
)

###############################################################################
# References
# ----------
#
# .. footbibliography::
#