id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 1157140667,I_kwDOAMm_X85E-JC7,6317,Handle scipy fitting errors in `xarray.curve_fit`,11289391,closed,0,,,2,2022-03-02T12:38:04Z,2023-06-16T03:15:08Z,2023-06-16T03:15:08Z,CONTRIBUTOR,,,,"### Is your feature request related to a problem? I'm trying to fit a bunch of exponential decays to a dataset of dimensions `(Time, experimental_index)`, along the `Time` dimension. This works nicely... except in some cases, `scipy.optimize` raises an error such as `RuntimeError: Optimal parameters not found: The maximum number of function evaluations is exceeded.`. That's, of course, reasonable, except in my use case I'm fine with a bunch of `experimental_index` failing to fit, and I'd still like to access the fits that made it through. ### Describe the solution you'd like I'm proposing adding a flag for `xarray.curvefit`, for example `allow_failures: bool = False`, that would take this part: https://github.com/pydata/xarray/blob/f9037c41e36254bfe7efa9feaedd3eae5512bd11/xarray/core/dataset.py#L7738-L7752 and, if active, wrap `popt, pcov = curve_fit(func, x, y, **kwargs)` in a `try/except` block that would output `nan` just as the `not len(y)` conditional clause does if `scipy.optimize.curve_fit` fails with a `RuntimeError`. ### Describe alternatives you've considered At the moment, I sidestep the issue like this: ```python fits = [] for experimental_index in timetraces.experimental_index: trace = traces.sel(experimental_index=experimental_index) try: fit = trace.curvefit( ""Time"", fitfunc, p0=(...), bounds={...}, ) except (RuntimeError, TypeError) as e: logger.debug(...) continue fits.append(fit) fits = xarray.concat(fits, dim=""experimental_index"", coords=""all"") ``` but that seems like a pretty ugly hack... ### Additional context I would of course be happy to implement this!","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/6317/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue