Skip to content

projection_utils

DaskProjectionMethod

Bases: Enum

Registration method selection

Choose which method to use for intensity projection along the Z axis.

ATTRIBUTE DESCRIPTION
MIP

Maximum intensity projection

MINIP

Minimum intensityp projection

MEANIP

Mean intensity projection

SUMIP

Sum intensityp projection

Source code in fractal_tasks_core/tasks/projection_utils.py
 83
 84
 85
 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class DaskProjectionMethod(Enum):
    """
    Registration method selection

    Choose which method to use for intensity projection along the Z axis.

    Attributes:
        MIP: Maximum intensity projection
        MINIP: Minimum intensityp projection
        MEANIP: Mean intensity projection
        SUMIP: Sum intensityp projection
    """

    MIP = "mip"
    MINIP = "minip"
    MEANIP = "meanip"
    SUMIP = "sumip"

    def apply(
        self, dask_array: da.Array, axis: int = 0, **kwargs: Dict[str, Any]
    ) -> da.Array:
        """
        Apply the selected projection method to the given Dask array.

        Args:
            dask_array (dask.array.Array): The Dask array to project.
            axis (int): The axis along which to apply the projection.
            **kwargs: Additional keyword arguments to pass to the projection
                method.

        Returns:
            dask.array.Array: The resulting Dask array after applying the
                projection.

        Example:
            >>> array = da.random.random((1000, 1000), chunks=(100, 100))
            >>> method = DaskProjectionMethod.MAX
            >>> result = method.apply(array, axis=0)
            >>> computed_result = result.compute()
            >>> print(computed_result)
        """
        # Map the Enum values to the actual Dask array methods
        method_map = {
            DaskProjectionMethod.MIP: lambda arr, axis, **kw: arr.max(
                axis=axis, **kw
            ),
            DaskProjectionMethod.MINIP: lambda arr, axis, **kw: arr.min(
                axis=axis, **kw
            ),
            DaskProjectionMethod.MEANIP: mean_wrapper,
            DaskProjectionMethod.SUMIP: safe_sum,
        }
        # Call the appropriate method, passing in the dask_array explicitly
        return method_map[self](dask_array, axis=axis, **kwargs)

apply(dask_array, axis=0, **kwargs)

Apply the selected projection method to the given Dask array.

PARAMETER DESCRIPTION
dask_array

The Dask array to project.

TYPE: Array

axis

The axis along which to apply the projection.

TYPE: int DEFAULT: 0

**kwargs

Additional keyword arguments to pass to the projection method.

TYPE: Dict[str, Any] DEFAULT: {}

RETURNS DESCRIPTION
Array

dask.array.Array: The resulting Dask array after applying the projection.

Example

array = da.random.random((1000, 1000), chunks=(100, 100)) method = DaskProjectionMethod.MAX result = method.apply(array, axis=0) computed_result = result.compute() print(computed_result)

Source code in fractal_tasks_core/tasks/projection_utils.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def apply(
    self, dask_array: da.Array, axis: int = 0, **kwargs: Dict[str, Any]
) -> da.Array:
    """
    Apply the selected projection method to the given Dask array.

    Args:
        dask_array (dask.array.Array): The Dask array to project.
        axis (int): The axis along which to apply the projection.
        **kwargs: Additional keyword arguments to pass to the projection
            method.

    Returns:
        dask.array.Array: The resulting Dask array after applying the
            projection.

    Example:
        >>> array = da.random.random((1000, 1000), chunks=(100, 100))
        >>> method = DaskProjectionMethod.MAX
        >>> result = method.apply(array, axis=0)
        >>> computed_result = result.compute()
        >>> print(computed_result)
    """
    # Map the Enum values to the actual Dask array methods
    method_map = {
        DaskProjectionMethod.MIP: lambda arr, axis, **kw: arr.max(
            axis=axis, **kw
        ),
        DaskProjectionMethod.MINIP: lambda arr, axis, **kw: arr.min(
            axis=axis, **kw
        ),
        DaskProjectionMethod.MEANIP: mean_wrapper,
        DaskProjectionMethod.SUMIP: safe_sum,
    }
    # Call the appropriate method, passing in the dask_array explicitly
    return method_map[self](dask_array, axis=axis, **kwargs)

mean_wrapper(dask_array, axis=0, **kwargs)

Perform a da.mean on the dask_array & cast it to its original dtype.

Without casting, the result can change dtype to e.g. float64

PARAMETER DESCRIPTION
dask_array

The input Dask array.

TYPE: Array

axis

The axis along which to mean the array. Defaults to 0.

TYPE: int DEFAULT: 0

**kwargs

Additional keyword arguments passed to da.mean.

TYPE: Dict[str, Any] DEFAULT: {}

RETURNS DESCRIPTION
Array

dask.array.Array: The result of the mean, cast back to the original dtype.

Source code in fractal_tasks_core/tasks/projection_utils.py
49
50
51
52
53
54
55
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
def mean_wrapper(
    dask_array: da.Array, axis: int = 0, **kwargs: Dict[str, Any]
) -> da.Array:
    """
    Perform a da.mean on the dask_array & cast it to its original dtype.

    Without casting, the result can change dtype to e.g. float64

    Args:
        dask_array (dask.array.Array): The input Dask array.
        axis (int, optional): The axis along which to mean the array.
            Defaults to 0.
        **kwargs: Additional keyword arguments passed to da.mean.

    Returns:
        dask.array.Array: The result of the mean, cast back to the original
            dtype.
    """
    # Handle empty array
    if any(dim == 0 for dim in dask_array.shape):
        return dask_array

    # Determine the original dtype
    original_dtype = dask_array.dtype

    # Perform the sum
    result = da.mean(dask_array, axis=axis, **kwargs)

    # Cast back to the original dtype
    result = result.astype(original_dtype)

    return result

safe_sum(dask_array, axis=0, **kwargs)

Perform a safe sum on a Dask array to avoid overflow, by clipping the result of da.sum & casting it to its original dtype.

Dask.array already correctly handles promotion to uin32 or uint64 when necessary internally, but we want to ensure we clip the result.

PARAMETER DESCRIPTION
dask_array

The input Dask array.

TYPE: Array

axis

The axis along which to sum the array. Defaults to 0.

TYPE: int DEFAULT: 0

**kwargs

Additional keyword arguments passed to da.sum.

TYPE: Dict[str, Any] DEFAULT: {}

RETURNS DESCRIPTION
Array

dask.array.Array: The result of the sum, safely clipped and cast back to the original dtype.

Source code in fractal_tasks_core/tasks/projection_utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def safe_sum(
    dask_array: da.Array, axis: int = 0, **kwargs: Dict[str, Any]
) -> da.Array:
    """
    Perform a safe sum on a Dask array to avoid overflow, by clipping the
    result of da.sum & casting it to its original dtype.

    Dask.array already correctly handles promotion to uin32 or uint64 when
    necessary internally, but we want to ensure we clip the result.

    Args:
        dask_array (dask.array.Array): The input Dask array.
        axis (int, optional): The axis along which to sum the array.
            Defaults to 0.
        **kwargs: Additional keyword arguments passed to da.sum.

    Returns:
        dask.array.Array: The result of the sum, safely clipped and cast
            back to the original dtype.
    """
    # Handle empty array
    if any(dim == 0 for dim in dask_array.shape):
        return dask_array

    # Determine the original dtype
    original_dtype = dask_array.dtype
    max_value = np.iinfo(original_dtype).max

    # Perform the sum
    result = da.sum(dask_array, axis=axis, **kwargs)

    # Clip the values to the maximum possible value for the original dtype
    result = da.clip(result, 0, max_value)

    # Cast back to the original dtype
    result = result.astype(original_dtype)

    return result