From 68dbf32e9b3d91cdce6f011f8e10d873592b9971 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 8 Jul 2025 11:52:53 -0700 Subject: [PATCH 1/4] reword --- intermediate/hierarchical_computation.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intermediate/hierarchical_computation.ipynb b/intermediate/hierarchical_computation.ipynb index 17760d58..88297a94 100644 --- a/intermediate/hierarchical_computation.ipynb +++ b/intermediate/hierarchical_computation.ipynb @@ -137,7 +137,7 @@ "source": [ "## Applying functions designed for `Dataset` with `map_over_datasets`\n", "\n", - "What if we wanted to convert the data to log-space? For a `Dataset` or `DataArray`, we could just use {py:func}`xarray.ufuncs.log`, but that does not support `DataTree` objects, yet:" + "What if we wanted to apply a element-wise function, for example to convert the data to log-space? For a `Dataset` or `DataArray`, we could just use {py:func}`xarray.ufuncs.log`, but that does not support `DataTree` objects, yet:" ] }, { From 7c28d8067b4594520f73468b6d8b8650eb82bd5b Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 8 Jul 2025 11:53:09 -0700 Subject: [PATCH 2/4] escape hatches and a exercise --- intermediate/hierarchical_computation.ipynb | 72 +++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/intermediate/hierarchical_computation.ipynb b/intermediate/hierarchical_computation.ipynb index 88297a94..03e68ab9 100644 --- a/intermediate/hierarchical_computation.ipynb +++ b/intermediate/hierarchical_computation.ipynb @@ -235,6 +235,78 @@ "\n", "tree.map_over_datasets(demean)" ] + }, + { + "cell_type": "markdown", + "id": "21", + "metadata": {}, + "source": [ + "## Escape hatches\n", + "\n", + "For some more complex operations, it might make sense to work on {py:class}`xarray.Dataset` or {py:class}`xarray.DataArray` objects and reassemble the tree afterwards.\n", + "\n", + "Let's look at a new dataset:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22", + "metadata": {}, + "outputs": [], + "source": [ + "precipitation = xr.tutorial.open_datatree(\"precipitation.nc4\").load()\n", + "precipitation" + ] + }, + { + "cell_type": "markdown", + "id": "23", + "metadata": {}, + "source": [ + "Suppose we wanted to interpolate the observed precipitation to the modelled precipitation. We could use `map_over_datasets` for this, but we can also have a bit more control:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "interpolated = xr.DataTree.from_dict(\n", + " {\n", + " \"/\": precipitation.ds,\n", + " \"/observed\": precipitation[\"/observed\"].ds.interp(\n", + " lat=precipitation[\"/reanalysis/lat\"],\n", + " lon=precipitation[\"/reanalysis/lon\"],\n", + " ),\n", + " \"/reanalysis\": precipitation[\"/reanalysis\"],\n", + " }\n", + ")\n", + "interpolated" + ] + }, + { + "cell_type": "markdown", + "id": "25", + "metadata": {}, + "source": [ + "::::{admonition} Exercise\n", + ":class: tip\n", + "Compute the difference between total observed and modelled precipitation, and plot the result.\n", + "\n", + ":::{admonition} Solution\n", + ":class: dropdown\n", + "\n", + "```python\n", + "total = precipitation.sum(dim=[\"lon\", \"lat\"])\n", + "difference = total[\"/observed/precipitation\"] - total[\"/reanalysis/precipitation\"]\n", + "difference.plot()\n", + "```\n", + ":::\n", + "::::\n" + ] } ], "metadata": { From 745e95be848d07b3271c4d81c71bb603cc1608c8 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 8 Jul 2025 12:28:26 -0700 Subject: [PATCH 3/4] replace `xr.ufuncs.log` with `np.log` --- intermediate/hierarchical_computation.ipynb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/intermediate/hierarchical_computation.ipynb b/intermediate/hierarchical_computation.ipynb index 03e68ab9..0022dc3f 100644 --- a/intermediate/hierarchical_computation.ipynb +++ b/intermediate/hierarchical_computation.ipynb @@ -137,7 +137,7 @@ "source": [ "## Applying functions designed for `Dataset` with `map_over_datasets`\n", "\n", - "What if we wanted to apply a element-wise function, for example to convert the data to log-space? For a `Dataset` or `DataArray`, we could just use {py:func}`xarray.ufuncs.log`, but that does not support `DataTree` objects, yet:" + "What if we wanted to apply a element-wise function, for example to convert the data to log-space? For a `DataArray` we could just use {py:func}`numpy.log`, but this is not supported for `DataTree` objects:" ] }, { @@ -147,7 +147,7 @@ "metadata": {}, "outputs": [], "source": [ - "xr.ufuncs.log(tree)" + "np.log(tree)" ] }, { @@ -155,8 +155,6 @@ "id": "13", "metadata": {}, "source": [ - "Note how the result is a empty `Dataset`?\n", - "\n", "To map a function to all nodes, we can use {py:func}`xarray.map_over_datasets` and {py:meth}`xarray.DataTree.map_over_datasets`: " ] }, @@ -201,12 +199,7 @@ "cell_type": "code", "execution_count": null, "id": "18", - "metadata": { - "tags": [ - "raises-exception", - "hide-output" - ] - }, + "metadata": {}, "outputs": [], "source": [ "tree.map_over_datasets(demean)" From 6b7f40e6657370ebf8d1c8b39cd2f43bcdb47f51 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 8 Jul 2025 12:59:03 -0700 Subject: [PATCH 4/4] add tags --- intermediate/hierarchical_computation.ipynb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/intermediate/hierarchical_computation.ipynb b/intermediate/hierarchical_computation.ipynb index 0022dc3f..2c73a8d9 100644 --- a/intermediate/hierarchical_computation.ipynb +++ b/intermediate/hierarchical_computation.ipynb @@ -144,7 +144,11 @@ "cell_type": "code", "execution_count": null, "id": "12", - "metadata": {}, + "metadata": { + "tags": [ + "raises-exception" + ] + }, "outputs": [], "source": [ "np.log(tree)" @@ -199,7 +203,11 @@ "cell_type": "code", "execution_count": null, "id": "18", - "metadata": {}, + "metadata": { + "tags": [ + "raises-exception" + ] + }, "outputs": [], "source": [ "tree.map_over_datasets(demean)"