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

[BUG] AttributeError: can't set attribute in "Introduction to SINDy" ipynb #131

Closed
theo-brown opened this issue Oct 21, 2021 · 7 comments
Closed

Comments

@theo-brown
Copy link

I copied the code from examples/2_introduction_to_sindy.ipynb into a Python script. When I ran the code, I encountered an AttributeError when fitting the model.

Reproducing code example:

import numpy as np
import pysindy as ps

t = np.linspace(0, 1, 100)
x = 3 * np.exp(-2 * t)
y = 0.5 * np.exp(t)
X = np.stack((x, y), axis=-1)

differentiation_method = ps.FiniteDifference(order=2)
feature_library = ps.PolynomialLibrary(degree=3)
optimizer = ps.STLSQ(threshold=0.2)

model = ps.SINDy(differentiation_method=differentiation_method,
                 feature_library=feature_library,
                 optimizer=optimizer,
                 feature_names=["x", "y"])
model.fit(X, t=t)

Error message:

Traceback (most recent call last):
  File "..../example_2.py", line 18, in <module>
    model.fit(X, t=t)
  File "..../venv/lib/python3.9/site-packages/pysindy/pysindy.py", line 292, in fit
    self.model.fit(x, x_dot)
  File "..../venv/lib/python3.9/site-packages/sklearn/pipeline.py", line 390, in fit
    Xt = self._fit(X, y, **fit_params_steps)
  File "..../venv/lib/python3.9/site-packages/sklearn/pipeline.py", line 348, in _fit
    X, fitted_transformer = fit_transform_one_cached(
  File "..../venv/lib/python3.9/site-packages/joblib/memory.py", line 349, in __call__
    return self.func(*args, **kwargs)
  File "..../venv/lib/python3.9/site-packages/sklearn/pipeline.py", line 891, in _fit_transform_one
    res = transformer.fit_transform(X, y, **fit_params)
  File "..../venv/lib/python3.9/site-packages/sklearn/base.py", line 847, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)
  File "..../venv/lib/python3.9/site-packages/pysindy/feature_library/polynomial_library.py", line 170, in fit
    self.n_input_features_ = n_features
AttributeError: can't set attribute

PySINDy/Python version information:

>>> import sys, pysindy; print(pysindy.__version__, sys.version)
1.4.3 3.9.6 (default, Sep 25 2021, 20:17:33) 
[GCC 9.3.0]
@akaptano
Copy link
Collaborator

Hi Theo,

Thanks for your interest. See Issues #124 #125 #129. We will fix this bug with a major release in the next few weeks.

@theo-brown
Copy link
Author

Ah, got you! Sorry, I didn't search closed issues for this - maybe leave one open until the bug is fixed?

@akaptano
Copy link
Collaborator

Agreed, #124 is still open but maybe not very descriptive... I'll see if I can edit the title

@hasnainalik
Copy link

hasnainalik commented Aug 5, 2022

I still get the error. Any idea about solving it?

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [4], in <cell line: 2>()
1 model = ps.SINDy(feature_names=["x", "y"])
----> 2 model.fit(X, t=t)

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/pysindy/pysindy.py:292, in SINDy.fit(self, x, t, x_dot, u, multiple_trajectories, unbias, quiet)
289 warnings.filterwarnings(action, category=LinAlgWarning)
290 warnings.filterwarnings(action, category=UserWarning)
--> 292 self.model.fit(x, x_dot)
294 self.n_input_features_ = self.model.steps[0][1].n_input_features_
295 self.n_output_features_ = self.model.steps[0][1].n_output_features_

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/sklearn/pipeline.py:378, in Pipeline.fit(self, X, y, **fit_params)
352 """Fit the model.
353
354 Fit all the transformers one after the other and transform the
(...)
375 Pipeline with fitted steps.
376 """
377 fit_params_steps = self._check_fit_params(**fit_params)
--> 378 Xt = self._fit(X, y, **fit_params_steps)
379 with _print_elapsed_time("Pipeline", self._log_message(len(self.steps) - 1)):
380 if self._final_estimator != "passthrough":

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/sklearn/pipeline.py:336, in Pipeline._fit(self, X, y, **fit_params_steps)
334 cloned_transformer = clone(transformer)
335 # Fit or load from cache the current transformer
--> 336 X, fitted_transformer = fit_transform_one_cached(
337 cloned_transformer,
338 X,
339 y,
340 None,
341 message_clsname="Pipeline",
342 message=self._log_message(step_idx),
343 **fit_params_steps[name],
344 )
345 # Replace the transformer of the step with the fitted
346 # transformer. This is necessary when loading the transformer
347 # from the cache.
348 self.steps[step_idx] = (name, fitted_transformer)

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/joblib/memory.py:349, in NotMemorizedFunc.call(self, *args, **kwargs)
348 def call(self, *args, **kwargs):
--> 349 return self.func(*args, **kwargs)

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/sklearn/pipeline.py:870, in _fit_transform_one(transformer, X, y, weight, message_clsname, message, **fit_params)
868 with _print_elapsed_time(message_clsname, message):
869 if hasattr(transformer, "fit_transform"):
--> 870 res = transformer.fit_transform(X, y, **fit_params)
871 else:
872 res = transformer.fit(X, y, **fit_params).transform(X)

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/sklearn/base.py:870, in TransformerMixin.fit_transform(self, X, y, **fit_params)
867 return self.fit(X, **fit_params).transform(X)
868 else:
869 # fit method of arity 2 (supervised transformation)
--> 870 return self.fit(X, y, **fit_params).transform(X)

File ~/anaconda3/envs/drl/lib/python3.9/site-packages/pysindy/feature_library/polynomial_library.py:170, in PolynomialLibrary.fit(self, x, y)
162 n_samples, n_features = check_array(x, accept_sparse=True).shape
163 combinations = self.combinations(
164 n_features,
165 self.degree,
(...)
168 self.include_bias,
169 )
--> 170 self.n_input_features
= n_features
171 self.n_output_features_ = sum(1 for _ in combinations)
172 return self

AttributeError: can't set attribute

`

@akaptano
Copy link
Collaborator

akaptano commented Aug 5, 2022

What are your versions of pysindy and scikit-learn?

@akaptano akaptano reopened this Aug 5, 2022
@hasnainalik
Copy link

What are your versions of pysindy and scikit-learn?

pysindy = 1.3.0
scikit-learn = 1.1.1
Operating system = Ubuntu 20.04.4 LTS

@akaptano
Copy link
Collaborator

akaptano commented Aug 6, 2022

It has been solved on newer versions of PySINDy.

jpcurbelo pushed a commit to jpcurbelo/pysindy_fork that referenced this issue May 9, 2024
The part that uploads to pypi will only run on the public repo, the build part can also run in the private repo. The whole job is only triggered when a tag is created.
jpcurbelo pushed a commit to jpcurbelo/pysindy_fork that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants