-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
227 lines (183 loc) · 5.92 KB
/
Copy pathdata.py
File metadata and controls
227 lines (183 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
Data loading and preprocessing for reactive atom prediction.
Updates:
18.10.2025 Initial script [Rangsiman Ketkaew]
"""
import numpy as np
from rdkit import Chem
def build_mol_from_xyz(Z, pos, bond_fuzz=0.45):
# Construct undirected graph
# covalent radii for common QM9 elements
cov_radii = {1: 0.31, 6: 0.76, 7: 0.71, 8: 0.66, 9: 0.57}
rw = Chem.RWMol()
for z in Z:
rw.AddAtom(Chem.Atom(int(z)))
N = len(Z)
# Add bonds based on distance
edge_list = []
for i in range(N):
for j in range(i + 1, N):
dij = float(np.linalg.norm(pos[i] - pos[j]))
ri = cov_radii.get(int(Z[i]), 0.7)
rj = cov_radii.get(int(Z[j]), 0.7)
if dij <= ri + rj + bond_fuzz:
try:
rw.AddBond(int(i), int(j), Chem.BondType.SINGLE)
edge_list.append([i, j])
edge_list.append([j, i])
except Exception:
pass
mol = rw.GetMol()
# try:
# Chem.SanitizeMol(mol)
# except Exception:
# pass
# partial sanitization
mol.UpdatePropertyCache(strict=False)
Chem.SanitizeMol(
mol,
sanitizeOps=Chem.SanitizeFlags.SANITIZE_ALL
^ Chem.SanitizeFlags.SANITIZE_PROPERTIES,
)
# Attach 3D conformer
conf = Chem.Conformer(mol.GetNumAtoms())
for i, (x, y, z) in enumerate(pos):
conf.SetAtomPosition(i, (float(x), float(y), float(z)))
mol.AddConformer(conf, assignId=True)
return mol, edge_list
def xyz_to_data(Z, pos, target):
"""
Create dataset of graph features from Cartesian coordinates
Args:
Z: array of atomic numbers
R: array of positions
target: target energy value
Returns:
"x": node features from RDKit
"edge_index": edge connectivity
"edge_attr": edge features from RDKit
"E": energy
"""
# Build RDKit molecule from XYZ
mol, edge_list = build_mol_from_xyz(Z, pos, bond_fuzz=0.45)
# -------------------------
# Calculate node features
# -------------------------
x = []
for atom in mol.GetAtoms():
feat = [
atom.GetAtomicNum(),
atom.GetDegree(),
atom.GetTotalValence(),
atom.GetFormalCharge(),
int(atom.GetIsAromatic()),
atom.GetHybridization().real,
]
x.append(feat)
x = np.array(x, dtype=np.float32)
# -------------------------
# Calculate edge features
# -------------------------
edge_attr = []
edge_index = []
if len(edge_list) > 0:
edge_index = np.array(edge_list, dtype=np.int64).T
for i, j in edge_list:
bond = mol.GetBondBetweenAtoms(int(i), int(j))
if bond is not None:
feat = [
bond.GetBondTypeAsDouble(),
int(bond.GetIsConjugated()),
int(bond.IsInRing()),
]
else:
feat = [1.0, 0, 0]
edge_attr.append(feat)
edge_attr = np.array(edge_attr, dtype=np.float32)
else:
# no edges
edge_index = np.zeros((2, 0), dtype=np.int64)
edge_attr = np.zeros((0, 3), dtype=np.float32)
return {
"Z": Z,
"R": pos,
"x": x,
"edge_index": edge_index,
"edge_attr": edge_attr,
"E": float(target),
}
def qm9_to_data(qm9_data, target="U0", recal_features=False):
"""
Create dataset from QM9 data object
Returns:
"x": node features from RDKit
"edge_index": edge connectivity
"edge_attr": edge features from RDKit
"E": energy
"""
Z = qm9_data.z.cpu().numpy().astype(np.int32)
pos = qm9_data.pos.cpu().numpy().astype(np.float32)
x = qm9_data.x.cpu().numpy().astype(np.float32)
edge_index = qm9_data.edge_index.cpu().numpy().astype(np.float32)
edge_attr = qm9_data.edge_attr.cpu().numpy().astype(np.float32)
y = qm9_data.y.cpu().numpy().reshape(-1)
# https://pytorch-geometric.readthedocs.io/en/2.6.1/generated/torch_geometric.datasets.QM9.html
name_to_idx = {"U0": 7, "U": 8, "H": 9}
idx = name_to_idx.get(target, 7)
E = float(y[idx])
if recal_features:
feat = xyz_to_data(Z, pos, E)
x = feat["x"]
edge_index = feat["edge_index"]
edge_attr = feat["edge_attr"]
return {
"Z": Z,
"R": pos,
"x": x,
"edge_index": edge_index,
"edge_attr": edge_attr,
"E": float(E),
}
def qm9_splits(
qm9,
n_train=10000,
n_val=1000,
n_test=1000,
target="U0",
shuffle=False,
seed=12345,
subset=None,
):
"""Import QM9 dataset and create dataset for training a model
# targets: QM9 stores 19 targets in qm9_data.y
# see list of properties in torch_geometric.datasets.QM9
Args:
qm9_ds: QM9 dataset object from torch_geometric
n_train: number of training examples
n_val: number of validation examples
n_test: number of test examples
target: QM9 target property name (U0, U, H, etc.)
seed: random seed for shuffling
subset: optional limit on total dataset size
Returns:
train, val, test: dict of graph features
"""
# Shuffle indices
total = len(qm9)
if shuffle:
rng = np.random.default_rng(seed)
idx = np.arange(total)
rng.shuffle(idx)
qm9 = qm9[idx]
if subset is not None:
total = min(total, int(subset))
qm9 = qm9[:total]
total = len(qm9)
n_train = min(n_train, total)
n_val = min(n_val, max(0, total - n_train))
n_test = min(n_test, max(0, total - n_train - n_val))
my_data = [qm9_to_data(qm9[i], target, recal_features=True) for i in range(total)]
train = my_data[:n_train]
val = my_data[n_train : n_train + n_val]
test = my_data[n_train + n_val : n_train + n_val + n_test]
return train, val, test