revise
Browse files- NMRGym_both_test.pkl +3 -0
- NMRGym_both_train.pkl +3 -0
- NMRGym_test_balanced.pkl +3 -0
- NMRGym_train_balanced.pkl +3 -0
- NMRGym_val_balanced.pkl +3 -0
- README.md +237 -0
- analyze_data.py +368 -0
- dataset_overview.png +3 -0
- dataset_stats.json +20 -0
- element_distribution.png +3 -0
- functional_groups.png +3 -0
- quick_analyze.py +313 -0
NMRGym_both_test.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a999773f00c4cf5688900bc8edd4dc490a83a6f9209d3041d2f5eb682bb6f86f
|
| 3 |
+
size 172546408
|
NMRGym_both_train.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:627e133032a30e8b03e35f53d560b9a8cb41700371b288802034e3069bd6d59a
|
| 3 |
+
size 1348822022
|
NMRGym_test_balanced.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:996cb692e86c86de5ac3735274500298b3e80d3e1f57868478270fe472301a6b
|
| 3 |
+
size 207945355
|
NMRGym_train_balanced.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5cd4fdb6f8a54b1b49c27b94f2c32e449e6499fe247ada04651b5ca85716ed54
|
| 3 |
+
size 1041901432
|
NMRGym_val_balanced.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:deb0c5d1e27527986ef994ac53f75528ddf2dce2874bbbe75012729f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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
task_categories:
|
| 4 |
+
- text-to-text
|
| 5 |
+
- tabular-regression
|
| 6 |
+
- tabular-classification
|
| 7 |
+
pretty_name: NMRGym
|
| 8 |
+
language:
|
| 9 |
+
- en
|
| 10 |
+
tags:
|
| 11 |
+
- chemistry
|
| 12 |
+
- nmr
|
| 13 |
+
- spectroscopy
|
| 14 |
+
- molecular-property-prediction
|
| 15 |
+
- drug-discovery
|
| 16 |
+
- cheminformatics
|
| 17 |
+
size_categories:
|
| 18 |
+
- 100K<n<1M
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
# ‼️ Important ‼️
|
| 22 |
+
"If you need access to the dataset, please email [zhengf723@connect.hkust-gz.edu.cn](mailto:zhengf723@connect.hkust-gz.edu.cn)"
|
| 23 |
+
|
| 24 |
+
# NMRGym
|
| 25 |
+
Benchmark on NMR Spectrum.
|
| 26 |
+
|
| 27 |
+
## Data Sources (Before Cleaning)
|
| 28 |
+
|
| 29 |
+
| Source | Records | Unique SMILES | Total Spectrums | ¹H NMR | ¹³C NMR |
|
| 30 |
+
| :------------------ | ----------: | ------------: | --------------: | ----------: | ----------: |
|
| 31 |
+
| **CH-NP** | 12,165 | 12,165 | 24,326 | 12,161 | 12,165 |
|
| 32 |
+
| **HMDB** | 1,791 | 896 | 3,278 | 1,566 | 1,712 |
|
| 33 |
+
| **NMRBank** | 148,914 | 142,964 | 297,043 | 148,437 | 148,606 |
|
| 34 |
+
| **NMRShiftDB 2024** | 41,019 | 39,631 | 50,416 | 18,570 | 31,846 |
|
| 35 |
+
| **NP-MRD** | 489,569 | 243,598 | 950,242 | 462,233 | 488,009 |
|
| 36 |
+
| **PubChem-NMR** | 1,647 | 1,535 | 2,605 | 1,556 | 1,049 |
|
| 37 |
+
| **SDBS** | 12,926 | 12,626 | 22,711 | 11,522 | 11,189 |
|
| 38 |
+
| **Total** | **708,031** | **430,690** | **1,350,621** | **656,045** | **694,576** |
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## Balanced Dataset Statistics (After Cleaning & Processing)
|
| 43 |
+
|
| 44 |
+
After data cleaning, filtering, quality control, and balanced scaffold splitting, the final NMRGym dataset consists of three splits:
|
| 45 |
+
|
| 46 |
+
| Split | Records | Unique SMILES | ¹H NMR | ¹³C NMR |
|
| 47 |
+
| :------- | ----------: | ------------: | ----------: | ----------: |
|
| 48 |
+
| **Train** | 402,676 | 250,822 | 402,676 | 402,676 |
|
| 49 |
+
| **Val** | 53,672 | 50,716 | 53,672 | 53,672 |
|
| 50 |
+
| **Test** | 80,069 | 74,568 | 80,069 | 80,069 |
|
| 51 |
+
| **Total** | **536,417** | **376,106** | **536,417** | **536,417** |
|
| 52 |
+
|
| 53 |
+
### Dataset Visualizations
|
| 54 |
+
|
| 55 |
+
<!-- 
|
| 56 |
+
*Figure 1: Overview of dataset statistics including total records, unique SMILES, data duplication, NMR spectra types, and top elements.*
|
| 57 |
+
|
| 58 |
+

|
| 59 |
+
*Figure 2: Distribution of 22 functional groups across train, validation, and test sets.*
|
| 60 |
+
|
| 61 |
+

|
| 62 |
+
*Figure 3: Distribution of common elements (C, H, O, N, F, Cl, Br, S, P, I) across datasets.* -->
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
### Quick Checklist
|
| 69 |
+
|
| 70 |
+
* [✅] Data cleaning: Heavy atom filtering and illegal smiles exclusion.
|
| 71 |
+
* [✅] Data Summary and Visualization.
|
| 72 |
+
* [✅] Data Split.
|
| 73 |
+
* [] 3D coord. generation. Rdkit.
|
| 74 |
+
* [✅] Toxic Property label generation.
|
| 75 |
+
* [✅] Function Group label generation.
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
## NMRGym Split Dataset Format (`nmrgym_spec_filtered_both_{train,test}.pkl`)
|
| 79 |
+
|
| 80 |
+
This dataset contains post-QC, scaffold-split molecular entries with paired NMR spectra, structure fingerprints, functional group annotations, and toxicity labels.
|
| 81 |
+
|
| 82 |
+
Each `.pkl` file is a Python list of dictionaries. Each dictionary corresponds to a single unique molecule.
|
| 83 |
+
|
| 84 |
+
### Example record structure
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
{
|
| 88 |
+
"smiles": "CC(=O)Oc1ccccc1C(=O)O",
|
| 89 |
+
"h_shift": [7.25, 7.32, 2.14, 1.27],
|
| 90 |
+
"c_shift": [128.5, 130.1, 172.9, 20.7],
|
| 91 |
+
"fingerprint": np.ndarray(shape=(2048,), dtype=np.uint8),
|
| 92 |
+
"fg_onehot": np.ndarray(shape=(22,), dtype=np.uint8),
|
| 93 |
+
"toxicity": np.ndarray(shape=(7,), dtype=np.int8),
|
| 94 |
+
}
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
---
|
| 98 |
+
|
| 99 |
+
### Field definitions
|
| 100 |
+
|
| 101 |
+
**`smiles`** (`str`)
|
| 102 |
+
Canonical SMILES string for this molecule.
|
| 103 |
+
|
| 104 |
+
**`h_shift`** (`list[float]`)
|
| 105 |
+
Experimental or curated ¹H NMR peak positions in ppm.
|
| 106 |
+
|
| 107 |
+
**`c_shift`** (`list[float]`)
|
| 108 |
+
Experimental or curated ¹³C NMR peak positions in ppm.
|
| 109 |
+
|
| 110 |
+
**`fingerprint`** (`np.ndarray(2048,)`, `uint8`)
|
| 111 |
+
RDKit Morgan fingerprint (radius = 2, 2048 bits). Encodes local circular substructures.
|
| 112 |
+
|
| 113 |
+
**`fg_onehot`** (`np.ndarray(22,)`, `uint8`)
|
| 114 |
+
Binary functional-group vector. `1` means the SMARTS pattern is present in the molecule.
|
| 115 |
+
Index mapping (0→21):
|
| 116 |
+
|
| 117 |
+
1. Alcohol
|
| 118 |
+
2. Carboxylic Acid
|
| 119 |
+
3. Ester
|
| 120 |
+
4. Ether
|
| 121 |
+
5. Aldehyde
|
| 122 |
+
6. Ketone
|
| 123 |
+
7. Alkene
|
| 124 |
+
8. Alkyne
|
| 125 |
+
9. Benzene
|
| 126 |
+
10. Primary Amine
|
| 127 |
+
11. Secondary Amine
|
| 128 |
+
12. Tertiary Amine
|
| 129 |
+
13. Amide
|
| 130 |
+
14. Cyano
|
| 131 |
+
15. Fluorine
|
| 132 |
+
16. Chlorine
|
| 133 |
+
17. Bromine
|
| 134 |
+
18. Iodine
|
| 135 |
+
19. Sulfonamide
|
| 136 |
+
20. Sulfone
|
| 137 |
+
21. Sulfide
|
| 138 |
+
22. Phosphoric Acid
|
| 139 |
+
|
| 140 |
+
**`toxicity`** (`np.ndarray(7,)`, `int8`)
|
| 141 |
+
Seven binary toxicity endpoints (1 = toxic / positive, 0 = negative):
|
| 142 |
+
0. AMES (mutagenicity)
|
| 143 |
+
|
| 144 |
+
1. DILI (Drug-Induced Liver Injury)
|
| 145 |
+
2. Carcinogens_Lagunin (carcinogenicity)
|
| 146 |
+
3. hERG (cardiotoxic channel block)
|
| 147 |
+
4. ClinTox (clinical toxicity)
|
| 148 |
+
5. NR-ER (endocrine / estrogen receptor)
|
| 149 |
+
6. SR-ARE (oxidative stress response)
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
### Train / Test meaning
|
| 154 |
+
|
| 155 |
+
* `nmrgym_spec_filtered_both_train.pkl`: scaffold-based training set.
|
| 156 |
+
* `nmrgym_spec_filtered_both_test.pkl`: scaffold-disjoint test set.
|
| 157 |
+
|
| 158 |
+
Scaffold split is computed using Bemis–Murcko scaffolds. This means test scaffolds do not appear in train, simulating generalization to new chemotypes instead of just new stereoisomers.
|
| 159 |
+
|
| 160 |
+
---
|
| 161 |
+
|
| 162 |
+
### Minimal usage example
|
| 163 |
+
|
| 164 |
+
```python
|
| 165 |
+
import pickle
|
| 166 |
+
from rdkit import Chem
|
| 167 |
+
from rdkit.Chem import AllChem
|
| 168 |
+
|
| 169 |
+
# load the dataset
|
| 170 |
+
test_path = "/gemini/user/private/NMRGym/utils/split_output/nmrgym_spec_filtered_both_test.pkl"
|
| 171 |
+
with open(test_path, "rb") as f:
|
| 172 |
+
dataset = pickle.load(f)
|
| 173 |
+
|
| 174 |
+
print("num molecules:", len(dataset))
|
| 175 |
+
print("keys:", dataset[0].keys())
|
| 176 |
+
print("example toxicity vector:", dataset[0]["toxicity"])
|
| 177 |
+
|
| 178 |
+
# build 3D conformer for the first molecule
|
| 179 |
+
smi = dataset[0]["smiles"]
|
| 180 |
+
mol = Chem.MolFromSmiles(smi)
|
| 181 |
+
mol = Chem.AddHs(mol) # add hydrogens for 3D
|
| 182 |
+
AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
| 183 |
+
AllChem.UFFOptimizeMolecule(mol)
|
| 184 |
+
|
| 185 |
+
# extract 3D coordinates (in Å)
|
| 186 |
+
conf = mol.GetConformer()
|
| 187 |
+
coords = []
|
| 188 |
+
for atom_idx in range(mol.GetNumAtoms()):
|
| 189 |
+
pos = conf.GetAtomPosition(atom_idx)
|
| 190 |
+
coords.append([pos.x, pos.y, pos.z])
|
| 191 |
+
|
| 192 |
+
print("3D coords for first molecule (Angstrom):")
|
| 193 |
+
for i, (x,y,z) in enumerate(coords):
|
| 194 |
+
sym = mol.GetAtomWithIdx(i).GetSymbol()
|
| 195 |
+
print(f"{i:2d} {sym:2s} {x:8.3f} {y:8.3f} {z:8.3f}")
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
### Notes on 3D coordinates
|
| 199 |
+
|
| 200 |
+
* We generate a single low-energy conformer using RDKit ETKDG embedding + UFF optimization.
|
| 201 |
+
* Coordinates are in Ångström.
|
| 202 |
+
* These coordinates are not guaranteed to match the experimental NMR conformer in solvent; they are intended for featurization (message passing models, geometry-aware models, etc.), not quantum-accurate geometry.
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
## Downstream Benchmark Tasks
|
| 207 |
+
|
| 208 |
+
This benchmark suite evaluates AI models on multiple spectroscopy-related prediction tasks. Each task reflects a distinct molecular reasoning aspect — from direct spectrum regression to property and toxicity prediction.
|
| 209 |
+
|
| 210 |
+
### 1. Spectral Prediction
|
| 211 |
+
|
| 212 |
+
| **Method / Paper Title** | **Model Type / Approach** | **Metric(s)** | **Model Size** | **Notes** |
|
| 213 |
+
| ------------------------ | ------------------------- | ------------- | -------------- | --------- |
|
| 214 |
+
| x | x |x | x | x |
|
| 215 |
+
|
| 216 |
+
### 2. Structure Prediction (Inverse NMR)
|
| 217 |
+
|
| 218 |
+
| **Method / Paper Title** | **Model Type / Approach** | **Metric(s)** | **Model Size** | **Notes** |
|
| 219 |
+
| ------------------------ | ------------------------- | ------------- | -------------- | --------- |
|
| 220 |
+
| x | x |x | x | x |
|
| 221 |
+
### 3. Molecular Fingerprint Prediction (Spec2FP)
|
| 222 |
+
|
| 223 |
+
| **Method / Paper Title** | **Model Type / Approach** | **Metric(s)** | **Model Size** | **Notes** |
|
| 224 |
+
| ------------------------ | ------------------------- | ------------- | -------------- | --------- |
|
| 225 |
+
| x | x |x | x | x |
|
| 226 |
+
### 4. Functional Group Classification (Spec2Func)
|
| 227 |
+
|
| 228 |
+
| **Method / Paper Title** | **Model Type / Approach** | **Metric(s)** | **Model Size** | **Notes** |
|
| 229 |
+
| ------------------------ | ------------------------- | ------------- | -------------- | --------- |
|
| 230 |
+
| x | x |x | x | x |
|
| 231 |
+
### 5. Molecular Toxicity Prediction (Spec2Tox / ADMET)
|
| 232 |
+
|
| 233 |
+
| **Method / Paper Title** | **Model Type / Approach** | **Metric(s)** | **Model Size** | **Notes** |
|
| 234 |
+
| ------------------------ | ------------------------- | ------------- | -------------- | --------- |
|
| 235 |
+
| x | x |x | x | x |
|
| 236 |
+
|
| 237 |
+
---
|
analyze_data.py
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Analyze NMRGym balanced datasets and generate visualizations
|
| 4 |
+
"""
|
| 5 |
+
import pickle
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import seaborn as sns
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from rdkit import Chem
|
| 11 |
+
from rdkit.Chem.Scaffolds import MurckoScaffold
|
| 12 |
+
import json
|
| 13 |
+
|
| 14 |
+
# Set style
|
| 15 |
+
sns.set_style("whitegrid")
|
| 16 |
+
plt.rcParams['font.size'] = 10
|
| 17 |
+
plt.rcParams['figure.dpi'] = 300
|
| 18 |
+
|
| 19 |
+
# Functional group names (index 0-21)
|
| 20 |
+
FG_NAMES = [
|
| 21 |
+
"Alcohol", "Carboxylic Acid", "Ester", "Ether", "Aldehyde", "Ketone",
|
| 22 |
+
"Alkene", "Alkyne", "Benzene", "Primary Amine", "Secondary Amine",
|
| 23 |
+
"Tertiary Amine", "Amide", "Cyano", "Fluorine", "Chlorine",
|
| 24 |
+
"Bromine", "Iodine", "Sulfonamide", "Sulfone", "Sulfide", "Phosphoric Acid"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def load_dataset(pkl_path):
|
| 28 |
+
"""Load a pickle file"""
|
| 29 |
+
with open(pkl_path, "rb") as f:
|
| 30 |
+
return pickle.load(f)
|
| 31 |
+
|
| 32 |
+
def get_scaffold(smiles):
|
| 33 |
+
"""Get Bemis-Murcko scaffold"""
|
| 34 |
+
try:
|
| 35 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 36 |
+
if mol is None:
|
| 37 |
+
return None
|
| 38 |
+
scaffold = MurckoScaffold.GetScaffoldForMol(mol)
|
| 39 |
+
return Chem.MolToSmiles(scaffold)
|
| 40 |
+
except:
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
def get_element_counts(smiles):
|
| 44 |
+
"""Get element counts from SMILES"""
|
| 45 |
+
try:
|
| 46 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 47 |
+
if mol is None:
|
| 48 |
+
return {}
|
| 49 |
+
element_counts = {}
|
| 50 |
+
for atom in mol.GetAtoms():
|
| 51 |
+
symbol = atom.GetSymbol()
|
| 52 |
+
element_counts[symbol] = element_counts.get(symbol, 0) + 1
|
| 53 |
+
return element_counts
|
| 54 |
+
except:
|
| 55 |
+
return {}
|
| 56 |
+
|
| 57 |
+
def analyze_dataset(dataset, name):
|
| 58 |
+
"""Analyze a single dataset"""
|
| 59 |
+
print(f"\n{'='*60}")
|
| 60 |
+
print(f"Analyzing {name}")
|
| 61 |
+
print(f"{'='*60}")
|
| 62 |
+
|
| 63 |
+
stats = {
|
| 64 |
+
'name': name,
|
| 65 |
+
'total_records': len(dataset),
|
| 66 |
+
'unique_smiles': len(set(d['smiles'] for d in dataset)),
|
| 67 |
+
'scaffolds': [],
|
| 68 |
+
'functional_groups': np.zeros(22),
|
| 69 |
+
'elements': Counter(),
|
| 70 |
+
'h_spectra': 0,
|
| 71 |
+
'c_spectra': 0,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
all_smiles = set()
|
| 75 |
+
scaffold_counter = Counter()
|
| 76 |
+
|
| 77 |
+
for record in dataset:
|
| 78 |
+
smiles = record['smiles']
|
| 79 |
+
all_smiles.add(smiles)
|
| 80 |
+
|
| 81 |
+
# Count spectra
|
| 82 |
+
if 'h_shift' in record and record['h_shift'] is not None and len(record['h_shift']) > 0:
|
| 83 |
+
stats['h_spectra'] += 1
|
| 84 |
+
if 'c_shift' in record and record['c_shift'] is not None and len(record['c_shift']) > 0:
|
| 85 |
+
stats['c_spectra'] += 1
|
| 86 |
+
|
| 87 |
+
# Get scaffold
|
| 88 |
+
scaffold = get_scaffold(smiles)
|
| 89 |
+
if scaffold:
|
| 90 |
+
scaffold_counter[scaffold] += 1
|
| 91 |
+
|
| 92 |
+
# Functional groups
|
| 93 |
+
if 'fg_onehot' in record:
|
| 94 |
+
stats['functional_groups'] += record['fg_onehot']
|
| 95 |
+
|
| 96 |
+
# Element counts
|
| 97 |
+
elem_counts = get_element_counts(smiles)
|
| 98 |
+
for elem, count in elem_counts.items():
|
| 99 |
+
stats['elements'][elem] += count
|
| 100 |
+
|
| 101 |
+
stats['unique_scaffolds'] = len(scaffold_counter)
|
| 102 |
+
stats['top_scaffolds'] = scaffold_counter.most_common(10)
|
| 103 |
+
|
| 104 |
+
print(f"Total records: {stats['total_records']:,}")
|
| 105 |
+
print(f"Unique SMILES: {stats['unique_smiles']:,}")
|
| 106 |
+
print(f"Unique scaffolds: {stats['unique_scaffolds']:,}")
|
| 107 |
+
print(f"¹H NMR spectra: {stats['h_spectra']:,}")
|
| 108 |
+
print(f"¹³C NMR spectra: {stats['c_spectra']:,}")
|
| 109 |
+
print(f"\nTop 5 elements:")
|
| 110 |
+
for elem, count in stats['elements'].most_common(5):
|
| 111 |
+
print(f" {elem}: {count:,}")
|
| 112 |
+
|
| 113 |
+
return stats
|
| 114 |
+
|
| 115 |
+
def plot_functional_groups(train_stats, val_stats, test_stats, output_path):
|
| 116 |
+
"""Plot functional group distribution"""
|
| 117 |
+
fig, ax = plt.subplots(figsize=(14, 6))
|
| 118 |
+
|
| 119 |
+
x = np.arange(len(FG_NAMES))
|
| 120 |
+
width = 0.25
|
| 121 |
+
|
| 122 |
+
train_fg = train_stats['functional_groups']
|
| 123 |
+
val_fg = val_stats['functional_groups']
|
| 124 |
+
test_fg = test_stats['functional_groups']
|
| 125 |
+
|
| 126 |
+
ax.bar(x - width, train_fg, width, label='Train', alpha=0.8)
|
| 127 |
+
ax.bar(x, val_fg, width, label='Val', alpha=0.8)
|
| 128 |
+
ax.bar(x + width, test_fg, width, label='Test', alpha=0.8)
|
| 129 |
+
|
| 130 |
+
ax.set_xlabel('Functional Group')
|
| 131 |
+
ax.set_ylabel('Count')
|
| 132 |
+
ax.set_title('Functional Group Distribution Across Datasets')
|
| 133 |
+
ax.set_xticks(x)
|
| 134 |
+
ax.set_xticklabels(FG_NAMES, rotation=45, ha='right')
|
| 135 |
+
ax.legend()
|
| 136 |
+
ax.grid(axis='y', alpha=0.3)
|
| 137 |
+
|
| 138 |
+
plt.tight_layout()
|
| 139 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 140 |
+
plt.close()
|
| 141 |
+
print(f"Saved: {output_path}")
|
| 142 |
+
|
| 143 |
+
def plot_element_distribution(train_stats, val_stats, test_stats, output_path):
|
| 144 |
+
"""Plot element distribution for common elements"""
|
| 145 |
+
# Focus on common organic elements
|
| 146 |
+
common_elements = ['C', 'H', 'O', 'N', 'F', 'Cl', 'Br', 'S', 'P', 'I']
|
| 147 |
+
|
| 148 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 149 |
+
|
| 150 |
+
# Get counts for each element
|
| 151 |
+
train_counts = [train_stats['elements'].get(e, 0) for e in common_elements]
|
| 152 |
+
val_counts = [val_stats['elements'].get(e, 0) for e in common_elements]
|
| 153 |
+
test_counts = [test_stats['elements'].get(e, 0) for e in common_elements]
|
| 154 |
+
|
| 155 |
+
x = np.arange(len(common_elements))
|
| 156 |
+
width = 0.25
|
| 157 |
+
|
| 158 |
+
ax.bar(x - width, train_counts, width, label='Train', alpha=0.8)
|
| 159 |
+
ax.bar(x, val_counts, width, label='Val', alpha=0.8)
|
| 160 |
+
ax.bar(x + width, test_counts, width, label='Test', alpha=0.8)
|
| 161 |
+
|
| 162 |
+
ax.set_xlabel('Element')
|
| 163 |
+
ax.set_ylabel('Total Count')
|
| 164 |
+
ax.set_title('Element Distribution Across Datasets')
|
| 165 |
+
ax.set_xticks(x)
|
| 166 |
+
ax.set_xticklabels(common_elements)
|
| 167 |
+
ax.legend()
|
| 168 |
+
ax.grid(axis='y', alpha=0.3)
|
| 169 |
+
ax.set_yscale('log')
|
| 170 |
+
|
| 171 |
+
plt.tight_layout()
|
| 172 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 173 |
+
plt.close()
|
| 174 |
+
print(f"Saved: {output_path}")
|
| 175 |
+
|
| 176 |
+
def plot_dataset_overview(train_stats, val_stats, test_stats, output_path):
|
| 177 |
+
"""Plot overview of dataset statistics"""
|
| 178 |
+
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
|
| 179 |
+
|
| 180 |
+
# 1. Total records and unique SMILES
|
| 181 |
+
ax = axes[0, 0]
|
| 182 |
+
datasets = ['Train', 'Val', 'Test']
|
| 183 |
+
total_records = [train_stats['total_records'], val_stats['total_records'], test_stats['total_records']]
|
| 184 |
+
unique_smiles = [train_stats['unique_smiles'], val_stats['unique_smiles'], test_stats['unique_smiles']]
|
| 185 |
+
|
| 186 |
+
x = np.arange(len(datasets))
|
| 187 |
+
width = 0.35
|
| 188 |
+
|
| 189 |
+
ax.bar(x - width/2, total_records, width, label='Total Records', alpha=0.8)
|
| 190 |
+
ax.bar(x + width/2, unique_smiles, width, label='Unique SMILES', alpha=0.8)
|
| 191 |
+
ax.set_ylabel('Count')
|
| 192 |
+
ax.set_title('Dataset Size Comparison')
|
| 193 |
+
ax.set_xticks(x)
|
| 194 |
+
ax.set_xticklabels(datasets)
|
| 195 |
+
ax.legend()
|
| 196 |
+
ax.grid(axis='y', alpha=0.3)
|
| 197 |
+
|
| 198 |
+
# Add value labels on bars
|
| 199 |
+
for i, (tr, us) in enumerate(zip(total_records, unique_smiles)):
|
| 200 |
+
ax.text(i - width/2, tr, f'{tr:,}', ha='center', va='bottom', fontsize=8)
|
| 201 |
+
ax.text(i + width/2, us, f'{us:,}', ha='center', va='bottom', fontsize=8)
|
| 202 |
+
|
| 203 |
+
# 2. Unique scaffolds
|
| 204 |
+
ax = axes[0, 1]
|
| 205 |
+
unique_scaffolds = [train_stats['unique_scaffolds'], val_stats['unique_scaffolds'], test_stats['unique_scaffolds']]
|
| 206 |
+
|
| 207 |
+
ax.bar(datasets, unique_scaffolds, alpha=0.8, color='coral')
|
| 208 |
+
ax.set_ylabel('Count')
|
| 209 |
+
ax.set_title('Unique Scaffolds')
|
| 210 |
+
ax.grid(axis='y', alpha=0.3)
|
| 211 |
+
|
| 212 |
+
for i, count in enumerate(unique_scaffolds):
|
| 213 |
+
ax.text(i, count, f'{count:,}', ha='center', va='bottom', fontsize=9)
|
| 214 |
+
|
| 215 |
+
# 3. NMR spectra types
|
| 216 |
+
ax = axes[1, 0]
|
| 217 |
+
h_spectra = [train_stats['h_spectra'], val_stats['h_spectra'], test_stats['h_spectra']]
|
| 218 |
+
c_spectra = [train_stats['c_spectra'], val_stats['c_spectra'], test_stats['c_spectra']]
|
| 219 |
+
|
| 220 |
+
x = np.arange(len(datasets))
|
| 221 |
+
width = 0.35
|
| 222 |
+
|
| 223 |
+
ax.bar(x - width/2, h_spectra, width, label='¹H NMR', alpha=0.8)
|
| 224 |
+
ax.bar(x + width/2, c_spectra, width, label='¹³C NMR', alpha=0.8)
|
| 225 |
+
ax.set_ylabel('Count')
|
| 226 |
+
ax.set_title('NMR Spectra Types')
|
| 227 |
+
ax.set_xticks(x)
|
| 228 |
+
ax.set_xticklabels(datasets)
|
| 229 |
+
ax.legend()
|
| 230 |
+
ax.grid(axis='y', alpha=0.3)
|
| 231 |
+
|
| 232 |
+
# 4. Top 5 elements (train set)
|
| 233 |
+
ax = axes[1, 1]
|
| 234 |
+
top_elements = train_stats['elements'].most_common(5)
|
| 235 |
+
elements = [e[0] for e in top_elements]
|
| 236 |
+
counts = [e[1] for e in top_elements]
|
| 237 |
+
|
| 238 |
+
ax.bar(elements, counts, alpha=0.8, color='skyblue')
|
| 239 |
+
ax.set_ylabel('Total Count')
|
| 240 |
+
ax.set_title('Top 5 Elements (Train Set)')
|
| 241 |
+
ax.grid(axis='y', alpha=0.3)
|
| 242 |
+
ax.set_yscale('log')
|
| 243 |
+
|
| 244 |
+
plt.tight_layout()
|
| 245 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 246 |
+
plt.close()
|
| 247 |
+
print(f"Saved: {output_path}")
|
| 248 |
+
|
| 249 |
+
def plot_scaffold_diversity(train_stats, val_stats, test_stats, output_path):
|
| 250 |
+
"""Plot scaffold diversity comparison"""
|
| 251 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 252 |
+
|
| 253 |
+
datasets = ['Train', 'Val', 'Test']
|
| 254 |
+
total_records = [train_stats['total_records'], val_stats['total_records'], test_stats['total_records']]
|
| 255 |
+
unique_scaffolds = [train_stats['unique_scaffolds'], val_stats['unique_scaffolds'], test_stats['unique_scaffolds']]
|
| 256 |
+
|
| 257 |
+
# Calculate scaffold diversity ratio
|
| 258 |
+
diversity_ratio = [s/r for s, r in zip(unique_scaffolds, total_records)]
|
| 259 |
+
|
| 260 |
+
x = np.arange(len(datasets))
|
| 261 |
+
width = 0.6
|
| 262 |
+
|
| 263 |
+
bars = ax.bar(x, diversity_ratio, width, alpha=0.8, color=['#1f77b4', '#ff7f0e', '#2ca02c'])
|
| 264 |
+
ax.set_ylabel('Scaffold Diversity Ratio')
|
| 265 |
+
ax.set_title('Scaffold Diversity (Unique Scaffolds / Total Records)')
|
| 266 |
+
ax.set_xticks(x)
|
| 267 |
+
ax.set_xticklabels(datasets)
|
| 268 |
+
ax.grid(axis='y', alpha=0.3)
|
| 269 |
+
ax.set_ylim(0, 1)
|
| 270 |
+
|
| 271 |
+
# Add value labels
|
| 272 |
+
for i, (bar, ratio) in enumerate(zip(bars, diversity_ratio)):
|
| 273 |
+
height = bar.get_height()
|
| 274 |
+
ax.text(bar.get_x() + bar.get_width()/2., height,
|
| 275 |
+
f'{ratio:.3f}\n({unique_scaffolds[i]:,}/{total_records[i]:,})',
|
| 276 |
+
ha='center', va='bottom', fontsize=9)
|
| 277 |
+
|
| 278 |
+
plt.tight_layout()
|
| 279 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 280 |
+
plt.close()
|
| 281 |
+
print(f"Saved: {output_path}")
|
| 282 |
+
|
| 283 |
+
def main():
|
| 284 |
+
# File paths
|
| 285 |
+
train_path = "/gemini/code/NMRGym/NMRGym_train_balanced.pkl"
|
| 286 |
+
val_path = "/gemini/code/NMRGym/NMRGym_val_balanced.pkl"
|
| 287 |
+
test_path = "/gemini/code/NMRGym/NMRGym_test_balanced.pkl"
|
| 288 |
+
|
| 289 |
+
# Load datasets
|
| 290 |
+
print("Loading datasets...")
|
| 291 |
+
train_data = load_dataset(train_path)
|
| 292 |
+
val_data = load_dataset(val_path)
|
| 293 |
+
test_data = load_dataset(test_path)
|
| 294 |
+
|
| 295 |
+
# Analyze each dataset
|
| 296 |
+
train_stats = analyze_dataset(train_data, "Train (Balanced)")
|
| 297 |
+
val_stats = analyze_dataset(val_data, "Val (Balanced)")
|
| 298 |
+
test_stats = analyze_dataset(test_data, "Test (Balanced)")
|
| 299 |
+
|
| 300 |
+
# Generate visualizations
|
| 301 |
+
print("\n" + "="*60)
|
| 302 |
+
print("Generating visualizations...")
|
| 303 |
+
print("="*60)
|
| 304 |
+
|
| 305 |
+
plot_dataset_overview(train_stats, val_stats, test_stats,
|
| 306 |
+
"/gemini/code/NMRGym/dataset_overview.png")
|
| 307 |
+
|
| 308 |
+
plot_functional_groups(train_stats, val_stats, test_stats,
|
| 309 |
+
"/gemini/code/NMRGym/functional_groups.png")
|
| 310 |
+
|
| 311 |
+
plot_element_distribution(train_stats, val_stats, test_stats,
|
| 312 |
+
"/gemini/code/NMRGym/element_distribution.png")
|
| 313 |
+
|
| 314 |
+
plot_scaffold_diversity(train_stats, val_stats, test_stats,
|
| 315 |
+
"/gemini/code/NMRGym/scaffold_diversity.png")
|
| 316 |
+
|
| 317 |
+
# Save statistics as JSON
|
| 318 |
+
summary = {
|
| 319 |
+
'train': {
|
| 320 |
+
'total_records': train_stats['total_records'],
|
| 321 |
+
'unique_smiles': train_stats['unique_smiles'],
|
| 322 |
+
'unique_scaffolds': train_stats['unique_scaffolds'],
|
| 323 |
+
'h_spectra': train_stats['h_spectra'],
|
| 324 |
+
'c_spectra': train_stats['c_spectra'],
|
| 325 |
+
},
|
| 326 |
+
'val': {
|
| 327 |
+
'total_records': val_stats['total_records'],
|
| 328 |
+
'unique_smiles': val_stats['unique_smiles'],
|
| 329 |
+
'unique_scaffolds': val_stats['unique_scaffolds'],
|
| 330 |
+
'h_spectra': val_stats['h_spectra'],
|
| 331 |
+
'c_spectra': val_stats['c_spectra'],
|
| 332 |
+
},
|
| 333 |
+
'test': {
|
| 334 |
+
'total_records': test_stats['total_records'],
|
| 335 |
+
'unique_smiles': test_stats['unique_smiles'],
|
| 336 |
+
'unique_scaffolds': test_stats['unique_scaffolds'],
|
| 337 |
+
'h_spectra': test_stats['h_spectra'],
|
| 338 |
+
'c_spectra': test_stats['c_spectra'],
|
| 339 |
+
}
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
with open('/gemini/code/NMRGym/dataset_stats.json', 'w') as f:
|
| 343 |
+
json.dump(summary, f, indent=2)
|
| 344 |
+
print("\nSaved: /gemini/code/NMRGym/dataset_stats.json")
|
| 345 |
+
|
| 346 |
+
# Print final summary table
|
| 347 |
+
print("\n" + "="*60)
|
| 348 |
+
print("FINAL SUMMARY")
|
| 349 |
+
print("="*60)
|
| 350 |
+
print(f"{'Dataset':<15} {'Records':>10} {'Unique SMILES':>15} {'Scaffolds':>12} {'¹H NMR':>10} {'¹³C NMR':>10}")
|
| 351 |
+
print("-" * 85)
|
| 352 |
+
for name, stats in [('Train', train_stats), ('Val', val_stats), ('Test', test_stats)]:
|
| 353 |
+
print(f"{name:<15} {stats['total_records']:>10,} {stats['unique_smiles']:>15,} {stats['unique_scaffolds']:>12,} "
|
| 354 |
+
f"{stats['h_spectra']:>10,} {stats['c_spectra']:>10,}")
|
| 355 |
+
|
| 356 |
+
total_records = train_stats['total_records'] + val_stats['total_records'] + test_stats['total_records']
|
| 357 |
+
total_unique = train_stats['unique_smiles'] + val_stats['unique_smiles'] + test_stats['unique_smiles']
|
| 358 |
+
total_scaffolds = train_stats['unique_scaffolds'] + val_stats['unique_scaffolds'] + test_stats['unique_scaffolds']
|
| 359 |
+
total_h = train_stats['h_spectra'] + val_stats['h_spectra'] + test_stats['h_spectra']
|
| 360 |
+
total_c = train_stats['c_spectra'] + val_stats['c_spectra'] + test_stats['c_spectra']
|
| 361 |
+
|
| 362 |
+
print("-" * 85)
|
| 363 |
+
print(f"{'Total':<15} {total_records:>10,} {total_unique:>15,} {total_scaffolds:>12,} "
|
| 364 |
+
f"{total_h:>10,} {total_c:>10,}")
|
| 365 |
+
print("="*60 + "\n")
|
| 366 |
+
|
| 367 |
+
if __name__ == "__main__":
|
| 368 |
+
main()
|
dataset_overview.png
ADDED
|
Git LFS Details
|
dataset_stats.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"train": {
|
| 3 |
+
"total_records": 402676,
|
| 4 |
+
"unique_smiles": 250822,
|
| 5 |
+
"h_spectra": 402676,
|
| 6 |
+
"c_spectra": 402676
|
| 7 |
+
},
|
| 8 |
+
"val": {
|
| 9 |
+
"total_records": 53672,
|
| 10 |
+
"unique_smiles": 50716,
|
| 11 |
+
"h_spectra": 53672,
|
| 12 |
+
"c_spectra": 53672
|
| 13 |
+
},
|
| 14 |
+
"test": {
|
| 15 |
+
"total_records": 80069,
|
| 16 |
+
"unique_smiles": 74568,
|
| 17 |
+
"h_spectra": 80069,
|
| 18 |
+
"c_spectra": 80069
|
| 19 |
+
}
|
| 20 |
+
}
|
element_distribution.png
ADDED
|
Git LFS Details
|
functional_groups.png
ADDED
|
Git LFS Details
|
quick_analyze.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Quick analysis of NMRGym balanced datasets without expensive scaffold computation
|
| 4 |
+
"""
|
| 5 |
+
import pickle
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import seaborn as sns
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from rdkit import Chem
|
| 11 |
+
import json
|
| 12 |
+
|
| 13 |
+
# Set style
|
| 14 |
+
sns.set_style("whitegrid")
|
| 15 |
+
plt.rcParams['font.size'] = 10
|
| 16 |
+
plt.rcParams['figure.dpi'] = 300
|
| 17 |
+
|
| 18 |
+
# Functional group names (index 0-21)
|
| 19 |
+
FG_NAMES = [
|
| 20 |
+
"Alcohol", "Carboxylic Acid", "Ester", "Ether", "Aldehyde", "Ketone",
|
| 21 |
+
"Alkene", "Alkyne", "Benzene", "Primary Amine", "Secondary Amine",
|
| 22 |
+
"Tertiary Amine", "Amide", "Cyano", "Fluorine", "Chlorine",
|
| 23 |
+
"Bromine", "Iodine", "Sulfonamide", "Sulfone", "Sulfide", "Phosphoric Acid"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
def load_dataset(pkl_path):
|
| 27 |
+
"""Load a pickle file"""
|
| 28 |
+
print(f"Loading {pkl_path}...")
|
| 29 |
+
with open(pkl_path, "rb") as f:
|
| 30 |
+
return pickle.load(f)
|
| 31 |
+
|
| 32 |
+
def get_element_counts(smiles):
|
| 33 |
+
"""Get element counts from SMILES"""
|
| 34 |
+
try:
|
| 35 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 36 |
+
if mol is None:
|
| 37 |
+
return {}
|
| 38 |
+
element_counts = {}
|
| 39 |
+
for atom in mol.GetAtoms():
|
| 40 |
+
symbol = atom.GetSymbol()
|
| 41 |
+
element_counts[symbol] = element_counts.get(symbol, 0) + 1
|
| 42 |
+
return element_counts
|
| 43 |
+
except:
|
| 44 |
+
return {}
|
| 45 |
+
|
| 46 |
+
def analyze_dataset(dataset, name):
|
| 47 |
+
"""Analyze a single dataset (quick version without scaffold)"""
|
| 48 |
+
print(f"\n{'='*60}")
|
| 49 |
+
print(f"Analyzing {name}")
|
| 50 |
+
print(f"{'='*60}")
|
| 51 |
+
|
| 52 |
+
stats = {
|
| 53 |
+
'name': name,
|
| 54 |
+
'total_records': len(dataset),
|
| 55 |
+
'unique_smiles': 0,
|
| 56 |
+
'functional_groups': np.zeros(22),
|
| 57 |
+
'elements': Counter(),
|
| 58 |
+
'h_spectra': 0,
|
| 59 |
+
'c_spectra': 0,
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
unique_smiles = set()
|
| 63 |
+
|
| 64 |
+
for i, record in enumerate(dataset):
|
| 65 |
+
if (i + 1) % 1000 == 0:
|
| 66 |
+
print(f" Processed {i+1}/{len(dataset)} records...")
|
| 67 |
+
|
| 68 |
+
smiles = record['smiles']
|
| 69 |
+
unique_smiles.add(smiles)
|
| 70 |
+
|
| 71 |
+
# Count spectra
|
| 72 |
+
if 'h_shift' in record and record['h_shift'] is not None and len(record['h_shift']) > 0:
|
| 73 |
+
stats['h_spectra'] += 1
|
| 74 |
+
if 'c_shift' in record and record['c_shift'] is not None and len(record['c_shift']) > 0:
|
| 75 |
+
stats['c_spectra'] += 1
|
| 76 |
+
|
| 77 |
+
# Functional groups
|
| 78 |
+
if 'fg_onehot' in record:
|
| 79 |
+
stats['functional_groups'] += record['fg_onehot']
|
| 80 |
+
|
| 81 |
+
# Element counts
|
| 82 |
+
elem_counts = get_element_counts(smiles)
|
| 83 |
+
for elem, count in elem_counts.items():
|
| 84 |
+
stats['elements'][elem] += count
|
| 85 |
+
|
| 86 |
+
stats['unique_smiles'] = len(unique_smiles)
|
| 87 |
+
|
| 88 |
+
print(f"Total records: {stats['total_records']:,}")
|
| 89 |
+
print(f"Unique SMILES: {stats['unique_smiles']:,}")
|
| 90 |
+
print(f"¹H NMR spectra: {stats['h_spectra']:,}")
|
| 91 |
+
print(f"¹³C NMR spectra: {stats['c_spectra']:,}")
|
| 92 |
+
print(f"\nTop 5 elements:")
|
| 93 |
+
for elem, count in stats['elements'].most_common(5):
|
| 94 |
+
print(f" {elem}: {count:,}")
|
| 95 |
+
|
| 96 |
+
return stats
|
| 97 |
+
|
| 98 |
+
def plot_functional_groups(train_stats, val_stats, test_stats, output_path):
|
| 99 |
+
"""Plot functional group distribution"""
|
| 100 |
+
fig, ax = plt.subplots(figsize=(14, 6))
|
| 101 |
+
|
| 102 |
+
x = np.arange(len(FG_NAMES))
|
| 103 |
+
width = 0.25
|
| 104 |
+
|
| 105 |
+
train_fg = train_stats['functional_groups']
|
| 106 |
+
val_fg = val_stats['functional_groups']
|
| 107 |
+
test_fg = test_stats['functional_groups']
|
| 108 |
+
|
| 109 |
+
ax.bar(x - width, train_fg, width, label='Train', alpha=0.8)
|
| 110 |
+
ax.bar(x, val_fg, width, label='Val', alpha=0.8)
|
| 111 |
+
ax.bar(x + width, test_fg, width, label='Test', alpha=0.8)
|
| 112 |
+
|
| 113 |
+
ax.set_xlabel('Functional Group')
|
| 114 |
+
ax.set_ylabel('Count')
|
| 115 |
+
ax.set_title('Functional Group Distribution Across Datasets')
|
| 116 |
+
ax.set_xticks(x)
|
| 117 |
+
ax.set_xticklabels(FG_NAMES, rotation=45, ha='right')
|
| 118 |
+
ax.legend()
|
| 119 |
+
ax.grid(axis='y', alpha=0.3)
|
| 120 |
+
|
| 121 |
+
plt.tight_layout()
|
| 122 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 123 |
+
plt.close()
|
| 124 |
+
print(f"\nSaved: {output_path}")
|
| 125 |
+
|
| 126 |
+
def plot_element_distribution(train_stats, val_stats, test_stats, output_path):
|
| 127 |
+
"""Plot element distribution for common elements"""
|
| 128 |
+
# Focus on common organic elements
|
| 129 |
+
common_elements = ['C', 'H', 'O', 'N', 'F', 'Cl', 'Br', 'S', 'P', 'I']
|
| 130 |
+
|
| 131 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 132 |
+
|
| 133 |
+
# Get counts for each element
|
| 134 |
+
train_counts = [train_stats['elements'].get(e, 0) for e in common_elements]
|
| 135 |
+
val_counts = [val_stats['elements'].get(e, 0) for e in common_elements]
|
| 136 |
+
test_counts = [test_stats['elements'].get(e, 0) for e in common_elements]
|
| 137 |
+
|
| 138 |
+
x = np.arange(len(common_elements))
|
| 139 |
+
width = 0.25
|
| 140 |
+
|
| 141 |
+
ax.bar(x - width, train_counts, width, label='Train', alpha=0.8)
|
| 142 |
+
ax.bar(x, val_counts, width, label='Val', alpha=0.8)
|
| 143 |
+
ax.bar(x + width, test_counts, width, label='Test', alpha=0.8)
|
| 144 |
+
|
| 145 |
+
ax.set_xlabel('Element')
|
| 146 |
+
ax.set_ylabel('Total Count')
|
| 147 |
+
ax.set_title('Element Distribution Across Datasets')
|
| 148 |
+
ax.set_xticks(x)
|
| 149 |
+
ax.set_xticklabels(common_elements)
|
| 150 |
+
ax.legend()
|
| 151 |
+
ax.grid(axis='y', alpha=0.3)
|
| 152 |
+
ax.set_yscale('log')
|
| 153 |
+
|
| 154 |
+
plt.tight_layout()
|
| 155 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 156 |
+
plt.close()
|
| 157 |
+
print(f"Saved: {output_path}")
|
| 158 |
+
|
| 159 |
+
def plot_dataset_overview(train_stats, val_stats, test_stats, output_path):
|
| 160 |
+
"""Plot overview of dataset statistics"""
|
| 161 |
+
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
|
| 162 |
+
|
| 163 |
+
# 1. Total records and unique SMILES
|
| 164 |
+
ax = axes[0, 0]
|
| 165 |
+
datasets = ['Train', 'Val', 'Test']
|
| 166 |
+
total_records = [train_stats['total_records'], val_stats['total_records'], test_stats['total_records']]
|
| 167 |
+
unique_smiles = [train_stats['unique_smiles'], val_stats['unique_smiles'], test_stats['unique_smiles']]
|
| 168 |
+
|
| 169 |
+
x = np.arange(len(datasets))
|
| 170 |
+
width = 0.35
|
| 171 |
+
|
| 172 |
+
ax.bar(x - width/2, total_records, width, label='Total Records', alpha=0.8)
|
| 173 |
+
ax.bar(x + width/2, unique_smiles, width, label='Unique SMILES', alpha=0.8)
|
| 174 |
+
ax.set_ylabel('Count')
|
| 175 |
+
ax.set_title('Dataset Size Comparison')
|
| 176 |
+
ax.set_xticks(x)
|
| 177 |
+
ax.set_xticklabels(datasets)
|
| 178 |
+
ax.legend()
|
| 179 |
+
ax.grid(axis='y', alpha=0.3)
|
| 180 |
+
|
| 181 |
+
# Add value labels on bars
|
| 182 |
+
for i, (tr, us) in enumerate(zip(total_records, unique_smiles)):
|
| 183 |
+
ax.text(i - width/2, tr, f'{tr:,}', ha='center', va='bottom', fontsize=8)
|
| 184 |
+
ax.text(i + width/2, us, f'{us:,}', ha='center', va='bottom', fontsize=8)
|
| 185 |
+
|
| 186 |
+
# 2. Data duplicates (Total vs Unique SMILES)
|
| 187 |
+
ax = axes[0, 1]
|
| 188 |
+
duplication_ratio = [1 - (u/t) if t > 0 else 0 for u, t in zip(unique_smiles, total_records)]
|
| 189 |
+
|
| 190 |
+
bars = ax.bar(datasets, duplication_ratio, alpha=0.8, color='coral')
|
| 191 |
+
ax.set_ylabel('Duplication Ratio')
|
| 192 |
+
ax.set_title('Data Duplication (1 - Unique/Total)')
|
| 193 |
+
ax.grid(axis='y', alpha=0.3)
|
| 194 |
+
ax.set_ylim(0, max(duplication_ratio) * 1.2 if max(duplication_ratio) > 0 else 1)
|
| 195 |
+
|
| 196 |
+
for i, (bar, ratio) in enumerate(zip(bars, duplication_ratio)):
|
| 197 |
+
height = bar.get_height()
|
| 198 |
+
ax.text(bar.get_x() + bar.get_width()/2., height,
|
| 199 |
+
f'{ratio:.2%}',
|
| 200 |
+
ha='center', va='bottom', fontsize=9)
|
| 201 |
+
|
| 202 |
+
# 3. NMR spectra types
|
| 203 |
+
ax = axes[1, 0]
|
| 204 |
+
h_spectra = [train_stats['h_spectra'], val_stats['h_spectra'], test_stats['h_spectra']]
|
| 205 |
+
c_spectra = [train_stats['c_spectra'], val_stats['c_spectra'], test_stats['c_spectra']]
|
| 206 |
+
|
| 207 |
+
x = np.arange(len(datasets))
|
| 208 |
+
width = 0.35
|
| 209 |
+
|
| 210 |
+
ax.bar(x - width/2, h_spectra, width, label='¹H NMR', alpha=0.8)
|
| 211 |
+
ax.bar(x + width/2, c_spectra, width, label='¹³C NMR', alpha=0.8)
|
| 212 |
+
ax.set_ylabel('Count')
|
| 213 |
+
ax.set_title('NMR Spectra Types')
|
| 214 |
+
ax.set_xticks(x)
|
| 215 |
+
ax.set_xticklabels(datasets)
|
| 216 |
+
ax.legend()
|
| 217 |
+
ax.grid(axis='y', alpha=0.3)
|
| 218 |
+
|
| 219 |
+
# 4. Top 5 elements (train set)
|
| 220 |
+
ax = axes[1, 1]
|
| 221 |
+
top_elements = train_stats['elements'].most_common(5)
|
| 222 |
+
elements = [e[0] for e in top_elements]
|
| 223 |
+
counts = [e[1] for e in top_elements]
|
| 224 |
+
|
| 225 |
+
ax.bar(elements, counts, alpha=0.8, color='skyblue')
|
| 226 |
+
ax.set_ylabel('Total Count')
|
| 227 |
+
ax.set_title('Top 5 Elements (Train Set)')
|
| 228 |
+
ax.grid(axis='y', alpha=0.3)
|
| 229 |
+
ax.set_yscale('log')
|
| 230 |
+
|
| 231 |
+
plt.tight_layout()
|
| 232 |
+
plt.savefig(output_path, dpi=300, bbox_inches='tight')
|
| 233 |
+
plt.close()
|
| 234 |
+
print(f"Saved: {output_path}")
|
| 235 |
+
|
| 236 |
+
def main():
|
| 237 |
+
# File paths
|
| 238 |
+
train_path = "/gemini/code/NMRGym/NMRGym_train_balanced.pkl"
|
| 239 |
+
val_path = "/gemini/code/NMRGym/NMRGym_val_balanced.pkl"
|
| 240 |
+
test_path = "/gemini/code/NMRGym/NMRGym_test_balanced.pkl"
|
| 241 |
+
|
| 242 |
+
# Load datasets
|
| 243 |
+
train_data = load_dataset(train_path)
|
| 244 |
+
val_data = load_dataset(val_path)
|
| 245 |
+
test_data = load_dataset(test_path)
|
| 246 |
+
|
| 247 |
+
# Analyze each dataset
|
| 248 |
+
train_stats = analyze_dataset(train_data, "Train (Balanced)")
|
| 249 |
+
val_stats = analyze_dataset(val_data, "Val (Balanced)")
|
| 250 |
+
test_stats = analyze_dataset(test_data, "Test (Balanced)")
|
| 251 |
+
|
| 252 |
+
# Generate visualizations
|
| 253 |
+
print("\n" + "="*60)
|
| 254 |
+
print("Generating visualizations...")
|
| 255 |
+
print("="*60)
|
| 256 |
+
|
| 257 |
+
plot_dataset_overview(train_stats, val_stats, test_stats,
|
| 258 |
+
"/gemini/code/NMRGym/dataset_overview.png")
|
| 259 |
+
|
| 260 |
+
plot_functional_groups(train_stats, val_stats, test_stats,
|
| 261 |
+
"/gemini/code/NMRGym/functional_groups.png")
|
| 262 |
+
|
| 263 |
+
plot_element_distribution(train_stats, val_stats, test_stats,
|
| 264 |
+
"/gemini/code/NMRGym/element_distribution.png")
|
| 265 |
+
|
| 266 |
+
# Save statistics as JSON
|
| 267 |
+
summary = {
|
| 268 |
+
'train': {
|
| 269 |
+
'total_records': train_stats['total_records'],
|
| 270 |
+
'unique_smiles': train_stats['unique_smiles'],
|
| 271 |
+
'h_spectra': train_stats['h_spectra'],
|
| 272 |
+
'c_spectra': train_stats['c_spectra'],
|
| 273 |
+
},
|
| 274 |
+
'val': {
|
| 275 |
+
'total_records': val_stats['total_records'],
|
| 276 |
+
'unique_smiles': val_stats['unique_smiles'],
|
| 277 |
+
'h_spectra': val_stats['h_spectra'],
|
| 278 |
+
'c_spectra': val_stats['c_spectra'],
|
| 279 |
+
},
|
| 280 |
+
'test': {
|
| 281 |
+
'total_records': test_stats['total_records'],
|
| 282 |
+
'unique_smiles': test_stats['unique_smiles'],
|
| 283 |
+
'h_spectra': test_stats['h_spectra'],
|
| 284 |
+
'c_spectra': test_stats['c_spectra'],
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
with open('/gemini/code/NMRGym/dataset_stats.json', 'w') as f:
|
| 289 |
+
json.dump(summary, f, indent=2)
|
| 290 |
+
print("\nSaved: /gemini/code/NMRGym/dataset_stats.json")
|
| 291 |
+
|
| 292 |
+
# Print final summary table
|
| 293 |
+
print("\n" + "="*60)
|
| 294 |
+
print("FINAL SUMMARY")
|
| 295 |
+
print("="*60)
|
| 296 |
+
print(f"{'Dataset':<15} {'Records':>10} {'Unique SMILES':>15} {'¹H NMR':>10} {'¹³C NMR':>10}")
|
| 297 |
+
print("-" * 70)
|
| 298 |
+
for name, stats in [('Train', train_stats), ('Val', val_stats), ('Test', test_stats)]:
|
| 299 |
+
print(f"{name:<15} {stats['total_records']:>10,} {stats['unique_smiles']:>15,} "
|
| 300 |
+
f"{stats['h_spectra']:>10,} {stats['c_spectra']:>10,}")
|
| 301 |
+
|
| 302 |
+
total_records = train_stats['total_records'] + val_stats['total_records'] + test_stats['total_records']
|
| 303 |
+
total_unique = train_stats['unique_smiles'] + val_stats['unique_smiles'] + test_stats['unique_smiles']
|
| 304 |
+
total_h = train_stats['h_spectra'] + val_stats['h_spectra'] + test_stats['h_spectra']
|
| 305 |
+
total_c = train_stats['c_spectra'] + val_stats['c_spectra'] + test_stats['c_spectra']
|
| 306 |
+
|
| 307 |
+
print("-" * 70)
|
| 308 |
+
print(f"{'Total':<15} {total_records:>10,} {total_unique:>15,} "
|
| 309 |
+
f"{total_h:>10,} {total_c:>10,}")
|
| 310 |
+
print("="*60 + "\n")
|
| 311 |
+
|
| 312 |
+
if __name__ == "__main__":
|
| 313 |
+
main()
|