You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* main:
Fix multiple grouping with missing groups (pydata#9650)
flox: Properly propagate multiindex (pydata#9649)
Update Datatree html repr to indicate inheritance (pydata#9633)
Re-implement map_over_datasets using group_subtrees (pydata#9636)
fix zarr intersphinx (pydata#9652)
Replace black and blackdoc with ruff-format (pydata#9506)
Fix error and missing code cell in io.rst (pydata#9641)
Support alternative names for the root node in DataTree.from_dict (pydata#9638)
Updates to DataTree.equals and DataTree.identical (pydata#9627)
DOC: Clarify error message in open_dataarray (pydata#9637)
Add zip_subtrees for paired iteration over DataTrees (pydata#9623)
Type check datatree tests (pydata#9632)
Add missing `memo` argument to DataTree.__deepcopy__ (pydata#9631)
Bug fixes for DataTree indexing and aggregation (pydata#9626)
Add inherit=False option to DataTree.copy() (pydata#9628)
docs(groupby): mention deprecation of `squeeze` kwarg (pydata#9625)
Migration guide for users of old datatree repo (pydata#9598)
Reimplement Datatree typed ops (pydata#9619)
# Migration guide for users of `xarray-contrib/datatree`
2
+
3
+
_15th October 2024_
4
+
5
+
This guide is for previous users of the prototype `datatree.DataTree` class in the `xarray-contrib/datatree repository`. That repository has now been archived, and will not be maintained. This guide is intended to help smooth your transition to using the new, updated `xarray.DataTree` class.
6
+
7
+
> [!IMPORTANT]
8
+
> There are breaking changes! You should not expect that code written with `xarray-contrib/datatree` will work without any modifications. At the absolute minimum you will need to change the top-level import statement, but there are other changes too.
9
+
10
+
We have made various changes compared to the prototype version. These can be split into three categories: data model changes, which affect the hierarchal structure itself; integration with xarray's IO backends; and minor API changes, which mostly consist of renaming methods to be more self-consistent.
11
+
12
+
### Data model changes
13
+
14
+
The most important changes made are to the data model of `DataTree`. Whilst previously data in different nodes was unrelated and therefore unconstrained, now trees have "internal alignment" - meaning that dimensions and indexes in child nodes must exactly align with those in their parents.
15
+
16
+
These alignment checks happen at tree construction time, meaning there are some netCDF4 files and zarr stores that could previously be opened as `datatree.DataTree` objects using `datatree.open_datatree`, but now cannot be opened as `xr.DataTree` objects using `xr.open_datatree`. For these cases we added a new opener function `xr.open_groups`, which returns a `dict[str, Dataset]`. This is intended as a fallback for tricky cases, where the idea is that you can still open the entire contents of the file using `open_groups`, edit the `Dataset` objects, then construct a valid tree from the edited dictionary using `DataTree.from_dict`.
17
+
18
+
The alignment checks allowed us to add "Coordinate Inheritance", a much-requested feature where indexed coordinate variables are now "inherited" down to child nodes. This allows you to define common coordinates in a parent group that are then automatically available on every child node. The distinction between a locally-defined coordinate variables and an inherited coordinate that was defined on a parent node is reflected in the `DataTree.__repr__`. Generally if you prefer not to have these variables be inherited you can get more similar behaviour to the old `datatree` package by removing indexes from coordinates, as this prevents inheritance.
19
+
20
+
Tree structure checks between multiple trees (i.e., `DataTree.isomorophic`) and pairing of nodes in arithmetic has also changed. Nodes are now matched (with `xarray.group_subtrees`) based on their relative paths, without regard to the order in which child nodes are defined.
21
+
22
+
For further documentation see the page in the user guide on Hierarchical Data.
23
+
24
+
### Integrated backends
25
+
26
+
Previously `datatree.open_datatree` used a different codepath from `xarray.open_dataset`, and was hard-coded to only support opening netCDF files and Zarr stores.
27
+
Now xarray's backend entrypoint system has been generalized to include `open_datatree` and the new `open_groups`.
28
+
This means we can now extend other xarray backends to support `open_datatree`! If you are the maintainer of an xarray backend we encourage you to add support for `open_datatree` and `open_groups`!
29
+
30
+
Additionally:
31
+
- A `group` kwarg has been added to `open_datatree` for choosing which group in the file should become the root group of the created tree.
32
+
- Various performance improvements have been made, which should help when opening netCDF files and Zarr stores with large numbers of groups.
33
+
- We anticipate further performance improvements being possible for datatree IO.
34
+
35
+
### API changes
36
+
37
+
A number of other API changes have been made, which should only require minor modifications to your code:
38
+
- The top-level import has changed, from `from datatree import DataTree, open_datatree` to `from xarray import DataTree, open_datatree`. Alternatively you can now just use the `import xarray as xr` namespace convention for everything datatree-related.
39
+
- The `DataTree.ds` property has been changed to `DataTree.dataset`, though `DataTree.ds` remains as an alias for `DataTree.dataset`.
40
+
- Similarly the `ds` kwarg in the `DataTree.__init__` constructor has been replaced by `dataset`, i.e. use `DataTree(dataset=)` instead of `DataTree(ds=...)`.
41
+
- The method `DataTree.to_dataset()` still exists but now has different options for controlling which variables are present on the resulting `Dataset`, e.g. `inherit=True/False`.
42
+
-`DataTree.copy()` also has a new `inherit` keyword argument for controlling whether or not coordinates defined on parents are copied (only relevant when copying a non-root node).
43
+
- The `DataTree.parent` property is now read-only. To assign a ancestral relationships directly you must instead use the `.children` property on the parent node, which remains settable.
44
+
- Similarly the `parent` kwarg has been removed from the `DataTree.__init__` constuctor.
45
+
- DataTree objects passed to the `children` kwarg in `DataTree.__init__` are now shallow-copied.
46
+
-`DataTree.as_array` has been replaced by `DataTree.to_dataarray`.
47
+
- A number of methods which were not well tested have been (temporarily) disabled. In general we have tried to only keep things that are known to work, with the plan to increase API surface incrementally after release.
48
+
49
+
## Thank you!
50
+
51
+
Thank you for trying out `xarray-contrib/datatree`!
52
+
53
+
We welcome contributions of any kind, including good ideas that never quite made it into the original datatree repository. Please also let us know if we have forgotten to mention a change that should have been listed in this guide.
- Test the code using `Pytest <http://doc.pytest.org/en/latest/>`_. Running all tests (type ``pytest`` in the root directory) takes a while, so feel free to only run the tests you think are needed based on your PR (example: ``pytest xarray/tests/test_dataarray.py``). CI will catch any failing tests.
1070
1066
- By default, the upstream dev CI is disabled on pull request and push events. You can override this behavior per commit by adding a ``[test-upstream]`` tag to the first line of the commit message. For documentation-only commits, you can skip the CI per commit by adding a ``[skip-ci]`` tag to the first line of the commit message.
1071
1067
1072
-
-**Properly format your code** and verify that it passes the formatting guidelines set by `Black<https://black.readthedocs.io/en/stable/>`_ and `Flake8<http://flake8.pycqa.org/en/latest/>`_. See `"Code formatting" <https://docs.xarray.dev/en/stablcontributing.html#code-formatting>`_. You can use `pre-commit <https://pre-commit.com/>`_ to run these automatically on each commit.
1068
+
-**Properly format your code** and verify that it passes the formatting guidelines set by `ruff<https://github.com/astral-sh/ruff>`_. See `"Code formatting" <https://docs.xarray.dev/en/stablcontributing.html#code-formatting>`_. You can use `pre-commit <https://pre-commit.com/>`_ to run these automatically on each commit.
1073
1069
1074
1070
- Run ``pre-commit run --all-files`` in the root directory. This may modify some files. Confirm and commit any formatting changes.
Copy file name to clipboardexpand all lines: doc/user-guide/hierarchical-data.rst
+63-18
Original file line number
Diff line number
Diff line change
@@ -362,21 +362,26 @@ This returns an iterable of nodes, which yields them in depth-first order.
362
362
for node in vertebrates.subtree:
363
363
print(node.path)
364
364
365
-
A very useful pattern is to use :py:class:`~xarray.DataTree.subtree` conjunction with the :py:class:`~xarray.DataTree.path` property to manipulate the nodes however you wish,
366
-
then rebuild a new tree using :py:meth:`xarray.DataTree.from_dict()`.
365
+
Similarly, :py:class:`~xarray.DataTree.subtree_with_keys` returns an iterable of
366
+
relative paths and corresponding nodes.
367
367
368
+
A very useful pattern is to iterate over :py:class:`~xarray.DataTree.subtree_with_keys`
369
+
to manipulate nodes however you wish, then rebuild a new tree using
370
+
:py:meth:`xarray.DataTree.from_dict()`.
368
371
For example, we could keep only the nodes containing data by looping over all nodes,
369
372
checking if they contain any data using :py:class:`~xarray.DataTree.has_data`,
370
373
then rebuilding a new tree using only the paths of those nodes:
371
374
372
375
.. ipython:: python
373
376
374
-
non_empty_nodes = {node.path: node.dataset for node in dt.subtree if node.has_data}
377
+
non_empty_nodes = {
378
+
path: node.dataset for path, node in dt.subtree_with_keys if node.has_data
379
+
}
375
380
xr.DataTree.from_dict(non_empty_nodes)
376
381
377
382
You can see this tree is similar to the ``dt`` object above, except that it is missing the empty nodes ``a/c`` and ``a/c/d``.
378
383
379
-
(If you want to keep the name of the root node, you will need to add the ``name`` kwarg to :py:class:`~xarray.DataTree.from_dict`, i.e. ``DataTree.from_dict(non_empty_nodes, name=dt.root.name)``.)
384
+
(If you want to keep the name of the root node, you will need to add the ``name`` kwarg to :py:class:`~xarray.DataTree.from_dict`, i.e. ``DataTree.from_dict(non_empty_nodes, name=dt.name)``.)
380
385
381
386
.. _manipulating trees:
382
387
@@ -573,38 +578,78 @@ Then calculate the RMS value of these signals:
573
578
574
579
.. _multiple trees:
575
580
576
-
We can also use the :py:meth:`~xarray.map_over_datasets` decorator to promote a function which accepts datasets into one which
577
-
accepts datatrees.
581
+
We can also use :py:func:`~xarray.map_over_datasets` to apply a function over
582
+
the data in multiple trees, by passing the trees as positional arguments.
578
583
579
584
Operating on Multiple Trees
580
585
---------------------------
581
586
582
587
The examples so far have involved mapping functions or methods over the nodes of a single tree,
583
588
but we can generalize this to mapping functions over multiple trees at once.
584
589
590
+
Iterating Over Multiple Trees
591
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
592
+
593
+
To iterate over the corresponding nodes in multiple trees, use
594
+
:py:func:`~xarray.group_subtrees` instead of
595
+
:py:class:`~xarray.DataTree.subtree_with_keys`. This combines well with
596
+
:py:meth:`xarray.DataTree.from_dict()` to build a new tree:
``xarray.register_datatree_accessor`` and ``xarray.testing.assert_isomorphic``.
28
28
By `Owen Littlejohns <https://github.com/owenlittlejohns>`_,
29
29
`Eni Awowale <https://github.com/eni-awowale>`_,
30
30
`Matt Savoie <https://github.com/flamingbear>`_,
31
31
`Stephan Hoyer <https://github.com/shoyer>`_ and
32
32
`Tom Nicholas <https://github.com/TomNicholas>`_.
33
+
- A migration guide for users of the prototype `xarray-contrib/datatree repository <https://github.com/xarray-contrib/datatree>`_ has been added, and can be found in the `DATATREE_MIGRATION_GUIDE.md` file in the repository root.
34
+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
33
35
- Added zarr backends for :py:func:`open_groups` (:issue:`9430`, :pull:`9469`).
34
36
By `Eni Awowale <https://github.com/eni-awowale>`_.
35
37
- Added support for vectorized interpolation using additional interpolators
@@ -65,11 +67,13 @@ Bug fixes
65
67
the non-missing times could in theory be encoded with integers
66
68
(:issue:`9488`, :pull:`9497`). By `Spencer Clark
67
69
<https://github.com/spencerkclark>`_.
68
-
- Fix a few bugs affecting groupby reductions with `flox`. (:issue:`8090`, :issue:`9398`).
70
+
- Fix a few bugs affecting groupby reductions with `flox`. (:issue:`8090`, :issue:`9398`, :issue:`9648`).
69
71
By `Deepak Cherian <https://github.com/dcherian>`_.
70
72
- Fix the safe_chunks validation option on the to_zarr method
71
73
(:issue:`5511`, :pull:`9559`). By `Joseph Nowak
72
74
<https://github.com/josephnowak>`_.
75
+
- Fix binning by multiple variables where some bins have no observations. (:issue:`9630`).
76
+
By `Deepak Cherian <https://github.com/dcherian>`_.
0 commit comments