make_broadcastable#
- movement.utils.broadcasting.make_broadcastable(*, is_classmethod=False, only_broadcastable_along=None, new_dimension_name=None)[source]#
- Overloads:
is_classmethod (Literal[False]), only_broadcastable_along (str | None), new_dimension_name (str | None) → Callable[[Function1DTo1D[P, ScalarOr1D]], FunctionDaToDa[P]]
is_classmethod (Literal[True]), only_broadcastable_along (str | None), new_dimension_name (str | None) → Callable[[ClsMethod1DTo1D[Self, P, ScalarOr1D]], ClsMethodDaToDa[Self, P]]
is_classmethod (bool), only_broadcastable_along (str | None), new_dimension_name (str | None) → Callable[…, FunctionDaToDa[P] | ClsMethodDaToDa[Self, P]]
Create a decorator that allows a function to be broadcast.
- Parameters:
is_classmethod (
bool) – Whether the target of the decoration is a class method which takes theselfargument, or a standalone function that receives no implicit arguments.only_broadcastable_along (
str|None) – Whether the decorated function should only support broadcasting along this dimension. The returned function will not take thebroadcast_dimensionargument, and will use the dimension provided here as the value for this argument.new_dimension_name (
str|None) – Passed toapply_along_da_axis().
- Returns:
Decorator function that can be applied with the
@make_broadcastable(...)syntax. See Notes for a description of the action of the returned decorator.- Return type:
Notes
The returned decorator (the “
r_decorator”) extends a function that acts on a 1D sequence of values, allowing it to be broadcast along the axes of an inputxarray.DataArray.The
r_decoratortakes a single parameter,f.fshould be aCallablethat acts on 1D inputs, that is to be converted into a broadcast-able functionfr, applying the action offalong an axis of anxarray.DataArray.fshould return either scalar or 1D outputs.If
fis a class method, it should be callable asf(self, [x, y, ...], *args, **kwargs). Otherwise,fshould be callable asf([x, y, ...], *args, **kwargs).The function
frreturned by ther_decoratoris callable with the signaturefr([self,] data, *args, broadcast_dimension = str, **kwargs), where theselfargument is present only iffwas a class method.frappliesfalong thebroadcast_dimensionofdata. The*argsand**kwargsmatch those passed tof, and retain the same interpretations and effects on the result. Ifdataprovided tofris not anxarray.DataArray, it will fall back on the behaviour off(and ignore thebroadcast_dimensionargument).See also
broadcastable_methodConvenience alias for
is_classmethod = True.space_broadcastableConvenience alias for
only_broadcastable_along = "space".
Examples
Make a standalone function broadcast along the
"space"axis of anxarray.DataArray.>>> @make_broadcastable(is_classmethod=False, only_broadcast_along="space") ... def my_function(xyz_data, *args, **kwargs) ... ... # Call via the usual arguments, replacing the xyz_data argument with ... # the xarray.DataArray to broadcast over ... my_function(data_array, *args, **kwargs)
Make a class method broadcast along any axis of an
xarray.DataArray.>>> from dataclasses import dataclass >>> >>> @dataclass ... class MyClass: ... factor: float ... offset: float ... ... @make_broadcastable(is_classmethod=True) ... def manipulate_values(self, xyz_values, *args, **kwargs): ... return self.factor * sum(xyz_values) + self.offset >>> m = MyClass(factor=5.9, offset=1.0) >>> m.manipulate_values( ... data_array, *args, broadcast_dimension="time", **kwargs ... )