prcv 1.2.0__tar.gz → 1.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {prcv-1.2.0 → prcv-1.2.2}/PKG-INFO +5 -3
- {prcv-1.2.0 → prcv-1.2.2}/README.md +1 -1
- {prcv-1.2.0 → prcv-1.2.2}/pyproject.toml +3 -2
- {prcv-1.2.0 → prcv-1.2.2}/src/prcv/methods.py +89 -61
- {prcv-1.2.0 → prcv-1.2.2}/src/prcv/misc.py +1 -1
- {prcv-1.2.0 → prcv-1.2.2}/tests/common.py +1 -1
- {prcv-1.2.0 → prcv-1.2.2}/tests/test_pcvpca.py +35 -7
- {prcv-1.2.0 → prcv-1.2.2}/tests/test_pcvpcr.py +44 -7
- {prcv-1.2.0 → prcv-1.2.2}/tests/test_pcvpls.py +43 -7
- {prcv-1.2.0 → prcv-1.2.2}/.gitignore +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/LICENSE +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/demo/corn.csv +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/demo/demo.ipynb +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/demo/demo.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/demo/misc.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/src/__init__.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/src/prcv/__init__.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/src/prcv/scale.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/tests/__init__.py +0 -0
- {prcv-1.2.0 → prcv-1.2.2}/tests/test_misc.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: prcv
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.2
|
|
4
4
|
Summary: Implementation of Procrustes cross-validation in Python
|
|
5
5
|
Project-URL: Homepage, https://github.com/svkucheryavski/pcv
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/svkucheryavski/pcv/issues
|
|
@@ -10,13 +10,15 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
10
10
|
Classifier: Operating System :: OS Independent
|
|
11
11
|
Classifier: Programming Language :: Python :: 3
|
|
12
12
|
Requires-Python: >=3.7
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: scipy
|
|
13
15
|
Description-Content-Type: text/markdown
|
|
14
16
|
|
|
15
17
|
# Procrustes cross-validation for Python
|
|
16
18
|
|
|
17
19
|
Package `prcv` implements [Procrustes cross-validation](https://github.com/svkucheryavski/pcv) in Python language.
|
|
18
20
|
|
|
19
|
-
Last
|
|
21
|
+
Last version of the package (1.2.2) was released in November, 2026. It fixes several bugs: columns with constant values were not handled correctly during scaling, a response given as a one-dimensional array was not accepted, PLS could fail when the number of components had to be reduced, and rounding errors could flip the signs of local loadings. NumPy and SciPy are now installed automatically as dependencies. See details in the overall [project description](https://github.com/svkucheryavski/pcv).
|
|
20
22
|
|
|
21
23
|
## Getting started
|
|
22
24
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Package `prcv` implements [Procrustes cross-validation](https://github.com/svkucheryavski/pcv) in Python language.
|
|
4
4
|
|
|
5
|
-
Last
|
|
5
|
+
Last version of the package (1.2.2) was released in November, 2026. It fixes several bugs: columns with constant values were not handled correctly during scaling, a response given as a one-dimensional array was not accepted, PLS could fail when the number of components had to be reduced, and rounding errors could flip the signs of local loadings. NumPy and SciPy are now installed automatically as dependencies. See details in the overall [project description](https://github.com/svkucheryavski/pcv).
|
|
6
6
|
|
|
7
7
|
## Getting started
|
|
8
8
|
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["hatchling"
|
|
2
|
+
requires = ["hatchling"]
|
|
3
3
|
build-backend = "hatchling.build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "prcv"
|
|
7
|
-
version = "1.2.
|
|
7
|
+
version = "1.2.2"
|
|
8
8
|
authors = [
|
|
9
9
|
{ name="Sergey Kucheryavskiy", email="svkucheryavski@gmail.com" },
|
|
10
10
|
]
|
|
11
11
|
description = "Implementation of Procrustes cross-validation in Python"
|
|
12
12
|
readme = "README.md"
|
|
13
13
|
requires-python = ">=3.7"
|
|
14
|
+
dependencies = ["numpy", "scipy"]
|
|
14
15
|
classifiers = [
|
|
15
16
|
"Programming Language :: Python :: 3",
|
|
16
17
|
"License :: OSI Approved :: MIT License",
|
|
@@ -69,7 +69,8 @@ def pcvpls(X: np.ndarray,
|
|
|
69
69
|
References:
|
|
70
70
|
-----------
|
|
71
71
|
1. S. Kucheryavskiy, O. Rodionova, A. Pomerantsev. Procrustes cross-validation of multivariate
|
|
72
|
-
regression models. Analytica Chimica Acta, 1255
|
|
72
|
+
regression models. Analytica Chimica Acta, 1255, 2023.
|
|
73
|
+
DOI: https://doi.org/10.1016/j.aca.2023.341096
|
|
73
74
|
"""
|
|
74
75
|
|
|
75
76
|
def get_globalmodel(X: np.ndarray, Y: np.ndarray, ncomp: int) -> dict:
|
|
@@ -83,11 +84,11 @@ def pcvpls(X: np.ndarray,
|
|
|
83
84
|
def get_localmodel(Xc: np.ndarray, Yc: np.ndarray, m: dict) -> dict:
|
|
84
85
|
""" Create a local PLS model. """
|
|
85
86
|
ncols = Xc.shape[1]
|
|
86
|
-
Rk, Pk, Ck = simpls(Xc, Yc, ncomp)
|
|
87
|
-
|
|
88
|
-
Pk = Pk *
|
|
89
|
-
Rk = Rk *
|
|
90
|
-
Ck = Ck *
|
|
87
|
+
Rk, Pk, Ck = simpls(Xc, Yc, m['ncomp'])
|
|
88
|
+
signs = flip_signs(normalize(m['R']), normalize(Rk))
|
|
89
|
+
Pk = Pk * signs
|
|
90
|
+
Rk = Rk * signs
|
|
91
|
+
Ck = Ck * signs
|
|
91
92
|
PRM = np.eye(ncols) - np.dot(Rk, Pk.T)
|
|
92
93
|
return {'R': Rk, 'P': Pk, 'C': Ck, 'PRM': PRM}
|
|
93
94
|
|
|
@@ -99,7 +100,8 @@ def pcvpls(X: np.ndarray,
|
|
|
99
100
|
for a in range(0, m['ncomp']):
|
|
100
101
|
ca = m['C'][..., a]
|
|
101
102
|
cka = mk['C'][..., a]
|
|
102
|
-
|
|
103
|
+
denom = np.dot(ca.T, ca)
|
|
104
|
+
dk[a] = 0.0 if denom < eps else np.dot(cka.T, ca) / denom
|
|
103
105
|
|
|
104
106
|
Tpvk = Tk * dk
|
|
105
107
|
Xpvk_hat = np.dot(Tpvk, m['P'].T)
|
|
@@ -184,7 +186,8 @@ def pcvpcr(X: np.ndarray,
|
|
|
184
186
|
References:
|
|
185
187
|
-----------
|
|
186
188
|
1. S. Kucheryavskiy, O. Rodionova, A. Pomerantsev. Procrustes cross-validation of multivariate
|
|
187
|
-
regression models. Analytica Chimica Acta, 1255
|
|
189
|
+
regression models. Analytica Chimica Acta, 1255, 2023.
|
|
190
|
+
DOI: https://doi.org/10.1016/j.aca.2023.341096
|
|
188
191
|
"""
|
|
189
192
|
|
|
190
193
|
def get_globalmodel(X: np.ndarray, Y: np.ndarray, ncomp: int) -> dict:
|
|
@@ -193,7 +196,7 @@ def pcvpcr(X: np.ndarray,
|
|
|
193
196
|
P, s = get_pcamodel(X, ncomp)
|
|
194
197
|
PRM = np.eye(ncols) - np.dot(P, P.T)
|
|
195
198
|
T = np.dot(X, P)
|
|
196
|
-
C = np.dot(Y.T, T) / (
|
|
199
|
+
C = np.dot(Y.T, T) / (T * T).sum(axis=0)
|
|
197
200
|
return {'P': P, 'C': C, 'PRM': PRM, 'ncomp': ncomp}
|
|
198
201
|
|
|
199
202
|
|
|
@@ -201,17 +204,16 @@ def pcvpcr(X: np.ndarray,
|
|
|
201
204
|
""" Create a local PCR model. """
|
|
202
205
|
ncols = Xc.shape[1]
|
|
203
206
|
Pk, sk = get_pcamodel(Xc, m['ncomp'])
|
|
204
|
-
|
|
205
|
-
Pk = Pk * (2 * a - 1)
|
|
207
|
+
Pk = Pk * flip_signs(m['P'], Pk)
|
|
206
208
|
PRMk = np.eye(ncols) - np.dot(Pk, Pk.T)
|
|
207
209
|
Tc = np.dot(Xc, Pk)
|
|
208
|
-
Ck = np.dot(Yc.T, Tc) / (
|
|
210
|
+
Ck = np.dot(Yc.T, Tc) / (Tc * Tc).sum(axis=0)
|
|
209
211
|
return {'P': Pk, 'C': Ck, 'PRM': PRMk}
|
|
210
212
|
|
|
211
213
|
def get_xpvhat(m: dict, mk: dict, Xk: np.ndarray):
|
|
212
214
|
""" Compute explained part of Xpv for current segment k. """
|
|
213
215
|
Tk = np.dot(Xk, mk['P'])
|
|
214
|
-
dk = mk['C'] / m['C']
|
|
216
|
+
dk = np.where(np.abs(m['C']) < eps, 0.0, mk['C'] / m['C'])
|
|
215
217
|
Tpvk = Tk * dk
|
|
216
218
|
Xpvk_hat = np.dot(Tpvk, m['P'].T)
|
|
217
219
|
return (Xpvk_hat, dk)
|
|
@@ -275,11 +277,14 @@ def pcvreg(X: np.ndarray,
|
|
|
275
277
|
|
|
276
278
|
"""
|
|
277
279
|
|
|
280
|
+
if cvscope not in ('global', 'local'):
|
|
281
|
+
raise ValueError('argument "cvscope" should be one of "global" or "local".')
|
|
282
|
+
|
|
278
283
|
(nrows, ncols) = X.shape
|
|
279
284
|
|
|
280
285
|
# correct shape of Y just in case
|
|
281
|
-
if Y.
|
|
282
|
-
Y.reshape(
|
|
286
|
+
if Y.ndim == 1:
|
|
287
|
+
Y = Y.reshape(-1, 1)
|
|
283
288
|
|
|
284
289
|
nresp = Y.shape[1]
|
|
285
290
|
|
|
@@ -292,14 +297,11 @@ def pcvreg(X: np.ndarray,
|
|
|
292
297
|
mY = Y.mean(axis = 0) if center else np.zeros(nresp)
|
|
293
298
|
sY = Y.std(axis = 0, ddof = 1) if scale else np.ones(nresp)
|
|
294
299
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
is_const_Y = np.allclose(Y - mY, 0.0, atol=eps, rtol=0.0)
|
|
299
|
-
sY[is_const_Y] <- 1.0 - eps;
|
|
300
|
+
sX[sX < eps] = 1.0
|
|
301
|
+
sY[sY < eps] = 1.0
|
|
300
302
|
|
|
301
|
-
X = (X - mX) /
|
|
302
|
-
Y = (Y - mY) /
|
|
303
|
+
X = (X - mX) / sX
|
|
304
|
+
Y = (Y - mY) / sY
|
|
303
305
|
|
|
304
306
|
|
|
305
307
|
# create a global model
|
|
@@ -325,15 +327,12 @@ def pcvreg(X: np.ndarray,
|
|
|
325
327
|
mYl = Yc.mean(axis = 0) if center else np.zeros(nresp)
|
|
326
328
|
sYl = Yc.std(axis = 0, ddof = 1) if scale else np.ones(nresp)
|
|
327
329
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
is_const_Y = np.allclose(Yc - mYl, 0.0, atol=eps, rtol=0.0)
|
|
332
|
-
sYl[is_const_Y] <- 1.0 - eps
|
|
330
|
+
sXl[sXl < eps] = 1.0
|
|
331
|
+
sYl[sYl < eps] = 1.0
|
|
333
332
|
|
|
334
|
-
Xc = (Xc - mXl) /
|
|
335
|
-
Yc = (Yc - mYl) /
|
|
336
|
-
Xk = (Xk - mXl) /
|
|
333
|
+
Xc = (Xc - mXl) / sXl
|
|
334
|
+
Yc = (Yc - mYl) / sYl
|
|
335
|
+
Xk = (Xk - mXl) / sXl
|
|
337
336
|
|
|
338
337
|
# get local model
|
|
339
338
|
mk = funlist['get_localmodel'](Xc, Yc, m)
|
|
@@ -410,9 +409,13 @@ def pcvpca(X: np.ndarray,
|
|
|
410
409
|
References:
|
|
411
410
|
-----------
|
|
412
411
|
1. S. Kucheryavskiy, O. Rodionova, A. Pomerantsev. Procrustes cross-validation of multivariate
|
|
413
|
-
regression models. Analytica Chimica Acta, 1255
|
|
412
|
+
regression models. Analytica Chimica Acta, 1255, 2023.
|
|
413
|
+
DOI: https://doi.org/10.1016/j.aca.2023.341096
|
|
414
414
|
"""
|
|
415
415
|
|
|
416
|
+
if cvscope not in ('global', 'local'):
|
|
417
|
+
raise ValueError('argument "cvscope" should be one of "global" or "local".')
|
|
418
|
+
|
|
416
419
|
(nrows, ncols) = X.shape
|
|
417
420
|
|
|
418
421
|
# get cross-validation settings
|
|
@@ -421,9 +424,8 @@ def pcvpca(X: np.ndarray,
|
|
|
421
424
|
# compute global mean and standard deviation and autoscale the whole data
|
|
422
425
|
mX = X.mean(axis = 0) if center else np.zeros(ncols)
|
|
423
426
|
sX = X.std(axis = 0, ddof = 1) if scale else np.ones(ncols)
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
X = (X - mX) / (sX + eps)
|
|
427
|
+
sX[sX < eps] = 1.0
|
|
428
|
+
X = (X - mX) / sX
|
|
427
429
|
|
|
428
430
|
|
|
429
431
|
# create a global model
|
|
@@ -446,16 +448,14 @@ def pcvpca(X: np.ndarray,
|
|
|
446
448
|
if cvscope == 'local':
|
|
447
449
|
mXl = Xc.mean(axis = 0) if center else np.zeros(ncols)
|
|
448
450
|
sXl = Xc.std(axis = 0, ddof = 1) if scale else np.ones(ncols)
|
|
449
|
-
|
|
450
|
-
sXl[is_const_X] <- 1.0 - eps
|
|
451
|
+
sXl[sXl < eps] = 1.0
|
|
451
452
|
|
|
452
|
-
Xc = (Xc - mXl) /
|
|
453
|
-
Xk = (Xk - mXl) /
|
|
453
|
+
Xc = (Xc - mXl) / sXl
|
|
454
|
+
Xk = (Xk - mXl) / sXl
|
|
454
455
|
|
|
455
456
|
# get local model and correct directions of the loadings
|
|
456
457
|
Pk, sk = get_pcamodel(Xc, cvncomp)
|
|
457
|
-
|
|
458
|
-
Pk = Pk * (2 * a - 1)
|
|
458
|
+
Pk = Pk * flip_signs(P, Pk)
|
|
459
459
|
|
|
460
460
|
# get scores and residuals by projection local validation set to the local model
|
|
461
461
|
Tk = np.dot(Xk, Pk)
|
|
@@ -505,39 +505,62 @@ def get_xpvorth(qk: np.ndarray, Xk: np.ndarray, PRM: np.ndarray) -> np.ndarray:
|
|
|
505
505
|
# project Xk to a set of random vectors and normalize columns
|
|
506
506
|
Z = np.random.randn(nobj, nobj)
|
|
507
507
|
Xpv_orth = np.dot(Z, Xk)
|
|
508
|
-
|
|
508
|
+
n1 = np.linalg.norm(Xpv_orth, axis = 0)
|
|
509
|
+
n1[n1 < eps * 0.01] = 1.0 - eps
|
|
510
|
+
Xpv_orth = Xpv_orth / (n1 + eps)
|
|
509
511
|
|
|
510
512
|
# orthogonalize and rescale rows so sum of their squares is the same as qk
|
|
511
513
|
Xpv_orth = np.dot(Xpv_orth, PRM)
|
|
512
|
-
|
|
514
|
+
n2 = np.linalg.norm(Xpv_orth, axis = 1)
|
|
515
|
+
n2[n2 < eps * 0.01] = 1.0 - eps
|
|
516
|
+
Xpv_orth = Xpv_orth * (np.sqrt(qk) / (n2 + eps)).reshape(-1, 1)
|
|
513
517
|
|
|
514
518
|
return Xpv_orth
|
|
515
519
|
|
|
516
520
|
def normalize(X: np.ndarray, axis: int = 0) -> np.ndarray:
|
|
517
521
|
""" Normalize rows or columns of X to Euclidean norm. """
|
|
518
522
|
norm = np.sqrt((X * X).sum(axis = axis))
|
|
523
|
+
norm = np.where(norm < eps, 1.0, norm)
|
|
519
524
|
return X / norm
|
|
520
525
|
|
|
526
|
+
|
|
527
|
+
def flip_signs(V1: np.ndarray, V2: np.ndarray) -> np.ndarray:
|
|
528
|
+
"""
|
|
529
|
+
Return array of +1/-1 signs for direction correction of local loadings.
|
|
530
|
+
|
|
531
|
+
Compares column directions of two matrices and returns +1 if angle < 90 degrees,
|
|
532
|
+
-1 otherwise. Input dot products are clamped to [-1, 1] before arccos to avoid
|
|
533
|
+
NaN from floating-point overshoot.
|
|
534
|
+
"""
|
|
535
|
+
cosines = np.clip((V1 * V2).sum(axis=0), -1, 1)
|
|
536
|
+
return 2 * (np.arccos(cosines) < math.pi / 2) - 1
|
|
537
|
+
|
|
521
538
|
def simpls(X: np.ndarray, Y: np.ndarray, ncomp: int):
|
|
522
539
|
"""
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
540
|
+
SIMPLS algorithm for calibration of PLS model.
|
|
541
|
+
|
|
542
|
+
Parameters:
|
|
543
|
+
-----------
|
|
544
|
+
X : numpy.ndarray
|
|
545
|
+
Matrix with x values (predictors).
|
|
546
|
+
Y : numpy.ndarray
|
|
547
|
+
Matrix with y values (responses).
|
|
548
|
+
ncomp : int
|
|
549
|
+
Number of components to calculate.
|
|
550
|
+
|
|
551
|
+
Returns:
|
|
552
|
+
--------
|
|
553
|
+
R : numpy.ndarray
|
|
554
|
+
Weights matrix.
|
|
555
|
+
P : numpy.ndarray
|
|
556
|
+
X-loadings matrix.
|
|
557
|
+
C : numpy.ndarray
|
|
558
|
+
Y-loadings matrix.
|
|
559
|
+
|
|
560
|
+
References:
|
|
561
|
+
-----------
|
|
562
|
+
1. S. de Jong. SIMPLS: An Alternative approach to partial least squares regression.
|
|
563
|
+
Chemometrics and Intelligent Laboratory Systems, 18, 1993 (251-263).
|
|
541
564
|
"""
|
|
542
565
|
|
|
543
566
|
(nrows, ncols) = X.shape
|
|
@@ -557,6 +580,8 @@ def simpls(X: np.ndarray, Y: np.ndarray, ncomp: int):
|
|
|
557
580
|
r = Vl[..., :1]
|
|
558
581
|
t = np.dot(X, r)
|
|
559
582
|
tnorm = math.sqrt((t * t).sum())
|
|
583
|
+
if tnorm < eps:
|
|
584
|
+
break
|
|
560
585
|
t = t / tnorm
|
|
561
586
|
r = r / tnorm
|
|
562
587
|
|
|
@@ -569,7 +594,10 @@ def simpls(X: np.ndarray, Y: np.ndarray, ncomp: int):
|
|
|
569
594
|
v = v - np.dot(V, np.dot(V.T, p))
|
|
570
595
|
u = u - np.dot(T, np.dot(T.T, u))
|
|
571
596
|
|
|
572
|
-
|
|
597
|
+
vnorm = math.sqrt((v * v).sum())
|
|
598
|
+
if vnorm < eps:
|
|
599
|
+
break
|
|
600
|
+
v = v / vnorm
|
|
573
601
|
|
|
574
602
|
R[..., a] = r.flatten()
|
|
575
603
|
V[..., a] = v.flatten()
|
|
@@ -12,7 +12,7 @@ def get_splits(nrows: int, nseg: int):
|
|
|
12
12
|
Generate vector with segment numbers.
|
|
13
13
|
"""
|
|
14
14
|
nrep = math.ceil(nrows / nseg)
|
|
15
|
-
return np.
|
|
15
|
+
return np.tile(np.arange(1, nseg + 1, dtype = int), nrep)[:nrows]
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
|
|
@@ -4,7 +4,7 @@ import unittest
|
|
|
4
4
|
import math
|
|
5
5
|
|
|
6
6
|
from src.prcv.methods import pcvpca, get_pcamodel, get_xpvorth
|
|
7
|
-
from .common import
|
|
7
|
+
from .common import run_reference_cases
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
@@ -47,7 +47,7 @@ def pca_predict(X: np.ndarray, Xpv: np.ndarray, ncomp: int, center: bool = True,
|
|
|
47
47
|
return {'T': Tpv, 'H': Hpv, 'Q': Qpv}
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def
|
|
50
|
+
def check_pcacase(X, ncomp, cv, center, scale, T, H, Q):
|
|
51
51
|
"""
|
|
52
52
|
Create PCA global model, apply it to generated Xpv set and test equality of
|
|
53
53
|
main outcomes (scores, score distances and orthogonal distances) entered manually
|
|
@@ -61,7 +61,7 @@ def test_pcacase(X, ncomp, cv, center, scale, T, H, Q):
|
|
|
61
61
|
np.testing.assert_array_almost_equal(r['Q'], np.array(Q).reshape(X.shape[0], ncomp))
|
|
62
62
|
|
|
63
63
|
|
|
64
|
-
def
|
|
64
|
+
def check_pcacase_ref(X, Y, ncomp, cv, center, scale, path, file_suffix):
|
|
65
65
|
"""
|
|
66
66
|
Create PCA global model, apply it to generated Xpv set and test equality of
|
|
67
67
|
main outcomes (scores, score distances and orthogonal distances) taken from a
|
|
@@ -117,7 +117,7 @@ class TestPCVPCAMethods(unittest.TestCase):
|
|
|
117
117
|
center = True
|
|
118
118
|
scale = False
|
|
119
119
|
|
|
120
|
-
|
|
120
|
+
check_pcacase(self.X, ncomp, cv, center, scale,
|
|
121
121
|
T = [
|
|
122
122
|
-10.0140380, -8.5235218, -3.2171212,
|
|
123
123
|
-8.6658232, -4.0965210, 1.7038996,
|
|
@@ -160,7 +160,7 @@ class TestPCVPCAMethods(unittest.TestCase):
|
|
|
160
160
|
center = True
|
|
161
161
|
scale = False
|
|
162
162
|
|
|
163
|
-
|
|
163
|
+
check_pcacase(self.X, ncomp, cv, center, scale,
|
|
164
164
|
T = [
|
|
165
165
|
-12.2937342, -3.5829202,
|
|
166
166
|
-8.8832034, -3.6204597,
|
|
@@ -203,7 +203,7 @@ class TestPCVPCAMethods(unittest.TestCase):
|
|
|
203
203
|
center = True
|
|
204
204
|
scale = False
|
|
205
205
|
|
|
206
|
-
|
|
206
|
+
check_pcacase(self.X, ncomp, cv, center, scale,
|
|
207
207
|
T = [
|
|
208
208
|
-9.39633799,
|
|
209
209
|
-8.75468266,
|
|
@@ -238,7 +238,7 @@ class TestPCVPCAMethods(unittest.TestCase):
|
|
|
238
238
|
|
|
239
239
|
def test_references(self):
|
|
240
240
|
""" Test combination of settings by comparing with reference values. """
|
|
241
|
-
|
|
241
|
+
run_reference_cases('pcvpca', check_pcacase_ref)
|
|
242
242
|
|
|
243
243
|
|
|
244
244
|
|
|
@@ -258,5 +258,33 @@ class TestXpvOrthMethods(unittest.TestCase):
|
|
|
258
258
|
np.testing.assert_array_almost_equal((Y * Y).sum(axis = 1), qk)
|
|
259
259
|
|
|
260
260
|
|
|
261
|
+
class TestPCVPCAEdgeCases(unittest.TestCase):
|
|
262
|
+
|
|
263
|
+
def test_constant_column(self):
|
|
264
|
+
""" Data with a constant column - tests epsilon protection on std dev. """
|
|
265
|
+
X = np.array([1, 3, 17, 19, 10, 14, 7, 13, 9, 11, 2, 15, 8, 6, 4, 12,
|
|
266
|
+
22, 18, 4, 14, 13, 12, 1, 15]).reshape(3, 8).transpose()
|
|
267
|
+
Xc = np.hstack([X, np.full((X.shape[0], 1), 42.0)])
|
|
268
|
+
np.random.seed(42)
|
|
269
|
+
Xpv = pcvpca(Xc, ncomp=2, scale=True, cv={'type': 'ven', 'nseg': 2})
|
|
270
|
+
self.assertEqual(Xpv.shape, Xc.shape)
|
|
271
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
272
|
+
|
|
273
|
+
def test_small_dataset(self):
|
|
274
|
+
""" Small dataset (6 rows) - tests ncomp auto-reduction. """
|
|
275
|
+
X = np.array([1, 3, 17, 19, 10, 14, 7, 13, 9, 11, 2, 15, 8, 6, 4, 12,
|
|
276
|
+
22, 18, 4, 14, 13, 12, 1, 15]).reshape(3, 8).transpose()
|
|
277
|
+
np.random.seed(42)
|
|
278
|
+
Xpv = pcvpca(X[:6, :], ncomp=10, cv={'type': 'ven', 'nseg': 3})
|
|
279
|
+
self.assertEqual(Xpv.shape[0], 6)
|
|
280
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
281
|
+
|
|
282
|
+
def test_invalid_cvscope(self):
|
|
283
|
+
""" Invalid cvscope should raise ValueError. """
|
|
284
|
+
X = np.array([1, 3, 17, 19, 10, 14, 7, 13, 9, 11, 2, 15, 8, 6, 4, 12,
|
|
285
|
+
22, 18, 4, 14, 13, 12, 1, 15]).reshape(3, 8).transpose()
|
|
286
|
+
self.assertRaises(ValueError, pcvpca, X, 2, cvscope='wrong')
|
|
287
|
+
|
|
288
|
+
|
|
261
289
|
if __name__ == '__main__':
|
|
262
290
|
unittest.main()
|
|
@@ -4,7 +4,7 @@ import math
|
|
|
4
4
|
|
|
5
5
|
from src.prcv.misc import get_cvsettings
|
|
6
6
|
from src.prcv.methods import pcvpcr, get_pcamodel
|
|
7
|
-
from .common import
|
|
7
|
+
from .common import run_reference_cases
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
################################
|
|
@@ -20,8 +20,8 @@ def pcr_predict(X: np.ndarray, Y: np.ndarray, Xpv: np.ndarray, ncomp: int, cente
|
|
|
20
20
|
(nrows, ncols) = X.shape
|
|
21
21
|
|
|
22
22
|
# correct shape of Y just in case
|
|
23
|
-
if Y.
|
|
24
|
-
Y.reshape(
|
|
23
|
+
if Y.ndim == 1:
|
|
24
|
+
Y = Y.reshape(-1, 1)
|
|
25
25
|
|
|
26
26
|
nresp = 1
|
|
27
27
|
|
|
@@ -65,7 +65,7 @@ def pcr_predict(X: np.ndarray, Y: np.ndarray, Xpv: np.ndarray, ncomp: int, cente
|
|
|
65
65
|
return {'Yp': Ypv, 'T': Tpv, 'H': Hpv, 'Q': Qpv, 'RMSE': np.sqrt((Epv * Epv).mean(axis = 0))}
|
|
66
66
|
|
|
67
67
|
|
|
68
|
-
def
|
|
68
|
+
def check_pcrcase(X, Y, ncomp, cv, center, scale, D, T, Yp):
|
|
69
69
|
"""
|
|
70
70
|
Create PCR global model, apply it to generated Xpv set and test equality of
|
|
71
71
|
main outcomes (predictions and errors) entered manually from R tests.
|
|
@@ -83,7 +83,7 @@ def test_pcrcase(X, Y, ncomp, cv, center, scale, D, T, Yp):
|
|
|
83
83
|
)
|
|
84
84
|
|
|
85
85
|
|
|
86
|
-
def
|
|
86
|
+
def check_pcrcase_ref(X, Y, ncomp, cv, center, scale, path, file_suffix):
|
|
87
87
|
"""
|
|
88
88
|
Create PCR global model, apply it to generated Xpv set and test equality of
|
|
89
89
|
main outcomes (predictions and scalars) taken from a
|
|
@@ -145,7 +145,7 @@ class TestPCVPCRMethods(unittest.TestCase):
|
|
|
145
145
|
center = True
|
|
146
146
|
scale = True
|
|
147
147
|
|
|
148
|
-
|
|
148
|
+
check_pcrcase(self.X, self.Y, ncomp = ncomp, cv = cv, center=center, scale=scale,
|
|
149
149
|
D = [
|
|
150
150
|
0.8928038, 0.6953515, 1.7268124,
|
|
151
151
|
0.9272071, 1.4768482, -0.4511441,
|
|
@@ -177,7 +177,44 @@ class TestPCVPCRMethods(unittest.TestCase):
|
|
|
177
177
|
|
|
178
178
|
def test_references(self):
|
|
179
179
|
""" Test combination of settings by comparing with reference values. """
|
|
180
|
-
|
|
180
|
+
run_reference_cases('pcvpcr', check_pcrcase_ref)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class TestPCVPCREdgeCases(unittest.TestCase):
|
|
184
|
+
|
|
185
|
+
def setUp(self):
|
|
186
|
+
self.Y = np.array([32, 35, 36, 37, 42, 43, 43, 44]).reshape(8, 1)
|
|
187
|
+
self.X = np.array([
|
|
188
|
+
150, 41, 28000, 119,
|
|
189
|
+
160, 48, 31000, 129,
|
|
190
|
+
166, 47, 28000, 112,
|
|
191
|
+
166, 49, 14000, 123,
|
|
192
|
+
175, 67, 38000, 105,
|
|
193
|
+
180, 80, 30000, 129,
|
|
194
|
+
181, 75, 31000, 105,
|
|
195
|
+
180, 81, 42000, 113
|
|
196
|
+
]).reshape(8, 4).astype(float)
|
|
197
|
+
|
|
198
|
+
def test_constant_column(self):
|
|
199
|
+
""" Data with a constant column - tests epsilon protection on scaling and D. """
|
|
200
|
+
Xc = np.hstack([self.X, np.full((self.X.shape[0], 1), 42.0)])
|
|
201
|
+
np.random.seed(42)
|
|
202
|
+
Xpv, D = pcvpcr(Xc, self.Y, ncomp=3, scale=True, cv={'type': 'ven', 'nseg': 4})
|
|
203
|
+
self.assertEqual(Xpv.shape, Xc.shape)
|
|
204
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
205
|
+
self.assertFalse(np.any(np.isnan(D)))
|
|
206
|
+
|
|
207
|
+
def test_small_dataset(self):
|
|
208
|
+
""" Small dataset (6 rows) - tests ncomp auto-reduction. """
|
|
209
|
+
np.random.seed(42)
|
|
210
|
+
Xpv, D = pcvpcr(self.X[:6, :], self.Y[:6, :], ncomp=10, cv={'type': 'ven', 'nseg': 3})
|
|
211
|
+
self.assertEqual(Xpv.shape[0], 6)
|
|
212
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
213
|
+
|
|
214
|
+
def test_invalid_cvscope(self):
|
|
215
|
+
""" Invalid cvscope should raise ValueError. """
|
|
216
|
+
self.assertRaises(ValueError, pcvpcr, self.X, self.Y, 3, cvscope='wrong')
|
|
217
|
+
|
|
181
218
|
|
|
182
219
|
if __name__ == '__main__':
|
|
183
220
|
unittest.main()
|
|
@@ -3,7 +3,7 @@ import unittest
|
|
|
3
3
|
|
|
4
4
|
from src.prcv.misc import get_cvsettings
|
|
5
5
|
from src.prcv.methods import pcvpls, simpls
|
|
6
|
-
from .common import
|
|
6
|
+
from .common import run_reference_cases
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
@@ -25,8 +25,8 @@ def pls_predict(X: np.ndarray, Y: np.ndarray, Xpv: np.ndarray, ncomp: int, cente
|
|
|
25
25
|
(nrows, ncols) = X.shape
|
|
26
26
|
|
|
27
27
|
# correct shape of Y just in case
|
|
28
|
-
if Y.
|
|
29
|
-
Y.reshape(
|
|
28
|
+
if Y.ndim == 1:
|
|
29
|
+
Y = Y.reshape(-1, 1)
|
|
30
30
|
|
|
31
31
|
nresp = 1
|
|
32
32
|
|
|
@@ -73,7 +73,7 @@ def pls_predict(X: np.ndarray, Y: np.ndarray, Xpv: np.ndarray, ncomp: int, cente
|
|
|
73
73
|
return {'Yp': Ypv, 'T': Tpv, 'H': Hpv, 'Q': Qpv, 'RMSE': np.sqrt((Epv * Epv).mean(axis = 0))}
|
|
74
74
|
|
|
75
75
|
|
|
76
|
-
def
|
|
76
|
+
def check_plscase(X, Y, ncomp, cv, center, scale, D, T, H, Yp):
|
|
77
77
|
"""
|
|
78
78
|
Create PLS global model, apply it to generated Xpv set and test equality of
|
|
79
79
|
main outcomes (predictions and errors) entered manually from R tests.
|
|
@@ -88,7 +88,7 @@ def test_plscase(X, Y, ncomp, cv, center, scale, D, T, H, Yp):
|
|
|
88
88
|
np.testing.assert_array_almost_equal(r['H'], np.array(H).reshape(X.shape[0], cvncomp), decimal = 5)
|
|
89
89
|
|
|
90
90
|
|
|
91
|
-
def
|
|
91
|
+
def check_plscase_ref(X, Y, ncomp, cv, center, scale, path, file_suffix):
|
|
92
92
|
"""
|
|
93
93
|
Create PLS global model, apply it to generated Xpv set and test equality of
|
|
94
94
|
main outcomes (predictions and scalars) taken from a
|
|
@@ -171,7 +171,7 @@ class TestPCVPLSMethods(unittest.TestCase):
|
|
|
171
171
|
center = True
|
|
172
172
|
scale = True
|
|
173
173
|
|
|
174
|
-
|
|
174
|
+
check_plscase(self.X, self.Y, ncomp = ncomp, cv = cv, center=center, scale=scale,
|
|
175
175
|
D = [
|
|
176
176
|
0.7618305, 0.9701346, 0.8583589,
|
|
177
177
|
0.8664892, 1.1233015, -0.4189311,
|
|
@@ -213,7 +213,43 @@ class TestPCVPLSMethods(unittest.TestCase):
|
|
|
213
213
|
|
|
214
214
|
def test_references(self):
|
|
215
215
|
""" Test combination of settings by comparing with reference values. """
|
|
216
|
-
|
|
216
|
+
run_reference_cases('pcvpls', check_plscase_ref)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class TestPCVPLSEdgeCases(unittest.TestCase):
|
|
220
|
+
|
|
221
|
+
def setUp(self):
|
|
222
|
+
self.Y = np.array([32, 35, 36, 37, 42, 43, 43, 44]).reshape(8, 1)
|
|
223
|
+
self.X = np.array([
|
|
224
|
+
150, 41, 28000, 119,
|
|
225
|
+
160, 48, 31000, 129,
|
|
226
|
+
166, 47, 28000, 112,
|
|
227
|
+
166, 49, 14000, 123,
|
|
228
|
+
175, 67, 38000, 105,
|
|
229
|
+
180, 80, 30000, 129,
|
|
230
|
+
181, 75, 31000, 105,
|
|
231
|
+
180, 81, 42000, 113
|
|
232
|
+
]).reshape(8, 4).astype(float)
|
|
233
|
+
|
|
234
|
+
def test_constant_column(self):
|
|
235
|
+
""" Data with a constant column - tests epsilon protection on scaling and D. """
|
|
236
|
+
Xc = np.hstack([self.X, np.full((self.X.shape[0], 1), 42.0)])
|
|
237
|
+
np.random.seed(42)
|
|
238
|
+
Xpv, D = pcvpls(Xc, self.Y, ncomp=3, scale=True, cv={'type': 'ven', 'nseg': 4})
|
|
239
|
+
self.assertEqual(Xpv.shape, Xc.shape)
|
|
240
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
241
|
+
self.assertFalse(np.any(np.isnan(D)))
|
|
242
|
+
|
|
243
|
+
def test_small_dataset(self):
|
|
244
|
+
""" Small dataset (6 rows) - tests ncomp auto-reduction. """
|
|
245
|
+
np.random.seed(42)
|
|
246
|
+
Xpv, D = pcvpls(self.X[:6, :], self.Y[:6, :], ncomp=10, cv={'type': 'ven', 'nseg': 3})
|
|
247
|
+
self.assertEqual(Xpv.shape[0], 6)
|
|
248
|
+
self.assertFalse(np.any(np.isnan(Xpv)))
|
|
249
|
+
|
|
250
|
+
def test_invalid_cvscope(self):
|
|
251
|
+
""" Invalid cvscope should raise ValueError. """
|
|
252
|
+
self.assertRaises(ValueError, pcvpls, self.X, self.Y, 3, cvscope='wrong')
|
|
217
253
|
|
|
218
254
|
|
|
219
255
|
if __name__ == '__main__':
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|