question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: optimize.minimize backwards compatability in scipy 1.9

See original GitHub issue

Describe your issue.

Hi, we have some code that works with scipy 1.8 but crashes 1.9. An example to reproduce is below. The issue arises in the code than transforms between the new/old bounds definition, but is actually related to the way x0 is defined.

You can ‘fix’ this by changing start to start = np.array([0.0]). We can actually do this in our code too, but I wanted to check if this behavior is intentional or whether this lack of backward compatibility constitutes a bug?

Reproducing Code Example

'''
example which will run on scipy 1.8 but not scipy 1.9
'''
from scipy.optimize import minimize
import numpy as np

def objective(x):
    return(x+10)**2

bounds = np.array([[-20, 20]])
start = np.array([0.0]).reshape(-1, 1)
res = minimize(objective, start, bounds=bounds, method="L-BFGS-B")

Error message

TypeError: object of type 'Bounds' has no len()

SciPy/NumPy/Python version information

1.8/1.9

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
andyfaffcommented, Aug 25, 2022

At the start of minimize this code is called:

 x0 = np.atleast_1d(np.asarray(x0))
    if x0.ndim != 1:
        message = ('Use of `minimize` with `x0.ndim != 1` is deprecated. '
                   'Currently, singleton dimensions will be removed from '
                   '`x0`, but an error will be raised in SciPy 1.11.0.')
        warn(message, DeprecationWarning, stacklevel=2)
        x0 = np.squeeze(x0)

Because there is more than one dimension in start the array is squeezed. We do this squeeze because minimize is only designed to work with 1D arrays (problems are experienced with ndim > 1). You should’ve seen this DeprecationWarning when you ran your code. When the code squeezes np.array([[0.0]]) you end up with np.array(0.0), which has shape (). When x0.shape[0] is requested an IndexError is raised, IndexError: tuple index out of range. I get a different error than you see.

We could fix scipy to make it back compatible by doing x0 = np.atleast_1d(np.squeeze(x0)). However, as you can see from the code snippet above we’re going to raise an Error if x0.ndim > 1 in scipy 1.11. This probably makes it less important to make a ‘fix’ in scipy; can you fix the downstream code here?

0reactions
mdhabercommented, Aug 26, 2022

Hmm I do think this was an unintentional mistake on our part to remove all the dimensions, so from that perspective, yes. We might be able to get it in 1.9.1. I can’t push the buttons, but I can review quickly if it’s the one-liner you described, @andyfaff.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SciPy 1.9.0 Release Notes — SciPy v1.9.3 Manual
Our development attention will now shift to bug-fix releases on the 1.9.x ... the output Laplacian: * array is a numpy array (backward...
Read more >
SciPy: doc/release/1.2.0-notes.rst - Fossies
The one-dimensional nonlinear solvers have been given a unified interface scipy.optimize.root_scalar, similar to the scipy.optimize.root interface for multi- ...
Read more >
scipy/scipy: SciPy 1.9.0rc1 | Zenodo
Add a full_output parameter to scipy.optimize.curve_fit to output ... format of the output Laplacian: array is a numpy array (backward compatible default); ...
Read more >
Mailman 3 ANN: SciPy 1.9.0 - NumPy-Discussion - python.org
It contains many new features, numerous bug-fixes, improved test coverage and better documentation. There have been a number of deprecations and API changes...
Read more >
SciPy 0.15.0 Release Notes — SciPy v1.0.0 Reference Guide
optimize.minimize provides a generic interface to nonlinear programming optimizers. Currently the only method supported is simplex which ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found