Release FairNMR500 v1.0.0
Browse filesPublish 500 paired 1H/13C NMR peak-table examples with molecular formula, solvent, reference SMILES, documentation, and standalone rank-matcher evaluation.
- LICENSE +19 -0
- README.md +151 -0
- data/test.jsonl +0 -0
- data/test.parquet +3 -0
- evaluation/README.md +48 -0
- evaluation/metric.py +209 -0
- requirements.txt +2 -0
LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FairNMR500
|
| 2 |
+
|
| 3 |
+
This release is licensed under the Creative Commons Attribution 4.0
|
| 4 |
+
International license (CC BY 4.0).
|
| 5 |
+
|
| 6 |
+
License summary: https://creativecommons.org/licenses/by/4.0/
|
| 7 |
+
Full legal text: https://creativecommons.org/licenses/by/4.0/legalcode
|
| 8 |
+
|
| 9 |
+
Underlying data attribution:
|
| 10 |
+
NMRexp: A database of 3.37 million experimental NMR spectra
|
| 11 |
+
Creator: Junjie Wang
|
| 12 |
+
Dataset DOI: https://doi.org/10.5281/zenodo.16809534
|
| 13 |
+
Source license declaration: https://zenodo.org/api/records/16809534
|
| 14 |
+
|
| 15 |
+
The FairNMR500 benchmark subset, release formatting, and accompanying evaluation
|
| 16 |
+
code are provided by DP Technology. Release formatting renames fields, parses
|
| 17 |
+
coupling-constant lists, and removes redundant fields and stored model outputs.
|
| 18 |
+
The release does not modify the existing reference structures or spectral
|
| 19 |
+
measurements. Please retain attribution and indicate any further modifications.
|
README.md
CHANGED
|
@@ -1,3 +1,154 @@
|
|
| 1 |
---
|
|
|
|
| 2 |
license: cc-by-4.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
pretty_name: FairNMR500
|
| 3 |
license: cc-by-4.0
|
| 4 |
+
size_categories:
|
| 5 |
+
- n<1K
|
| 6 |
+
tags:
|
| 7 |
+
- chemistry
|
| 8 |
+
- nmr
|
| 9 |
+
- molecular-structure-elucidation
|
| 10 |
+
- benchmark
|
| 11 |
+
- specxmaster
|
| 12 |
+
configs:
|
| 13 |
+
- config_name: default
|
| 14 |
+
data_files:
|
| 15 |
+
- split: test
|
| 16 |
+
path: data/test.parquet
|
| 17 |
---
|
| 18 |
+
|
| 19 |
+
# FairNMR500
|
| 20 |
+
|
| 21 |
+
## Overview
|
| 22 |
+
|
| 23 |
+
FairNMR500 is a 500-example benchmark for molecular structure elucidation from
|
| 24 |
+
one-dimensional experimental NMR peak tables. Each example provides a molecular
|
| 25 |
+
formula, a solvent, a ¹H NMR peak list, a ¹³C NMR peak list, and a reference molecular
|
| 26 |
+
structure represented as SMILES.
|
| 27 |
+
|
| 28 |
+
The benchmark corresponds to the open 500-case evaluation set described in
|
| 29 |
+
Section 5.3 of the [SpecXMaster Technical Report](https://arxiv.org/abs/2603.23101).
|
| 30 |
+
The report describes this set as sampled from its NMRexp test split. For the
|
| 31 |
+
versioned evaluation protocol, see
|
| 32 |
+
[Sections 5.1 and 5.3 of report v5](https://arxiv.org/html/2603.23101v5#S5.SS1).
|
| 33 |
+
|
| 34 |
+
This release contains structured peak tables. Raw FID files, continuous spectral
|
| 35 |
+
intensities, and spectrum images are not included.
|
| 36 |
+
|
| 37 |
+
## Dataset structure
|
| 38 |
+
|
| 39 |
+
The dataset has one split: **test**, with **500 examples**. Each row corresponds
|
| 40 |
+
to one molecule and its paired ¹H/¹³C NMR information.
|
| 41 |
+
|
| 42 |
+
| Field | Description |
|
| 43 |
+
| --- | --- |
|
| 44 |
+
| `id` | Stable release identifier, such as `fairnmr500_000`. |
|
| 45 |
+
| `fair_idx` | Index retained from the existing 500-example benchmark; not an upstream NMRexp record identifier. |
|
| 46 |
+
| `molecular_formula` | Molecular formula supplied as input. |
|
| 47 |
+
| `solvent` | Reported NMR solvent. |
|
| 48 |
+
| `h_nmr_peaks` | List of ¹H NMR peaks, with the attributes below. |
|
| 49 |
+
| `c_nmr_peaks` | List of ¹³C NMR peaks; each contains `delta_ppm` only. |
|
| 50 |
+
| `gt_smiles` | Existing standardized reference SMILES used for evaluation. |
|
| 51 |
+
| `gt_smiles_raw` | Reference SMILES before the existing standardization, retained for traceability. |
|
| 52 |
+
|
| 53 |
+
Each ¹H peak contains:
|
| 54 |
+
|
| 55 |
+
| Attribute | Meaning |
|
| 56 |
+
| --- | --- |
|
| 57 |
+
| `delta_ppm` | Peak chemical shift or reported centroid, in ppm. |
|
| 58 |
+
| `multiplicity` | Reported splitting label, such as `s`, `d`, `t`, or `m`. |
|
| 59 |
+
| `j_hz` | Reported coupling constants, in Hz. An empty list means not provided, not zero. |
|
| 60 |
+
| `n_hydrogens` | Reported hydrogen count corresponding to the integral. |
|
| 61 |
+
| `range_min_ppm`, `range_max_ppm` | Retained peak interval; equal for a single reported position. |
|
| 62 |
+
|
| 63 |
+
The ¹³C records contain chemical shifts only: intensities, integrals,
|
| 64 |
+
multiplicities, and coupling constants are not available in this benchmark file.
|
| 65 |
+
|
| 66 |
+
## Usage
|
| 67 |
+
|
| 68 |
+
Load the test split:
|
| 69 |
+
|
| 70 |
+
```bash
|
| 71 |
+
python -m pip install datasets
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
from datasets import load_dataset
|
| 76 |
+
|
| 77 |
+
test = load_dataset("dptech/FairNMR500", split="test")
|
| 78 |
+
example = test[0]
|
| 79 |
+
|
| 80 |
+
model_input = {
|
| 81 |
+
key: example[key]
|
| 82 |
+
for key in ("molecular_formula", "solvent", "h_nmr_peaks", "c_nmr_peaks")
|
| 83 |
+
}
|
| 84 |
+
reference = example["gt_smiles"]
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
Reference structures are evaluation labels and should be withheld from the
|
| 88 |
+
model during inference. JSONL and Parquet downloads represent the same 500
|
| 89 |
+
examples; the default Hugging Face configuration selects only Parquet.
|
| 90 |
+
|
| 91 |
+
## Evaluation
|
| 92 |
+
|
| 93 |
+
Use **rank-matcher hit@1 and hit@10**, following Section 5.1 of the technical
|
| 94 |
+
report. An example is a hit when at least one of its first k ranked predictions
|
| 95 |
+
has the same normalized molecular key as the reference. The denominator is all
|
| 96 |
+
500 examples; missing, empty, or invalid predictions count as misses.
|
| 97 |
+
|
| 98 |
+
The comparison key removes atom maps and explicit hydrogens, ignores a single
|
| 99 |
+
tetrahedral stereocenter, equates globally inverted enantiomers while preserving
|
| 100 |
+
relative stereochemistry, and applies the documented E/Z normalization rules.
|
| 101 |
+
Connectivity must otherwise match exactly. Predictions retain their original
|
| 102 |
+
rank positions, including duplicate and invalid candidates.
|
| 103 |
+
|
| 104 |
+
The standalone [evaluation script](evaluation/metric.py) implements these rules.
|
| 105 |
+
See [evaluation instructions](evaluation/README.md) for the prediction format and
|
| 106 |
+
command. The released GT itself is not modified using these equivalence rules.
|
| 107 |
+
|
| 108 |
+
## Data source and release processing
|
| 109 |
+
|
| 110 |
+
The source dataset is **NMRexp**, described in
|
| 111 |
+
[NMRexp: A database of 3.3 million experimental NMR spectra](https://doi.org/10.1038/s41597-025-06245-5).
|
| 112 |
+
FairNMR500 is the 500-case evaluation subset used in the SpecXMaster report, not
|
| 113 |
+
a release of the full NMRexp corpus or its full test split.
|
| 114 |
+
|
| 115 |
+
Release processing preserves sample order, reference structures, and existing
|
| 116 |
+
spectral values. It renames fields, converts coupling-constant strings to numeric
|
| 117 |
+
lists, removes redundant fields, and excludes stored model predictions and
|
| 118 |
+
reranking outputs. It does not generate additional spectral measurements or
|
| 119 |
+
correct reference structures.
|
| 120 |
+
|
| 121 |
+
The current subset does not retain per-example source DOIs, acquisition
|
| 122 |
+
frequencies, or original-document page references.
|
| 123 |
+
|
| 124 |
+
Release validation found 500 parseable standardized
|
| 125 |
+
reference SMILES, 500 unique canonical isomeric reference structures, and no
|
| 126 |
+
molecular-formula discrepancies. These checks establish format and consistency;
|
| 127 |
+
they do not constitute independent manual verification of every reference.
|
| 128 |
+
|
| 129 |
+
## Citation
|
| 130 |
+
|
| 131 |
+
If you use FairNMR500, please cite the SpecXMaster Technical Report and acknowledge
|
| 132 |
+
NMRexp as the underlying data source.
|
| 133 |
+
|
| 134 |
+
```bibtex
|
| 135 |
+
@misc{ge2026specxmaster,
|
| 136 |
+
title = {SpecXMaster Technical Report},
|
| 137 |
+
author = {Yutang Ge and Yaning Cui and Hanzheng Li and Jun-Jie Wang and Fanjie Xu and Jinhan Dong and Yongqi Jin and Dongxu Cui and Peng Jin and Guojiang Zhao and Hengxing Cai and Tianci Yangfeng and Xueqing Chen and Hongshuai Wang and Rong Zhu and Linfeng Zhang and Xiaohong Ji and Zhifeng Gao},
|
| 138 |
+
year = {2026},
|
| 139 |
+
eprint = {2603.23101},
|
| 140 |
+
archivePrefix = {arXiv},
|
| 141 |
+
primaryClass = {cs.LG},
|
| 142 |
+
url = {https://arxiv.org/abs/2603.23101}
|
| 143 |
+
}
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
## License
|
| 147 |
+
|
| 148 |
+
This release is distributed under **Creative Commons Attribution 4.0
|
| 149 |
+
International (CC BY 4.0)**, following the license declared for the underlying
|
| 150 |
+
[NMRexp dataset](https://zenodo.org/records/16809534). See [LICENSE](LICENSE).
|
| 151 |
+
|
| 152 |
+
Attribution: NMRexp, Junjie Wang, DOI: 10.5281/zenodo.16809534. This benchmark
|
| 153 |
+
subset and its release formatting are provided by DP Technology. Changes from
|
| 154 |
+
the existing benchmark records are described in the processing section above.
|
data/test.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/test.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71dde01aab49cee39f77252e94e31580fbe0e717cc4a13db4b7c98ee4470ad6d
|
| 3 |
+
size 83120
|
evaluation/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# FairNMR500 evaluation
|
| 2 |
+
|
| 3 |
+
Requires Python 3.10+ and RDKit. From the repository root:
|
| 4 |
+
|
| 5 |
+
```bash
|
| 6 |
+
python -m pip install -r requirements.txt
|
| 7 |
+
python evaluation/metric.py --predictions predictions.jsonl --output metrics.json
|
| 8 |
+
```
|
| 9 |
+
|
| 10 |
+
Prepare one JSON object per line, using the released sample `id` and an ordered
|
| 11 |
+
list of predicted SMILES:
|
| 12 |
+
|
| 13 |
+
```json
|
| 14 |
+
{"id": "fairnmr500_000", "smiles": ["CCO", "CCCO"]}
|
| 15 |
+
{"id": "fairnmr500_001", "smiles": []}
|
| 16 |
+
```
|
| 17 |
+
|
| 18 |
+
These two rows illustrate the format, not benchmark predictions. Empty lists,
|
| 19 |
+
omitted samples, empty strings, and invalid SMILES count as misses. Invalid and
|
| 20 |
+
duplicate SMILES retain their original candidate positions; candidates are not
|
| 21 |
+
deduplicated or promoted. Duplicate IDs, unknown IDs, malformed JSON, and
|
| 22 |
+
non-string candidates are rejected as input errors. Prediction order across
|
| 23 |
+
rows does not matter; candidate order within each row does.
|
| 24 |
+
|
| 25 |
+
The script reports `correct@1`, `correct@10`, `hit@1`, and `hit@10`. The hit rates
|
| 26 |
+
are fractions between 0 and 1. The denominator is the full reference file
|
| 27 |
+
(500 examples for this release), including samples without predictions.
|
| 28 |
+
|
| 29 |
+
## Molecular comparison
|
| 30 |
+
|
| 31 |
+
The rank matcher follows Section 5.1 of the
|
| 32 |
+
[SpecXMaster Technical Report, v5](https://arxiv.org/html/2603.23101v5#S5.SS1):
|
| 33 |
+
|
| 34 |
+
- Parse SMILES with RDKit, remove atom maps and explicit hydrogens, and produce
|
| 35 |
+
canonical isomeric representations.
|
| 36 |
+
- Ignore the configuration of a single tetrahedral stereocenter.
|
| 37 |
+
- For multiple tetrahedral stereocenters, equate full-inversion enantiomers and
|
| 38 |
+
preserve relative stereochemistry; partial inversion is not generally equal.
|
| 39 |
+
- Normalize unspecified stereogenic double bonds to E, while retaining explicit
|
| 40 |
+
E/Z labels except for the original matcher's nitrogen-associated patterns.
|
| 41 |
+
The exact implemented pattern is a double-bond endpoint that is nitrogen, or
|
| 42 |
+
nitrogen directly attached to either endpoint.
|
| 43 |
+
- Require all other connectivity to match exactly. Empty/invalid SMILES have no
|
| 44 |
+
comparison key and cannot match a valid reference.
|
| 45 |
+
|
| 46 |
+
The evaluation equivalences do not change the GT stored in the dataset. The
|
| 47 |
+
script uses no internal services, model weights, or inference-project modules.
|
| 48 |
+
Dependencies are pinned to the RDKit version used in release validation.
|
evaluation/metric.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Standalone rank-matcher hit@k for FairNMR500 (Python >= 3.10).
|
| 3 |
+
|
| 4 |
+
The structural normalization follows the SpecXMaster rank matcher. Candidate
|
| 5 |
+
positions are preserved, including duplicates and invalid SMILES.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
from functools import lru_cache
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
from rdkit import Chem, rdBase
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _canonical_no_map_no_h(mol):
|
| 19 |
+
mol = Chem.Mol(mol)
|
| 20 |
+
for atom in mol.GetAtoms():
|
| 21 |
+
atom.SetAtomMapNum(0)
|
| 22 |
+
mol = Chem.RemoveHs(mol, implicitOnly=False)
|
| 23 |
+
return Chem.MolToSmiles(mol, canonical=True, isomericSmiles=True)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def _find_chiral_center_indices(mol):
|
| 27 |
+
mol = Chem.Mol(mol)
|
| 28 |
+
Chem.AssignStereochemistry(mol, cleanIt=True, force=True)
|
| 29 |
+
centers = Chem.FindMolChiralCenters(
|
| 30 |
+
mol, includeUnassigned=True, useLegacyImplementation=False
|
| 31 |
+
)
|
| 32 |
+
return sorted({int(idx) for idx, _ in centers})
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _clear_single_chiral_center(mol, center_idx):
|
| 36 |
+
mol = Chem.Mol(mol)
|
| 37 |
+
atom = mol.GetAtomWithIdx(int(center_idx))
|
| 38 |
+
atom.SetChiralTag(Chem.rdchem.ChiralType.CHI_UNSPECIFIED)
|
| 39 |
+
if atom.HasProp("_CIPCode"):
|
| 40 |
+
atom.ClearProp("_CIPCode")
|
| 41 |
+
return mol
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _invert_all_tetrahedral_centers(mol, center_indices):
|
| 45 |
+
mol = Chem.Mol(mol)
|
| 46 |
+
for idx in center_indices:
|
| 47 |
+
atom = mol.GetAtomWithIdx(int(idx))
|
| 48 |
+
tag = atom.GetChiralTag()
|
| 49 |
+
if tag == Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CW:
|
| 50 |
+
atom.SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CCW)
|
| 51 |
+
elif tag == Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CCW:
|
| 52 |
+
atom.SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CW)
|
| 53 |
+
return mol
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _alkene_has_terminal_nitrogen(bond):
|
| 57 |
+
# Preserve the original matcher's exact pattern: a double-bond endpoint
|
| 58 |
+
# is N, or N is directly attached to either endpoint.
|
| 59 |
+
begin, end = bond.GetBeginAtom(), bond.GetEndAtom()
|
| 60 |
+
if begin.GetAtomicNum() == 7 or end.GetAtomicNum() == 7:
|
| 61 |
+
return True
|
| 62 |
+
return any(
|
| 63 |
+
neighbor.GetIdx() != other.GetIdx() and neighbor.GetAtomicNum() == 7
|
| 64 |
+
for atom, other in ((begin, end), (end, begin))
|
| 65 |
+
for neighbor in atom.GetNeighbors()
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _clear_neighbor_single_bond_dirs(double_bond):
|
| 70 |
+
for atom in (double_bond.GetBeginAtom(), double_bond.GetEndAtom()):
|
| 71 |
+
for bond in atom.GetBonds():
|
| 72 |
+
if (
|
| 73 |
+
bond.GetIdx() != double_bond.GetIdx()
|
| 74 |
+
and bond.GetBondType() == Chem.BondType.SINGLE
|
| 75 |
+
):
|
| 76 |
+
bond.SetBondDir(Chem.BondDir.NONE)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def _force_unspecified_alkene_to_trans(mol):
|
| 80 |
+
mol = Chem.Mol(mol)
|
| 81 |
+
Chem.FindPotentialStereoBonds(mol)
|
| 82 |
+
changed = False
|
| 83 |
+
for bond in mol.GetBonds():
|
| 84 |
+
if bond.GetBondType() != Chem.BondType.DOUBLE or bond.GetIsAromatic():
|
| 85 |
+
continue
|
| 86 |
+
if _alkene_has_terminal_nitrogen(bond):
|
| 87 |
+
if bond.GetStereo() != Chem.BondStereo.STEREONONE:
|
| 88 |
+
bond.SetStereo(Chem.BondStereo.STEREONONE)
|
| 89 |
+
_clear_neighbor_single_bond_dirs(bond)
|
| 90 |
+
changed = True
|
| 91 |
+
continue
|
| 92 |
+
if bond.GetStereo() == Chem.BondStereo.STEREOANY:
|
| 93 |
+
bond.SetStereo(Chem.BondStereo.STEREOE)
|
| 94 |
+
changed = True
|
| 95 |
+
if changed:
|
| 96 |
+
Chem.SetDoubleBondNeighborDirections(mol)
|
| 97 |
+
return mol
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@lru_cache(maxsize=65536)
|
| 101 |
+
def get_rank_key(smiles: str) -> str | None:
|
| 102 |
+
"""Return a molecular comparison key; empty/invalid input has no key."""
|
| 103 |
+
if not isinstance(smiles, str) or not smiles.strip():
|
| 104 |
+
return None
|
| 105 |
+
with rdBase.BlockLogs():
|
| 106 |
+
try:
|
| 107 |
+
mol = Chem.MolFromSmiles(smiles.strip())
|
| 108 |
+
if mol is None:
|
| 109 |
+
return None
|
| 110 |
+
mol = _force_unspecified_alkene_to_trans(mol)
|
| 111 |
+
centers = _find_chiral_center_indices(mol)
|
| 112 |
+
if not centers:
|
| 113 |
+
return _canonical_no_map_no_h(mol)
|
| 114 |
+
if len(centers) == 1:
|
| 115 |
+
return _canonical_no_map_no_h(
|
| 116 |
+
_clear_single_chiral_center(mol, centers[0])
|
| 117 |
+
)
|
| 118 |
+
own = _canonical_no_map_no_h(mol)
|
| 119 |
+
mirror = _canonical_no_map_no_h(
|
| 120 |
+
_invert_all_tetrahedral_centers(mol, centers)
|
| 121 |
+
)
|
| 122 |
+
return "||".join(sorted([own, mirror]))
|
| 123 |
+
except Exception:
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _index_unique(rows, label):
|
| 128 |
+
indexed = {}
|
| 129 |
+
for row in rows:
|
| 130 |
+
if not isinstance(row, dict) or not isinstance(row.get("id"), str):
|
| 131 |
+
raise ValueError(f"{label}: each row must contain a string id")
|
| 132 |
+
if not row["id"] or row["id"] in indexed:
|
| 133 |
+
raise ValueError(f"{label}: empty or duplicate id {row['id']!r}")
|
| 134 |
+
indexed[row["id"]] = row
|
| 135 |
+
return indexed
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def evaluate(references, predictions, ks=(1, 10)):
|
| 139 |
+
"""Score all reference IDs; omitted prediction rows count as misses."""
|
| 140 |
+
if not ks or any(type(k) is not int or k < 1 for k in ks):
|
| 141 |
+
raise ValueError("ks must contain positive integer cutoffs")
|
| 142 |
+
refs = _index_unique(references, "references")
|
| 143 |
+
preds = _index_unique(predictions, "predictions")
|
| 144 |
+
if not refs:
|
| 145 |
+
raise ValueError("reference dataset is empty")
|
| 146 |
+
unknown = set(preds) - set(refs)
|
| 147 |
+
if unknown:
|
| 148 |
+
raise ValueError(f"unknown prediction IDs: {sorted(unknown)[:5]}")
|
| 149 |
+
for sample_id, row in preds.items():
|
| 150 |
+
candidates = row.get("smiles")
|
| 151 |
+
if not isinstance(candidates, list) or not all(
|
| 152 |
+
isinstance(s, str) for s in candidates
|
| 153 |
+
):
|
| 154 |
+
raise ValueError(f"{sample_id}: smiles must be a list of strings")
|
| 155 |
+
|
| 156 |
+
counts = {k: 0 for k in ks}
|
| 157 |
+
for sample_id, ref in refs.items():
|
| 158 |
+
gt = ref.get("gt_smiles")
|
| 159 |
+
if not isinstance(gt, str) or not (gt_key := get_rank_key(gt)):
|
| 160 |
+
raise ValueError(f"{sample_id}: invalid reference SMILES")
|
| 161 |
+
candidates = preds.get(sample_id, {}).get("smiles", [])
|
| 162 |
+
rank = next(
|
| 163 |
+
(i for i, smi in enumerate(candidates[: max(ks)], start=1)
|
| 164 |
+
if get_rank_key(smi) == gt_key),
|
| 165 |
+
None,
|
| 166 |
+
)
|
| 167 |
+
for k in ks:
|
| 168 |
+
counts[k] += int(rank is not None and rank <= k)
|
| 169 |
+
|
| 170 |
+
result = {
|
| 171 |
+
"num_samples": len(refs),
|
| 172 |
+
"num_prediction_rows": len(preds),
|
| 173 |
+
"num_missing_predictions": len(refs) - len(preds),
|
| 174 |
+
"match_mode": "rank-matcher",
|
| 175 |
+
"rdkit_version": rdBase.rdkitVersion,
|
| 176 |
+
}
|
| 177 |
+
for k in ks:
|
| 178 |
+
result[f"correct@{k}"] = counts[k]
|
| 179 |
+
result[f"hit@{k}"] = counts[k] / len(refs)
|
| 180 |
+
return result
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def read_jsonl(path):
|
| 184 |
+
with Path(path).open(encoding="utf-8") as handle:
|
| 185 |
+
return [json.loads(line) for line in handle if line.strip()]
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def main():
|
| 189 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 190 |
+
parser.add_argument(
|
| 191 |
+
"--references", type=Path,
|
| 192 |
+
default=Path(__file__).resolve().parents[1] / "data/test.jsonl",
|
| 193 |
+
)
|
| 194 |
+
parser.add_argument("--predictions", type=Path, required=True)
|
| 195 |
+
parser.add_argument("--output", type=Path)
|
| 196 |
+
args = parser.parse_args()
|
| 197 |
+
result = evaluate(read_jsonl(args.references), read_jsonl(args.predictions))
|
| 198 |
+
text = json.dumps(result, indent=2, ensure_ascii=False) + "\n"
|
| 199 |
+
print(text, end="")
|
| 200 |
+
if args.output:
|
| 201 |
+
if args.output.resolve() in {
|
| 202 |
+
args.references.resolve(), args.predictions.resolve()
|
| 203 |
+
}:
|
| 204 |
+
raise ValueError("output cannot overwrite an input file")
|
| 205 |
+
args.output.write_text(text, encoding="utf-8")
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
if __name__ == "__main__":
|
| 209 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python >= 3.10; needed only for evaluation/metric.py.
|
| 2 |
+
rdkit==2025.9.3
|