matplotlib-torch 0.1.0__tar.gz
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.
- matplotlib_torch-0.1.0/PKG-INFO +29 -0
- matplotlib_torch-0.1.0/README.md +18 -0
- matplotlib_torch-0.1.0/matplotlib_torch/__init__.py +47 -0
- matplotlib_torch-0.1.0/matplotlib_torch.egg-info/PKG-INFO +29 -0
- matplotlib_torch-0.1.0/matplotlib_torch.egg-info/SOURCES.txt +9 -0
- matplotlib_torch-0.1.0/matplotlib_torch.egg-info/dependency_links.txt +1 -0
- matplotlib_torch-0.1.0/matplotlib_torch.egg-info/requires.txt +5 -0
- matplotlib_torch-0.1.0/matplotlib_torch.egg-info/top_level.txt +1 -0
- matplotlib_torch-0.1.0/pyproject.toml +17 -0
- matplotlib_torch-0.1.0/setup.cfg +4 -0
- matplotlib_torch-0.1.0/tests/test_basic.py +99 -0
|
@@ -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,18 @@
|
|
|
1
|
+
# matplotlib-torch
|
|
2
|
+
|
|
3
|
+
Transparently pass PyTorch tensors (CPU, CUDA, MPS, with/without grad) to
|
|
4
|
+
matplotlib. Avoid `.detach().cpu().numpy()` boilerplate.
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
import matplotlib_torch
|
|
8
|
+
matplotlib_torch.activate()
|
|
9
|
+
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
|
+
import torch
|
|
12
|
+
|
|
13
|
+
plt.plot(torch.tensor([1., 2., 3.], device='cuda')) # just works
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
pip install matplotlib-torch
|
|
@@ -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,9 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
matplotlib_torch/__init__.py
|
|
4
|
+
matplotlib_torch.egg-info/PKG-INFO
|
|
5
|
+
matplotlib_torch.egg-info/SOURCES.txt
|
|
6
|
+
matplotlib_torch.egg-info/dependency_links.txt
|
|
7
|
+
matplotlib_torch.egg-info/requires.txt
|
|
8
|
+
matplotlib_torch.egg-info/top_level.txt
|
|
9
|
+
tests/test_basic.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
matplotlib_torch
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "matplotlib-torch"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Autocast PyTorch tensors to numpy/matplotlib-CPU arrays in matplotlib."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
dependencies = ["matplotlib", "torch"]
|
|
12
|
+
|
|
13
|
+
[project.optional-dependencies]
|
|
14
|
+
test = ["pytest"]
|
|
15
|
+
|
|
16
|
+
[tool.setuptools.packages.find]
|
|
17
|
+
include = ["matplotlib_torch*"]
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import matplotlib
|
|
2
|
+
|
|
3
|
+
matplotlib.use("Agg") # headless for tests
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pytest
|
|
7
|
+
import torch
|
|
8
|
+
|
|
9
|
+
import matplotlib_torch
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@pytest.fixture(autouse=True)
|
|
13
|
+
def _activate():
|
|
14
|
+
matplotlib_torch.activate()
|
|
15
|
+
yield
|
|
16
|
+
matplotlib_torch.deactivate()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_is_active_toggle():
|
|
20
|
+
assert matplotlib_torch.is_active()
|
|
21
|
+
matplotlib_torch.deactivate()
|
|
22
|
+
assert not matplotlib_torch.is_active()
|
|
23
|
+
matplotlib_torch.activate()
|
|
24
|
+
assert matplotlib_torch.is_active()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_cpu_tensor():
|
|
28
|
+
x = torch.arange(5, dtype=torch.float32)
|
|
29
|
+
(line,) = plt.plot(x)
|
|
30
|
+
np.testing.assert_array_equal(line.get_xdata(), np.arange(5))
|
|
31
|
+
np.testing.assert_array_equal(line.get_ydata(), np.arange(5))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_cuda_tensor():
|
|
35
|
+
if not torch.cuda.is_available():
|
|
36
|
+
pytest.skip("no cuda")
|
|
37
|
+
x = torch.arange(5, dtype=torch.float32, device="cuda")
|
|
38
|
+
(line,) = plt.plot(x)
|
|
39
|
+
assert isinstance(line.get_ydata(), np.ndarray)
|
|
40
|
+
np.testing.assert_array_equal(line.get_ydata(), np.arange(5))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_grad_tensor():
|
|
44
|
+
x = torch.arange(5, dtype=torch.float32, requires_grad=True)
|
|
45
|
+
y = x * 2
|
|
46
|
+
(line,) = plt.plot(x, y)
|
|
47
|
+
np.testing.assert_array_equal(line.get_ydata(), np.arange(5) * 2)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_xy_both_tensors():
|
|
51
|
+
x = torch.linspace(0, 1, 10)
|
|
52
|
+
y = torch.linspace(1, 2, 10)
|
|
53
|
+
(line,) = plt.plot(x, y)
|
|
54
|
+
np.testing.assert_allclose(line.get_xdata(), np.linspace(0, 1, 10))
|
|
55
|
+
np.testing.assert_allclose(line.get_ydata(), np.linspace(1, 2, 10))
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_scatter():
|
|
59
|
+
x = torch.randn(20)
|
|
60
|
+
y = torch.randn(20)
|
|
61
|
+
c = torch.rand(20)
|
|
62
|
+
sc = plt.scatter(x, y, c=c)
|
|
63
|
+
assert sc.get_offsets().shape == (20, 2)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_scatter_cuda():
|
|
67
|
+
if not torch.cuda.is_available():
|
|
68
|
+
pytest.skip("no cuda")
|
|
69
|
+
x = torch.randn(20, device="cuda")
|
|
70
|
+
y = torch.randn(20, device="cuda")
|
|
71
|
+
sc = plt.scatter(x, y)
|
|
72
|
+
assert sc.get_offsets().shape == (20, 2)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_imshow():
|
|
76
|
+
img = torch.rand(8, 8, 3)
|
|
77
|
+
im = plt.imshow(img)
|
|
78
|
+
arr = im.get_array()
|
|
79
|
+
assert arr.shape == (8, 8, 3)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_imshow_cuda():
|
|
83
|
+
if not torch.cuda.is_available():
|
|
84
|
+
pytest.skip("no cuda")
|
|
85
|
+
img = torch.rand(8, 8, 3, device="cuda")
|
|
86
|
+
im = plt.imshow(img)
|
|
87
|
+
assert im.get_array().shape == (8, 8, 3)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_non_tensor_passes_through():
|
|
91
|
+
# plain numpy / lists should still work alongside tensors.
|
|
92
|
+
(line,) = plt.plot([1, 2, 3], np.array([4, 5, 6]))
|
|
93
|
+
np.testing.assert_array_equal(line.get_ydata(), [4, 5, 6])
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_scalar_tensor():
|
|
97
|
+
# A 0-d tensor as a y value should not crash the converter.
|
|
98
|
+
(line,) = plt.plot([0], torch.tensor(2.0))
|
|
99
|
+
assert line.get_ydata()[0] == 2.0
|