From 3979fca6b1e135fe46ac5899e781440514af77b3 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Tue, 15 Oct 2024 23:53:34 -0400 Subject: [PATCH 1/5] _inherited_vars -> inherited_vars --- xarray/core/formatting.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 570892bcb6b..47dd9062136 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -448,7 +448,7 @@ def coords_repr(coords: AbstractCoordinates, col_width=None, max_rows=None): def inherited_coords_repr(node: DataTree, col_width=None, max_rows=None): - coords = _inherited_vars(node._coord_variables) + coords = inherited_vars(node._coord_variables) if col_width is None: col_width = _calculate_col_width(coords) return _mapping_repr( @@ -1072,7 +1072,7 @@ def diff_datatree_repr(a: DataTree, b: DataTree, compat): return "\n".join(summary) -def _inherited_vars(mapping: ChainMap) -> dict: +def inherited_vars(mapping: ChainMap) -> dict: return {k: v for k, v in mapping.parents.items() if k not in mapping.maps[0]} @@ -1082,7 +1082,7 @@ def _datatree_node_repr(node: DataTree, show_inherited: bool) -> str: col_width = _calculate_col_width(node.variables) max_rows = OPTIONS["display_max_rows"] - inherited_coords = _inherited_vars(node._coord_variables) + inherited_coords = inherited_vars(node._coord_variables) # Only show dimensions if also showing a variable or coordinates section. show_dims = ( From 21d0e9516d38e61383484d5a9bc3b713f4b7e45e Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Tue, 15 Oct 2024 23:57:37 -0400 Subject: [PATCH 2/5] implementation using Coordinates --- xarray/core/formatting_html.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index e0a461caea7..ced5ba8aa60 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -9,6 +9,7 @@ from typing import TYPE_CHECKING from xarray.core.formatting import ( + inherited_vars, inline_index_repr, inline_variable_array_repr, short_data_repr, @@ -248,7 +249,6 @@ def array_section(obj) -> str: expand_option_name="display_expand_coords", ) - datavar_section = partial( _mapping_section, name="Data variables", @@ -382,16 +382,34 @@ def summarize_datatree_children(children: Mapping[str, DataTree]) -> str: expand_option_name="display_expand_groups", ) +inherited_coord_section = partial( + _mapping_section, + name="Inherited coordinates", + details_func=summarize_coords, + max_items_collapse=25, + expand_option_name="display_expand_coords", +) + + +def datatree_node_repr(group_title: str, node: DataTree) -> str: + from xarray.core.coordinates import Coordinates -def datatree_node_repr(group_title: str, dt: DataTree) -> str: header_components = [f"
{escape(group_title)}
"] - ds = dt._to_dataset_view(rebuild_dims=False, inherit=True) + ds = node._to_dataset_view(rebuild_dims=False, inherit=True) + node_coords = node.to_dataset(inherit=False).coords + + # use this class to get access to .xindexes property + inherited_coords = Coordinates( + coords=inherited_vars(node._coord_variables), + indexes=inherited_vars(node._indexes), + ) sections = [ - children_section(dt.children), + children_section(node.children), dim_section(ds), - coord_section(ds.coords), + coord_section(node_coords), + inherited_coord_section(inherited_coords), datavar_section(ds.data_vars), attr_section(ds.attrs), ] From ca7e3e8aea823c372146c7679143e8854f991d00 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Wed, 16 Oct 2024 00:18:31 -0400 Subject: [PATCH 3/5] datatree.DataTree -> xarray.DataTree --- xarray/core/formatting_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index ced5ba8aa60..1d3b9fb5955 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -488,5 +488,5 @@ def _wrap_datatree_repr(r: str, end: bool = False) -> str: def datatree_repr(dt: DataTree) -> str: - obj_type = f"datatree.{type(dt).__name__}" + obj_type = f"xarray.{type(dt).__name__}" return datatree_node_repr(obj_type, dt) From 01a923c91c3aae87009b754dc9f8dc1d3d40573f Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Wed, 16 Oct 2024 00:23:37 -0400 Subject: [PATCH 4/5] only show inherited coordinates on root --- xarray/core/formatting_html.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index 1d3b9fb5955..d0cb7c30e91 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -391,7 +391,7 @@ def summarize_datatree_children(children: Mapping[str, DataTree]) -> str: ) -def datatree_node_repr(group_title: str, node: DataTree) -> str: +def datatree_node_repr(group_title: str, node: DataTree, show_inherited=False) -> str: from xarray.core.coordinates import Coordinates header_components = [f"
{escape(group_title)}
"] @@ -409,7 +409,13 @@ def datatree_node_repr(group_title: str, node: DataTree) -> str: children_section(node.children), dim_section(ds), coord_section(node_coords), - inherited_coord_section(inherited_coords), + ] + + # only show inherited coordinates on the root + if show_inherited: + sections.append(inherited_coord_section(inherited_coords)) + + sections += [ datavar_section(ds.data_vars), attr_section(ds.attrs), ] @@ -489,4 +495,4 @@ def _wrap_datatree_repr(r: str, end: bool = False) -> str: def datatree_repr(dt: DataTree) -> str: obj_type = f"xarray.{type(dt).__name__}" - return datatree_node_repr(obj_type, dt) + return datatree_node_repr(obj_type, dt, show_inherited=True) From 7a6708eebafea94bb39fa11dd90a4cb3e49e1f5d Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Wed, 16 Oct 2024 10:01:59 -0400 Subject: [PATCH 5/5] test that there is an Inherited coordinates header --- xarray/tests/test_formatting_html.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/xarray/tests/test_formatting_html.py b/xarray/tests/test_formatting_html.py index 9e325b1258d..b518e7e95d9 100644 --- a/xarray/tests/test_formatting_html.py +++ b/xarray/tests/test_formatting_html.py @@ -320,6 +320,25 @@ def test_two_children( ) +class TestDataTreeInheritance: + def test_inherited_section_present(self) -> None: + dt = xr.DataTree.from_dict( + { + "/": None, + "a": None, + } + ) + with xr.set_options(display_style="html"): + html = dt._repr_html_().strip() + # checks that the section appears somewhere + assert "Inherited coordinates" in html + + # TODO how can we assert that the Inherited coordinates section does not appear in the child group? + # with xr.set_options(display_style="html"): + # child_html = dt["a"]._repr_html_().strip() + # assert "Inherited coordinates" not in child_html + + class Test__wrap_datatree_repr: """ Unit tests for _wrap_datatree_repr.