(
self,
rebuild_dims: bool,
inherit: bool | Literal["all_coords", "indexes"] = True,
)
| 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: |
no test coverage detected