-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.py
344 lines (305 loc) · 11.1 KB
/
cli.py
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
import argparse
import subprocess
import sys
from functools import wraps
from pathlib import Path
from typing import Any, Callable, List
from omero.cli import CLI, BaseControl, Parser, ProxyStringType
from omero.gateway import BlitzGateway, BlitzObjectWrapper
from omero.model import ImageI, PlateI
from zarr.hierarchy import open_group
from zarr.storage import FSStore
from .masks import MASK_DTYPE_SIZE, image_shapes_to_zarr, plate_shapes_to_zarr
from .raw_pixels import (
add_omero_metadata,
add_toplevel_metadata,
image_to_zarr,
plate_to_zarr,
)
HELP = """Export data in zarr format.
Subcommands
===========
- export
- masks
"""
EXPORT_HELP = """Export an image in zarr format.
In order to use bioformats2raw for the actual export,
make sure the bioformats2raw binary is in the $PATH.
"""
MASKS_HELP = """Export ROI Masks on the Image in zarr format.
Options
-------
--style
'labeled': 5D integer values (default but overlaps are not supported!)
'split': one group per ROI
"""
POLYGONS_HELP = """Export ROI Polygons on the Image or Plate in zarr format"""
def gateway_required(func: Callable) -> Callable:
"""
Decorator which initializes a client (self.client),
a BlitzGateway (self.gateway), and makes sure that
all services of the Blitzgateway are closed again.
"""
@wraps(func)
def _wrapper(self: Any, *args: Any, **kwargs: Any) -> Callable:
self.client = self.ctx.conn(*args)
self.gateway = BlitzGateway(client_obj=self.client)
try:
return func(self, *args, **kwargs)
finally:
if self.gateway is not None:
self.gateway.close(hard=False)
self.gateway = None
self.client = None # type: ignore
return _wrapper
class ZarrControl(BaseControl):
gateway = None
client = None
def _configure(self, parser: Parser) -> None:
parser.add_login_arguments()
parser.add_argument(
"--output", type=str, default="", help="The output directory"
)
parser.add_argument(
"--cache_numpy",
action="store_true",
help="Save planes as .npy files in case of connection loss",
)
# Subcommands
sub = parser.sub()
polygons = parser.add(sub, self.polygons, POLYGONS_HELP)
polygons.add_argument(
"object",
type=ProxyStringType("Image"),
help="The Image from which to export Polygons.",
)
polygons.add_argument(
"--label-bits",
default=str(max(MASK_DTYPE_SIZE.keys())),
choices=[str(s) for s in sorted(MASK_DTYPE_SIZE.keys())],
help=(
"Integer bit size for each label pixel, use 1 for a binary "
"label, default %(default)s"
),
)
polygons.add_argument(
"--style",
choices=("split", "labeled"),
default="labeled",
help=("Choice of storage for ROIs [breaks ome-zarr]"),
)
polygons.add_argument(
"--label-path",
help=(
"Subdirectory of the image location for storing labels. "
"[breaks ome-zarr]"
),
default="labels",
)
polygons.add_argument(
"--source-image",
help=(
"Path to the multiscales group containing the source image/plate. "
"By default, use the output directory"
),
default=None,
)
polygons.add_argument(
"--label-map",
help=(
"File in format: ID,NAME,ROI_ID which is used to separate "
"overlapping labels"
),
)
polygons.add_argument(
"--label-name",
help=("Name of the array that will be stored. Ignored for --style=split"),
default="0",
)
masks = parser.add(sub, self.masks, MASKS_HELP)
masks.add_argument(
"object",
type=ProxyStringType("Image"),
help="The Image from which to export Masks.",
)
masks.add_argument(
"--source-image",
help=(
"Path to the multiscales group containing the source image/plate. "
"By default, use the output directory"
),
default=None,
)
masks.add_argument(
"--label-path",
help=(
"Subdirectory of the image location for storing labels. "
"[breaks ome-zarr]"
),
default="labels",
)
masks.add_argument(
"--label-name",
help=("Name of the array that will be stored. Ignored for --style=split"),
default="0",
)
masks.add_argument(
"--style",
choices=("split", "labeled"),
default="labeled",
help=("Choice of storage for ROIs [breaks ome-zarr]"),
)
masks.add_argument(
"--label-bits",
default=str(max(MASK_DTYPE_SIZE.keys())),
choices=[str(s) for s in sorted(MASK_DTYPE_SIZE.keys())],
help=(
"Integer bit size for each label pixel, use 1 for a binary "
"label, default %(default)s"
),
)
masks.add_argument(
"--label-map",
help=(
"File in format: ID,NAME,ROI_ID which is used to separate "
"overlapping labels"
),
)
export = parser.add(sub, self.export, EXPORT_HELP)
export.add_argument(
"--bf",
action="store_true",
help="Use bioformats2raw to export the image. Requires"
" bioformats2raw 0.3.0 or higher.",
)
export.add_argument(
"--tile_width",
default=None,
help="Maximum tile width to read (only for use with bioformats2raw)",
)
export.add_argument(
"--tile_height",
default=None,
help="Maximum tile height to read (only for use with bioformats2raw)",
)
export.add_argument(
"--resolutions",
default=None,
help="Number of pyramid resolutions to generate"
" (only for use with bioformats2raw)",
)
export.add_argument(
"--max_workers",
default=None,
help="Maximum number of workers (only for use with bioformats2raw)",
)
export.add_argument(
"object",
type=ProxyStringType("Image"),
help="The Image to export.",
)
@gateway_required
def masks(self, args: argparse.Namespace) -> None:
"""Export masks on the Image as zarr files."""
if isinstance(args.object, ImageI):
image_id = args.object.id
image = self._lookup(self.gateway, "Image", image_id)
self.ctx.out("Export Masks on Image: %s" % image.name)
image_shapes_to_zarr(image, ["Mask"], args)
elif isinstance(args.object, PlateI):
plate = self._lookup(self.gateway, "Plate", args.object.id)
plate_shapes_to_zarr(plate, ["Mask"], args)
@gateway_required
def polygons(self, args: argparse.Namespace) -> None:
"""Export polygons on the Plate or Image as zarr files."""
if isinstance(args.object, ImageI):
image_id = args.object.id
image = self._lookup(self.gateway, "Image", image_id)
self.ctx.out("Export Polygons on Image: %s" % image.name)
image_shapes_to_zarr(image, ["Polygon"], args)
elif isinstance(args.object, PlateI):
plate = self._lookup(self.gateway, "Plate", args.object.id)
plate_shapes_to_zarr(plate, ["Polygon"], args)
@gateway_required
def export(self, args: argparse.Namespace) -> None:
if isinstance(args.object, ImageI):
image = self._lookup(self.gateway, "Image", args.object.id)
if args.bf:
self._bf_export(image, args)
else:
image_to_zarr(image, args)
elif isinstance(args.object, PlateI):
plate = self._lookup(self.gateway, "Plate", args.object.id)
plate_to_zarr(plate, args)
def _lookup(
self, gateway: BlitzGateway, otype: str, oid: int
) -> BlitzObjectWrapper:
"""Find object of type by ID."""
gateway.SERVICE_OPTS.setOmeroGroup("-1")
obj = gateway.getObject(otype, oid)
if not obj:
self.ctx.die(110, f"No such {otype}: {oid}")
return obj
def _bf_export(self, image: BlitzObjectWrapper, args: argparse.Namespace) -> None:
if image.getInplaceImport():
p = image.getImportedImageFilePaths()["client_paths"][0]
abs_path = Path("/") / Path(p)
else:
if self.client is None:
raise Exception("This cannot happen") # mypy is confused
prx, desc = self.client.getManagedRepository(description=True)
p = image.getImportedImageFilePaths()["server_paths"][0]
abs_path = Path(desc._path._val) / Path(desc._name._val) / Path(p)
target = (Path(args.output) or Path.cwd()) / Path(abs_path).name
image_target = (Path(args.output) or Path.cwd()) / f"{image.id}.zarr"
if target.exists():
self.ctx.die(111, f"{target.resolve()} already exists")
if image_target.exists():
self.ctx.die(111, f"{image_target.resolve()} already exists")
cmd: List[str] = [
"bioformats2raw",
str(abs_path.resolve()),
str(target.resolve()),
]
if args.tile_width:
cmd.append(f"--tile_width={args.tile_width}")
if args.tile_height:
cmd.append(f"--tile_height={args.tile_height}")
if args.resolutions:
cmd.append(f"--resolutions={args.resolutions}")
if args.max_workers:
cmd.append(f"--max_workers={args.max_workers}")
cmd.append(f"--series={image.series}")
cmd.append("--no-root-group")
cmd.append("--no-ome-meta-export")
self.ctx.dbg(" ".join(cmd))
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate()
if stderr:
self.ctx.err(stderr.decode("utf-8"))
if process.returncode == 0:
image_source = target / "0"
image_source.rename(image_target)
target.rmdir()
self.ctx.out(f"Image exported to {image_target.resolve()}")
# Add OMERO metadata
store = FSStore(
str(image_target.resolve()),
auto_mkdir=False,
normalize_keys=False,
mode="w",
)
root = open_group(store)
add_omero_metadata(root, image)
add_toplevel_metadata(root)
try:
register("zarr", ZarrControl, HELP) # type: ignore
except NameError:
if __name__ == "__main__":
cli = CLI()
cli.register("zarr", ZarrControl, HELP)
cli.invoke(sys.argv[1:])