Note
Go to the end to download the full example code.
Computing NMR shielding tensors using ShiftML¶
- Authors:
Michele Ceriotti @ceriottm
This example shows how to compute NMR shielding tensors using a point-edge transformer model trained on the ShiftML dataset.
For the newer ShiftML4 model, which predicts shieldings of approximate hybrid-functional quality, and for combining it with machine-learned geometry relaxation, see Accurate NMR chemical shifts with ShiftML4 and PET-MOLS.
import os
import zipfile
import chemiscope
import matplotlib.pyplot as plt
import numpy as np
from ase.io import read
from atomistic_cookbook_utils import download_with_retry
from shiftml.ase import ShiftML
Create a ShiftML calculator and fetch a dataset¶
calculator = ShiftML("ShiftML3")
filename = "ShiftML_poly.zip"
download_with_retry(
"https://archive.materialscloud.org/records/j2fka-sda13/files/ShiftML_poly.zip",
filename,
)
with zipfile.ZipFile(filename, "r") as zip_ref:
for file in ["ShiftML_poly/Cocaine/cocaine_QuantumEspresso.xyz"]:
target = os.path.basename(file)
with zip_ref.open(file) as source, open(target, "wb") as dest:
dest.write(source.read())
frames_cocaine = read("cocaine_QuantumEspresso.xyz", index=":16")
reference = [frame.arrays["CS"] for frame in frames_cocaine]
2026-11-26 20:55:39,438 - INFO - Found model version in url_resolve
2026-11-26 20:55:39,438 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_0.pt?download=1
2026-11-26 20:55:39,439 - INFO - Found ShiftML30 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML30
2026-11-26 20:55:39,592 - INFO - Found model version in url_resolve
2026-11-26 20:55:39,592 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_1.pt?download=1
2026-11-26 20:55:39,592 - INFO - Found ShiftML31 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML31
2026-11-26 20:55:39,738 - INFO - Found model version in url_resolve
2026-11-26 20:55:39,739 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_2.pt?download=1
2026-11-26 20:55:39,739 - INFO - Found ShiftML32 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML32
2026-11-26 20:55:39,886 - INFO - Found model version in url_resolve
2026-11-26 20:55:39,886 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_3.pt?download=1
2026-11-26 20:55:39,886 - INFO - Found ShiftML33 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML33
2026-11-26 20:55:40,032 - INFO - Found model version in url_resolve
2026-11-26 20:55:40,032 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_4.pt?download=1
2026-11-26 20:55:40,033 - INFO - Found ShiftML34 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML34
2026-11-26 20:55:40,178 - INFO - Found model version in url_resolve
2026-11-26 20:55:40,178 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_5.pt?download=1
2026-11-26 20:55:40,179 - INFO - Found ShiftML35 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML35
2026-11-26 20:55:40,325 - INFO - Found model version in url_resolve
2026-11-26 20:55:40,325 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_6.pt?download=1
2026-11-26 20:55:40,325 - INFO - Found ShiftML36 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML36
2026-11-26 20:55:40,472 - INFO - Found model version in url_resolve
2026-11-26 20:55:40,472 - INFO - Resolving model version to model files at url: https://zenodo.org/records/15767390/files/model_7.pt?download=1
2026-11-26 20:55:40,472 - INFO - Found ShiftML37 in cache, and importing it from here: /home/runner/cookbook-cache/xdg/shiftml/ShiftML37
Predicts isotropic chemical shielding tensors, including uncertainty¶
predicted = [calculator.get_cs_iso_ensemble(frame) for frame in frames_cocaine]
Make a plot for all the H shielding values
h_shieldings = np.hstack(
[
[
r[f.symbols == "H"],
p[f.symbols == "H"].mean(axis=1),
p[f.symbols == "H"].std(axis=1),
]
for r, p, f in zip(reference, predicted, frames_cocaine)
]
)
and of the uncertainty
fig, ax = plt.subplots(figsize=(4, 3))
ax.plot(h_shieldings[0], h_shieldings[1], "o", markersize=2, alpha=0.5)

[<matplotlib.lines.Line2D object at 0x794c1f1a15d0>]
fig, ax = plt.subplots(figsize=(4, 3))
ax.loglog(
h_shieldings[2],
np.abs(h_shieldings[0] - h_shieldings[1]),
"o",
markersize=2,
alpha=0.5,
)
ax.plot([0.2, 0.8], [0.2, 0.8], "k--", lw=0.5)

[<matplotlib.lines.Line2D object at 0x794c1796df10>]
Anisotropic shielding tensors¶
tensors = [calculator.get_cs_tensor(frame) for frame in frames_cocaine]
h_tensors = np.array(
[
(t[iat] if f.symbols[iat] == "H" else np.zeros((3, 3)))
for t, f in zip(tensors, frames_cocaine)
for iat in range(len(f))
]
)
chemiscope.show(
frames_cocaine,
shapes={
"cs_ellipsoid": {
"kind": "ellipsoid",
"parameters": {
"global": {},
"atom": [
chemiscope.ellipsoid_from_tensor(cs_h * 0.05) for cs_h in h_tensors
],
},
}
},
mode="structure",
settings=chemiscope.quick_settings(
periodic=True,
structure_settings={
"shape": ["cs_ellipsoid"],
},
),
)
Total running time of the script: (2 minutes 11.810 seconds)