Skip to content

Commit

Permalink
Bump to Jupyter Book v1.0 (#270)
Browse files Browse the repository at this point in the history
* jbook1, drop sphinx-exercise

* update exercise format

* pin numpy<2
  • Loading branch information
scottyhq authored Jun 24, 2024
1 parent b8573b5 commit da26b73
Show file tree
Hide file tree
Showing 19 changed files with 4,604 additions and 4,415 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
conda/**
conda/
1 change: 0 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ sphinx:
# maintain old paths and redirect them (so google results dont go to 404)
# https://github.com/wpilibsuite/sphinxext-rediraffe
- sphinxext.rediraffe
- sphinx_exercise
config:
language: en # accessibility
# application/vnd.holoviews_load.v0+json, application/vnd.holoviews_exec.v0+json
Expand Down
13 changes: 6 additions & 7 deletions advanced/apply_ufunc/automatic-vectorizing-numpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@
" out[index, :] = np.interp(..., array[index, :], ...)\n",
"```\n",
"\n",
"\n",
"```{exercise}\n",
":label: coreloopdims\n",
"\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"Consider the example problem of interpolating a 2D array with dimensions `space` and `time` along the `time` dimension.\n",
"Which dimension is the core dimension, and which is the \"loop dimension\"?\n",
"```\n",
"```{solution} coreloopdims\n",
"\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"`time` is the core dimension, and `space` is the loop dimension.\n",
"```\n",
":::\n",
"::::\n",
"\n",
"## Vectorization\n",
"\n",
Expand Down
23 changes: 12 additions & 11 deletions advanced/apply_ufunc/complex-output-numpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,20 @@
"tags": []
},
"source": [
"```{exercise}\n",
":label: newdim\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"\n",
"Apply the following function using `apply_ufunc`. It adds a new dimension to the input array, let's call it `newdim`. Specify the new dimension using `output_core_dims`. Do you need any `input_core_dims`?\n",
"\n",
"```python\n",
"def add_new_dim(array):\n",
" return np.expand_dims(array, axis=-1)\n",
"```\n",
"````{solution} newdim\n",
"\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"``` python\n",
"```python\n",
"def add_new_dim(array):\n",
" return np.expand_dims(array, axis=-1)\n",
"\n",
Expand All @@ -161,7 +162,8 @@
" output_core_dims=[[\"newdim\"]],\n",
")\n",
"```\n",
"````"
":::\n",
"::::"
]
},
{
Expand Down Expand Up @@ -327,8 +329,8 @@
"tags": []
},
"source": [
"````{exercise}\n",
":label: generalize\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"\n",
"We presented the concept of \"core dimensions\" as the \"smallest unit of data the function could handle.\" Do you understand how the above use of `apply_ufunc` generalizes to an array with more than one dimension? \n",
"\n",
Expand All @@ -337,9 +339,8 @@
"air3d = xr.tutorial.load_dataset(\"air_temperature\").air)\n",
"``` \n",
"Your goal is to have a minimum and maximum value of temperature across all latitudes for a given time and longitude.\n",
"````\n",
"\n",
"````{solution} generalize\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"We want to use `minmax` to compute the minimum and maximum along the \"lat\" dimension always, regardless of how many dimensions are on the input. So we specify `input_core_dims=[[\"lat\"]]`. The output does not contain the \"lat\" dimension, but we expect two returned variables. So we pass an empty list `[]` for each returned array, so `output_core_dims=[[], []]` just as before.\n",
Expand All @@ -352,8 +353,8 @@
" input_core_dims=[[\"lat\"]],\n",
" output_core_dims=[[],[]],\n",
")\n",
"```\n",
"````"
":::\n",
"::::"
]
}
],
Expand Down
10 changes: 5 additions & 5 deletions advanced/apply_ufunc/core-dimensions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,12 @@
"tags": []
},
"source": [
"```{exercise}\n",
":label: trapezoid\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"\n",
"Use `apply_ufunc` to apply `scipy.integrate.trapezoid` along the `time` axis.\n",
"```\n",
"\n",
"````{solution} trapezoid\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"```python\n",
Expand All @@ -350,7 +349,8 @@
"\n",
"xr.apply_ufunc(scipy.integrate.trapezoid, ds, input_core_dims=[[\"time\"]], kwargs={\"axis\": -1})\n",
"```\n",
"````"
":::\n",
"::::"
]
}
],
Expand Down
27 changes: 14 additions & 13 deletions advanced/apply_ufunc/dask_apply_ufunc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@
"Such functions involve the concept of \"core dimensions\". This concept is independent of the underlying array type, and is a property of the applied function. See the [core dimensions with NumPy](core-dimensions) tutorial for more.\n",
"\n",
"\n",
"```{exercise}\n",
":label: daskmean\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"\n",
"Use `dask.array.mean` as an example of a function that can handle dask\n",
"arrays and uses an `axis` kwarg. \n",
"```\n",
"\n",
"````{solution} daskmean\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"```python\n",
"def time_mean(da):\n",
" return xr.apply_ufunc(\n",
Expand All @@ -285,10 +285,11 @@
" dask=\"allowed\",\n",
" kwargs={\"axis\": -1}, # core dimensions are moved to the end\n",
" )\n",
"\n",
"\n",
" \n",
"time_mean(ds.air)\n",
"````\n"
"```\n",
":::\n",
"::::\n"
]
},
{
Expand Down Expand Up @@ -493,12 +494,11 @@
"tags": []
},
"source": [
"```{exercise} \n",
":label: rechunk\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"Apply the integrate function to `ds` after rechunking to have a different chunksize along `lon` using `ds.chunk(lon=4)` (for example). What happens?\n",
"```\n",
"\n",
"```{solution} rechunk\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"`apply_ufunc` complains that it cannot automatically parallelize because the dataset `ds` is now chunked along the core dimension `lon`. You should see the following error:\n",
Expand All @@ -509,7 +509,8 @@
" ``.chunk(dict(lon=-1))``, or pass ``allow_rechunk=True`` in ``dask_gufunc_kwargs`` \n",
" but beware that this may significantly increase memory usage.\n",
"\n",
"```"
":::\n",
"::::"
]
},
{
Expand Down Expand Up @@ -652,7 +653,7 @@
"source": [
"### Adding new dimensions\n",
"\n",
"We use the [expand_dims example](newdim) that changes the size of the input along a single dimension.\n",
"We use the `np.expand_dims` to change the size of the input along a single dimension.\n",
"\n",
"```python\n",
"def add_new_dim(array):\n",
Expand Down
15 changes: 8 additions & 7 deletions advanced/apply_ufunc/numba-vectorization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"tags": []
},
"source": [
"<img src=\"https://numba.pydata.org/_static/numba-blue-horizontal-rgb.svg\" width=\"40%\" align=\"right\">\n",
"# Fast vectorization with Numba\n",
"\n",
"# Fast vectorization with Numba"
"<img src=\"https://numba.pydata.org/_static/numba-blue-horizontal-rgb.svg\" width=\"40%\" align=\"right\">"
]
},
{
Expand Down Expand Up @@ -241,12 +241,12 @@
"id": "18",
"metadata": {},
"source": [
"```{exercise}\n",
":label: g\n",
"::::{admonition} Exercise\n",
":class: tip\n",
"\n",
"Apply `g` to `da_dask`\n",
"```\n",
"````{solution} g\n",
"\n",
":::{admonition} Solution\n",
":class: dropdown\n",
"\n",
"```python\n",
Expand All @@ -259,7 +259,8 @@
" dask=\"parallelized\",\n",
")\n",
"```\n",
"````"
":::\n",
"::::"
]
},
{
Expand Down
Loading

0 comments on commit da26b73

Please sign in to comment.