.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated\examples\tutorials\plot_model_selection.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_tutorials_plot_model_selection.py: =============================================== The effect of model selection =============================================== Since ``mdreg`` performs model-driven motion correction, the choice of an appropriate model is important to the result. We illustrate this here by coregistering a dataset with different models. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Setup ----- .. GENERATED FROM PYTHON SOURCE LINES 14-17 .. code-block:: Python import numpy as np import mdreg .. GENERATED FROM PYTHON SOURCE LINES 18-19 fetch test data .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python data = mdreg.fetch('MOLLI') # We will consider the slice z=0 of the data array: array = data['array'][:,:,0,:] .. GENERATED FROM PYTHON SOURCE LINES 26-31 Default model ------------- The breathing motion is clearly visible in this slice and we can use ``mdreg`` to remove it. As a starting point, we could try ``mdreg`` with default settings. .. GENERATED FROM PYTHON SOURCE LINES 31-39 .. code-block:: Python # Perform model-driven coregistration with default settings coreg, fit, defo, pars = mdreg.fit(array) # And visualise the results anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 40-45 The default model is a constant, so the model fit (left) does not show any changes. The coregistered image has not properly removed the motions. This is not unexpected, because a constant model does not provide a good approximation to the changes in image contrast. We clearly need a more complex model for this sequence. .. GENERATED FROM PYTHON SOURCE LINES 47-51 Linear model ------------ In order to improve on this result, we could try a linear model, which approximates the signal changes in each pixel as a straight line. .. GENERATED FROM PYTHON SOURCE LINES 51-63 .. code-block:: Python # Perform model-driven coregistration with default settings coreg, fit, defo, pars = mdreg.fit( array, fit_pixels = { 'model': mdreg.lin, 'p0': [1, 0], }, ) # And visualise the results anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none Covariance of the parameters could not be estimated .. GENERATED FROM PYTHON SOURCE LINES 64-67 Still not a great motion correction (right). The model fit (left) shows that while the linear model does allow for some changes in contrast over time, it does not capture the actual changes very well (middle). .. GENERATED FROM PYTHON SOURCE LINES 69-72 Quadratic model --------------- Let's step up the complexity once again and fit with a quadratic model: .. GENERATED FROM PYTHON SOURCE LINES 72-81 .. code-block:: Python coreg, fit, defo, pars = mdreg.fit( array, fit_pixels = { 'model': mdreg.quad, 'p0': [1, 0, 0], }, ) anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none Covariance of the parameters could not be estimated .. GENERATED FROM PYTHON SOURCE LINES 82-84 This now captures the signal changes better, leading to an improved motion correction, but the result is far from perfect. .. GENERATED FROM PYTHON SOURCE LINES 86-90 Fourth order polynomial ----------------------- Let's step it up one more time to see if we can improve on this further. We'll skip a step and go straight to fourth order: .. GENERATED FROM PYTHON SOURCE LINES 90-99 .. code-block:: Python coreg, fit, defo, pars = mdreg.fit( array, fit_pixels = { 'model': mdreg.ofour, 'p0': [1, 0, 0, 0, 0], }, ) anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none Covariance of the parameters could not be estimated .. GENERATED FROM PYTHON SOURCE LINES 100-108 This now appears to have made it worse: there is more motion again in the coregistered series (right). Looking at the model fit (left) we see what is happening: this model has so much freedom that it can now model the deformations as well, creating a moving target for the coregistration. The best solution, when available, is always to use the actual model of the signal changes, with the smallest amount of free parameters as is needed to describe them accurately. .. GENERATED FROM PYTHON SOURCE LINES 110-115 MOLLI model ----------- We will run this one final time, now using the correct model for a Look- Locker MRI signal sequence. Tis only has 2 parameters, but models the signal changes well: .. GENERATED FROM PYTHON SOURCE LINES 115-126 .. code-block:: Python coreg, fit, defo, pars = mdreg.fit( array, fit_pixels = { 'model': mdreg.abs_exp_recovery_2p, 'p0': [1, 1], 'xdata': np.array(data['TI'])/1000, 'func_init':mdreg.abs_exp_recovery_2p_init, }, ) anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none Covariance of the parameters could not be estimated overflow encountered in exp overflow encountered in multiply .. GENERATED FROM PYTHON SOURCE LINES 127-131 This show the best result so far, despite the model only having 2 free parameters. At this point the result cannot be improved by fine tuning the modelling, but changing the restrictions in the default coregistration does help to improve further: .. GENERATED FROM PYTHON SOURCE LINES 131-145 .. code-block:: Python coreg, fit, defo, pars = mdreg.fit( array, fit_pixels = { 'model': mdreg.abs_exp_recovery_2p, 'p0': [1, 1], 'xdata': np.array(data['TI'])/1000, 'func_init':mdreg.abs_exp_recovery_2p_init, }, fit_coreg = { 'attachment': 30, }, ) anim = mdreg.plot.series(array, fit, coreg, vmin=0, vmax=1e4) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none Covariance of the parameters could not be estimated overflow encountered in exp overflow encountered in multiply .. rst-class:: sphx-glr-timing **Total running time of the script:** (45 minutes 36.591 seconds) .. _sphx_glr_download_generated_examples_tutorials_plot_model_selection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_model_selection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_model_selection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_model_selection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_