matplotlib-torch 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Autocast PyTorch tensors to numpy arrays for matplotlib.
|
|
2
|
+
|
|
3
|
+
Strategy: patch ``torch.Tensor.__array__`` so that any numpy conversion of a
|
|
4
|
+
tensor (on any device, with any grad state) detaches, moves to CPU, and returns
|
|
5
|
+
an ndarray. matplotlib's plotting paths all funnel through ``np.array(x)`` /
|
|
6
|
+
``x.__array__()``, so this single hook covers ``plot``, ``scatter``, ``imshow``,
|
|
7
|
+
``bar``, ``hist``, etc.
|
|
8
|
+
|
|
9
|
+
Notes:
|
|
10
|
+
- This is intentionally a global patch on torch, scoped to when the package is
|
|
11
|
+
activated. It also silences numpy 2.0's ``copy`` kwarg deprecation warning
|
|
12
|
+
that affects torch's stock ``__array__``.
|
|
13
|
+
- matplotlib's ``units`` registry is not used: matplotlib's
|
|
14
|
+
``cbook._unpack_to_numpy`` runs before unit conversion and erases the torch
|
|
15
|
+
type, so a registered converter would never be reached.
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import torch
|
|
20
|
+
|
|
21
|
+
__all__ = ["activate", "deactivate", "is_active"]
|
|
22
|
+
|
|
23
|
+
_ORIGINAL_array = torch.Tensor.__array__
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def __array__(self, dtype=None, copy=None):
|
|
27
|
+
# Unconditional detach + cpu: handles CUDA/MPS/CPU, leaf/non-leaf,
|
|
28
|
+
# requires_grad True/False, in one path. Matches the package contract that
|
|
29
|
+
# tensors on any device "just work" with matplotlib.
|
|
30
|
+
arr = self.detach().cpu().numpy()
|
|
31
|
+
if dtype is not None:
|
|
32
|
+
arr = arr.astype(dtype)
|
|
33
|
+
return arr
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def activate() -> None:
|
|
37
|
+
"""Install the torch-aware ``__array__`` patch."""
|
|
38
|
+
torch.Tensor.__array__ = __array__
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def deactivate() -> None:
|
|
42
|
+
"""Restore torch's stock ``__array__``."""
|
|
43
|
+
torch.Tensor.__array__ = _ORIGINAL_array
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def is_active() -> bool:
|
|
47
|
+
return torch.Tensor.__array__ is __array__
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: matplotlib-torch
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Autocast PyTorch tensors to numpy/matplotlib-CPU arrays in matplotlib.
|
|
5
|
+
Requires-Python: >=3.9
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: matplotlib
|
|
8
|
+
Requires-Dist: torch
|
|
9
|
+
Provides-Extra: test
|
|
10
|
+
Requires-Dist: pytest; extra == "test"
|
|
11
|
+
|
|
12
|
+
# matplotlib-torch
|
|
13
|
+
|
|
14
|
+
Transparently pass PyTorch tensors (CPU, CUDA, MPS, with/without grad) to
|
|
15
|
+
matplotlib. Avoid `.detach().cpu().numpy()` boilerplate.
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import matplotlib_torch
|
|
19
|
+
matplotlib_torch.activate()
|
|
20
|
+
|
|
21
|
+
import matplotlib.pyplot as plt
|
|
22
|
+
import torch
|
|
23
|
+
|
|
24
|
+
plt.plot(torch.tensor([1., 2., 3.], device='cuda')) # just works
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
pip install matplotlib-torch
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
matplotlib_torch/__init__.py,sha256=x8edDD6CavrREqOJOQdFFtTVjgrk0xMFF6lj_r9jrxs,1628
|
|
2
|
+
matplotlib_torch-0.1.0.dist-info/METADATA,sha256=MjCgYsarllw2artG7GAqX96ra2QulpRbWqfNmtonLTg,689
|
|
3
|
+
matplotlib_torch-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
4
|
+
matplotlib_torch-0.1.0.dist-info/top_level.txt,sha256=NoCj4lTgsw-yljtiBP-IjwsH2-2Y6JNQP9A1O5Iu2kg,17
|
|
5
|
+
matplotlib_torch-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
matplotlib_torch
|