Skip to content

Commit 689fd31

Browse files
alxndrkalininclaude
andcommitted
style: ruff check + format the HF demo scripts
Sort imports (I001) in upload_checkpoints.py and upload_hf_space.py and ruff-format app.py, predict_runner.py, and upload_checkpoints.py (checkpoint tuple reflow). Formatting only — no behavior change. Fixes the failing Lint check on PR #449. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 705bc76 commit 689fd31

4 files changed

Lines changed: 113 additions & 79 deletions

File tree

applications/dynacell/examples/hf_demo/hf_space/app.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
FLUOR_CH = 2
4444
_DEMO_REPO = "biohub/dynacell-demo-data"
4545

46-
PATCH_D = 8 # Z window CELL-Diff operates on (center of the stack)
47-
PANEL_IN = 2.2 # per-panel width (inches) for data + regression → equal image heights
48-
FIG_H = 2.8 # figure height (inches) for data + regression
46+
PATCH_D = 8 # Z window CELL-Diff operates on (center of the stack)
47+
PANEL_IN = 2.2 # per-panel width (inches) for data + regression → equal image heights
48+
FIG_H = 2.8 # figure height (inches) for data + regression
4949
# Generative panels are larger: its controls column is taller (extra ODE-step slider).
5050
GEN_PANEL_IN = 3.0
5151
GEN_FIG_H = 3.6
@@ -55,6 +55,7 @@
5555
# Helpers
5656
# ---------------------------------------------------------------------------
5757

58+
5859
def is_dark(request: gr.Request | None) -> bool:
5960
"""Detect the client theme from the `__theme` query param (default: dark)."""
6061
try:
@@ -125,6 +126,7 @@ def compute_spectral_pcc(pred_zarr_path: str, gt_fluor_vol: np.ndarray) -> float
125126
# Renderers (all transparent + themed; constant per-panel size → equal heights)
126127
# ---------------------------------------------------------------------------
127128

129+
128130
def render_phase_exp(
129131
zarr_state: str | None,
130132
timepoint: int,
@@ -227,9 +229,9 @@ def render_trajectory_frame(
227229
if traj_info is None:
228230
return None
229231
with np.load(traj_info["traj_path"]) as data:
230-
traj = data["traj"] # (num_steps, 1, D, H, W)
232+
traj = data["traj"] # (num_steps, 1, D, H, W)
231233
phase = data["phase"] # (D, H, W)
232-
gt = data["gt"] # (D, H, W)
234+
gt = data["gt"] # (D, H, W)
233235
z_start = traj_info["z_start"]
234236
patch_d = traj_info["patch_d"]
235237
organelle = traj_info["organelle"]
@@ -258,6 +260,7 @@ def render_trajectory_frame(
258260
# 1. Data
259261
# ---------------------------------------------------------------------------
260262

263+
261264
def load_demo_data(organelle: str, progress=gr.Progress(), request: gr.Request | None = None) -> tuple:
262265
"""Download + extract the demo zarr; set every section's slider ranges; render view."""
263266
from huggingface_hub import hf_hub_download
@@ -278,17 +281,17 @@ def t_slider():
278281
return gr.Slider(minimum=0, maximum=n_tp - 1, step=1, value=0)
279282

280283
return (
281-
data_path, # zarr_state
282-
status, # data_status
283-
t_slider(), # data_t
284-
gr.Slider(minimum=0, maximum=n_z - 1, step=1, value=z_mid), # data_z
285-
t_slider(), # reg_t
286-
gr.Slider(minimum=0, maximum=n_z - 1, step=1, value=z_mid), # reg_z
287-
t_slider(), # gen_t
288-
gr.Slider( # gen_z (center patch)
284+
data_path, # zarr_state
285+
status, # data_status
286+
t_slider(), # data_t
287+
gr.Slider(minimum=0, maximum=n_z - 1, step=1, value=z_mid), # data_z
288+
t_slider(), # reg_t
289+
gr.Slider(minimum=0, maximum=n_z - 1, step=1, value=z_mid), # reg_z
290+
t_slider(), # gen_t
291+
gr.Slider( # gen_z (center patch)
289292
minimum=z_start, maximum=z_start + PATCH_D - 1, step=1, value=z_start + PATCH_D // 2
290293
),
291-
fig, # data_view
294+
fig, # data_view
292295
)
293296

294297

@@ -309,6 +312,7 @@ def on_data_slider(
309312
# 2. Regression models
310313
# ---------------------------------------------------------------------------
311314

315+
312316
def run_regression(
313317
zarr_state: str | None,
314318
organelle: str,
@@ -347,8 +351,12 @@ def run_regression(
347351
print(f"Prediction failed for {model_key}: {e}")
348352

349353
pred_info = {
350-
"timepoint": tp, "organelle": organelle, "selected_models": list(selected_models),
351-
"paths": pred_paths, "pccs": pred_pccs, "n_z": n_z,
354+
"timepoint": tp,
355+
"organelle": organelle,
356+
"selected_models": list(selected_models),
357+
"paths": pred_paths,
358+
"pccs": pred_pccs,
359+
"n_z": n_z,
352360
}
353361
progress(0.9, desc="Rendering...")
354362
fig = render_predictions(pred_info, min(int(z_slice), n_z - 1), zarr_state, is_dark(request))
@@ -372,6 +380,7 @@ def rerender_regression(
372380
# 3. Generative model — CELL-Diff
373381
# ---------------------------------------------------------------------------
374382

383+
375384
def run_generative(
376385
zarr_state: str | None,
377386
organelle: str,
@@ -430,7 +439,8 @@ def rerender_generative(
430439
with gr.Column(scale=1):
431440
organelle = gr.Dropdown(
432441
choices=[(ORGANELLE_LABELS[o], o) for o in ORGANELLES],
433-
value="CAAX", label="Organelle",
442+
value="CAAX",
443+
label="Organelle",
434444
info="Select the target organelle.",
435445
)
436446
load_demo_btn = gr.Button("Load Demo Data", variant="primary")
@@ -447,7 +457,8 @@ def rerender_generative(
447457
with gr.Column(scale=1):
448458
reg_models = gr.CheckboxGroup(
449459
choices=[("FNet3D", "fnet3d"), ("VSCyto3D", "vscyto3d")],
450-
value=REGRESSION_MODELS, label="Models",
460+
value=REGRESSION_MODELS,
461+
label="Models",
451462
)
452463
reg_t = gr.Slider(0, 4, step=1, value=0, label="Timepoint")
453464
reg_z = gr.Slider(0, 15, step=1, value=8, label="Z slice")

applications/dynacell/examples/hf_demo/hf_space/predict_runner.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@
1818

1919
# (model, organelle) → filename in the HF checkpoint repo
2020
CHECKPOINT_FILES: dict[tuple[str, str], str] = {
21-
("celldiff", "CAAX"): "celldiff_caax.ckpt",
22-
("celldiff", "H2B"): "celldiff_h2b.ckpt",
23-
("celldiff", "SEC61B"): "celldiff_sec61b.ckpt",
24-
("celldiff", "TOMM20"): "celldiff_tomm20.ckpt",
25-
("fnet3d", "CAAX"): "fnet3d_caax.ckpt",
26-
("fnet3d", "H2B"): "fnet3d_h2b.ckpt",
27-
("fnet3d", "SEC61B"): "fnet3d_sec61b.ckpt",
28-
("fnet3d", "TOMM20"): "fnet3d_tomm20.ckpt",
29-
("vscyto3d", "CAAX"): "vscyto3d_caax.ckpt",
30-
("vscyto3d", "H2B"): "vscyto3d_h2b.ckpt",
31-
("vscyto3d", "SEC61B"): "vscyto3d_sec61b.ckpt",
32-
("vscyto3d", "TOMM20"): "vscyto3d_tomm20.ckpt",
21+
("celldiff", "CAAX"): "celldiff_caax.ckpt",
22+
("celldiff", "H2B"): "celldiff_h2b.ckpt",
23+
("celldiff", "SEC61B"): "celldiff_sec61b.ckpt",
24+
("celldiff", "TOMM20"): "celldiff_tomm20.ckpt",
25+
("fnet3d", "CAAX"): "fnet3d_caax.ckpt",
26+
("fnet3d", "H2B"): "fnet3d_h2b.ckpt",
27+
("fnet3d", "SEC61B"): "fnet3d_sec61b.ckpt",
28+
("fnet3d", "TOMM20"): "fnet3d_tomm20.ckpt",
29+
("vscyto3d", "CAAX"): "vscyto3d_caax.ckpt",
30+
("vscyto3d", "H2B"): "vscyto3d_h2b.ckpt",
31+
("vscyto3d", "SEC61B"): "vscyto3d_sec61b.ckpt",
32+
("vscyto3d", "TOMM20"): "vscyto3d_tomm20.ckpt",
3333
}
3434

3535
TARGET_CHANNELS: dict[str, str] = {
36-
"CAAX": "Membrane",
37-
"H2B": "Nuclei",
36+
"CAAX": "Membrane",
37+
"H2B": "Nuclei",
3838
"SEC61B": "Structure",
3939
"TOMM20": "Structure",
4040
}
4141

4242
ORGANELLE_LABELS: dict[str, str] = {
43-
"CAAX": "Membrane (CAAX)",
44-
"H2B": "Chromatin (H2B)",
43+
"CAAX": "Membrane (CAAX)",
44+
"H2B": "Chromatin (H2B)",
4545
"SEC61B": "ER (SEC61B)",
4646
"TOMM20": "Mitochondria (TOMM20)",
4747
}
@@ -197,7 +197,7 @@ def compute_trajectory(
197197
progress(0.25, desc="Reading phase data...")
198198
with open_ome_zarr(data_path, mode="r") as plate:
199199
_, pos = next(plate.positions())
200-
phase_ch = pos.get_channel_index("Phase3D")
200+
phase_ch = pos.get_channel_index("Phase3D")
201201
phase_raw = np.array(pos.data[timepoint, phase_ch])
202202
fluor_raw = np.array(pos.data[timepoint, FLUOR_CH])
203203
norm_meta = _read_norm_meta(pos)
@@ -210,19 +210,15 @@ def compute_trajectory(
210210

211211
z_total = phase_norm.shape[0]
212212
z_start = (z_total - patch_d) // 2
213-
phase_crop = phase_norm[z_start:z_start + patch_d, :patch_h, :patch_w]
213+
phase_crop = phase_norm[z_start : z_start + patch_d, :patch_h, :patch_w]
214214
# Raw phase + experimental fluorescence over the same window, for the
215215
# display panels and the per-step Spectral PCC.
216-
phase_disp = phase_raw[z_start:z_start + patch_d, :patch_h, :patch_w].astype(np.float32)
217-
gt_crop = fluor_raw[z_start:z_start + patch_d, :patch_h, :patch_w].astype(np.float32)
216+
phase_disp = phase_raw[z_start : z_start + patch_d, :patch_h, :patch_w].astype(np.float32)
217+
gt_crop = fluor_raw[z_start : z_start + patch_d, :patch_h, :patch_w].astype(np.float32)
218218

219219
if progress is not None:
220220
progress(0.35, desc=f"Generating {num_steps}-step ODE trajectory...")
221-
phase_tensor = (
222-
torch.from_numpy(phase_crop).float()
223-
.unsqueeze(0).unsqueeze(0)
224-
.to(device)
225-
)
221+
phase_tensor = torch.from_numpy(phase_crop).float().unsqueeze(0).unsqueeze(0).to(device)
226222
with torch.no_grad():
227223
trajectory = model.model.generate_trajectory(phase_tensor, num_steps=num_steps)
228224
traj_np = trajectory[:, 0].cpu().numpy().astype(np.float32) # (num_steps, 1, D, H, W)

applications/dynacell/examples/hf_demo/upload_checkpoints.py

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from pathlib import Path
12+
1213
from huggingface_hub import HfApi, create_repo
1314

1415
REPO_ID = "biohub/dynacell-checkpoints"
@@ -17,47 +18,72 @@
1718

1819
# (hf_filename, local_path)
1920
CHECKPOINTS: list[tuple[str, str]] = [
20-
("celldiff_caax.ckpt",
21-
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
22-
"/a549_mantis/memb/celldiff_r2/checkpoints/last.ckpt"),
23-
("celldiff_h2b.ckpt",
24-
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
25-
"/a549_mantis/nucl/celldiff_r2/checkpoints/last.ckpt"),
26-
("celldiff_sec61b.ckpt",
27-
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
28-
"/a549_mantis/sec61b/celldiff_r2/checkpoints/last.ckpt"),
29-
("celldiff_tomm20.ckpt",
30-
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
31-
"/a549_mantis/tomm20/celldiff_r2/checkpoints/last.ckpt"),
32-
("fnet3d_caax.ckpt",
33-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
34-
"/a549_mantis/memb/fnet3d_paper/checkpoints/epoch=281-step=191760.ckpt"),
35-
("fnet3d_h2b.ckpt",
36-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
37-
"/a549_mantis/nucl/fnet3d_paper/checkpoints/epoch=293-step=199920.ckpt"),
38-
("fnet3d_sec61b.ckpt",
39-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
40-
"/a549_mantis/sec61b/fnet3d_paper/checkpoints/last.ckpt"),
41-
("fnet3d_tomm20.ckpt",
42-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
43-
"/a549_mantis/tomm20/fnet3d_paper/checkpoints/epoch=248-step=126990.ckpt"),
44-
("vscyto3d_caax.ckpt",
45-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
46-
"/a549_mantis/memb/fcmae_vscyto3d_pretrained/checkpoints/last.ckpt"),
47-
("vscyto3d_h2b.ckpt",
48-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
49-
"/a549_mantis/nucl/fcmae_vscyto3d_pretrained/checkpoints/last.ckpt"),
50-
("vscyto3d_sec61b.ckpt",
51-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
52-
"/a549_mantis/sec61b/fcmae_vscyto3d_pretrained_ws8500/checkpoints/last.ckpt"),
53-
("vscyto3d_tomm20.ckpt",
54-
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
55-
"/a549_mantis/tomm20/fcmae_vscyto3d_pretrained_ws8500/checkpoints/last.ckpt"),
21+
(
22+
"celldiff_caax.ckpt",
23+
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
24+
"/a549_mantis/memb/celldiff_r2/checkpoints/last.ckpt",
25+
),
26+
(
27+
"celldiff_h2b.ckpt",
28+
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
29+
"/a549_mantis/nucl/celldiff_r2/checkpoints/last.ckpt",
30+
),
31+
(
32+
"celldiff_sec61b.ckpt",
33+
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
34+
"/a549_mantis/sec61b/celldiff_r2/checkpoints/last.ckpt",
35+
),
36+
(
37+
"celldiff_tomm20.ckpt",
38+
"/hpc/projects/comp.micro/virtual_staining/models/cell_diff_vs_viscy"
39+
"/a549_mantis/tomm20/celldiff_r2/checkpoints/last.ckpt",
40+
),
41+
(
42+
"fnet3d_caax.ckpt",
43+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
44+
"/a549_mantis/memb/fnet3d_paper/checkpoints/epoch=281-step=191760.ckpt",
45+
),
46+
(
47+
"fnet3d_h2b.ckpt",
48+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
49+
"/a549_mantis/nucl/fnet3d_paper/checkpoints/epoch=293-step=199920.ckpt",
50+
),
51+
(
52+
"fnet3d_sec61b.ckpt",
53+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
54+
"/a549_mantis/sec61b/fnet3d_paper/checkpoints/last.ckpt",
55+
),
56+
(
57+
"fnet3d_tomm20.ckpt",
58+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
59+
"/a549_mantis/tomm20/fnet3d_paper/checkpoints/epoch=248-step=126990.ckpt",
60+
),
61+
(
62+
"vscyto3d_caax.ckpt",
63+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
64+
"/a549_mantis/memb/fcmae_vscyto3d_pretrained/checkpoints/last.ckpt",
65+
),
66+
(
67+
"vscyto3d_h2b.ckpt",
68+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
69+
"/a549_mantis/nucl/fcmae_vscyto3d_pretrained/checkpoints/last.ckpt",
70+
),
71+
(
72+
"vscyto3d_sec61b.ckpt",
73+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
74+
"/a549_mantis/sec61b/fcmae_vscyto3d_pretrained_ws8500/checkpoints/last.ckpt",
75+
),
76+
(
77+
"vscyto3d_tomm20.ckpt",
78+
"/hpc/projects/comp.micro/virtual_staining/models/dynacell"
79+
"/a549_mantis/tomm20/fcmae_vscyto3d_pretrained_ws8500/checkpoints/last.ckpt",
80+
),
5681
]
5782

5883

5984
def main() -> None:
6085
import os
86+
6187
token = os.environ.get("HF_TOKEN")
6288
api = HfApi(token=token)
6389

applications/dynacell/examples/hf_demo/upload_hf_space.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import os
1111
from pathlib import Path
12+
1213
from huggingface_hub import HfApi
1314

1415
SPACE_REPO = "biohub/dynacell"

0 commit comments

Comments
 (0)