Repository navigation
Expand file tree
/
Copy pathgraphs.py
More file actions
320 lines (276 loc) · 10.4 KB
/
Copy pathgraphs.py
File metadata and controls
320 lines (276 loc) · 10.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""Implementation based on the template of ALIGNN."""
from multiprocessing.context import ForkContext
from re import X
import numpy as np
import pandas as pd
from jarvis.core.specie import chem_data, get_node_attributes
# from jarvis.core.atoms import Atoms
from collections import defaultdict
from typing import List, Tuple, Sequence
import torch
from torch_geometric.data import Data
from torch_geometric.data.batch import Batch
class GraphDataset(torch.utils.data.Dataset):
def __init__(
self,
df: pd.DataFrame,
graphs: Sequence[Data],
target: str,
# id_tag="material_id",
):
self.df = df
self.graphs = graphs
self.target = target
# self.ids = self.df[id_tag]
self.labels = [torch.tensor(itm).type(torch.get_default_dtype()) for itm in self.df[target]]
self.feat_mask = [torch.tensor(itm).type(torch.get_default_dtype()) for itm in self.df["feature_mask"]]
self.rot_list = [torch.tensor(itm).type(torch.get_default_dtype()) for itm in self.df["rot_list"]]
self.equality = [itm for itm in self.df["matrix_equal"]]
# self.wignerD_num = [torch.Tensor(itm).float().type(torch.get_default_dtype()) for itm in self.df["wigner_D_num"]]
tmp_labels = torch.stack(self.labels).view(-1, 9)
print('dataset mean value ', tmp_labels.view(-1).mean())
diagonal = [0, 4, 8]
off_diagonal = [1, 2, 3, 5, 6, 7]
print('diagonal mean, std, max', tmp_labels[:,diagonal].mean(), tmp_labels[:,diagonal].std(), tmp_labels[:,diagonal].max())
print('off diagonal mean, std, max ', tmp_labels[:,off_diagonal].mean(), tmp_labels[:,off_diagonal].std(), tmp_labels[:,off_diagonal].max())
def __len__(self):
return len(self.labels)
def __getitem__(self, idx):
g = self.graphs[idx]
g.wignerD_num = None
label = self.labels[idx]
mask = self.feat_mask[idx]
equality = self.equality[idx]
rot_list = self.rot_list[idx]
return g, mask, equality, label, rot_list
@staticmethod
def collate(samples: List[Tuple[Data, torch.Tensor]]):
graphs, masks, equalitys, labels, rot_list = map(list, zip(*samples))
batched_graph = Batch.from_data_list(graphs)
return batched_graph, torch.stack(masks), torch.stack(equalitys), torch.stack(labels), rot_list
def canonize_edge(
src_id,
dst_id,
src_image,
dst_image,
):
"""Compute canonical edge representation.
Sort vertex ids
shift periodic images so the first vertex is in (0,0,0) image
"""
# store directed edges src_id <= dst_id
if dst_id < src_id:
src_id, dst_id = dst_id, src_id
src_image, dst_image = dst_image, src_image
# shift periodic images so that src is in (0,0,0) image
if not np.array_equal(src_image, (0, 0, 0)):
shift = src_image
src_image = tuple(np.subtract(src_image, shift))
dst_image = tuple(np.subtract(dst_image, shift))
assert src_image == (0, 0, 0)
return src_id, dst_id, src_image, dst_image
def E3Graph(
atoms=None,
cutoff=8,
max_neighbors=12,
id=None,
use_canonize=False,
reduce=False,
equivalent_atoms=None,
):
"""Construct k-NN edge list."""
# returns List[List[Tuple[site, distance, index, image]]]
lat = atoms.lattice
all_neighbors_now = atoms.get_all_neighbors(r=cutoff)
min_nbrs = min(len(neighborlist) for neighborlist in all_neighbors_now)
attempt = 0
if min_nbrs < max_neighbors:
lat = atoms.lattice
if cutoff < max(lat.a, lat.b, lat.c):
r_cut = max(lat.a, lat.b, lat.c)
else:
r_cut = 2 * cutoff
attempt += 1
return E3Graph(
atoms=atoms,
use_canonize=use_canonize,
cutoff=r_cut,
max_neighbors=max_neighbors,
id=id,
reduce=reduce,
equivalent_atoms=equivalent_atoms,
)
edges = defaultdict(set)
for site_idx, neighborlist in enumerate(all_neighbors_now):
# if reduce:
# if equivalent_atoms[site_idx] != site_idx:
# continue
# sort on distance
neighborlist = sorted(neighborlist, key=lambda x: x[2])
distances = np.array([nbr[2] for nbr in neighborlist])
ids = np.array([nbr[1] for nbr in neighborlist])
images = np.array([nbr[3] for nbr in neighborlist])
# find the distance to the k-th nearest neighbor
max_dist = distances[max_neighbors - 1]
ids = ids[distances <= max_dist]
images = images[distances <= max_dist]
distances = distances[distances <= max_dist]
for dst, image in zip(ids, images):
src_id, dst_id, _, dst_image = canonize_edge(
site_idx, dst, (0, 0, 0), tuple(image)
)
if use_canonize:
edges[(src_id, dst_id)].add(dst_image)
else:
edges[(site_idx, dst)].add(tuple(image))
return edges
def E3Graph_r(
atoms=None,
cutoff=6.0,
use_canonize=False,
):
"""Construct k-NN edge list."""
# returns List[List[Tuple[site, distance, index, image]]]
lat = atoms.lattice
all_neighbors_now = atoms.get_all_neighbors(r=cutoff)
edges = defaultdict(set)
for site_idx, neighborlist in enumerate(all_neighbors_now):
neighborlist = sorted(neighborlist, key=lambda x: x[2])
distances = np.array([nbr[2] for nbr in neighborlist])
ids = np.array([nbr[1] for nbr in neighborlist])
images = np.array([nbr[3] for nbr in neighborlist])
for dst, image in zip(ids, images):
src_id, dst_id, _, dst_image = canonize_edge(
site_idx, dst, (0, 0, 0), tuple(image)
)
if use_canonize:
edges[(src_id, dst_id)].add(dst_image)
else:
edges[(site_idx, dst)].add(tuple(image))
return edges
def equivalent_decrease(equivalent_atoms):
# here decrease the number of atoms in the crystal structure according to equivalent_atoms
new_id_maps = np.ones([len(equivalent_atoms)]) * -1
nid = 0
for cur_id in range(len(equivalent_atoms)):
if equivalent_atoms[cur_id] == cur_id: # map cur_id to new_id and new_id += 1
new_id_maps[cur_id] = nid
nid += 1
new_equivalent_list = [int(new_id_maps[itm]) for itm in equivalent_atoms]
assert np.min(new_equivalent_list) > -0.5
return new_equivalent_list
def build_undirected_edgedata(
atoms=None,
edges={},
reduce=False,
equivalent_atoms=None,
):
u = []
v = []
r = []
for (src_id, dst_id), images in edges.items():
for dst_image in images:
# fractional coordinate for periodic image of dst
dst_coord = atoms.frac_coords[dst_id] + dst_image
# cartesian displacement vector pointing from src -> dst
d = atoms.lattice.cart_coords(
dst_coord - atoms.frac_coords[src_id]
)
# if reduce:
# new_equivalent_list = equivalent_decrease(equivalent_atoms)
# dst_id = new_equivalent_list[dst_id]
# src_id = new_equivalent_list[src_id]
for uu, vv, dd in [(src_id, dst_id, d), (dst_id, src_id, -d)]:
u.append(uu)
v.append(vv)
r.append(dd)
u = torch.tensor(u)
v = torch.tensor(v)
r = torch.tensor(np.array(r)).type(torch.get_default_dtype())
return u, v, r
def build_undirected_edgedata_r(
atoms=None,
edges={},
reduce=False,
equivalent_atoms=None,
):
u = []
v = []
r = []
cell_offsets = []
offsets = []
pos = []
for (src_id, dst_id), images in edges.items():
for dst_image in images:
# fractional coordinate for periodic image of dst
dst_coord = atoms.frac_coords[dst_id] + dst_image
# cartesian displacement vector pointing from src -> dst
d = atoms.lattice.cart_coords(
dst_coord - atoms.frac_coords[src_id]
)
for uu, vv, dd in [(src_id, dst_id, d), (dst_id, src_id, -d)]:
u.append(uu)
v.append(vv)
r.append(dd)
cell_offsets.append(np.array(dst_image))
cell_offsets.append(-np.array(dst_image))
offsets.append(atoms.lattice.cart_coords(dst_image))
offsets.append(-atoms.lattice.cart_coords(dst_image))
u = torch.tensor(u)
v = torch.tensor(v)
r = torch.tensor(np.array(r)).type(torch.get_default_dtype())
cell_offsets = torch.tensor(cell_offsets)
offsets = torch.tensor(offsets)
pos = torch.tensor(atoms.lattice.cart_coords(atoms.frac_coords))
return u, v, r, cell_offsets, offsets, pos
def atoms2graphs(
atoms=None,
cutoff=4.0,
max_neighbors=12,
atom_features="cgcnn",
id=None,
use_canonize=True,
reduce=False,
equivalent_atoms=None,
):
edges = E3Graph(
atoms=atoms,
cutoff=cutoff,
max_neighbors=max_neighbors,
id=id,
use_canonize=use_canonize,
reduce=reduce,
equivalent_atoms=equivalent_atoms,
)
u, v, r = build_undirected_edgedata(atoms, edges, reduce=reduce, equivalent_atoms=equivalent_atoms)
sps_features = []
for ii, s in enumerate(atoms.elements):
feat = list(get_node_attributes(s, atom_features=atom_features))
sps_features.append(feat)
sps_features = np.array(sps_features)
node_features = torch.tensor(sps_features).type(
torch.get_default_dtype()
)
edge_index = torch.cat((u.unsqueeze(0), v.unsqueeze(0)), dim=0).long()
g = Data(x=node_features, edge_index=edge_index, edge_attr=r)
return g
def atoms2graphs_etgnn(
atoms=None,
cutoff=6.0,
use_canonize=True,
):
edges = E3Graph_r(
atoms=atoms,
cutoff=cutoff,
use_canonize=use_canonize,
)
u, v, r, cell_offsets, offsets, pos = build_undirected_edgedata_r(atoms, edges)
sps_features = []
for ii, s in enumerate(atoms.elements):
feat = list(get_node_attributes(s, atom_features="atomic_number"))
sps_features.append(feat)
sps_features = np.array(sps_features)
node_features = torch.tensor(sps_features)
edge_index = torch.cat((u.unsqueeze(0), v.unsqueeze(0)), dim=0).long()
g = Data(atomic_numbers=node_features, edge_index=edge_index, edge_attr=r, cell_offsets=cell_offsets, offsets=offsets, pos=pos)
return g