.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "fitting/2D_fitting/plot_1_Rb2SO4_QMAT.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_fitting_2D_fitting_plot_1_Rb2SO4_QMAT.py: ⁸⁷Rb 2D QMAT NMR of Rb₂SO₄ ^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 7-10 The following is an illustration for fitting 2D QMAT/QPASS datasets. The example dataset is a :math:`^{87}\text{Rb}` 2D QMAT spectrum of :math:`\text{Rb}_2\text{SO}_4` from Walder `et al.` [#f1]_ .. GENERATED FROM PYTHON SOURCE LINES 10-23 .. code-block:: default import numpy as np import csdmpy as cp import matplotlib.pyplot as plt from lmfit import Minimizer from mrsimulator import Simulator, SpinSystem, Site from mrsimulator.method.lib import SSB2D from mrsimulator import signal_processor as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.spin_system.tensors import SymmetricTensor .. GENERATED FROM PYTHON SOURCE LINES 25-27 Import the dataset ------------------ .. GENERATED FROM PYTHON SOURCE LINES 27-53 .. code-block:: default filename = "https://ssnmr.org/sites/default/files/mrsimulator/Rb2SO4_QMAT.csdf" qmat_dataset = cp.load(filename) # standard deviation of noise from the dataset sigma = 6.530634 # For the spectral fitting, we only focus on the real part of the complex dataset. qmat_dataset = qmat_dataset.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in qmat_dataset.dimensions] # plot of the dataset. max_amp = qmat_dataset.max() levels = (np.arange(31) + 0.15) * max_amp / 32 # contours are drawn at these levels. options = dict(levels=levels, alpha=1, linewidths=0.5) # plot options plt.figure(figsize=(8, 3.5)) ax = plt.subplot(projection="csdm") ax.contour(qmat_dataset.T, colors="k", **options) ax.set_xlim(200, -200) ax.set_ylim(75, -120) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_001.png :alt: plot 1 Rb2SO4 QMAT :srcset: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-59 Create a fitting model ---------------------- **Guess model** Create a guess list of spin systems. .. GENERATED FROM PYTHON SOURCE LINES 59-72 .. code-block:: default Rb_1 = Site( isotope="87Rb", isotropic_chemical_shift=16, # in ppm quadrupolar=SymmetricTensor(Cq=5.3e6, eta=0.1), # Cq in Hz ) Rb_2 = Site( isotope="87Rb", isotropic_chemical_shift=40, # in ppm quadrupolar=SymmetricTensor(Cq=2.2e6, eta=0.95), # Cq in Hz ) spin_systems = [SpinSystem(sites=[s]) for s in [Rb_1, Rb_2]] .. GENERATED FROM PYTHON SOURCE LINES 73-76 **Method** Create the SSB2D method. .. GENERATED FROM PYTHON SOURCE LINES 76-93 .. code-block:: default # Get the spectral dimension parameters from the experiment. spectral_dims = get_spectral_dimensions(qmat_dataset) PASS = SSB2D( channels=["87Rb"], magnetic_flux_density=9.395, # in T rotor_frequency=2604, # in Hz spectral_dimensions=spectral_dims, experiment=qmat_dataset, # add the measurement to the method. ) # Optimize the script by pre-setting the transition pathways for each spin system from # the method. for sys in spin_systems: sys.transition_pathways = PASS.get_transition_pathways(sys) .. GENERATED FROM PYTHON SOURCE LINES 94-95 **Guess Spectrum** .. GENERATED FROM PYTHON SOURCE LINES 95-127 .. code-block:: default # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[PASS]) sim.run() # Post Simulation Processing # -------------------------- processor = sp.SignalProcessor( operations=[ # Lorentzian convolution along the isotropic dimensions. sp.FFT(dim_index=0), sp.apodization.Gaussian(FWHM="100 Hz"), sp.IFFT(dim_index=0), sp.Scale(factor=1e4), ] ) processed_dataset = processor.apply_operations(dataset=sim.methods[0].simulation).real # Plot of the guess Spectrum # -------------------------- plt.figure(figsize=(8, 3.5)) ax = plt.subplot(projection="csdm") ax.contour(qmat_dataset.T, colors="k", **options) ax.contour(processed_dataset.T, colors="r", linestyles="--", **options) ax.set_xlim(200, -200) ax.set_ylim(75, -120) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_002.png :alt: plot 1 Rb2SO4 QMAT :srcset: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 128-132 Least-squares minimization with LMFIT ------------------------------------- Use the :func:`~mrsimulator.utils.spectral_fitting.make_LMFIT_params` for a quick setup of the fitting parameters. .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. code-block:: default params = sf.make_LMFIT_params(sim, processor) params["SP_0_operation_1_Gaussian_FWHM"].min = 0 print(params.pretty_print(columns=["value", "min", "max", "vary", "expr"])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name Value Min Max Vary Expr SP_0_operation_1_Gaussian_FWHM 100 0 inf True None SP_0_operation_3_Scale_factor 1e+04 -inf inf True None sys_0_abundance 50 0 100 True None sys_0_site_0_isotropic_chemical_shift 16 -inf inf True None sys_0_site_0_quadrupolar_Cq 5.3e+06 -inf inf True None sys_0_site_0_quadrupolar_eta 0.1 0 1 True None sys_1_abundance 50 0 100 False 100-sys_0_abundance sys_1_site_0_isotropic_chemical_shift 40 -inf inf True None sys_1_site_0_quadrupolar_Cq 2.2e+06 -inf inf True None sys_1_site_0_quadrupolar_eta 0.95 0 1 True None None .. GENERATED FROM PYTHON SOURCE LINES 137-138 **Solve the minimizer using LMFIT** .. GENERATED FROM PYTHON SOURCE LINES 138-143 .. code-block:: default minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma)) result = minner.minimize() result .. raw:: html

Fit Statistics

fitting methodleastsq
# function evals177
# data points65536
# variables9
chi-square 215065.515
reduced chi-square 3.28209005
Akaike info crit. 77897.2528
Bayesian info crit. 77979.0660

Variables

name value standard error relative error initial value min max vary expression
sys_0_site_0_isotropic_chemical_shift 13.8300771 0.01969201 (0.14%) 16.0 -inf inf True
sys_0_site_0_quadrupolar_Cq 5232738.84 1048.92789 (0.02%) 5300000.0 -inf inf True
sys_0_site_0_quadrupolar_eta 0.12929392 4.0717e-04 (0.31%) 0.1 0.00000000 1.00000000 True
sys_0_abundance 56.0331100 0.05589944 (0.10%) 50.0 0.00000000 100.000000 True
sys_1_site_0_isotropic_chemical_shift 40.3548314 0.01019456 (0.03%) 40.0 -inf inf True
sys_1_site_0_quadrupolar_Cq 2618470.99 4285747.11 (163.67%) 2200000.0 -inf inf True
sys_1_site_0_quadrupolar_eta 0.99999812 5.44707516 (544.71%) 0.95 0.00000000 1.00000000 True
sys_1_abundance 43.9668900 0.05589944 (0.13%) 50.0 0.00000000 100.000000 False 100-sys_0_abundance
SP_0_operation_1_Gaussian_FWHM 197.505475 1.97354061 (1.00%) 100.0 0.00000000 inf True
SP_0_operation_3_Scale_factor 6295.09183 7.79166625 (0.12%) 10000.0 -inf inf True

Correlations (unreported correlations are < 0.100)

sys_1_site_0_quadrupolar_Cqsys_1_site_0_quadrupolar_eta1.0000
sys_0_site_0_isotropic_chemical_shiftsys_0_site_0_quadrupolar_Cq0.8130
sys_1_site_0_isotropic_chemical_shiftsys_1_site_0_quadrupolar_eta-0.7719
sys_1_site_0_isotropic_chemical_shiftsys_1_site_0_quadrupolar_Cq-0.7719
sys_0_abundanceSP_0_operation_3_Scale_factor0.6299
sys_0_site_0_quadrupolar_etaSP_0_operation_3_Scale_factor0.2563
sys_0_site_0_quadrupolar_etasys_0_abundance0.2500
sys_0_site_0_isotropic_chemical_shiftsys_0_site_0_quadrupolar_eta-0.2298
SP_0_operation_1_Gaussian_FWHMSP_0_operation_3_Scale_factor0.1899
sys_0_site_0_quadrupolar_CqSP_0_operation_3_Scale_factor0.1798
sys_0_abundancesys_1_site_0_isotropic_chemical_shift-0.1661
sys_0_site_0_quadrupolar_Cqsys_0_site_0_quadrupolar_eta-0.1654
sys_0_site_0_quadrupolar_Cqsys_0_abundance0.1580
sys_1_site_0_isotropic_chemical_shiftSP_0_operation_3_Scale_factor0.1468
sys_0_site_0_isotropic_chemical_shiftSP_0_operation_3_Scale_factor0.1170
sys_1_site_0_isotropic_chemical_shiftSP_0_operation_1_Gaussian_FWHM0.1168
sys_0_site_0_isotropic_chemical_shiftsys_0_abundance0.1036
sys_1_site_0_quadrupolar_etaSP_0_operation_1_Gaussian_FWHM-0.1018
sys_1_site_0_quadrupolar_CqSP_0_operation_1_Gaussian_FWHM-0.1017


.. GENERATED FROM PYTHON SOURCE LINES 144-146 The best fit solution --------------------- .. GENERATED FROM PYTHON SOURCE LINES 146-159 .. code-block:: default best_fit = sf.bestfit(sim, processor)[0].real # Plot of the best fit solution plt.figure(figsize=(8, 3.5)) ax = plt.subplot(projection="csdm") ax.contour(qmat_dataset.T, colors="k", **options) ax.contour(best_fit.T, colors="r", linestyles="--", **options) ax.set_xlim(200, -200) ax.set_ylim(75, -120) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_003.png :alt: plot 1 Rb2SO4 QMAT :srcset: /fitting/2D_fitting/images/sphx_glr_plot_1_Rb2SO4_QMAT_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 160-164 .. [#f1] B. J. Walder, K. K. Dey, D. C. Kaseman, J. H. Baltisberger, and P. J. Grandinetti, Sideband separation experiments in NMR with phase incremented echo train acquisition, J. Phys. Chem. 2013, **138**, 174203-1-12. `DOI: 10.1063/1.4803142 `_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 9.262 seconds) .. _sphx_glr_download_fitting_2D_fitting_plot_1_Rb2SO4_QMAT.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_1_Rb2SO4_QMAT.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_1_Rb2SO4_QMAT.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_