Internal helper to extract data from e.g. pandas and xarray objects.
(x)
| 2493 | |
| 2494 | |
| 2495 | def _unpack_to_numpy(x): |
| 2496 | """Internal helper to extract data from e.g. pandas and xarray objects.""" |
| 2497 | if isinstance(x, np.ndarray): |
| 2498 | # If numpy, return directly |
| 2499 | return x |
| 2500 | if hasattr(x, 'to_numpy'): |
| 2501 | # Assume that any to_numpy() method actually returns a numpy array |
| 2502 | return x.to_numpy() |
| 2503 | if hasattr(x, 'values'): |
| 2504 | xtmp = x.values |
| 2505 | # For example a dict has a 'values' attribute, but it is not a property |
| 2506 | # so in this case we do not want to return a function |
| 2507 | if isinstance(xtmp, np.ndarray): |
| 2508 | return xtmp |
| 2509 | if _is_torch_array(x) \ |
| 2510 | or _is_jax_array(x) \ |
| 2511 | or _is_tensorflow_array(x) \ |
| 2512 | or _is_mlx_array(x): |
| 2513 | # using np.asarray() instead of explicitly __array__(), as the latter is |
| 2514 | # only _one_ of many methods, and it's the last resort, see also |
| 2515 | # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy |
| 2516 | # therefore, let arrays do better if they can |
| 2517 | xtmp = np.asarray(x) |
| 2518 | |
| 2519 | # In case np.asarray method does not return a numpy array in future |
| 2520 | if isinstance(xtmp, np.ndarray): |
| 2521 | return xtmp |
| 2522 | return x |
| 2523 | |
| 2524 | |
| 2525 | def _auto_format_str(fmt, value): |
no test coverage detected
searching dependent graphs…