-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathpretrained.py
More file actions
482 lines (446 loc) · 14.5 KB
/
pretrained.py
File metadata and controls
482 lines (446 loc) · 14.5 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python
"""Module to download and load pre-trained ALIGNN models."""
import requests
import os
import zipfile
from tqdm import tqdm
from alignn.models.alignn import ALIGNN, ALIGNNConfig
from torch.utils.data import DataLoader
import tempfile
import torch
import sys
import json
import argparse
from jarvis.core.atoms import Atoms
from alignn.graphs import Graph
from jarvis.db.jsonutils import dumpjson
import pandas as pd
from alignn.dataset import get_torch_dataset
import numpy as np
# from jarvis.core.graphs import Graph
tqdm.pandas()
"""
Name of the model, figshare link, number of outputs,
extra config params (optional)
"""
# See also, alignn/ff/ff.py
# Both alignn and alignn_atomwise
# models are shared
# See: alignn/ff/all_models_alignn.json
# to load as a calculator
all_models = {
"jv_formation_energy_peratom_alignn": [
"https://figshare.com/ndownloader/files/31458679",
1,
],
"jv_optb88vdw_total_energy_alignn": [
"https://figshare.com/ndownloader/files/31459642",
1,
],
"jv_optb88vdw_bandgap_alignn": [
"https://figshare.com/ndownloader/files/31459636",
1,
],
"jv_mbj_bandgap_alignn": [
"https://figshare.com/ndownloader/files/31458694",
1,
],
"jv_spillage_alignn": [
"https://figshare.com/ndownloader/files/31458736",
1,
],
"jv_slme_alignn": ["https://figshare.com/ndownloader/files/31458727", 1],
"jv_bulk_modulus_kv_alignn": [
"https://figshare.com/ndownloader/files/31458649",
1,
],
"jv_shear_modulus_gv_alignn": [
"https://figshare.com/ndownloader/files/31458724",
1,
],
"jv_n-Seebeck_alignn": [
"https://figshare.com/ndownloader/files/31458718",
1,
],
"jv_n-powerfact_alignn": [
"https://figshare.com/ndownloader/files/31458712",
1,
],
"intermat_cbm": [
"https://figshare.com/ndownloader/files/45392908",
1,
],
"intermat_vbm": [
"https://figshare.com/ndownloader/files/45392914",
1,
],
"intermat_phi": [
"https://figshare.com/ndownloader/files/45392911",
1,
],
"jv_magmom_oszicar_alignn": [
"https://figshare.com/ndownloader/files/31458685",
1,
],
"jv_kpoint_length_unit_alignn": [
"https://figshare.com/ndownloader/files/31458682",
1,
],
"jv_avg_elec_mass_alignn": [
"https://figshare.com/ndownloader/files/31458643",
1,
],
"jv_avg_hole_mass_alignn": [
"https://figshare.com/ndownloader/files/31458646",
1,
],
"jv_epsx_alignn": ["https://figshare.com/ndownloader/files/31458667", 1],
"jv_mepsx_alignn": ["https://figshare.com/ndownloader/files/31458703", 1],
"jv_max_efg_alignn": [
"https://figshare.com/ndownloader/files/31458691",
1,
],
"jv_ehull_alignn": ["https://figshare.com/ndownloader/files/31458658", 1],
"jv_dfpt_piezo_max_dielectric_alignn": [
"https://figshare.com/ndownloader/files/31458652",
1,
],
"jv_dfpt_piezo_max_dij_alignn": [
"https://figshare.com/ndownloader/files/31458655",
1,
],
"jv_exfoliation_energy_alignn": [
"https://figshare.com/ndownloader/files/31458676",
1,
],
"jv_supercon_tc_alignn": [
"https://figshare.com/ndownloader/files/38789199",
1,
],
"jv_supercon_edos_alignn": [
"https://figshare.com/ndownloader/files/39946300",
1,
],
"jv_supercon_debye_alignn": [
"https://figshare.com/ndownloader/files/39946297",
1,
],
"jv_supercon_a2F_alignn": [
"https://figshare.com/ndownloader/files/38801886",
100,
],
"mp_e_form_alignn": [
"https://figshare.com/ndownloader/files/31458811",
1,
],
"mp_gappbe_alignn": [
"https://figshare.com/ndownloader/files/31458814",
1,
],
"tinnet_O_alignn": ["https://figshare.com/ndownloader/files/41962800", 1],
"tinnet_N_alignn": ["https://figshare.com/ndownloader/files/41962797", 1],
"tinnet_OH_alignn": ["https://figshare.com/ndownloader/files/41962803", 1],
"AGRA_O_alignn": ["https://figshare.com/ndownloader/files/41966619", 1],
"AGRA_OH_alignn": ["https://figshare.com/ndownloader/files/41966610", 1],
"AGRA_CHO_alignn": ["https://figshare.com/ndownloader/files/41966643", 1],
"AGRA_CO_alignn": ["https://figshare.com/ndownloader/files/41966634", 1],
"AGRA_COOH_alignn": ["https://figshare.com/ndownloader/41966646", 1],
"qm9_U0_alignn": ["https://figshare.com/ndownloader/files/31459054", 1],
"qm9_U_alignn": ["https://figshare.com/ndownloader/files/31459051", 1],
"qm9_alpha_alignn": ["https://figshare.com/ndownloader/files/31459027", 1],
"qm9_gap_alignn": ["https://figshare.com/ndownloader/files/31459036", 1],
"qm9_G_alignn": ["https://figshare.com/ndownloader/files/31459033", 1],
"qm9_HOMO_alignn": ["https://figshare.com/ndownloader/files/31459042", 1],
"qm9_LUMO_alignn": ["https://figshare.com/ndownloader/files/31459045", 1],
"qm9_ZPVE_alignn": ["https://figshare.com/ndownloader/files/31459057", 1],
"hmof_co2_absp_alignn": [
"https://figshare.com/ndownloader/files/31459198",
5,
],
"hmof_max_co2_adsp_alignn": [
"https://figshare.com/ndownloader/files/31459207",
1,
],
"hmof_surface_area_m2g_alignn": [
"https://figshare.com/ndownloader/files/31459222",
1,
],
"hmof_surface_area_m2cm3_alignn": [
"https://figshare.com/ndownloader/files/31459219",
1,
],
"hmof_pld_alignn": ["https://figshare.com/ndownloader/files/31459216", 1],
"hmof_lcd_alignn": ["https://figshare.com/ndownloader/files/31459201", 1],
"hmof_void_fraction_alignn": [
"https://figshare.com/ndownloader/files/31459228",
1,
],
"ocp2020_all": ["https://figshare.com/ndownloader/files/41411025", 1],
"ocp2020_100k": ["https://figshare.com/ndownloader/files/41967303", 1],
"ocp2020_10k": ["https://figshare.com/ndownloader/files/41967330", 1],
"jv_pdos_alignn": [
"https://figshare.com/ndownloader/files/36757005",
66,
{"alignn_layers": 6, "gcn_layers": 6},
],
}
parser = argparse.ArgumentParser(
description="Atomistic Line Graph Neural Network Pretrained Models"
)
parser.add_argument(
"--model_name",
default="jv_formation_energy_peratom_alignn",
help="Choose a model from these "
+ str(len(list(all_models.keys())))
+ " models:"
+ ", ".join(list(all_models.keys())),
)
parser.add_argument(
"--file_format", default="poscar", help="poscar/cif/xyz/pdb file format."
)
parser.add_argument(
"--file_path",
default="alignn/examples/sample_data/POSCAR-JVASP-10.vasp",
help="Path to file.",
)
parser.add_argument(
"--cutoff",
default=8,
help="Distance cut-off for graph constuction"
+ ", usually 8 for solids and 5 for molecules.",
)
parser.add_argument(
"--max_neighbors",
default=12,
help="Maximum number of nearest neighbors in the periodic atomistic graph"
+ " construction.",
)
device = "cpu"
if torch.cuda.is_available():
device = torch.device("cuda")
# device = "cpu"
def get_all_models():
"""Return the figshare links for models."""
return all_models
def get_figshare_model(model_name="jv_formation_energy_peratom_alignn"):
"""Get ALIGNN torch models from figshare."""
# https://figshare.com/projects/ALIGNN_models/126478
tmp = all_models[model_name]
url = tmp[0]
# output_features = tmp[1]
# if len(tmp) > 2:
# config_params = tmp[2]
# else:
# config_params = {}
zfile = model_name + ".zip"
path = str(os.path.join(os.path.dirname(__file__), zfile))
if not os.path.isfile(path):
response = requests.get(url, stream=True)
total_size_in_bytes = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 Kibibyte
progress_bar = tqdm(
total=total_size_in_bytes, unit="iB", unit_scale=True
)
with open(path, "wb") as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
progress_bar.close()
zp = zipfile.ZipFile(path)
names = zp.namelist()
chks = []
cfg = []
for i in names:
if "checkpoint_" in i and "pt" in i:
tmp = i
chks.append(i)
if "config.json" in i:
cfg = i
if "best_model.pt" in i:
tmp = i
chks.append(i)
print("Using chk file", tmp, "from ", chks)
print("Path", os.path.abspath(path))
print("Config", os.path.abspath(cfg))
config = json.loads(zipfile.ZipFile(path).read(cfg))
# print("Loading the zipfile...", zipfile.ZipFile(path).namelist())
data = zipfile.ZipFile(path).read(tmp)
# model = ALIGNN(
# ALIGNNConfig(
# name="alignn", output_features=output_features, **config_params
# )
# )
model = ALIGNN(ALIGNNConfig(**config["model"]))
new_file, filename = tempfile.mkstemp()
with open(filename, "wb") as f:
f.write(data)
model.load_state_dict(torch.load(filename, map_location=device)["model"])
model.to(device)
model.eval()
if os.path.exists(filename):
os.remove(filename)
return model
def get_prediction(
model_name="jv_formation_energy_peratom_alignn",
atoms=None,
cutoff=8,
max_neighbors=12,
):
"""Get model prediction on a single structure."""
model = get_figshare_model(model_name)
# print("Loading completed.")
g, lg = Graph.atom_dgl_multigraph(
atoms,
cutoff=float(cutoff),
max_neighbors=max_neighbors,
)
lat = torch.tensor(atoms.lattice_mat)
out_data = (
model([g.to(device), lg.to(device), lat.to(device)])
.detach()
.cpu()
.numpy()
.flatten()
.tolist()
)
return out_data
def get_multiple_predictions(
atoms_array=[],
jids=[],
cutoff=8,
neighbor_strategy="k-nearest",
max_neighbors=12,
use_canonize=True,
target="prop",
atom_features="cgcnn",
line_graph=True,
workers=0,
filename="pred_data.json",
include_atoms=True,
pin_memory=False,
output_features=1,
batch_size=1,
model=None,
model_name="jv_formation_energy_peratom_alignn",
print_freq=100,
# use_lmdb=True,
):
"""Use pretrained model on a number of structures."""
# if use_lmdb:
# print("Using LMDB dataset.")
# from alignn.lmdb_dataset import get_torch_dataset
# else:
# print("Not using LMDB dataset, memory footprint maybe high.")
# from alignn.dataset import get_torch_dataset
# import glob
# atoms_array=[]
# for i in glob.glob("alignn/examples/sample_data/*.vasp"):
# atoms=Atoms.from_poscar(i)
# atoms_array.append(atoms)
# get_multiple_predictions(atoms_array=atoms_array)
if not jids:
jids = ["id-" + str(i) for i in np.arange(len(atoms_array))]
mem = []
for i, ii in tqdm(enumerate(atoms_array), total=len(atoms_array)):
info = {}
if isinstance(ii, Atoms):
ii = ii.to_dict()
info["atoms"] = ii # .to_dict()
info["prop"] = -9999 # place-holder only
info["jid"] = jids[i] # str(i)
mem.append(info)
if model is None:
try:
model = get_figshare_model(model_name)
except Exception as exp:
raise ValueError(
'Check is the model name exists using "pretrained.py -h"', exp
)
pass
# Note cut-off is usually 8 for solids and 5 for molecules
def atoms_to_graph(atoms):
"""Convert structure dict to DGLGraph."""
structure = Atoms.from_dict(atoms)
return Graph.atom_dgl_multigraph(
structure,
cutoff=cutoff,
atom_features="atomic_number",
max_neighbors=max_neighbors,
compute_line_graph=True,
use_canonize=use_canonize,
)
test_data = get_torch_dataset(
dataset=mem,
target="prop",
neighbor_strategy=neighbor_strategy,
atom_features=atom_features,
use_canonize=use_canonize,
line_graph=line_graph,
)
collate_fn = test_data.collate_line_graph
test_loader = DataLoader(
test_data,
batch_size=batch_size,
shuffle=False,
collate_fn=collate_fn,
drop_last=False,
num_workers=workers,
pin_memory=pin_memory,
)
results = []
with torch.no_grad():
ids = test_loader.dataset.ids
for dat, id in zip(test_loader, ids):
g, lg, lat, target = dat
out_data = model([g.to(device), lg.to(device), lat.to(device)])
out_data = out_data.cpu().numpy().tolist()
target = target.cpu().numpy().flatten().tolist()
info = {}
info["id"] = id
info["pred"] = out_data
results.append(info)
print_freq = int(print_freq)
if len(results) % print_freq == 0:
print(len(results))
df1 = pd.DataFrame(mem)
df2 = pd.DataFrame(results)
df2["jid"] = df2["id"]
df3 = pd.merge(df1, df2, on="jid")
save = []
for i, ii in df3.iterrows():
info = {}
info["id"] = ii["id"]
info["atoms"] = ii["atoms"]
info["pred"] = ii["pred"]
save.append(info)
dumpjson(data=save, filename=filename)
if __name__ == "__main__":
args = parser.parse_args(sys.argv[1:])
model_name = args.model_name
file_path = args.file_path
file_format = args.file_format
cutoff = args.cutoff
max_neighbors = args.max_neighbors
if file_format == "poscar":
atoms = Atoms.from_poscar(file_path)
elif file_format == "cif":
atoms = Atoms.from_cif(file_path)
elif file_format == "xyz":
atoms = Atoms.from_xyz(file_path, box_size=500)
elif file_format == "pdb":
atoms = Atoms.from_pdb(file_path, max_lat=500)
else:
raise NotImplementedError("File format not implemented", file_format)
out_data = get_prediction(
model_name=model_name,
cutoff=float(cutoff),
max_neighbors=int(max_neighbors),
atoms=atoms,
)
print("Predicted value:", model_name, file_path, out_data)
# import glob
# atoms_array = []
# for i in glob.glob("alignn/examples/sample_data/*.vasp"):
# atoms = Atoms.from_poscar(i)
# atoms_array.append(atoms)
# get_multiple_predictions(atoms_array=atoms_array)