miblab-plot 0.0.3__tar.gz → 0.0.5__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.
- {miblab_plot-0.0.3/src/miblab_plot.egg-info → miblab_plot-0.0.5}/PKG-INFO +1 -1
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/pyproject.toml +1 -1
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/pvplot.py +177 -51
- {miblab_plot-0.0.3 → miblab_plot-0.0.5/src/miblab_plot.egg-info}/PKG-INFO +1 -1
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/LICENSE +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/README.md +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/setup.cfg +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/__init__.py +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/gui.py +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/image_3d.py +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/movie.py +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot/mp4.py +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot.egg-info/SOURCES.txt +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot.egg-info/dependency_links.txt +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot.egg-info/requires.txt +0 -0
- {miblab_plot-0.0.3 → miblab_plot-0.0.5}/src/miblab_plot.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
from pathlib import Path
|
|
3
3
|
from typing import Union
|
|
4
|
+
import gc
|
|
4
5
|
|
|
5
6
|
import numpy as np
|
|
6
7
|
from tqdm import tqdm
|
|
@@ -9,6 +10,27 @@ import dbdicom as db
|
|
|
9
10
|
import zarr
|
|
10
11
|
|
|
11
12
|
import miblab_ssa as ssa
|
|
13
|
+
import platform
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def setup_rendering_env():
|
|
17
|
+
current_os = platform.system().lower()
|
|
18
|
+
# On Linux, check for DISPLAY. On Windows/Mac, we usually assume a display exists.
|
|
19
|
+
is_headless = (os.environ.get("DISPLAY") is None) if current_os == "linux" else False
|
|
20
|
+
|
|
21
|
+
if is_headless:
|
|
22
|
+
try:
|
|
23
|
+
pv.start_xvfb()
|
|
24
|
+
except Exception:
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
# IMPORTANT: We want to ALLOW off-screen rendering even on Windows
|
|
28
|
+
# So we don't force pv.OFF_SCREEN to be False just because we have a screen.
|
|
29
|
+
pv.OFF_SCREEN = True
|
|
30
|
+
|
|
31
|
+
print(f"OS: {current_os} | Headless: {is_headless} | Off-screen: {pv.OFF_SCREEN}")
|
|
32
|
+
|
|
33
|
+
setup_rendering_env()
|
|
12
34
|
|
|
13
35
|
|
|
14
36
|
def mosaic_masks_dcm(masks, imagefile, labels=None, view_vector=(1, 0, 0)):
|
|
@@ -23,7 +45,13 @@ def mosaic_masks_dcm(masks, imagefile, labels=None, view_vector=(1, 0, 0)):
|
|
|
23
45
|
nrows = int(np.ceil(np.sqrt((width*n_mosaics)/(aspect_ratio*height))))
|
|
24
46
|
ncols = int(np.ceil(n_mosaics/nrows))
|
|
25
47
|
|
|
26
|
-
plotter = pv.Plotter(
|
|
48
|
+
plotter = pv.Plotter(
|
|
49
|
+
window_size=(ncols*width, nrows*height),
|
|
50
|
+
shape=(nrows, ncols),
|
|
51
|
+
border=False,
|
|
52
|
+
# off_screen=True,
|
|
53
|
+
off_screen=pv.OFF_SCREEN,
|
|
54
|
+
)
|
|
27
55
|
plotter.background_color = 'white'
|
|
28
56
|
|
|
29
57
|
row = 0
|
|
@@ -57,72 +85,167 @@ def mosaic_masks_dcm(masks, imagefile, labels=None, view_vector=(1, 0, 0)):
|
|
|
57
85
|
|
|
58
86
|
|
|
59
87
|
def rotating_masks_grid(
|
|
60
|
-
dir_output:str,
|
|
61
|
-
masks:Union[zarr.Array, np.ndarray],
|
|
62
|
-
labels:np.ndarray=None,
|
|
63
|
-
nviews=25,
|
|
88
|
+
dir_output: str,
|
|
89
|
+
masks: Union[zarr.Array, np.ndarray],
|
|
90
|
+
labels: np.ndarray = None,
|
|
91
|
+
nviews = 25,
|
|
64
92
|
):
|
|
65
|
-
#
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
93
|
+
# 1. Setup metadata
|
|
94
|
+
width, height = 150, 150
|
|
95
|
+
angles = np.linspace(0, 2*np.pi, nviews)
|
|
96
|
+
# View directions (Z-rotation and Y-rotation)
|
|
97
|
+
dirs = [(np.cos(a), np.sin(a), 0.0) for a in angles]
|
|
98
|
+
dirs += [(np.cos(a), 0.0, np.sin(a)) for a in angles]
|
|
99
|
+
|
|
100
|
+
ncols, nrows = masks.shape[0], masks.shape[1]
|
|
101
|
+
os.makedirs(dir_output, exist_ok=True)
|
|
102
|
+
|
|
103
|
+
# 2. Outer Loop: Views (The "Memory Clear" loop)
|
|
104
|
+
# By putting 'views' on the outside, we only ever need ONE plotter in RAM.
|
|
105
|
+
for i, vec in enumerate(tqdm(dirs, desc="Rendering View Angles")):
|
|
106
|
+
|
|
107
|
+
plotter = pv.Plotter(
|
|
108
|
+
window_size=(ncols*width, nrows*height),
|
|
109
|
+
shape=(nrows, ncols),
|
|
110
|
+
border=False,
|
|
111
|
+
# off_screen=True,
|
|
112
|
+
off_screen=pv.OFF_SCREEN,
|
|
113
|
+
)
|
|
114
|
+
plotter.background_color = 'white'
|
|
115
|
+
|
|
116
|
+
# 3. Inner Loops: Grid
|
|
117
|
+
for row in range(nrows):
|
|
118
|
+
for col in range(ncols):
|
|
119
|
+
# Load mask (27MB bool) -> Convert to 109MB float32 (not float64!)
|
|
120
|
+
mask_norm = masks[col, row, ...].astype(np.float32)
|
|
121
|
+
|
|
122
|
+
# Generate Surface Mesh (The 10x-to-Mesh conversion)
|
|
123
|
+
#
|
|
124
|
+
vol = pv.wrap(mask_norm)
|
|
125
|
+
vol.spacing = [1.0, 1.0, 1.0]
|
|
126
|
+
surf = vol.contour(isosurfaces=[0.5])
|
|
127
|
+
|
|
128
|
+
# Cleanup volume from RAM immediately
|
|
129
|
+
del vol
|
|
130
|
+
|
|
131
|
+
# Camera Math
|
|
132
|
+
distance = surf.length * 2.5
|
|
133
|
+
center = list(surf.center)
|
|
134
|
+
pos = center + distance * np.array(vec)
|
|
135
|
+
|
|
136
|
+
# Plotting
|
|
137
|
+
plotter.subplot(row, col)
|
|
138
|
+
if labels is not None:
|
|
139
|
+
plotter.add_text(str(labels[col, row]), font_size=6, color='black')
|
|
140
|
+
|
|
141
|
+
plotter.add_mesh(surf, color='lightblue', smooth_shading=True)
|
|
142
|
+
plotter.camera_position = [pos, center, (0, 0, 1)] # Simple Up-vector
|
|
143
|
+
|
|
144
|
+
# 4. Save and Destroy
|
|
145
|
+
# This is where the RAM resets to zero for the next angle
|
|
146
|
+
file = os.path.join(dir_output, f"mosaic_{i:03d}.png")
|
|
147
|
+
plotter.screenshot(file)
|
|
148
|
+
plotter.close() # CRITICAL: Releases the GPU and RAM buffers
|
|
149
|
+
del plotter
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def rotating_mosaics_da(dir_output, masks:list, labels=None, chunksize=None, nviews=25, columns=None, rows=None):
|
|
155
|
+
# masks - list of numpy or dask arrays
|
|
156
|
+
if labels is None:
|
|
157
|
+
labels = [str(i) for i in range(len(masks))]
|
|
158
|
+
if chunksize is None:
|
|
159
|
+
chunksize = len(masks)
|
|
160
|
+
os.makedirs(dir_output, exist_ok=True)
|
|
161
|
+
|
|
162
|
+
# Split into numbered chunks
|
|
163
|
+
def chunk_list(lst, size):
|
|
164
|
+
chunks = [lst[i:i+size] for i in range(0, len(lst), size)]
|
|
165
|
+
return list(enumerate(chunks))
|
|
166
|
+
|
|
167
|
+
mask_chunks = chunk_list(masks, chunksize)
|
|
168
|
+
label_chunks = chunk_list(labels, chunksize)
|
|
70
169
|
|
|
71
170
|
# Define view points
|
|
72
171
|
angles = np.linspace(0, 2*np.pi, nviews)
|
|
73
172
|
dirs = [(np.cos(a), np.sin(a), 0.0) for a in angles] # rotate around z
|
|
74
173
|
dirs += [(np.cos(a), 0.0, np.sin(a)) for a in angles] # rotate around y
|
|
75
174
|
|
|
76
|
-
#
|
|
77
|
-
|
|
78
|
-
|
|
175
|
+
# Save mosaics for each chunk and view
|
|
176
|
+
for mask_chunk, label_chunk in tqdm(zip(mask_chunks, label_chunks), total=len(label_chunks), desc='Building chunk of mosaics'):
|
|
177
|
+
chunk_idx = mask_chunk[0]
|
|
178
|
+
names = [f"group_{str(chunk_idx).zfill(2)}_{i:02d}.png" for i in range(len(dirs))]
|
|
179
|
+
directions = {vec: os.path.join(dir_output, name) for name, vec in zip(names, dirs)}
|
|
180
|
+
multiple_mosaic_masks_da(mask_chunk[1], directions, label_chunk[1], columns=columns, rows=rows)
|
|
181
|
+
|
|
182
|
+
def multiple_mosaic_masks_da(masks: list, directions: dict, labels, columns=None, rows=None):
|
|
183
|
+
# Plot settings
|
|
184
|
+
width, height = 150, 150
|
|
185
|
+
aspect_ratio = 16/9
|
|
79
186
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
187
|
+
n_mosaics = len(masks)
|
|
188
|
+
ncols = columns if columns else int(np.ceil(np.sqrt((height*n_mosaics)/(aspect_ratio*width))))
|
|
189
|
+
nrows = rows if rows else int(np.ceil(n_mosaics/ncols))
|
|
190
|
+
|
|
191
|
+
# We loop through DIRECTIONS first. This keeps only ONE plotter in RAM at a time.
|
|
192
|
+
# To avoid re-computing the Spline/RBF surface 50 times per mask,
|
|
193
|
+
# we pre-calculate the surfaces once.
|
|
194
|
+
|
|
195
|
+
surfaces = []
|
|
196
|
+
for mask_series in tqdm(masks, desc="Pre-calculating Surfaces"):
|
|
197
|
+
# Your Spline/RBF smoothing
|
|
198
|
+
#mask_norm = ssa.sdf_ft.smooth_mask(mask_series[:].astype(bool), order=16)
|
|
199
|
+
mask_norm = mask_series[:].astype(bool)
|
|
200
|
+
|
|
201
|
+
vol = pv.wrap(mask_norm.astype(np.float32))
|
|
202
|
+
vol.spacing = [1.0, 1.0, 1.0]
|
|
203
|
+
surf = vol.contour(isosurfaces=[0.5])
|
|
204
|
+
surfaces.append(surf)
|
|
205
|
+
del vol # Clear RAM
|
|
206
|
+
|
|
207
|
+
# Now render each view angle
|
|
208
|
+
for vec, file_path in tqdm(directions.items(), desc="Rendering Views"):
|
|
209
|
+
plotter = pv.Plotter(
|
|
83
210
|
window_size=(ncols*width, nrows*height),
|
|
84
211
|
shape=(nrows, ncols),
|
|
85
212
|
border=False,
|
|
86
|
-
off_screen=
|
|
213
|
+
off_screen=pv.OFF_SCREEN,
|
|
87
214
|
)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
for row in tqdm(range(nrows), desc=f'Building mosaic'):
|
|
91
|
-
for col in range(ncols):
|
|
92
|
-
|
|
93
|
-
# Load data once
|
|
94
|
-
mask_norm = masks[col, row, ...]
|
|
95
|
-
|
|
96
|
-
orig_vol = pv.wrap(mask_norm.astype(float))
|
|
97
|
-
orig_vol.spacing = [1.0, 1.0, 1.0]
|
|
98
|
-
orig_surface = orig_vol.contour(isosurfaces=[0.5])
|
|
215
|
+
plotter.background_color = 'white'
|
|
99
216
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
distance = orig_surface.length * 2.0 # controls zoom
|
|
104
|
-
center = list(orig_surface.center)
|
|
105
|
-
pos = center + distance * np.array(vec) # vec = direction
|
|
106
|
-
up = _camera_up_from_direction(vec, prev_up)
|
|
107
|
-
prev_up = up
|
|
217
|
+
for i, surf in enumerate(surfaces):
|
|
218
|
+
row, col = divmod(i, ncols)
|
|
219
|
+
if row >= nrows: break # Safety break
|
|
108
220
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
221
|
+
# Camera Position
|
|
222
|
+
distance = surf.length * 2.0
|
|
223
|
+
center = list(surf.center)
|
|
224
|
+
pos = center + distance * np.array(vec)
|
|
225
|
+
|
|
226
|
+
# Using a simple up vector (0,0,1) is usually more stable across views
|
|
227
|
+
# unless you have a specific requirement for the _camera_up_from_direction
|
|
228
|
+
up = (0, 0, 1)
|
|
115
229
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
230
|
+
plotter.subplot(row, col)
|
|
231
|
+
if labels is not None:
|
|
232
|
+
plotter.add_text(labels[i], font_size=6, color='black')
|
|
233
|
+
|
|
234
|
+
plotter.add_mesh(surf, color='lightblue', smooth_shading=True)
|
|
235
|
+
plotter.camera_position = [pos, center, up]
|
|
121
236
|
|
|
237
|
+
# 1. Initialize the window in the background
|
|
238
|
+
plotter.show(auto_close=False, interactive=False, interactive_update=True)
|
|
239
|
+
|
|
240
|
+
# 2. Capture the pixels
|
|
241
|
+
plotter.screenshot(file_path)
|
|
242
|
+
|
|
243
|
+
# 3. Destroy the plotter immediately
|
|
244
|
+
plotter.close()
|
|
122
245
|
|
|
123
246
|
|
|
124
247
|
|
|
125
|
-
def rotating_mosaics_npz(dir_output, masks, labels=None, chunksize=None, nviews=25, columns=None, rows=None):
|
|
248
|
+
def rotating_mosaics_npz(dir_output, masks:list, labels=None, chunksize=None, nviews=25, columns=None, rows=None):
|
|
126
249
|
|
|
127
250
|
if labels is None:
|
|
128
251
|
labels = [str(i) for i in range(len(masks))]
|
|
@@ -175,7 +298,8 @@ def multiple_mosaic_masks_npz(masks, directions:dict, labels, columns=None, rows
|
|
|
175
298
|
window_size=(ncols*width, nrows*height),
|
|
176
299
|
shape=(nrows, ncols),
|
|
177
300
|
border=False,
|
|
178
|
-
off_screen=True,
|
|
301
|
+
# off_screen=True,
|
|
302
|
+
off_screen=pv.OFF_SCREEN,
|
|
179
303
|
)
|
|
180
304
|
plotters[vec].background_color = 'white'
|
|
181
305
|
|
|
@@ -280,7 +404,8 @@ def mosaic_masks_npz(masks, imagefile, labels=None, view_vector=(1, 0, 0)):
|
|
|
280
404
|
window_size=(ncols*width, nrows*height),
|
|
281
405
|
shape=(nrows, ncols),
|
|
282
406
|
border=False,
|
|
283
|
-
off_screen=True,
|
|
407
|
+
# off_screen=True,
|
|
408
|
+
off_screen=pv.OFF_SCREEN,
|
|
284
409
|
)
|
|
285
410
|
plotter.background_color = 'white'
|
|
286
411
|
|
|
@@ -330,7 +455,8 @@ def mosaic_features_npz(features, imagefile, labels=None, view_vector=(1, 0, 0))
|
|
|
330
455
|
window_size=(ncols*width, nrows*height),
|
|
331
456
|
shape=(nrows, ncols),
|
|
332
457
|
border=False,
|
|
333
|
-
off_screen=True,
|
|
458
|
+
# off_screen=True,
|
|
459
|
+
off_screen=pv.OFF_SCREEN,
|
|
334
460
|
)
|
|
335
461
|
plotter.background_color = 'white'
|
|
336
462
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|