.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "galley_examples/synthetic/plot_1D_1_sideband.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_1_sideband.py: Unimodal distribution (Aniso Shielding Sideband Inversion) ========================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-16 The following example demonstrates the statistical learning based determination of the nuclear shielding tensor parameters from a one-dimensional cross-section of a spinning sideband correlation spectrum. In this example, we use a synthetic sideband amplitude spectrum from a unimodal tensor distribution. Before getting started ---------------------- Import all relevant packages. .. GENERATED FROM PYTHON SOURCE LINES 16-40 .. code-block:: Python import csdmpy as cp import matplotlib.pyplot as plt import numpy as np from mrinversion.kernel.nmr import ShieldingPALineshape from mrinversion.linear_model import SmoothLasso, SmoothLassoCV, TSVDCompression from mrinversion.utils import get_polar_grids # Setup for the matplotlib figures # function for 2D x-y plot. def plot2D(ax, csdm_object, title=""): # convert the dimension coordinates of the csdm_object from Hz to pmm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in csdm_object.dimensions] levels = (np.arange(9) + 1) / 10 ax.contourf(csdm_object, cmap="gist_ncar", levels=levels) ax.grid(None) ax.set_title(title) get_polar_grids(ax) ax.set_aspect("equal") .. GENERATED FROM PYTHON SOURCE LINES 41-48 Dataset setup ------------- Import the dataset '''''''''''''''''' Load the dataset. Here, we import the dataset as a CSDM data-object. .. GENERATED FROM PYTHON SOURCE LINES 48-57 .. code-block:: Python # the 1D spinning sideband cross-section data in csdm format domain = "https://ssnmr.org/resources/mrinversion" filename = f"{domain}/kehokr5op0amkfp5auyd498nblcdr1xy.csdf" data_object = cp.load(filename) # convert the data dimension from `Hz` to `ppm`. data_object.dimensions[0].to("ppm", "nmr_frequency_ratio") .. GENERATED FROM PYTHON SOURCE LINES 58-61 The variable ``data_object`` holds the 1D dataset. For comparison, let's also import the true tensor parameter distribution from which the synthetic 1D pure anisotropic spinning sideband cross-section amplitudes is simulated. .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python datafile = f"{domain}/s5wpm26w4cv3w64qjhouqu458ch4z0nd.csdf" true_data_object = cp.load(datafile) .. GENERATED FROM PYTHON SOURCE LINES 65-67 The plot of the 1D sideband cross-section along with the 2D true tensor parameter distribution of the synthetic dataset is shown below. .. GENERATED FROM PYTHON SOURCE LINES 67-78 .. code-block:: Python # the plot of the 1D MAF cross-section dataset. _, ax = plt.subplots(1, 2, figsize=(9, 3.5), subplot_kw={"projection": "csdm"}) ax[0].plot(data_object) ax[0].invert_xaxis() # the plot of the true tensor distribution. plot2D(ax[1], true_data_object, title="True distribution") plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_001.png :alt: True distribution :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-87 Linear Inversion setup ---------------------- Dimension setup ''''''''''''''' **Anisotropic-dimension:** The dimension of the dataset that holds the pure anisotropic spinning sidebands. .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python anisotropic_dimension = data_object.dimensions[0] .. GENERATED FROM PYTHON SOURCE LINES 90-92 **x-y dimensions:** The two inverse dimensions corresponding to the `x` and `y`-axis of the `x`-`y` grid. .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python inverse_dimension = [ cp.LinearDimension(count=25, increment="370 Hz", label="x"), # the `x`-dimension. cp.LinearDimension(count=25, increment="370 Hz", label="y"), # the `y`-dimension. ] .. GENERATED FROM PYTHON SOURCE LINES 98-105 Generating the kernel ''''''''''''''''''''' For sideband datasets, the line-shape kernel corresponds to the pure anisotropic nuclear shielding spinning sideband spectra. Use the :class:`~mrinversion.kernel.nmr.ShieldingPALineshape` class to generate the sideband kernel. .. GENERATED FROM PYTHON SOURCE LINES 105-115 .. code-block:: Python lineshape = ShieldingPALineshape( anisotropic_dimension=anisotropic_dimension, inverse_dimension=inverse_dimension, channel="29Si", magnetic_flux_density="9.4 T", rotor_angle="54.735 deg", rotor_frequency="625 Hz", number_of_sidebands=32, ) .. GENERATED FROM PYTHON SOURCE LINES 116-131 Here, ``lineshape`` is an instance of the :class:`~mrinversion.kernel.nmr.ShieldingPALineshape` class. The required arguments of this class are the `anisotropic_dimension`, `inverse_dimension`, and `channel`. We have already defined the first two arguments in the previous sub-section. The value of the `channel` argument is the observed nucleus. In this example, this value is '29Si'. The remaining arguments, such as the `magnetic_flux_density`, `rotor_angle`, and `rotor_frequency`, are set to match the conditions under which the spectrum was acquired. Note, the rotor frequency is the effective anisotropic modulation frequency, which may be less than the physical rotor frequency. The number of sidebands is usually the number of points along the sideband dimension. Once the ShieldingPALineshape instance is created, use the :meth:`~mrinversion.kernel.nmr.ShieldingPALineshape.kernel` method of the instance to generate the sideband kernel. .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python K = lineshape.kernel(supersampling=1) .. GENERATED FROM PYTHON SOURCE LINES 134-139 Data Compression '''''''''''''''' Data compression is optional but recommended. It may reduce the size of the inverse problem and, thus, further computation time. .. GENERATED FROM PYTHON SOURCE LINES 139-145 .. code-block:: Python new_system = TSVDCompression(K, data_object) 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.032258064516129 truncation_index = 31 .. GENERATED FROM PYTHON SOURCE LINES 146-160 Solving the inverse problem --------------------------- Smooth-LASSO problem '''''''''''''''''''' Solve the smooth-lasso problem. You may choose to skip this step and proceed to the statistical learning method. Usually, the statistical learning method is a time-consuming process that solves the smooth-lasso problem over a range of predefined hyperparameters. If you are unsure what range of hyperparameters to use, you can use this step for a quick look into the possible solution by giving a guess value for the :math:`\alpha` and :math:`\lambda` hyperparameters, and then decide on the hyperparameters range accordingly. .. GENERATED FROM PYTHON SOURCE LINES 160-166 .. code-block:: Python # guess alpha and lambda values. s_lasso = SmoothLasso(alpha=5e-5, lambda1=5e-6, inverse_dimension=inverse_dimension) s_lasso.fit(K=compressed_K, s=compressed_s) f_sol = s_lasso.f .. GENERATED FROM PYTHON SOURCE LINES 167-170 Here, ``f_sol`` is the solution corresponding to hyperparameters :math:`\alpha=5\times10^{-5}` and :math:`\lambda=5\times 10^{-6}`. The plot of this solution is .. GENERATED FROM PYTHON SOURCE LINES 170-180 .. code-block:: Python _, ax = plt.subplots(1, 2, figsize=(9, 3.5), subplot_kw={"projection": "csdm"}) # the plot of the guess tensor distribution solution. plot2D(ax[0], f_sol / f_sol.max(), title="Guess distribution") # the plot of the true tensor distribution. plot2D(ax[1], true_data_object, title="True distribution") plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_002.png :alt: Guess distribution, True distribution :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 181-185 Predicted spectrum '''''''''''''''''' You may also evaluate the predicted spectrum from the above solution following .. GENERATED FROM PYTHON SOURCE LINES 185-197 .. code-block:: Python residuals = s_lasso.residuals(K, data_object) predicted_spectrum = data_object - residuals plt.figure(figsize=(4, 3)) plt.subplot(projection="csdm") plt.plot(data_object, color="black", label="spectrum") # the original spectrum plt.plot(predicted_spectrum, color="red", label="prediction") # the predicted spectrum plt.gca().invert_xaxis() plt.legend() plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_003.png :alt: plot 1D 1 sideband :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-212 As you can see from the predicted spectrum, our guess isn't far from the optimum hyperparameters. Let's create a search grid about the guess hyperparameters and run a cross-validation method for selection. Statistical learning of the tensors ----------------------------------- Smooth LASSO cross-validation ''''''''''''''''''''''''''''' Create a guess range of values for the :math:`\alpha` and :math:`\lambda` hyperparameters. The following code generates a range of :math:`\lambda` and :math:`\alpha` values that are uniformly sampled on the log scale. .. GENERATED FROM PYTHON SOURCE LINES 212-226 .. code-block:: Python lambdas = 10 ** (-5 - 1 * (np.arange(6) / 5)) alphas = 10 ** (-4 - 2 * (np.arange(6) / 5)) # set up cross validation smooth lasso method s_lasso_cv = SmoothLassoCV( alphas=alphas, lambdas=lambdas, inverse_dimension=inverse_dimension, sigma=0.005, folds=10, ) # run the fit using the compressed kernel and compressed signal. s_lasso_cv.fit(compressed_K, compressed_s) .. GENERATED FROM PYTHON SOURCE LINES 227-233 The optimum hyper-parameters '''''''''''''''''''''''''''' Use the :attr:`~mrinversion.linear_model.SmoothLassoCV.hyperparameters` attribute of the instance for the optimum hyper-parameters, :math:`\alpha` and :math:`\lambda`, determined from the cross-validation. .. GENERATED FROM PYTHON SOURCE LINES 233-235 .. code-block:: Python print(s_lasso_cv.hyperparameters) .. rst-class:: sphx-glr-script-out .. code-block:: none {'alpha': np.float64(2.5118864315095823e-06), 'lambda': np.float64(1.584893192461114e-06)} .. GENERATED FROM PYTHON SOURCE LINES 236-243 The cross-validation surface '''''''''''''''''''''''''''' Optionally, you may want to visualize the cross-validation error curve/surface. Use the :attr:`~mrinversion.linear_model.SmoothLassoCV.cross_validation_curve` attribute of the instance, as follows. The cross-validation metric is the mean square error (MSE). .. GENERATED FROM PYTHON SOURCE LINES 243-258 .. code-block:: Python cv_curve = s_lasso_cv.cross_validation_curve # plot of the cross-validation curve plt.figure(figsize=(5, 3.5)) ax = plt.subplot(projection="csdm") ax.contour(np.log10(s_lasso_cv.cross_validation_curve), levels=25) ax.scatter( -np.log10(s_lasso_cv.hyperparameters["alpha"]), -np.log10(s_lasso_cv.hyperparameters["lambda"]), marker="x", color="k", ) plt.tight_layout(pad=0.5) plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_004.png :alt: plot 1D 1 sideband :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 259-264 The optimum solution '''''''''''''''''''' The :attr:`~mrinversion.linear_model.SmoothLassoCV.f` attribute of the instance holds the solution. .. GENERATED FROM PYTHON SOURCE LINES 264-266 .. code-block:: Python f_sol = s_lasso_cv.f .. GENERATED FROM PYTHON SOURCE LINES 267-269 The corresponding plot of the solution, along with the true tensor distribution, is shown below. .. GENERATED FROM PYTHON SOURCE LINES 269-278 .. code-block:: Python _, ax = plt.subplots(1, 2, figsize=(9, 3.5), subplot_kw={"projection": "csdm"}) # the plot of the tensor distribution solution. plot2D(ax[0], f_sol / f_sol.max(), title="Optimum distribution") # the plot of the true tensor distribution. plot2D(ax[1], true_data_object, title="True distribution") plt.tight_layout() plt.show() .. image-sg:: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_005.png :alt: Optimum distribution, True distribution :srcset: /galley_examples/synthetic/images/sphx_glr_plot_1D_1_sideband_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.951 seconds) .. _sphx_glr_download_galley_examples_synthetic_plot_1D_1_sideband.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_1_sideband.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1D_1_sideband.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_1D_1_sideband.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_