.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "fitting/1D_fitting/plot_4_11B_Quad_NMR.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_1D_fitting_plot_4_11B_Quad_NMR.py: ¹¹B MAS NMR of Lithium orthoborate crystal ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 7-9 The following is a quadrupolar lineshape fitting example for the 11B MAS NMR of lithium orthoborate crystal. The dataset was shared by Dr. Nathan Barrow. .. GENERATED FROM PYTHON SOURCE LINES 9-21 .. code-block:: default import csdmpy as cp import matplotlib.pyplot as plt from lmfit import Minimizer from mrsimulator import Simulator, Site, SpinSystem from mrsimulator.method.lib import BlochDecayCTSpectrum 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 23-25 Import the dataset ------------------ .. GENERATED FROM PYTHON SOURCE LINES 25-47 .. code-block:: default host = "https://ssnmr.org/sites/default/files/mrsimulator/" filename = "11B_lithum_orthoborate.csdf" experiment = cp.load(host + filename) # standard deviation of noise from the dataset sigma = 0.08078374 # For spectral fitting, we only focus on the real part of the complex dataset experiment = experiment.real # Convert the coordinates along each dimension from Hz to ppm. _ = [item.to("ppm", "nmr_frequency_ratio") for item in experiment.dimensions] # plot of the dataset. plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, "k", alpha=0.5) ax.set_xlim(100, -100) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_001.png :alt: plot 4 11B Quad NMR :srcset: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 48-51 Create a fitting model ---------------------- **Spin System** .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: default B11 = Site( isotope="11B", isotropic_chemical_shift=20.0, # in ppm quadrupolar=SymmetricTensor(Cq=2.3e6, eta=0.03), # Cq in Hz ) spin_systems = [SpinSystem(sites=[B11])] .. GENERATED FROM PYTHON SOURCE LINES 60-61 **Method** .. GENERATED FROM PYTHON SOURCE LINES 61-78 .. code-block:: default # Get the spectral dimension parameters from the experiment. spectral_dims = get_spectral_dimensions(experiment) MAS_CT = BlochDecayCTSpectrum( channels=["11B"], magnetic_flux_density=14.1, # in T rotor_frequency=12500, # in Hz spectral_dimensions=spectral_dims, experiment=experiment, # 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 = MAS_CT.get_transition_pathways(sys) .. GENERATED FROM PYTHON SOURCE LINES 79-80 **Guess Model Spectrum** .. GENERATED FROM PYTHON SOURCE LINES 80-111 .. code-block:: default # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[MAS_CT]) sim.run() # Post Simulation Processing # -------------------------- processor = sp.SignalProcessor( operations=[ sp.IFFT(), sp.apodization.Exponential(FWHM="100 Hz"), sp.FFT(), sp.Scale(factor=200), ] ) processed_dataset = processor.apply_operations(dataset=sim.methods[0].simulation).real # Plot of the guess Spectrum # -------------------------- plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, "k", linewidth=1, label="Experiment") ax.plot(processed_dataset, "r", alpha=0.75, linewidth=1, label="guess spectrum") ax.set_xlim(100, -100) plt.grid() plt.legend() plt.tight_layout() plt.show() .. image-sg:: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_002.png :alt: plot 4 11B Quad NMR :srcset: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-116 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 116-120 .. code-block:: default params = sf.make_LMFIT_params(sim, processor) params.pop("sys_0_abundance") 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_Exponential_FWHM 100 -inf inf True None SP_0_operation_3_Scale_factor 200 -inf inf True None sys_0_site_0_isotropic_chemical_shift 20 -inf inf True None sys_0_site_0_quadrupolar_Cq 2.3e+06 -inf inf True None sys_0_site_0_quadrupolar_eta 0.03 0 1 True None None .. GENERATED FROM PYTHON SOURCE LINES 121-122 **Solve the minimizer using LMFIT** .. GENERATED FROM PYTHON SOURCE LINES 122-126 .. 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 evals119
# data points4096
# variables5
chi-square 203470.421
reduced chi-square 49.7361087
Akaike info crit. 16006.9680
Bayesian info crit. 16038.5568

Variables

name value standard error relative error initial value min max vary
sys_0_site_0_isotropic_chemical_shift 20.0533817 0.00147191 (0.01%) 20.0 -inf inf True
sys_0_site_0_quadrupolar_Cq 2731196.61 417.270375 (0.02%) 2300000.0 -inf inf True
sys_0_site_0_quadrupolar_eta 0.04712757 7.3037e-04 (1.55%) 0.03 0.00000000 1.00000000 True
SP_0_operation_1_Exponential_FWHM 70.9385144 0.52813486 (0.74%) 100.0 -inf inf True
SP_0_operation_3_Scale_factor 199.362369 0.20570628 (0.10%) 200.0 -inf inf True

Correlations (unreported correlations are < 0.100)

sys_0_site_0_isotropic_chemical_shiftsys_0_site_0_quadrupolar_Cq0.7932
sys_0_site_0_quadrupolar_etaSP_0_operation_1_Exponential_FWHM-0.6610
SP_0_operation_1_Exponential_FWHMSP_0_operation_3_Scale_factor0.5298
sys_0_site_0_quadrupolar_etaSP_0_operation_3_Scale_factor-0.2422
sys_0_site_0_quadrupolar_Cqsys_0_site_0_quadrupolar_eta-0.1331
sys_0_site_0_quadrupolar_CqSP_0_operation_3_Scale_factor0.1264


.. GENERATED FROM PYTHON SOURCE LINES 127-129 The best fit solution --------------------- .. GENERATED FROM PYTHON SOURCE LINES 129-143 .. code-block:: default best_fit = sf.bestfit(sim, processor)[0].real residuals = sf.residuals(sim, processor)[0].real # Plot the spectrum plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.plot(experiment, "k", linewidth=1, label="Experiment") ax.plot(best_fit, "r", alpha=0.75, linewidth=1, label="Best Fit") ax.plot(residuals, alpha=0.75, linewidth=1, label="Residuals") ax.set_xlim(100, -100) plt.grid() plt.legend() plt.tight_layout() plt.show() .. image-sg:: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_003.png :alt: plot 4 11B Quad NMR :srcset: /fitting/1D_fitting/images/sphx_glr_plot_4_11B_Quad_NMR_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.244 seconds) .. _sphx_glr_download_fitting_1D_fitting_plot_4_11B_Quad_NMR.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_4_11B_Quad_NMR.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_4_11B_Quad_NMR.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_