Note
Click here to download the full example code or to run this example in your browser via Binder
³¹P static NMR of crystalline Na2PO4 (CSA)¶
The following is a CSA static least-squares fitting example of a \(^{31}\text{P}\) MAS NMR spectrum of \(\text{Na}_{2}\text{PO}_{4}\). The following experimental dataset is a part of DMFIT 1 examples. We thank Dr. Dominique Massiot for sharing the dataset.
import csdmpy as cp
import matplotlib.pyplot as plt
from lmfit import Minimizer, report_fit
from mrsimulator import Simulator, SpinSystem, Site
from mrsimulator.methods import BlochDecaySpectrum
from mrsimulator import signal_processing as sp
from mrsimulator.utils import spectral_fitting as sf
from mrsimulator.utils import get_spectral_dimensions
Import the dataset¶
host = "https://nmr.cemhti.cnrs-orleans.fr/Dmfit/Help/csdm/"
filename = "31P Phophonate Static.csdf"
experiment = cp.load(host + filename)
# standard deviation of noise from the dataset
sigma = 3.258224
# 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, color="black", linewidth=0.5, label="Experiment")
ax.set_xlim(200, -200)
plt.grid()
plt.tight_layout()
plt.show()

Create a fitting model¶
Spin System
P_31 = Site(
isotope="31P",
isotropic_chemical_shift=5.0, # in ppm,
shielding_symmetric={"zeta": -80, "eta": 0.5}, # zeta in Hz
)
spin_systems = [SpinSystem(sites=[P_31])]
Method
# Get the spectral dimension parameters from the experiment.
spectral_dims = get_spectral_dimensions(experiment)
static1D = BlochDecaySpectrum(
channels=["31P"],
magnetic_flux_density=9.395, # in T
rotor_frequency=0, # in Hz
spectral_dimensions=spectral_dims,
experiment=experiment, # experimental dataset
)
# Optimize the script by pre-setting the transition pathways for each spin system from
# the method.
for sys in spin_systems:
sys.transition_pathways = static1D.get_transition_pathways(sys)
Guess Model Spectrum
# Simulation
# ----------
sim = Simulator(spin_systems=spin_systems, methods=[static1D])
sim.run()
# Post Simulation Processing
# --------------------------
processor = sp.SignalProcessor(
operations=[
sp.IFFT(),
sp.apodization.Gaussian(FWHM="3000 Hz"),
sp.FFT(),
sp.Scale(factor=4000),
]
)
processed_data = processor.apply_operations(data=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, color="black", linewidth=0.5, label="Experiment")
ax.plot(processed_data, linewidth=2, alpha=0.6, label="Guess Spectrum")
ax.set_xlim(200, -200)
plt.grid()
plt.legend()
plt.tight_layout()
plt.show()

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.pop("sys_0_abundance")
print(params.pretty_print(columns=["value", "min", "max", "vary", "expr"]))
Out:
Name Value Min Max Vary Expr
SP_0_operation_1_Gaussian_FWHM 3000 -inf inf True None
SP_0_operation_3_Scale_factor 4000 -inf inf True None
sys_0_site_0_isotropic_chemical_shift 5 -inf inf True None
sys_0_site_0_shielding_symmetric_eta 0.5 0 1 True None
sys_0_site_0_shielding_symmetric_zeta -80 -inf inf True None
None
Solve the minimizer using LMFIT
minner = Minimizer(sf.LMFIT_min_function, params, fcn_args=(sim, processor, sigma))
result = minner.minimize()
report_fit(result)
Out:
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 69
# data points = 2048
# variables = 5
chi-square = 3405.48278
reduced chi-square = 1.66690298
Akaike info crit = 1051.45512
Bayesian info crit = 1079.57821
[[Variables]]
sys_0_site_0_isotropic_chemical_shift: 6.32228235 +/- 0.07291393 (1.15%) (init = 5)
sys_0_site_0_shielding_symmetric_zeta: -86.8324323 +/- 0.14006961 (0.16%) (init = -80)
sys_0_site_0_shielding_symmetric_eta: 0.18101163 +/- 0.02313105 (12.78%) (init = 0.5)
SP_0_operation_1_Gaussian_FWHM: 5978.91422 +/- 101.488619 (1.70%) (init = 3000)
SP_0_operation_3_Scale_factor: 4626.51660 +/- 6.23103807 (0.13%) (init = 4000)
[[Correlations]] (unreported correlations are < 0.100)
C(sys_0_site_0_shielding_symmetric_eta, SP_0_operation_1_Gaussian_FWHM) = -0.975
C(sys_0_site_0_isotropic_chemical_shift, sys_0_site_0_shielding_symmetric_zeta) = -0.590
C(sys_0_site_0_shielding_symmetric_zeta, SP_0_operation_1_Gaussian_FWHM) = 0.325
C(sys_0_site_0_shielding_symmetric_zeta, SP_0_operation_3_Scale_factor) = -0.308
C(sys_0_site_0_shielding_symmetric_zeta, sys_0_site_0_shielding_symmetric_eta) = -0.296
C(sys_0_site_0_isotropic_chemical_shift, SP_0_operation_3_Scale_factor) = 0.279
C(sys_0_site_0_isotropic_chemical_shift, sys_0_site_0_shielding_symmetric_eta) = -0.249
C(sys_0_site_0_isotropic_chemical_shift, SP_0_operation_1_Gaussian_FWHM) = 0.216
C(SP_0_operation_1_Gaussian_FWHM, SP_0_operation_3_Scale_factor) = 0.182
C(sys_0_site_0_shielding_symmetric_eta, SP_0_operation_3_Scale_factor) = -0.118
The best fit solution¶
best_fit = sf.bestfit(sim, processor)[0]
residuals = sf.residuals(sim, processor)[0]
# Plot the spectrum
plt.figure(figsize=(4.25, 3.0))
ax = plt.subplot(projection="csdm")
ax.plot(experiment, color="black", linewidth=0.5, label="Experiment")
ax.plot(residuals, color="gray", linewidth=0.5, label="Residual")
ax.plot(best_fit, linewidth=2, alpha=0.6, label="Best Fit")
ax.set_xlim(200, -200)
plt.grid()
plt.legend()
plt.tight_layout()
plt.show()

- 1
D.Massiot, F.Fayon, M.Capron, I.King, S.Le Calvé, B.Alonso, J.O.Durand, B.Bujoli, Z.Gan, G.Hoatson, ‘Modelling one and two-dimensional solid-state NMR spectra.’, Magn. Reson. Chem. 40 70-76 (2002) DOI: 10.1002/mrc.984
Total running time of the script: ( 0 minutes 1.372 seconds)