This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpython_utils_test.py
More file actions
133 lines (112 loc) · 4.21 KB
/
python_utils_test.py
File metadata and controls
133 lines (112 loc) · 4.21 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
from rascal.representations import (
SphericalExpansion,
SphericalInvariants,
)
from rascal.utils import (
get_radial_basis_covariance,
get_radial_basis_pca,
get_radial_basis_projections,
get_optimal_radial_basis_hypers,
json_dumps_frame,
)
from rascal.lib import neighbour_list
from rascal.neighbourlist import base
from test_utils import load_json_frame, BoxList, Box, dot
import tempfile
import unittest
import numpy as np
import sys
import os
import json
import tempfile
from copy import copy, deepcopy
from scipy.stats import ortho_group
import pickle
import ase.io
rascal_reference_path = "reference_data"
inputs_path = os.path.join(rascal_reference_path, "inputs")
dump_path = os.path.join(rascal_reference_path, "tests_only")
class TestOptimalRadialBasis(unittest.TestCase):
def setUp(self):
"""
builds the test case.
"""
fns = [
os.path.join(inputs_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
os.path.join(inputs_path, "SiC_moissanite_supercell.json"),
os.path.join(inputs_path, "methane.json"),
]
self.frames = [load_json_frame(fn) for fn in fns]
global_species = []
for frame in self.frames:
global_species.extend(frame["atom_types"])
self.global_species = list(np.unique(global_species))
self.hypers = dict(
interaction_cutoff=3.5,
max_radial=6,
max_angular=6,
gaussian_sigma_constant=0.4,
gaussian_sigma_type="Constant",
cutoff_smooth_width=0.5,
)
self.expanded_max_radial = 20
def test_hypers_construction(self):
"""Checks that manually-constructed and automatic
framework are consistent."""
hypers = deepcopy(self.hypers)
hypers["max_radial"] = self.expanded_max_radial
spex = SphericalExpansion(**hypers)
feats = spex.transform(self.frames).get_features_by_species(spex)
cov = get_radial_basis_covariance(spex, feats)
p_val, p_vec = get_radial_basis_pca(cov)
p_mat = get_radial_basis_projections(p_vec, self.hypers["max_radial"])
# now makes this SOAP
hypers["max_radial"] = self.hypers["max_radial"]
hypers["soap_type"] = "PowerSpectrum"
hypers["optimization"] = {
"RadialDimReduction": {"projection_matrices": p_mat},
"Spline": {"accuracy": 1e-8},
}
# compute SOAP
soap_opt = SphericalInvariants(**hypers)
soap_feats = soap_opt.transform(self.frames).get_features(soap_opt)
# now we do the same with the compact utils
hypers = deepcopy(self.hypers)
hypers["soap_type"] = "PowerSpectrum"
hypers = get_optimal_radial_basis_hypers(
hypers, self.frames, expanded_max_radial=self.expanded_max_radial
)
soap_opt_2 = SphericalInvariants(**hypers)
soap_feats_2 = soap_opt_2.transform(self.frames).get_features(soap_opt_2)
self.assertTrue(np.allclose(soap_feats, soap_feats_2))
class TestIO(unittest.TestCase):
def setUp(self):
self.fns = [
os.path.join(inputs_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
os.path.join(inputs_path, "SiC_moissanite_supercell.json"),
os.path.join(inputs_path, "methane.json"),
]
def test_json_dumps_frame(self):
"""
Checks if json file decoded by RascalEncoder in dumps_frame can be read
by rascal
"""
nl_options = [
dict(name="centers", args=dict()),
dict(name="neighbourlist", args=dict(cutoff=3)),
dict(name="centercontribution", args=dict()),
dict(name="strict", args=dict(cutoff=3)),
]
managers = base.StructureCollectionFactory(nl_options)
for fn in self.fns:
frame = ase.io.read(fn)
dumped_json = json_dumps_frame(frame)
tmp = tempfile.NamedTemporaryFile("w", suffix=".json", delete=False)
tmp.write(dumped_json)
try:
managers.add_structures(tmp.name)
tmp.close()
os.unlink(tmp.name)
except:
tmp.close()
os.unlink(tmp.name)