-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpr_sampler_panel.py
More file actions
380 lines (311 loc) · 12.5 KB
/
vpr_sampler_panel.py
File metadata and controls
380 lines (311 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""
vpr_sampler_panel.py — VPR Sequence Sampler (Panel web app)
=============================================================
Browser-based version of vpr_sampler.py. No display required — runs
headlessly on an HPC and is accessed via an SSH tunnel from your laptop.
Usage
-----
panel serve vpr_sampler_panel.py \
--args \
--D_matrix path/to/D.npy \
--rgb_csv path/to/rgb.csv \
--img_root path/to/image/root \
--output path/to/output_folder \
[--extra_folders rgb_1 depth_0] \
[--n_thresholds 50] \
[--th_min 0.1] \
[--th_max 1.0]
SSH tunnel (run on your laptop)
--------------------------------
ssh -L 5006:localhost:5006 user@hpc-hostname
Then open:
http://localhost:5006/vpr_sampler_panel
Workflow
--------
1. Click anywhere on the threshold curve — the nearest evaluated threshold
is selected and the D matrix comparison updates instantly.
2. Press "Save images" to copy the selected frames to disk.
All output folders are cleared before each save.
Install
-------
pip install panel bokeh matplotlib numpy pandas
"""
import argparse
import shutil
from io import BytesIO
from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import panel as pn
import panel.widgets as pnw
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Span, TapTool
pn.extension(sizing_mode="stretch_width")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def parse_args():
p = argparse.ArgumentParser(description="VPR Sequence Sampler — Panel app")
p.add_argument("--D_matrix", required=True)
p.add_argument("--rgb_csv", required=True)
p.add_argument("--img_root", required=True)
p.add_argument("--output", required=True)
p.add_argument("--extra_folders", nargs="*", default=[])
p.add_argument("--n_thresholds", type=int, default=50)
p.add_argument("--th_min", type=float, default=0.1)
p.add_argument("--th_max", type=float, default=1.0)
return p.parse_args()
# ---------------------------------------------------------------------------
# I/O helpers
# ---------------------------------------------------------------------------
def load_inputs(D_path, csv_path, img_root):
D = np.load(D_path)
assert D.ndim == 2 and D.shape[0] == D.shape[1], \
f"D must be square, got {D.shape}"
df = pd.read_csv(csv_path)
missing = {"ts_rgb_0 (ns)", "path_rgb_0"} - set(df.columns)
if missing:
raise ValueError(f"CSV missing columns: {missing}")
root = Path(img_root)
image_paths = [root / rel for rel in df["path_rgb_0"]]
if len(image_paths) != D.shape[0]:
raise ValueError(f"D has {D.shape[0]} rows but CSV has {len(image_paths)} entries.")
return D, image_paths
def collect_extra_folders(img_root, folder_names, n):
root, folders = Path(img_root), []
for name in folder_names:
src = root / name
if not src.is_dir():
print(f"[warn] extra folder not found, skipping: {src}")
continue
imgs = sorted([f for f in src.iterdir()
if f.suffix.lower() in {".png", ".jpg", ".jpeg", ".tif", ".tiff"}])
if len(imgs) != n:
print(f"[warn] {src} has {len(imgs)} images, expected {n} — skipping")
continue
folders.append((src, src.parent / f"{name}_filtered", imgs))
return folders
# ---------------------------------------------------------------------------
# Sampling
# ---------------------------------------------------------------------------
def run_threshold_sweep(D, th_min, th_max, n_steps):
all_indexes, results = {}, []
for th in np.linspace(th_min, th_max, num=n_steps):
idxs = _sample_indexes(D, th)
results.append((th, len(idxs)))
all_indexes[th] = idxs
return all_indexes, results
def _sample_indexes(D, th):
indexes = [0]
i = j = 0
while i < D.shape[0] and j < D.shape[1]:
while j < D.shape[1]:
if D[i, j] > th:
indexes.append(j - 1)
i = j
j += 1
return indexes
# ---------------------------------------------------------------------------
# Saving
# ---------------------------------------------------------------------------
def _clear_and_save(src_paths, indexes, dst_dir, label):
dst_dir = Path(dst_dir)
if dst_dir.exists():
for f in dst_dir.iterdir():
if f.is_file():
f.unlink()
dst_dir.mkdir(parents=True, exist_ok=True)
copied = 0
for idx in indexes:
if idx >= len(src_paths):
continue
src = Path(src_paths[idx])
if not src.exists():
print(f"[warn] not found: {src}")
continue
shutil.copy(src, dst_dir / src.name)
copied += 1
print(f"Saved {copied} -> {dst_dir} [{label}]")
return copied
def save_all(image_paths, extra_folders, indexes, output_dir):
total = _clear_and_save(image_paths, indexes, output_dir, "primary")
for src_dir, dst_dir, imgs in extra_folders:
total += _clear_and_save(imgs, indexes, dst_dir, src_dir.name)
return total
# ---------------------------------------------------------------------------
# Matplotlib D-matrix figure -> PNG pane
# ---------------------------------------------------------------------------
def make_matrix_png(D, indexes, nearest_th):
D_filtered = D[np.ix_(indexes, indexes)]
fig, axes = plt.subplots(1, 2, figsize=(11, 4.5))
fig.suptitle(
f"th = {nearest_th:.4f} | {len(indexes)} / {D.shape[0]} frames",
fontsize=11
)
im0 = axes[0].imshow(D, cmap="viridis", aspect="auto")
axes[0].set_title("Original D")
axes[0].set_xlabel("j"); axes[0].set_ylabel("i")
plt.colorbar(im0, ax=axes[0])
im1 = axes[1].imshow(D_filtered, cmap="viridis", aspect="auto")
axes[1].set_title(f"Filtered D ({len(indexes)} frames)")
axes[1].set_xlabel("j (sub)"); axes[1].set_ylabel("i (sub)")
plt.colorbar(im1, ax=axes[1])
fig.tight_layout()
buf = BytesIO()
fig.savefig(buf, format="png", dpi=120, bbox_inches="tight")
plt.close(fig)
buf.seek(0)
return buf.read()
# ---------------------------------------------------------------------------
# Bokeh curve with real tap interaction
# ---------------------------------------------------------------------------
def make_bokeh_curve(ths, lengths, all_indexes, D, image_paths,
extra_folders, output_dir, matrix_pane,
status_md, save_btn, state):
th_arr = np.array(ths)
source = ColumnDataSource(dict(th=ths, n=lengths))
p = figure(
width=600, height=350,
title="Click on the curve to select a threshold",
x_axis_label="Threshold (th)",
y_axis_label="Selected frames",
tools="tap,reset",
toolbar_location="above",
)
p.line("th", "n", source=source, line_width=2, color="steelblue")
p.circle("th", "n", source=source, size=6, color="steelblue",
hover_color="crimson", nonselection_alpha=0.4)
p.grid.grid_line_dash = [6, 4]
vline = Span(location=0, dimension="height",
line_color="crimson", line_dash="dashed", line_width=2,
visible=False)
p.add_layout(vline)
# TapTool fires selected.on_change when the user clicks a point
def on_selection_change(attr, old, new):
if not new:
return
# 'new' is a list of selected indices into the ColumnDataSource
idx = new[0]
nearest_th = ths[idx]
indexes = all_indexes[nearest_th]
state["th"] = nearest_th
state["indexes"] = indexes
# update vline
vline.location = nearest_th
vline.visible = True
# update D matrix pane
png_bytes = make_matrix_png(D, indexes, nearest_th)
matrix_pane.object = png_bytes
# update status
status_md.object = (
f"**Threshold:** `{nearest_th:.4f}` | "
f"**Selected:** {len(indexes)} / {D.shape[0]} frames"
)
save_btn.disabled = False
# clear selection so the same point can be clicked again
source.selected.indices = []
source.selected.on_change("indices", on_selection_change)
return p
# ---------------------------------------------------------------------------
# Full Panel app
# ---------------------------------------------------------------------------
def build_app(D, image_paths, extra_folders, all_indexes, results, output_dir):
ths = [r[0] for r in results]
lengths = [r[1] for r in results]
state = {"th": None, "indexes": None}
# ---- reactive widgets ----
status_md = pn.pane.Markdown(
"Click on the curve to select a threshold.",
styles={"background": "#f0f4f8", "padding": "8px", "border-radius": "4px"},
)
n_total = 1 + len(extra_folders)
save_btn = pnw.Button(
name=f"Save images ({n_total} folder{'s' if n_total > 1 else ''})",
button_type="primary", disabled=True, width=240,
)
matrix_pane = pn.pane.PNG(
object=None, sizing_mode="stretch_width",
min_height=300,
)
matrix_placeholder = pn.pane.Markdown(
"_Select a threshold to see the D matrix comparison._",
sizing_mode="stretch_width",
)
# show placeholder until first click
matrix_col = pn.Column(matrix_placeholder, sizing_mode="stretch_width")
def on_save(event):
if state["indexes"] is None:
return
save_btn.name = "Saving..."
save_btn.disabled = True
save_all(image_paths, extra_folders, state["indexes"], output_dir)
save_btn.name = "Saved ✓"
save_btn.disabled = False
out_dirs = [str(output_dir)] + [str(dst) for _, dst, _ in extra_folders]
status_md.object = (
f"**Saved** {len(state['indexes'])} images across "
f"{n_total} folder(s):\n\n"
+ "\n\n".join(f"- `{d}`" for d in out_dirs)
)
save_btn.on_click(on_save)
# wrap the matrix pane so it swaps in after first click
def on_selection_done(attr, old, new):
# called from bokeh; swap placeholder for real pane on first click
if matrix_placeholder in matrix_col:
matrix_col.clear()
matrix_col.append(matrix_pane)
# ---- Bokeh curve ----
bokeh_curve = make_bokeh_curve(
ths, lengths, all_indexes, D, image_paths,
extra_folders, output_dir,
matrix_pane, status_md, save_btn, state,
)
# hook the swap-in callback onto the source inside the figure
bokeh_curve.renderers[1].data_source.selected.on_change(
"indices", on_selection_done
)
curve_pane = pn.pane.Bokeh(bokeh_curve, sizing_mode="stretch_width")
# ---- layout ----
sidebar = pn.Column(
pn.pane.Markdown("## VPR Sequence Sampler"),
pn.pane.Markdown(
f"**D:** `{D.shape[0]}×{D.shape[1]}` | "
f"**Images:** {len(image_paths)} | "
f"**Extra folders:** {len(extra_folders)}"
),
pn.layout.Divider(),
curve_pane,
pn.layout.Divider(),
save_btn,
pn.layout.Divider(),
status_md,
width=640, margin=(10, 20),
)
main = pn.Column(
pn.pane.Markdown("### D matrix comparison"),
matrix_col,
sizing_mode="stretch_both", margin=(10, 10),
)
return pn.Row(sidebar, main, sizing_mode="stretch_both")
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
args = parse_args()
D, img_paths = load_inputs(args.D_matrix, args.rgb_csv, args.img_root)
extra = collect_extra_folders(args.img_root, args.extra_folders, D.shape[0])
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
print(f"D shape : {D.shape}")
print(f"Images : {len(img_paths)}")
print(f"Extra folders: {len(extra)}")
all_indexes, results = run_threshold_sweep(
D, args.th_min, args.th_max, args.n_thresholds
)
ths, lengths = zip(*results)
print(f"Threshold range: {min(ths):.2f} – {max(ths):.2f} | "
f"frames range: {min(lengths)} – {max(lengths)}")
app = build_app(D, img_paths, extra, all_indexes, results, output_dir)
app.servable()