From f4c74f55368a9fb8b2061ab516bd4dbcbf48a6d5 Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Tue, 20 Feb 2024 22:34:02 -0800 Subject: [PATCH 1/8] ENH: AdIntroduces differentiation for multidimensional arrays in SINDyDerivative --- pysindy/differentiation/sindy_derivative.py | 17 +++++++++++++---- test/test_differentiation.py | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pysindy/differentiation/sindy_derivative.py b/pysindy/differentiation/sindy_derivative.py index 533724b71..69008acb4 100644 --- a/pysindy/differentiation/sindy_derivative.py +++ b/pysindy/differentiation/sindy_derivative.py @@ -3,6 +3,7 @@ Some default values used here may differ from those used in :doc:`derivative:index`. """ +import numpy as np from derivative import methods from numpy import arange @@ -33,7 +34,8 @@ class SINDyDerivative(BaseDifferentiation): for acceptable keywords. """ - def __init__(self, save_smooth=True, **kwargs): + def __init__(self, axis=0, save_smooth=True, **kwargs): + self.axis = axis self.kwargs = kwargs self.save_smooth = save_smooth @@ -76,9 +78,16 @@ def _differentiate(self, x, t=1): differentiator = methods[self.kwargs["kind"]]( **{k: v for k, v in self.kwargs.items() if k != "kind"} ) - x_dot = differentiator.d(x, t, axis=0) + x = np.moveaxis(x, self.axis, 0) + x_shape = x.shape + flat_x = x.reshape((t.size, int(x.size / t.size))) + flat_x_dot = differentiator.d(flat_x, t, axis=0) if self.save_smooth: - self.smoothed_x_ = differentiator.x(x, t, axis=0) + self.smoothed_x_ = differentiator.x(flat_x, t, axis=0) else: - self.smoothed_x_ = x + self.smoothed_x_ = flat_x + x_dot = flat_x_dot.reshape(x_shape) + self.smoothed_x_ = self.smoothed_x_.reshape(x_shape) + x_dot = np.moveaxis(x_dot, 0, self.axis) + self.smoothed_x_ = np.moveaxis(self.smoothed_x_, 0, self.axis) return x_dot diff --git a/test/test_differentiation.py b/test/test_differentiation.py index dcafa51ba..92e48984e 100644 --- a/test/test_differentiation.py +++ b/test/test_differentiation.py @@ -418,3 +418,12 @@ def test_centered_difference_noaxis_vs_axis(data_2d_resolved_pde): slow_differences_t, atol=atol, ) + + +def test_multidimensional_differentiation(): + X = np.random.random(size=(10, 100, 2)) + t = np.arange(0, 10, 0.1) + + X_dot = SINDyDerivative(kind="kalman", axis=-2)._differentiate(X, t) + + assert X_dot.shape == X.shape From 406623d310eeda04fac5b537dfcef7df7d46c208 Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Sat, 1 Jun 2024 20:10:29 -0700 Subject: [PATCH 2/8] TST: added test for multidimensional differ array differentiation --- test/test_differentiation.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/test/test_differentiation.py b/test/test_differentiation.py index 92e48984e..c63b047aa 100644 --- a/test/test_differentiation.py +++ b/test/test_differentiation.py @@ -420,10 +420,22 @@ def test_centered_difference_noaxis_vs_axis(data_2d_resolved_pde): ) -def test_multidimensional_differentiation(): - X = np.random.random(size=(10, 100, 2)) - t = np.arange(0, 10, 0.1) - - X_dot = SINDyDerivative(kind="kalman", axis=-2)._differentiate(X, t) - - assert X_dot.shape == X.shape +def test_nd_differentiation(): + t = np.arange(3) + x = np.random.random(size=(2, 3, 2)) + x[1, :, 1] = 1 + xdot_sindy = SINDyDerivative(kind="kalman", axis=-2)._differentiate(x, t) + xdot_fd = FiniteDifference(axis=-2)._differentiate(x, t) + xdot_sindyfd = SINDyDerivative( + kind="finite_difference", k=1, axis=-2 + )._differentiate(x, t) + xdot_sfd = SmoothedFiniteDifference( + axis=-2, smoother_kws={"window_length": 2, "polyorder": 1} + )._differentiate(x, t) + xdot_spectral = SpectralDerivative(axis=-2)._differentiate(x, t) + zeros = np.zeros(3) + np.testing.assert_array_almost_equal(xdot_sindy[1, :, 1], zeros) + np.testing.assert_array_almost_equal(xdot_fd[1, :, 1], zeros) + np.testing.assert_array_almost_equal(xdot_sindyfd[1, :, 1], zeros) + np.testing.assert_array_almost_equal(xdot_sfd[1, :, 1], zeros) + np.testing.assert_array_almost_equal(xdot_spectral[1, :, 1], zeros) From f9bf57f983bc1e5c54b8fc840c45dec1614edddb Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Sat, 1 Jun 2024 20:34:18 -0700 Subject: [PATCH 3/8] CLN: sindyderivative calculate values before assignment --- pysindy/differentiation/sindy_derivative.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pysindy/differentiation/sindy_derivative.py b/pysindy/differentiation/sindy_derivative.py index 69008acb4..151c39276 100644 --- a/pysindy/differentiation/sindy_derivative.py +++ b/pysindy/differentiation/sindy_derivative.py @@ -83,11 +83,11 @@ def _differentiate(self, x, t=1): flat_x = x.reshape((t.size, int(x.size / t.size))) flat_x_dot = differentiator.d(flat_x, t, axis=0) if self.save_smooth: - self.smoothed_x_ = differentiator.x(flat_x, t, axis=0) + smoothed_x_ = differentiator.x(flat_x, t, axis=0) else: - self.smoothed_x_ = flat_x + smoothed_x_ = flat_x x_dot = flat_x_dot.reshape(x_shape) - self.smoothed_x_ = self.smoothed_x_.reshape(x_shape) + smoothed_x_ = smoothed_x_.reshape(x_shape) x_dot = np.moveaxis(x_dot, 0, self.axis) - self.smoothed_x_ = np.moveaxis(self.smoothed_x_, 0, self.axis) + self.smoothed_x_ = np.moveaxis(smoothed_x_, 0, self.axis) return x_dot From ad53a457a3001f805265716e014251a79ec7067c Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Tue, 4 Jun 2024 13:23:00 -0700 Subject: [PATCH 4/8] TST: parametrized the multidimensional array differentiation test --- test/test_differentiation.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/test_differentiation.py b/test/test_differentiation.py index c63b047aa..2f31b98e4 100644 --- a/test/test_differentiation.py +++ b/test/test_differentiation.py @@ -420,22 +420,22 @@ def test_centered_difference_noaxis_vs_axis(data_2d_resolved_pde): ) -def test_nd_differentiation(): +@pytest.mark.parametrize( + "derivative, kwargs", + [ + (SINDyDerivative, {"kind": "kalman", "axis": -2}), + (FiniteDifference, {"axis": -2}), + ( + SmoothedFiniteDifference, + {"axis": -2, "smoother_kws": {"window_length": 2, "polyorder": 1}}, + ), + (SpectralDerivative, {"axis": -2}), + ], +) +def test_nd_differentiation(derivative, kwargs): t = np.arange(3) x = np.random.random(size=(2, 3, 2)) x[1, :, 1] = 1 - xdot_sindy = SINDyDerivative(kind="kalman", axis=-2)._differentiate(x, t) - xdot_fd = FiniteDifference(axis=-2)._differentiate(x, t) - xdot_sindyfd = SINDyDerivative( - kind="finite_difference", k=1, axis=-2 - )._differentiate(x, t) - xdot_sfd = SmoothedFiniteDifference( - axis=-2, smoother_kws={"window_length": 2, "polyorder": 1} - )._differentiate(x, t) - xdot_spectral = SpectralDerivative(axis=-2)._differentiate(x, t) - zeros = np.zeros(3) - np.testing.assert_array_almost_equal(xdot_sindy[1, :, 1], zeros) - np.testing.assert_array_almost_equal(xdot_fd[1, :, 1], zeros) - np.testing.assert_array_almost_equal(xdot_sindyfd[1, :, 1], zeros) - np.testing.assert_array_almost_equal(xdot_sfd[1, :, 1], zeros) - np.testing.assert_array_almost_equal(xdot_spectral[1, :, 1], zeros) + xdot = derivative(**kwargs)._differentiate(x, t) + expected = np.zeros(3) + np.testing.assert_array_almost_equal(xdot[1, :, 1], expected) From d6f76bce25ee88f6c94c7368fda8bc60fef6547c Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Tue, 4 Jun 2024 14:49:44 -0700 Subject: [PATCH 5/8] TST: speeded up test_nmd_differentiation --- test/test_differentiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_differentiation.py b/test/test_differentiation.py index 2f31b98e4..90872241c 100644 --- a/test/test_differentiation.py +++ b/test/test_differentiation.py @@ -423,7 +423,7 @@ def test_centered_difference_noaxis_vs_axis(data_2d_resolved_pde): @pytest.mark.parametrize( "derivative, kwargs", [ - (SINDyDerivative, {"kind": "kalman", "axis": -2}), + (SINDyDerivative, {"kind": "finite_difference", "k": 1, "axis": -2}), (FiniteDifference, {"axis": -2}), ( SmoothedFiniteDifference, From 62163be318780dc45923c16fc981b3a906941271 Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Tue, 4 Jun 2024 14:50:41 -0700 Subject: [PATCH 6/8] updated derivative package dependency --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 18d7e17ce..d6bf35a3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ readme = "README.rst" dependencies = [ "scikit-learn>=1.1, !=1.5.0", - "derivative>=0.5.4", + "derivative>=0.6.1", ] [project.optional-dependencies] From 30188e9cda3eed0843bc2e9f3b142654425f5bd6 Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Tue, 4 Jun 2024 15:16:29 -0700 Subject: [PATCH 7/8] ENH: changed SINDyDerivative to recent derivative changes --- pysindy/differentiation/sindy_derivative.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/pysindy/differentiation/sindy_derivative.py b/pysindy/differentiation/sindy_derivative.py index 151c39276..703194dd7 100644 --- a/pysindy/differentiation/sindy_derivative.py +++ b/pysindy/differentiation/sindy_derivative.py @@ -3,7 +3,6 @@ Some default values used here may differ from those used in :doc:`derivative:index`. """ -import numpy as np from derivative import methods from numpy import arange @@ -78,16 +77,9 @@ def _differentiate(self, x, t=1): differentiator = methods[self.kwargs["kind"]]( **{k: v for k, v in self.kwargs.items() if k != "kind"} ) - x = np.moveaxis(x, self.axis, 0) - x_shape = x.shape - flat_x = x.reshape((t.size, int(x.size / t.size))) - flat_x_dot = differentiator.d(flat_x, t, axis=0) + x_dot = differentiator.d(x, t, axis=self.axis) if self.save_smooth: - smoothed_x_ = differentiator.x(flat_x, t, axis=0) + self.smoothed_x_ = differentiator.x(x, t, axis=self.axis) else: - smoothed_x_ = flat_x - x_dot = flat_x_dot.reshape(x_shape) - smoothed_x_ = smoothed_x_.reshape(x_shape) - x_dot = np.moveaxis(x_dot, 0, self.axis) - self.smoothed_x_ = np.moveaxis(smoothed_x_, 0, self.axis) + self.smoothed_x_ = x return x_dot From 234c3e36795fa2ff49fbc1a2eb9c1b0341f65daf Mon Sep 17 00:00:00 2001 From: Yash Bhangale Date: Sat, 13 Jul 2024 15:07:14 -0700 Subject: [PATCH 8/8] BLD: Updated derivative to 0.6.2 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index d6bf35a3e..ad6099cd0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ readme = "README.rst" dependencies = [ "scikit-learn>=1.1, !=1.5.0", - "derivative>=0.6.1", + "derivative>=0.6.2", ] [project.optional-dependencies]