Skip to content

Commit f188b30

Browse files
Copilotakhanf
andauthored
Address review: improve subprocess security and add retry logging
Agent-Logs-Url: https://github.com/khanlab/SPIMquant/sessions/e025f01c-8e6d-495f-b403-90c8c2aa6710 Co-authored-by: akhanf <11492701+akhanf@users.noreply.github.com>
1 parent b2da92b commit f188b30

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

spimquant/workflow/scripts/atropos_seg.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,36 @@
2323

2424
os.makedirs(posteriors_dir, exist_ok=True)
2525

26+
env = os.environ.copy()
27+
env["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str(threads)
28+
2629
for k in range(init_k, min_k - 1, -1):
27-
cmd = (
28-
f"ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads} "
29-
f"Atropos -v -d 3 --initialization KMeans[{k}] "
30-
f" --intensity-image {downsampled} "
31-
f" --output [{dseg},{posteriors_dir}/class-%02d.nii] "
32-
f" --mask-image {mask} --mrf [{mrf_smoothing},{mrf_radius}]"
33-
)
34-
result = subprocess.run(cmd, shell=True)
30+
cmd = [
31+
"Atropos",
32+
"-v",
33+
"-d",
34+
"3",
35+
"--initialization",
36+
f"KMeans[{k}]",
37+
"--intensity-image",
38+
downsampled,
39+
"--output",
40+
f"[{dseg},{posteriors_dir}/class-%02d.nii]",
41+
"--mask-image",
42+
mask,
43+
"--mrf",
44+
f"[{mrf_smoothing},{mrf_radius}]",
45+
]
46+
result = subprocess.run(cmd, env=env)
3547
if result.returncode == 0:
3648
break
3749
elif k == min_k:
3850
raise subprocess.CalledProcessError(result.returncode, cmd)
3951
else:
52+
print(
53+
f"Atropos failed with k={k} (exit code {result.returncode}), "
54+
f"retrying with k={k - 1}"
55+
)
4056
# Clean up and retry with lower k
4157
if os.path.exists(dseg):
4258
os.remove(dseg)

0 commit comments

Comments
 (0)