Home
Neuroimaging Analysis
try%3A%0A%20%20%20%20import%20micropip%0A%20%20%20%20await%20micropip.install(%5B'dartbrains-tools'%2C%20'matplotlib'%2C%20'numpy'%2C%20'plotly'%5D)%0Aexcept%20ImportError%3A%0A%20%20%20%20pass%0A_marimo_book_micropip_done%20%3D%20True
Preprocessing
Written by Luke Chang
Being able to study brain activity associated with cognitive processes
in humans is an amazing achievement. However, there is an extraordinary
amount of noise and very low levels of signal, which makes it difficult
to make inferences about brain function using BOLD imaging. A critical
step before any analysis is to remove as much noise as possible. The
series of steps to remove noise comprise our neuroimaging data
preprocessing pipeline . See slides on our preprocessing lecture here . In this lab, we will go over the basics of preprocessing fMRI data using the fmriprep preprocessing pipeline. We will cover:
Image transformations (rigid body and affine)
Cost functions for image registration
Head motion correction (realignment)
Spatial normalization
Spatial smoothing
fMRIPrep automated preprocessing pipeline
There are other preprocessing steps that are also common, but not necessarily performed by all labs such as slice timing and distortion correction. We will not be discussing these in depth outside of the videos.
Let’s start with watching a short video by Martin Lindquist to get a general overview of the main steps of preprocessing and the basics of how to transform images and register them to other images.
mo.vstack(%5Bmo.md('%5Cn%20%20%20%20%20%20%20%20%23%20Preprocessing%5Cn%20%20%20%20%20%20%20%20*Written%20by%20Luke%20Chang*%5Cn%5Cn%20%20%20%20%20%20%20%20Being%20able%20to%20study%20brain%20activity%20associated%20with%20cognitive%20processes%5Cn%20%20%20%20%20%20%20%20in%20humans%20is%20an%20amazing%20achievement.%20However%2C%20there%20is%20an%20extraordinary%5Cn%20%20%20%20%20%20%20%20amount%20of%20noise%20and%20very%20low%20levels%20of%20signal%2C%20which%20makes%20it%20difficult%5Cn%20%20%20%20%20%20%20%20to%20make%20inferences%20about%20brain%20function%20using%20BOLD%20imaging.%20A%20critical%5Cn%20%20%20%20%20%20%20%20step%20before%20any%20analysis%20is%20to%20remove%20as%20much%20noise%20as%20possible.%20The%5Cn%20%20%20%20%20%20%20%20series%20of%20steps%20to%20remove%20noise%20comprise%20our%20*neuroimaging%20data%5Cn%20%20%20%20%20%20%20%20**preprocessing**%20pipeline*.%20See%20slides%20on%20our%20preprocessing%20lecture%20%5Bhere%5D(..%2Fimages%2Flectures%2FPreprocessing.pdf).%5Cn%20%20%20%20%20%20%20%20')%2C%20mo.image(img_src('preprocessing.png'))%2C%20mo.md('%5Cn%20%20%20%20%20%20%20%20In%20this%20lab%2C%20we%20will%20go%20over%20the%20basics%20of%20preprocessing%20fMRI%20data%20using%20the%20%5Bfmriprep%5D(https%3A%2F%2Ffmriprep.org%2F)%20preprocessing%20pipeline.%20We%20will%20cover%3A%5Cn%5Cn%20%20%20%20%20%20%20%20-%20**Image%20transformations**%20(rigid%20body%20and%20affine)%5Cn%20%20%20%20%20%20%20%20-%20**Cost%20functions**%20for%20image%20registration%5Cn%20%20%20%20%20%20%20%20-%20**Head%20motion%20correction**%20(realignment)%5Cn%20%20%20%20%20%20%20%20-%20**Spatial%20normalization**%5Cn%20%20%20%20%20%20%20%20-%20**Spatial%20smoothing**%5Cn%20%20%20%20%20%20%20%20-%20**fMRIPrep**%20automated%20preprocessing%20pipeline%5Cn%5Cn%20%20%20%20%20%20%20%20There%20are%20other%20preprocessing%20steps%20that%20are%20also%20common%2C%20but%20not%20necessarily%20performed%20by%20all%20labs%20such%20as%20slice%20timing%20and%20distortion%20correction.%20We%20will%20not%20be%20discussing%20these%20in%20depth%20outside%20of%20the%20videos.%5Cn%5Cn%20%20%20%20%20%20%20%20Let%E2%80%99s%20start%20with%20watching%20a%20short%20video%20by%20Martin%20Lindquist%20to%20get%20a%20general%20overview%20of%20the%20main%20steps%20of%20preprocessing%20and%20the%20basics%20of%20how%20to%20transform%20images%20and%20register%20them%20to%20other%20images.%5Cn%20%20%20%20%20%20%20%20')%5D)
import%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Aimport%20plotly.graph_objects%20as%20go%0Afrom%20pathlib%20import%20Path%0Afrom%20dartbrains_tools.mr_widgets%20import%20TransformCubeWidget%2C%20CostFunctionWidget%2C%20SmoothingWidget%0A%0Adef%20_find_root()%20-%3E%20Path%3A%0A%20%20%20%20for%20candidate%20in%20(Path.cwd()%2C%20*Path.cwd().resolve().parents)%3A%0A%20%20%20%20%20%20%20%20if%20(candidate%20%2F%20'book.yml').exists()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20candidate%0A%20%20%20%20return%20Path.cwd()%0A_ROOT%20%3D%20_find_root()%0AIMG_DIR%20%3D%20_ROOT%20%2F%20'images'%20%2F%20'preprocessing'%0A%0Adef%20img_src(filename%3A%20str)%3A%0A%20%20%20%20p%20%3D%20IMG_DIR%20%2F%20filename%0A%20%20%20%20return%20p%20if%20p.is_file()%20else%20f'..%2Fimages%2Fpreprocessing%2F%7Bfilename%7D'%0A%0Adef%20read_svg(filename%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20p%20%3D%20IMG_DIR%20%2F%20filename%0A%20%20%20%20if%20p.is_file()%3A%0A%20%20%20%20%20%20%20%20return%20p.read_text()%0A%20%20%20%20import%20urllib.request%0A%20%20%20%20with%20urllib.request.urlopen(f'https%3A%2F%2Fdartbrains.org%2Fimages%2Fpreprocessing%2F%7Bfilename%7D')%20as%20resp%3A%0A%20%20%20%20%20%20%20%20return%20resp.read().decode('utf-8')
mo.md('%5Cn%20%20%20%20')
VIDEO
mo.Html('%5Cn%20%20%20%20%20%20%3Ciframe%5Cn%20%20%20%20%20%20%20%20%20%20width%3D%22560%22%20height%3D%22315%22%5Cn%20%20%20%20%20%20%20%20%20%20src%3D%22https%3A%2F%2Fwww.youtube.com%2Fembed%2FQc3rRaJWOc4%22%5Cn%20%20%20%20%20%20%20%20%20%20frameborder%3D%220%22%20allowfullscreen%3E%5Cn%20%20%20%20%20%20%3C%2Fiframe%3E%5Cn%20%20%20%20%20%20')
mo.md('%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%20Image%20Transformations%5Cn%5Cn%20%20%20%20Ok%2C%20now%20let%E2%80%99s%20dive%20deeper%20into%20how%20we%20can%20transform%20images%20into%20different%20spaces%20using%20linear%20transformations.%5Cn%5Cn%20%20%20%20Recall%20from%20our%20introduction%20to%20neuroimaging%20data%20lab%2C%20that%20neuroimaging%20data%20is%20typically%20stored%20in%20a%20nifti%20container%2C%20which%20contains%20a%203D%20or%204D%20matrix%20of%20the%20voxel%20intensities%20and%20also%20an%20**affine%20matrix**%20that%20maps%20voxel%5Cn%20%20%20%20coordinates%20%5C%5C((i%2C%20j%2C%20k)%5C%5C)%20to%20world%20coordinates%20%5C%5C((x%2C%20y%2C%20z)%5C%5C).%5Cn%5Cn%5Cn%20%20%20%20%24%24%5C%5Cbegin%7Bbmatrix%7D%20x%20%5C%5C%5C%5C%20y%20%5C%5C%5C%5C%20z%20%5C%5C%5C%5C%201%20%5C%5Cend%7Bbmatrix%7D%20%3D%20%5C%5Cmathbf%7BA%7D%20%5C%5Cbegin%7Bbmatrix%7D%20i%20%5C%5C%5C%5C%20j%20%5C%5C%5C%5C%20k%20%5C%5C%5C%5C%201%20%5C%5Cend%7Bbmatrix%7D%24%24%5Cn%5Cn%20%20%20%20A%20**rigid%20body%20transformation**%20has%206%20parameters%3A%5Cn%20%20%20%20-%203%20**translations**%20(shift%20in%20x%2C%20y%2C%20z)%5Cn%20%20%20%20-%203%20**rotations**%20(around%20x%2C%20y%2C%20z%20axes)%5Cn%5Cn%20%20%20%20A%20full%20**affine%20transformation**%20adds%206%20more%20(12%20total)%3A%5Cn%20%20%20%20-%203%20**scale**%20factors%5Cn%20%20%20%20-%203%20**shear**%20parameters%5Cn%5Cn%20%20%20%20Let%E2%80%99s%20create%20an%20interactive%20plot%20to%20get%20an%20intuition%20for%20how%20these%20affine%20matrices%20can%20be%20used%20to%20transform%20a%203D%20image.%20The%20ghost%20(wireframe)%20shows%20the%20original%20position%3B%20the%20solid%20red%20cube%20shows%20the%20transformed%5Cn%20%20%20%20position.%5Cn%5Cn%20%20%20%20We%20can%20move%20the%20sliders%20to%20play%20with%20applying%20rigid%20body%20transforms%20to%20a%203D%20cube.%20A%20rigid%20body%20transformation%20has%206%20parameters%3A%20translation%20in%20x%2Cy%2C%20%26%20z%2C%20and%20rotation%20around%20each%20of%20these%20axes.%20The%20key%20thing%20to%20remember%20is%20that%20a%20rigid%20body%20transform%20doesn%E2%80%99t%20allow%20the%20image%20to%20be%20fundamentally%20changed.%20A%20full%2012%20parameter%20affine%20transformation%20adds%20an%20additional%203%20parameters%20each%20for%20scaling%20and%20shearing%2C%20which%20can%20change%20the%20shape%20of%20the%20cube.%5Cn%5Cn%20%20%20%20Try%20moving%20some%20of%20the%20sliders%20around.%20Each%20time%20you%20move%20a%20slider%20it%20is%20applying%20an%20affine%20transformation%20to%20the%20matrix%20and%20re-plotting.%5Cn%5Cn%20%20%20%20Translation%20moves%20the%20cube%20in%20x%2C%20y%2C%20and%20z%20dimensions.%5Cn%5Cn%20%20%20%20We%20can%20also%20rotate%20the%20cube%20around%20the%20x%2C%20y%2C%20and%20z%20axes%20where%20the%20origin%20is%20the%20center%20point.%20Continuing%20to%20rotate%20around%20the%20point%20will%20definitely%20lead%20to%20the%20cube%20leaving%20the%20current%20field%20of%20view%2C%20but%20it%20will%20come%20back%20if%20you%20keep%20rotating%20it.%5Cn%20%20%20%20')
tx_slider%20%3D%20mo.ui.slider(start%3D-15%2C%20stop%3D15%2C%20step%3D0.5%2C%20value%3D0%2C%20label%3D'Translate%20X')%0Aty_slider%20%3D%20mo.ui.slider(start%3D-15%2C%20stop%3D15%2C%20step%3D0.5%2C%20value%3D0%2C%20label%3D'Translate%20Y')%0Atz_slider%20%3D%20mo.ui.slider(start%3D-15%2C%20stop%3D15%2C%20step%3D0.5%2C%20value%3D0%2C%20label%3D'Translate%20Z')%0Arx_slider%20%3D%20mo.ui.slider(start%3D-180%2C%20stop%3D180%2C%20step%3D5%2C%20value%3D0%2C%20label%3D'Rotate%20X%20(%C2%B0)')%0Ary_slider%20%3D%20mo.ui.slider(start%3D-180%2C%20stop%3D180%2C%20step%3D5%2C%20value%3D0%2C%20label%3D'Rotate%20Y%20(%C2%B0)')%0Arz_slider%20%3D%20mo.ui.slider(start%3D-180%2C%20stop%3D180%2C%20step%3D5%2C%20value%3D0%2C%20label%3D'Rotate%20Z%20(%C2%B0)')%0Asx_slider%20%3D%20mo.ui.slider(start%3D0.5%2C%20stop%3D2.0%2C%20step%3D0.1%2C%20value%3D1.0%2C%20label%3D'Scale%20X')%0Asy_slider%20%3D%20mo.ui.slider(start%3D0.5%2C%20stop%3D2.0%2C%20step%3D0.1%2C%20value%3D1.0%2C%20label%3D'Scale%20Y')%0Asz_slider%20%3D%20mo.ui.slider(start%3D0.5%2C%20stop%3D2.0%2C%20step%3D0.1%2C%20value%3D1.0%2C%20label%3D'Scale%20Z')
Translation:
Rotation:
Scale:
_widget%20%3D%20TransformCubeWidget(trans_x%3Dfloat(tx_slider.value)%2C%20trans_y%3Dfloat(ty_slider.value)%2C%20trans_z%3Dfloat(tz_slider.value)%2C%20rot_x%3Dfloat(rx_slider.value)%2C%20rot_y%3Dfloat(ry_slider.value)%2C%20rot_z%3Dfloat(rz_slider.value)%2C%20scale_x%3Dfloat(sx_slider.value)%2C%20scale_y%3Dfloat(sy_slider.value)%2C%20scale_z%3Dfloat(sz_slider.value))%0A_wrapped%20%3D%20mo.ui.anywidget(_widget)%0Amo.vstack(%5Bmo.md('**Translation%3A**')%2C%20mo.hstack(%5Btx_slider%2C%20ty_slider%2C%20tz_slider%5D%2C%20justify%3D'start'%2C%20gap%3D1)%2C%20mo.md('**Rotation%3A**')%2C%20mo.hstack(%5Brx_slider%2C%20ry_slider%2C%20rz_slider%5D%2C%20justify%3D'start'%2C%20gap%3D1)%2C%20mo.md('**Scale%3A**')%2C%20mo.hstack(%5Bsx_slider%2C%20sy_slider%2C%20sz_slider%5D%2C%20justify%3D'start'%2C%20gap%3D1)%2C%20_wrapped%5D)
mo.callout(mo.md('**Try%20this%3A**%20Move%20translation%20sliders%20to%20shift%20the%20cube.%20Rotate%20around%20each%20axis.%20Change%20scale%20to%20stretch%2Fcompress%20(goes%20beyond%20rigid%20body!).%20The%20affine%20matrix%20on%20the%20right%20updates%20live.%20**Drag%20to%20orbit**%20the%203D%20view.')%2C%20kind%3D'info')
mo.md('%5Cn%20%20%20%20The%20affine%20matrix%20encodes%20all%20of%20these%20transformations%20compactly.%20The%5Cn%20%20%20%20upper-left%203%5C%5C(%5C%5Ctimes%5C%5C)3%20block%20contains%20rotation%20and%20scaling%2C%20while%20the%5Cn%20%20%20%20rightmost%20column%20contains%20translation.%20The%20bottom%20row%20is%20always%5Cn%20%20%20%20%5C%5C(%5B0%2C%200%2C%200%2C%201%5D%5C%5C).%5Cn%5Cn%20%20%20%20**Rotation%20matrices**%20for%20each%20axis%3A%5Cn%5Cn%20%20%20%20%24%24R_x(%5C%5Ctheta)%20%3D%20%5C%5Cbegin%7Bbmatrix%7D%201%20%26%200%20%26%200%20%5C%5C%5C%5C%200%20%26%20%5C%5Ccos%5C%5Ctheta%20%26%20-%5C%5Csin%5C%5Ctheta%20%5C%5C%5C%5C%200%20%26%20%5C%5Csin%5C%5Ctheta%20%26%20%5C%5Ccos%5C%5Ctheta%20%5C%5Cend%7Bbmatrix%7D%24%24%5Cn%5Cn%20%20%20%20%24%24R_y(%5C%5Cphi)%20%3D%20%5C%5Cbegin%7Bbmatrix%7D%20%5C%5Ccos%5C%5Cphi%20%26%200%20%26%20%5C%5Csin%5C%5Cphi%20%5C%5C%5C%5C%200%20%26%201%20%26%200%20%5C%5C%5C%5C%20-%5C%5Csin%5C%5Cphi%20%26%200%20%26%20%5C%5Ccos%5C%5Cphi%20%5C%5Cend%7Bbmatrix%7D%24%24%5Cn%5Cn%20%20%20%20%24%24R_z(%5C%5Cpsi)%20%3D%20%5C%5Cbegin%7Bbmatrix%7D%20%5C%5Ccos%5C%5Cpsi%20%26%20-%5C%5Csin%5C%5Cpsi%20%26%200%20%5C%5C%5C%5C%20%5C%5Csin%5C%5Cpsi%20%26%20%5C%5Ccos%5C%5Cpsi%20%26%200%20%5C%5C%5C%5C%200%20%26%200%20%26%201%20%5C%5Cend%7Bbmatrix%7D%24%24%5Cn%5Cn%20%20%20%20The%20combined%20rotation%20is%20%5C%5C(R%20%3D%20R_x%20%5C%5Ccdot%20R_y%20%5C%5Ccdot%20R_z%5C%5C)%2C%20and%20the%20full%5Cn%20%20%20%20affine%20matrix%20is%20built%20by%20combining%20rotation%2C%20scaling%2C%20and%20translation.%5Cn%5Cn%20%20%20%20Every%20time%20we%20apply%20a%20transformation%20to%20our%20images%2C%20the%20result%20is%20not%5Cn%20%20%20%20a%20perfect%20representation%20of%20the%20original%20data%20--%20we%20need%20**interpolation**%5Cn%20%20%20%20to%20fill%20gaps%2C%20which%20introduces%20small%20errors.%20This%20is%20why%20minimizing%20the%5Cn%20%20%20%20number%20of%20resampling%20steps%20matters%20in%20preprocessing.%5Cn%20%20%20%20')
mo.md('%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%20Cost%20Functions%5Cn%5Cn%20%20%20%20Now%20that%20we%20understand%20affine%20transformations%2C%20how%20do%20we%20use%20them%20to%5Cn%20%20%20%20**register**%20one%20brain%20image%20to%20another%3F%20We%20need%20a%20way%20to%20quantify%5Cn%20%20%20%20how%20well%20two%20images%20are%20aligned.%5Cn%5Cn%20%20%20%20The%20key%20is%20to%20identify%20a%20way%20to%20quantify%20how%20aligned%20the%20two%20images%20are%20to%20each%20other.%20Our%20visual%20systems%20are%20very%20good%20at%20identifying%20when%20two%20images%20are%20aligned%2C%20however%2C%20we%20need%20to%20create%20a%20quantitative%20alignment%20measure.%20These%20measures%20are%20called%5Cn%20%20%20%20**cost%20functions**.%5Cn%5Cn%20%20%20%20A%20common%20cost%20function%20is%20the%20**Sum%20of%20Squared%20Errors%20(SSE)**.%20You%20may%20remember%20that%20this%20same%20cost%20function%20is%20used%20by%20linear%20regression%20to%20find%20the%20best%20fitting%20line%20to%20data.%20This%20measure%20works%20best%20if%20the%20images%20are%20of%20the%20same%20type%20and%20have%20roughly%20equivalent%20signal%20intensities.%5Cn%5Cn%20%20%20%20%24%24SSE%20%3D%20%5C%5Csum_%7Bi%7D%20(I_%7Btarget%7D(i)%20-%20I_%7Breference%7D(i))%5E2%24%24%5Cn%5Cn%20%20%20%20The%20goal%20is%20to%20find%20the%20transformation%20parameters%20that%20**minimize**%5Cn%20%20%20%20the%20cost%20function.%20This%20process%20is%20called%20**optimization**.%5Cn%5Cn%20%20%20%20Let%E2%80%99s%20create%20another%20interactive%20plot%20and%20find%20the%20optimal%20X%20%26%20Y%20translation%20parameters%20that%20minimize%20the%20difference%20between%20a%20two-dimensional%20target%20image%20to%20a%20reference%20image.%20Try%20to%20align%20the%20target%20image%20(blue%20square)%20with%20the%20reference%20image%5Cn%20%20%20%20by%20adjusting%20the%20translation%20sliders.%20Watch%20the%20SSE%20drop%20to%20zero%20when%20perfectly%20aligned!%5Cn%20%20%20%20')
cost_tx%20%3D%20mo.ui.slider(start%3D0%2C%20stop%3D20%2C%20step%3D1%2C%20value%3D0%2C%20label%3D'Translate%20X')%0Acost_ty%20%3D%20mo.ui.slider(start%3D0%2C%20stop%3D20%2C%20step%3D1%2C%20value%3D0%2C%20label%3D'Translate%20Y')
_widget%20%3D%20CostFunctionWidget(trans_x%3Dfloat(cost_tx.value)%2C%20trans_y%3Dfloat(cost_ty.value))%0A_wrapped%20%3D%20mo.ui.anywidget(_widget)%0Amo.vstack(%5Bmo.hstack(%5Bcost_tx%2C%20cost_ty%5D%2C%20justify%3D'start'%2C%20gap%3D2)%2C%20_wrapped%2C%20mo.callout(mo.md('**Goal%3A**%20Find%20the%20translation%20that%20makes%20SSE%20%3D%200%20(perfect%20overlap%2C%20shown%20in%20green).%20This%20is%20exactly%20what%20registration%20algorithms%20do%20automatically%20--%20they%20search%20the%20parameter%20space%20to%20minimize%20the%20cost%20function.')%2C%20kind%3D'info')%5D)
mo.md('%5Cn%20%20%20%20You%20probably%20had%20to%20move%20the%20sliders%20around%20back%20and%20forth%20until%20you%20were%20able%20to%20reduce%20the%20sum%20of%20squared%20error%20to%20zero.%20This%20cost%20function%20increases%20exponentially%20the%20further%20you%20are%20away%20from%20your%20target.%20The%20process%20of%20minimizing%20(or%20sometimes%20maximizing)%20cost%20functions%20to%20identify%20the%20best%20fitting%20parameters%20is%20called%20optimization%20and%20is%20a%20concept%20that%20is%20core%20to%20fitting%20models%20to%20data%20across%20many%20different%20disciplines.%5Cn%5Cn%20%20%20%20Different%20cost%20functions%20work%20best%20for%20different%20image%20types%3A%5Cn%5Cn%20%20%20%20%7C%20Cost%20Function%20%7C%20Use%20Case%20%7C%20Example%20%7C%5Cn%20%20%20%20%7C%3A---%3A%7C%3A---%3A%7C%3A---%3A%7C%5Cn%20%20%20%20%7C%20Sum%20of%20Squared%20Error%20%7C%20Same%20modality%20%26%20scaling%20%7C%20Two%20T2*%20images%20%7C%5Cn%20%20%20%20%7C%20Normalized%20correlation%20%7C%20Same%20modality%20%7C%20Two%20T1%20images%20%7C%5Cn%20%20%20%20%7C%20Correlation%20ratio%20%7C%20Any%20modality%20%7C%20T1%20and%20FLAIR%20%7C%5Cn%20%20%20%20%7C%20Mutual%20information%20%7C%20Any%20modality%20%7C%20T1%20and%20CT%20%7C%5Cn%20%20%20%20%7C%20Boundary%20Based%20Registration%20%7C%20Contrast%20across%20boundaries%20%7C%20EPI%20and%20T1%20%7C%5Cn%20%20%20%20')
Realignment
Now let's put everything we learned together to understand how we can correct for head motion in functional images that occurred during a scanning session. It is extremely important to make sure that a specific voxel has the same 3D coordinate across all time points to be able to model neural processes. This of course is made difficult by the fact that participants move during a scanning session and also in between runs.
Realignment is the preprocessing step in which a rigid body transformation is applied to each volume to align them to a common space. One typically needs to choose a reference volume, which might be the first, middle, or last volume, or the mean of all volumes.
Let's look at an example of the translation and rotation parameters after running realignment on our first subject. Don't forget that even though we can approximately put each volume into a similar position with realignment, head motion always distorts the magnetic field and can lead to nonlinear changes in signal intensity that will not be addressed by this procedure. In the resting-state literature, where many analyses are based on functional connectivity, head motion can lead to spurious correlations. Some researchers choose to exclude any subject that moved more than a certain amount. Others choose to remove the impact of these time points in their data through removing the volumes via scrubbing or modeling out the volume with a dummy code in the first level general linear models.
mo.vstack(%5Bmo.md(%22%5Cn%20%20%20%20%20%20%20%20---%5Cn%20%20%20%20%20%20%20%20%23%23%20Realignment%5Cn%5Cn%20%20%20%20%20%20%20%20Now%20let's%20put%20everything%20we%20learned%20together%20to%20understand%20how%20we%20can%20correct%20for%20head%20motion%20in%20functional%20images%20that%20occurred%20during%20a%20scanning%20session.%20It%20is%20extremely%20important%20to%20make%20sure%20that%20a%20specific%20voxel%20has%20the%20same%203D%20coordinate%20across%20all%20time%20points%20to%20be%20able%20to%20model%20neural%20processes.%20This%20of%20course%20is%20made%20difficult%20by%20the%20fact%20that%20participants%20move%20during%20a%20scanning%20session%20and%20also%20in%20between%20runs.%5Cn%5Cn%20%20%20%20%20%20%20%20Realignment%20is%20the%20preprocessing%20step%20in%20which%20a%20rigid%20body%20transformation%20is%20applied%20to%20each%20volume%20to%20align%20them%20to%20a%20common%20space.%20One%20typically%20needs%20to%20choose%20a%20reference%20volume%2C%20which%20might%20be%20the%20first%2C%20middle%2C%20or%20last%20volume%2C%20or%20the%20mean%20of%20all%20volumes.%5Cn%5Cn%20%20%20%20%20%20%20%20Let's%20look%20at%20an%20example%20of%20the%20translation%20and%20rotation%20parameters%20after%20running%20realignment%20on%20our%20first%20subject.%5Cn%20%20%20%20%20%20%20%20%22)%2C%20mo.image(img_src('realignment_parameters.png'))%2C%20mo.md(%22%5Cn%20%20%20%20%20%20%20%20Don't%20forget%20that%20even%20though%20we%20can%20approximately%20put%20each%20volume%20into%20a%20similar%20position%20with%20realignment%2C%20head%20motion%20always%20distorts%20the%20magnetic%20field%20and%20can%20lead%20to%20nonlinear%20changes%20in%20signal%20intensity%20that%20will%20not%20be%20addressed%20by%20this%20procedure.%20In%20the%20resting-state%20literature%2C%20where%20many%20analyses%20are%20based%20on%20functional%20connectivity%2C%20head%20motion%20can%20lead%20to%20spurious%20correlations.%20Some%20researchers%20choose%20to%20exclude%20any%20subject%20that%20moved%20more%20than%20a%20certain%20amount.%20Others%20choose%20to%20remove%20the%20impact%20of%20these%20time%20points%20in%20their%20data%20through%20removing%20the%20volumes%20via%20*scrubbing*%20or%20modeling%20out%20the%20volume%20with%20a%20dummy%20code%20in%20the%20first%20level%20general%20linear%20models.%5Cn%20%20%20%20%20%20%20%20%22)%5D)
mo.md(%22%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%20Spatial%20Normalization%5Cn%5Cn%20%20%20%20There%20are%20several%20other%20preprocessing%20steps%20that%20involve%20image%20registration.%20The%20main%20one%20is%20called%20*spatial%20normalization*%2C%20in%20which%20each%20subject's%20brain%20data%20is%20warped%20into%20a%20common%20stereotactic%20space.%20Talairach%20is%20an%20older%20space%2C%20that%20has%20been%20subsumed%20by%20various%20standards%20developed%20by%20the%20Montreal%20Neurological%20Institute.%5Cn%5Cn%20%20%20%20There%20are%20a%20variety%20of%20algorithms%20to%20warp%20subject%20data%20into%20stereotactic%20space.%20Linear%2012%20parameter%20affine%20transformations%20have%20increasingly%20been%20replaced%20by%20more%20complicated%20nonlinear%20normalizations%20that%20have%20hundreds%20to%20thousands%20of%20parameters.%5Cn%5Cn%20%20%20%20One%20nonlinear%20algorithm%20that%20has%20performed%20very%20well%20across%20comparison%20studies%20is%20*diffeomorphic%20registration*%2C%20which%20can%20also%20be%20inverted%20so%20that%20subject%20space%20can%20be%20transformed%20into%20stereotactic%20space%20and%20back%20to%20subject%20space.%20This%20is%20the%20core%20of%20the%20%5BANTs%5D(http%3A%2F%2Fstnava.github.io%2FANTs%2F)%20algorithm%20that%20is%20implemented%20in%20fmriprep.%5Cn%5Cn%20%20%20%20Let's%20watch%20another%20short%20video%20by%20Martin%20Lindquist%20and%20Tor%20Wager%20to%20learn%20more%20about%20the%20core%20preprocessing%20steps.%5Cn%20%20%20%20%22)
VIDEO
mo.Html('%5Cn%20%20%20%20%20%20%3Ciframe%5Cn%20%20%20%20%20%20%20%20%20%20width%3D%22560%22%20height%3D%22315%22%5Cn%20%20%20%20%20%20%20%20%20%20src%3D%22https%3A%2F%2Fwww.youtube.com%2Fembed%2FqamRGWSC-6g%22%5Cn%20%20%20%20%20%20%20%20%20%20frameborder%3D%220%22%20allowfullscreen%3E%5Cn%20%20%20%20%20%20%3C%2Fiframe%3E%5Cn%20%20%20%20%20%20')
There are many different steps involved in the spatial normalization process and these details vary widely across various imaging software packages. We will briefly discuss some of the steps involved in the anatomical preprocessing pipeline implemented by fMRIprep and will be showing example figures from the output generated by the pipeline.
First, brains are extracted from the skull and surrounding dura mater. You can check and see how well the algorithm performed by examining the red outline. Next, the anatomical images are segmented into different tissue types. These tissue maps are used for various types of analyses, including providing a grey matter mask to reduce the computational time in estimating statistics. In addition, they provide masks to aid in extracting average activity in CSF, or white matter, which might be used as covariates in the statistical analyses to account for physiological noise.
mo.vstack(%5Bmo.md('%5Cn%20%20%20%20%20%20%20%20There%20are%20many%20different%20steps%20involved%20in%20the%20spatial%20normalization%20process%20and%20these%20details%20vary%20widely%20across%20various%20imaging%20software%20packages.%20We%20will%20briefly%20discuss%20some%20of%20the%20steps%20involved%20in%20the%20anatomical%20preprocessing%20pipeline%20implemented%20by%20fMRIprep%20and%20will%20be%20showing%20example%20figures%20from%20the%20output%20generated%20by%20the%20pipeline.%5Cn%5Cn%20%20%20%20%20%20%20%20First%2C%20brains%20are%20extracted%20from%20the%20skull%20and%20surrounding%20dura%20mater.%20You%20can%20check%20and%20see%20how%20well%20the%20algorithm%20performed%20by%20examining%20the%20red%20outline.%5Cn%20%20%20%20%20%20%20%20')%2C%20mo.image(img_src('T1_normalization.png'))%2C%20mo.md('%5Cn%20%20%20%20%20%20%20%20Next%2C%20the%20anatomical%20images%20are%20segmented%20into%20different%20tissue%20types.%20These%20tissue%20maps%20are%20used%20for%20various%20types%20of%20analyses%2C%20including%20providing%20a%20grey%20matter%20mask%20to%20reduce%20the%20computational%20time%20in%20estimating%20statistics.%20In%20addition%2C%20they%20provide%20masks%20to%20aid%20in%20extracting%20average%20activity%20in%20CSF%2C%20or%20white%20matter%2C%20which%20might%20be%20used%20as%20covariates%20in%20the%20statistical%20analyses%20to%20account%20for%20physiological%20noise.%5Cn%20%20%20%20%20%20%20%20')%2C%20mo.image(img_src('T1_segmentation.png'))%5D)
mo.md('%5Cn%20%20%20%20%23%23%23%20Spatial%20normalization%20of%20the%20anatomical%20T1w%20reference%5Cn%5Cn%20%20%20%20fmriprep%20uses%20%5BANTs%5D(http%3A%2F%2Fstnava.github.io%2FANTs%2F)%20to%20perform%20nonlinear%20spatial%20normalization.%20It%20is%20easy%20to%20check%20to%20see%20how%20well%20the%20algorithm%20performed%20by%20viewing%20the%20results%20of%20aligning%20the%20T1w%20reference%20to%20the%20stereotactic%20reference%20space.%20Hover%20on%20the%20panels%20with%20the%20mouse%20pointer%20to%20transition%20between%20both%20spaces.%20We%20are%20using%20the%20MNI152NLin2009cAsym%20template.%5Cn%20%20%20%20')
mo.Html(f%22%22%22%3Cdiv%20style%3D%22max-width%3A100%25%22%3E%7Bread_svg('sub-S01_space-MNI152NLin2009cAsym_T1w.svg')%7D%3C%2Fdiv%3E%22%22%22)
mo.md('%5Cn%20%20%20%20%23%23%23%20Alignment%20of%20functional%20and%20anatomical%20MRI%20data%5Cn%5Cn%20%20%20%20Next%2C%20we%20can%20evaluate%20the%20quality%20of%20alignment%20of%20the%20functional%20data%20to%20the%20anatomical%20T1%20image.%20FSL%20%60flirt%60%20was%20used%20to%20generate%20transformations%20from%20EPI-space%20to%20T1w-space%20%E2%80%94%20the%20white%20matter%20mask%20calculated%20with%20FSL%20%60fast%60%20(brain%20tissue%20segmentation)%20was%20used%20for%20BBR.%20Note%20that%20Nearest%20Neighbor%20interpolation%20is%20used%20in%20the%20reportlets%20in%20order%20to%20highlight%20potential%20spin-history%20and%20other%20artifacts%2C%20whereas%20final%20images%20are%20resampled%20using%20Lanczos%20interpolation.%20Notice%20these%20images%20are%20much%20blurrier%20and%20show%20some%20distortion%20compared%20to%20the%20T1s.%5Cn%20%20%20%20')