MCPcopy
hub / github.com/pydata/xarray / _to_dataset_view

Method _to_dataset_view

xarray/core/datatree.py:621–671  ·  view source on GitHub ↗
(
        self,
        rebuild_dims: bool,
        inherit: bool | Literal["all_coords", "indexes"] = True,
    )

Source from the content-addressed store, hash-verified

619 return ChainMap(self._node_indexes, *(p._node_indexes for p in self.parents))
620
621 def _to_dataset_view(
622 self,
623 rebuild_dims: bool,
624 inherit: bool | Literal["all_coords", "indexes"] = True,
625 ) -> DatasetView:
626 coord_vars, indexes = self._resolve_inherit(inherit)
627 variables = dict(self._data_variables)
628 variables |= coord_vars
629 if rebuild_dims:
630 dims = calculate_dimensions(variables)
631 elif inherit:
632 # Note: rebuild_dims=False with inherit=True can create
633 # technically invalid Dataset objects because it still includes
634 # dimensions that are only defined on parent data variables
635 # (i.e. not present on any parent coordinate variables).
636 #
637 # For example:
638 # >>> tree = DataTree.from_dict(
639 # ... {
640 # ... "/": xr.Dataset({"foo": ("x", [1, 2])}), # x has size 2
641 # ... "/b": xr.Dataset(),
642 # ... }
643 # ... )
644 # >>> ds = tree["b"]._to_dataset_view(rebuild_dims=False, inherit=True)
645 # >>> ds
646 # <xarray.DatasetView> Size: 0B
647 # Dimensions: (x: 2)
648 # Dimensions without coordinates: x
649 # Data variables:
650 # *empty*
651 #
652 # Notice the "x" dimension is still defined, even though there are no variables
653 # or coordinates.
654 #
655 # Normally this is not supposed to be possible in xarray's data model,
656 # but here it is useful internally for use cases where we
657 # want to inherit everything from parents nodes, e.g., for align() and repr().
658 #
659 # The user should never be able to see this dimension via public API.
660 dims = dict(self._dims)
661 else:
662 dims = dict(self._node_dims)
663 return DatasetView._constructor(
664 variables=variables,
665 coord_names=set(coord_vars),
666 dims=dims,
667 attrs=self._attrs,
668 indexes=indexes,
669 encoding=self._encoding,
670 close=None,
671 )
672
673 @property
674 def dataset(self) -> DatasetView:

Callers 6

datasetMethod · 0.95
_copy_nodeMethod · 0.95
_datatree_node_sectionsFunction · 0.80
_pre_attachMethod · 0.80
_replace_nodeMethod · 0.80
_update_coordsMethod · 0.80

Calls 3

_resolve_inheritMethod · 0.95
calculate_dimensionsFunction · 0.85
_constructorMethod · 0.80

Tested by

no test coverage detected