Skip to content

Commit 2ca47a9

Browse files
authored
Migrate to exercise syntax (#196)
* Migrate to exercise syntax * fix? * Add dask to syllabus * Tweaks * more tweaks
1 parent 8b7b3fd commit 2ca47a9

File tree

6 files changed

+171
-272
lines changed

6 files changed

+171
-272
lines changed

fundamentals/02.3_aligning_data_objects.ipynb

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -249,40 +249,31 @@
249249
},
250250
{
251251
"cell_type": "markdown",
252-
"metadata": {},
253-
"source": [
254-
"### Exercise\n",
255-
"\n",
256-
"Consider the following 2D array. What are the dimensions of `array - array.mean(\"time\")`?"
257-
]
258-
},
259-
{
260-
"cell_type": "code",
261-
"execution_count": null,
262252
"metadata": {
263253
"tags": []
264254
},
265-
"outputs": [],
266255
"source": [
256+
"````{exercise}\n",
257+
":label: ex1\n",
258+
"\n",
259+
"Consider the following 2D array. What are the dimensions of `array - array.mean(\"time\")`?\n",
260+
"```python\n",
267261
"array = xr.DataArray(\n",
268262
" np.arange(12).reshape(3, 4),\n",
269263
" dims=(\"space\", \"time\"),\n",
270264
" coords={\"space\": [\"a\", \"b\", \"c\"], \"time\": [0, 1, 2, 3]},\n",
271265
" name=\"array\",\n",
272-
")"
273-
]
274-
},
275-
{
276-
"cell_type": "code",
277-
"execution_count": null,
278-
"metadata": {
279-
"tags": [
280-
"hide-output"
281-
]
282-
},
283-
"outputs": [],
284-
"source": [
285-
"(array - array.mean(\"time\")).dims"
266+
")\n",
267+
"```\n",
268+
"````\n",
269+
"\n",
270+
"````{solution} ex1\n",
271+
":class: dropdown\n",
272+
"\n",
273+
"```python\n",
274+
"(array - array.mean(\"time\")).dims\n",
275+
"```\n",
276+
"````"
286277
]
287278
},
288279
{
@@ -448,17 +439,17 @@
448439
},
449440
{
450441
"cell_type": "markdown",
451-
"metadata": {},
452-
"source": [
453-
"**Exercise** Consider the following two arrays. Write down the `x` and `y` coordinate locations for `da1 - da2`"
454-
]
455-
},
456-
{
457-
"cell_type": "code",
458-
"execution_count": null,
459-
"metadata": {},
460-
"outputs": [],
442+
"metadata": {
443+
"tags": [
444+
"hide-input"
445+
]
446+
},
461447
"source": [
448+
"````{exercise}\n",
449+
":label: dims\n",
450+
"\n",
451+
"Consider the following two arrays. Write down the `x` and `y` coordinate locations for `da1 - da2`\n",
452+
"```python\n",
462453
"da1 = xr.DataArray(\n",
463454
" np.arange(12).reshape(3, 4),\n",
464455
" dims=(\"space\", \"time\"),\n",
@@ -468,18 +459,15 @@
468459
" [0, 1],\n",
469460
" dims=\"space\",\n",
470461
" coords={\"space\": [\"b\", \"d\"]},\n",
471-
")"
472-
]
473-
},
474-
{
475-
"cell_type": "markdown",
476-
"metadata": {
477-
"tags": [
478-
"hide-input"
479-
]
480-
},
481-
"source": [
482-
"**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."
462+
")\n",
463+
"```\n",
464+
"````\n",
465+
"\n",
466+
"```{solution} dims\n",
467+
":class: dropdown\n",
468+
"\n",
469+
"`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",
470+
"```"
483471
]
484472
},
485473
{

fundamentals/03.1_computation_with_xarray.ipynb

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"<img src=\"https://docs.xarray.dev/en/stable/_static/dataset-diagram-logo.png\" align=\"right\" width=\"30%\">\n",
8-
"\n",
97
"# Basic Computation\n",
108
"\n",
119
"In this lesson, we discuss how to do scientific computations with xarray\n",
@@ -176,12 +174,9 @@
176174
"cell_type": "markdown",
177175
"metadata": {},
178176
"source": [
179-
"<div class=\"alert alert-info\">\n",
180-
" <strong>Note:</strong> <code>apply_ufunc</code> is a powerful function.\n",
181-
" It has many options for doing more complicated things.\n",
182-
" Unfortunately, we don't have time to go into more depth here.\n",
183-
" Please consult the <a href=\"https://docs.xarray.dev/en/stable/user-guide/dask.html#apply-ufunc\">documentation</a> for more details.\n",
184-
"</div>\n"
177+
"```{tip}\n",
178+
"`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",
179+
"```\n"
185180
]
186181
},
187182
{
@@ -270,24 +265,15 @@
270265
"tags": []
271266
},
272267
"source": [
273-
"### Exercise\n",
268+
"```{exercise}\n",
269+
":label: sst-mean\n",
270+
"Take the mean of `sst` in both longitude and latitude. Make a simple timeseries plot.\n",
271+
"```\n",
272+
"````{solution} sst-mean\n",
273+
":class: dropdown\n",
274274
"\n",
275-
"Take the mean of `sst` in both longitude and latitude. Make a simple timeseries\n",
276-
"plot:\n"
277-
]
278-
},
279-
{
280-
"cell_type": "code",
281-
"execution_count": null,
282-
"metadata": {
283-
"tags": [
284-
"hide-input",
285-
"hide-output"
286-
]
287-
},
288-
"outputs": [],
289-
"source": [
290-
"sst.mean([\"lat\", \"lon\"]).plot();"
275+
"sst.mean([\"lat\", \"lon\"]).plot();\n",
276+
"```\n"
291277
]
292278
}
293279
],

fundamentals/03.2_groupby_with_xarray.ipynb

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"<img src=\"https://docs.xarray.dev/en/stable/_static/dataset-diagram-logo.png\" align=\"right\" width=\"30%\">\n",
8-
"\n",
97
"# Grouped Computations\n",
108
"\n",
119
"In this lesson, we discuss how to do scientific computations with defined \"groups\" of data\n",
@@ -29,7 +27,7 @@
2927
"import matplotlib.pyplot as plt\n",
3028
"\n",
3129
"# don't expand data by default\n",
32-
"xr.set_options(display_expand_data=False)\n",
30+
"xr.set_options(display_expand_data=False, display_expand_attrs=False)\n",
3331
"\n",
3432
"%config InlineBackend.figure_format='retina'"
3533
]
@@ -217,9 +215,10 @@
217215
"`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",
218216
"\n",
219217
"\n",
220-
"<div class=\"alert alert-info\">\n",
221-
" 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",
222-
"</div>"
218+
"```{tip}\n",
219+
"\n",
220+
"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",
221+
"```"
223222
]
224223
},
225224
{
@@ -480,22 +479,17 @@
480479
"cell_type": "markdown",
481480
"metadata": {},
482481
"source": [
483-
"### Exercise\n",
482+
"```{exercise} \n",
483+
":label: annual-mean\n",
484484
"\n",
485-
"Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N"
486-
]
487-
},
488-
{
489-
"cell_type": "code",
490-
"execution_count": null,
491-
"metadata": {
492-
"tags": [
493-
"hide-output"
494-
]
495-
},
496-
"outputs": [],
497-
"source": [
498-
"ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();"
485+
"Using `groupby`, plot the annual mean time series of SST at 300°E, 50°N\n",
486+
"```\n",
487+
"````{solution} annual-mean\n",
488+
":class: dropdown\n",
489+
"```python\n",
490+
"ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50).plot();\n",
491+
"```\n",
492+
"````"
499493
]
500494
},
501495
{
@@ -539,9 +533,9 @@
539533
"cell_type": "markdown",
540534
"metadata": {},
541535
"source": [
542-
"<div class=\"alert alert-info\">\n",
543-
" <strong>Note:</strong> <code>resample</code> only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n",
544-
"</div>"
536+
"```{note}\n",
537+
"`resample` only works with proper datetime64 coordinate labels. Note the `dtype` of `time` in the repr above.\n",
538+
"```"
545539
]
546540
},
547541
{
@@ -588,26 +582,20 @@
588582
"tags": []
589583
},
590584
"source": [
591-
"### Exercise\n",
585+
"```{exercise}\n",
586+
":label: resample-mean\n",
592587
"\n",
593588
"Using `resample`, plot the annual mean time series of SST at 300°E, 50°N.\n",
594589
"\n",
595-
"Compare this output to the groupby output. What differences do you see?"
596-
]
597-
},
598-
{
599-
"cell_type": "code",
600-
"execution_count": null,
601-
"metadata": {
602-
"tags": [
603-
"hide-output",
604-
"hide-input"
605-
]
606-
},
607-
"outputs": [],
608-
"source": [
590+
"Compare this output to the groupby output. What differences do you see?\n",
591+
"```\n",
592+
"````{solution} resample-mean\n",
593+
":class: dropdown\n",
594+
"```python\n",
609595
"resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n",
610-
"resampled.plot();"
596+
"resampled.plot();\n",
597+
"```\n",
598+
"````"
611599
]
612600
},
613601
{
@@ -618,6 +606,8 @@
618606
]
619607
},
620608
"source": [
609+
"## GroupBy vs Resample \n",
610+
"\n",
621611
"Let's compare the grouped and resampled outputs.\n",
622612
"\n",
623613
"\n",
@@ -630,16 +620,14 @@
630620
"cell_type": "code",
631621
"execution_count": null,
632622
"metadata": {
633-
"tags": [
634-
"hide-output",
635-
"hide-input"
636-
]
623+
"tags": []
637624
},
638625
"outputs": [],
639626
"source": [
640627
"from IPython.display import display_html\n",
641628
"\n",
642629
"grouped = ds.groupby(\"time.year\").mean().sst.sel(lon=300, lat=50)\n",
630+
"resampled = ds.resample(time='Y').mean().sst.sel(lon=300, lat=50)\n",
643631
"display_html(grouped)\n",
644632
"display_html(resampled)"
645633
]
@@ -648,10 +636,7 @@
648636
"cell_type": "code",
649637
"execution_count": null,
650638
"metadata": {
651-
"tags": [
652-
"hide-input",
653-
"hide-output"
654-
]
639+
"tags": []
655640
},
656641
"outputs": [],
657642
"source": [

0 commit comments

Comments
 (0)