⁸⁷Rb 2D QMAT NMR of Rb₂SO₄

The following is an illustration for fitting 2D QMAT/QPASS datasets. The example dataset is a \(^{87}\text{Rb}\) 2D QMAT spectrum of \(\text{Rb}_2\text{SO}_4\) from Walder et al. [1]

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

Import the dataset

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()
plot 1 Rb2SO4 QMAT

Create a fitting model

Guess model

Create a guess list of spin systems.

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]]

Method

Create the SSB2D method.

# 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)

Guess Spectrum

# 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()
plot 1 Rb2SO4 QMAT

Least-squares minimization with LMFIT

Use the make_LMFIT_params() for a quick setup of the fitting parameters.

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"]))

Out:

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

Solve the minimizer using LMFIT

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


The best fit solution

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()
plot 1 Rb2SO4 QMAT

Total running time of the script: ( 0 minutes 9.262 seconds)

Gallery generated by Sphinx-Gallery