Skip to content

tools

filter_image_list(images, filters)

Compute a sublist with images that match a filter set.

Parameters:

Name Type Description Default
images list[dict[str, Any]]

A list of images.

required
filters Filters

A set of filters.

required

Returns:

Type Description
list[dict[str, Any]]

List of the images elements which match the filter set.

Source code in fractal_server/images/tools.py
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
def filter_image_list(
    images: list[dict[str, Any]],
    filters: Filters,
) -> list[dict[str, Any]]:
    """
    Compute a sublist with images that match a filter set.

    Arguments:
        images: A list of images.
        filters: A set of filters.

    Returns:
        List of the `images` elements which match the filter set.
    """

    # When no filter is provided, return all images
    if filters.attributes == {} and filters.types == {}:
        return images

    filtered_images = [
        copy(this_image)
        for this_image in images
        if match_filter(this_image, filters=filters)
    ]
    return filtered_images

find_image_by_zarr_url(*, images, zarr_url)

Return a copy of the image with a given zarr_url, and its positional index.

Parameters:

Name Type Description Default
images list[dict[str, Any]]

List of images.

required
zarr_url str

Path that the returned image must have.

required

Returns:

Type Description
Optional[ImageSearch]

The first image from images which has zarr_url equal to zarr_url.

Source code in fractal_server/images/tools.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def find_image_by_zarr_url(
    *,
    images: list[dict[str, Any]],
    zarr_url: str,
) -> Optional[ImageSearch]:
    """
    Return a copy of the image with a given zarr_url, and its positional index.

    Arguments:
        images: List of images.
        zarr_url: Path that the returned image must have.

    Returns:
        The first image from `images` which has zarr_url equal to `zarr_url`.
    """
    image_urls = [img["zarr_url"] for img in images]
    try:
        ind = image_urls.index(zarr_url)
    except ValueError:
        return None
    return dict(image=copy(images[ind]), index=ind)

match_filter(image, filters)

Find whether an image matches a filter set.

Parameters:

Name Type Description Default
image dict[str, Any]

A single image.

required
filters Filters

A set of filters.

required

Returns:

Type Description
bool

Whether the image matches the filter set.

Source code in fractal_server/images/tools.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def match_filter(image: dict[str, Any], filters: Filters) -> bool:
    """
    Find whether an image matches a filter set.

    Arguments:
        image: A single image.
        filters: A set of filters.

    Returns:
        Whether the image matches the filter set.
    """
    # Verify match with types (using a False default)
    for key, value in filters.types.items():
        if image["types"].get(key, False) != value:
            return False
    # Verify match with attributes (only for non-None filters)
    for key, value in filters.attributes.items():
        if value is None:
            continue
        if image["attributes"].get(key) != value:
            return False
    return True