|
| 1 | +# include all packages, including those needed for the children classes |
| 2 | +from abc import ABCMeta |
| 3 | +from collections.abc import Mapping |
| 4 | +from copy import copy |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from numpy.linalg import lstsq, norm |
| 8 | +from tensorly.decomposition._cp import parafac |
| 9 | +from tensorly.tenalg import ( |
| 10 | + multi_mode_dot, # ty: ignore[unresolved-import] # backend-dispatched at runtime |
| 11 | +) |
| 12 | + |
| 13 | +from .missingvals import miss_mmodedot, miss_tensordot |
| 14 | +from .util import calcR2X, factors_to_tensor |
| 15 | + |
| 16 | + |
| 17 | +class ctPLS(Mapping, metaclass=ABCMeta): |
| 18 | + """Coupled tensor PLS""" |
| 19 | + |
| 20 | + def __init__(self, n_components: int): |
| 21 | + super().__init__() |
| 22 | + # Parameters |
| 23 | + self.n_components = n_components |
| 24 | + |
| 25 | + def __getitem__(self, index): |
| 26 | + if index == 0: |
| 27 | + return self.Xs_factors |
| 28 | + elif index == 1: |
| 29 | + return self.Y_factors |
| 30 | + elif index == 2: |
| 31 | + return self.coef_ |
| 32 | + else: |
| 33 | + raise IndexError |
| 34 | + |
| 35 | + def __iter__(self): |
| 36 | + yield self.Xs_factors |
| 37 | + yield self.Y_factors |
| 38 | + yield self.coef_ |
| 39 | + |
| 40 | + def __len__(self): |
| 41 | + return 3 |
| 42 | + |
| 43 | + def copy(self): |
| 44 | + return copy(self) |
| 45 | + |
| 46 | + def preprocess(self, Xs, Y): |
| 47 | + # check input integrity |
| 48 | + assert isinstance(Xs, list) |
| 49 | + |
| 50 | + for X in Xs: |
| 51 | + assert X.shape[0] == Y.shape[0] |
| 52 | + assert X.ndim >= 2 |
| 53 | + assert np.all(np.any(np.isfinite(X), axis=0)), ( |
| 54 | + "Each measurement (chord) must have at least one sample." |
| 55 | + ) |
| 56 | + assert Y.ndim <= 2, "Only a matrix (2-mode tensor) Y is acceptable." |
| 57 | + if Y.ndim == 1: |
| 58 | + Y = Y.reshape(-1, 1) |
| 59 | + assert np.all( |
| 60 | + np.any( |
| 61 | + [ |
| 62 | + np.any( |
| 63 | + np.isfinite(Xs[ti]), |
| 64 | + axis=tuple(i for i in range(1, Xs[ti].ndim)), |
| 65 | + ) |
| 66 | + for ti in range(len(Xs)) |
| 67 | + ], |
| 68 | + axis=0, |
| 69 | + ) |
| 70 | + ), "Each sample must have at least one value across tensors." |
| 71 | + |
| 72 | + # mean center the data; set up factors |
| 73 | + self.Xs_len = len(Xs) |
| 74 | + self.Xs_dim = [X.ndim for X in Xs] |
| 75 | + self.Xs_shape = [X.shape for X in Xs] |
| 76 | + self.Y_shape = Y.shape |
| 77 | + |
| 78 | + self.factor_T = np.zeros((self.Y_shape[0], self.n_components)) |
| 79 | + self.Xs_factors = [ |
| 80 | + [self.factor_T] + [np.zeros((lf, self.n_components)) for lf in X.shape[1:]] |
| 81 | + for X in Xs |
| 82 | + ] |
| 83 | + self.Y_factors = [np.zeros((lf, self.n_components)) for lf in Y.shape] |
| 84 | + self.coef_ = np.zeros( |
| 85 | + (self.n_components, self.n_components) |
| 86 | + ) # a upper triangular matrix |
| 87 | + |
| 88 | + self.R2Xs = [np.zeros(self.n_components) for _ in range(self.Xs_len)] |
| 89 | + self.R2Y = np.zeros(self.n_components) |
| 90 | + |
| 91 | + self.Xs_mean = [np.nanmean(X, axis=0) for X in Xs] |
| 92 | + self.Y_mean = np.nanmean(Y, axis=0) |
| 93 | + |
| 94 | + return [X - self.Xs_mean[i] for (i, X) in enumerate(Xs)], Y - self.Y_mean |
| 95 | + |
| 96 | + def fit(self, Xs, Y, tol=1e-8, max_iter=100, verbose=0): |
| 97 | + oXs, oY = [X.copy() for X in Xs], Y.copy() |
| 98 | + Xs, Y = self.preprocess(Xs, Y) |
| 99 | + |
| 100 | + # Run-specific variables, not saved in the object |
| 101 | + Xs_hasMiss = [np.any(np.isnan(X)) for X in Xs] |
| 102 | + if any(Xs_hasMiss): |
| 103 | + print("At least one X has missing values") |
| 104 | + Xs_miss = [ |
| 105 | + np.isnan(X) for X in Xs |
| 106 | + ] # positions of missing value, not the opposite |
| 107 | + |
| 108 | + for a in range(self.n_components): |
| 109 | + oldU = np.ones_like(self.Y_factors[0][:, a]) * np.inf |
| 110 | + self.Y_factors[0][:, a] = Y[:, 0] |
| 111 | + for iter in range(max_iter): |
| 112 | + for ti, X in enumerate(Xs): |
| 113 | + if not Xs_hasMiss[ti]: |
| 114 | + Z = np.einsum("i...,i...->...", X, self.Y_factors[0][:, a]) |
| 115 | + else: |
| 116 | + Z = miss_tensordot(X, self.Y_factors[0][:, a], Xs_miss[ti]) |
| 117 | + |
| 118 | + Z_comp = [Z / norm(Z)] |
| 119 | + if Z.ndim >= 2: |
| 120 | + Z_comp = parafac( |
| 121 | + Z, |
| 122 | + 1, |
| 123 | + n_iter_max=max_iter, |
| 124 | + tol=tol, |
| 125 | + init="svd", |
| 126 | + normalize_factors=True, |
| 127 | + )[1] |
| 128 | + # TODO: add parafac allowing missing values |
| 129 | + for ii in range(Z.ndim): |
| 130 | + self.Xs_factors[ti][ii + 1][:, a] = Z_comp[ii].flatten() |
| 131 | + |
| 132 | + Ts = [ |
| 133 | + multi_mode_dot( |
| 134 | + Xs[ti], |
| 135 | + [ff[:, a] for ff in self.Xs_factors[ti][1:]], |
| 136 | + range(1, self.Xs_dim[ti]), |
| 137 | + ) |
| 138 | + if not Xs_hasMiss[ti] |
| 139 | + else miss_mmodedot( |
| 140 | + Xs[ti], |
| 141 | + [ff[:, a] for ff in self.Xs_factors[ti][1:]], |
| 142 | + Xs_miss[ti], |
| 143 | + ) |
| 144 | + for ti in range(self.Xs_len) |
| 145 | + ] |
| 146 | + self.factor_T[:, a] = np.nanmean(Ts, axis=0) |
| 147 | + self.Y_factors[1][:, a] = Y.T @ self.factor_T[:, a] |
| 148 | + self.Y_factors[1][:, a] /= norm(self.Y_factors[1][:, a]) |
| 149 | + self.Y_factors[0][:, a] = Y @ self.Y_factors[1][:, a] |
| 150 | + if norm(oldU - self.Y_factors[0][:, a]) < tol: |
| 151 | + if verbose: |
| 152 | + print(f"Comp {a}: converged after {iter} iterations") |
| 153 | + break |
| 154 | + oldU = self.Y_factors[0][:, a].copy() |
| 155 | + |
| 156 | + for ti, X in enumerate(Xs): |
| 157 | + X -= factors_to_tensor([ff[:, [a]] for ff in self.Xs_factors[ti]]) |
| 158 | + self.R2Xs[ti][a] = calcR2X( |
| 159 | + oXs[ti] - self.Xs_mean[ti], factors_to_tensor(self.Xs_factors[ti]) |
| 160 | + ) |
| 161 | + self.coef_[:, a] = lstsq(self.factor_T, self.Y_factors[0][:, a], rcond=-1)[ |
| 162 | + 0 |
| 163 | + ] |
| 164 | + Y -= self.factor_T @ self.coef_[:, [a]] @ self.Y_factors[1][:, [a]].T |
| 165 | + self.R2Y[a] = calcR2X(oY - self.Y_mean, self.predict(oXs) - self.Y_mean) |
| 166 | + # Y -= T b q' = T pinv(T) u q' = T lstsq(T, u) q'; b = inv(T'T) T' u = pinv(T) u |
| 167 | + |
| 168 | + def predict(self, Xs): |
| 169 | + Xs = [X.copy() for X in Xs] |
| 170 | + assert len(Xs) == self.Xs_len |
| 171 | + Xs_hasMiss = [np.any(np.isnan(X)) for X in Xs] |
| 172 | + Xs_miss = [np.isnan(X) for X in Xs] |
| 173 | + for ti, X in enumerate(Xs): |
| 174 | + if self.Xs_shape[ti][1:] != X.shape[1:]: |
| 175 | + raise ValueError( |
| 176 | + f"Training X[{ti}] has shape {self.Xs_shape[ti]}, while the new X has shape {X.shape}" |
| 177 | + ) |
| 178 | + Xs[ti] -= self.Xs_mean[ti] |
| 179 | + X_projection = np.zeros((Xs[0].shape[0], self.n_components)) |
| 180 | + for a in range(self.n_components): |
| 181 | + X_projection[:, a] = np.nanmean( |
| 182 | + [ |
| 183 | + multi_mode_dot( |
| 184 | + Xs[ti], |
| 185 | + [ff[:, a] for ff in self.Xs_factors[ti][1:]], |
| 186 | + range(1, self.Xs_dim[ti]), |
| 187 | + ) |
| 188 | + if not Xs_hasMiss[ti] |
| 189 | + else miss_mmodedot( |
| 190 | + Xs[ti], |
| 191 | + [ff[:, a] for ff in self.Xs_factors[ti][1:]], |
| 192 | + Xs_miss[ti], |
| 193 | + ) |
| 194 | + for ti in range(self.Xs_len) |
| 195 | + ], |
| 196 | + axis=0, |
| 197 | + ) |
| 198 | + for ti, X in enumerate(Xs): |
| 199 | + X -= factors_to_tensor( |
| 200 | + [X_projection[:, [a]]] |
| 201 | + + [ff[:, [a]] for ff in self.Xs_factors[ti][1:]] |
| 202 | + ) |
| 203 | + return X_projection @ self.coef_ @ self.Y_factors[1].T + self.Y_mean |
| 204 | + |
| 205 | + def transform(self, Xs, Y=None): |
| 206 | + Xs = [X.copy() for X in Xs] |
| 207 | + assert len(Xs) == self.Xs_len |
| 208 | + Xs_hasMiss = [np.any(np.isnan(X)) for X in Xs] |
| 209 | + Xs_miss = [np.isnan(X) for X in Xs] |
| 210 | + for ti, X in enumerate(Xs): |
| 211 | + if self.Xs_shape[ti][1:] != X.shape[1:]: |
| 212 | + raise ValueError( |
| 213 | + f"Training X[{ti}] has shape {self.Xs_shape[ti]}, while the new X has shape {X.shape}" |
| 214 | + ) |
| 215 | + Xs[ti] -= self.Xs_mean[ti] |
| 216 | + X_scores = np.zeros((Xs[0].shape[0], self.n_components)) |
| 217 | + |
| 218 | + for a in range(self.n_components): |
| 219 | + Ts = [ |
| 220 | + multi_mode_dot( |
| 221 | + Xs[ti], |
| 222 | + [ff[:, a] for ff in self.Xs_factors[ti][1:]], |
| 223 | + range(1, self.Xs_dim[ti]), |
| 224 | + ) |
| 225 | + if not Xs_hasMiss[ti] |
| 226 | + else miss_mmodedot( |
| 227 | + Xs[ti], [ff[:, a] for ff in self.Xs_factors[ti][1:]], Xs_miss[ti] |
| 228 | + ) |
| 229 | + for ti in range(self.Xs_len) |
| 230 | + ] |
| 231 | + |
| 232 | + X_scores[:, a] = np.nanmean(Ts, axis=0) |
| 233 | + for ti, X in enumerate(Xs): |
| 234 | + X -= factors_to_tensor( |
| 235 | + [X_scores[:, [a]]] + [ff[:, [a]] for ff in self.Xs_factors[ti][1:]] |
| 236 | + ) |
| 237 | + |
| 238 | + if Y is not None: |
| 239 | + Y = Y.copy() |
| 240 | + # Check on the shape of Y |
| 241 | + if (Y.ndim != 1) and (Y.ndim != 2): |
| 242 | + raise ValueError("Only a matrix (2-mode tensor) Y is allowed.") |
| 243 | + if Y.ndim == 1: |
| 244 | + Y = Y.reshape((-1, 1)) |
| 245 | + if self.Y_shape[1:] != Y.shape[1:]: |
| 246 | + raise ValueError( |
| 247 | + f"Training Y has shape {self.Y_shape}, while the new Y has shape {Y.shape}" |
| 248 | + ) |
| 249 | + |
| 250 | + Y -= self.Y_mean |
| 251 | + Y_scores = np.zeros((Y.shape[0], self.n_components)) |
| 252 | + for a in range(self.n_components): |
| 253 | + Y_scores[:, a] = Y @ self.Y_factors[1][:, a] |
| 254 | + Y -= X_scores @ self.coef_[:, [a]] @ self.Y_factors[1][:, [a]].T |
| 255 | + return X_scores, Y_scores |
| 256 | + |
| 257 | + return X_scores |
| 258 | + |
| 259 | + def Xs_reconstructed(self): |
| 260 | + return [ |
| 261 | + factors_to_tensor(self.Xs_factors[ti]) + self.Xs_mean[ti] |
| 262 | + for ti in range(self.Xs_len) |
| 263 | + ] |
0 commit comments