datapipe-neuro 0.1.0__py3-none-any.whl
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.
- datapipe/__init__.py +38 -0
- datapipe/cli.py +459 -0
- datapipe/compress.py +75 -0
- datapipe/models/__init__.py +19 -0
- datapipe/models/serialsection.py +164 -0
- datapipe/place.py +331 -0
- datapipe/place_session.py +217 -0
- datapipe/register.py +225 -0
- datapipe/segment.py +126 -0
- datapipe/splits.py +296 -0
- datapipe_neuro-0.1.0.dist-info/METADATA +105 -0
- datapipe_neuro-0.1.0.dist-info/RECORD +15 -0
- datapipe_neuro-0.1.0.dist-info/WHEEL +4 -0
- datapipe_neuro-0.1.0.dist-info/entry_points.txt +2 -0
- datapipe_neuro-0.1.0.dist-info/licenses/LICENSE +32 -0
datapipe/__init__.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""datapipe — dataset-aware processing & moving pipeline for lab acquisition data.
|
|
2
|
+
|
|
3
|
+
Home of the serial-section dataset model/loader and the processing stages
|
|
4
|
+
(registration now; segmentation / compress-handoff / namespace-finalize to come).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from datapipe.models.serialsection import (
|
|
10
|
+
SerialSectionSample,
|
|
11
|
+
find_samples,
|
|
12
|
+
load_sample,
|
|
13
|
+
)
|
|
14
|
+
from datapipe.register import (
|
|
15
|
+
RegistrationPlan,
|
|
16
|
+
RegistrationResult,
|
|
17
|
+
plan_registration,
|
|
18
|
+
register_sample,
|
|
19
|
+
run_registration,
|
|
20
|
+
)
|
|
21
|
+
from datapipe.compress import compress_commands, resolve_datapack
|
|
22
|
+
from datapipe.segment import launch_segment, registration_dir, resolve_registration
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"SerialSectionSample",
|
|
26
|
+
"find_samples",
|
|
27
|
+
"load_sample",
|
|
28
|
+
"RegistrationPlan",
|
|
29
|
+
"RegistrationResult",
|
|
30
|
+
"plan_registration",
|
|
31
|
+
"register_sample",
|
|
32
|
+
"run_registration",
|
|
33
|
+
"launch_segment",
|
|
34
|
+
"registration_dir",
|
|
35
|
+
"resolve_registration",
|
|
36
|
+
"compress_commands",
|
|
37
|
+
"resolve_datapack",
|
|
38
|
+
]
|
datapipe/cli.py
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
"""datapipe CLI. First stage: ``datapipe register`` — one sample per invocation.
|
|
2
|
+
|
|
3
|
+
Per-sample by design: no top-level crawler. Each call registers exactly one
|
|
4
|
+
serial_section sample, so chaining/parallelism is done by the caller (a shell
|
|
5
|
+
loop, GNU parallel, or a SLURM array — one task per sample).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import shlex
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from datapipe.compress import CompressError, compress_commands, resolve_datapack
|
|
18
|
+
from datapipe.models.serialsection import (
|
|
19
|
+
SerialSectionError,
|
|
20
|
+
is_sample,
|
|
21
|
+
load_sample,
|
|
22
|
+
)
|
|
23
|
+
from datapipe.place import (
|
|
24
|
+
PlacementError,
|
|
25
|
+
append_step,
|
|
26
|
+
datapack_command,
|
|
27
|
+
placement_record,
|
|
28
|
+
plan_placement,
|
|
29
|
+
write_manifests,
|
|
30
|
+
)
|
|
31
|
+
from datapipe.place_session import (
|
|
32
|
+
looks_like_session,
|
|
33
|
+
plan_session_placement,
|
|
34
|
+
)
|
|
35
|
+
from datapipe.register import (
|
|
36
|
+
DEFAULT_ATLAS,
|
|
37
|
+
DEFAULT_CHANNEL,
|
|
38
|
+
DEFAULT_ORIENTATION,
|
|
39
|
+
RegistrationError,
|
|
40
|
+
output_state,
|
|
41
|
+
plan_registration,
|
|
42
|
+
resolve_brainreg,
|
|
43
|
+
run_registration,
|
|
44
|
+
)
|
|
45
|
+
from datapipe.segment import SegmentError, launch_segment
|
|
46
|
+
from datapipe.splits import SplitsError, as_json, find_splits, render
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
50
|
+
parser = argparse.ArgumentParser(
|
|
51
|
+
prog="datapipe", description="Dataset-aware processing & moving pipeline."
|
|
52
|
+
)
|
|
53
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
54
|
+
|
|
55
|
+
r = sub.add_parser(
|
|
56
|
+
"register",
|
|
57
|
+
help="brainreg-register ONE serial_section sample (output beside stitched dirs)",
|
|
58
|
+
)
|
|
59
|
+
r.add_argument("sample", type=Path, help="a single serial_section sample directory")
|
|
60
|
+
r.add_argument(
|
|
61
|
+
"--channel", default=DEFAULT_CHANNEL, help="autofluo/registration channel"
|
|
62
|
+
)
|
|
63
|
+
r.add_argument("--atlas", default=DEFAULT_ATLAS)
|
|
64
|
+
r.add_argument("--orientation", default=DEFAULT_ORIENTATION)
|
|
65
|
+
r.add_argument(
|
|
66
|
+
"--source",
|
|
67
|
+
choices=["full", "downsampled"],
|
|
68
|
+
default="full",
|
|
69
|
+
help="full = stitched channel + recipe voxel; downsampled = ds stack at --res",
|
|
70
|
+
)
|
|
71
|
+
r.add_argument("--res", type=int, default=25, help="µm, when --source downsampled")
|
|
72
|
+
r.add_argument(
|
|
73
|
+
"--main-only",
|
|
74
|
+
action="store_true",
|
|
75
|
+
help="register ONLY the main channel (default: all stitched channels, so every "
|
|
76
|
+
"channel transforms into atlas space and auto-loads together)",
|
|
77
|
+
)
|
|
78
|
+
r.add_argument(
|
|
79
|
+
"--additional",
|
|
80
|
+
nargs="*",
|
|
81
|
+
metavar="CH",
|
|
82
|
+
default=None,
|
|
83
|
+
help="specific extra channel numbers to register (-a); default = all other channels",
|
|
84
|
+
)
|
|
85
|
+
r.add_argument(
|
|
86
|
+
"--analysis-root",
|
|
87
|
+
type=Path,
|
|
88
|
+
help="write derived output to <analysis_root>/<sample>/registration (mirrors the "
|
|
89
|
+
"raw tree so RO-raw + RW-analysis overlay cleanly); default = in-place beside the data",
|
|
90
|
+
)
|
|
91
|
+
r.add_argument(
|
|
92
|
+
"--output",
|
|
93
|
+
type=Path,
|
|
94
|
+
help="write the registration to THIS directory, for a tree that is not "
|
|
95
|
+
"<root>/<sample>/registration (e.g. the namespace's "
|
|
96
|
+
"derivative/<subject>/<session>__histology/<session>__brainreg). "
|
|
97
|
+
"Takes precedence over --analysis-root.",
|
|
98
|
+
)
|
|
99
|
+
r.add_argument("--brainreg", help="path to brainreg (or $BRAINREG / PATH)")
|
|
100
|
+
r.add_argument(
|
|
101
|
+
"--brainreg-extra",
|
|
102
|
+
default="",
|
|
103
|
+
help="extra args forwarded verbatim to brainreg, as a quoted string "
|
|
104
|
+
"(e.g. --brainreg-extra '--n-free-cpus 8' to cap RAM/CPU)",
|
|
105
|
+
)
|
|
106
|
+
r.add_argument(
|
|
107
|
+
"--overwrite", action="store_true", help="re-run even if registration/ exists"
|
|
108
|
+
)
|
|
109
|
+
r.add_argument(
|
|
110
|
+
"--dry-run", action="store_true", help="print the brainreg command only"
|
|
111
|
+
)
|
|
112
|
+
r.set_defaults(func=_cmd_register)
|
|
113
|
+
|
|
114
|
+
g = sub.add_parser(
|
|
115
|
+
"segment",
|
|
116
|
+
help="open napari + brainglobe-segmentation preloaded on ONE sample's registration",
|
|
117
|
+
)
|
|
118
|
+
g.add_argument(
|
|
119
|
+
"sample",
|
|
120
|
+
type=Path,
|
|
121
|
+
help="a registration dir (contains brainreg.json) OR a sample dir (+ --analysis-root)",
|
|
122
|
+
)
|
|
123
|
+
g.add_argument(
|
|
124
|
+
"--analysis-root",
|
|
125
|
+
type=Path,
|
|
126
|
+
help="analysis ROOT (not the per-sample path); resolves "
|
|
127
|
+
"<analysis_root>/<sample>/registration. Ignored if the positional is already a "
|
|
128
|
+
"registration dir. Default: in-place <sample>/registration",
|
|
129
|
+
)
|
|
130
|
+
g.add_argument(
|
|
131
|
+
"--sample-space",
|
|
132
|
+
action="store_true",
|
|
133
|
+
help="segment in sample space (default: atlas space — trace in the reference "
|
|
134
|
+
"frame so tracks need no sample→atlas transform)",
|
|
135
|
+
)
|
|
136
|
+
g.add_argument(
|
|
137
|
+
"--napari-python",
|
|
138
|
+
help="python with napari + brainglobe-segmentation (or $DATAPIPE_NAPARI_PYTHON)",
|
|
139
|
+
)
|
|
140
|
+
g.add_argument(
|
|
141
|
+
"--dry-run", action="store_true", help="print the launch command only"
|
|
142
|
+
)
|
|
143
|
+
g.set_defaults(func=_cmd_segment)
|
|
144
|
+
|
|
145
|
+
c = sub.add_parser(
|
|
146
|
+
"compress",
|
|
147
|
+
help="compress ONE sample's data via datapack (raw + analysis trees) -> dest/<sample>",
|
|
148
|
+
)
|
|
149
|
+
c.add_argument("sample", type=Path, help="a serial_section sample directory (raw)")
|
|
150
|
+
c.add_argument("dest", type=Path, help="destination root (output -> dest/<sample>)")
|
|
151
|
+
c.add_argument(
|
|
152
|
+
"--analysis-root",
|
|
153
|
+
type=Path,
|
|
154
|
+
help="also compress <analysis_root>/<sample> (registration/segmentation)",
|
|
155
|
+
)
|
|
156
|
+
c.add_argument(
|
|
157
|
+
"--archive-raw",
|
|
158
|
+
action="store_true",
|
|
159
|
+
help="bundle raw stitched/downsampled dirs to zips (default: in-place recompress)",
|
|
160
|
+
)
|
|
161
|
+
c.add_argument("--codec", choices=["deflate", "zstd"], default="deflate")
|
|
162
|
+
c.add_argument("--datapack", help="path to the datapack CLI (or $DATAPACK / PATH)")
|
|
163
|
+
c.add_argument(
|
|
164
|
+
"--dry-run", action="store_true", help="print the datapack commands only"
|
|
165
|
+
)
|
|
166
|
+
c.set_defaults(func=_cmd_compress)
|
|
167
|
+
|
|
168
|
+
pl = sub.add_parser(
|
|
169
|
+
"place",
|
|
170
|
+
help="file ONE scan or acquisition session into a data tree, refusing on identity mismatch",
|
|
171
|
+
description=(
|
|
172
|
+
"Takes either a serial_section sample or an acquisition session, and routes on what the "
|
|
173
|
+
"directory IS rather than on a flag you have to remember. A scan is repacked into the "
|
|
174
|
+
"tree; a session is moved, because compression is stage 2 and has already run by the "
|
|
175
|
+
"time anything is placed.\n\n"
|
|
176
|
+
"The subject is checked against the directory's own record and a mismatch is refused, "
|
|
177
|
+
"not warned about. For an acquisition session that record is the full prefix -- "
|
|
178
|
+
"`subj042_m1000001_001`, not its middle field."
|
|
179
|
+
),
|
|
180
|
+
)
|
|
181
|
+
pl.add_argument(
|
|
182
|
+
"sample",
|
|
183
|
+
type=Path,
|
|
184
|
+
help="a serial_section sample directory, OR an acquisition session directory",
|
|
185
|
+
)
|
|
186
|
+
pl.add_argument(
|
|
187
|
+
"subject",
|
|
188
|
+
help="subject to file it under; checked against the scan's or session's own record",
|
|
189
|
+
)
|
|
190
|
+
pl.add_argument(
|
|
191
|
+
"data_root", type=Path, help="data tree root (<root>/<subject>/... is created)"
|
|
192
|
+
)
|
|
193
|
+
pl.add_argument(
|
|
194
|
+
"--roster",
|
|
195
|
+
type=Path,
|
|
196
|
+
help="LEGACY only: subject map relating a pre-migration rig name to a namespace id. "
|
|
197
|
+
"Current scans name their own subject and need none.",
|
|
198
|
+
)
|
|
199
|
+
pl.add_argument("--codec", choices=["deflate", "zstd"], default="deflate")
|
|
200
|
+
pl.add_argument("--datapack", help="path to the datapack CLI (or $DATAPACK / PATH)")
|
|
201
|
+
pl.add_argument(
|
|
202
|
+
"--dry-run",
|
|
203
|
+
action="store_true",
|
|
204
|
+
help="check identity and print the destination and what would move; write nothing",
|
|
205
|
+
)
|
|
206
|
+
pl.set_defaults(func=_cmd_place)
|
|
207
|
+
|
|
208
|
+
sp = sub.add_parser(
|
|
209
|
+
"splits",
|
|
210
|
+
help="find acquisitions the recorder broke in two, and tell them from false starts",
|
|
211
|
+
description=(
|
|
212
|
+
"Reports two classes and acts on neither. A SPLIT is one recording in two parts, worth "
|
|
213
|
+
"joining; a STUB is a recording that died seconds in and was restarted, which must be "
|
|
214
|
+
"excluded rather than appended to anything. Most close pairs are stubs, so both are "
|
|
215
|
+
"always printed."
|
|
216
|
+
),
|
|
217
|
+
)
|
|
218
|
+
sp.add_argument(
|
|
219
|
+
"root", type=Path, help="a data tree: <root>/<subject>/<session>/<acquisition>"
|
|
220
|
+
)
|
|
221
|
+
sp.add_argument(
|
|
222
|
+
"--suffix",
|
|
223
|
+
default="pxi",
|
|
224
|
+
help="acquisition directory suffix to scan (default: pxi)",
|
|
225
|
+
)
|
|
226
|
+
sp.add_argument(
|
|
227
|
+
"--fs",
|
|
228
|
+
type=float,
|
|
229
|
+
default=30000.0,
|
|
230
|
+
help="sample rate used to turn samples into seconds",
|
|
231
|
+
)
|
|
232
|
+
sp.add_argument(
|
|
233
|
+
"--stream",
|
|
234
|
+
default="",
|
|
235
|
+
help="only measure sidecars whose path contains this, e.g. a probe stream name; "
|
|
236
|
+
"default takes the longest stream in the acquisition",
|
|
237
|
+
)
|
|
238
|
+
sp.add_argument(
|
|
239
|
+
"--min-half-minutes",
|
|
240
|
+
type=float,
|
|
241
|
+
default=15.0,
|
|
242
|
+
help="a pair whose shorter side is under this is a stub, not a split (default: 15)",
|
|
243
|
+
)
|
|
244
|
+
sp.add_argument(
|
|
245
|
+
"--json", action="store_true", help="emit JSON instead of the report"
|
|
246
|
+
)
|
|
247
|
+
sp.set_defaults(func=_cmd_splits)
|
|
248
|
+
return parser
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _cmd_place_session(args: argparse.Namespace) -> int:
|
|
252
|
+
"""File an acquisition session. Same contract as a scan: check, then file, never skip.
|
|
253
|
+
|
|
254
|
+
A session is moved rather than repacked -- compression is stage 2 and has already run by the
|
|
255
|
+
time anything is placed, so doing it again here would recompress an artifact that was verified
|
|
256
|
+
once already.
|
|
257
|
+
"""
|
|
258
|
+
try:
|
|
259
|
+
plan = plan_session_placement(
|
|
260
|
+
args.sample, args.subject, args.data_root, roster=args.roster
|
|
261
|
+
)
|
|
262
|
+
except PlacementError as exc:
|
|
263
|
+
print(f"datapipe place: {exc}", file=sys.stderr)
|
|
264
|
+
return 2
|
|
265
|
+
|
|
266
|
+
ident = plan.identity
|
|
267
|
+
print(f" session : {ident.session_name or plan.source.name}")
|
|
268
|
+
print(f" recorded as : {sorted(ident.recorded_names)}")
|
|
269
|
+
print(f" acquisitions : {ident.n_acquisitions}")
|
|
270
|
+
print(f" destination : {plan.session_dir}")
|
|
271
|
+
|
|
272
|
+
if args.dry_run:
|
|
273
|
+
print(f" would move : {plan.source} -> {plan.session_dir}")
|
|
274
|
+
return 0
|
|
275
|
+
|
|
276
|
+
if plan.session_dir.exists():
|
|
277
|
+
print(
|
|
278
|
+
f"datapipe place: {plan.session_dir} already exists; refusing to merge two sessions "
|
|
279
|
+
f"into one directory",
|
|
280
|
+
file=sys.stderr,
|
|
281
|
+
)
|
|
282
|
+
return 2
|
|
283
|
+
plan.session_dir.parent.mkdir(parents=True, exist_ok=True)
|
|
284
|
+
shutil.move(str(plan.source), str(plan.session_dir))
|
|
285
|
+
print(f" placed : {plan.session_dir}")
|
|
286
|
+
return 0
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _cmd_place(args: argparse.Namespace) -> int:
|
|
290
|
+
"""Check, then file. The check is not a separate step that can be skipped."""
|
|
291
|
+
# Route on what the directory is rather than on a flag the caller has to remember: a session
|
|
292
|
+
# and a scan are both "place this under that subject", and the identity records differ.
|
|
293
|
+
if looks_like_session(args.sample):
|
|
294
|
+
return _cmd_place_session(args)
|
|
295
|
+
try:
|
|
296
|
+
plan = plan_placement(
|
|
297
|
+
args.sample, args.subject, args.data_root, roster=args.roster
|
|
298
|
+
)
|
|
299
|
+
except (PlacementError, SerialSectionError) as exc:
|
|
300
|
+
print(f"datapipe place: {exc}", file=sys.stderr)
|
|
301
|
+
return 2
|
|
302
|
+
|
|
303
|
+
print(f" scan identity : {sorted(plan.identity.names)}")
|
|
304
|
+
print(f" datetime : {plan.datetime_str} (from {plan.datetime_source})")
|
|
305
|
+
print(f" destination : {plan.acq_dir}")
|
|
306
|
+
|
|
307
|
+
try:
|
|
308
|
+
datapack = resolve_datapack(args.datapack)
|
|
309
|
+
except CompressError as exc:
|
|
310
|
+
print(f"datapipe place: {exc}", file=sys.stderr)
|
|
311
|
+
return 2
|
|
312
|
+
cmd = datapack_command(plan, datapack, codec=args.codec)
|
|
313
|
+
|
|
314
|
+
if args.dry_run:
|
|
315
|
+
print(f" would run : {shlex.join(cmd)}")
|
|
316
|
+
return 0
|
|
317
|
+
|
|
318
|
+
write_manifests(plan)
|
|
319
|
+
rc = subprocess.call(cmd)
|
|
320
|
+
if rc != 0:
|
|
321
|
+
print(
|
|
322
|
+
f"datapipe place: datapack exited {rc}; manifests written, data not packed",
|
|
323
|
+
file=sys.stderr,
|
|
324
|
+
)
|
|
325
|
+
return rc
|
|
326
|
+
append_step(plan.acq_dir, placement_record(plan))
|
|
327
|
+
print(f" placed : {plan.acq_dir}")
|
|
328
|
+
return 0
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _cmd_compress(args: argparse.Namespace) -> int:
|
|
332
|
+
datapack = "datapack" if args.dry_run else resolve_datapack(args.datapack)
|
|
333
|
+
cmds = compress_commands(
|
|
334
|
+
datapack,
|
|
335
|
+
args.sample,
|
|
336
|
+
args.dest,
|
|
337
|
+
analysis_root=args.analysis_root,
|
|
338
|
+
archive_raw=args.archive_raw,
|
|
339
|
+
codec=args.codec,
|
|
340
|
+
)
|
|
341
|
+
for label, cmd in cmds:
|
|
342
|
+
print(f"[{label}] {shlex.join(cmd)}")
|
|
343
|
+
if args.dry_run:
|
|
344
|
+
return 0
|
|
345
|
+
for label, cmd in cmds:
|
|
346
|
+
if subprocess.run(cmd).returncode != 0:
|
|
347
|
+
print(f"FAIL: {label} compress failed", file=sys.stderr)
|
|
348
|
+
return 1
|
|
349
|
+
print(f"ok: {args.sample.name} -> {args.dest / args.sample.name}")
|
|
350
|
+
return 0
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _cmd_segment(args: argparse.Namespace) -> int:
|
|
354
|
+
cmd = launch_segment(
|
|
355
|
+
args.sample,
|
|
356
|
+
analysis_root=args.analysis_root,
|
|
357
|
+
atlas_space=not args.sample_space,
|
|
358
|
+
napari_python=args.napari_python,
|
|
359
|
+
dry_run=args.dry_run,
|
|
360
|
+
)
|
|
361
|
+
if args.dry_run:
|
|
362
|
+
print(shlex.join(cmd[:2]) + " <launch-script> " + shlex.join(cmd[3:]))
|
|
363
|
+
return 0
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def _cmd_register(args: argparse.Namespace) -> int:
|
|
367
|
+
if not is_sample(args.sample):
|
|
368
|
+
print(
|
|
369
|
+
f"error: {args.sample} is not a serial_section sample "
|
|
370
|
+
"(need recipe_*.yml + stitchedImages_*/)",
|
|
371
|
+
file=sys.stderr,
|
|
372
|
+
)
|
|
373
|
+
return 2
|
|
374
|
+
|
|
375
|
+
sample = load_sample(args.sample)
|
|
376
|
+
if args.additional is not None:
|
|
377
|
+
additional = tuple(args.additional)
|
|
378
|
+
elif args.main_only:
|
|
379
|
+
additional = ()
|
|
380
|
+
else: # default: register every other stitched channel too
|
|
381
|
+
additional = tuple(c for c in sorted(sample.channels) if c != args.channel)
|
|
382
|
+
try:
|
|
383
|
+
plan = plan_registration(
|
|
384
|
+
sample,
|
|
385
|
+
channel=args.channel,
|
|
386
|
+
atlas=args.atlas,
|
|
387
|
+
orientation=args.orientation,
|
|
388
|
+
source=args.source,
|
|
389
|
+
res_micron=args.res,
|
|
390
|
+
analysis_root=args.analysis_root,
|
|
391
|
+
output_dir=args.output,
|
|
392
|
+
extra_args=tuple(shlex.split(args.brainreg_extra)),
|
|
393
|
+
additional_channels=additional,
|
|
394
|
+
)
|
|
395
|
+
except (RegistrationError, SerialSectionError) as exc:
|
|
396
|
+
print(f"error: {sample.path.name}: {exc}", file=sys.stderr)
|
|
397
|
+
return 1
|
|
398
|
+
|
|
399
|
+
brainreg = "brainreg" if args.dry_run else resolve_brainreg(args.brainreg)
|
|
400
|
+
# The point of the wrapper: show the exact args handed to brainreg.
|
|
401
|
+
print(shlex.join(plan.cmd(brainreg)))
|
|
402
|
+
if args.dry_run:
|
|
403
|
+
return 0
|
|
404
|
+
|
|
405
|
+
if (
|
|
406
|
+
plan.output_dir.exists()
|
|
407
|
+
and any(plan.output_dir.iterdir())
|
|
408
|
+
and not args.overwrite
|
|
409
|
+
):
|
|
410
|
+
state, missing = output_state(plan.output_dir)
|
|
411
|
+
if state == "complete":
|
|
412
|
+
# idempotent: re-running a cohort must not fail on the samples already registered
|
|
413
|
+
print(f"skip: {plan.output_dir} is already a complete registration")
|
|
414
|
+
return 0
|
|
415
|
+
print(
|
|
416
|
+
f"REFUSE: {plan.output_dir} holds a PARTIAL registration, missing "
|
|
417
|
+
f"{', '.join(missing)}. A skip here would report a registration that never ran as a "
|
|
418
|
+
f"success. Clear the directory and re-run, or pass --overwrite.",
|
|
419
|
+
file=sys.stderr,
|
|
420
|
+
)
|
|
421
|
+
return 3
|
|
422
|
+
|
|
423
|
+
rc = run_registration(plan, brainreg=brainreg)
|
|
424
|
+
if rc != 0:
|
|
425
|
+
print(f"FAIL: {sample.path.name}: brainreg exited {rc}", file=sys.stderr)
|
|
426
|
+
return 1
|
|
427
|
+
print(f"ok: {sample.path.name} -> {plan.output_dir}")
|
|
428
|
+
return 0
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def _cmd_splits(a: argparse.Namespace) -> int:
|
|
432
|
+
try:
|
|
433
|
+
report = find_splits(
|
|
434
|
+
a.root,
|
|
435
|
+
suffix=a.suffix,
|
|
436
|
+
fs=a.fs,
|
|
437
|
+
stream_hint=a.stream,
|
|
438
|
+
min_half_s=a.min_half_minutes * 60.0,
|
|
439
|
+
)
|
|
440
|
+
except SplitsError as exc:
|
|
441
|
+
print(f"datapipe splits: {exc}", file=sys.stderr)
|
|
442
|
+
return 2
|
|
443
|
+
print(as_json(report) if a.json else render(report))
|
|
444
|
+
# Exit 0 either way: finding splits is the tool working, not a failure. A caller that wants to
|
|
445
|
+
# gate on it should read the JSON.
|
|
446
|
+
return 0
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def main(argv: list[str] | None = None) -> int:
|
|
450
|
+
args = _build_parser().parse_args(argv)
|
|
451
|
+
try:
|
|
452
|
+
return int(args.func(args))
|
|
453
|
+
except (RegistrationError, SerialSectionError, SegmentError, CompressError) as exc:
|
|
454
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
455
|
+
return 1
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
if __name__ == "__main__": # pragma: no cover
|
|
459
|
+
raise SystemExit(main())
|
datapipe/compress.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Compress stage — delegate to the datapack CLI (no datapack import).
|
|
2
|
+
|
|
3
|
+
datapack owns all compression; this stage just invokes its dataset-aware drivers
|
|
4
|
+
per sample (consistent with how datapipe shells out to brainreg/napari from their
|
|
5
|
+
own envs, keeping datapipe's env lean). For one sample it compresses:
|
|
6
|
+
|
|
7
|
+
- the **raw acquisition** tree (``stitchedImages_*``/``downsampled_stacks`` + metadata)
|
|
8
|
+
via ``datapack serial-section`` — in-place lossless recompress by default
|
|
9
|
+
(``--archive-raw`` to bundle the raw dirs into zips instead);
|
|
10
|
+
- the **analysis** tree (``registration/`` + ``manual_segmentation/``), if present,
|
|
11
|
+
via ``datapack histology tree`` — in-place lossless recompress, browsable, with
|
|
12
|
+
``.npy`` tracks / JSON copied verbatim.
|
|
13
|
+
|
|
14
|
+
Both write to ``<dest>/<sample>/…`` so the compressed raw + derived land in one tree
|
|
15
|
+
(ready for the finalize stage). The source is only read.
|
|
16
|
+
|
|
17
|
+
Resolve the datapack CLI via ``--datapack`` / ``$DATAPACK`` / PATH / the datapack venv.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import os
|
|
23
|
+
import shutil
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
#: Last resort after an explicit path, $DATAPACK and PATH have all come up empty. A bare name,
|
|
27
|
+
#: so the failure reads as "datapack not found" rather than naming one machine's virtualenv.
|
|
28
|
+
DEFAULT_DATAPACK = "datapack"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CompressError(RuntimeError):
|
|
32
|
+
"""Raised when datapack can't be resolved or a compress sub-step fails."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def resolve_datapack(explicit: str | None = None) -> str:
|
|
36
|
+
"""Find the datapack CLI (arg > $DATAPACK > PATH)."""
|
|
37
|
+
cand = explicit or os.environ.get("DATAPACK") or shutil.which("datapack") or DEFAULT_DATAPACK
|
|
38
|
+
if Path(cand).exists():
|
|
39
|
+
return str(cand)
|
|
40
|
+
found = shutil.which(cand)
|
|
41
|
+
if found:
|
|
42
|
+
return found
|
|
43
|
+
raise CompressError(
|
|
44
|
+
f"datapack CLI not found ({cand!r}) — install datapack, set $DATAPACK, or "
|
|
45
|
+
"pass --datapack"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def compress_commands(
|
|
50
|
+
datapack: str,
|
|
51
|
+
sample: Path | str,
|
|
52
|
+
dest: Path | str,
|
|
53
|
+
*,
|
|
54
|
+
analysis_root: Path | str | None = None,
|
|
55
|
+
archive_raw: bool = False,
|
|
56
|
+
codec: str = "deflate",
|
|
57
|
+
) -> list[tuple[str, list[str]]]:
|
|
58
|
+
"""Build the datapack command(s) for one sample. Returns (label, argv) pairs."""
|
|
59
|
+
sample = Path(sample)
|
|
60
|
+
dst_sample = Path(dest) / sample.name
|
|
61
|
+
cmds: list[tuple[str, list[str]]] = []
|
|
62
|
+
|
|
63
|
+
raw = [datapack, "serial-section", str(sample), str(dst_sample), "--codec", codec]
|
|
64
|
+
if archive_raw:
|
|
65
|
+
raw.append("--archive-raw")
|
|
66
|
+
cmds.append(("raw", raw))
|
|
67
|
+
|
|
68
|
+
if analysis_root is not None:
|
|
69
|
+
ana = Path(analysis_root) / sample.name
|
|
70
|
+
if ana.is_dir():
|
|
71
|
+
cmds.append(
|
|
72
|
+
("analysis",
|
|
73
|
+
[datapack, "histology", "tree", str(ana), str(dst_sample), "--codec", codec])
|
|
74
|
+
)
|
|
75
|
+
return cmds
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Dataset models/loaders — the single home for lab dataset-layout knowledge."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datapipe.models.serialsection import (
|
|
6
|
+
SerialSectionError,
|
|
7
|
+
SerialSectionSample,
|
|
8
|
+
find_samples,
|
|
9
|
+
is_sample,
|
|
10
|
+
load_sample,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"SerialSectionError",
|
|
15
|
+
"SerialSectionSample",
|
|
16
|
+
"find_samples",
|
|
17
|
+
"is_sample",
|
|
18
|
+
"load_sample",
|
|
19
|
+
]
|