Coverage for install/scipp/core/dimensions.py: 69%
32 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-04-28 01:28 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-04-28 01:28 +0000
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3# @author Simon Heybrock
4from typing import Optional
6from .._scipp.core import CoordError, DataArray, Dataset, Variable
7from ..typing import VariableLikeType
8from .argument_handlers import combine_dict_args
9from .bins import bins
10from .variable import scalar
13def _rename_dims(
14 self: VariableLikeType, dims_dict: Optional[dict[str, str]] = None, /, **names: str
15) -> VariableLikeType:
16 """Rename dimensions.
18 The renaming can be defined:
20 - using a dict mapping the old to new names, e.g.
21 ``rename_dims({'x': 'a', 'y': 'b'})``
22 - using keyword arguments, e.g. ``rename_dims(x='a', y='b')``
24 In both cases, x is renamed to a and y to b.
26 Dimensions not specified in either input are unchanged.
28 This function only renames dimensions.
29 See the ``rename`` method to also rename coordinates and attributes.
31 Parameters
32 ----------
33 dims_dict:
34 Dictionary mapping old to new names.
35 names:
36 Mapping of old to new names as keyword arguments.
38 Returns
39 -------
40 :
41 A new object with renamed dimensions.
42 """
43 return self._rename_dims(combine_dict_args(dims_dict, names))
46def _rename_variable(
47 var: Variable, dims_dict: Optional[dict[str, str]] = None, /, **names: str
48) -> Variable:
49 """Rename dimension labels.
51 The renaming can be defined:
53 - using a dict mapping the old to new names, e.g. ``rename({'x': 'a', 'y': 'b'})``
54 - using keyword arguments, e.g. ``rename(x='a', y='b')``
56 In both cases, x is renamed to a and y to b.
58 Dimensions not specified in either input are unchanged.
60 Parameters
61 ----------
62 dims_dict:
63 Dictionary mapping old to new names.
64 names:
65 Mapping of old to new names as keyword arguments.
67 Returns
68 -------
69 :
70 A new variable with renamed dimensions which shares a buffer with the input.
72 See Also
73 --------
74 scipp.Variable.rename_dims:
75 Equivalent for ``Variable`` but differs for ``DataArray`` and ``Dataset``.
76 """
77 return var.rename_dims(combine_dict_args(dims_dict, names))
80def _rename_data_array(
81 da: DataArray, dims_dict: Optional[dict[str, str]] = None, /, **names: str
82) -> DataArray:
83 """Rename the dimensions, coordinates, and attributes.
85 The renaming can be defined:
87 - using a dict mapping the old to new names, e.g. ``rename({'x': 'a', 'y': 'b'})``
88 - using keyword arguments, e.g. ``rename(x='a', y='b')``
90 In both cases, x is renamed to a and y to b.
92 Names not specified in either input are unchanged.
94 Parameters
95 ----------
96 dims_dict:
97 Dictionary mapping old to new names.
98 names:
99 Mapping of old to new names as keyword arguments.
101 Returns
102 -------
103 :
104 A new data array with renamed dimensions, coordinates, and attributes.
105 Buffers are shared with the input.
107 See Also
108 --------
109 scipp.DataArray.rename_dims:
110 Only rename dimensions, not coordinates and attributes.
111 """
112 renaming_dict = combine_dict_args(dims_dict, names)
113 out = da.rename_dims(renaming_dict)
114 if out.bins is not None:
115 out.data = bins(**out.bins.constituents)
116 for old, new in renaming_dict.items():
117 if new in out.deprecated_meta:
118 raise CoordError(
119 f"Cannot rename '{old}' to '{new}', since a coord or attr named {new} "
120 "already exists."
121 )
122 for meta in (out.coords, out.deprecated_attrs):
123 if old in meta:
124 meta[new] = meta.pop(old)
125 if out.bins is not None:
126 for meta in (out.bins.coords, out.bins.deprecated_attrs):
127 if old in meta:
128 meta[new] = meta.pop(old)
129 return out
132def _rename_dataset(
133 ds: Dataset, dims_dict: Optional[dict[str, str]] = None, /, **names: str
134) -> Dataset:
135 """Rename the dimensions, coordinates and attributes of all the items.
137 The renaming can be defined:
139 - using a dict mapping the old to new names, e.g. ``rename({'x': 'a', 'y': 'b'})``
140 - using keyword arguments, e.g. ``rename(x='a', y='b')``
142 In both cases, x is renamed to a and y to b.
144 Names not specified in either input are unchanged.
146 Parameters
147 ----------
148 dims_dict:
149 Dictionary mapping old to new names.
150 names:
151 Mapping of old to new names as keyword arguments.
153 Returns
154 -------
155 :
156 A new dataset with renamed dimensions, coordinates, and attributes.
157 Buffers are shared with the input.
159 See Also
160 --------
161 scipp.Dataset.rename_dims:
162 Only rename dimensions, not coordinates and attributes.
163 """
164 dims_dict = combine_dict_args(dims_dict, names)
165 if len(ds) != 0:
166 return Dataset(
167 {key: _rename_data_array(value, dims_dict) for key, value in ds.items()}
168 )
169 # This relies on broadcast and DataArray.__init__ not making copies
170 # to avoid allocating too much extra memory.
171 dummy = DataArray(
172 scalar(0).broadcast(dims=ds.dims, shape=ds.shape), coords=ds.coords
173 )
174 return Dataset(coords=_rename_data_array(dummy, dims_dict).coords)