Introduction to Magnetic Resonance Imaging
try%3A%0A%20%20%20%20import%20micropip%0A%20%20%20%20await%20micropip.install(%5B'dartbrains-tools'%2C%20'numpy'%2C%20'plotly'%5D)%0Aexcept%20ImportError%3A%0A%20%20%20%20pass%0A_marimo_book_micropip_done%20%3D%20True
mo.md('%5Cn%20%20%20%20%23%20MR%20Physics%3A%20From%20Protons%20to%20Brain%20Images%5Cn%5Cn%20%20%20%20*Written%20by%20Luke%20Chang%20%26%20Ben%20Graul*%5Cn%5Cn%20%20%20%20How%20do%20MRI%20scanners%20work%3F%20And%20what%20signal%20are%20we%20actually%20measuring%3F%5Cn%20%20%20%20Understanding%20the%20physics%20underlying%20MRI%20is%20essential%20for%20interpreting%5Cn%20%20%20%20neuroimaging%20results%2C%20yet%20many%20people%20who%20use%20MRI%20don%5C't%20fully%20grasp%5Cn%20%20%20%20how%20it%20works%20--%20largely%20because%20MR%20physics%20can%20be%20unintuitive.%5Cn%5Cn%20%20%20%20This%20course%20primarily%20focuses%20on%20Blood%20Oxygenated%20Level%20Dependent%20(BOLD)%20fMRI%20signals.%20Gaining%20a%20deep%20understanding%20of%20the%20MR%20physics%20and%20physiological%20basis%20for%20the%20BOLD%20fMRI%20signal%20is%20beyond%20the%20scope%20of%20this%20course%20and%20we%20refer%20the%20interested%20reader%20to%20the%20excellent%20%5BHuettel%2C%20Song%2C%20%26%20McCarthy%20(2004)%20Functional%20magnetic%20resonance%20imaging%20textbook%5D(https%3A%2F%2Fwww.amazon.com%2FFunctional-Magnetic-Resonance-Imaging-Huettel%2Fdp%2F0878936270%2Fref%3Dpd_sbs_14_1%2F144-9493364-1935804%3F_encoding%3DUTF8%26pd_rd_i%3D0878936270%26pd_rd_r%3Dac61b1df-17bf-47c5-8db5-25dfa36bcd16%26pd_rd_w%3DJ61zv%26pd_rd_wg%3Dd1O2i%26pf_rd_p%3D703f3758-d945-4136-8df6-a43d19d750d1%26pf_rd_r%3DPCEXDFT3TQQ4JW7FD8HF%26psc%3D1%26refRID%3DPCEXDFT3TQQ4JW7FD8HF)%20for%20a%20more%20in%20depth%20conceptual%20and%20quantitative%20overview.%5Cn%5Cn%20%20%20%20The%20goal%20of%20this%20tutorial%20is%20to%20build%20your%20intuition%20from%20the%20ground%5Cn%20%20%20%20up%2C%20starting%20with%20things%20you%20already%20know%20(magnets%20and%20compasses)%20and%5Cn%20%20%20%20working%20toward%20functional%20brain%20imaging.%20Along%20the%20way%2C%20you%5C'll%20interact%5Cn%20%20%20%20with%20simulations%20that%20let%20you%20**see**%20and%20**feel**%20how%20changing%20physical%5Cn%20%20%20%20parameters%20affects%20the%20signal.%5Cn%5Cn%20%20%20%20%3E%20**Note%20on%20simplifications%3A**%20We%5C'll%20use%20*classical*%20physics%20explanations%5Cn%20%20%20%20%3E%20throughout%2C%20following%20the%20approach%20advocated%20by%5Cn%20%20%20%20%3E%20%5BLars%20G.%20Hanson%5D(https%3A%2F%2Fwww.drcmr.dk%2Fbloch).%20While%20not%20quantum-mechanically%5Cn%20%20%20%20%3E%20complete%2C%20this%20classical%20picture%20provides%20strong%20intuitions%20that%20will%5Cn%20%20%20%20%3E%20serve%20you%20well.%20Where%20the%20full%20story%20differs%2C%20we%5C'll%20flag%20it%20in%5Cn%20%20%20%20%3E%20optional%20%22Deep%20Dive%22%20sections.%5Cn%5Cn%20%20%20%20This%20notebook%20covers%20three%20major%20topics%3A%5Cn%20%20%20%201.%20**Magnetism%20%26%20Resonance**%20%E2%80%94%20magnetic%20fields%2C%20proton%20alignment%2C%20precession%2C%20RF%20excitation%2C%20and%20the%20FID%20signal%5Cn%20%20%20%202.%20**Signal%20%26%20Contrast**%20%E2%80%94%20T%E2%82%81%2FT%E2%82%82%20relaxation%2C%20the%20Bloch%20equations%2C%20tissue%20contrast%2C%20and%20pulse%20sequences%5Cn%20%20%20%203.%20**Imaging%20%26%20fMRI**%20%E2%80%94%20gradients%2C%20spatial%20encoding%2C%20k-space%2C%20the%20BOLD%20signal%2C%20and%20EPI%5Cn%20%20%20%20')
import%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Aimport%20plotly.graph_objects%20as%20go%0Afrom%20pathlib%20import%20Path%0Afrom%20plotly.subplots%20import%20make_subplots%0Afrom%20dartbrains_tools.mr_simulations%20import%20GAMMA_H%2C%20GAMMA%2C%20TISSUE_PROPERTIES%2C%20rotation_x%2C%20rotation_y%2C%20rotation_z%2C%20apply_rf_pulse%2C%20apply_relaxation%2C%20simulate_bloch%2C%20fid_signal%2C%20compute_spectrum%2C%20t1_recovery%2C%20t2_decay%2C%20spin_echo_signal%2C%20gradient_echo_signal%2C%20hrf%2C%20image_to_kspace%2C%20kspace_to_image%2C%20mask_kspace%2C%20plot_magnetization_3d%2C%20plot_signal_timeline%2C%20plot_contrast_bars%2C%20plot_pulse_sequence%2C%20plot_kspace_and_image%0Afrom%20dartbrains_tools.mr_widgets%20import%20CompassWidget%2C%20NetMagnetizationWidget%2C%20PrecessionWidget%2C%20SpinEnsembleWidget%2C%20EncodingWidget%2C%20KSpaceWidget%2C%20ConvolutionWidget%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'signal_generation'%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%2Fsignal_generation%2F%7Bfilename%7D'
mo.md('%5Cn%20%20%20%20')
mo.md(%22%5Cn%20%20%20%20Let's%20first%20start%20with%20a%20short%20video%20on%20the%20basics%20of%20MR%20physics%20by%20Martin%20Lindquist.%5Cn%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%20Part%201%3A%20Magnetism%20%26%20Resonance%5Cn%5Cn%20%20%20%20---%5Cn%20%20%20%20%22)
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%2FXsDXxgjEJVY%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(%22%5Cn%20%20%20%20%23%23%201.%20What%20is%20a%20Magnetic%20Field%3F%5Cn%5Cn%20%20%20%20You've%20used%20a%20compass%20before%20--%20a%20tiny%20magnetized%20needle%20that%20aligns%5Cn%20%20%20%20with%20the%20Earth's%20magnetic%20field%20to%20point%20north.%20This%20simple%20device%5Cn%20%20%20%20illustrates%20the%20core%20idea%20behind%20MRI%3A%20**magnetic%20objects%20tend%20to%20align%5Cn%20%20%20%20with%20an%20applied%20magnetic%20field.**%5Cn%5Cn%20%20%20%20The%20Earth's%20magnetic%20field%20is%20weak%20--%20only%20about%2025-65%20*micro*Teslas%5Cn%20%20%20%20(%C2%B5T).%20An%20MRI%20scanner%20produces%20a%20field%20that%20is%20roughly%20**100%2C000%20times%5Cn%20%20%20%20stronger**%3A%20typically%201.5%20or%203%20Tesla%20(T)%2C%20with%20research%20scanners%5Cn%20%20%20%20going%20up%20to%207T%20or%20beyond.%5Cn%5Cn%20%20%20%20Let's%20start%20with%20a%20simulation%20inspired%20by%20the%5Cn%20%20%20%20%5BDRCMR%20Compass%20MR%20Simulator%5D(https%3A%2F%2Fwww.drcmr.dk%2FCompassMR%2F).%5Cn%20%20%20%20Use%20the%20slider%20below%20to%20change%20the%20strength%20of%20the%20external%20magnetic%5Cn%20%20%20%20field%20(%5C%5C(B_0%5C%5C))%20and%20watch%20how%20a%20compass%20needle%20responds.%5Cn%20%20%20%20%22)
b0_compass_slider%20%3D%20mo.ui.slider(start%3D0.0%2C%20stop%3D5.0%2C%20step%3D0.1%2C%20value%3D3.0%2C%20label%3D'B%E2%82%80%20field%20strength%20(mT)'%2C%20full_width%3DTrue)
_widget%20%3D%20CompassWidget(b0%3Dfloat(b0_compass_slider.value))%0A_wrapped%20%3D%20mo.ui.anywidget(_widget)%0Amo.vstack(%5B_wrapped%5D)
mo.md(%22%5Cn%20%20%20%20**Key%20observations%3A**%5Cn%20%20%20%20-%20With%20no%20field%20(%5C%5C(B_0%20%3D%200%5C%5C))%2C%20the%20needle%20just%20sits%20wherever%20it%20is%20--%20no%20preferred%20direction.%5Cn%20%20%20%20-%20As%20you%20increase%20%5C%5C(B_0%5C%5C)%2C%20the%20needle%20oscillates%20back%20toward%20alignment%20**faster**.%5Cn%20%20%20%20-%20The%20**frequency**%20of%20oscillation%20increases%20with%20field%20strength.%5Cn%20%20%20%20-%20The%20oscillation%20is%20detected%20by%20a%20nearby%20coil%20as%20a%20**signal**%20that%20decays%20over%20time.%5Cn%5Cn%20%20%20%20These%20same%20principles%20apply%20inside%20an%20MRI%20scanner%2C%20except%20instead%20of%20compass%5Cn%20%20%20%20needles%2C%20we're%20working%20with%20hydrogen%20nuclei%20--%20tiny%20magnets%20inside%20your%20body.%5Cn%20%20%20%20%22)
mo.md(%22%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%202.%20From%20Compass%20to%20Proton%5Cn%5Cn%20%20%20%20We%20don't%20have%20compass%20needles%20inside%20the%20body%2C%20but%20do%20have%20a%20large%20number%20of%20**hydrogen%20nuclei**%20(protons).%5Cn%20%20%20%20Hydrogen%20is%20the%20most%20abundant%20element%20in%20the%20body%20(it's%20in%20every%20water%5Cn%20%20%20%20molecule)%2C%20and%20each%20hydrogen%20nucleus%20acts%20like%20a%20tiny%20magnet%20because%20of%5Cn%20%20%20%20a%20quantum%20property%20called%20**spin**.%5Cn%5Cn%20%20%20%20In%20the%20absence%20of%20an%20external%20magnetic%20field%2C%20these%20tiny%20magnets%20point%5Cn%20%20%20%20in%20random%20directions%2C%20and%20their%20effects%20cancel%20out%20--%20there's%20no%20net%5Cn%20%20%20%20magnetization.%20But%20when%20we%20place%20them%20in%20a%20strong%20%5C%5C(B_0%5C%5C)%20field%2C%20a%5Cn%20%20%20%20slight%20majority%20align%20*with*%20the%20field%20rather%20than%20*against*%20it.%20This%5Cn%20%20%20%20tiny%20surplus%20creates%20a%20measurable%20**net%20magnetization**%20vector%2C%20called%5Cn%20%20%20%20%5C%5C(M_0%5C%5C)%2C%20that%20points%20along%20%5C%5C(B_0%5C%5C).%5Cn%5Cn%20%20%20%20Use%20the%20sliders%20below%20to%20see%20how%20random%20spins%20create%20a%20net%20magnetization%5Cn%20%20%20%20when%20a%20field%20is%20applied.%5Cn%20%20%20%20%22)
n_protons_slider%20%3D%20mo.ui.slider(start%3D10%2C%20stop%3D500%2C%20step%3D10%2C%20value%3D100%2C%20label%3D'Number%20of%20protons')%0Ab0_on_toggle%20%3D%20mo.ui.switch(label%3D'B%E2%82%80%20field%20ON'%2C%20value%3DFalse)
_widget%20%3D%20NetMagnetizationWidget(n_protons%3Dint(n_protons_slider.value)%2C%20b0_on%3Dbool(b0_on_toggle.value))%0A_wrapped%20%3D%20mo.ui.anywidget(_widget)%0Amo.vstack(%5Bmo.hstack(%5Bn_protons_slider%2C%20b0_on_toggle%5D%2C%20justify%3D'start'%2C%20gap%3D2)%2C%20_wrapped%2C%20mo.callout(mo.md('With%20the%20field%20**OFF**%2C%20spins%20point%20in%20random%20directions%20and%20jitter%20freely%20--%20the%20net%20magnetization%20(red%20arrow)%20is%20near%20zero.%20Toggle%20B%E2%82%80%20**ON**%20to%20watch%20the%20spins%20gradually%20align%2C%20and%20a%20net%20magnetization%20emerges%20along%20z.%20**Drag%20to%20rotate**%20the%203D%20view.')%2C%20kind%3D'info')%5D)
mo.md(%22%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%203.%20Precession%20%26%20the%20Larmor%20Frequency%5Cn%5Cn%20%20%20%20When%20a%20spinning%20top%20is%20tilted%2C%20it%20doesn't%20just%20fall%20over%20--%20it%5Cn%20%20%20%20**precesses**%2C%20tracing%20a%20circle%20as%20it%20wobbles%20around%20the%20vertical%20axis.%5Cn%20%20%20%20Protons%20in%20a%20magnetic%20field%20do%20the%20same%20thing%3A%20their%20spin%20axes%20precess%5Cn%20%20%20%20around%20the%20direction%20of%20%5C%5C(B_0%5C%5C).%5Cn%5Cn%20%20%20%20The%20rate%20of%20precession%20is%20governed%20by%20the%20**Larmor%20equation**%3A%5Cn%5Cn%20%20%20%20%24%24%5C%5Comega_0%20%3D%20%5C%5Cgamma%20%5C%5Ccdot%20B_0%24%24%5Cn%5Cn%20%20%20%20where%3A%5Cn%20%20%20%20-%20%5C%5C(%5C%5Comega_0%5C%5C)%20is%20the%20Larmor%20(precession)%20frequency%5Cn%20%20%20%20-%20%5C%5C(%5C%5Cgamma%5C%5C)%20is%20the%20**gyromagnetic%20ratio**%20--%20a%20constant%20unique%20to%20each%20nucleus%5Cn%20%20%20%20-%20%5C%5C(B_0%5C%5C)%20is%20the%20magnetic%20field%20strength%5Cn%5Cn%20%20%20%20For%20hydrogen%20protons%2C%20%5C%5C(%5C%5Cgamma%20%3D%2042.576%5C%5C)%20MHz%2FT.%20This%20means%20at%203T%2C%5Cn%20%20%20%20protons%20precess%20at%20about%20**127.7%20MHz**%20--%20in%20the%20radiofrequency%20(RF)%5Cn%20%20%20%20range!%5Cn%5Cn%20%20%20%20Adjust%20the%20field%20strength%20below%20and%20see%20how%20the%20precession%20frequency%20changes.%5Cn%20%20%20%20%22)
b0_larmor_slider%20%3D%20mo.ui.slider(start%3D0.5%2C%20stop%3D7.0%2C%20step%3D0.1%2C%20value%3D3.0%2C%20label%3D'B%E2%82%80%20(Tesla)'%2C%20full_width%3DTrue)
_b0%20%3D%20b0_larmor_slider.value%0A_larmor_freq%20%3D%20GAMMA_H%20*%20_b0%0A_widget%20%3D%20PrecessionWidget(b0%3D_b0%2C%20flip_angle%3D30.0%2C%20show_relaxation%3DFalse)%0A_wrapped%20%3D%20mo.ui.anywidget(_widget)%0Amo.vstack(%5Bb0_larmor_slider%2C%20_wrapped%5D)
Larmor frequencies of different nuclei at this field strength:
| Nucleus |
γ (MHz/T) |
Larmor freq at 3.0T |
| 1H |
42.576 |
127.73 MHz |
| 13C |
10.708 |
32.12 MHz |
| 23Na |
11.262 |
33.79 MHz |
| 31P |
17.235 |
51.70 MHz |
_b0%20%3D%20b0_larmor_slider.value%0A_larmor_freq%20%3D%20GAMMA_H%20*%20_b0%0Amo.vstack(%5Bmo.callout(mo.md(f'At%20**B%E2%82%80%20%3D%20%7B_b0%3A.1f%7D%20T**%2C%20the%20Larmor%20frequency%20for%20hydrogen%20is%20**%7B_larmor_freq%3A.1f%7D%20MHz**%20(%7B_larmor_freq%20%2F%201000.0%3A.4f%7D%20GHz).%20This%20is%20in%20the%20**radiofrequency**%20range%20--%20the%20same%20part%20of%20the%20electromagnetic%20spectrum%20used%20by%20FM%20radio!%5Cn%5Cn**Drag%20to%20rotate**%20the%203D%20view.%20The%20signal%20traces%20on%20the%20right%20show%20%7CMxy%7C%20(red)%20and%20Mz%20(teal)%20in%20real%20time.')%2C%20kind%3D'success')%2C%20mo.md('**Larmor%20frequencies%20of%20different%20nuclei%20at%20this%20field%20strength%3A**')%2C%20mo.md('%7C%20Nucleus%20%7C%20%CE%B3%20(MHz%2FT)%20%7C%20Larmor%20freq%20at%20'%20%2B%20f'%7B_b0%3A.1f%7DT%20%7C%5Cn%7C---------%7C-----------%7C----------%7C%5Cn'%20%2B%20'%5Cn'.join((f'%7C%20%7Bname%7D%20%7C%20%7Bratio%3A.3f%7D%20%7C%20%7Bratio%20*%20_b0%3A.2f%7D%20MHz%20%7C'%20for%20name%2C%20ratio%20in%20GAMMA.items())))%5D)
mo.md('%5Cn%20%20%20%20---%5Cn%20%20%20%20%23%23%204.%20RF%20Excitation%20%26%20Resonance%5Cn%5Cn%20%20%20%20In%20an%20MRI%20machine%2C%20%24B_0%24%20is%20the%20strong%2C%20static%20magnetic%20field%20that%20runs%20along%20the%20bore%20of%20the%20scanner%20(the%20**longitudinal%20plane**%20or%20z-axis).%20It%20aligns%20hydrogen%20protons%20in%20your%20body%20roughly%20parallel%20to%20the%20field%2C%20creating%20a%20net%20magnetization%20%24M_0%24.%5Cn%20%20%20%20')
