(*values)
| 47 | |
| 48 | |
| 49 | def get_array_namespace(*values): |
| 50 | def _get_single_namespace(x): |
| 51 | if hasattr(x, "__array_namespace__"): |
| 52 | return x.__array_namespace__() |
| 53 | elif isinstance(x, array_type("cupy")): |
| 54 | # cupy is fully compliant from xarray's perspective, but will not expose |
| 55 | # __array_namespace__ until at least v14. Special case it for now |
| 56 | import cupy as cp |
| 57 | |
| 58 | return cp |
| 59 | else: |
| 60 | return np |
| 61 | |
| 62 | namespaces = {_get_single_namespace(t) for t in values} |
| 63 | non_numpy = namespaces - {np} |
| 64 | |
| 65 | if len(non_numpy) > 1: |
| 66 | names = [module.__name__ for module in non_numpy] |
| 67 | raise TypeError(f"Mixed array types {names} are not supported.") |
| 68 | elif non_numpy: |
| 69 | [xp] = non_numpy |
| 70 | else: |
| 71 | xp = np |
| 72 | |
| 73 | return xp |
| 74 | |
| 75 | |
| 76 | def to_like_array(array, like): |
no test coverage detected
searching dependent graphs…