.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\use_cases\plot_vfa_liver.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_use_cases_plot_vfa_liver.py: =============================================== 3D Variable Flip Angle (Linear) =============================================== This example illustrates motion correction of a 3D time series with variable flip angles (VFA). The motion correction is performed with 3D coregistration and using a linear signal model fit. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 15-27 .. code-block:: Python import numpy as np import mdreg # Example data included in mdreg data = mdreg.fetch('VFA') # Variables used in this examples array = data['array'] # 4D signal data (x, y, z, FA) FA = data['FA'] # The FA values in degrees spacing = data['spacing'] # (x,y,z) voxel size in mm. .. GENERATED FROM PYTHON SOURCE LINES 28-33 Perform motion correction ------------------------- The signal model above is included in `mdreg` as the function `mdreg.fit_spgr_vfa_lin`, which require the flip angle (FA) values in degrees as input: .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python fit_params = { 'func': mdreg.fit_spgr_vfa_lin, # VFA signal model 'FA': FA, # Flip angle in degress } .. GENERATED FROM PYTHON SOURCE LINES 40-43 For coregistration we will use elastix and a relatively coarse deformation field with grid spacing 50mm. We also ask to return the deformation field so we can inspect it: .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python coreg_params = { 'package': 'elastix', 'spacing': spacing, 'FinalGridSpacingInPhysicalUnits': 50.0, 'return_deformation': True, } .. GENERATED FROM PYTHON SOURCE LINES 52-53 We can now perform the motion correction: .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: Python coreg, fit, transfo, pars, defo = mdreg.fit( array, # Signal data to correct fit_image = fit_params, # Signal model fit_coreg = coreg_params, # Coregistration model maxit = 2, # Maximum number of iteration verbose = 2, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Initializing.. Iteration 1: fitting signal model divide by zero encountered in divide Fitting deformation field (iteration 1) Building elastix parameter object.. Coregistering.. Calculation time for iteration 1: 0.6096879998842876 min Iteration 2: fitting signal model Fitting deformation field (iteration 2) Building elastix parameter object.. Coregistering.. Calculation time for iteration 2: 0.45500829617182414 min Total calculation time: 1.0646962960561117 min .. GENERATED FROM PYTHON SOURCE LINES 63-68 Visualize the results --------------------- We visualise the original data and results of the computation using the builtin `animation` function. Since we want to call this 3 times, we define the settings up front: .. GENERATED FROM PYTHON SOURCE LINES 68-74 .. code-block:: Python plot_settings = { 'vmin' : 0, # Minimum value of the colorbar 'vmax' : np.percentile(array,99), # Maximum value of the colorbar } .. GENERATED FROM PYTHON SOURCE LINES 75-76 Now we can plot the data, coregistered images and model fits separately: .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python anim = mdreg.plot.animation(array, title='Original data', **plot_settings) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python anim = mdreg.plot.animation(coreg, title='Motion corrected', **plot_settings) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python anim = mdreg.plot.animation(fit, title='Model fit', **plot_settings) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 87-90 It's also instructive to show the deformation field and check whether deformations are consistent with the effect of breathing motion. Since the deformation field is a vector we show here its norm: .. GENERATED FROM PYTHON SOURCE LINES 92-100 .. code-block:: Python # Get the norm of the deformation field and adjust the plot settings defo = mdreg.defo_norm(defo) plot_settings['vmax'] = np.percentile(defo, 90) # Display the norm of the deformation field anim = mdreg.plot.animation(defo, title='Deformation field', **plot_settings) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 27.383 seconds) .. _sphx_glr_download_generated_examples_use_cases_plot_vfa_liver.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_vfa_liver.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_vfa_liver.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_vfa_liver.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_