Skip to content

Migrate to exercise syntax #196

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

Merged
merged 6 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 35 additions & 47 deletions fundamentals/02.3_aligning_data_objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -249,40 +249,31 @@
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"Consider the following 2D array. What are the dimensions of `array - array.mean(\"time\")`?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"````{exercise}\n",
":label: ex1\n",
"\n",
"Consider the following 2D array. What are the dimensions of `array - array.mean(\"time\")`?\n",
"```python\n",
"array = xr.DataArray(\n",
" np.arange(12).reshape(3, 4),\n",
" dims=(\"space\", \"time\"),\n",
" coords={\"space\": [\"a\", \"b\", \"c\"], \"time\": [0, 1, 2, 3]},\n",
" name=\"array\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-output"
]
},
"outputs": [],
"source": [
"(array - array.mean(\"time\")).dims"
")\n",
"```\n",
"````\n",
"\n",
"````{solution} ex1\n",
":class: dropdown\n",
"\n",
"```python\n",
"(array - array.mean(\"time\")).dims\n",
"```\n",
"````"
]
},
{
Expand Down Expand Up @@ -448,17 +439,17 @@
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise** Consider the following two arrays. Write down the `x` and `y` coordinate locations for `da1 - da2`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"metadata": {
"tags": [
"hide-input"
]
},
"source": [
"````{exercise}\n",
":label: dims\n",
"\n",
"Consider the following two arrays. Write down the `x` and `y` coordinate locations for `da1 - da2`\n",
"```python\n",
"da1 = xr.DataArray(\n",
" np.arange(12).reshape(3, 4),\n",
" dims=(\"space\", \"time\"),\n",
Expand All @@ -468,18 +459,15 @@
" [0, 1],\n",
" dims=\"space\",\n",
" coords={\"space\": [\"b\", \"d\"]},\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": [
"hide-input"
]
},
"source": [
"**Answer** `x = [\"b\"], y=[0, 1, 2, 3]` . `da2` has been broadcasted to 2D (so dimension \"y\" has been inserted) and the two arrays are aligned using `join=\"inner\"` prior to subtraction."
")\n",
"```\n",
"````\n",
"\n",
"```{solution} dims\n",
":class: dropdown\n",
"\n",
"`x = [\"b\"], y=[0, 1, 2, 3]` . `da2` has been broadcasted to 2D (so dimension \"y\" has been inserted) and the two arrays are aligned using `join=\"inner\"` prior to subtraction.\n",
"```"
]
},
{
Expand Down
36 changes: 11 additions & 25 deletions fundamentals/03.1_computation_with_xarray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://docs.xarray.dev/en/stable/_static/dataset-diagram-logo.png\" align=\"right\" width=\"30%\">\n",
"\n",
"# Basic Computation\n",
"\n",
"In this lesson, we discuss how to do scientific computations with xarray\n",
Expand Down Expand Up @@ -176,12 +174,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
" <strong>Note:</strong> <code>apply_ufunc</code> is a powerful function.\n",
" It has many options for doing more complicated things.\n",
" Unfortunately, we don't have time to go into more depth here.\n",
" Please consult the <a href=\"https://docs.xarray.dev/en/stable/user-guide/dask.html#apply-ufunc\">documentation</a> for more details.\n",
"</div>\n"
"```{tip}\n",
"`apply_ufunc` is a powerful function. It has many options for doing more complicated things. Unfortunately, we don't have time to go into more depth here. See the [`apply_ufunc` tutorial material](https://tutorial.xarray.dev/advanced/apply_ufunc/apply_ufunc.html) for more.\n",
"```\n"
]
},
{
Expand Down Expand Up @@ -270,24 +265,15 @@
"tags": []
},
"source": [
"### Exercise\n",
"```{exercise}\n",
":label: sst-mean\n",
"Take the mean of `sst` in both longitude and latitude. Make a simple timeseries plot.\n",
"```\n",
"````{solution} sst-mean\n",
":class: dropdown\n",
"\n",
"Take the mean of `sst` in both longitude and latitude. Make a simple timeseries\n",
"plot:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"hide-output"
]
},
"outputs": [],
"source": [
"sst.mean([\"lat\", \"lon\"]).plot();"
"sst.mean([\"lat\", \"lon\"]).plot();\n",
"```\n"
]
}
],
Expand Down
81 changes: 33 additions & 48 deletions fundamentals/03.2_groupby_with_xarray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://docs.xarray.dev/en/stable/_static/dataset-diagram-logo.png\" align=\"right\" width=\"30%\">\n",
"\n",
"# Grouped Computations\n",
"\n",
"In this lesson, we discuss how to do scientific computations with defined \"groups\" of data\n",
Expand All @@ -29,7 +27,7 @@
"import matplotlib.pyplot as plt\n",
"\n",
"# don't expand data by default\n",
"xr.set_options(display_expand_data=False)\n",
"xr.set_options(display_expand_data=False, display_expand_attrs=False)\n",
"\n",
"%config InlineBackend.figure_format='retina'"
]
Expand Down Expand Up @@ -217,9 +215,10 @@
"`gb` is a DatasetGroupBy object. It represents a GroupBy operation and helpfully tells us the unique \"groups\" or labels found during the split step.\n",
"\n",
"\n",
"<div class=\"alert alert-info\">\n",
" Xarrays' computation methods (groupby, groupby_bins, rolling, coarsen, weighted) all return special objects that represent the basic underlying computation pattern. For e.g. `gb` above is a `DatasetGroupBy` object that represents monthly groupings of the data in `ds` . It is usually helpful to save and reuse these objects for multiple operations (e.g. a mean and standard deviation calculation).\n",
"</div>"
"```{tip}\n",
"\n",
"Xarrays' computation methods (`groupby`, `groupby_bins`, `rolling`, `coarsen`, `weighted`) all return special objects that represent the basic underlying computation pattern. For e.g. `gb` above is a `DatasetGroupBy` object that represents monthly groupings of the data in `ds` . It is usually helpful to save and reuse these objects for multiple operations (e.g. a mean and standard deviation calculation).\n",
"```"
]
},
{
Expand Down Expand Up @@ -480,22 +479,17 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"```{exercise} \n",
":label: annual-mean\n",
"\n",
"Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-output"
]
},
"outputs": [],
"source": [
"ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();"
"Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N\n",
"```\n",
"````{solution} annual-mean\n",
":class: dropdown\n",
"```python\n",
"ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();\n",
"```\n",
"````"
]
},
{
Expand Down Expand Up @@ -539,9 +533,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\">\n",
" <strong>Note:</strong> <code>resample</code> only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n",
"</div>"
"```{note}\n",
"`resample` only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n",
"```"
]
},
{
Expand Down Expand Up @@ -588,26 +582,20 @@
"tags": []
},
"source": [
"### Exercise\n",
"```{exercise}\n",
":label: resample-mean\n",
"\n",
"Using `resample`, plot the annual mean time series of SST at 300°E, 50°N.\n",
"\n",
"Compare this output to the groupby output. What differences do you see?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-output",
"hide-input"
]
},
"outputs": [],
"source": [
"Compare this output to the groupby output. What differences do you see?\n",
"```\n",
"````{solution} resample-mean\n",
":class: dropdown\n",
"```python\n",
"resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n",
"resampled.plot();"
"resampled.plot();\n",
"```\n",
"````"
]
},
{
Expand All @@ -618,6 +606,8 @@
]
},
"source": [
"## GroupBy vs Resample \n",
"\n",
"Let's compare the grouped and resampled outputs.\n",
"\n",
"\n",
Expand All @@ -630,16 +620,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-output",
"hide-input"
]
"tags": []
},
"outputs": [],
"source": [
"from IPython.display import display_html\n",
"\n",
"grouped = ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50)\n",
"resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n",
"display_html(grouped)\n",
"display_html(resampled)"
]
Expand All @@ -648,10 +636,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-input",
"hide-output"
]
"tags": []
},
"outputs": [],
"source": [
Expand Down
Loading