Skip to content

Commit 16b8427

Browse files
committed
Merge branch 'Jimmy-adding-customBasis' into niharika-krithika-mohammad-regionConstraints
2 parents 9cb0a4a + 960d971 commit 16b8427

File tree

5 files changed

+1064
-8
lines changed

5 files changed

+1064
-8
lines changed

examples/basis_comparison-Copy1.ipynb

Lines changed: 962 additions & 0 deletions
Large diffs are not rendered by default.

pysensors/basis/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ._identity import Identity
22
from ._random_projection import RandomProjection
33
from ._svd import SVD
4+
from ._custom import Custom
45

5-
6-
__all__ = ["Identity", "SVD", "RandomProjection"]
6+
__all__ = ["Identity", "SVD", "RandomProjection","Custom"]

pysensors/basis/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def matrix_representation(self, n_basis_modes=None, copy=False):
4343
n_basis_modes = self._validate_input(n_basis_modes)
4444

4545
if copy:
46-
return self.basis_matrix_[:, :n_basis_modes].copy()
46+
return self.basis_matrix_[:, :n_basis_modes].copy()#self.original_data @
4747
else:
48-
return self.basis_matrix_[:, :n_basis_modes]
48+
return self.basis_matrix_[:, :n_basis_modes]#self.original_data @
4949

5050
def _validate_input(self, n_basis_modes):
5151
"""

pysensors/basis/_custom.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
custom mode basis class.
3+
"""
4+
from ._base import InvertibleBasis
5+
from ._base import MatrixMixin
6+
7+
class Custom(InvertibleBasis, MatrixMixin):
8+
"""
9+
<<<<<<< HEAD
10+
Use a custom transformation to maps input features to
11+
=======
12+
Generate a custom transformation which maps input features to
13+
>>>>>>> 1ccd23fafed0ca39dac6cde204efa228d133df1c
14+
custom modes.
15+
16+
Assumes the data has already been centered (to have mean 0).
17+
18+
Parameters
19+
----------
20+
n_basis_modes : int, optional (default 10)
21+
Number of basis modes to retain. Cannot be larger than
22+
the number of features ``n_features``, or the number of examples
23+
``n_examples``.
24+
U: The custom basis matrix
25+
26+
Attributes
27+
----------
28+
basis_matrix_ : numpy ndarray, shape (n_features, n_basis_modes)
29+
The top n_basis_modes left singular vectors of the training data.
30+
31+
"""
32+
33+
def __init__(self, U, n_basis_modes=10, **kwargs):
34+
if isinstance(n_basis_modes, int) and n_basis_modes > 0:
35+
super(Custom, self).__init__()#n_components=n_basis_modes, **kwargs
36+
self._n_basis_modes = n_basis_modes
37+
self.custom_basis_ = U
38+
else:
39+
raise ValueError("n_basis_modes must be a positive integer.")
40+
41+
def fit(self, X):
42+
"""
43+
Parameters
44+
----------
45+
X : array-like, shape (n_samples, n_features)
46+
The training data.
47+
48+
Returns
49+
-------
50+
self : instance
51+
"""
52+
# self.basis_matrix_ = self.custom_basis_[:,: self.n_basis_modes] @ self.custom_basis_[:,: self.n_basis_modes].T @ X[: self.n_basis_modes, :].T.copy()
53+
# self.basis_matrix_ = self.custom_basis_ @ self.custom_basis_.T @ X[: self.n_basis_modes, :].T.copy()
54+
self.basis_matrix_ = self.custom_basis_[:,:self.n_basis_modes]
55+
# self.basis_matrix_ = (X @ self.custom_basis_[:,:self.n_basis_modes] @ self.custom_basis_[:,:self.n_basis_modes].T)[:self.n_basis_modes,:].T
56+
57+
# self.basis_matrix_ = ((X @ self.custom_basis_).T)[:,:self.n_basis_modes]
58+
# self.basis_matrix_ = ((X @ self.custom_basis_ @ self.custom_basis_.T).T)[:,:self.n_basis_modes]
59+
return self
60+
61+
def matrix_inverse(self, n_basis_modes=None):
62+
"""
63+
Get the inverse matrix mapping from measurement space to
64+
coordinates with respect to the basis.
65+
66+
Note that this is not the inverse of the matrix returned by
67+
``self.matrix_representation``. It is the (pseudo) inverse of
68+
the matrix whose columns are the basis modes.
69+
70+
Parameters
71+
----------
72+
n_basis_modes : positive int, optional (default None)
73+
Number of basis modes to be used to compute inverse.
74+
75+
Returns
76+
-------
77+
B : numpy ndarray, shape (n_basis_modes, n_features)
78+
The inverse matrix.
79+
"""
80+
n_basis_modes = self._validate_input(n_basis_modes)
81+
82+
return self.basis_matrix_[:, :n_basis_modes].T
83+
84+
@property
85+
def n_basis_modes(self):
86+
"""Number of basis modes."""
87+
return self._n_basis_modes
88+
89+
@n_basis_modes.setter
90+
def n_basis_modes(self, n_basis_modes):
91+
self._n_basis_modes = n_basis_modes
92+
self.n_components = n_basis_modes

pysensors/basis/_identity.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from warnings import warn
77

88
from numpy import identity
9+
import numpy as np
910
from sklearn.base import BaseEstimator
1011
from sklearn.utils import check_array
1112

@@ -52,7 +53,8 @@ def fit(self, X):
5253
-------
5354
self : instance
5455
"""
55-
56+
# Store original data
57+
self.original_data = X
5658
# Note that we take a transpose here, so columns correspond to examples
5759
if self.n_basis_modes is None:
5860
self.basis_matrix_ = check_array(X).T.copy()
@@ -65,10 +67,10 @@ def fit(self, X):
6567
)
6668
)
6769

68-
self.basis_matrix_ = check_array(X)[: self.n_basis_modes, :].T.copy()
70+
self.basis_matrix_ = np.eye(X.shape[1])[:,:self.n_basis_modes] #check_array(X)[: self.n_basis_modes, :].T.copy()
6971

70-
if self.n_basis_modes < X.shape[0]:
71-
warn(f"Only the first {self.n_basis_modes} examples were retained.")
72+
# if self.n_basis_modes < X.shape[0]:
73+
# warn(f"Only the first {self.n_basis_modes} examples were retained.")
7274
return self
7375

7476
def matrix_inverse(self, n_basis_modes=None):

0 commit comments

Comments
 (0)