.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "fitting/2D_fitting/plot_2_Coesite_DAS.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_fitting_2D_fitting_plot_2_Coesite_DAS.py: ¹⁷O 2D DAS NMR of Coesite ^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 7-11 Coesite is a high-pressure (2-3 GPa) and high-temperature (700°C) polymorph of silicon dioxide :math:`\text{SiO}_2`. Coesite has five crystallographic :math:`^{17}\text{O}` sites. The experimental dataset used in this example is published in Grandinetti `et al.` [#f1]_ .. GENERATED FROM PYTHON SOURCE LINES 11-24 .. code-block:: Python import numpy as np import csdmpy as cp import matplotlib.pyplot as plt from lmfit import Minimizer from mrsimulator import Simulator from mrsimulator import signal_processor as sp from mrsimulator.utils import spectral_fitting as sf from mrsimulator.utils import get_spectral_dimensions from mrsimulator.utils.collection import single_site_system_generator from mrsimulator.method import Method, SpectralDimension, SpectralEvent, MixingEvent .. GENERATED FROM PYTHON SOURCE LINES 26-28 Import the dataset ------------------ .. GENERATED FROM PYTHON SOURCE LINES 28-51 .. code-block:: Python filename = "https://ssnmr.org/sites/default/files/mrsimulator/DASCoesite.csdf" experiment = cp.load(filename) # 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. max_amp = experiment.max() levels = (np.arange(14) + 1) * max_amp / 15 # contours are drawn at these levels. options = dict(levels=levels, alpha=0.75, linewidths=0.5) # plot options plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.contour(experiment, colors="k", **options) ax.invert_xaxis() ax.set_ylim(30, -30) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_001.png :alt: plot 2 Coesite DAS :srcset: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-53 Estimate noise statistics from the dataset .. GENERATED FROM PYTHON SOURCE LINES 53-68 .. code-block:: Python coords = experiment.dimensions[0].coordinates noise_region = np.where(coords < -75e-6) noise_data = experiment[noise_region] plt.figure(figsize=(3.75, 2.5)) ax = plt.subplot(projection="csdm") ax.imshow(noise_data, aspect="auto", interpolation="none") plt.title("Noise section") plt.axis("off") plt.tight_layout() plt.show() noise_mean, sigma = noise_data.mean(), noise_data.std() noise_mean, sigma .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_002.png :alt: Noise section :srcset: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (, ) .. GENERATED FROM PYTHON SOURCE LINES 69-74 Create a fitting model ---------------------- **Guess model** Create a guess list of spin systems. .. GENERATED FROM PYTHON SOURCE LINES 74-88 .. code-block:: Python shifts = [29, 39, 54.8, 51, 56] # in ppm Cq = [6.1e6, 5.4e6, 5.5e6, 5.5e6, 5.1e6] # in Hz eta = [0.1, 0.2, 0.15, 0.15, 0.3] abundance_ratio = [1, 1, 2, 2, 2] abundance = np.asarray(abundance_ratio) / 8 * 100 # in % spin_systems = single_site_system_generator( isotope="17O", isotropic_chemical_shift=shifts, quadrupolar={"Cq": Cq, "eta": eta}, abundance=abundance, ) .. GENERATED FROM PYTHON SOURCE LINES 89-92 **Method** Create the DAS method. .. GENERATED FROM PYTHON SOURCE LINES 92-132 .. code-block:: Python # Get the spectral dimension parameters from the experiment. spectral_dims = get_spectral_dimensions(experiment) DAS = Method( channels=["17O"], magnetic_flux_density=11.744, # in T rotor_frequency=np.inf, spectral_dimensions=[ SpectralDimension( **spectral_dims[0], events=[ SpectralEvent( fraction=0.5, rotor_angle=37.38 * np.pi / 180, # in rads transition_queries=[{"ch1": {"P": [-1], "D": [0]}}], ), MixingEvent(query="NoMixing"), SpectralEvent( fraction=0.5, rotor_angle=79.19 * np.pi / 180, # in rads transition_queries=[{"ch1": {"P": [-1], "D": [0]}}], ), MixingEvent(query="NoMixing"), ], ), # The last spectral dimension block is the direct-dimension SpectralDimension( **spectral_dims[1], events=[ SpectralEvent( rotor_angle=54.735 * np.pi / 180, # in rads transition_queries=[{"ch1": {"P": [-1], "D": [0]}}], ) ], ), ], experiment=experiment, # also add the measurement to the method. ) .. GENERATED FROM PYTHON SOURCE LINES 133-134 **Guess Spectrum** .. GENERATED FROM PYTHON SOURCE LINES 134-168 .. code-block:: Python # Simulation # ---------- sim = Simulator(spin_systems=spin_systems, methods=[DAS]) sim.config.number_of_sidebands = 1 # no sidebands are required for this dataset. sim.run() # Post Simulation Processing # -------------------------- processor = sp.SignalProcessor( operations=[ # Gaussian convolution along both dimensions. sp.IFFT(dim_index=(0, 1)), sp.apodization.Gaussian(FWHM="0.15 kHz", dim_index=0), sp.apodization.Gaussian(FWHM="0.1 kHz", dim_index=1), sp.FFT(dim_index=(0, 1)), sp.Scale(factor=4e8), ] ) 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.contour(experiment, colors="k", **options) ax.contour(processed_dataset, colors="r", linestyles="--", **options) ax.invert_xaxis() ax.set_ylim(30, -30) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_003.png :alt: plot 2 Coesite DAS :srcset: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 169-173 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 173-176 .. code-block:: Python params = sf.make_LMFIT_params(sim, processor) print(params.pretty_print(columns=["value", "min", "max", "vary", "expr"])) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Value Min Max Vary Expr SP_0_operation_1_Gaussian_FWHM 0.15 -inf inf True None SP_0_operation_2_Gaussian_FWHM 0.1 -inf inf True None SP_0_operation_4_Scale_factor 4e+08 -inf inf True None sys_0_abundance 12.5 0 100 True None sys_0_site_0_isotropic_chemical_shift 29 -inf inf True None sys_0_site_0_quadrupolar_Cq 6.1e+06 -inf inf True None sys_0_site_0_quadrupolar_eta 0.1 0 1 True None sys_1_abundance 12.5 0 100 True None sys_1_site_0_isotropic_chemical_shift 39 -inf inf True None sys_1_site_0_quadrupolar_Cq 5.4e+06 -inf inf True None sys_1_site_0_quadrupolar_eta 0.2 0 1 True None sys_2_abundance 25 0 100 True None sys_2_site_0_isotropic_chemical_shift 54.8 -inf inf True None sys_2_site_0_quadrupolar_Cq 5.5e+06 -inf inf True None sys_2_site_0_quadrupolar_eta 0.15 0 1 True None sys_3_abundance 25 0 100 True None sys_3_site_0_isotropic_chemical_shift 51 -inf inf True None sys_3_site_0_quadrupolar_Cq 5.5e+06 -inf inf True None sys_3_site_0_quadrupolar_eta 0.15 0 1 True None sys_4_abundance 25 0 100 False 100-sys_0_abundance-sys_1_abundance-sys_2_abundance-sys_3_abundance sys_4_site_0_isotropic_chemical_shift 56 -inf inf True None sys_4_site_0_quadrupolar_Cq 5.1e+06 -inf inf True None sys_4_site_0_quadrupolar_eta 0.3 0 1 True None None .. GENERATED FROM PYTHON SOURCE LINES 177-178 **Solve the minimizer using LMFIT** .. GENERATED FROM PYTHON SOURCE LINES 178-188 .. code-block:: Python opt = sim.optimize() # Pre-compute transition pathways minner = Minimizer( sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma), fcn_kws={"opt": opt}, ) result = minner.minimize(method="powell") result .. raw:: html

Fit Result



.. GENERATED FROM PYTHON SOURCE LINES 189-191 The best fit solution --------------------- .. GENERATED FROM PYTHON SOURCE LINES 191-204 .. code-block:: Python best_fit = sf.bestfit(sim, processor)[0].real # Plot the spectrum plt.figure(figsize=(4.25, 3.0)) ax = plt.subplot(projection="csdm") ax.contour(experiment, colors="k", **options) ax.contour(best_fit, colors="r", linestyles="--", **options) ax.invert_xaxis() ax.set_ylim(30, -30) plt.grid() plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_004.png :alt: plot 2 Coesite DAS :srcset: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 205-207 The best fit solution --------------------- .. GENERATED FROM PYTHON SOURCE LINES 207-220 .. code-block:: Python residuals = sf.residuals(sim, processor)[0].real fig, ax = plt.subplots( 1, 3, sharey=True, figsize=(10, 3.0), subplot_kw={"projection": "csdm"} ) vmax, vmin = experiment.max(), experiment.min() for i, dat in enumerate([experiment, best_fit, residuals]): ax[i].imshow(dat, aspect="auto", vmax=vmax, vmin=vmin, interpolation="none") ax[i].invert_xaxis() ax[0].set_ylim(30, -30) plt.tight_layout() plt.show() .. image-sg:: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_005.png :alt: plot 2 Coesite DAS :srcset: /fitting/2D_fitting/images/sphx_glr_plot_2_Coesite_DAS_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 221-227 .. [#f1] Grandinetti, P. J., Baltisberger, J. H., Farnan, I., Stebbins, J. F., Werner, U. and Pines, A. Solid-State :math:`^{17}\text{O}` Magic-Angle and Dynamic-Angle Spinning NMR Study of the :math:`\text{SiO}_2` Polymorph Coesite, J. Phys. Chem. 1995, **99**, *32*, 12341-12348. `DOI: 10.1021/j100032a045 `_ .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 26.978 seconds) .. _sphx_glr_download_fitting_2D_fitting_plot_2_Coesite_DAS.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2_Coesite_DAS.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2_Coesite_DAS.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_