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

_minimize_scalar_bounded: reference before assignment #11207

Closed
MartinGrignard opened this issue Dec 12, 2019 · 5 comments · Fixed by #11208
Closed

_minimize_scalar_bounded: reference before assignment #11207

MartinGrignard opened this issue Dec 12, 2019 · 5 comments · Fixed by #11208
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.optimize
Milestone

Comments

@MartinGrignard
Copy link
Contributor

Hi,
I've recently changed my docker configuration and, while using chaospy package, I came across an error which never occurred before.
It looks like it comes from the fact that fu is not instantiated before the while loop but I'm not sure.
I hope this report will help.

Reproducing code example:

import chaospy as cp

distribution = cp.J(cp.Uniform(0, 1), cp.Uniform(0, 1))
abscissas, weights = cp.generate_quadrature(7, distribution, rule="Leja", sparse=True)

Error message:

This error message occured in a Jupyter notebook:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-1-b2f64640c4d5> in <module>
      1 import chaospy as cp
      2 dist=cp.J(cp.Uniform(0, 1), cp.Uniform(0, 1))
----> 3 points = cp.generate_quadrature(2, dist, rule="Leja", sparse=True)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    134         from . import sparse_grid
    135         return sparse_grid.construct_sparse_grid(
--> 136             order, dist, rule=rule, accuracy=accuracy, growth=growth)
    137 
    138     if not isinstance(rule, str):

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in construct_sparse_grid(order, dist, rule, accuracy, growth, recurrence_algorithm)
    122 
    123     x_lookup, w_lookup = _construct_lookup(
--> 124         orders, dist, rule, accuracy, growth, recurrence_algorithm)
    125     collection = _construct_collection(
    126         order, dist, x_lookup, w_lookup)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/sparse_grid.py in _construct_lookup(orders, dists, rules, accuracy, growth, recurrence_algorithm)
    182                 rule=rule,
    183                 growth=growth,
--> 184                 recurrence_algorithm=recurrence_algorithm,
    185             )
    186             x_lookup[-1].append(abscissas)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/frontend.py in generate_quadrature(order, dist, rule, sparse, accuracy, growth, recurrence_algorithm)
    157 
    158     quad_function = QUAD_FUNCTIONS[rule]
--> 159     abscissas, weights = quad_function(order, dist, **kwargs)
    160 
    161     assert len(weights) == abscissas.shape[1]

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in quad_leja(order, dist, rule, accuracy, recurrence_algorithm)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--> 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/chaospy/quadrature/leja.py in <listcomp>(.0)
    104             *[fminbound(
    105                 objective, abscissas[idx], abscissas[idx+1], full_output=1)[:2]
--> 106               for idx in range(len(abscissas)-1)]
    107         )
    108         index = numpy.argmin(vals)

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in fminbound(func, x1, x2, args, xtol, maxfun, full_output, disp)
   1734                'disp': disp}
   1735 
-> 1736     res = _minimize_scalar_bounded(func, (x1, x2), args, **options)
   1737     if full_output:
   1738         return res['x'], res['fun'], res['status'], res['nfev']

/usr/local/lib/python3.7/dist-packages/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1868             break
   1869 
-> 1870     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1871         flag = 2
   1872 

UnboundLocalError: local variable 'fu' referenced before assignment

Scipy/Numpy/Python version information:

1.3.3 1.17.4 sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
@andyfaff andyfaff added defect A clear bug or issue that prevents SciPy from being installed or used as expected good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.optimize labels Dec 12, 2019
@andyfaff
Copy link
Contributor

Thank you for this report. As you say fu is not initialised before the while loop, and the while loop doesn't run in this instance because it's within some kind of tolerance.
It looks to me like fu could be initialised as fu = np.inf without an issue, but I'm not familiar with this optimizer. It looks like it could be fixed relatively simply, would you be interested in making a PR?

@MartinGrignard
Copy link
Contributor Author

Thanks for this quick answer!
I can try your solution. If it works I'll do a PR.

@WarrenWeckesser
Copy link
Member

Here's a simple example to reproduce the problem:

In [1]: from scipy.optimize import fminbound

In [2]: def fun(x):
   ...:     return x**2
   ...:     

In [3]: fminbound(fun, 0, 0)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-178a9623aadc> in <module>
----> 1 fminbound(fun, 0, 0)

~/mc37scipy/lib/python3.7/site-packages/scipy-1.5.0.dev0+0a7b39a-py3.7-macosx-10.7-x86_64.egg/scipy/optimize/optimize.py in fminbound(func, x1, x2, args, xtol, maxfun, full_output, disp)
   1744                'disp': disp}
   1745 
-> 1746     res = _minimize_scalar_bounded(func, (x1, x2), args, **options)
   1747     if full_output:
   1748         return res['x'], res['fun'], res['status'], res['nfev']

~/mc37scipy/lib/python3.7/site-packages/scipy-1.5.0.dev0+0a7b39a-py3.7-macosx-10.7-x86_64.egg/scipy/optimize/optimize.py in _minimize_scalar_bounded(func, bounds, args, xatol, maxiter, disp, **unknown_options)
   1878             break
   1879 
-> 1880     if np.isnan(xf) or np.isnan(fx) or np.isnan(fu):
   1881         flag = 2
   1882 

UnboundLocalError: local variable 'fu' referenced before assignment

@andyfaff
Copy link
Contributor

@MartinGrignard you can use that easier example as a test to make sure it doesn't happen again.

Normally when I add a test I call the test something like test_gh11207 so that we know why the test was made.

@MartinGrignard
Copy link
Contributor Author

@andyfaff it looks like it passed all the tests 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected good first issue Good topic for first contributor pull requests, with a relatively straightforward solution scipy.optimize
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants