Skip to content

Commit

Permalink
Fix: update deprecated scipy.integrate.trapz to trapezoid #243
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
wietzesuijker committed Jun 4, 2024
1 parent ca22016 commit 07c4b41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ updates:
directory: "/.devcontainer"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/.github"
- package-ecosystem: "github-actions"
directory: "/.github"
schedule:
interval: "monthly"
8 changes: 4 additions & 4 deletions advanced/apply_ufunc/core-dimensions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,19 @@
},
"source": [
"```{exercise}\n",
":label: trapz\n",
":label: trapezoid\n",
"\n",
"Use `apply_ufunc` to apply `scipy.integrate.trapz` along the `time` axis.\n",
"Use `apply_ufunc` to apply `scipy.integrate.trapezoid` along the `time` axis.\n",
"```\n",
"\n",
"````{solution} trapz\n",
"````{solution} trapezoid\n",
":class: dropdown\n",
"\n",
"```python\n",
"import scipy as sp\n",
"import scipy.integrate\n",
"\n",
"xr.apply_ufunc(scipy.integrate.trapz, ds, input_core_dims=[[\"time\"]], kwargs={\"axis\": -1})\n",
"xr.apply_ufunc(scipy.integrate.trapezoid, ds, input_core_dims=[[\"time\"]], kwargs={\"axis\": -1})\n",
"```\n",
"````"
]
Expand Down
16 changes: 8 additions & 8 deletions advanced/apply_ufunc/dask_apply_ufunc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@
"in parallel to each block. This ability can be activated using\n",
"`dask=\"parallelized\"`. \n",
"\n",
"We will use `scipy.integrate.trapz` as an example of a function that cannot\n",
"handle dask arrays and requires a core dimension. If we call `trapz` with a dask\n",
"We will use `scipy.integrate.trapezoid` as an example of a function that cannot\n",
"handle dask arrays and requires a core dimension. If we call `trapezoid` with a dask\n",
"array, we get a numpy array back that is, the values have been eagerly computed.\n",
"This is undesirable behaviour\n"
]
Expand All @@ -354,7 +354,7 @@
"import scipy as sp\n",
"import scipy.integrate\n",
"\n",
"sp.integrate.trapz(\n",
"sp.integrate.trapezoid(\n",
" ds.air.data, axis=ds.air.get_axis_num(\"lon\")\n",
") # does NOT return a dask array, you should see activity on the dashboard"
]
Expand All @@ -377,7 +377,7 @@
"outputs": [],
"source": [
"integrated = xr.apply_ufunc(\n",
" sp.integrate.trapz,\n",
" sp.integrate.trapezoid,\n",
" ds,\n",
" input_core_dims=[[\"lon\"]],\n",
" kwargs={\"axis\": -1},\n",
Expand Down Expand Up @@ -479,7 +479,7 @@
"tags": []
},
"source": [
"The core dimension for `trapz` is `lon`, and there is only one chunk along `lon`. This means that integrating along `lon` is a \"blockwise\" or \"embarrassingly parallel\" operation and `dask=\"parallelized\"` works quite well. \n",
"The core dimension for `trapezoid` is `lon`, and there is only one chunk along `lon`. This means that integrating along `lon` is a \"blockwise\" or \"embarrassingly parallel\" operation and `dask=\"parallelized\"` works quite well. \n",
"\n",
"```{caution} Question\n",
"Do you understand why `integrate(ds)` when `ds` has a single chunk along `lon` is a \"embarrassingly parallel\" operation?\n",
Expand Down Expand Up @@ -535,7 +535,7 @@
"source": [
"def integrate_wrapper(array, **kwargs):\n",
" print(f\"received array of type {type(array)}, shape {array.shape}\")\n",
" result = sp.integrate.trapz(array, **kwargs)\n",
" result = sp.integrate.trapezoid(array, **kwargs)\n",
" print(f\"received array of type {type(result)}, shape {result.shape}\")\n",
" return result\n",
"\n",
Expand Down Expand Up @@ -611,15 +611,15 @@
"\n",
"Conceptually, there is a two-way flow of information between various packages when executing `integrated.compute()`:\n",
"\n",
"`xarray.apply_ufunc` ↔ `dask.array.apply_gufunc` ↔ `integrate_wrapper` ↔ `scipy.integrate.trapz` ↔ `ds.air.data`\n",
"`xarray.apply_ufunc` ↔ `dask.array.apply_gufunc` ↔ `integrate_wrapper` ↔ `scipy.integrate.trapezoid` ↔ `ds.air.data`\n",
"\n",
"\n",
"When executed\n",
"\n",
"1. Xarray loops over all data variables.\n",
"1. Xarray unwraps the underlying dask array (e.g. `ds.air`) and passes that to dask's `apply_gufunc`.\n",
"1. `apply_gufunc` calls `integrate_wrapper` on each block of the array.\n",
"1. For each block, `integrate_wrapper` calls `scipy.integrate.trapz` and returns one block of the output array.\n",
"1. For each block, `integrate_wrapper` calls `scipy.integrate.trapezoid` and returns one block of the output array.\n",
"1. dask stitches all the output blocks to form the output array.\n",
"1. `xarray.apply_ufunc` wraps the output array with Xarray metadata to give the final result.\n",
"\n",
Expand Down

0 comments on commit 07c4b41

Please sign in to comment.