-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterials.py
More file actions
720 lines (602 loc) · 22.7 KB
/
Copy pathmaterials.py
File metadata and controls
720 lines (602 loc) · 22.7 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import json
import warnings
import math
import numpy as np
import scipy
import pandas as pd
import networkx as nx
import torch
import copy
import itertools
from pathlib import Path
from pymatgen.core.structure import Structure
from pymatgen.core.lattice import Lattice
from sklearn.metrics import accuracy_score, recall_score, precision_score
from p_tqdm import p_umap
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pyxtal import pyxtal
import faulthandler
faulthandler.enable()
# Tensor of unit cells. Assumes 27 cells in -1, 0, 1 offsets in the x and y dimensions
# Note that differing from OCP, we have 27 offsets here because we are in 3D
OFFSET_LIST = [
[-1, -1, -1],
[-1, -1, 0],
[-1, -1, 1],
[-1, 0, -1],
[-1, 0, 0],
[-1, 0, 1],
[-1, 1, -1],
[-1, 1, 0],
[-1, 1, 1],
[0, -1, -1],
[0, -1, 0],
[0, -1, 1],
[0, 0, -1],
[0, 0, 0],
[0, 0, 1],
[0, 1, -1],
[0, 1, 0],
[0, 1, 1],
[1, -1, -1],
[1, -1, 0],
[1, -1, 1],
[1, 0, -1],
[1, 0, 0],
[1, 0, 1],
[1, 1, -1],
[1, 1, 0],
[1, 1, 1],
]
EPSILON = 1e-5
chemical_symbols = [
# 0
'X',
# 1
'H', 'He',
# 2
'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne',
# 3
'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar',
# 4
'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn',
'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr',
# 5
'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd',
'In', 'Sn', 'Sb', 'Te', 'I', 'Xe',
# 6
'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy',
'Ho', 'Er', 'Tm', 'Yb', 'Lu',
'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi',
'Po', 'At', 'Rn',
# 7
'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk',
'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr',
'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Nh', 'Fl', 'Mc',
'Lv', 'Ts', 'Og']
def build_crystal(crystal_str, niggli=True, primitive=False):
"""Build crystal from cif string."""
with warnings.catch_warnings():
warnings.simplefilter('ignore')
crystal = Structure.from_str(crystal_str, fmt='cif')
if primitive:
crystal = crystal.get_primitive_structure()
if niggli:
crystal = crystal.get_reduced_structure()
canonical_crystal = Structure(
lattice=Lattice.from_parameters(*crystal.lattice.parameters),
species=crystal.species,
coords=crystal.frac_coords,
coords_are_cartesian=False,
)
# match is gaurantteed because cif only uses lattice params & frac_coords
# assert canonical_crystal.matches(crystal)
return canonical_crystal
def refine_spacegroup(crystal, tol=0.01):
spga = SpacegroupAnalyzer(crystal, symprec=tol)
crystal = spga.get_conventional_standard_structure()
space_group = spga.get_space_group_number()
crystal = Structure(
lattice=Lattice.from_parameters(*crystal.lattice.parameters),
species=crystal.species,
coords=crystal.frac_coords,
coords_are_cartesian=False,
)
return crystal, space_group
def find_symm_map(frac_coords, general_wyckoff_ops):
frac_coords = frac_coords % 1
transformed = np.einsum('pij,nj->pni', general_wyckoff_ops[:, :3, :3], frac_coords)
transformed += general_wyckoff_ops[:, :3, 3][:, None, :]
diff = frac_coords[None, None, :, :] - transformed[:, :, None, :]
diff -= np.round(diff)
norms = np.linalg.norm(diff, axis=-1)
symm_map = np.argmin(norms, axis=-1)
return symm_map
def get_symmetry_info(crystal, tol=0.01):
spga = SpacegroupAnalyzer(crystal, symprec=tol)
crystal = spga.get_refined_structure()
c = pyxtal()
try:
c.from_seed(crystal, tol=0.01)
except:
c.from_seed(crystal, tol=0.0001)
space_group = c.group.number
species = []
anchors = []
matrices = []
coords = []
for site in c.atom_sites:
specie = site.specie
anchor = len(matrices)
coord = site.position
for syms in site.wp:
species.append(specie)
matrices.append(syms.affine_matrix)
coords.append(syms.operate(coord))
anchors.append(anchor)
anchors = np.array(anchors)
matrices = np.array(matrices)
coords = np.array(coords) % 1.
general_wyckoff_ops = np.array([op.affine_matrix for op in c.group[0].ops])
symm_map = find_symm_map(coords, general_wyckoff_ops)
sym_info = {
'anchors': anchors,
'wyckoff_ops': matrices,
'spacegroup': space_group,
'general_wyckoff_ops': general_wyckoff_ops,
'symm_map': symm_map,
}
crystal = Structure(
lattice=Lattice.from_parameters(*np.array(c.lattice.get_para(degree=True))),
species=species,
coords=coords,
coords_are_cartesian=False,
)
return crystal, sym_info
def build_crystal_graph(crystal, graph_method='crystalnn'):
"""
"""
cell = crystal.lattice.matrix
frac_coords = crystal.frac_coords
atom_types = crystal.atomic_numbers
lattice_parameters = crystal.lattice.parameters
lengths = lattice_parameters[:3]
angles = lattice_parameters[3:]
assert np.allclose(crystal.lattice.matrix, lattice_params_to_matrix(*lengths, *angles))
atom_types = np.array(atom_types)
lengths, angles = np.array(lengths), np.array(angles)
num_atoms = atom_types.shape[0]
return {
"atom_types": atom_types,
"frac_coords": frac_coords,
"cell": cell,
"lengths": lengths,
"angles": angles,
"num_atoms": num_atoms,
}
def abs_cap(val, max_abs_val=1):
"""
Returns the value with its absolute value capped at max_abs_val.
Particularly useful in passing values to trignometric functions where
numerical errors may result in an argument > 1 being passed in.
https://github.com/materialsproject/pymatgen/blob/b789d74639aa851d7e5ee427a765d9fd5a8d1079/pymatgen/util/num.py#L15
Args:
val (float): Input value.
max_abs_val (float): The maximum absolute value for val. Defaults to 1.
Returns:
val if abs(val) < 1 else sign of val * max_abs_val.
"""
return max(min(val, max_abs_val), -max_abs_val)
def lattice_params_to_matrix(a, b, c, alpha, beta, gamma):
"""Converts lattice from abc, angles to matrix.
https://github.com/materialsproject/pymatgen/blob/b789d74639aa851d7e5ee427a765d9fd5a8d1079/pymatgen/core/lattice.py#L311
"""
angles_r = np.radians([alpha, beta, gamma])
cos_alpha, cos_beta, cos_gamma = np.cos(angles_r)
sin_alpha, sin_beta, sin_gamma = np.sin(angles_r)
val = (cos_alpha * cos_beta - cos_gamma) / (sin_alpha * sin_beta)
# Sometimes rounding errors result in values slightly > 1.
val = abs_cap(val)
gamma_star = np.arccos(val)
vector_a = [a * sin_beta, 0.0, a * cos_beta]
vector_b = [
-b * sin_alpha * np.cos(gamma_star),
b * sin_alpha * np.sin(gamma_star),
b * cos_alpha,
]
vector_c = [0.0, 0.0, float(c)]
return np.array([vector_a, vector_b, vector_c])
def lattice_params_to_matrix_torch(lengths, angles):
"""Batched torch version to compute lattice matrix from params.
lengths: torch.Tensor of shape (N, 3), unit A
angles: torch.Tensor of shape (N, 3), unit degree
"""
angles_r = torch.deg2rad(angles)
coses = torch.cos(angles_r)
sins = torch.sin(angles_r)
val = (coses[:, 0] * coses[:, 1] - coses[:, 2]) / (sins[:, 0] * sins[:, 1])
# Sometimes rounding errors result in values slightly > 1.
val = torch.clamp(val, -1., 1.)
gamma_star = torch.arccos(val)
vector_a = torch.stack([
lengths[:, 0] * sins[:, 1],
torch.zeros(lengths.size(0), device=lengths.device),
lengths[:, 0] * coses[:, 1]], dim=1)
vector_b = torch.stack([
-lengths[:, 1] * sins[:, 0] * torch.cos(gamma_star),
lengths[:, 1] * sins[:, 0] * torch.sin(gamma_star),
lengths[:, 1] * coses[:, 0]], dim=1)
vector_c = torch.stack([
torch.zeros(lengths.size(0), device=lengths.device),
torch.zeros(lengths.size(0), device=lengths.device),
lengths[:, 2]], dim=1)
return torch.stack([vector_a, vector_b, vector_c], dim=1)
def compute_volume(batch_lattice):
"""Compute volume from batched lattice matrix
batch_lattice: (N, 3, 3)
"""
vector_a, vector_b, vector_c = torch.unbind(batch_lattice, dim=1)
return torch.abs(torch.einsum('bi,bi->b', vector_a,
torch.cross(vector_b, vector_c, dim=1)))
def lengths_angles_to_volume(lengths, angles):
lattice = lattice_params_to_matrix_torch(lengths, angles)
return compute_volume(lattice)
def lattice_matrix_to_params(matrix):
lengths = np.sqrt(np.sum(matrix ** 2, axis=1)).tolist()
angles = np.zeros(3)
for i in range(3):
j = (i + 1) % 3
k = (i + 2) % 3
angles[i] = abs_cap(np.dot(matrix[j], matrix[k]) /
(lengths[j] * lengths[k]))
angles = np.arccos(angles) * 180.0 / np.pi
a, b, c = lengths
alpha, beta, gamma = angles
return a, b, c, alpha, beta, gamma
@torch.no_grad()
def lattice_matrix_to_params_torch(batch_lattice):
lengths = torch.sqrt(torch.sum(batch_lattice ** 2, dim=1))
raise NotImplementedError()
# k1 k2 k3 k4 k5 k6
# [[0, 1, 0], [[0, 0, 1], [[0, 0, 0], [[1, 0, 0], [[1, 0, 0], [[1, 0, 0],
# [1, 0, 0], [0, 0, 0], [0, 0, 1], [0,-1, 0], [0, 1, 0], [0, 1, 0],
# [0, 0, 0]] [1, 0, 0]] [0, 1, 0]] [0, 0, 0]] [0, 0,-2]] [0, 0, 1]]
def lattice_polar_decompose(lattice: np.ndarray):
assert lattice.ndim == 2
A, U = np.linalg.eigh(lattice @ lattice.T)
A, U = np.real(A), np.real(U)
A = np.diag(np.log(A)) / 2
S = U @ A @ U.T
k = np.array(
[
S[0, 1],
S[0, 2],
S[1, 2],
(S[0, 0] - S[1, 1]) / 2,
(S[0, 0] + S[1, 1] - 2 * S[2, 2]) / 6,
(S[0, 0] + S[1, 1] + S[2, 2]) / 3,
]
)
return k
def lattice_polar_build(k: np.ndarray):
assert k.ndim == 1
S = np.array(
[
[k[3] + k[4] + k[5], k[0], k[1]],
[k[0], -k[3] + k[4] + k[5], k[2]],
[k[1], k[2], -2 * k[4] + k[5]],
]
) # (3, 3)
expS = scipy.linalg.expm(S) # (3, 3)
return expS
def decompose_symmetric_matrix(S: torch.Tensor):
k0 = S[:, 0, 1]
k1 = S[:, 0, 2]
k2 = S[:, 1, 2]
k3 = (S[:, 0, 0] - S[:, 1, 1]) / 2
k4 = (S[:, 0, 0] + S[:, 1, 1] - 2 * S[:, 2, 2]) / 6
k5 = (S[:, 0, 0] + S[:, 1, 1] + S[:, 2, 2]) / 3
k = torch.vstack([k0, k1, k2, k3, k4, k5]).transpose(-1, -2)
return k
@torch.no_grad()
def lattice_polar_decompose_torch(lattices: torch.Tensor):
assert lattices.dim() == 3, "input must be batched lattices of shape (B,3,3)"
A, U = torch.linalg.eigh(lattices @ lattices.transpose(-1,-2)) # J = L^T @ L
# S = 1/2 U log(A) U^T
A = torch.diag_embed(A.log()) / 2
S = U @ A @ U.transpose(-1, -2)
k = decompose_symmetric_matrix(S)
return k
@torch.no_grad()
def lattice_polar_build_torch(k):
assert k.dim() == 2, "input must be batched k of shape (B,6)"
S0 = torch.stack([k[:, 3] + k[:, 4] + k[:, 5], k[:, 0], k[:, 1]], dim=1) # (B, 3)
S1 = torch.stack([k[:, 0], -k[:, 3] + k[:, 4] + k[:, 5], k[:, 2]], dim=1) # (B, 3)
S2 = torch.stack([k[:, 1], k[:, 2], -2 * k[:, 4] + k[:, 5]], dim=1) # (B, 3)
S = torch.stack([S0, S1, S2], dim=1) # (B, 3, 3)
expS = torch.matrix_exp(S) # (B, 3, 3)
return expS
def get_reciprocal_lattice_torch(L):
V = torch.det(L)[:, None] + 1e-9 # (B, 1)
a0 = L[:, 0, :] # (B, 3)
a1 = L[:, 1, :]
a2 = L[:, 2, :]
cross_a12 = torch.cross(a1, a2, dim=1) # (B, 3)
cross_a20 = torch.cross(a2, a0, dim=1)
cross_a01 = torch.cross(a0, a1, dim=1)
b0 = (2 * math.pi * cross_a12 / V)[:, None, :] # (B, 1, 3)
b1 = (2 * math.pi * cross_a20 / V)[:, None, :] # (B, 1, 3)
b2 = (2 * math.pi * cross_a01 / V)[:, None, :] # (B, 1, 3)
b = torch.cat([b0, b1, b2], dim=1) # (B, 3, 3)
return b
def lattices_to_params_shape(lattices):
lengths = torch.sqrt(torch.sum(lattices ** 2, dim=-1))
angles = torch.zeros_like(lengths)
for i in range(3):
j = (i + 1) % 3
k = (i + 2) % 3
angles[...,i] = torch.clamp(torch.sum(lattices[...,j,:] * lattices[...,k,:], dim = -1) /
(lengths[...,j] * lengths[...,k]), -1., 1.)
angles = torch.arccos(angles) * 180.0 / np.pi
return lengths, angles
def frac_to_cart_coords(
frac_coords,
lengths,
angles,
num_atoms,
regularized = True,
lattices = None
):
if regularized:
frac_coords = frac_coords % 1.
if lattices is None:
lattices = lattice_params_to_matrix_torch(lengths, angles)
lattice_nodes = torch.repeat_interleave(lattices, num_atoms, dim=0)
pos = torch.einsum('bi,bij->bj', frac_coords, lattice_nodes) # cart coords
return pos
def cart_to_frac_coords(
cart_coords,
lengths,
angles,
num_atoms,
regularized = True
):
lattice = lattice_params_to_matrix_torch(lengths, angles)
# use pinv in case the predicted lattice is not rank 3
inv_lattice = torch.linalg.pinv(lattice)
inv_lattice_nodes = torch.repeat_interleave(inv_lattice, num_atoms, dim=0)
frac_coords = torch.einsum('bi,bij->bj', cart_coords, inv_lattice_nodes)
if regularized:
frac_coords = frac_coords % 1.
return frac_coords
def frac_to_cart_coords_with_lattice(
frac_coords: torch.Tensor, num_atoms: torch.Tensor, lattice: torch.Tensor
) -> torch.Tensor:
lattice_nodes = torch.repeat_interleave(lattice, num_atoms, dim=0)
pos = torch.einsum("bi,bij->bj", frac_coords, lattice_nodes) # cart coords
return pos
def array2tensor(array, dtype=torch.float):
if isinstance(array, torch.Tensor):
return array.clone().detach().to(dtype)
else:
return torch.tensor(array, dtype=dtype)
class StandardScalerTorch(object):
"""Normalizes the targets of a dataset."""
def __init__(self, means=None, stds=None):
self.means = means
self.stds = stds
def fit(self, X):
X = array2tensor(X, dtype=torch.float)
self.means = torch.mean(X, dim=0)
# https://github.com/pytorch/pytorch/issues/29372
self.stds = torch.std(X, dim=0, unbiased=False) + EPSILON
def transform(self, X):
X = array2tensor(X, dtype=torch.float)
return (X - self.means) / self.stds
def inverse_transform(self, X):
X = array2tensor(X, dtype=torch.float)
return X * self.stds + self.means
def match_device(self, tensor):
if self.means.device != tensor.device:
self.means = self.means.to(tensor.device)
self.stds = self.stds.to(tensor.device)
def copy(self):
return StandardScalerTorch(
means=self.means.clone().detach(),
stds=self.stds.clone().detach())
def __repr__(self) -> str:
return (
f"{self.__class__.__name__}("
f"means: {self.means.tolist()}, "
f"stds: {self.stds.tolist()})"
)
def get_scaler_from_data_list(data_list, key):
targets = torch.tensor(np.array([d[key] for d in data_list]))
scaler = StandardScalerTorch()
scaler.fit(targets)
return scaler
def parse_prop(item):
if isinstance(item, (float, int, np.generic)):
return item
elif isinstance(item, str):
try:
loadeditem = json.loads(item)
except Exception as e:
pass
return loadeditem
else:
raise ValueError(f"Parse prop failed: {item}")
def process_one(row, niggli, primitive, graph_method, prop_list, use_space_group = False, tol=0.01):
crystal_str = row['cif']
crystal = build_crystal(
crystal_str, niggli=niggli, primitive=primitive)
result_dict = {}
if use_space_group:
crystal, sym_info = get_symmetry_info(crystal, tol = tol)
result_dict.update(sym_info)
else:
result_dict['spacegroup'] = 1
graph_arrays = build_crystal_graph(crystal, graph_method)
properties = {k: parse_prop(row[k]) for k in prop_list if k in row.keys()}
result_dict.update({
'mp_id': row['material_id'],
# 'cif': crystal_str,
'graph_arrays': graph_arrays
})
result_dict.update(properties)
return result_dict
def preprocess(
input_file,
num_workers,
niggli,
primitive,
graph_method,
prop_list,
use_space_group=False,
tol=0.01
):
print(f"Preprocessing {input_file}")
suffix = Path(input_file).suffix
if suffix == ".csv":
df = pd.read_csv(input_file)
elif suffix == ".feather":
df = pd.read_feather(input_file)
else:
raise ValueError(f"Unknown format of file: {input_file}")
unordered_results = p_umap(
process_one,
[df.iloc[idx] for idx in range(len(df))],
[niggli] * len(df),
[primitive] * len(df),
[graph_method] * len(df),
[prop_list] * len(df),
[use_space_group] * len(df),
[tol] * len(df),
num_cpus=num_workers)
mpid_to_results = {result['mp_id']: result for result in unordered_results}
ordered_results = [mpid_to_results[df.iloc[idx]['material_id']]
for idx in range(len(df))]
return ordered_results
def preprocess_tensors(crystal_array_list, niggli, primitive, graph_method):
def process_one(batch_idx, crystal_array, niggli, primitive, graph_method):
frac_coords = crystal_array['frac_coords']
atom_types = crystal_array['atom_types']
lengths = crystal_array['lengths']
angles = crystal_array['angles']
crystal = Structure(
lattice=Lattice.from_parameters(
*(lengths.tolist() + angles.tolist())),
species=atom_types,
coords=frac_coords,
coords_are_cartesian=False)
graph_arrays = build_crystal_graph(crystal, graph_method)
result_dict = {
'batch_idx': batch_idx,
'graph_arrays': graph_arrays,
}
return result_dict
unordered_results = p_umap(
process_one,
list(range(len(crystal_array_list))),
crystal_array_list,
[niggli] * len(crystal_array_list),
[primitive] * len(crystal_array_list),
[graph_method] * len(crystal_array_list),
num_cpus=30,
)
ordered_results = list(
sorted(unordered_results, key=lambda x: x['batch_idx']))
return ordered_results
def add_scaled_lattice_prop(data_list, lattice_scale_method):
for dict in data_list:
graph_arrays = dict['graph_arrays']
# the indexes are brittle if more objects are returned
lengths = graph_arrays[2]
angles = graph_arrays[3]
num_atoms = graph_arrays[6]
assert lengths.shape[0] == angles.shape[0] == 3
assert isinstance(num_atoms, int)
if lattice_scale_method == 'scale_length':
lengths = lengths / float(num_atoms)**(1/3)
dict['scaled_lattice'] = np.concatenate([lengths, angles])
def mard(targets, preds):
"""Mean absolute relative difference."""
assert torch.all(targets > 0.)
return torch.mean(torch.abs(targets - preds) / targets)
def batch_accuracy_precision_recall(
pred_edge_probs,
edge_overlap_mask,
num_bonds
):
if (pred_edge_probs is None and edge_overlap_mask is None and
num_bonds is None):
return 0., 0., 0.
pred_edges = pred_edge_probs.max(dim=1)[1].float()
target_edges = edge_overlap_mask.float()
start_idx = 0
accuracies, precisions, recalls = [], [], []
for num_bond in num_bonds.tolist():
pred_edge = pred_edges.narrow(
0, start_idx, num_bond).detach().cpu().numpy()
target_edge = target_edges.narrow(
0, start_idx, num_bond).detach().cpu().numpy()
accuracies.append(accuracy_score(target_edge, pred_edge))
precisions.append(precision_score(
target_edge, pred_edge, average='binary'))
recalls.append(recall_score(target_edge, pred_edge, average='binary'))
start_idx = start_idx + num_bond
return np.mean(accuracies), np.mean(precisions), np.mean(recalls)
class StandardScaler:
"""A :class:`StandardScaler` normalizes the features of a dataset.
When it is fit on a dataset, the :class:`StandardScaler` learns the
mean and standard deviation across the 0th axis.
When transforming a dataset, the :class:`StandardScaler` subtracts the
means and divides by the standard deviations.
"""
def __init__(self, means=None, stds=None, replace_nan_token=None):
"""
:param means: An optional 1D numpy array of precomputed means.
:param stds: An optional 1D numpy array of precomputed standard deviations.
:param replace_nan_token: A token to use to replace NaN entries in the features.
"""
self.means = means
self.stds = stds
self.replace_nan_token = replace_nan_token
def fit(self, X):
"""
Learns means and standard deviations across the 0th axis of the data :code:`X`.
:param X: A list of lists of floats (or None).
:return: The fitted :class:`StandardScaler` (self).
"""
X = np.array(X).astype(float)
self.means = np.nanmean(X, axis=0)
self.stds = np.nanstd(X, axis=0)
self.means = np.where(np.isnan(self.means),
np.zeros(self.means.shape), self.means)
self.stds = np.where(np.isnan(self.stds),
np.ones(self.stds.shape), self.stds)
self.stds = np.where(self.stds == 0, np.ones(
self.stds.shape), self.stds)
return self
def transform(self, X):
"""
Transforms the data by subtracting the means and dividing by the standard deviations.
:param X: A list of lists of floats (or None).
:return: The transformed data with NaNs replaced by :code:`self.replace_nan_token`.
"""
X = np.array(X).astype(float)
transformed_with_nan = (X - self.means) / self.stds
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none
def inverse_transform(self, X):
"""
Performs the inverse transformation by multiplying by the standard deviations and adding the means.
:param X: A list of lists of floats.
:return: The inverse transformed data with NaNs replaced by :code:`self.replace_nan_token`.
"""
X = np.array(X).astype(float)
transformed_with_nan = X * self.stds + self.means
transformed_with_none = np.where(
np.isnan(transformed_with_nan), self.replace_nan_token, transformed_with_nan)
return transformed_with_none