Skip to content

Commit ace87ed

Browse files
authored
Types and format (#3)
Added monkeytype types and applied formatting tules
1 parent 6b2c2df commit ace87ed

File tree

15 files changed

+1128
-982
lines changed

15 files changed

+1128
-982
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ python=./env/bin/python
22
mamba=mamba
33
pkg=qmllib
44
pip=./env/bin/pip
5+
pytest=pytest
56
j=1
67

78
.PHONY: build
@@ -24,7 +25,7 @@ test:
2425
${python} -m pytest -rs ./tests
2526

2627
types:
27-
${python} -m monkeytype run $(which pytest) ./tests/
28+
${python} -m monkeytype run $$(which ${pytest}) ./tests
2829
${python} -m monkeytype list-modules | grep ${pkg} | parallel -j${j} "${python} -m monkeytype apply {}"
2930

3031
cov:

src/qmllib/kernels/distance.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from typing import Union
2+
13
import numpy as np
4+
from numpy import ndarray
25

36
from .fdistance import fl2_distance, fmanhattan_distance, fp_distance_double, fp_distance_integer
47

58

6-
def manhattan_distance(A, B):
9+
def manhattan_distance(A: ndarray, B: ndarray) -> ndarray:
710
"""Calculates the Manhattan distances, D, between two
811
Numpy arrays of representations.
912
@@ -37,7 +40,7 @@ def manhattan_distance(A, B):
3740
return D
3841

3942

40-
def l2_distance(A, B):
43+
def l2_distance(A: ndarray, B: ndarray) -> ndarray:
4144
"""Calculates the L2 distances, D, between two
4245
Numpy arrays of representations.
4346
@@ -71,7 +74,7 @@ def l2_distance(A, B):
7174
return D
7275

7376

74-
def p_distance(A, B, p=2):
77+
def p_distance(A: ndarray, B: ndarray, p: Union[int, float] = 2) -> ndarray:
7578
"""Calculates the p-norm distances between two
7679
Numpy arrays of representations.
7780
The value of the keyword argument ``p =`` sets the norm order.

src/qmllib/kernels/gradient_kernels.py

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import List, Union
2+
13
import numpy as np
4+
from numpy import ndarray
25

36
from qmllib.utils.environment_manipulation import (
47
mkl_get_num_threads,
@@ -22,7 +25,9 @@
2225
)
2326

2427

25-
def get_global_kernel(X1, X2, Q1, Q2, SIGMA):
28+
def get_global_kernel(
29+
X1: ndarray, X2: ndarray, Q1: List[List[int]], Q2: List[List[int]], SIGMA: float
30+
) -> ndarray:
2631
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
2732
2833
:math:`K_{ij} = \\sum_{I\\in i} \\sum_{J\\in j}\\exp \\big( -\\frac{\\|X_I - X_J\\|_2^2}{2\\sigma^2} \\big)`
@@ -75,7 +80,9 @@ def get_global_kernel(X1, X2, Q1, Q2, SIGMA):
7580
return K
7681

7782

78-
def get_local_kernels(X1, X2, Q1, Q2, SIGMAS):
83+
def get_local_kernels(
84+
X1: ndarray, X2: ndarray, Q1: List[List[int]], Q2: List[List[int]], SIGMAS: List[float]
85+
) -> ndarray:
7986
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
8087
8188
:math:`K_{ij} = \\sum_{I\\in i} \\sum_{J\\in j}\\exp \\big( -\\frac{\\|X_I - X_J\\|_2^2}{2\\sigma^2} \\big)`
@@ -131,7 +138,13 @@ def get_local_kernels(X1, X2, Q1, Q2, SIGMAS):
131138
return K
132139

133140

134-
def get_local_kernel(X1, X2, Q1, Q2, SIGMA):
141+
def get_local_kernel(
142+
X1: ndarray,
143+
X2: ndarray,
144+
Q1: List[Union[ndarray, List[int]]],
145+
Q2: List[Union[ndarray, List[int]]],
146+
SIGMA: float,
147+
) -> ndarray:
135148
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
136149
137150
:math:`K_{ij} = \\sum_{I\\in i} \\sum_{J\\in j}\\exp \\big( -\\frac{\\|X_I - X_J\\|_2^2}{2\\sigma^2} \\big)`
@@ -184,7 +197,7 @@ def get_local_kernel(X1, X2, Q1, Q2, SIGMA):
184197
return K
185198

186199

187-
def get_local_symmetric_kernels(X1, Q1, SIGMAS):
200+
def get_local_symmetric_kernels(X1: ndarray, Q1: List[List[int]], SIGMAS: List[float]) -> ndarray:
188201
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
189202
190203
:math:`K_{ij} = \\sum_{I\\in i} \\sum_{J\\in j}\\exp \\big( -\\frac{\\|X_I - X_J\\|_2^2}{2\\sigma^2} \\big)`
@@ -229,7 +242,9 @@ def get_local_symmetric_kernels(X1, Q1, SIGMAS):
229242
return K
230243

231244

232-
def get_local_symmetric_kernel(X1, Q1, SIGMA):
245+
def get_local_symmetric_kernel(
246+
X1: ndarray, Q1: List[Union[ndarray, List[int]]], SIGMA: float
247+
) -> ndarray:
233248
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
234249
235250
:math:`K_{ij} = \\sum_{I\\in i} \\sum_{J\\in j}\\exp \\big( -\\frac{\\|X_I - X_J\\|_2^2}{2\\sigma^2} \\big)`
@@ -273,7 +288,13 @@ def get_local_symmetric_kernel(X1, Q1, SIGMA):
273288
return K
274289

275290

276-
def get_atomic_local_kernel(X1, X2, Q1, Q2, SIGMA):
291+
def get_atomic_local_kernel(
292+
X1: ndarray,
293+
X2: ndarray,
294+
Q1: List[Union[ndarray, List[int]]],
295+
Q2: List[Union[ndarray, List[int]]],
296+
SIGMA: float,
297+
) -> ndarray:
277298

278299
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
279300
@@ -332,7 +353,14 @@ def get_atomic_local_kernel(X1, X2, Q1, Q2, SIGMA):
332353
return K
333354

334355

335-
def get_atomic_local_gradient_kernel(X1, X2, dX2, Q1, Q2, SIGMA):
356+
def get_atomic_local_gradient_kernel(
357+
X1: ndarray,
358+
X2: ndarray,
359+
dX2: ndarray,
360+
Q1: List[Union[ndarray, List[int]]],
361+
Q2: List[Union[ndarray, List[int]]],
362+
SIGMA: float,
363+
) -> ndarray:
336364

337365
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
338366
@@ -412,7 +440,9 @@ def get_atomic_local_gradient_kernel(X1, X2, dX2, Q1, Q2, SIGMA):
412440
return K
413441

414442

415-
def get_local_gradient_kernel(X1, X2, dX2, Q1, Q2, SIGMA):
443+
def get_local_gradient_kernel(
444+
X1: ndarray, X2: ndarray, dX2: ndarray, Q1: List[List[int]], Q2: List[List[int]], SIGMA: float
445+
) -> ndarray:
416446

417447
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
418448
@@ -478,7 +508,15 @@ def get_local_gradient_kernel(X1, X2, dX2, Q1, Q2, SIGMA):
478508
return K
479509

480510

481-
def get_gdml_kernel(X1, X2, dX1, dX2, Q1, Q2, SIGMA):
511+
def get_gdml_kernel(
512+
X1: ndarray,
513+
X2: ndarray,
514+
dX1: ndarray,
515+
dX2: ndarray,
516+
Q1: List[List[int]],
517+
Q2: List[List[int]],
518+
SIGMA: float,
519+
) -> ndarray:
482520

483521
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
484522
@@ -560,7 +598,9 @@ def get_gdml_kernel(X1, X2, dX1, dX2, Q1, Q2, SIGMA):
560598
return K
561599

562600

563-
def get_symmetric_gdml_kernel(X1, dX1, Q1, SIGMA):
601+
def get_symmetric_gdml_kernel(
602+
X1: ndarray, dX1: ndarray, Q1: List[List[int]], SIGMA: float
603+
) -> ndarray:
564604

565605
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
566606
@@ -613,7 +653,15 @@ def get_symmetric_gdml_kernel(X1, dX1, Q1, SIGMA):
613653
return K
614654

615655

616-
def get_gp_kernel(X1, X2, dX1, dX2, Q1, Q2, SIGMA):
656+
def get_gp_kernel(
657+
X1: ndarray,
658+
X2: ndarray,
659+
dX1: ndarray,
660+
dX2: ndarray,
661+
Q1: List[Union[ndarray, List[int]]],
662+
Q2: List[Union[ndarray, List[int]]],
663+
SIGMA: float,
664+
) -> ndarray:
617665

618666
"""Calculates the Gaussian kernel matrix K with the local decomposition where :math:`K_{ij}`:
619667
@@ -692,7 +740,9 @@ def get_gp_kernel(X1, X2, dX1, dX2, Q1, Q2, SIGMA):
692740
return K
693741

694742

695-
def get_symmetric_gp_kernel(X1, dX1, Q1, SIGMA):
743+
def get_symmetric_gp_kernel(
744+
X1: ndarray, dX1: ndarray, Q1: List[Union[ndarray, List[int]]], SIGMA: float
745+
) -> ndarray:
696746

697747
"""
698748
This symmetric kernel corresponds to a Gaussian process regression (GPR) approach.

src/qmllib/kernels/kernels.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import List, Union
2+
13
import numpy as np
4+
from numpy import float64, ndarray
25

36
from .fkernels import (
47
fgaussian_kernel,
@@ -15,7 +18,7 @@
1518
)
1619

1720

18-
def wasserstein_kernel(A, B, sigma, p=1, q=1):
21+
def wasserstein_kernel(A: ndarray, B: ndarray, sigma: float, p: int = 1, q: int = 1) -> ndarray:
1922
"""Calculates the Wasserstein kernel matrix K, where :math:`K_{ij}`:
2023
2124
:math:`K_{ij} = \\exp \\big( -\\frac{(W_p(A_i, B_i))^q}{\\sigma} \\big)`
@@ -45,7 +48,7 @@ def wasserstein_kernel(A, B, sigma, p=1, q=1):
4548
return K
4649

4750

48-
def laplacian_kernel(A, B, sigma):
51+
def laplacian_kernel(A: ndarray, B: ndarray, sigma: float) -> ndarray:
4952
"""Calculates the Laplacian kernel matrix K, where :math:`K_{ij}`:
5053
5154
:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - B_j\\|_1}{\\sigma} \\big)`
@@ -75,7 +78,7 @@ def laplacian_kernel(A, B, sigma):
7578
return K
7679

7780

78-
def laplacian_kernel_symmetric(A, sigma):
81+
def laplacian_kernel_symmetric(A: ndarray, sigma: float) -> ndarray:
7982
"""Calculates the symmetric Laplacian kernel matrix K, where :math:`K_{ij}`:
8083
8184
:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - A_j\\|_1}{\\sigma} \\big)`
@@ -102,7 +105,7 @@ def laplacian_kernel_symmetric(A, sigma):
102105
return K
103106

104107

105-
def gaussian_kernel(A, B, sigma):
108+
def gaussian_kernel(A: ndarray, B: ndarray, sigma: float) -> ndarray:
106109
"""Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
107110
108111
:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - B_j\\|_2^2}{2\\sigma^2} \\big)`
@@ -132,7 +135,7 @@ def gaussian_kernel(A, B, sigma):
132135
return K
133136

134137

135-
def gaussian_kernel_symmetric(A, sigma):
138+
def gaussian_kernel_symmetric(A: ndarray, sigma: float) -> ndarray:
136139
"""Calculates the symmetric Gaussian kernel matrix K, where :math:`K_{ij}`:
137140
138141
:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - A_j\\|_2^2}{2\\sigma^2} \\big)`
@@ -159,7 +162,7 @@ def gaussian_kernel_symmetric(A, sigma):
159162
return K
160163

161164

162-
def linear_kernel(A, B):
165+
def linear_kernel(A: ndarray, B: ndarray) -> ndarray:
163166
"""Calculates the linear kernel matrix K, where :math:`K_{ij}`:
164167
165168
:math:`K_{ij} = A_i \\cdot B_j`
@@ -188,7 +191,12 @@ def linear_kernel(A, B):
188191
return K
189192

190193

191-
def sargan_kernel(A, B, sigma, gammas):
194+
def sargan_kernel(
195+
A: ndarray,
196+
B: ndarray,
197+
sigma: Union[float, float64],
198+
gammas: Union[ndarray, List[Union[int, float]], List[int]],
199+
) -> ndarray:
192200
"""Calculates the Sargan kernel matrix K, where :math:`K_{ij}`:
193201
194202
:math:`K_{ij} = \\exp \\big( -\\frac{\\| A_i - B_j \\|_1)}{\\sigma} \\big) \\big(1 + \\sum_{k} \\frac{\\gamma_{k} \\| A_i - B_j \\|_1^k}{\\sigma^k} \\big)`
@@ -225,7 +233,9 @@ def sargan_kernel(A, B, sigma, gammas):
225233
return K
226234

227235

228-
def matern_kernel(A, B, sigma, order=0, metric="l1"):
236+
def matern_kernel(
237+
A: ndarray, B: ndarray, sigma: float, order: int = 0, metric: str = "l1"
238+
) -> ndarray:
229239
"""Calculates the Matern kernel matrix K, where :math:`K_{ij}`:
230240
231241
for order = 0:
@@ -286,7 +296,9 @@ def matern_kernel(A, B, sigma, order=0, metric="l1"):
286296
return K
287297

288298

289-
def get_local_kernels_gaussian(A, B, na, nb, sigmas):
299+
def get_local_kernels_gaussian(
300+
A: ndarray, B: ndarray, na: ndarray, nb: ndarray, sigmas: List[float]
301+
) -> ndarray:
290302
"""Calculates the Gaussian kernel matrix K, for a local representation where :math:`K_{ij}`:
291303
292304
:math:`K_{ij} = \\sum_{a \\in i} \\sum_{b \\in j} \\exp \\big( -\\frac{\\|A_a - B_b\\|_2^2}{2\\sigma^2} \\big)`
@@ -328,7 +340,9 @@ def get_local_kernels_gaussian(A, B, na, nb, sigmas):
328340
return fget_local_kernels_gaussian(A.T, B.T, na, nb, sigmas, nma, nmb, nsigmas)
329341

330342

331-
def get_local_kernels_laplacian(A, B, na, nb, sigmas):
343+
def get_local_kernels_laplacian(
344+
A: ndarray, B: ndarray, na: ndarray, nb: ndarray, sigmas: List[float]
345+
) -> ndarray:
332346
"""Calculates the Local Laplacian kernel matrix K, for a local representation where :math:`K_{ij}`:
333347
334348
:math:`K_{ij} = \\sum_{a \\in i} \\sum_{b \\in j} \\exp \\big( -\\frac{\\|A_a - B_b\\|_1}{\\sigma} \\big)`
@@ -370,7 +384,7 @@ def get_local_kernels_laplacian(A, B, na, nb, sigmas):
370384
return fget_local_kernels_laplacian(A.T, B.T, na, nb, sigmas, nma, nmb, nsigmas)
371385

372386

373-
def kpca(K, n=2, centering=True):
387+
def kpca(K: ndarray, n: int = 2, centering: bool = True) -> ndarray:
374388
"""Calculates `n` first principal components for the kernel :math:`K`.
375389
376390
The PCA is calculated using an OpenMP parallel Fortran routine.

0 commit comments

Comments
 (0)