Constructs reduction methods built on a numpy reduction function (e.g. sum), a numbagg reduction function (e.g. move_sum), a bottleneck reduction function (e.g. move_sum), or a Rolling reduction (_mean). The logic here for which function to run is quite diffuse, across this
( # type: ignore[misc]
name: str,
fillna: Any,
rolling_agg_func: Callable | None = None,
automatic_rechunk: bool = False,
)
| 145 | return len(self.dim) |
| 146 | |
| 147 | def _reduce_method( # type: ignore[misc] |
| 148 | name: str, |
| 149 | fillna: Any, |
| 150 | rolling_agg_func: Callable | None = None, |
| 151 | automatic_rechunk: bool = False, |
| 152 | ) -> Callable[..., T_Xarray]: |
| 153 | """Constructs reduction methods built on a numpy reduction function (e.g. sum), |
| 154 | a numbagg reduction function (e.g. move_sum), a bottleneck reduction function |
| 155 | (e.g. move_sum), or a Rolling reduction (_mean). |
| 156 | |
| 157 | The logic here for which function to run is quite diffuse, across this method & |
| 158 | _array_reduce. Arguably we could refactor this. But one constraint is that we |
| 159 | need context of xarray options, of the functions each library offers, of |
| 160 | the array (e.g. dtype). |
| 161 | |
| 162 | Set automatic_rechunk=True when the reduction method makes a memory copy. |
| 163 | """ |
| 164 | if rolling_agg_func: |
| 165 | array_agg_func = None |
| 166 | else: |
| 167 | array_agg_func = getattr(duck_array_ops, name) |
| 168 | |
| 169 | bottleneck_move_func = getattr(bottleneck, "move_" + name, None) |
| 170 | if module_available("numbagg"): |
| 171 | import numbagg # type: ignore[import-not-found, unused-ignore] |
| 172 | |
| 173 | numbagg_move_func = getattr(numbagg, "move_" + name, None) |
| 174 | else: |
| 175 | numbagg_move_func = None |
| 176 | |
| 177 | def method(self, keep_attrs=None, **kwargs): |
| 178 | keep_attrs = self._get_keep_attrs(keep_attrs) |
| 179 | |
| 180 | return self._array_reduce( |
| 181 | array_agg_func=array_agg_func, |
| 182 | bottleneck_move_func=bottleneck_move_func, |
| 183 | numbagg_move_func=numbagg_move_func, |
| 184 | rolling_agg_func=rolling_agg_func, |
| 185 | keep_attrs=keep_attrs, |
| 186 | fillna=fillna, |
| 187 | sliding_window_view_kwargs=dict(automatic_rechunk=automatic_rechunk), |
| 188 | **kwargs, |
| 189 | ) |
| 190 | |
| 191 | method.__name__ = name |
| 192 | method.__doc__ = _ROLLING_REDUCE_DOCSTRING_TEMPLATE.format(name=name) |
| 193 | return method |
| 194 | |
| 195 | def _mean(self, keep_attrs, **kwargs): |
| 196 | result = self.sum(keep_attrs=False, **kwargs) |
no test coverage detected