Skip to content

handle nan in tools.golden_sect_DataFrame #1408

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

Merged
merged 6 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pvlib/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ def test__golden_sect_DataFrame_vector():
v, x = tools._golden_sect_DataFrame(params, lower, upper,
_obj_test_golden_sect)
assert np.allclose(x, expected, atol=1e-8)


def test__golden_sect_DataFrame_nans():
# nan in bounds
params = {'c': np.array([1., 2., 1.]), 'n': np.array([1., 1., 1.])}
lower = np.array([0., 0.001, np.nan])
upper = np.array([1.1, 1.2, 1.])
expected = np.array([0.5, 0.25, np.nan])
v, x = tools._golden_sect_DataFrame(params, lower, upper,
_obj_test_golden_sect)
assert np.allclose(x, expected, atol=1e-8, equal_nan=True)
# nan in function values
params = {'c': np.array([1., 2., np.nan]), 'n': np.array([1., 1., 1.])}
lower = np.array([0., 0.001, 0.])
upper = np.array([1.1, 1.2, 1.])
expected = np.array([0.5, 0.25, np.nan])
v, x = tools._golden_sect_DataFrame(params, lower, upper,
_obj_test_golden_sect)
assert np.allclose(x, expected, atol=1e-8, equal_nan=True)
25 changes: 16 additions & 9 deletions pvlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,17 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):

Parameters
----------
params : dict or Dataframe
Parameters to be passed to `func`.
params : dict of numeric
Parameters to be passed to `func`. Each entry must be of the same
length.

lower: numeric
Lower bound for the optimization
Lower bound for the optimization. Must be the same length as each
entry of params.

upper: numeric
Upper bound for the optimization
Upper bound for the optimization. Must be the same length as each
entry of params.

func: function
Function to be optimized. Must be in the form
Expand All @@ -312,6 +315,7 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):
Notes
-----
This function will find the points where the function is maximized.
Returns nan where lower or upper is nan, or where func evaluates to nan.

See also
--------
Expand All @@ -326,10 +330,10 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):

converged = False
iterations = 0
iterlimit = 1 + np.max(
iterlimit = 1 + np.nanmax(
np.trunc(np.log(atol / (df['VH'] - df['VL'])) / np.log(phim1)))

while not converged and (iterations < iterlimit):
while not converged and (iterations <= iterlimit):

phi = phim1 * (df['VH'] - df['VL'])
df['V1'] = df['VL'] + phi
Expand All @@ -345,15 +349,18 @@ def _golden_sect_DataFrame(params, lower, upper, func, atol=1e-8):
err = abs(df['V2'] - df['V1'])

# works with single value because err is np.float64
converged = (err < atol).all()
converged = (err[~np.isnan(err)] < atol).all()
# err will be less than atol before iterations hit the limit
# but just to be safe
iterations += 1

if iterations > iterlimit:
raise Exception("iterations exceeded maximum") # pragma: no cover
raise Exception("Iterations exceeded maximum. Check that func",
" is not NaN in (lower, upper)")

return func(df, 'V1'), df['V1']
func_result = func(df, 'V1')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR gets #1394 working again for me locally, so +1 for that. However I think this will still fail with a KeyError if either of upper or lower is all nans: in that case iterlimit will still be nan, so the while loop will not execute at all, causing df['V1'] to not exist when this line executes. I think the all-nan case is worth supporting: a ModelChain simulation of only nighttime is not particularly useful, but shouldn't throw an error IMHO.

x = np.where(np.isnan(func_result), np.nan, df['V1'])
return func_result, x


def _get_sample_intervals(times, win_length):
Expand Down