-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
208 lines (180 loc) · 6.96 KB
/
Copy pathtasks.py
File metadata and controls
208 lines (180 loc) · 6.96 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
"""Small task runner that wraps the most common workflows.
Examples:
python tasks.py test
python tasks.py list-profiles
python tasks.py segment Tv143
python tasks.py proposals Tv143
python tasks.py annotate Tv143
python tasks.py prepare-dataset Tv143
python tasks.py refine Tv143 --run-biomass
Run with no args to see the full subcommand list. All paths default to the
conventional layout: data in `data/samples`, outputs in `outputs/<stage>`.
Override anything by passing flags after the subcommand — they are forwarded
to the underlying CLI verbatim.
"""
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
from typing import Sequence
DATA_ROOT = "data/samples"
OUT_SEGMENT = "outputs/segment" # param-tuning playground for full-image segmentation
OUT_ANNOT = "outputs/annot" # human-in-the-loop pipeline root: proposals, manifests, classifier splits, refined masks
DEFAULT_PROFILE_SEGMENT = "default"
DEFAULT_PROFILE_PROPOSAL = "proposal_high_recall"
def _run(cmd: Sequence[str]) -> int:
printable = " ".join(cmd)
print(f"$ {printable}")
return subprocess.call([sys.executable, *cmd])
def _segment_cmd(sample: str, extra: list[str]) -> list[str]:
return [
"-m", "src.cli",
"--mode", "segment",
"--profile", DEFAULT_PROFILE_SEGMENT,
"--data-root", DATA_ROOT,
"--sample", sample,
"--output-dir", OUT_SEGMENT,
*extra,
]
def _proposals_cmd(sample: str, extra: list[str]) -> list[str]:
return [
"-m", "src.cli",
"--mode", "proposals",
"--profile", DEFAULT_PROFILE_PROPOSAL,
"--data-root", DATA_ROOT,
"--sample", sample,
"--output-dir", OUT_ANNOT,
*extra,
]
def _annotate_cmd(sample: str | None, extra: list[str]) -> list[str]:
if sample is None:
return ["-m", "src.annotation_cli", "--root", OUT_ANNOT, *extra]
manifest = Path(OUT_ANNOT) / sample / "annotation" / "annotation_manifest.csv"
return ["-m", "src.annotation_cli", "--manifest", str(manifest), *extra]
def _prepare_dataset_cmd(sample: str, extra: list[str]) -> list[str]:
manifest = Path(OUT_ANNOT) / sample / "annotation" / "annotation_manifest.csv"
return [
"-m", "src.cli",
"--mode", "prepare-dataset",
"--profile", DEFAULT_PROFILE_PROPOSAL,
"--manifest", str(manifest),
"--output-dir", OUT_ANNOT,
*extra,
]
def _refine_cmd(sample: str, extra: list[str]) -> list[str]:
manifest = Path(OUT_ANNOT) / sample / "annotation" / "annotation_manifest.csv"
return [
"-m", "src.cli",
"--mode", "refine",
"--profile", DEFAULT_PROFILE_PROPOSAL,
"--manifest", str(manifest),
"--output-dir", OUT_ANNOT,
*extra,
]
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(prog="tasks.py", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = parser.add_subparsers(dest="task", required=True)
sub.add_parser("test", help="Run the test suite via pytest.")
sub.add_parser("list-profiles", help="List bundled segmentation profiles.")
sub.add_parser(
"cellpose-export",
help="Pack human-edited crops + masks into a Cellpose training bundle.",
)
sub.add_parser(
"cellpose-eval",
help="Score a trained Cellpose model on annotated crops, write visualizations.",
)
sub.add_parser(
"cellpose-reconstruct",
help="Rebuild full-image instance masks from per-crop annotations (seeds for Cellpose GUI).",
)
sub.add_parser(
"cellpose-pack",
help="Package Cellpose-GUI-annotated full images into a train/val bundle.",
)
sub.add_parser(
"overlay",
help="Overlay instance-mask outlines on the original image for visual QC.",
)
sub.add_parser(
"cellpose-sweep",
help="Sweep cellprob_threshold for a model; count cells + write overlays per threshold.",
)
sub.add_parser(
"cellpose-biomass",
help="Compute biovolume from Cellpose instance-label masks.",
)
def _add_sample_cmd(name: str, help_: str, required: bool = True) -> argparse.ArgumentParser:
p = sub.add_parser(name, help=help_)
if required:
p.add_argument("sample", help="Sample stem, e.g. Tv143.")
else:
p.add_argument("sample", nargs="?", default=None, help="Sample stem; omit to scan the whole proposals root.")
return p
_add_sample_cmd("segment", "Run full-image segmentation on a sample.")
_add_sample_cmd("proposals", "Run proposal generation + crop export on a sample.")
_add_sample_cmd("annotate", "Open the annotation tool on a sample's manifest.", required=False)
_add_sample_cmd("prepare-dataset", "Prepare the classifier dataset from a sample's manifest.")
_add_sample_cmd("refine", "Refine positively-labeled crops for a sample.")
args, extra = parser.parse_known_args(argv)
if args.task == "test":
return subprocess.call([sys.executable, "-m", "pytest", *extra])
if args.task == "list-profiles":
return _run(["-m", "src.cli", "--list-profiles", *extra])
if args.task == "cellpose-export":
return _run([
"-m", "src.cellpose_export",
"--annot-root", OUT_ANNOT,
"--out-dir", "outputs/cellpose_training",
*extra,
])
if args.task == "cellpose-eval":
return _run([
"-m", "src.cellpose_eval",
*extra,
])
if args.task == "cellpose-reconstruct":
return _run([
"-m", "src.cellpose_reconstruct",
"--annot-root", OUT_ANNOT,
"--samples-root", DATA_ROOT,
"--out-dir", "outputs/cellpose_fullimage_seed",
*extra,
])
if args.task == "cellpose-pack":
return _run([
"-m", "src.cellpose_pack",
"--seed-dir", "outputs/cellpose_fullimage_seed",
"--out-dir", "outputs/cellpose_fullimage_train",
*extra,
])
if args.task == "overlay":
return _run([
"-m", "src.mask_overlay",
*extra,
])
if args.task == "cellpose-sweep":
return _run([
"-m", "src.cellpose_sweep",
*extra,
])
if args.task == "cellpose-biomass":
return _run([
"-m", "src.cellpose_biomass",
*extra,
])
if args.task == "segment":
return _run(_segment_cmd(args.sample, extra))
if args.task == "proposals":
return _run(_proposals_cmd(args.sample, extra))
if args.task == "annotate":
return _run(_annotate_cmd(args.sample, extra))
if args.task == "prepare-dataset":
return _run(_prepare_dataset_cmd(args.sample, extra))
if args.task == "refine":
return _run(_refine_cmd(args.sample, extra))
parser.error(f"Unknown task: {args.task}")
return 2
if __name__ == "__main__":
raise SystemExit(main())