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

Add errors option to curvefit #7891

Merged
merged 13 commits into from Jun 16, 2023
Merged

Conversation

mgunyho
Copy link
Contributor

@mgunyho mgunyho commented Jun 4, 2023

This is a rebased version of #6515, with the arg errors = "raise" | "ignore" added to Dataset and DataArray, and with tests. Let me know if the tests should be expanded further.

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for adding a test for the original PR.

Just some minor remarks

allow_failures: bool, default: False
If True and the underlying `scipy.optimize_curve_fit` optimization fails for
any of the fits, return NaN in coefficients and covariances for those
coordinates. Helpful when fitting multiple curves and some of the data just
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the Helpful... Part. It just adds clutter for little benefit.

If you want to add such tips do it above the argument list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in d3decbb. I was also wondering if the name could be something different, like allow_errors for example. Are there any other examples of similar args in xarray, so we could be consistent with those?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point.

In other places we use errors = "raise" | "ignore" | "warn" (the warn part is optional)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll change it to that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in efce0b3. I was considering adding the option to also warn, but looks like scipy has its own internal mechanism for producing warnings, see here: https://github.com/scipy/scipy/blob/v1.10.1/scipy/optimize/_minpack_py.py#L491. I'm not sure how to control it and didn't want to get into that now. Maybe a warning is now always issued? Although I didn't see any warnings in pytest on my setup at least.

xarray/tests/test_dataarray.py Show resolved Hide resolved
@mgunyho mgunyho changed the title Add allow_failures flag to curvefit Add errors option to curvefit Jun 4, 2023
@mgunyho
Copy link
Contributor Author

mgunyho commented Jun 4, 2023

Oh no, the doctest failure is because the test is flaky, this was introduced by me in #7821, see here: #7821 (comment) and here: a0e6659. I'll submit another patch to fix it soon, although I'm not sure how. If you have any tips to avoid this problem, let me know.

@Illviljan
Copy link
Contributor

You could use DataArray.round to round to significant decimals.

@headtr1ck
Copy link
Collaborator

You could use DataArray.round to round to significant decimals.

Better to use a tolerance in the assterion testing.

@headtr1ck headtr1ck added the plan to merge Final call for comments label Jun 6, 2023
@dcherian dcherian merged commit 99f9559 into pydata:main Jun 16, 2023
29 checks passed
dstansby pushed a commit to dstansby/xarray that referenced this pull request Jun 28, 2023
* Add allow_failures flag to Dataset.curve_fit

* Reword docstring

* Add allow_failures flag also to DataArray

* Add unit test for curvefit with allow_failures

* Update whats-new

Co-authored-by: Dominik Stańczak <stanczakdominik@gmail.com>

* Add PR to whats-new

* Update docstring

* Rename allow_failures to errors to be consistent with other methods

* Compute array so test passes also with dask

* Check error message

* Update whats-new

---------

Co-authored-by: Dominik Stańczak <stanczakdominik@gmail.com>
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle scipy fitting errors in xarray.curve_fit
5 participants