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

Numpy 2 compatibility #1530

Open
MichaelClerx opened this issue Jun 17, 2024 · 3 comments
Open

Numpy 2 compatibility #1530

MichaelClerx opened this issue Jun 17, 2024 · 3 comments

Comments

@MichaelClerx
Copy link
Member

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

@MichaelClerx
Copy link
Member Author

@MichaelClerx
Copy link
Member Author

MichaelClerx commented Jun 17, 2024

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 MichaelClerx changed the title Numpy 2 Numpy 2 compatibility Jun 17, 2024
@MichaelClerx
Copy link
Member Author

Unit tests now working. Interfaces notebooks not yet, currently at least autograd is not numpy 2 ready HIPS/autograd#622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant