Skip to content

Commit 5947c80

Browse files
committed
Fixed kernel test
1 parent e1c7389 commit 5947c80

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

Makefile

+23-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ format:
2222

2323
test:
2424
${python} -m pytest -rs \
25-
./tests/test_kernels.py \
26-
./tests/test_solvers.py \
27-
./tests/test_distance.py \
28-
./tests/test_slatm.py
25+
tests/test_acsf_linear_angles.py \
26+
tests/test_acsf.py \
27+
tests/test_arad.py \
28+
tests/test_armp.py \
29+
tests/test_compound.py \
30+
tests/test_distance.py \
31+
tests/test_energy_krr_atomic_cmat.py \
32+
tests/test_energy_krr_bob.py \
33+
tests/test_energy_krr_cmat.py \
34+
tests/test_fchl_acsf_energy.py \
35+
tests/test_fchl_acsf_forces.py \
36+
tests/test_fchl_acsf.py \
37+
tests/test_fchl_electric_field.py \
38+
tests/test_fchl_force.py \
39+
tests/test_fchl_scalar.py \
40+
tests/test_kernel_derivatives.py \
41+
tests/test_kernels.py \
42+
tests/test_mrmp.py \
43+
tests/test_neural_network.py \
44+
tests/test_representations.py \
45+
tests/test_slatm.py \
46+
tests/test_solvers.py \
47+
tests/test_symm_funct.py
2948

3049
types:
3150
${python} -m monkeytype run $(which pytest) ./tests/

src/qmllib/representations/slatm.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from __future__ import print_function
2-
31
import itertools as itl
42

53
import numpy as np
6-
import scipy.spatial.distance as ssd
4+
import scipy.spatial.distance as spatial_distance
75

86
from .fslatm import fget_sbop, fget_sbop_local, fget_sbot, fget_sbot_local
97

@@ -16,7 +14,7 @@ def update_m(obj, ia, rcut=9.0, pbc=None):
1614

1715
zs, coords, c = obj
1816
v1, v2, v3 = c
19-
vs = ssd.norm(c, axis=0)
17+
vs = spatial_distance.norm(c, axis=0)
2018

2119
nns = []
2220
for i, vi in enumerate(vs):
@@ -68,7 +66,7 @@ def update_m(obj, ia, rcut=9.0, pbc=None):
6866
if na == 1:
6967
ds = np.array([[0.0]])
7068
else:
71-
ds = ssd.squareform(ssd.pdist(coords))
69+
ds = spatial_distance.squareform(spatial_distance.pdist(coords))
7270

7371
zs_u = []
7472
coords_u = []
@@ -86,7 +84,7 @@ def update_m(obj, ia, rcut=9.0, pbc=None):
8684
ts[iau] = np.dot(n123s[iau], c)
8785

8886
coords_iu = coords[i] + ts # np.dot(n123s, c)
89-
dsi = ssd.norm(coords_iu - cia, axis=1)
87+
dsi = spatial_distance.norm(coords_iu - cia, axis=1)
9088
filt = np.logical_and(dsi > 0, dsi <= rcut)
9189
nx = filt.sum()
9290
zs_u += [

tests/test_kernels.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
1+
from pathlib import Path
22

33
import numpy as np
4-
import pytest
4+
from conftest import ASSETS
55
from scipy.stats import wasserstein_distance
66
from sklearn.decomposition import KernelPCA
77

8-
import qmllib
98
from qmllib.kernels import (
109
gaussian_kernel,
1110
gaussian_kernel_symmetric,
@@ -17,14 +16,15 @@
1716
sargan_kernel,
1817
wasserstein_kernel,
1918
)
19+
from qmllib.representations import generate_bob
20+
from qmllib.utils.xyz_format import read_xyz
2021

2122

22-
def get_energies(filename):
23+
def get_energies(filename: Path):
2324
"""Returns a dictionary with heats of formation for each xyz-file."""
2425

25-
f = open(filename, "r")
26-
lines = f.readlines()
27-
f.close()
26+
with open(filename, "r") as f:
27+
lines = f.readlines()
2828

2929
energies = dict()
3030

@@ -239,16 +239,10 @@ def array_nan_close(a, b):
239239
return np.allclose(a[m], b[m], atol=1e-8, rtol=0.0)
240240

241241

242-
@pytest.mark.skip(reason="Removing all Compound classes")
243242
def test_kpca():
244243

245-
test_dir = os.path.dirname(os.path.realpath(__file__))
246-
247244
# Parse file containing PBE0/def2-TZVP heats of formation and xyz filenam
248-
data = get_energies(test_dir + "/data/hof_qm7.txt")
249-
250-
# Generate a list of qmllib.Compound() objects
251-
mols = []
245+
data = get_energies(ASSETS / "hof_qm7.txt")
252246

253247
keys = sorted(data.keys())
254248

@@ -257,17 +251,24 @@ def test_kpca():
257251

258252
n_mols = 100
259253

254+
representations = []
255+
260256
for xyz_file in keys[:n_mols]:
261257

262-
mol = qmllib.Compound(xyz=test_dir + "/qm7/" + xyz_file)
263-
mol.properties = data[xyz_file]
264-
mol.generate_bob()
265-
mols.append(mol)
258+
filename = ASSETS / "qm7" / xyz_file
259+
coordinates, atoms = read_xyz(filename)
266260

267-
X = np.array([mol.representation for mol in mols])
261+
atomtypes = np.unique(atoms)
262+
representation = generate_bob(atoms, coordinates, atomtypes)
263+
representations.append(representation)
264+
265+
X = np.array([representation for representation in representations])
268266
K = laplacian_kernel(X, X, 2e5)
269267

268+
# calculate pca
270269
pcas_qml = kpca(K, n=10)
270+
271+
# Calculate with sklearn
271272
pcas_sklearn = KernelPCA(10, eigen_solver="dense", kernel="precomputed").fit_transform(K)
272273

273274
assert array_nan_close(

0 commit comments

Comments
 (0)