dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.46k stars 324 forks source link

[BUG] SINDyPI and CVXPY issues on Windows 11 #483

Closed zbrookshowe closed 8 months ago

zbrookshowe commented 8 months ago

Hello! I am having trouble running the pysindy example jupyter file 'examples/9_sindypi_with_sympy.ipynb'. I am using a Windows 11 system and am not using a WSL. I get the following error message when running:

AttributeError: module 'pysindy' has no attribute 'SINDyPI'

To try and track down this error, I have reviewed the following issues:

270, #366, #369, #420, #421, and #431

I have also looked at the following stack overflow posts:

Based on these, I believe that my issue is related to the CVXPY package, which I am unable to manually install with pip install cvxpy. I have also tried installing with only pysindy dependencies, i.e. pip install pysindy[cvxpy]. The full error message when I try to install CVXPY is included below, but the message is essentially the same as the first stack overflow post from above: ModuleNotFoundError: No module named 'cmake'. This was seemingly solved in #431 by using brew install cmake, but since I am running a Windows I can't use homebrew. Should I just try to redo the process using a WSL?

Reproducing code example:

I have copied code inputs [1] and [3] from the tutorial jupyter notebook 'examples/9_sindypi_with_sympy.ipynb'.

# Import libraries.
import numpy as np
from matplotlib import pyplot as plt
import pysindy as ps 
from pysindy.differentiation import FiniteDifference
from pysindy.utils import enzyme, bacterial
import sympy as sp
from scipy.integrate import solve_ivp
from sklearn.metrics import r2_score

# For reproducibility
np.random.seed(100)

integrator_keywords = {}
integrator_keywords['rtol'] = 1e-12
integrator_keywords['method'] = 'LSODA'
integrator_keywords['atol'] = 1e-12

# define parameters
r = 1
dt = 0.001
T = 4
t = np.arange(0, T + dt, dt)
t_span = (t[0], t[-1])
x0_train = [0.55]
x_train = solve_ivp(enzyme, t_span, x0_train, t_eval=t, **integrator_keywords).y.T

# Initialize custom SINDy library so that we can have x_dot inside it. 
library_functions = [
    lambda x: x,
    lambda x, y: x * y,
    lambda x: x ** 2,
    lambda x, y, z: x * y * z,
    lambda x, y: x * y ** 2,
    lambda x: x ** 3,
    lambda x, y, z, w: x * y * z * w,
    lambda x, y, z: x * y * z ** 2,
    lambda x, y: x * y ** 3,
    lambda x: x ** 4,
]
x_dot_library_functions = [lambda x: x]

# library function names includes both 
# the x_library_functions and x_dot_library_functions names
library_function_names = [
    lambda x: x,
    lambda x, y: x + y,
    lambda x: x + x,
    lambda x, y, z: x + y + z,
    lambda x, y: x + y + y,
    lambda x: x + x + x,
    lambda x, y, z, w: x + y + z + w,
    lambda x, y, z: x + y + z + z,
    lambda x, y: x + y + y + y,
    lambda x: x + x + x + x,
    lambda x: x,
]

# Need to pass time base to the library so can build the x_dot library from x
sindy_library = ps.SINDyPILibrary(
    library_functions=library_functions,
    x_dot_library_functions=x_dot_library_functions,
    t=t,
    function_names=library_function_names,
    include_bias=True,
)

sindy_opt = ps.SINDyPI(
    threshold=1e-6,
    tol=1e-8,
    thresholder="l1",
    max_iter=20000,
)

model = ps.SINDy(
    optimizer=sindy_opt,
    feature_library=sindy_library,
    differentiation_method=ps.FiniteDifference(drop_endpoints=True),
)
model.fit(x_train, t=t)
model.print()

sindy_library.get_feature_names()

Error message:

PySINDy Error Message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[2], line 50
     41 # Need to pass time base to the library so can build the x_dot library from x
     42 sindy_library = ps.SINDyPILibrary(
     43     library_functions=library_functions,
     44     x_dot_library_functions=x_dot_library_functions,
   (...)
     47     include_bias=True,
     48 )
---> 50 sindy_opt = ps.SINDyPI(
     51     threshold=1e-6,
     52     tol=1e-8,
     53     thresholder="l1",
     54     max_iter=20000,
     55 )
     57 model = ps.SINDy(
     58     optimizer=sindy_opt,
     59     feature_library=sindy_library,
     60     differentiation_method=ps.FiniteDifference(drop_endpoints=True),
     61 )
     62 model.fit(x_train, t=t)

AttributeError: module 'pysindy' has no attribute 'SINDyPI'

CVXPY Error Message

C:\Users\<uname>>pip install cvxpy
Collecting cvxpy
  Using cached cvxpy-1.4.2-cp312-cp312-win_amd64.whl
Collecting osqp>=0.6.2 (from cvxpy)
  Using cached osqp-0.6.5-cp312-cp312-win_amd64.whl.metadata (1.8 kB)
Collecting ecos>=2 (from cvxpy)
  Using cached ecos-2.0.13-cp312-cp312-win_amd64.whl.metadata (8.2 kB)
Collecting clarabel>=0.5.0 (from cvxpy)
  Using cached clarabel-0.7.1-cp37-abi3-win_amd64.whl.metadata (4.7 kB)
Requirement already satisfied: scs>=3.0 in c:\users\<uname>\appdata\local\programs\python\python312\lib\site-packages (from cvxpy) (3.2.4.post1)
Requirement already satisfied: numpy>=1.15 in c:\users\<uname>\appdata\local\programs\python\python312\lib\site-packages (from cvxpy) (1.26.4)
Requirement already satisfied: scipy>=1.1.0 in c:\users\<uname>\appdata\local\programs\python\python312\lib\site-packages (from cvxpy) (1.11.4)
Requirement already satisfied: pybind11 in c:\users\<uname>\appdata\local\programs\python\python312\lib\site-packages (from cvxpy) (2.11.1)
Collecting qdldl (from osqp>=0.6.2->cvxpy)
  Using cached qdldl-0.1.7.post0.tar.gz (70 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Using cached clarabel-0.7.1-cp37-abi3-win_amd64.whl (321 kB)
Using cached ecos-2.0.13-cp312-cp312-win_amd64.whl (72 kB)
Using cached osqp-0.6.5-cp312-cp312-win_amd64.whl (293 kB)
Building wheels for collected packages: qdldl
  Building wheel for qdldl (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for qdldl (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [66 lines of output]
      running bdist_wheel
      running build
      running build_ext
      Traceback (most recent call last):
        File "<frozen runpy>", line 198, in _run_module_as_main
        File "<frozen runpy>", line 88, in _run_code
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Scripts\cmake.exe\__main__.py", line 4, in <module>
      ModuleNotFoundError: No module named 'cmake'
      Traceback (most recent call last):
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 251, in build_wheel
          return _build_backend().build_wheel(wheel_directory, config_settings,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\build_meta.py", line 410, in build_wheel
          return self._build_with_temp_dir(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\build_meta.py", line 395, in _build_with_temp_dir
          self.run_setup()
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 113, in <module>
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\__init__.py", line 104, in setup
          return distutils.core.setup(**attrs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\core.py", line 185, in setup
          return run_commands(dist)
                 ^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\core.py", line 201, in run_commands
          dist.run_commands()
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 969, in run_commands
          self.run_command(cmd)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\dist.py", line 967, in run_command
          super().run_command(command)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
          cmd_obj.run()
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\normal\Lib\site-packages\wheel\bdist_wheel.py", line 368, in run
          self.run_command("build")
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\cmd.py", line 318, in run_command
          self.distribution.run_command(command)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\dist.py", line 967, in run_command
          super().run_command(command)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
          cmd_obj.run()
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\command\build.py", line 131, in run
          self.run_command(cmd_name)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\cmd.py", line 318, in run_command
          self.distribution.run_command(command)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\dist.py", line 967, in run_command
          super().run_command(command)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
          cmd_obj.run()
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\command\build_ext.py", line 91, in run
          _build_ext.run(self)
        File "C:\Users\<uname>\AppData\Local\Temp\pip-build-env-ba_fwwge\overlay\Lib\site-packages\setuptools\_distutils\command\build_ext.py", line 345, in run
          self.build_extensions()
        File "<string>", line 81, in build_extensions
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 466, in check_output
          return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "C:\Users\<uname>\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 571, in run
          raise CalledProcessError(retcode, process.args,
      subprocess.CalledProcessError: Command '['cmake', '--version']' returned non-zero exit status 1.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for qdldl
Failed to build qdldl
ERROR: Could not build wheels for qdldl, which is required to install pyproject.toml-based projects

PySINDy/Python version information:

PySINDy version: 1.7.5 Python Version: 3.11.6

Jacob-Stevens-Haas commented 8 months ago

The error occurs because qdldl doesn't list cmake as a build requirement, but it is a build requirement. Qdlql has been fixed on master, but it was recent and probably isn't in a release yet.

The solution is to just pip install cmake. The Python package vendors the whole command line program.

zbrookshowe commented 8 months ago

@Jacob-Stevens-Haas Thank you for your reply. I had already tried this last week, and when I reran pip install cmake, it told me "Requirement already satisfied". However, when I tried again to install cvxpy, it worked. So I'm not sure if pip install cmake actually solved the issue, but somehow this got resolved. Thank you for your time!