|
19 | 19 |
|
20 | 20 | import zarr.hierarchy
|
21 | 21 | from anndata.experimental import write_elem
|
| 22 | +from pydantic.error_wrappers import ValidationError |
22 | 23 | from zarr.errors import ContainsGroupError
|
23 | 24 | from zarr.errors import GroupNotFoundError
|
24 | 25 |
|
| 26 | +from .lib_ngff import NgffImageMeta |
| 27 | + |
25 | 28 |
|
26 | 29 | class OverwriteNotAllowedError(RuntimeError):
|
27 | 30 | pass
|
@@ -213,7 +216,8 @@ def prepare_label_group(
|
213 | 216 | image_group:
|
214 | 217 | The group to write to.
|
215 | 218 | label_name:
|
216 |
| - The name of the new label. |
| 219 | + The name of the new label; this name also overrides the multiscale |
| 220 | + name in NGFF-image Zarr attributes, if needed. |
217 | 221 | overwrite:
|
218 | 222 | If `False`, check that the new label does not exist (either in zarr
|
219 | 223 | attributes or as a zarr sub-group); if `True` propagate parameter
|
@@ -270,6 +274,26 @@ def prepare_label_group(
|
270 | 274 |
|
271 | 275 | # Optionally update attributes of the new-table zarr group
|
272 | 276 | if label_attrs is not None:
|
| 277 | + # Validate attrs against NGFF specs 0.4 |
| 278 | + try: |
| 279 | + meta = NgffImageMeta(**label_attrs) |
| 280 | + except ValidationError as e: |
| 281 | + error_msg = ( |
| 282 | + "Label attributes do not comply with NGFF image " |
| 283 | + "specifications, as encoded in fractal-tasks-core.\n" |
| 284 | + f"Original error:\nValidationError: {str(e)}" |
| 285 | + ) |
| 286 | + logger.error(error_msg) |
| 287 | + raise ValueError(error_msg) |
| 288 | + # Replace multiscale name with label_name, if needed |
| 289 | + current_multiscale_name = meta.multiscale.name |
| 290 | + if current_multiscale_name != label_name: |
| 291 | + logger.warning( |
| 292 | + f"Setting multiscale name to '{label_name}' (old value: " |
| 293 | + f"'{current_multiscale_name}') in label-image NGFF " |
| 294 | + "attributes." |
| 295 | + ) |
| 296 | + label_attrs["multiscales"][0]["name"] = label_name |
273 | 297 | # Overwrite label_group attributes with label_attrs key/value pairs
|
274 | 298 | label_group.attrs.put(label_attrs)
|
275 | 299 |
|
|
0 commit comments