.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "galley_examples/synthetic/plot_1D_5_T2_relaxation.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_galley_examples_synthetic_plot_1D_5_T2_relaxation.py: Narrow T2 distribution (Inverse Laplace) ======================================== .. GENERATED FROM PYTHON SOURCE LINES 7-14 The following example demonstrates the statistical learning based determination of the NMR T2 relaxation vis inverse Laplace transformation. Before getting started ---------------------- Import all relevant packages. .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: Python import csdmpy as cp import matplotlib.pyplot as plt import numpy as np from mrinversion.kernel import relaxation from mrinversion.linear_model import LassoFistaCV, TSVDCompression .. GENERATED FROM PYTHON SOURCE LINES 24-28 Dataset setup ------------- Load the dataset '''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python domain = "https://ssnmr.org/resources/mrinversion" filename = f"{domain}/test1_signal.csdf" signal = cp.load(filename) sigma = 0.0008 .. GENERATED FROM PYTHON SOURCE LINES 34-37 The variable ``signal`` holds the 1D dataset. For comparison, let's also import the true t2 distribution from which the synthetic 1D signal decay is simulated. .. GENERATED FROM PYTHON SOURCE LINES 37-45 .. code-block:: Python datafile = f"{domain}/test1_t2.csdf" true_t2_dist = cp.load(datafile) plt.figure(figsize=(4.5, 3.5)) signal.plot() plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_001.png :alt: plot 1D 5 T2 relaxation :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-50 Linear Inversion setup ---------------------- Generating the kernel ''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 50-61 .. code-block:: Python kernel_dimension = signal.dimensions[0] relaxT2 = relaxation.T2( kernel_dimension=kernel_dimension, inverse_dimension=dict( count=64, minimum="1e-2 s", maximum="1e3 s", scale="log", label="log (T2 / s)" ), ) inverse_dimension = relaxT2.inverse_dimension K = relaxT2.kernel(supersampling=1) .. GENERATED FROM PYTHON SOURCE LINES 62-64 Data Compression '''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 64-70 .. code-block:: Python new_system = TSVDCompression(K, signal) compressed_K = new_system.compressed_K compressed_s = new_system.compressed_s print(f"truncation_index = {new_system.truncation_index}") .. rst-class:: sphx-glr-script-out .. code-block:: none compression factor = 1.0344827586206897 truncation_index = 29 .. GENERATED FROM PYTHON SOURCE LINES 71-74 Fista LASSO cross-validation ''''''''''''''''''''''''''''' Create a guess range of values for the :math:`\lambda` hyperparameters. .. GENERATED FROM PYTHON SOURCE LINES 74-87 .. code-block:: Python lambdas = 10 ** (-5 + 4 * (np.arange(64) / 63)) # setup the smooth lasso cross-validation class f_lasso_cv = LassoFistaCV( lambdas=lambdas, # A numpy array of lambda values. folds=5, # The number of folds in n-folds cross-validation. sigma=sigma, # noise standard deviation inverse_dimension=inverse_dimension, # previously defined inverse dimensions. ) # run the fit method on the compressed kernel and compressed data. f_lasso_cv.fit(K=compressed_K, s=compressed_s) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/mrinversion/envs/latest/lib/python3.10/site-packages/mrinversion/linear_model/fista/__init__.py:127: NumbaPerformanceWarning: '@' is faster on contiguous arrays, called on (Array(float64, 2, 'A', False, aligned=True), Array(float64, 2, 'A', False, aligned=True)) gradient = x_train.T @ x_train /home/docs/checkouts/readthedocs.org/user_builds/mrinversion/envs/latest/lib/python3.10/site-packages/mrinversion/linear_model/fista/__init__.py:158: NumbaPerformanceWarning: '@' is faster on contiguous arrays, called on (Array(float64, 2, 'A', False, aligned=True), Array(float64, 2, 'C', False, aligned=True)) residue[k] = norm(x_train @ f_k - y_train) ** 2 .. GENERATED FROM PYTHON SOURCE LINES 88-90 The optimum hyper-parameters '''''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python print(f_lasso_cv.hyperparameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'lambda': np.float64(0.0034648348557303663)} .. GENERATED FROM PYTHON SOURCE LINES 93-95 The cross-validation curve '''''''''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: Python plt.figure(figsize=(4.5, 3.5)) f_lasso_cv.cv_plot() plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_002.png :alt: plot 1D 5 T2 relaxation :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 101-103 The optimum solution '''''''''''''''''''' .. GENERATED FROM PYTHON SOURCE LINES 103-115 .. code-block:: Python sol = f_lasso_cv.f plt.figure(figsize=(4, 3)) plt.subplot(projection="csdm") plt.plot(true_t2_dist / true_t2_dist.max(), label="true distribution") plt.plot(sol / sol.max(), label="opt solution") plt.legend() plt.grid() plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_003.png :alt: plot 1D 5 T2 relaxation :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-118 Residuals ''''''''' .. GENERATED FROM PYTHON SOURCE LINES 118-125 .. code-block:: Python residuals = f_lasso_cv.residuals(K=K, s=signal) print(residuals.std()) plt.figure(figsize=(4.5, 3.5)) residuals.plot() plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_004.png :alt: plot 1D 5 T2 relaxation :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_5_T2_relaxation_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0027977179174245915 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.103 seconds) .. _sphx_glr_download_galley_examples_synthetic_plot_1D_5_T2_relaxation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1D_5_T2_relaxation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1D_5_T2_relaxation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_1D_5_T2_relaxation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_