cogmatplot 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.
- cogmatplot-0.1.0/LICENSE +21 -0
- cogmatplot-0.1.0/PKG-INFO +217 -0
- cogmatplot-0.1.0/README.md +198 -0
- cogmatplot-0.1.0/cogmatplot/__init__.py +46 -0
- cogmatplot-0.1.0/cogmatplot/api.py +43 -0
- cogmatplot-0.1.0/cogmatplot/client.py +151 -0
- cogmatplot-0.1.0/cogmatplot/debug_views.py +222 -0
- cogmatplot-0.1.0/cogmatplot/images.py +54 -0
- cogmatplot-0.1.0/cogmatplot/layout.py +88 -0
- cogmatplot-0.1.0/cogmatplot/matplot.py +967 -0
- cogmatplot-0.1.0/cogmatplot/mode.py +77 -0
- cogmatplot-0.1.0/cogmatplot/place.py +65 -0
- cogmatplot-0.1.0/cogmatplot/ply.py +325 -0
- cogmatplot-0.1.0/cogmatplot/server.py +124 -0
- cogmatplot-0.1.0/cogmatplot/view3.py +353 -0
- cogmatplot-0.1.0/cogmatplot/wire.py +93 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/PKG-INFO +217 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/SOURCES.txt +23 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/dependency_links.txt +1 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/entry_points.txt +2 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/requires.txt +8 -0
- cogmatplot-0.1.0/cogmatplot.egg-info/top_level.txt +1 -0
- cogmatplot-0.1.0/pyproject.toml +28 -0
- cogmatplot-0.1.0/setup.cfg +4 -0
- cogmatplot-0.1.0/tests/test_library.py +408 -0
cogmatplot-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cogency
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cogmatplot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MATLAB-style 2D and 3D figures, inline or on another monitor
|
|
5
|
+
Author-email: Cogency <mathew@cogency.co.za>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: numpy
|
|
11
|
+
Requires-Dist: matplotlib
|
|
12
|
+
Requires-Dist: pyzmq
|
|
13
|
+
Requires-Dist: screeninfo
|
|
14
|
+
Requires-Dist: opencv-python
|
|
15
|
+
Requires-Dist: PyQt5
|
|
16
|
+
Requires-Dist: pyvista
|
|
17
|
+
Requires-Dist: pyvistaqt
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# cogmatplot
|
|
21
|
+
|
|
22
|
+
I often like to work with many types of plots and viewers (and miss the versatility of MATLAB figures) when doing research, so I made this library for Python.
|
|
23
|
+
One grid can hold images, 2D plots, 3D lines, and point clouds. Each tile picks its own viewer: matplotlib, an OpenCV window, or a PyVista 3D view. `FigureManager(rows, cols, mon)` places
|
|
24
|
+
that grid on any monitor; use a different `mon` when you want another screen. Heads up:The library is intended for development and debugging workflows, not end-user applications.
|
|
25
|
+
|
|
26
|
+
Example of 2x2 grid plotted with `inline` backend (from `examples/scene.py`). In `Qt` mode the same four cells are separate windows on the monitor you choose.
|
|
27
|
+
|
|
28
|
+

|
|
29
|
+
|
|
30
|
+
Pick where the figures go:
|
|
31
|
+
|
|
32
|
+
- **Inline** draws static figures in a notebook or interactive window.
|
|
33
|
+
- **Qt** opens interactive windows on a monitor. You can pan, zoom, and orbit.
|
|
34
|
+
- **Server** lets another process send the same calls to those windows.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install -e .
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
or `pip install -r requirements.txt`. That installs the viewers with the library:
|
|
43
|
+
|
|
44
|
+
| Package | Used for |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| `numpy`, `matplotlib` | 2D plots, images, and inline figures |
|
|
47
|
+
| `PyQt5` | Pop-out windows on a monitor |
|
|
48
|
+
| `pyvista`, `pyvistaqt` | Interactive 3D points and meshes |
|
|
49
|
+
| `opencv-python` | Fast live image tiles |
|
|
50
|
+
| `screeninfo` | Which monitor a tile lands on |
|
|
51
|
+
| `pyzmq` | The debug server |
|
|
52
|
+
|
|
53
|
+
## Quickstart
|
|
54
|
+
|
|
55
|
+
Call `use` before importing `FigureManager`.
|
|
56
|
+
|
|
57
|
+
### Notebook or interactive window
|
|
58
|
+
|
|
59
|
+
Figures stay in the output. They are not interactive.
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import numpy as np
|
|
63
|
+
import cogmatplot
|
|
64
|
+
|
|
65
|
+
cogmatplot.use("inline")
|
|
66
|
+
from cogmatplot import FigureManager
|
|
67
|
+
|
|
68
|
+
x = np.linspace(0, 2 * np.pi, 200)
|
|
69
|
+
image = np.zeros((120, 160, 3), np.uint8)
|
|
70
|
+
image[:, :, 1] = 180
|
|
71
|
+
|
|
72
|
+
fm = FigureManager(1, 2)
|
|
73
|
+
fm.imshow(0, 0, image, title="image")
|
|
74
|
+
fm.plot(0, 1, x, np.sin(x), ".-", label="sin", xlabel="x", ylabel="y")
|
|
75
|
+
fm.drawnow()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Open `examples/notebook_inline.ipynb` for the same idea with a 3D line and a point cloud.
|
|
79
|
+
|
|
80
|
+
### Interactive windows on a monitor
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
%matplotlib qt
|
|
84
|
+
|
|
85
|
+
import cogmatplot
|
|
86
|
+
cogmatplot.use("qt")
|
|
87
|
+
from cogmatplot import FigureManager, closeAll
|
|
88
|
+
|
|
89
|
+
MON = 0 # screen index
|
|
90
|
+
|
|
91
|
+
closeAll()
|
|
92
|
+
fm = FigureManager(1, 2, MON, pause=False)
|
|
93
|
+
fm.plot(0, 0, [0, 1, 2], [0, 1, 0], "o-")
|
|
94
|
+
fm.drawnow()
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`FigureManager(rows, cols, mon)` places one window in each cell of that screen. `pause=False` updates without a pause after every call. Call `drawnow()` when you want the windows to refresh.
|
|
98
|
+
|
|
99
|
+
See `examples/interactive_qt.py`.
|
|
100
|
+
|
|
101
|
+
## 2D and 3D
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
fm = FigureManager(2, 2, pause=False)
|
|
105
|
+
|
|
106
|
+
fm.imshow(0, 0, image, title="image")
|
|
107
|
+
fm.hold(0, 0, "on")
|
|
108
|
+
fm.plot(0, 0, poly[:, 0], poly[:, 1], "y-", grid=0)
|
|
109
|
+
fm.axis(0, 0, "off")
|
|
110
|
+
|
|
111
|
+
fm.plot(0, 1, x, y, ".-", label="sin")
|
|
112
|
+
fm.hold(0, 1, "on")
|
|
113
|
+
fm.plot(0, 1, x, y2, "-", label="cos")
|
|
114
|
+
fm.legend(0, 1)
|
|
115
|
+
|
|
116
|
+
fm.plot3(1, 0, x, y, z, "-")
|
|
117
|
+
fm.title(1, 0, "plot3")
|
|
118
|
+
|
|
119
|
+
cloud = fm.figure3(1, 1, title="depth")
|
|
120
|
+
cloud.scatter3(points, colors=rgb, point_size=3) # (N, 3) xyz and (N, 3) rgb
|
|
121
|
+
cloud.scatter3(tag, color="red", point_size=8) # xyz, one color
|
|
122
|
+
cloud.show_grid()
|
|
123
|
+
cloud.view_xy()
|
|
124
|
+
fm.drawnow()
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`colors` can be `uint8` or float `0–1`. A single `color` such as `"silver"` or `"red"` paints the whole cloud. An object with `.points` and `.colors` (for example an Open3D cloud) is accepted.
|
|
128
|
+
|
|
129
|
+
`hold on` adds another series. The next `plot` without hold updates the current line in place.
|
|
130
|
+
|
|
131
|
+
Names follow MATLAB: `plot`, `plot3`, `imshow`, `imagesc`, `hold`, `axis`, `grid`, `title`, `xlabel`, `ylabel`, `xlim`, `ylim`, `zlim`, `legend`, `colorbar`, `text`, `rectangle`, `clf`, `cla`, `closeAll`, `drawnow`.
|
|
132
|
+
|
|
133
|
+
In-process, `imshow` and `plot` also return the matplotlib axes, so `ax.plot(...)` still works. Over the server, use the `FigureManager` methods above. They do the same thing.
|
|
134
|
+
|
|
135
|
+
## Fast image streams
|
|
136
|
+
|
|
137
|
+
OpenCV windows are the path for live frames. Publish by name, then render once per frame. Nothing is shown until `init_debug_views`.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from cogmatplot.debug_views import init_debug_views, debug_image, render_debug_views
|
|
141
|
+
|
|
142
|
+
init_debug_views(cols=2, monitor=0)
|
|
143
|
+
debug_image("gray", gray)
|
|
144
|
+
debug_image("color", color)
|
|
145
|
+
render_debug_views()
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`examples/debug_streams.py` is a short loop.
|
|
149
|
+
|
|
150
|
+
You can also put an OpenCV tile in a `FigureManager` cell:
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
fm.imshow(0, 0, frame, viewer="cv", title="live")
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
In inline mode that same call draws a static image in the output.
|
|
157
|
+
|
|
158
|
+
## PLY point clouds and meshes
|
|
159
|
+
|
|
160
|
+
Binary PLY files load through `numpy` after the header. That is the fast path. A rectangular prism is included as sample data: a colored face cloud and a 12-triangle box.
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from pathlib import Path
|
|
164
|
+
from cogmatplot.ply import load_ply
|
|
165
|
+
|
|
166
|
+
data = Path("examples/data")
|
|
167
|
+
cloud = load_ply(data / "prism_cloud.ply")
|
|
168
|
+
x, y, z = cloud.points.T
|
|
169
|
+
fm.plot3(0, 0, x, y, z, ".", colors=cloud.colors, point_size=8)
|
|
170
|
+
|
|
171
|
+
mesh = load_ply(data / "prism_mesh.ply")
|
|
172
|
+
fm.plot_mesh(0, 1, mesh.points, mesh.faces, style="wireframe")
|
|
173
|
+
fm.plot_mesh(1, 0, mesh.points, mesh.faces, style="surface", color="lightsteelblue")
|
|
174
|
+
fm.plot_mesh(1, 1, mesh.points, mesh.faces, style="both", color="lightsteelblue")
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`load_ply` only reads the file. It returns `.points`, `.colors`, `.normals`, and `.faces`. `plot3` is the MATLAB call: `plot3(x, y, z, '-')` is a 3D line, and `plot3(x, y, z, '.')` is a point cloud. `plot_mesh` draws faces. `style` is `surface` (shaded), `wireframe`, or `both`. `rectangular_prism()` builds the same box in memory, and `save_ply` writes it back out.
|
|
178
|
+
|
|
179
|
+
`examples/ply_prism.py` loads the two sample files.
|
|
180
|
+
|
|
181
|
+
## Server
|
|
182
|
+
|
|
183
|
+
The server owns the windows. A debugger, or any other process, sends the same calls.
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
python -m cogmatplot.server -m 0 -r 2 -c 2
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`-m` is the monitor (default 2), `-r` and `-c` are the grid, `-p` is the port (default 5555). Key presses from the windows are published on the next port.
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from cogmatplot import FigureManager
|
|
193
|
+
from scene import draw # examples/scene.py
|
|
194
|
+
|
|
195
|
+
fm = FigureManager.connect("127.0.0.1", 5555)
|
|
196
|
+
fm.reset(2, 2, 0)
|
|
197
|
+
draw(fm)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
`examples/debug_client.py` is that client. `PlotClient` is the same object `connect` returns. Older names still work: `close_all`, `clf_all`, `inter`, and `box`.
|
|
201
|
+
|
|
202
|
+
## Examples
|
|
203
|
+
|
|
204
|
+
| File | What it shows |
|
|
205
|
+
| --- | --- |
|
|
206
|
+
| `examples/notebook_inline.ipynb` | Static notebook figures |
|
|
207
|
+
| `examples/interactive_qt.py` | Interactive windows on a monitor |
|
|
208
|
+
| `examples/debug_streams.py` | Named OpenCV streams |
|
|
209
|
+
| `examples/debug_client.py` | Plot into the server |
|
|
210
|
+
| `examples/scene.py` | The shared 2×2 scene used above |
|
|
211
|
+
| `examples/ply_prism.py` | PLY point cloud, wireframe, and shaded mesh |
|
|
212
|
+
|
|
213
|
+
## Tests
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
python -m unittest discover -s tests
|
|
217
|
+
```
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# cogmatplot
|
|
2
|
+
|
|
3
|
+
I often like to work with many types of plots and viewers (and miss the versatility of MATLAB figures) when doing research, so I made this library for Python.
|
|
4
|
+
One grid can hold images, 2D plots, 3D lines, and point clouds. Each tile picks its own viewer: matplotlib, an OpenCV window, or a PyVista 3D view. `FigureManager(rows, cols, mon)` places
|
|
5
|
+
that grid on any monitor; use a different `mon` when you want another screen. Heads up:The library is intended for development and debugging workflows, not end-user applications.
|
|
6
|
+
|
|
7
|
+
Example of 2x2 grid plotted with `inline` backend (from `examples/scene.py`). In `Qt` mode the same four cells are separate windows on the monitor you choose.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
Pick where the figures go:
|
|
12
|
+
|
|
13
|
+
- **Inline** draws static figures in a notebook or interactive window.
|
|
14
|
+
- **Qt** opens interactive windows on a monitor. You can pan, zoom, and orbit.
|
|
15
|
+
- **Server** lets another process send the same calls to those windows.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install -e .
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
or `pip install -r requirements.txt`. That installs the viewers with the library:
|
|
24
|
+
|
|
25
|
+
| Package | Used for |
|
|
26
|
+
| --- | --- |
|
|
27
|
+
| `numpy`, `matplotlib` | 2D plots, images, and inline figures |
|
|
28
|
+
| `PyQt5` | Pop-out windows on a monitor |
|
|
29
|
+
| `pyvista`, `pyvistaqt` | Interactive 3D points and meshes |
|
|
30
|
+
| `opencv-python` | Fast live image tiles |
|
|
31
|
+
| `screeninfo` | Which monitor a tile lands on |
|
|
32
|
+
| `pyzmq` | The debug server |
|
|
33
|
+
|
|
34
|
+
## Quickstart
|
|
35
|
+
|
|
36
|
+
Call `use` before importing `FigureManager`.
|
|
37
|
+
|
|
38
|
+
### Notebook or interactive window
|
|
39
|
+
|
|
40
|
+
Figures stay in the output. They are not interactive.
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import numpy as np
|
|
44
|
+
import cogmatplot
|
|
45
|
+
|
|
46
|
+
cogmatplot.use("inline")
|
|
47
|
+
from cogmatplot import FigureManager
|
|
48
|
+
|
|
49
|
+
x = np.linspace(0, 2 * np.pi, 200)
|
|
50
|
+
image = np.zeros((120, 160, 3), np.uint8)
|
|
51
|
+
image[:, :, 1] = 180
|
|
52
|
+
|
|
53
|
+
fm = FigureManager(1, 2)
|
|
54
|
+
fm.imshow(0, 0, image, title="image")
|
|
55
|
+
fm.plot(0, 1, x, np.sin(x), ".-", label="sin", xlabel="x", ylabel="y")
|
|
56
|
+
fm.drawnow()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Open `examples/notebook_inline.ipynb` for the same idea with a 3D line and a point cloud.
|
|
60
|
+
|
|
61
|
+
### Interactive windows on a monitor
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
%matplotlib qt
|
|
65
|
+
|
|
66
|
+
import cogmatplot
|
|
67
|
+
cogmatplot.use("qt")
|
|
68
|
+
from cogmatplot import FigureManager, closeAll
|
|
69
|
+
|
|
70
|
+
MON = 0 # screen index
|
|
71
|
+
|
|
72
|
+
closeAll()
|
|
73
|
+
fm = FigureManager(1, 2, MON, pause=False)
|
|
74
|
+
fm.plot(0, 0, [0, 1, 2], [0, 1, 0], "o-")
|
|
75
|
+
fm.drawnow()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`FigureManager(rows, cols, mon)` places one window in each cell of that screen. `pause=False` updates without a pause after every call. Call `drawnow()` when you want the windows to refresh.
|
|
79
|
+
|
|
80
|
+
See `examples/interactive_qt.py`.
|
|
81
|
+
|
|
82
|
+
## 2D and 3D
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
fm = FigureManager(2, 2, pause=False)
|
|
86
|
+
|
|
87
|
+
fm.imshow(0, 0, image, title="image")
|
|
88
|
+
fm.hold(0, 0, "on")
|
|
89
|
+
fm.plot(0, 0, poly[:, 0], poly[:, 1], "y-", grid=0)
|
|
90
|
+
fm.axis(0, 0, "off")
|
|
91
|
+
|
|
92
|
+
fm.plot(0, 1, x, y, ".-", label="sin")
|
|
93
|
+
fm.hold(0, 1, "on")
|
|
94
|
+
fm.plot(0, 1, x, y2, "-", label="cos")
|
|
95
|
+
fm.legend(0, 1)
|
|
96
|
+
|
|
97
|
+
fm.plot3(1, 0, x, y, z, "-")
|
|
98
|
+
fm.title(1, 0, "plot3")
|
|
99
|
+
|
|
100
|
+
cloud = fm.figure3(1, 1, title="depth")
|
|
101
|
+
cloud.scatter3(points, colors=rgb, point_size=3) # (N, 3) xyz and (N, 3) rgb
|
|
102
|
+
cloud.scatter3(tag, color="red", point_size=8) # xyz, one color
|
|
103
|
+
cloud.show_grid()
|
|
104
|
+
cloud.view_xy()
|
|
105
|
+
fm.drawnow()
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`colors` can be `uint8` or float `0–1`. A single `color` such as `"silver"` or `"red"` paints the whole cloud. An object with `.points` and `.colors` (for example an Open3D cloud) is accepted.
|
|
109
|
+
|
|
110
|
+
`hold on` adds another series. The next `plot` without hold updates the current line in place.
|
|
111
|
+
|
|
112
|
+
Names follow MATLAB: `plot`, `plot3`, `imshow`, `imagesc`, `hold`, `axis`, `grid`, `title`, `xlabel`, `ylabel`, `xlim`, `ylim`, `zlim`, `legend`, `colorbar`, `text`, `rectangle`, `clf`, `cla`, `closeAll`, `drawnow`.
|
|
113
|
+
|
|
114
|
+
In-process, `imshow` and `plot` also return the matplotlib axes, so `ax.plot(...)` still works. Over the server, use the `FigureManager` methods above. They do the same thing.
|
|
115
|
+
|
|
116
|
+
## Fast image streams
|
|
117
|
+
|
|
118
|
+
OpenCV windows are the path for live frames. Publish by name, then render once per frame. Nothing is shown until `init_debug_views`.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from cogmatplot.debug_views import init_debug_views, debug_image, render_debug_views
|
|
122
|
+
|
|
123
|
+
init_debug_views(cols=2, monitor=0)
|
|
124
|
+
debug_image("gray", gray)
|
|
125
|
+
debug_image("color", color)
|
|
126
|
+
render_debug_views()
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`examples/debug_streams.py` is a short loop.
|
|
130
|
+
|
|
131
|
+
You can also put an OpenCV tile in a `FigureManager` cell:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
fm.imshow(0, 0, frame, viewer="cv", title="live")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
In inline mode that same call draws a static image in the output.
|
|
138
|
+
|
|
139
|
+
## PLY point clouds and meshes
|
|
140
|
+
|
|
141
|
+
Binary PLY files load through `numpy` after the header. That is the fast path. A rectangular prism is included as sample data: a colored face cloud and a 12-triangle box.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from pathlib import Path
|
|
145
|
+
from cogmatplot.ply import load_ply
|
|
146
|
+
|
|
147
|
+
data = Path("examples/data")
|
|
148
|
+
cloud = load_ply(data / "prism_cloud.ply")
|
|
149
|
+
x, y, z = cloud.points.T
|
|
150
|
+
fm.plot3(0, 0, x, y, z, ".", colors=cloud.colors, point_size=8)
|
|
151
|
+
|
|
152
|
+
mesh = load_ply(data / "prism_mesh.ply")
|
|
153
|
+
fm.plot_mesh(0, 1, mesh.points, mesh.faces, style="wireframe")
|
|
154
|
+
fm.plot_mesh(1, 0, mesh.points, mesh.faces, style="surface", color="lightsteelblue")
|
|
155
|
+
fm.plot_mesh(1, 1, mesh.points, mesh.faces, style="both", color="lightsteelblue")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`load_ply` only reads the file. It returns `.points`, `.colors`, `.normals`, and `.faces`. `plot3` is the MATLAB call: `plot3(x, y, z, '-')` is a 3D line, and `plot3(x, y, z, '.')` is a point cloud. `plot_mesh` draws faces. `style` is `surface` (shaded), `wireframe`, or `both`. `rectangular_prism()` builds the same box in memory, and `save_ply` writes it back out.
|
|
159
|
+
|
|
160
|
+
`examples/ply_prism.py` loads the two sample files.
|
|
161
|
+
|
|
162
|
+
## Server
|
|
163
|
+
|
|
164
|
+
The server owns the windows. A debugger, or any other process, sends the same calls.
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
python -m cogmatplot.server -m 0 -r 2 -c 2
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`-m` is the monitor (default 2), `-r` and `-c` are the grid, `-p` is the port (default 5555). Key presses from the windows are published on the next port.
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
from cogmatplot import FigureManager
|
|
174
|
+
from scene import draw # examples/scene.py
|
|
175
|
+
|
|
176
|
+
fm = FigureManager.connect("127.0.0.1", 5555)
|
|
177
|
+
fm.reset(2, 2, 0)
|
|
178
|
+
draw(fm)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
`examples/debug_client.py` is that client. `PlotClient` is the same object `connect` returns. Older names still work: `close_all`, `clf_all`, `inter`, and `box`.
|
|
182
|
+
|
|
183
|
+
## Examples
|
|
184
|
+
|
|
185
|
+
| File | What it shows |
|
|
186
|
+
| --- | --- |
|
|
187
|
+
| `examples/notebook_inline.ipynb` | Static notebook figures |
|
|
188
|
+
| `examples/interactive_qt.py` | Interactive windows on a monitor |
|
|
189
|
+
| `examples/debug_streams.py` | Named OpenCV streams |
|
|
190
|
+
| `examples/debug_client.py` | Plot into the server |
|
|
191
|
+
| `examples/scene.py` | The shared 2×2 scene used above |
|
|
192
|
+
| `examples/ply_prism.py` | PLY point cloud, wireframe, and shaded mesh |
|
|
193
|
+
|
|
194
|
+
## Tests
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
python -m unittest discover -s tests
|
|
198
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""MATLAB-style figures for notebooks, pop-out monitors, and a debug server.
|
|
2
|
+
|
|
3
|
+
Pick the display before importing the plotting classes when you can::
|
|
4
|
+
|
|
5
|
+
import cogmatplot
|
|
6
|
+
cogmatplot.use("inline") # static figures in the interactive viewer
|
|
7
|
+
cogmatplot.use("qt") # interactive windows on a monitor
|
|
8
|
+
|
|
9
|
+
``FigureManager`` is loaded lazily so ``use`` can select the backend first.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from cogmatplot.mode import current_mode, use
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"use",
|
|
16
|
+
"current_mode",
|
|
17
|
+
"FigureManager",
|
|
18
|
+
"closeAll",
|
|
19
|
+
"drawnow",
|
|
20
|
+
"Fig3d",
|
|
21
|
+
"PlotClient",
|
|
22
|
+
"Ply",
|
|
23
|
+
"load_ply",
|
|
24
|
+
"save_ply",
|
|
25
|
+
"rectangular_prism",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def __getattr__(name):
|
|
30
|
+
if name in ("FigureManager", "closeAll", "drawnow"):
|
|
31
|
+
from cogmatplot import matplot
|
|
32
|
+
|
|
33
|
+
return getattr(matplot, name)
|
|
34
|
+
if name == "Fig3d":
|
|
35
|
+
from cogmatplot.view3 import Fig3d
|
|
36
|
+
|
|
37
|
+
return Fig3d
|
|
38
|
+
if name == "PlotClient":
|
|
39
|
+
from cogmatplot.client import PlotClient
|
|
40
|
+
|
|
41
|
+
return PlotClient
|
|
42
|
+
if name in ("Ply", "load_ply", "save_ply", "rectangular_prism"):
|
|
43
|
+
from cogmatplot import ply
|
|
44
|
+
|
|
45
|
+
return getattr(ply, name)
|
|
46
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Method names shared by FigureManager and the remote client.
|
|
2
|
+
|
|
3
|
+
The server calls ``getattr(figure, method)(*args, **kwargs)``. A new plotting
|
|
4
|
+
helper is added here and on ``FigureManager`` once.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
STREAM_METHODS = ("imshow", "imagesc")
|
|
8
|
+
|
|
9
|
+
METHODS = (
|
|
10
|
+
"imshow",
|
|
11
|
+
"imagesc",
|
|
12
|
+
"plot",
|
|
13
|
+
"plot3",
|
|
14
|
+
"scatter3",
|
|
15
|
+
"plot_mesh",
|
|
16
|
+
"figure3",
|
|
17
|
+
"title",
|
|
18
|
+
"xlabel",
|
|
19
|
+
"ylabel",
|
|
20
|
+
"axis",
|
|
21
|
+
"hold",
|
|
22
|
+
"grid",
|
|
23
|
+
"xlim",
|
|
24
|
+
"ylim",
|
|
25
|
+
"zlim",
|
|
26
|
+
"legend",
|
|
27
|
+
"colorbar",
|
|
28
|
+
"text",
|
|
29
|
+
"box",
|
|
30
|
+
"rectangle",
|
|
31
|
+
"clf",
|
|
32
|
+
"cla",
|
|
33
|
+
"close",
|
|
34
|
+
"closeAll",
|
|
35
|
+
"clfAll",
|
|
36
|
+
"reset",
|
|
37
|
+
"drawnow",
|
|
38
|
+
"pause",
|
|
39
|
+
"interactive",
|
|
40
|
+
"view3",
|
|
41
|
+
"grid3",
|
|
42
|
+
"clear3",
|
|
43
|
+
)
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Send FigureManager calls to a cogmatplot server.
|
|
2
|
+
|
|
3
|
+
Method names match the in-process manager. ``figure3`` returns a proxy whose
|
|
4
|
+
``scatter3``, ``view_xy``, and ``show_grid`` calls are forwarded.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
import zmq
|
|
12
|
+
|
|
13
|
+
from cogmatplot.api import METHODS, STREAM_METHODS
|
|
14
|
+
from cogmatplot.wire import encode
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _remote(name):
|
|
18
|
+
def method(self, *args, **kwargs):
|
|
19
|
+
return self._call(name, *args, **kwargs)
|
|
20
|
+
|
|
21
|
+
method.__name__ = name
|
|
22
|
+
method.__doc__ = f"Forward {name} to the figure server."
|
|
23
|
+
return method
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class RemoteCloud:
|
|
27
|
+
"""Stand-in for :class:`cogmatplot.view3.Fig3d` on the client side."""
|
|
28
|
+
|
|
29
|
+
def __init__(self, client, row, col):
|
|
30
|
+
self._client = client
|
|
31
|
+
self.row = row
|
|
32
|
+
self.col = col
|
|
33
|
+
|
|
34
|
+
def scatter3(self, points, color=None, colors=None, point_size=3, cmap=None, hold=True, **kwargs):
|
|
35
|
+
self._client.scatter3(
|
|
36
|
+
self.row, self.col, points,
|
|
37
|
+
color=color, colors=colors, point_size=point_size, cmap=cmap, hold=hold, **kwargs,
|
|
38
|
+
)
|
|
39
|
+
return self
|
|
40
|
+
|
|
41
|
+
def view(self, which="xy"):
|
|
42
|
+
self._client.view3(self.row, self.col, which)
|
|
43
|
+
return self
|
|
44
|
+
|
|
45
|
+
def view_xy(self):
|
|
46
|
+
return self.view("xy")
|
|
47
|
+
|
|
48
|
+
def view_xz(self):
|
|
49
|
+
return self.view("xz")
|
|
50
|
+
|
|
51
|
+
def view_yz(self):
|
|
52
|
+
return self.view("yz")
|
|
53
|
+
|
|
54
|
+
def view_isometric(self):
|
|
55
|
+
return self.view("iso")
|
|
56
|
+
|
|
57
|
+
def grid(self, on=True):
|
|
58
|
+
self._client.grid3(self.row, self.col, on)
|
|
59
|
+
return self
|
|
60
|
+
|
|
61
|
+
def show_grid(self):
|
|
62
|
+
return self.grid(True)
|
|
63
|
+
|
|
64
|
+
def clear(self):
|
|
65
|
+
self._client.clear3(self.row, self.col)
|
|
66
|
+
return self
|
|
67
|
+
|
|
68
|
+
def plot_mesh(self, points, faces, color=None, colors=None, style="surface", hold=True, **kwargs):
|
|
69
|
+
self._client.plot_mesh(
|
|
70
|
+
self.row, self.col, points, faces,
|
|
71
|
+
color=color, colors=colors, style=style, hold=hold, **kwargs,
|
|
72
|
+
)
|
|
73
|
+
return self
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class PlotClient:
|
|
77
|
+
"""Debugger-side handle. Construct with ``FigureManager.connect`` too."""
|
|
78
|
+
|
|
79
|
+
def __init__(self, host="localhost", port=5555):
|
|
80
|
+
self.host = host
|
|
81
|
+
self.port = int(port)
|
|
82
|
+
self._context = zmq.Context()
|
|
83
|
+
self._socket = self._context.socket(zmq.PUSH)
|
|
84
|
+
self._socket.setsockopt(zmq.LINGER, 0)
|
|
85
|
+
self._socket.setsockopt(zmq.SNDHWM, 16)
|
|
86
|
+
self._socket.connect(f"tcp://{host}:{self.port}")
|
|
87
|
+
self._events = None
|
|
88
|
+
|
|
89
|
+
def _call(self, method, *args, **kwargs):
|
|
90
|
+
kwargs.pop("flush", None)
|
|
91
|
+
frames = encode(method, args, kwargs)
|
|
92
|
+
wait = 0.25 if method in STREAM_METHODS else 2.0
|
|
93
|
+
deadline = time.perf_counter() + wait
|
|
94
|
+
while True:
|
|
95
|
+
try:
|
|
96
|
+
self._socket.send_multipart(frames, flags=zmq.NOBLOCK)
|
|
97
|
+
return
|
|
98
|
+
except zmq.Again:
|
|
99
|
+
if time.perf_counter() > deadline:
|
|
100
|
+
if method not in STREAM_METHODS:
|
|
101
|
+
self._socket.send_multipart(frames)
|
|
102
|
+
return
|
|
103
|
+
time.sleep(0.01)
|
|
104
|
+
|
|
105
|
+
def plot(self, row, col, x, y, fmt=None, hold=False, flush=True, **kwargs):
|
|
106
|
+
"""``fmt`` defaults to ``'.'`` so existing debugger scripts keep their markers."""
|
|
107
|
+
del flush
|
|
108
|
+
self._call("plot", row, col, x, y, "." if fmt is None else fmt, hold=hold, **kwargs)
|
|
109
|
+
|
|
110
|
+
def figure3(self, row, col, title="Fig3d", **kwargs):
|
|
111
|
+
self._call("figure3", row, col, title=title, **kwargs)
|
|
112
|
+
return RemoteCloud(self, row, col)
|
|
113
|
+
|
|
114
|
+
def close_all(self, rows=None, cols=None, mon=None):
|
|
115
|
+
self.reset(rows, cols, mon)
|
|
116
|
+
|
|
117
|
+
def clf_all(self):
|
|
118
|
+
self.clfAll()
|
|
119
|
+
|
|
120
|
+
def inter(self, val=1):
|
|
121
|
+
self.interactive(bool(val))
|
|
122
|
+
|
|
123
|
+
def poll_event(self, timeout=0):
|
|
124
|
+
"""Return the newest key event from the server, or ``None``."""
|
|
125
|
+
if self._events is None:
|
|
126
|
+
self._events = self._context.socket(zmq.SUB)
|
|
127
|
+
self._events.setsockopt(zmq.LINGER, 0)
|
|
128
|
+
self._events.setsockopt(zmq.SUBSCRIBE, b"")
|
|
129
|
+
self._events.setsockopt(zmq.CONFLATE, 1)
|
|
130
|
+
self._events.connect(f"tcp://{self.host}:{self.port + 1}")
|
|
131
|
+
if self._events.poll(int(timeout)):
|
|
132
|
+
return self._events.recv_json()
|
|
133
|
+
return None
|
|
134
|
+
|
|
135
|
+
def close(self):
|
|
136
|
+
for sock in (self._socket, self._events):
|
|
137
|
+
if sock is None:
|
|
138
|
+
continue
|
|
139
|
+
try:
|
|
140
|
+
sock.close(0)
|
|
141
|
+
except Exception:
|
|
142
|
+
pass
|
|
143
|
+
try:
|
|
144
|
+
self._context.term()
|
|
145
|
+
except Exception:
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
for _name in METHODS:
|
|
150
|
+
if not hasattr(PlotClient, _name):
|
|
151
|
+
setattr(PlotClient, _name, _remote(_name))
|