Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify derivatives in pysindy.py #185

Merged
merged 115 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
115 commits
Select commit Hold shift + click to select a range
451fa4c
PDELibrary version now works that replaces the SINDyPILibrary object.…
Jun 5, 2022
df0472a
Think I fixed small bug where checking for spatiotemporal_grid if Wea…
Jun 5, 2022
b97502d
CLN: adapt all trajectories to multiple trajectories
Jacob-Stevens-Haas May 20, 2022
1c0f779
CLN: Indirect all smoothing/differentiation/udot_integration
Jacob-Stevens-Haas May 20, 2022
d54f3c7
CLN: Indirect all smoothing/differentiation/udot_integration
Jacob-Stevens-Haas May 20, 2022
783377a
CLN: Move all calculate_trajectory inside _process_multiple_trajectories
Jacob-Stevens-Haas May 20, 2022
dc9f013
CLN: Break apart validate_input (WIP)
Jacob-Stevens-Haas May 24, 2022
c696458
CLN: Adapt scalar t to case when t is a Sequence
Jacob-Stevens-Haas May 24, 2022
9df1a31
CLN: Add PDELibrary.validate_input() (out of order)
Jacob-Stevens-Haas May 24, 2022
19f077d
CLN: Combine all calls to feature_library.differentiation_method
Jacob-Stevens-Haas May 24, 2022
9364b2f
CLN: Move adapt_to_multiple_trajectories to pysindy.py
Jacob-Stevens-Haas May 24, 2022
234706e
TST: Add test to ensure differentiate returns expected data type & shape
Jacob-Stevens-Haas May 24, 2022
8c12868
ENH: Add AxesArray type that stores the semantic meaning of each axis.
Jacob-Stevens-Haas May 27, 2022
0504a47
CLN: Move AxesArray to feature_library
Jacob-Stevens-Haas May 28, 2022
15155c8
TST: Correct error type for multiple_trajectory but x/u not list.
Jacob-Stevens-Haas May 28, 2022
9b6f79f
TST: Change x type in test_fit_multiple_trajectores() mismatched x, u
Jacob-Stevens-Haas May 28, 2022
8f308e8
TST: Make AxesArray raise AxesWarning and stop it from failing tests.
Jacob-Stevens-Haas May 28, 2022
7404090
ENH: Add DefaultShapedInputsMixin to handle reshaping data for sampling
Jacob-Stevens-Haas May 28, 2022
d4f9a18
CLN: Remove return_array param used for implicit shaping
Jacob-Stevens-Haas May 28, 2022
3a37562
CLN: Remove more calls to utils.validate_input
Jacob-Stevens-Haas May 28, 2022
c03ab9b
CLN: Add workflow files to gitignore
Jacob-Stevens-Haas May 28, 2022
9677969
BUG: Refactor validation and array axes comprehension out of fit()
Jacob-Stevens-Haas May 29, 2022
2d9d260
TST: Soften "test_differentiate_returns_same_data_type"
Jacob-Stevens-Haas May 29, 2022
8def374
TST: test_pysindy.test_t_default converts & checks array objects.
Jacob-Stevens-Haas May 29, 2022
4b0ea6f
TST: Fix derivative tests expecting old implicit shape conversion.
Jacob-Stevens-Haas May 29, 2022
a1cb8b8
CLN: Remove reshaping from _check_control_shape
Jacob-Stevens-Haas May 29, 2022
e63245b
ENH: Make predict() return an AxesArray
Jacob-Stevens-Haas May 29, 2022
59b9abb
BUG: Remove implicit shaping in pde calc_trajectory
Jacob-Stevens-Haas May 29, 2022
5bbcecc
BUG: Expand __array_ufunc__ for scalars and concat for array-like args
Jacob-Stevens-Haas May 29, 2022
c1959b7
BUG: deleted a duplicate parameter in test_pde_library_bad_parameters()
Jacob-Stevens-Haas May 29, 2022
df06496
ENH: Make WeakPDELibrary handle AxesArray objects
Jacob-Stevens-Haas May 30, 2022
26bdd7f
CLN: Replace iteration with reduce
Jacob-Stevens-Haas Jun 1, 2022
cd1cc12
ENH: Make PDELibrary.spatial_grid an AxesArray
Jacob-Stevens-Haas Jun 1, 2022
11908cb
CLN: Move AxesArray to utils
Jacob-Stevens-Haas Jun 1, 2022
c28e632
PRJ: Add black to dev requirements.
Jacob-Stevens-Haas Jun 1, 2022
bdf929a
BUG: zero-order derivatives in PDELibrary
Jacob-Stevens-Haas Jun 1, 2022
5e7428b
ENH: Change drop_nan_rows to drop_nan_samples
Jacob-Stevens-Haas Jun 2, 2022
3e1d588
ENH: move concat_sample_axis later in execution
Jacob-Stevens-Haas Jun 2, 2022
3bf0bd3
CLN: remove warning/error in score() since it calls predict()
Jacob-Stevens-Haas Jun 2, 2022
3466ed5
CLN: move ifs inside loop for ensemble fitting
Jacob-Stevens-Haas Jun 2, 2022
5fe611b
CLN: grouped together common library_ensemble and ensemble logic (WIP)
Jacob-Stevens-Haas Jun 2, 2022
ef1db79
CLN: Remove uncovered code when PDELibrary.spatial_grid is None
Jacob-Stevens-Haas Jun 3, 2022
9d39f18
CLN: make concat_sample_axis a function, not method
Jacob-Stevens-Haas Jun 3, 2022
5b9aae2
CLN: Delete WeakPDELibrary conditional in predict()
Jacob-Stevens-Haas Jun 3, 2022
7c06177
CLN: Move calls to ax_time_to_ax_sample out of process_mult_trajectories
Jacob-Stevens-Haas Jun 3, 2022
738bf77
CLN: Begin breaking apart concat_sample_axis()
Jacob-Stevens-Haas Jun 3, 2022
a4466b2
CLN: identify n_control_features earlier
Jacob-Stevens-Haas Jun 3, 2022
9f9e87a
CLN: Fully separate concat_sample_axis from ax_spatial_to_ax_time
Jacob-Stevens-Haas Jun 3, 2022
64f7e64
CLN: Remove concat_sample_axis.
Jacob-Stevens-Haas Jun 4, 2022
5e28740
ENH: Move concatenation and reshaping after drop_random_rows (WIP)
Jacob-Stevens-Haas Jun 4, 2022
56f281e
ENH: Add EnsembleOptimizer
Jacob-Stevens-Haas Jun 6, 2022
e6878fc
ENH: Add SampleConcatter step to SINDy Pipeline.
Jacob-Stevens-Haas Jun 6, 2022
7cb615e
ENH: Give WeakPDELibrary and GeneralizedLibrary a spatial_grid
Jacob-Stevens-Haas Jun 7, 2022
0ded480
BUG: Fix adding zero terms in library ensemble to coef_list
Jacob-Stevens-Haas Jun 7, 2022
2b93d34
new branch for Jake's changes. Update to PolynomialLibrary for list o…
znicolaou Jun 7, 2022
787f4d1
updated ensembling
znicolaou Jun 7, 2022
f43833a
updated weak implicit_terms
znicolaou Jun 7, 2022
0e6ed24
updated notebook
znicolaou Jun 7, 2022
a350dad
BUG: relabel x time axis as sample __after__ joining with controls
Jacob-Stevens-Haas Jun 8, 2022
624b7bf
BUG: Add decorator for library transform to accept list of arrays or …
Jacob-Stevens-Haas Jun 8, 2022
f9ed6b9
BUG: Make concat_sample_axis actually concat
Jacob-Stevens-Haas Jun 8, 2022
fe70b5a
updated libraries
znicolaou Jun 8, 2022
30d3bdf
merge jake's code
znicolaou Jun 8, 2022
3861b04
BUG: Make concat_sample_axis actually concat + flatten spatial axis
Jacob-Stevens-Haas Jun 8, 2022
57d8b40
BUG: Allow passing ODE data w/spatial axes to polynomial library
Jacob-Stevens-Haas Jun 8, 2022
e6a24c4
updated generalized
znicolaou Jun 8, 2022
9232f3c
finished all ode libraries (except sindy_pi); removed use of n_coord,…
znicolaou Jun 8, 2022
82d2d13
Merge remote-tracking branch 'jake/simplify-fitting' into simplify-fi…
znicolaou Jun 8, 2022
7944485
fixed linting
znicolaou Jun 8, 2022
4052fc4
really fixed linting
znicolaou Jun 8, 2022
049871e
BUG: Make ufunc.reduce work on AxesArrays
Jacob-Stevens-Haas Jun 9, 2022
9dce514
BUG: Make PolynomialLibrary work on sparse inputs
Jacob-Stevens-Haas Jun 9, 2022
d6a332d
first pass at pde libraries, and some test debugging
znicolaou Jun 9, 2022
449e7e5
updated sindypi
znicolaou Jun 9, 2022
be9db2e
fixed linting
znicolaou Jun 9, 2022
e3dc2ce
fixed predict shaping
znicolaou Jun 9, 2022
06d479b
BUG: Fix deeptime API for new SampleConcatter step
Jacob-Stevens-Haas Jun 10, 2022
c149b29
TST: Add EnsembleOptimizer test
Jacob-Stevens-Haas Jun 10, 2022
d5e5263
BUG: EnsembleOptimizer reject n=0 models
Jacob-Stevens-Haas Jun 10, 2022
2f07046
BUG: Add backwards compatability for library ensembling.
Jacob-Stevens-Haas Jun 10, 2022
9d0f768
TST: Removed accidentally-inserted parameter
Jacob-Stevens-Haas Jun 11, 2022
b76fc23
TST: Pass test_generalized_library_bad_parameters library objects
Jacob-Stevens-Haas Jun 12, 2022
965be47
BUG: Don't preallocate array for generalized library transform
Jacob-Stevens-Haas Jun 13, 2022
c9804c9
TST: correct spatiotemporal grid for data_1d_random_pde
Jacob-Stevens-Haas Jun 13, 2022
5925a74
BUG: Protect reshape_samples_to_spatial_grid() from SINDyPI.
Jacob-Stevens-Haas Jun 13, 2022
a99d0ea
TST: :boom: Reflect that ensembling only makes n_models models
Jacob-Stevens-Haas Jun 13, 2022
6cb59c0
TST: Pass correct shape of u_dot for WeakLibrary
Jacob-Stevens-Haas Jun 13, 2022
8266ae0
moved drop_nan_rows to sindy_optimizer and removed weak form testing …
znicolaou Jun 13, 2022
0c7504b
fixed ensembling normalize_trajectories
znicolaou Jun 13, 2022
0d68cd9
fixed notebook 15 sindypi
znicolaou Jun 14, 2022
c758f8a
Merge pull request #2 from dynamicslab/simplify-fitting
Jacob-Stevens-Haas Jun 14, 2022
2a5cc6e
NBK: uncomment # import sympy
Jacob-Stevens-Haas Jun 14, 2022
5688f8e
NBK: Remove out-of-order cell in NB 13
Jacob-Stevens-Haas Jun 14, 2022
7165a5a
BUG: Prevent ZeroDivisionError when printing every n=0 iterations
Jacob-Stevens-Haas Jun 14, 2022
ea0b274
Merge branch 'simplify-fitting' of https://github.com/Jacob-Stevens-H…
Jacob-Stevens-Haas Jun 14, 2022
2ba803e
BUG: Add SampleConcatter to deeptime SINDyModel pipeline
Jacob-Stevens-Haas Jun 14, 2022
5d34052
BUG: Prevent infinite recursion in some ufuncs where out= set
Jacob-Stevens-Haas Jun 15, 2022
fa0a5ba
added nan score test
znicolaou Jun 15, 2022
0167803
started mkdv notebook example
znicolaou Jun 15, 2022
2adb770
NKB: Prevent index error with mis-set index length in NB 7 plotting
Jacob-Stevens-Haas Jun 15, 2022
1787510
TST: Test drop_nan_samples works in score/predict
Jacob-Stevens-Haas Jun 15, 2022
53dfb7e
completed sindypi pde example, and debugged pde score
znicolaou Jun 17, 2022
ecf4f97
Merge branch 'master' into simplify-fitting
Jacob-Stevens-Haas Jun 28, 2022
a6bd451
CLN: Organization improvements
Jacob-Stevens-Haas Jun 28, 2022
6712504
CLN: Simplify stgrid initialization in PDELibrary
Jacob-Stevens-Haas Jun 28, 2022
fd93037
TST: Revert workaround for test_almost_equal(AxesArray)
Jacob-Stevens-Haas Jun 28, 2022
b7ba2d9
BUG: Make AxesArray.n_spatial (&c) work
Jacob-Stevens-Haas Jun 28, 2022
1fb3661
CLN: Simplify AxesArray patterns
Jacob-Stevens-Haas Jun 28, 2022
04e3003
CLN: make comprehend_axes module function in axes.py
Jacob-Stevens-Haas Jun 28, 2022
6648670
ENH: Improve errors/comments on ax_time_to_ax_sample
Jacob-Stevens-Haas Jun 29, 2022
3ba7473
CLN: Defer issue #220
Jacob-Stevens-Haas Jun 29, 2022
21e933b
ENH: Upgrade warn to error when concatenating misaligned AxesArray
Jacob-Stevens-Haas Jun 29, 2022
1128a5a
BUG: Fix deriv copy in PDELibrary.transform()
Jacob-Stevens-Haas Jul 1, 2022
40cb00a
CLN: remove commented out code
Jacob-Stevens-Haas Jul 1, 2022
f9ebbbd
CLN: Remove unused and unnecessary functions.
Jacob-Stevens-Haas Jul 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
build
dist
*.egg-info
commit-msg
todo

# Environments
.env
Expand Down
63 changes: 31 additions & 32 deletions examples/10_PDEFIND_examples.ipynb

Large diffs are not rendered by default.

231 changes: 45 additions & 186 deletions examples/12_weakform_SINDy_examples.ipynb

Large diffs are not rendered by default.

149 changes: 91 additions & 58 deletions examples/13_ensembling.ipynb

Large diffs are not rendered by default.

158 changes: 92 additions & 66 deletions examples/15_pysindy_lectures.ipynb

Large diffs are not rendered by default.

216 changes: 176 additions & 40 deletions examples/1_feature_overview.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions examples/7_plasma_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
" \n",
" if feature_names is None:\n",
" feature_names = [f\"f{k}\" for k in range(coefficients.shape[1])]\n",
"\n",
" max_ind = len(feature_names)\n",
" \n",
" with sns.axes_style(style=\"white\", rc={\"axes.facecolor\": (0, 0, 0, 0)}): \n",
" if ax is None:\n",
Expand All @@ -269,7 +269,7 @@
" heatmap_args.update(**heatmap_kws)\n",
"\n",
" sns.heatmap(\n",
" coefficients.T,\n",
" coefficients[:, :max_ind].T,\n",
" **heatmap_args\n",
" )\n",
"\n",
Expand Down Expand Up @@ -1609,7 +1609,7 @@
"fig.show()\n",
"\n",
"X, Y = np.meshgrid(\n",
" np.linspace(r_scan[0], r_scan[-1] + 1, r_scan[-1] - 1) - 0.5,\n",
" np.linspace(r_scan[0], r_scan[-1] + 1, r_length) - 0.5,\n",
" thresholds,\n",
" indexing=\"ij\",\n",
")\n",
Expand Down Expand Up @@ -1853,7 +1853,7 @@
"\n",
"fig, axs = plt.subplots(2, 1, figsize=(14, 10))\n",
"X, Y = np.meshgrid(\n",
" np.linspace(r_scan[0], r_scan[-1] + 1, r_scan[-1] - 1) - 0.5,\n",
" np.linspace(r_scan[0], r_scan[-1] + 1, r_length) - 0.5,\n",
" thresholds,\n",
" indexing=\"ij\",\n",
")\n",
Expand Down
1,033 changes: 738 additions & 295 deletions examples/9_sindypi_with_sympy.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ exclude = '''
\.git
| \.mypy_cache
| \.venv
| .vscode
| version.py
| build
| dist
| env
)/
'''

Expand Down
4 changes: 3 additions & 1 deletion pysindy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from . import deeptime
from . import utils
from .pysindy import SINDy
from .pysindy import AxesArray
from .differentiation import BaseDifferentiation
from .differentiation import FiniteDifference
from .differentiation import SpectralDerivative
Expand All @@ -36,9 +37,10 @@
from .optimizers import STLSQ
from .optimizers import SINDyPI
from .optimizers import TrappingSR3
from .optimizers import EnsembleOptimizer


__all__ = ["SINDy"]
__all__ = ["SINDy", "AxesArray"]
__all__.extend(differentiation.__all__)
__all__.extend(feature_library.__all__)
__all__.extend(optimizers.__all__)
Expand Down
14 changes: 10 additions & 4 deletions pysindy/deeptime/deeptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sklearn.utils.validation import check_is_fitted

from ..pysindy import SINDy
from ..utils import SampleConcatter


class SINDyEstimator(SINDy):
Expand Down Expand Up @@ -100,7 +101,7 @@ def fit(self, x, **kwargs):
super(SINDyEstimator, self).fit(x, **kwargs)
self._model = SINDyModel(
feature_library=self.model.steps[0][1],
optimizer=self.model.steps[1][1],
optimizer=self.model.steps[-1][1],
feature_names=self.feature_names,
t_default=self.t_default,
discrete_time=self.discrete_time,
Expand Down Expand Up @@ -134,10 +135,11 @@ class SINDyModel(SINDy):
Implementation of SINDy conforming to the API of a Deeptime
`Model <https://deeptime-ml.github.io/api/generated/deeptime.base.Model.html>`_.

The model is represented as a Scikit-learn pipeline object with two steps:
The model is represented as a Scikit-learn pipeline object with three steps:
1. Map the raw input data to nonlinear features according to the selected
``feature_library``
2. Multiply the nonlinear features with a coefficient matrix encapuslated
2. Reshape the data from input shape to an optimization problem
3. Multiply the nonlinear features with a coefficient matrix encapuslated
in ``optimizer``.

This class expects the feature library and optimizer to already be fit
Expand Down Expand Up @@ -208,7 +210,11 @@ def __init__(
check_is_fitted(feature_library)
check_is_fitted(optimizer)

steps = [("features", feature_library), ("model", optimizer)]
steps = [
("features", feature_library),
("shaping", SampleConcatter()),
("model", optimizer),
]
self.model = Pipeline(steps)

if float(__version__[:3]) >= 1.0:
Expand Down
3 changes: 0 additions & 3 deletions pysindy/differentiation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

from sklearn.base import BaseEstimator

from ..utils.base import validate_input


class BaseDifferentiation(BaseEstimator):
"""
Expand Down Expand Up @@ -48,5 +46,4 @@ def _differentiate(self, x, t=1):
raise NotImplementedError

def __call__(self, x, t=1):
x = validate_input(x)
return self._differentiate(x, t)
9 changes: 2 additions & 7 deletions pysindy/differentiation/sindy_derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"""
from derivative import dxdt
from numpy import arange
from sklearn.base import BaseEstimator

from ..utils.base import validate_input
from .base import BaseDifferentiation


class SINDyDerivative(BaseEstimator):
class SINDyDerivative(BaseDifferentiation):
"""
Wrapper class for differentiation classes from the :doc:`derivative:index` package.
This class is meant to provide all the same functionality as the
Expand Down Expand Up @@ -72,7 +71,3 @@ def _differentiate(self, x, t=1):
t = arange(x.shape[0]) * t

return dxdt(x, t, axis=0, **self.kwargs)

def __call__(self, x, t=1):
x = validate_input(x, t=t)
return self._differentiate(x, t)
Loading