Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit 4cb16f6

Browse files
committed
Reformatting with black
Updating scripts and formatting
1 parent c10707e commit 4cb16f6

28 files changed

Lines changed: 507 additions & 4058 deletions

notebooks/1_LinearMethods.ipynb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
" table_from_dict,\n",
4444
" check_mirrors,\n",
4545
")\n",
46-
"from skcosmo.preprocessing import KernelFlexibleCenterer as KFC\n",
46+
"from skcosmo.preprocessing import KernelNormalizer as KFC\n",
4747
"\n",
4848
"cmaps = get_cmaps()\n",
4949
"plt.style.use(\"../utilities/kernel_pcovr.mplstyle\")\n",
@@ -682,7 +682,7 @@
682682
"plt.xlabel(\"n\")\n",
683683
"plt.ylabel(r\"$v_n$\")\n",
684684
"\n",
685-
"np.linalg.norm(v_K[: v_C.shape[0]] - v_C)"
685+
"np.linalg.norm(v_K[: min(v_K.shape[0], v_C.shape[0])] - v_C[: min(v_K.shape[0], v_C.shape[0])])"
686686
]
687687
},
688688
{
@@ -1267,9 +1267,6 @@
12671267
"source": [
12681268
"## Download chemiscope from https://github.com/cosmo-epfl/chemiscope/\n",
12691269
"from chemiscope import write_input\n",
1270-
"\n",
1271-
"import ase\n",
1272-
"from ase import io\n",
12731270
"import json"
12741271
]
12751272
},
@@ -1279,8 +1276,6 @@
12791276
"metadata": {},
12801277
"outputs": [],
12811278
"source": [
1282-
"frames = ase.io.read(\"../datasets/CSD-1000R.xyz\", \":10\")\n",
1283-
"\n",
12841279
"# We want to have prediction and projection for all the environments\n",
12851280
"lr = Ridge(alpha=best_regularization)\n",
12861281
"lr.fit(X_train, Y_train)\n",
@@ -1295,21 +1290,21 @@
12951290
]
12961291
},
12971292
{
1298-
"cell_type": "code",
1299-
"execution_count": null,
1293+
"cell_type": "markdown",
13001294
"metadata": {},
1301-
"outputs": [],
13021295
"source": [
1296+
"``` python\n",
1297+
"\n",
13031298
"data = write_input(\n",
13041299
" # File to write \n",
13051300
" path=\"../CSD-1000R.json\",\n",
1306-
" \n",
1301+
"\n",
13071302
" # Dataset name\n",
13081303
" meta={\"name\": \"CSD-1000R\"},\n",
1309-
" \n",
1304+
"\n",
13101305
" # Structures in the dataset\n",
1311-
" frames=frames,\n",
1312-
" \n",
1306+
" frames=# corresponding ASE-type frames,\n",
1307+
"\n",
13131308
" # projection of the dataset, and properties predictions\n",
13141309
" properties={\n",
13151310
" **{f\"PCA_{i+1}\": {\"target\": \"atom\", \"values\": T[:, i]} for i in range(n_PC)},\n",
@@ -1326,10 +1321,11 @@
13261321
" \"values\": np.concatenate([frame.numbers for frame in frames])\n",
13271322
" },\n",
13281323
" },\n",
1329-
" \n",
1324+
"\n",
13301325
" # This should be the same cutoff used to generate the features\n",
13311326
" cutoff=3.5,\n",
1332-
")"
1327+
")\n",
1328+
"```"
13331329
]
13341330
},
13351331
{

notebooks/2_PrincipalCovariatesRegression.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"from sklearn.linear_model import Ridge\n",
4040
"from sklearn.decomposition import PCA\n",
4141
"\n",
42-
"from skcosmo.pcovr import PCovR\n",
42+
"from skcosmo.decomposition import PCovR\n",
4343
"\n",
4444
"\n",
4545
"cmaps = get_cmaps()\n",
@@ -978,7 +978,7 @@
978978
"metadata": {},
979979
"outputs": [],
980980
"source": [
981-
"from skcosmo.pcovr import PCovR"
981+
"from skcosmo.decomposition import PCovR"
982982
]
983983
},
984984
{

notebooks/3_KernelMethods.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"from sklearn.kernel_ridge import KernelRidge\n",
4646
"from functools import partial\n",
4747
"\n",
48-
"from skcosmo.pcovr import KPCovR\n",
48+
"from skcosmo.decomposition import KPCovR\n",
4949
"from skcosmo.preprocessing import KernelNormalizer\n",
5050
"\n",
5151
"cmaps = get_cmaps()\n",
@@ -1454,7 +1454,7 @@
14541454
"metadata": {},
14551455
"outputs": [],
14561456
"source": [
1457-
"from skcosmo.pcovr import KPCovR"
1457+
"from skcosmo.decomposition import KPCovR"
14581458
]
14591459
},
14601460
{

notebooks/4_SparseKernelMethods.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"from sklearn.metrics.pairwise import linear_kernel, rbf_kernel\n",
3636
"from sklearn.kernel_ridge import KernelRidge\n",
3737
"from sklearn.decomposition import KernelPCA\n",
38-
"from skcosmo.pcovr import KPCovR\n",
39-
"from skcosmo.selection import SampleFPS\n",
38+
"from skcosmo.decomposition import KPCovR\n",
39+
"from skcosmo.sample_selection import FPS\n",
4040
"from skcosmo.preprocessing import SparseKernelCenterer\n",
4141
"from functools import partial\n",
4242
"\n",
@@ -121,8 +121,8 @@
121121
"source": [
122122
"n_active = 20\n",
123123
"\n",
124-
"fps_selector = SampleFPS(mixing=1.0, X=X_train)\n",
125-
"fps_idxs = fps_selector.select(n_active)\n",
124+
"fps_selector = FPS(n_samples_to_select=n_active)\n",
125+
"fps_idxs = fps_selector.fit(X_train).selected_idx_\n",
126126
"\n",
127127
"Xsparse = X_train[fps_idxs, :]"
128128
]

notebooks/5_CUR.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
")\n",
4646
"from sklearn.decomposition import PCA\n",
4747
"from sklearn.linear_model import Ridge as LR\n",
48+
"from skcosmo.feature_selection import CUR, FPS\n",
4849
"from skcosmo.selection import FeatureCUR, FeatureFPS\n",
49-
"from skcosmo.pcovr import pcovr_covariance\n",
50+
"from skcosmo.utils import pcovr_covariance\n",
5051
"\n",
5152
"cmaps = get_cmaps()\n",
5253
"plt.style.use(\"../utilities/kernel_pcovr.mplstyle\")\n",
@@ -458,7 +459,7 @@
458459
"outputs": [],
459460
"source": [
460461
"ns = np.array([int(10 ** x) for x in np.linspace(1, np.log10(nCUR), 20)])\n",
461-
"ifps = FeatureFPS(X=X_train).select(n)\n",
462+
"ifps = FPS(n_features_to_select=n).fit(X_train).selected_idx_\n",
462463
"\n",
463464
"X_CUR = [approx_X(X_train, idxs[:n]) for n in ns]\n",
464465
"X_FPS = [approx_X(X_train, ifps[:n]) for n in ns]\n",
@@ -1176,8 +1177,8 @@
11761177
"metadata": {},
11771178
"outputs": [],
11781179
"source": [
1179-
"cur = FeatureCUR(X=X_train, progress_bar=True)\n",
1180-
"idx_cur = cur.select(max(ns))"
1180+
"cur = CUR(n_features_to_select=max(ns), progress_bar=True)\n",
1181+
"idx_cur = cur.fit(X_train).selected_idx_"
11811182
]
11821183
},
11831184
{

tests/pcovr.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,34 @@
44

55
from utilities.sklearn_covr.pcovr import PCovR
66

7+
78
def run_tests():
8-
data = np.load('./tests/CSD-test.npz')
9+
data = np.load("./tests/CSD-test.npz")
910
X = data["X"]
1011
Y = data["Y"]
1112

1213
# Basic Test of PCovR Errors
1314
lr_errors = np.nan * np.zeros(21)
1415
pca_errors = np.nan * np.zeros(21)
15-
for i, alpha in enumerate(np.linspace(0,1,21)):
16-
pcovr = PCovR(alpha = alpha,
17-
n_components=2,
18-
regularization=1e-6,
19-
tol=1e-12)
16+
for i, alpha in enumerate(np.linspace(0, 1, 21)):
17+
pcovr = PCovR(alpha=alpha, n_components=2, regularization=1e-6, tol=1e-12)
2018
pcovr.fit(X, Y)
2119

2220
T = pcovr.transform(X)
2321
Xr = pcovr.inverse_transform(T)
2422
Yp = pcovr.predict(X)
25-
lr_errors[i] = np.linalg.norm(Y-Yp)**2.0 / np.linalg.norm(Y)**2.0
26-
pca_errors[i] = np.linalg.norm(X-Xr)**2.0 / np.linalg.norm(X)**2.0
23+
lr_errors[i] = np.linalg.norm(Y - Yp) ** 2.0 / np.linalg.norm(Y) ** 2.0
24+
pca_errors[i] = np.linalg.norm(X - Xr) ** 2.0 / np.linalg.norm(X) ** 2.0
2725

2826
assert not np.isnan(lr_errors[i]) and not np.isnan(pca_errors[i])
2927

30-
assert all(lr_errors[i] <= lr_errors[i+1] and pca_errors[i] >= pca_errors[i+1]for i in range(len(lr_errors)-1))
28+
assert all(
29+
lr_errors[i] <= lr_errors[i + 1] and pca_errors[i] >= pca_errors[i + 1]
30+
for i in range(len(lr_errors) - 1)
31+
)
3132

3233
# Test of PCovR Fitting
33-
pcovr = PCovR(alpha = 0.5,
34-
n_components=2,
35-
regularization=1e-6,
36-
tol=1e-12)
34+
pcovr = PCovR(alpha=0.5, n_components=2, regularization=1e-6, tol=1e-12)
3735

3836
try:
3937
T = pcovr.transform(X)
@@ -47,7 +45,6 @@ def run_tests():
4745
assert check_X_y(X, T, multi_output=True)
4846

4947

50-
5148
if __name__ == "__main__":
5249
run_tests()
5350
print("Everything passed")

0 commit comments

Comments
 (0)