| 1 |
# -*- coding: utf-8 -*- |
| 2 |
import warnings |
| 3 |
|
| 4 |
import dask |
| 5 |
import dask.array as da |
| 6 |
import numpy as np |
| 7 |
|
| 8 |
|
| 9 |
def _norm_input_labels_index(image, label_image=None, index=None): |
| 10 |
""" |
| 11 |
Normalize arguments to a standard form. |
| 12 |
""" |
| 13 |
|
| 14 |
image = da.asarray(image) |
| 15 |
|
| 16 |
if label_image is None: |
| 17 |
label_image = da.ones( |
| 18 |
image.shape, dtype=int, chunks=image.chunks, |
| 19 |
) |
| 20 |
index = da.from_array(np.array(1, dtype=int)) |
| 21 |
elif index is None: |
| 22 |
label_image = (label_image > 0).astype(int) |
| 23 |
index = da.from_array(np.array(1, dtype=int)) |
| 24 |
|
| 25 |
label_image = da.asarray(label_image) |
| 26 |
index = da.asarray(index) |
| 27 |
|
| 28 |
if index.ndim > 1: |
| 29 |
warnings.warn( |
| 30 |
"Having index with dimensionality greater than 1 is undefined.", |
| 31 |
FutureWarning |
| 32 |
) |
| 33 |
|
| 34 |
if image.shape != label_image.shape: |
| 35 |
raise ValueError( |
| 36 |
"The image and label_image arrays must be the same shape." |
| 37 |
) |
| 38 |
|
| 39 |
return (image, label_image, index) |
| 40 |
|
| 41 |
|
| 42 |
def _ravel_shape_indices_kernel(*args): |
| 43 |
args2 = tuple( |
| 44 |
a[i * (None,) + (slice(None),) + (len(args) - i - 1) * (None,)] |
| 45 |
for i, a in enumerate(args) |
| 46 |
) |
| 47 |
return sum(args2) |
| 48 |
|
| 49 |
|
| 50 |
def _ravel_shape_indices(dimensions, dtype=int, chunks=None): |
| 51 |
""" |
| 52 |
Gets the raveled indices shaped like input. |
| 53 |
""" |
| 54 |
|
| 55 |
indices = [ |
| 56 |
da.arange( |
| 57 |
0, |
| 58 |
np.prod(dimensions[i:], dtype=dtype), |
| 59 |
np.prod(dimensions[i + 1:], dtype=dtype), |
| 60 |
dtype=dtype, |
| 61 |
chunks=c |
| 62 |
) |
| 63 |
for i, c in enumerate(chunks) |
| 64 |
] |
| 65 |
|
| 66 |
indices = da.blockwise( |
| 67 |
_ravel_shape_indices_kernel, tuple(range(len(indices))), |
| 68 |
*sum([(a, (i,)) for i, a in enumerate(indices)], tuple()), |
| 69 |
dtype=dtype |
| 70 |
) |
| 71 |
|
| 72 |
return indices |
| 73 |
|
| 74 |
|
| 75 |
def _argmax(a, positions, shape, dtype): |
| 76 |
""" |
| 77 |
Find original array position corresponding to the maximum. |
| 78 |
""" |
| 79 |
|
| 80 |
result = np.empty((1,), dtype=dtype) |
| 81 |
|
| 82 |
pos_nd = np.unravel_index(positions[np.argmax(a)], shape) |
| 83 |
for i, pos_nd_i in enumerate(pos_nd): |
| 84 |
result["pos"][0, i] = pos_nd_i |
| 85 |
|
| 86 |
return result[0] |
| 87 |
|
| 88 |
|
| 89 |
def _argmin(a, positions, shape, dtype): |
| 90 |
""" |
| 91 |
Find original array position corresponding to the minimum. |
| 92 |
""" |
| 93 |
|
| 94 |
result = np.empty((1,), dtype=dtype) |
| 95 |
|
| 96 |
pos_nd = np.unravel_index(positions[np.argmin(a)], shape) |
| 97 |
for i, pos_nd_i in enumerate(pos_nd): |
| 98 |
result["pos"][0, i] = pos_nd_i |
| 99 |
|
| 100 |
return result[0] |
| 101 |
|
| 102 |
|
| 103 |
def _center_of_mass(a, positions, shape, dtype): |
| 104 |
""" |
| 105 |
Find the center of mass for each ROI. |
| 106 |
""" |
| 107 |
|
| 108 |
result = np.empty((1,), dtype=dtype) |
| 109 |
|
| 110 |
positions_nd = np.unravel_index(positions, shape) |
| 111 |
a_sum = np.sum(a) |
| 112 |
|
| 113 |
a_wt_i = np.empty(a.shape) |
| 114 |
for i, pos_nd_i in enumerate(positions_nd): |
| 115 |
a_wt_sum_i = np.multiply(a, pos_nd_i, out=a_wt_i).sum() |
| 116 |
result["com"][0, i] = a_wt_sum_i / a_sum |
| 117 |
|
| 118 |
return result[0] |
| 119 |
|
| 120 |
|
| 121 |
def _extrema(a, positions, shape, dtype): |
| 122 |
""" |
| 123 |
Find minimum and maximum as well as positions for both. |
| 124 |
""" |
| 125 |
|
| 126 |
result = np.empty((1,), dtype=dtype) |
| 127 |
|
| 128 |
int_min_pos = np.argmin(a) |
| 129 |
int_max_pos = np.argmax(a) |
| 130 |
|
| 131 |
result["min_val"] = a[int_min_pos] |
| 132 |
result["max_val"] = a[int_max_pos] |
| 133 |
|
| 134 |
min_pos_nd = np.unravel_index(positions[int_min_pos], shape) |
| 135 |
max_pos_nd = np.unravel_index(positions[int_max_pos], shape) |
| 136 |
for i in range(len(shape)): |
| 137 |
result["min_pos"][0, i] = min_pos_nd[i] |
| 138 |
result["max_pos"][0, i] = max_pos_nd[i] |
| 139 |
|
| 140 |
return result[0] |
| 141 |
|
| 142 |
|
| 143 |
def _histogram(image, |
| 144 |
min, |
| 145 |
max, |
| 146 |
bins): |
| 147 |
""" |
| 148 |
Delayed wrapping of NumPy's histogram |
| 149 |
|
| 150 |
Also reformats the arguments. |
| 151 |
""" |
| 152 |
|
| 153 |
return np.histogram(image, bins, (min, max))[0] |
| 154 |
|
| 155 |
|
| 156 |
@dask.delayed |
| 157 |
def _labeled_comprehension_delayed(func, |
| 158 |
out_dtype, |
| 159 |
default, |
| 160 |
a, |
| 161 |
positions=None): |
| 162 |
""" |
| 163 |
Wrapped delayed labeled comprehension function |
| 164 |
|
| 165 |
Included in the module for pickling purposes. Also handle cases where |
| 166 |
computation should not occur. |
| 167 |
""" |
| 168 |
|
| 169 |
result = np.empty((1,), dtype=out_dtype) |
| 170 |
|
| 171 |
if a.size: |
| 172 |
if positions is None: |
| 173 |
result[0] = func(a) |
| 174 |
else: |
| 175 |
result[0] = func(a, positions) |
| 176 |
else: |
| 177 |
result[0] = default[0] |
| 178 |
|
| 179 |
return result |
| 180 |
|
| 181 |
|
| 182 |
def _labeled_comprehension_func(func, |
| 183 |
out_dtype, |
| 184 |
default, |
| 185 |
a, |
| 186 |
positions=None): |
| 187 |
""" |
| 188 |
Wrapped labeled comprehension function |
| 189 |
|
| 190 |
Ensures the result is a proper Dask Array and the computation delayed. |
| 191 |
""" |
| 192 |
|
| 193 |
return da.from_delayed( |
| 194 |
_labeled_comprehension_delayed(func, out_dtype, default, a, positions), |
| 195 |
(1,), |
| 196 |
out_dtype |
| 197 |
) |