cloudglancer 0.1.7__tar.gz → 0.1.8__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cloudglancer
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Simple interactive visualization of 3D point clouds
5
5
  Author-email: Your Name <your.email@example.com>
6
6
  License: MIT
@@ -75,6 +75,9 @@ fig = cg.plot(points, size=2.0)
75
75
  cg.animate(fig, "rotation.gif", axis="z", n_frames=60)
76
76
  ```
77
77
 
78
+ Frames are rendered in parallel; pass `n_workers` to control how many render
79
+ processes are used (defaults to `min(16, cpu_count)`).
80
+
78
81
  Render a batch of point clouds as a grid of subplots (one cloud per cell):
79
82
 
80
83
  ```python
@@ -40,6 +40,9 @@ fig = cg.plot(points, size=2.0)
40
40
  cg.animate(fig, "rotation.gif", axis="z", n_frames=60)
41
41
  ```
42
42
 
43
+ Frames are rendered in parallel; pass `n_workers` to control how many render
44
+ processes are used (defaults to `min(16, cpu_count)`).
45
+
43
46
  Render a batch of point clouds as a grid of subplots (one cloud per cell):
44
47
 
45
48
  ```python
@@ -2,9 +2,10 @@
2
2
 
3
3
  import multiprocessing
4
4
  import os
5
+ import sys
5
6
  from concurrent.futures import ProcessPoolExecutor
6
7
  from io import BytesIO
7
- from typing import Tuple
8
+ from typing import Optional, Tuple
8
9
 
9
10
  import numpy as np
10
11
  from plotly.graph_objects import Figure
@@ -40,25 +41,31 @@ def _scene_keys(fig: Figure) -> list:
40
41
  return keys
41
42
 
42
43
 
43
- def _eye_to_vec(eye) -> np.ndarray:
44
- if eye is None or eye.x is None:
44
+ def _eye_to_vec(eye: dict) -> np.ndarray:
45
+ if not eye or eye.get("x") is None:
45
46
  return np.array([1.25, 1.25, 1.25])
46
- return np.array([eye.x, eye.y, eye.z])
47
+ return np.array([eye["x"], eye["y"], eye["z"]], dtype=float)
47
48
 
48
49
 
49
- _WORKER_FIG = None
50
+ _WORKER_FIG_DICT = None
50
51
 
51
52
 
52
53
  def _init_worker(fig_dict: dict) -> None:
53
- global _WORKER_FIG
54
- from plotly.graph_objects import Figure as _Figure
55
- _WORKER_FIG = _Figure(fig_dict)
54
+ global _WORKER_FIG_DICT
55
+ import warnings
56
+ warnings.filterwarnings("ignore", category=DeprecationWarning,
57
+ message=r".*[Kk]aleido.*")
58
+ _WORKER_FIG_DICT = fig_dict
56
59
 
57
60
 
58
61
  def _render_frame(args: Tuple[dict, int, int]) -> bytes:
59
- scene_updates, width, height = args
60
- _WORKER_FIG.update_layout(**scene_updates)
61
- return _WORKER_FIG.to_image(format="png", width=width, height=height)
62
+ cameras, width, height = args
63
+ import plotly.io as pio
64
+ layout = _WORKER_FIG_DICT.setdefault("layout", {})
65
+ for key, cam in cameras.items():
66
+ layout.setdefault(key, {})["camera"] = cam
67
+ return pio.to_image(_WORKER_FIG_DICT, format="png",
68
+ width=width, height=height, validate=False)
62
69
 
63
70
 
64
71
  def animate(
@@ -71,6 +78,7 @@ def animate(
71
78
  height: int = 600,
72
79
  loop: int = 0,
73
80
  progress: bool = True,
81
+ n_workers: Optional[int] = None,
74
82
  ) -> str:
75
83
  """
76
84
  Render a rotating GIF of a 3D Plotly figure.
@@ -90,12 +98,18 @@ def animate(
90
98
  height: Frame height in pixels. Defaults to 600.
91
99
  loop: Number of times the GIF should loop (0 = infinite). Defaults to 0.
92
100
  progress: Show a tqdm progress bar while rendering frames. Defaults to True.
101
+ n_workers: Number of parallel render processes. Defaults to
102
+ ``min(16, cpu_count, n_frames)``. Raise it on machines with many
103
+ cores for large frame counts. On non-Linux platforms worker
104
+ processes are spawned, so scripts calling ``animate()`` there must
105
+ be guarded by ``if __name__ == "__main__":``.
93
106
 
94
107
  Returns:
95
108
  The ``output_path`` it wrote to.
96
109
 
97
110
  Raises:
98
- ValueError: If `axis` is not one of ``'x'``, ``'y'``, ``'z'``.
111
+ ValueError: If `axis` is not one of ``'x'``, ``'y'``, ``'z'``, or
112
+ `n_workers` is less than 1.
99
113
  ImportError: If ``kaleido`` or ``Pillow`` is not installed.
100
114
 
101
115
  Examples:
@@ -116,6 +130,14 @@ def animate(
116
130
  "animate() requires Pillow. Install it with `pip install Pillow`."
117
131
  ) from e
118
132
 
133
+ try:
134
+ import kaleido # noqa: F401
135
+ except ImportError as e:
136
+ raise ImportError(
137
+ "animate() requires kaleido for PNG export. "
138
+ "Install it with `pip install 'kaleido<1.0'`."
139
+ ) from e
140
+
119
141
  axis_vec = _AXIS_VECTORS[axis]
120
142
  up_vec = {"x": dict(x=1, y=0, z=0),
121
143
  "y": dict(x=0, y=1, z=0),
@@ -125,45 +147,53 @@ def animate(
125
147
  if not scene_keys:
126
148
  raise ValueError("figure has no 3D scene to animate")
127
149
 
128
- initial_eyes = {k: _eye_to_vec(fig.layout[k].camera.eye) for k in scene_keys}
150
+ fig_dict = fig.to_dict()
151
+ layout_dict = fig_dict.setdefault("layout", {})
152
+
153
+ orig_cams = {}
154
+ initial_eyes = {}
155
+ for k in scene_keys:
156
+ cam = dict((layout_dict.get(k) or {}).get("camera") or {})
157
+ orig_cams[k] = cam
158
+ initial_eyes[k] = _eye_to_vec(cam.get("eye"))
129
159
 
130
160
  tasks = []
131
161
  for i in range(n_frames):
132
162
  theta = 2.0 * np.pi * i / n_frames
133
163
  R = _rotation_matrix(axis_vec, theta)
134
- scene_updates = {}
164
+ cameras = {}
135
165
  for k in scene_keys:
136
166
  eye = R @ initial_eyes[k]
137
- scene_updates[k] = dict(
138
- camera=dict(
139
- eye=dict(x=float(eye[0]), y=float(eye[1]), z=float(eye[2])),
140
- up=up_vec,
141
- )
142
- )
143
- tasks.append((scene_updates, width, height))
144
-
145
- fig_dict = fig.to_dict()
146
- n_workers = min(4, os.cpu_count() or 2, n_frames)
167
+ cameras[k] = {
168
+ **orig_cams[k],
169
+ "eye": dict(x=float(eye[0]), y=float(eye[1]), z=float(eye[2])),
170
+ "up": up_vec,
171
+ }
172
+ tasks.append((cameras, width, height))
173
+
174
+ if n_workers is None:
175
+ n_workers = min(16, os.cpu_count() or 2)
176
+ elif n_workers < 1:
177
+ raise ValueError(f"n_workers must be >= 1 (got {n_workers})")
178
+ n_workers = min(n_workers, n_frames)
179
+
180
+ # fork keeps unguarded user scripts working and skips per-worker re-imports;
181
+ # spawn elsewhere (fork is unsafe on macOS, unavailable on Windows).
182
+ start_method = "fork" if sys.platform.startswith("linux") else "spawn"
147
183
 
148
184
  pngs: list = [None] * n_frames
149
- try:
150
- with ProcessPoolExecutor(
151
- max_workers=n_workers,
152
- mp_context=multiprocessing.get_context("spawn"),
153
- initializer=_init_worker,
154
- initargs=(fig_dict,),
155
- ) as executor:
156
- iterator = executor.map(_render_frame, tasks, chunksize=1)
157
- for i, png in enumerate(
158
- tqdm(iterator, total=n_frames, desc="Rendering frames",
159
- unit="frame", disable=not progress)
160
- ):
161
- pngs[i] = png
162
- except ValueError as e:
163
- raise ImportError(
164
- "animate() requires kaleido for PNG export. "
165
- "Install it with `pip install 'kaleido<1.0'`."
166
- ) from e
185
+ with ProcessPoolExecutor(
186
+ max_workers=n_workers,
187
+ mp_context=multiprocessing.get_context(start_method),
188
+ initializer=_init_worker,
189
+ initargs=(fig_dict,),
190
+ ) as executor:
191
+ iterator = executor.map(_render_frame, tasks, chunksize=1)
192
+ for i, png in enumerate(
193
+ tqdm(iterator, total=n_frames, desc="Rendering frames",
194
+ unit="frame", disable=not progress)
195
+ ):
196
+ pngs[i] = png
167
197
 
168
198
  ref_palette = Image.open(BytesIO(pngs[0])).convert("P", palette=Image.ADAPTIVE)
169
199
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cloudglancer
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: Simple interactive visualization of 3D point clouds
5
5
  Author-email: Your Name <your.email@example.com>
6
6
  License: MIT
@@ -75,6 +75,9 @@ fig = cg.plot(points, size=2.0)
75
75
  cg.animate(fig, "rotation.gif", axis="z", n_frames=60)
76
76
  ```
77
77
 
78
+ Frames are rendered in parallel; pass `n_workers` to control how many render
79
+ processes are used (defaults to `min(16, cpu_count)`).
80
+
78
81
  Render a batch of point clouds as a grid of subplots (one cloud per cell):
79
82
 
80
83
  ```python
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cloudglancer"
7
- version = "0.1.7"
7
+ version = "0.1.8"
8
8
  description = "Simple interactive visualization of 3D point clouds"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -52,6 +52,13 @@ include = ["cloudglancer*"]
52
52
  [tool.setuptools.package-data]
53
53
  cloudglancer = ["py.typed"]
54
54
 
55
+ [tool.pytest.ini_options]
56
+ filterwarnings = [
57
+ # animate() forks render workers after the pool's manager thread starts; the
58
+ # workers never touch the inherited locks, so this CPython warning is benign here.
59
+ "ignore:This process .* is multi-threaded, use of fork:DeprecationWarning",
60
+ ]
61
+
55
62
  [tool.black]
56
63
  line-length = 100
57
64
  target-version = ["py39"]
@@ -54,6 +54,19 @@ def test_animate_combined_figure(tmp_path):
54
54
  assert out.exists() and _is_gif(out)
55
55
 
56
56
 
57
+ def test_animate_n_workers(tmp_path):
58
+ fig = plot(np.random.randn(20, 3))
59
+ out = tmp_path / "w.gif"
60
+ animate(fig, str(out), n_frames=3, width=120, height=120, n_workers=2, progress=False)
61
+ assert _is_gif(out)
62
+
63
+
64
+ def test_animate_invalid_n_workers(tmp_path):
65
+ fig = plot(np.random.randn(10, 3))
66
+ with pytest.raises(ValueError, match="n_workers must be >= 1"):
67
+ animate(fig, str(tmp_path / "x.gif"), n_frames=2, n_workers=0)
68
+
69
+
57
70
  @pytest.mark.parametrize("axis", ["x", "y", "z"])
58
71
  def test_animate_each_axis(tmp_path, axis):
59
72
  fig = plot(np.random.randn(20, 3))
File without changes
File without changes
File without changes