Skip to content

Commit b06c4e4

Browse files
merge master -Dorg -Ssuccess-only: PR 147 (Name option)
2 parents ed229f0 + a14f62a commit b06c4e4

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

README.rst

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ from OMERO as zarr files, according to the spec at
1212
https://github.com/ome/omero-ms-zarr/blob/master/spec.md
1313
as well as Masks associated with Images.
1414

15-
Images are 5D arrays of shape `(t, c, z, y, x)`.
15+
Images are nD arrays of shape, up to `(t, c, z, y, x)`.
1616
Plates are a hierarchy of `plate/row/column/field(image)`.
1717
Masks are 2D bitmasks which can exist on muliplte planes of an Image.
1818
In `ome-zarr` sets of Masks are collected together into "labels".
1919

2020
It supports export using 2 alternative methods:
2121

2222
- By default the OMERO API is used to load planes as numpy arrays
23-
and the zarr file is created from this data. NB: currently, large
24-
tiled images are not supported by this method.
23+
and the zarr file is created from this data.
2524

2625
- Alternatively, if you can read directly from the OMERO binary
2726
repository and have installed https://github.com/glencoesoftware/bioformats2raw
@@ -37,16 +36,19 @@ Images and Plates
3736
To export Images or Plates via the OMERO API::
3837

3938

40-
# Image will be saved in current directory as 1.zarr
39+
# Image will be saved in current directory as 1.ome.zarr
4140
$ omero zarr export Image:1
4241

43-
# Plate will be saved in current directory as 2.zarr
42+
# Plate will be saved in current directory as 2.ome.zarr
4443
$ omero zarr export Plate:2
4544

45+
# Use the Image or Plate 'name' to save e.g. my_image.ome.zarr
46+
$ omero zarr --name_by name export Image:1
47+
4648
# Specify an output directory
4749
$ omero zarr --output /home/user/zarr_files export Image:1
4850

49-
# By default, a tile size of 1024 is used. Specify values with
51+
# By default, a tile (chunk) size of 1024 is used. Specify values with
5052
$ omero zarr export Image:1 --tile_width 256 --tile_height 256
5153

5254

@@ -66,24 +68,28 @@ Masks and Polygons
6668

6769
To export Masks or Polygons for an Image or Plate, use the `masks` or `polygons` command::
6870

69-
# Saved under 1.zarr/labels/0 - 1.zarr/ must already exist
71+
# Saved under 1.ome.zarr/labels/0
72+
# 1.ome.zarr/ should already exist...
7073
$ omero zarr masks Image:1
7174

72-
# Labels saved under each image. e.g 2.zarr/A/1/0/labels/0
73-
# Plate should already be exported
75+
# ...or specify path with --source-image
76+
$ omero zarr masks Image:1 --source-image my_image.ome.zarr
77+
78+
# Labels saved under each image. e.g 2.ome.zarr/A/1/0/labels/0
79+
# 2.ome.zarr should already be exported or specify path with --source-image
7480
$ omero zarr masks Plate:2
7581

76-
# Saved under zarr_files/1.zarr/labels/0
82+
# Saved under zarr_files/1.ome.zarr/labels/0
7783
$ omero zarr --output /home/user/zarr_files masks Image:1
7884

7985
# Specify the label-name. (default is '0')
80-
# e.g. Export to 1.zarr/labels/A
86+
# e.g. Export to 1.ome.zarr/labels/A
8187
$ omero zarr masks Image:1 --label-name=A
8288

8389
# Allow overlapping masks or polygons (overlap will be maximum value of the dtype)
8490
$ omero zarr polygons Image:1 --overlaps=dtype_max
8591

86-
The default behaviour is to export all masks or polygons on the Image to a single 5D
92+
The default behaviour is to export all masks or polygons on the Image to a single nD
8793
"labeled" zarr array, with a different value for each Shape.
8894
An exception will be thrown if any of the masks overlap, unless the `--overlaps`
8995
option is used as above.

src/omero_zarr/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,15 @@ def _configure(self, parser: Parser) -> None:
278278
default=None,
279279
help="Maximum number of workers (only for use with bioformats2raw)",
280280
)
281+
export.add_argument(
282+
"--name_by",
283+
default="id",
284+
choices=["id", "name"],
285+
help=(
286+
"How to name the Image or Plate zarr. Default 'id' is [ID].ome.zarr. "
287+
"'name' is [NAME].ome.zarr"
288+
),
289+
)
281290
export.add_argument(
282291
"object",
283292
type=ProxyStringType("Image"),

src/omero_zarr/masks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ def save(self, masks: List[omero.model.Shape], name: str) -> None:
289289
ignored_dimensions.add(d)
290290

291291
if self.plate:
292-
filename = f"{self.plate.id}.zarr"
292+
filename = f"{self.plate.id}.ome.zarr"
293293
else:
294-
filename = f"{self.image.id}.zarr"
294+
filename = f"{self.image.id}.ome.zarr"
295295

296296
# Verify that we are linking this mask to a real ome-zarr
297297
source_image = self.source_image

src/omero_zarr/raw_pixels.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,29 @@
4141
from .util import marshal_axes, marshal_transformations, open_store, print_status
4242

4343

44-
def image_to_zarr(image: omero.gateway.ImageWrapper, args: argparse.Namespace) -> None:
44+
def sanitize_name(zarr_name: str) -> str:
45+
# Avoids re.compile errors when writing Zarr data with the named root
46+
# https://github.com/ome/omero-cli-zarr/pull/147#issuecomment-1669075660
47+
return zarr_name.replace("[", "(").replace("]", ")")
48+
49+
50+
def get_zarr_name(
51+
obj: omero.gateway.BlitzObjectWrapper, args: argparse.Namespace
52+
) -> str:
4553
target_dir = args.output
54+
name_by = args.name_by
55+
if name_by == "name":
56+
obj_name = sanitize_name(obj.name)
57+
name = os.path.join(target_dir, "%s.ome.zarr" % obj_name)
58+
else:
59+
name = os.path.join(target_dir, "%s.ome.zarr" % obj.id)
60+
return name
61+
62+
63+
def image_to_zarr(image: omero.gateway.ImageWrapper, args: argparse.Namespace) -> None:
4664
tile_width = args.tile_width
4765
tile_height = args.tile_height
48-
49-
name = os.path.join(target_dir, "%s.zarr" % image.id)
66+
name = get_zarr_name(image, args)
5067
print(f"Exporting to {name} ({VERSION})")
5168
store = open_store(name)
5269
root = open_group(store)
@@ -243,9 +260,8 @@ def plate_to_zarr(plate: omero.gateway._PlateWrapper, args: argparse.Namespace)
243260
n_cols = gs["columns"]
244261
n_fields = plate.getNumberOfFields()
245262
total = n_rows * n_cols * (n_fields[1] - n_fields[0] + 1)
263+
name = get_zarr_name(plate, args)
246264

247-
target_dir = args.output
248-
name = os.path.join(target_dir, "%s.zarr" % plate.id)
249265
store = open_store(name)
250266
print(f"Exporting to {name} ({VERSION})")
251267
root = open_group(store)
@@ -296,6 +312,8 @@ def plate_to_zarr(plate: omero.gateway._PlateWrapper, args: argparse.Namespace)
296312
print_status(int(t0), int(time.time()), count, total)
297313

298314
# Update plate_metadata after each Well
315+
if len(well_paths) == 0:
316+
continue
299317
write_plate_metadata(
300318
root,
301319
row_names,

src/omero_zarr/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def print_status(t0: int, t: int, count: int, total: int) -> None:
3232
"""
3333
percent_done = float(count) * 100 / total
3434
dt = t - t0
35-
if dt > 0:
35+
if dt > 0 and count > 0:
3636
rate = float(count) / (t - t0)
3737
eta_f = float(total - count) / rate
3838
eta = time.strftime("%H:%M:%S", time.gmtime(eta_f))

0 commit comments

Comments
 (0)