pints-team / pints

Probabilistic Inference on Noisy Time Series
http://pints.readthedocs.io
Other
225 stars 33 forks source link

Numpy 2 compatibility #1530

Open MichaelClerx opened 3 months ago

MichaelClerx commented 3 months ago

Major numpy version is now available through pip, will likely require some changes

MichaelClerx commented 3 months ago

https://numpy.org/devdocs/release/2.0.0-notes.html

MichaelClerx commented 3 months ago

Errors from inside cma:

======================================================================
ERROR: test_bounded (test_opt_cmaes.TestCMAES.test_bounded)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_cmaes.py", line 55, in test_bounded
    found_parameters, found_solution = opt.run()
                                       ^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/__init__.py", line 648, in run
    xs = self._optimiser.ask()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 52, in ask
    self._initialise()
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 135, in _initialise
    self._es = cma.CMAEvolutionStrategy(self._x0, self._sigma0, options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/evolution_strategy.py", line 1503, in __init__
    self.boundary_handler = self.boundary_handler(opts['bounds'])
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/constraints_handler.py", line 274, in __init__
    self.bounds_tf = BoxConstraintsLinQuadTransformation(self.to_dim_times_two(bounds))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 263, in __init__
    super(BoxConstraintsLinQuadTransformation, self).__init__(bounds)
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 124, in __init__
    self.initialize()
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 272, in initialize
    self._lb = array([self.bounds[min((i, max_i))][0]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
======================================================================
ERROR: test_bounded_and_sigma (test_opt_cmaes.TestCMAES.test_bounded_and_sigma)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_cmaes.py", line 73, in test_bounded_and_sigma
    found_parameters, found_solution = opt.run()
                                       ^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/__init__.py", line 648, in run
    xs = self._optimiser.ask()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 52, in ask
    self._initialise()
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 135, in _initialise
    self._es = cma.CMAEvolutionStrategy(self._x0, self._sigma0, options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/evolution_strategy.py", line 1503, in __init__
    self.boundary_handler = self.boundary_handler(opts['bounds'])
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/constraints_handler.py", line 274, in __init__
    self.bounds_tf = BoxConstraintsLinQuadTransformation(self.to_dim_times_two(bounds))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 263, in __init__
    super(BoxConstraintsLinQuadTransformation, self).__init__(bounds)
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 124, in __init__
    self.initialize()
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 272, in initialize
    self._lb = array([self.bounds[min((i, max_i))][0]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
======================================================================
ERROR: test_unbounded (test_opt_cmaes.TestCMAES.test_unbounded)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_cmaes.py", line 44, in test_unbounded
    found_parameters, found_solution = opt.run()
                                       ^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/__init__.py", line 648, in run
    xs = self._optimiser.ask()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 52, in ask
    self._initialise()
  File "/home/michael/dev/pints/pints/_optimisers/_cmaes.py", line 135, in _initialise
    self._es = cma.CMAEvolutionStrategy(self._x0, self._sigma0, options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/evolution_strategy.py", line 1503, in __init__
    self.boundary_handler = self.boundary_handler(opts['bounds'])
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/constraints_handler.py", line 274, in __init__
    self.bounds_tf = BoxConstraintsLinQuadTransformation(self.to_dim_times_two(bounds))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 263, in __init__
    super(BoxConstraintsLinQuadTransformation, self).__init__(bounds)
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 124, in __init__
    self.initialize()
  File "/home/michael/dev/myokit/venv/lib/python3.12/site-packages/cma/transformations.py", line 274, in initialize
    else -np.Inf
          ^^^^^^
  File "/home/michael/dev/myokit/venv/lib64/python3.12/site-packages/numpy/__init__.py", line 397, in __getattr__
    raise AttributeError(
AttributeError: `np.Inf` was removed in the NumPy 2.0 release. Use `np.inf` instead.. Did you mean: 'inf'?

Local ones:

======================================================================
ERROR: test_rosenbrock (test_opt_nelder_mead.TestNelderMead.test_rosenbrock)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_nelder_mead.py", line 163, in test_rosenbrock
    x, f = opt.run()
           ^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/__init__.py", line 654, in run
    self._optimiser.tell(fs)
  File "/home/michael/dev/pints/pints/_optimisers/_nelder_mead.py", line 254, in tell
    fx = np.array(fx, copy=False)
         ^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
======================================================================
ERROR: test_zeros_in_x (test_opt_nelder_mead.TestNelderMead.test_zeros_in_x)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_nelder_mead.py", line 116, in test_zeros_in_x
    x, f = opt.run()
           ^^^^^^^^^
  File "/home/michael/dev/pints/pints/_optimisers/__init__.py", line 654, in run
    self._optimiser.tell(fs)
  File "/home/michael/dev/pints/pints/_optimisers/_nelder_mead.py", line 254, in tell
    fx = np.array(fx, copy=False)
         ^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
======================================================================
FAIL: test_bad_tell (test_opt_nelder_mead.TestNelderMead.test_bad_tell)
----------------------------------------------------------------------
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/michael/dev/pints/pints/tests/test_opt_nelder_mead.py", line 143, in test_bad_tell
    with self.assertRaisesRegex(ValueError, 'length n_parameters'):
AssertionError: "length n_parameters" does not match "Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword."
MichaelClerx commented 2 months ago

Unit tests now working. Interfaces notebooks not yet, currently at least autograd is not numpy 2 ready https://github.com/HIPS/autograd/issues/622