Skip to content

Images

Generate assay images.

AllImages

Bases: BaseModel

A set of generated images.

Source code in src/snailz/images.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class AllImages(BaseModel):
    """A set of generated images."""

    model_config = {"arbitrary_types_allowed": True, "extra": "forbid"}

    @staticmethod
    def generate(params: AssayParams, assays: AllAssays) -> dict:
        """Generate image files.

        Parameters:
            params: assay generation parameters
            assays: generated assays

        Returns:
            A dictionary of assay IDs and generated images.
        """
        max_reading = _find_max_reading(assays, params.plate_size)
        scaling = float(math.ceil(max_reading + 1))
        return {a.ident: _make_image(params, a, scaling) for a in assays.items}

generate(params, assays) staticmethod

Generate image files.

Parameters:

Name Type Description Default
params AssayParams

assay generation parameters

required
assays AllAssays

generated assays

required

Returns:

Type Description
dict

A dictionary of assay IDs and generated images.

Source code in src/snailz/images.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@staticmethod
def generate(params: AssayParams, assays: AllAssays) -> dict:
    """Generate image files.

    Parameters:
        params: assay generation parameters
        assays: generated assays

    Returns:
        A dictionary of assay IDs and generated images.
    """
    max_reading = _find_max_reading(assays, params.plate_size)
    scaling = float(math.ceil(max_reading + 1))
    return {a.ident: _make_image(params, a, scaling) for a in assays.items}

_find_max_reading(assays, p_size)

Find maximum assay reading value.

Parameters:

Name Type Description Default
assays AllAssays

generated assays

required
p_size int

plate size

required

Returns:

Type Description
float

Largest reading value across all assays.

Source code in src/snailz/images.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def _find_max_reading(assays: AllAssays, p_size: int) -> float:
    """Find maximum assay reading value.

    Parameters:
        assays: generated assays
        p_size: plate size

    Returns:
        Largest reading value across all assays.
    """
    result = 0.0
    for a in assays.items:
        for x in range(p_size):
            for y in range(p_size):
                result = max(result, a.readings[x, y])
    return result

_make_image(params, assay, scaling)

Generate a single image.

Parameters:

Name Type Description Default
params AssayParams

assay parameters

required
assay Assay

assay to generate image for

required
scaling float

color scaling factor

required

Returns:

Type Description
Image

Image.

Source code in src/snailz/images.py
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 _make_image(params: AssayParams, assay: Assay, scaling: float) -> PilImage:
    """Generate a single image.

    Parameters:
        params: assay parameters
        assay: assay to generate image for
        scaling: color scaling factor

    Returns:
       Image.
    """
    # Create blank image.
    p_size = params.plate_size
    img_size = (p_size * WELL_SIZE) + ((p_size + 1) * BORDER_WIDTH)
    img = Image.new("L", (img_size, img_size), color=utils.BLACK)

    # Fill with pristine reading values.
    spacing = WELL_SIZE + BORDER_WIDTH
    draw = ImageDraw.Draw(img)
    for ix, x in enumerate(range(BORDER_WIDTH, img_size, spacing)):
        for iy, y in enumerate(range(BORDER_WIDTH, img_size, spacing)):
            color = math.floor(utils.WHITE * assay.readings[ix, iy] / scaling)
            draw.rectangle((x, y, x + WELL_SIZE, y + WELL_SIZE), color)

    # Distort
    return model.image_noise(params, img, img_size)