Skip to content

Commit

Permalink
Merge branch 'charnley/consistency' into charnley/cites
Browse files Browse the repository at this point in the history
  • Loading branch information
charnley authored Nov 12, 2024
2 parents fbe6fbb + 694d15e commit e60ad37
Show file tree
Hide file tree
Showing 29 changed files with 241 additions and 1,203 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[report]
exclude_also =
def __repr__
raise ValueError
raise NotImplementedError
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ types:
${python} -m monkeytype list-modules | grep ${pkg} | parallel -j${j} "${python} -m monkeytype apply {} > /dev/null && echo {}"

cov:
${python} -m pytest -vrs --cov=${pkg} --cov-report html tests
${python} -m pytest --cov=${pkg} --cov-config .coveragerc --cov-report html tests

compile:
${python} _compile.py
Expand Down
1 change: 1 addition & 0 deletions environment_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- pre-commit
- pyarrow
- pytest
- pytest-cov
- scikit-learn
- scipy
# build
Expand Down
1 change: 0 additions & 1 deletion src/qmllib/kernels/fdistance.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

subroutine fmanhattan_distance(A, B, D)

implicit none
Expand Down
22 changes: 0 additions & 22 deletions src/qmllib/kernels/fgradient_kernels.f90
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@






















subroutine fglobal_kernel(x1, x2, q1, q2, n1, n2, nm1, nm2, sigma, kernel)

implicit none
Expand Down
1 change: 0 additions & 1 deletion src/qmllib/kernels/fkernels.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

subroutine fget_local_kernels_gaussian(q1, q2, n1, n2, sigmas, &
& nm1, nm2, nsigmas, kernels)

Expand Down
1 change: 0 additions & 1 deletion src/qmllib/kernels/fkpca.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

subroutine fkpca(k, n, centering, kpca)

implicit none
Expand Down
1 change: 0 additions & 1 deletion src/qmllib/kernels/fkwasserstein.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module searchtools

implicit none
Expand Down
100 changes: 40 additions & 60 deletions src/qmllib/kernels/gradient_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ def get_global_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("Error: List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -114,12 +112,10 @@ def get_local_kernels(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("Error: List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("Error: List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -176,12 +172,10 @@ def get_local_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -228,9 +222,8 @@ def get_local_symmetric_kernels(X1: ndarray, Q1: List[List[int]], SIGMAS: List[f

N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("Error: List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
for i, q in enumerate(Q1):
Expand Down Expand Up @@ -275,9 +268,8 @@ def get_local_symmetric_kernel(

N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("Error: List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
for i, q in enumerate(Q1):
Expand Down Expand Up @@ -329,12 +321,10 @@ def get_atomic_local_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -394,12 +384,10 @@ def get_atomic_local_gradient_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -475,12 +463,10 @@ def get_local_gradient_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -552,12 +538,10 @@ def get_gdml_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -627,9 +611,8 @@ def get_symmetric_gdml_kernel(

N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)

Expand Down Expand Up @@ -692,12 +675,10 @@ def get_gp_kernel(
N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)
N2 = np.array([len(Q) for Q in Q2], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
assert (
N2.shape[0] == X2.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")
if not (N2.shape[0] == X2.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)
Q2_input = np.zeros((max(N2), X2.shape[0]), dtype=np.int32)
Expand Down Expand Up @@ -765,9 +746,8 @@ def get_symmetric_gp_kernel(

N1 = np.array([len(Q) for Q in Q1], dtype=np.int32)

assert (
N1.shape[0] == X1.shape[0]
), "Error: List of charges does not match shape of representations"
if not (N1.shape[0] == X1.shape[0]):
raise ValueError("List of charges does not match shape of representations")

Q1_input = np.zeros((max(N1), X1.shape[0]), dtype=np.int32)

Expand Down
38 changes: 25 additions & 13 deletions src/qmllib/kernels/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,25 +265,28 @@ def matern_kernel(
"""

if metric == "l1":

if order == 0:
gammas = []

elif order == 1:
gammas = [1]
sigma /= np.sqrt(3)

elif order == 2:
gammas = [1, 1 / 3.0]
sigma /= np.sqrt(5)

else:
print("Order:%d not implemented in Matern Kernel" % order)
raise SystemExit
raise ValueError(f"Order '{order}' not implemented in Matern Kernel")

return sargan_kernel(A, B, sigma, gammas)

elif metric == "l2":
pass

else:
print("Error: Unknown distance metric %s in Matern kernel" % str(metric))
raise SystemExit
raise ValueError(f"Unknown distance metric {metric} in Matern kernel")

na = A.shape[0]
nb = B.shape[0]
Expand Down Expand Up @@ -326,10 +329,13 @@ def get_local_kernels_gaussian(
:rtype: numpy array
"""

assert np.sum(na) == A.shape[0], "Error in A input"
assert np.sum(nb) == B.shape[0], "Error in B input"
if np.sum(na) != A.shape[0]:
raise ValueError("Error in A input")
if np.sum(nb) != B.shape[0]:
raise ValueError("Error in B input")

assert A.shape[1] == B.shape[1], "Error in representation sizes"
if A.shape[1] != B.shape[1]:
raise ValueError("Error in representation sizes")

nma = len(na)
nmb = len(nb)
Expand Down Expand Up @@ -370,10 +376,13 @@ def get_local_kernels_laplacian(
:rtype: numpy array
"""

assert np.sum(na) == A.shape[0], "Error in A input"
assert np.sum(nb) == B.shape[0], "Error in B input"
if np.sum(na) != A.shape[0]:
raise ValueError("Error in A input")
if np.sum(nb) != B.shape[0]:
raise ValueError("Error in B input")

assert A.shape[1] == B.shape[1], "Error in representation sizes"
if A.shape[1] != B.shape[1]:
raise ValueError("Error in representation sizes")

nma = len(na)
nmb = len(nb)
Expand Down Expand Up @@ -403,9 +412,12 @@ def kpca(K: ndarray, n: int = 2, centering: bool = True) -> ndarray:
:rtype: numpy array
"""

assert K.shape[0] == K.shape[1], "ERROR: Square matrix required for Kernel PCA."
assert np.allclose(K, K.T, atol=1e-8), "ERROR: Symmetric matrix required for Kernel PCA."
assert n <= K.shape[0], "ERROR: Requested more principal components than matrix size."
if K.shape[0] != K.shape[1]:
raise ValueError("Square matrix required for Kernel PCA.")
if not np.allclose(K, K.T, atol=1e-8):
raise ValueError("Symmetric matrix required for Kernel PCA.")
if not n <= K.shape[0]:
raise ValueError("Requested more principal components than matrix size.")

size = K.shape[0]
pca = fkpca(K, size, centering)
Expand Down
Loading

0 comments on commit e60ad37

Please sign in to comment.