Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix _is_tensorflow_array.#30114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
oscargus merged 1 commit intomatplotlib:mainfromanntzer:ita
May 29, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 40 additions & 38 deletionslib/matplotlib/cbook.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2331,42 +2331,56 @@


def _is_torch_array(x):
"""Check if 'x' is a PyTorch Tensor."""
"""Return whether *x* is a PyTorch Tensor."""
try:
#we're intentionally not attempting to import torch. If somebody
# has created a torch array, torch should already be in sys.modules
return isinstance(x, sys.modules['torch'].Tensor)
exceptException: # TypeError, KeyError,AttributeError, maybe others?
# we're attempting to access attributes on imported modules which
#may have arbitrary user code, so we deliberately catch all exceptions
return False
#We're intentionally not attempting to import torch. If somebody
# has created a torch array, torch should already be in sys.modules.
tp = sys.modules.get("torch").Tensor
except AttributeError:
return False # Module not imported or a nonstandard module with no Tensor attr.
return (isinstance(tp, type) #Just in case it's a very nonstandard module.
and isinstance(x, tp))


def _is_jax_array(x):
"""Check if 'x' is a JAX Array."""
"""Return whether *x* is a JAX Array."""
try:
# we're intentionally not attempting to import jax. If somebody
# has created a jax array, jax should already be in sys.modules
return isinstance(x, sys.modules['jax'].Array)
except Exception: # TypeError, KeyError, AttributeError, maybe others?
# we're attempting to access attributes on imported modules which
# may have arbitrary user code, so we deliberately catch all exceptions
return False
# We're intentionally not attempting to import jax. If somebody
# has created a jax array, jax should already be in sys.modules.
tp = sys.modules.get("jax").Array
except AttributeError:
return False # Module not imported or a nonstandard module with no Array attr.
return (isinstance(tp, type) # Just in case it's a very nonstandard module.
and isinstance(x, tp))


def _is_pandas_dataframe(x):
"""Check if *x* is a Pandas DataFrame."""
try:
# We're intentionally not attempting to import Pandas. If somebody
# has created a Pandas DataFrame, Pandas should already be in sys.modules.
tp = sys.modules.get("pandas").DataFrame
except AttributeError:
return False # Module not imported or a nonstandard module with no Array attr.
return (isinstance(tp, type) # Just in case it's a very nonstandard module.
and isinstance(x, tp))


def _is_tensorflow_array(x):
"""Check if 'x' is a TensorFlow Tensor or Variable."""
"""Return whether *x* is a TensorFlow Tensor or Variable."""
try:
# we're intentionally not attempting to import TensorFlow. If somebody
# has created a TensorFlow array, TensorFlow should already be in sys.modules
# we use `is_tensor` to not depend on the class structure of TensorFlow
# arrays, as `tf.Variables` are not instances of `tf.Tensor`
# (they both convert the same way)
return isinstance(x, sys.modules['tensorflow'].is_tensor(x))
except Exception: # TypeError, KeyError, AttributeError, maybe others?
# we're attempting to access attributes on imported modules which
# may have arbitrary user code, so we deliberately catch all exceptions
# We're intentionally not attempting to import TensorFlow. If somebody
# has created a TensorFlow array, TensorFlow should already be in
# sys.modules we use `is_tensor` to not depend on the class structure
# of TensorFlow arrays, as `tf.Variables` are not instances of
# `tf.Tensor` (they both convert the same way).
is_tensor = sys.modules.get("tensorflow").is_tensor
except AttributeError:
return False
try:
return is_tensor(x)
except Exception:
return False # Just in case it's a very nonstandard module.

Check warning on line 2383 in lib/matplotlib/cbook.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/cbook.py#L2382-L2383

Added lines #L2382 - L2383 were not covered by tests


def _unpack_to_numpy(x):
Expand DownExpand Up@@ -2421,15 +2435,3 @@
return fmt % (value,)
except (TypeError, ValueError):
return fmt.format(value)


def _is_pandas_dataframe(x):
"""Check if 'x' is a Pandas DataFrame."""
try:
# we're intentionally not attempting to import Pandas. If somebody
# has created a Pandas DataFrame, Pandas should already be in sys.modules
return isinstance(x, sys.modules['pandas'].DataFrame)
except Exception: # TypeError, KeyError, AttributeError, maybe others?
# we're attempting to access attributes on imported modules which
# may have arbitrary user code, so we deliberately catch all exceptions
return False
3 changes: 3 additions & 0 deletionslib/matplotlib/tests/test_cbook.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1000,6 +1000,7 @@ def __array__(self):
torch_tensor = torch.Tensor(data)

result = cbook._unpack_to_numpy(torch_tensor)
assert isinstance(result, np.ndarray)
# compare results, do not check for identity: the latter would fail
# if not mocked, and the implementation does not guarantee it
# is the same Python object, just the same values.
Expand DownExpand Up@@ -1028,6 +1029,7 @@ def __array__(self):
jax_array = jax.Array(data)

result = cbook._unpack_to_numpy(jax_array)
assert isinstance(result, np.ndarray)
# compare results, do not check for identity: the latter would fail
# if not mocked, and the implementation does not guarantee it
# is the same Python object, just the same values.
Expand DownExpand Up@@ -1057,6 +1059,7 @@ def __array__(self):
tf_tensor = tensorflow.Tensor(data)

result = cbook._unpack_to_numpy(tf_tensor)
assert isinstance(result, np.ndarray)
# compare results, do not check for identity: the latter would fail
# if not mocked, and the implementation does not guarantee it
# is the same Python object, just the same values.
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp