Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0009acf

Browse files
committed
Add _is_mlx_array and update _unpack_to_numpy
1 parent c4b9963 commit 0009acf

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

lib/matplotlib/cbook.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,18 @@ def _is_jax_array(x):
24112411
and isinstance(x, tp))
24122412

24132413

2414+
def _is_mlx_array(x):
2415+
"""Return whether *x* is a MLX Array."""
2416+
try:
2417+
# We're intentionally not attempting to import mlx. If somebody
2418+
# has created a mlx array, mlx should already be in sys.modules.
2419+
tp = sys.modules.get("mlx.core").array
2420+
except AttributeError:
2421+
return False # Module not imported or a nonstandard module with no Array attr.
2422+
return (isinstance(tp, type) # Just in case it's a very nonstandard module.
2423+
and isinstance(x, tp))
2424+
2425+
24142426
def _is_pandas_dataframe(x):
24152427
"""Check if *x* is a Pandas DataFrame."""
24162428
try:
@@ -2454,7 +2466,10 @@ def _unpack_to_numpy(x):
24542466
# so in this case we do not want to return a function
24552467
if isinstance(xtmp, np.ndarray):
24562468
return xtmp
2457-
if _is_torch_array(x) or _is_jax_array(x) or _is_tensorflow_array(x):
2469+
if _is_torch_array(x) \
2470+
or _is_jax_array(x) \
2471+
or _is_tensorflow_array(x) \
2472+
or _is_mlx_array(x):
24582473
# using np.asarray() instead of explicitly __array__(), as the latter is
24592474
# only _one_ of many methods, and it's the last resort, see also
24602475
# https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy

0 commit comments

Comments
 (0)