(self, use_dask: bool)
| 5057 | @requires_scipy |
| 5058 | @pytest.mark.parametrize("use_dask", [True, False]) |
| 5059 | def test_curvefit_ignore_errors(self, use_dask: bool) -> None: |
| 5060 | if use_dask and not has_dask: |
| 5061 | pytest.skip("requires dask") |
| 5062 | |
| 5063 | # nonsense function to make the optimization fail |
| 5064 | def line(x, a, b): |
| 5065 | if a > 10: |
| 5066 | return 0 |
| 5067 | return a * x + b |
| 5068 | |
| 5069 | da = DataArray( |
| 5070 | [[1, 3, 5], [0, 20, 40]], |
| 5071 | coords={"i": [1, 2], "x": [0.0, 1.0, 2.0]}, |
| 5072 | ) |
| 5073 | |
| 5074 | if use_dask: |
| 5075 | da = da.chunk({"i": 1}) |
| 5076 | |
| 5077 | expected = DataArray( |
| 5078 | [[2, 1], [np.nan, np.nan]], coords={"i": [1, 2], "param": ["a", "b"]} |
| 5079 | ) |
| 5080 | |
| 5081 | with pytest.raises(RuntimeError, match="calls to function has reached maxfev"): |
| 5082 | da.curvefit( |
| 5083 | coords="x", |
| 5084 | func=line, |
| 5085 | # limit maximum number of calls so the optimization fails |
| 5086 | kwargs=dict(maxfev=5), |
| 5087 | ).compute() # have to compute to raise the error |
| 5088 | |
| 5089 | fit = da.curvefit( |
| 5090 | coords="x", |
| 5091 | func=line, |
| 5092 | errors="ignore", |
| 5093 | # limit maximum number of calls so the optimization fails |
| 5094 | kwargs=dict(maxfev=5), |
| 5095 | ).compute() |
| 5096 | |
| 5097 | assert_allclose(fit.curvefit_coefficients, expected) |
| 5098 | |
| 5099 | |
| 5100 | class TestReduce: |
nothing calls this directly
no test coverage detected