Skip to content

Commit

Permalink
Polished up Example 15. Fixed some small errors here and there, inclu…
Browse files Browse the repository at this point in the history
…ding: error in the GeneralizedLibrary that was not allowing for feature_names to be passed to this library properly. Added tests for GeneralizedLibrary with feature_names parameter passed or not passed.
  • Loading branch information
Alan Kaptanoglu authored and Alan Kaptanoglu committed Nov 29, 2021
1 parent 13a7143 commit 8cbaa74
Show file tree
Hide file tree
Showing 5 changed files with 1,179 additions and 297 deletions.
1,301 changes: 1,064 additions & 237 deletions examples/15_pysindy_lectures.ipynb

Large diffs are not rendered by default.

96 changes: 50 additions & 46 deletions examples/1_feature_overview.ipynb

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions pysindy/feature_library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,25 @@ def get_feature_names(self, input_features=None):
feature_names = list()
for i in range(len(self.libraries_)):
lib_i = self.libraries_[i]
input_features_i = [
"x%d" % k for k in np.unique(self.inputs_per_library_[i, :])
]
if input_features is None:
input_features_i = [
"x%d" % k for k in np.unique(self.inputs_per_library_[i, :])
]
else:
input_features_i = np.asarray(input_features)[
np.unique(self.inputs_per_library_[i, :])
].tolist()
lib_i_feat_names = lib_i.get_feature_names(input_features_i)
for j in range(i + 1, len(self.libraries_)):
lib_j = self.libraries_[j]
# Need to overwrite input feature names
input_features_j = [
"x%d" % k for k in np.unique(self.inputs_per_library_[j, :])
]
if input_features is None:
input_features_j = [
"x%d" % k for k in np.unique(self.inputs_per_library_[j, :])
]
else:
input_features_j = np.asarray(input_features)[
np.unique(self.inputs_per_library_[j, :])
].tolist()
lib_j_feat_names = lib_j.get_feature_names(input_features_j)
feature_names += self._name_combinations(
lib_i_feat_names, lib_j_feat_names
Expand Down
22 changes: 15 additions & 7 deletions pysindy/feature_library/generalized_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,22 @@ def get_feature_names(self, input_features=None):
feature_names = list()
for i, lib in enumerate(self.libraries_):
if i < self.inputs_per_library_.shape[0]:
input_features = [
"x%d" % k for k in np.unique(self.inputs_per_library_[i, :])
]
if input_features is None:
input_features_i = [
"x%d" % k for k in np.unique(self.inputs_per_library_[i, :])
]
else:
input_features_i = np.asarray(input_features)[
np.unique(self.inputs_per_library_[i, :])
].tolist()
else:
# Tensor libraries need all the inputs and then internally
# handle the subsampling of the input variables
input_features = [
"x%d" % k for k in range(self.inputs_per_library_.shape[0])
]
feature_names += lib.get_feature_names(input_features)
if input_features is None:
input_features_i = [
"x%d" % k for k in range(self.inputs_per_library_.shape[1])
]
else:
input_features_i = input_features
feature_names += lib.get_feature_names(input_features_i)
return feature_names
34 changes: 34 additions & 0 deletions test/feature_library/test_feature_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ def test_generalized_library(data_lorenz):
model.get_feature_names()
assert len(model.get_feature_names()) == 24

# Repeat with feature names
feature_names = ["x", "y", "z"]
model = SINDy(
optimizer=sindy_opt, feature_library=sindy_library, feature_names=feature_names
)
model.fit(x, t=t)
model.print()
model.get_feature_names()
assert len(model.get_feature_names()) == 24

# Next try with tensor libraries but still all the input variables
sindy_library = GeneralizedLibrary(
[poly_library, fourier_library, custom_library], tensor_array=tensor_array
Expand All @@ -501,6 +511,18 @@ def test_generalized_library(data_lorenz):
# 24 + (9 * 6) = 54 + (9 * 9) = 81
assert len(model.get_feature_names()) == 159

# Repeat with feature_names
sindy_library = GeneralizedLibrary(
[poly_library, fourier_library, custom_library], tensor_array=tensor_array
)
model = SINDy(
optimizer=sindy_opt, feature_library=sindy_library, feature_names=feature_names
)
model.fit(x, t=t)
model.print()
# 24 + (9 * 6) = 54 + (9 * 9) = 81
assert len(model.get_feature_names()) == 159

sindy_library = GeneralizedLibrary(
[poly_library, fourier_library, custom_library],
tensor_array=tensor_array,
Expand All @@ -513,6 +535,18 @@ def test_generalized_library(data_lorenz):
model.fit(x, t=t)
assert len(model.get_feature_names()) == 29

# Repeat with feature names
sindy_library = GeneralizedLibrary(
[poly_library, fourier_library, custom_library],
tensor_array=tensor_array,
inputs_per_library=inputs_per_library,
)
model = SINDy(
optimizer=sindy_opt, feature_library=sindy_library, feature_names=feature_names
)
model.fit(x, t=t)
assert len(model.get_feature_names()) == 29


# Helper function for testing PDE libraries
def pde_library_helper(library, u_flattened, u_dot_flattened, coef_first_dim):
Expand Down

0 comments on commit 8cbaa74

Please sign in to comment.