{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Between-volumes Motion Correction on DWI datasets\n\n## Overview\nDuring a dMRI acquisition, the subject motion inevitable. This motion implies\na misalignment between N volumes on a dMRI dataset. A common way to solve this\nissue is to perform a registration on each acquired volume to a\nreference b = 0. [JenkinsonSmith01]_\n\nThis preprocessing is an highly recommended step that should be executed before\nany dMRI dataset analysis.\n\n\nLet's import some essential functions.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from dipy.align import motion_correction\nfrom dipy.core.gradients import gradient_table\nfrom dipy.data import get_fnames\nfrom dipy.io.image import load_nifti, save_nifti\nfrom dipy.io.gradients import read_bvals_bvecs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We choose one of the data from the datasets in dipy_. However, you can\nreplace the following line with the path of your image.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dwi_fname, dwi_bval_fname, dwi_bvec_fname = get_fnames('sherbrooke_3shell')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We load the image and the affine of the image. The affine is the transformation\nmatrix which maps image coordinates to world (mm) coordinates. We also load the\nb-values and b-vectors.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data, affine = load_nifti(dwi_fname)\nbvals, bvecs = read_bvals_bvecs(dwi_bval_fname, dwi_bvec_fname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This data has 193 volumes. For this demo purpose, we decide to reduce the\nnumber of volumes to 5. However, we do not recommended to perform a motion\ncorrection with less than 10 volumes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_small = data[..., :3]\nbvals_small = bvals[:3]\nbvecs_small = bvecs[:3]\ngtab = gradient_table(bvals_small, bvecs_small)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start motion correction of our reduced DWI dataset(between-volumes motion\ncorrection).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_corrected, reg_affines = motion_correction(data_small, gtab, affine)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save our DWI dataset corrected to a new Nifti file.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "save_nifti('motion_correction.nii.gz', data_corrected.get_fdata(),\n data_corrected.affine)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n\n.. [JenkinsonSmith01] Jenkinson, M., Smith, S., 2001. A global optimisation\n method for robust affine registration of brain images. Med Image Anal 5\n (2), 143\u201356.\n\n.. include:: ../links_names.inc\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 0 }