Skip to content

Commit b7800c5

Browse files
committed
adding custom basis for quick testing
1 parent ab53f3e commit b7800c5

File tree

3 files changed

+969
-2
lines changed

3 files changed

+969
-2
lines changed

examples/basis_comparison-Copy1.ipynb

Lines changed: 885 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/_custom.py

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

0 commit comments

Comments
 (0)