Wrapper function for data-type agnostic processing. First converts input arrays to PyTorch tensors or NumPy arrays for middle calculation, then convert output to original data-type if `recover=True`. Args: to_torch (bool): Whether to convert to PyTorch tensors for middle
(to_torch: bool = True,
apply_to: Tuple[str, ...] = tuple(),
template_arg_name_: Optional[str] = None,
recover: bool = True)
| 10 | |
| 11 | |
| 12 | def array_converter(to_torch: bool = True, |
| 13 | apply_to: Tuple[str, ...] = tuple(), |
| 14 | template_arg_name_: Optional[str] = None, |
| 15 | recover: bool = True) -> Callable: |
| 16 | """Wrapper function for data-type agnostic processing. |
| 17 | |
| 18 | First converts input arrays to PyTorch tensors or NumPy arrays for middle |
| 19 | calculation, then convert output to original data-type if `recover=True`. |
| 20 | |
| 21 | Args: |
| 22 | to_torch (bool): Whether to convert to PyTorch tensors for middle |
| 23 | calculation. Defaults to True. |
| 24 | apply_to (Tuple[str]): The arguments to which we apply data-type |
| 25 | conversion. Defaults to an empty tuple. |
| 26 | template_arg_name_ (str, optional): Argument serving as the template |
| 27 | (return arrays should have the same dtype and device as the |
| 28 | template). Defaults to None. If None, we will use the first |
| 29 | argument in `apply_to` as the template argument. |
| 30 | recover (bool): Whether or not to recover the wrapped function outputs |
| 31 | to the `template_arg_name_` type. Defaults to True. |
| 32 | |
| 33 | Raises: |
| 34 | ValueError: When template_arg_name_ is not among all args, or when |
| 35 | apply_to contains an arg which is not among all args, a ValueError |
| 36 | will be raised. When the template argument or an argument to |
| 37 | convert is a list or tuple, and cannot be converted to a NumPy |
| 38 | array, a ValueError will be raised. |
| 39 | TypeError: When the type of the template argument or an argument to |
| 40 | convert does not belong to the above range, or the contents of such |
| 41 | an list-or-tuple-type argument do not share the same data type, a |
| 42 | TypeError will be raised. |
| 43 | |
| 44 | Returns: |
| 45 | Callable: Wrapped function. |
| 46 | |
| 47 | Examples: |
| 48 | >>> import torch |
| 49 | >>> import numpy as np |
| 50 | >>> |
| 51 | >>> # Use torch addition for a + b, |
| 52 | >>> # and convert return values to the type of a |
| 53 | >>> @array_converter(apply_to=('a', 'b')) |
| 54 | >>> def simple_add(a, b): |
| 55 | >>> return a + b |
| 56 | >>> |
| 57 | >>> a = np.array([1.1]) |
| 58 | >>> b = np.array([2.2]) |
| 59 | >>> simple_add(a, b) |
| 60 | >>> |
| 61 | >>> # Use numpy addition for a + b, |
| 62 | >>> # and convert return values to the type of b |
| 63 | >>> @array_converter(to_torch=False, apply_to=('a', 'b'), |
| 64 | >>> template_arg_name_='b') |
| 65 | >>> def simple_add(a, b): |
| 66 | >>> return a + b |
| 67 | >>> |
| 68 | >>> simple_add(a, b) |
| 69 | >>> |
nothing calls this directly
no outgoing calls
no test coverage detected