Skip to content

zarr_utils

Utilities to work with the Pydantic models from specs.py for Zarr groups.

ZarrGroupNotFoundError

Bases: ValueError

Wrap zarr.errors.GroupNotFoundError

This is used to provide a user-friendly error message.

Source code in fractal_tasks_core/ngff/zarr_utils.py
16
17
18
19
20
21
22
23
class ZarrGroupNotFoundError(ValueError):
    """
    Wrap zarr.errors.GroupNotFoundError

    This is used to provide a user-friendly error message.
    """

    pass

detect_ome_ngff_type(group)

Given a Zarr group, find whether it is an OME-NGFF plate, well or image.

PARAMETER DESCRIPTION
group

Zarr group

TYPE: Group

RETURNS DESCRIPTION
str

The detected OME-NGFF type (plate, well or image).

Source code in fractal_tasks_core/ngff/zarr_utils.py
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
def detect_ome_ngff_type(group: zarr.hierarchy.Group) -> str:
    """
    Given a Zarr group, find whether it is an OME-NGFF plate, well or image.

    Args:
        group: Zarr group

    Returns:
        The detected OME-NGFF type (`plate`, `well` or `image`).
    """
    attrs = group.attrs.asdict()
    if "plate" in attrs.keys():
        ngff_type = "plate"
    elif "well" in attrs.keys():
        ngff_type = "well"
    elif "multiscales" in attrs.keys():
        ngff_type = "image"
    else:
        error_msg = (
            "Zarr group at cannot be identified as one "
            "of OME-NGFF plate/well/image groups."
        )
        logger.error(error_msg)
        raise ValueError(error_msg)
    logger.info(f"Zarr group identified as OME-NGFF {ngff_type}.")
    return ngff_type

load_NgffImageMeta(zarr_path)

Load the attributes of a zarr group and cast them to NgffImageMeta.

PARAMETER DESCRIPTION
zarr_path

Path to the zarr group.

TYPE: str

RETURNS DESCRIPTION
NgffImageMeta

A new NgffImageMeta object.

Source code in fractal_tasks_core/ngff/zarr_utils.py
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
def load_NgffImageMeta(zarr_path: str) -> NgffImageMeta:
    """
    Load the attributes of a zarr group and cast them to `NgffImageMeta`.

    Args:
        zarr_path: Path to the zarr group.

    Returns:
        A new `NgffImageMeta` object.
    """
    try:
        zarr_group = zarr.open_group(zarr_path, mode="r")
    except GroupNotFoundError:
        error_msg = (
            "Could not load attributes for the requested image, "
            f"because no Zarr group was found at {zarr_path}"
        )
        logging.error(error_msg)
        raise ZarrGroupNotFoundError(error_msg)
    zarr_attrs = zarr_group.attrs.asdict()
    try:
        return NgffImageMeta(**zarr_attrs)
    except Exception as e:
        logging.error(
            f"Contents of {zarr_path} cannot be cast to NgffImageMeta.\n"
            f"Original error:\n{str(e)}"
        )
        raise e

load_NgffPlateMeta(zarr_path)

Load the attributes of a zarr group and cast them to NgffPlateMeta.

PARAMETER DESCRIPTION
zarr_path

Path to the zarr group.

TYPE: str

RETURNS DESCRIPTION
NgffPlateMeta

A new NgffPlateMeta object.

Source code in fractal_tasks_core/ngff/zarr_utils.py
 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
def load_NgffPlateMeta(zarr_path: str) -> NgffPlateMeta:
    """
    Load the attributes of a zarr group and cast them to `NgffPlateMeta`.

    Args:
        zarr_path: Path to the zarr group.

    Returns:
        A new `NgffPlateMeta` object.
    """
    try:
        zarr_group = zarr.open_group(zarr_path, mode="r")
    except GroupNotFoundError:
        error_msg = (
            "Could not load attributes for the requested plate, "
            f"because no Zarr group was found at {zarr_path}"
        )
        logging.error(error_msg)
        raise ZarrGroupNotFoundError(error_msg)
    zarr_attrs = zarr_group.attrs.asdict()
    try:
        return NgffPlateMeta(**zarr_attrs)
    except Exception as e:
        logging.error(
            f"Contents of {zarr_path} cannot be cast to NgffPlateMeta.\n"
            f"Original error:\n{str(e)}"
        )
        raise e

load_NgffWellMeta(zarr_path)

Load the attributes of a zarr group and cast them to NgffWellMeta.

PARAMETER DESCRIPTION
zarr_path

Path to the zarr group.

TYPE: str

RETURNS DESCRIPTION
NgffWellMeta

A new NgffWellMeta object.

Source code in fractal_tasks_core/ngff/zarr_utils.py
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
def load_NgffWellMeta(zarr_path: str) -> NgffWellMeta:
    """
    Load the attributes of a zarr group and cast them to `NgffWellMeta`.

    Args:
        zarr_path: Path to the zarr group.

    Returns:
        A new `NgffWellMeta` object.
    """
    try:
        zarr_group = zarr.open_group(zarr_path, mode="r")
    except GroupNotFoundError:
        error_msg = (
            "Could not load attributes for the requested well, "
            f"because no Zarr group was found at {zarr_path}"
        )
        logging.error(error_msg)
        raise ZarrGroupNotFoundError(error_msg)
    zarr_attrs = zarr_group.attrs.asdict()
    try:
        return NgffWellMeta(**zarr_attrs)
    except Exception as e:
        logging.error(
            f"Contents of {zarr_path} cannot be cast to NgffWellMeta.\n"
            f"Original error:\n{str(e)}"
        )
        raise e