| 1 |
import numpy as np |
| 2 |
from dask.delayed import Delayed |
| 3 |
import dask.config as dask_config |
| 4 |
|
| 5 |
|
| 6 |
def _array_chunk_location(block_id, chunks): |
| 7 |
"""Pixel coordinate of top left corner of the array chunk.""" |
| 8 |
array_location = [] |
| 9 |
for idx, chunk in zip(block_id, chunks): |
| 10 |
array_location.append(sum(chunk[:idx])) |
| 11 |
return tuple(array_location) |
| 12 |
|
| 13 |
|
| 14 |
def _find_bounding_boxes(x, array_location): |
| 15 |
"""An alternative to scipy.ndimage.find_objects. |
| 16 |
|
| 17 |
We use this alternative because scipy.ndimage.find_objects |
| 18 |
returns a tuple of length N, where N is the largest integer label. |
| 19 |
This is not ideal for distributed labels, where there might be only |
| 20 |
one or two objects in an image chunk labelled with very large integers. |
| 21 |
|
| 22 |
This alternative function returns a pandas dataframe, |
| 23 |
with one row per object found in the image chunk. |
| 24 |
""" |
| 25 |
import pandas as pd |
| 26 |
|
| 27 |
unique_vals = np.unique(x) |
| 28 |
unique_vals = unique_vals[unique_vals != 0] |
| 29 |
result = {} |
| 30 |
for val in unique_vals: |
| 31 |
positions = np.where(x == val) |
| 32 |
slices = tuple( |
| 33 |
slice( |
| 34 |
np.min(pos) + array_location[i], |
| 35 |
np.max(pos) + 1 + array_location[i] |
| 36 |
) |
| 37 |
for i, pos in enumerate(positions) |
| 38 |
) |
| 39 |
result[val] = slices |
| 40 |
column_names = [i for i in range(x.ndim)] # column names are: 0, 1, ... nD |
| 41 |
return pd.DataFrame.from_dict(result, orient='index', columns=column_names) |
| 42 |
|
| 43 |
|
| 44 |
def _combine_slices(slices): |
| 45 |
"Return the union of all slices." |
| 46 |
if len(slices) == 1: |
| 47 |
return slices[0] |
| 48 |
else: |
| 49 |
start = min([sl.start for sl in slices]) |
| 50 |
stop = max([sl.stop for sl in slices]) |
| 51 |
return slice(start, stop) |
| 52 |
|
| 53 |
|
| 54 |
def _merge_bounding_boxes(x, ndim): |
| 55 |
"Merge the bounding boxes describing objects over multiple image chunks." |
| 56 |
import pandas as pd |
| 57 |
|
| 58 |
x = x.dropna() |
| 59 |
data = {} |
| 60 |
# For each dimension in the array, |
| 61 |
# pick out the slice values belonging to that dimension |
| 62 |
# and combine the slices |
| 63 |
# (i.e. find the union; the slice expanded to all input slices). |
| 64 |
for i in range(ndim): |
| 65 |
# Array dimensions are labelled by a number followed by an underscroe |
| 66 |
# i.e. column labels are: 0_x, 1_x, 2_x, ... 0_y, 1_y, 2_y, ... |
| 67 |
# (x and y represent the pair of chunks label slices are merged from) |
| 68 |
slices = [x[ii] for ii in x.index if str(ii).startswith(str(i))] |
| 69 |
combined_slices = _combine_slices(slices) |
| 70 |
data[i] = combined_slices |
| 71 |
result = pd.Series(data=data, index=[i for i in range(ndim)], name=x.name) |
| 72 |
return result |
| 73 |
|
| 74 |
|
| 75 |
def _find_objects(ndim, df1, df2): |
| 76 |
"""Main utility function for find_objects.""" |
| 77 |
import pandas as pd |
| 78 |
import dask.dataframe as dd |
| 79 |
|
| 80 |
meta = dd.utils.make_meta([(i, object) for i in range(ndim)]) |
| 81 |
if isinstance(df1, Delayed): |
| 82 |
with dask_config.set({'dataframe.convert-string': False}): |
| 83 |
df1 = dd.from_delayed(df1, meta=meta) |
| 84 |
if isinstance(df2, Delayed): |
| 85 |
with dask_config.set({'dataframe.convert-string': False}): |
| 86 |
df2 = dd.from_delayed(df2, meta=meta) |
| 87 |
|
| 88 |
if len(df1) > 0 and len(df2) > 0: |
| 89 |
ddf = dd.merge( |
| 90 |
df1, df2, |
| 91 |
how="outer", left_index=True, right_index=True) |
| 92 |
elif len(df1) > 0: |
| 93 |
ddf = df1 |
| 94 |
elif len(df2) > 0: |
| 95 |
ddf = df2 |
| 96 |
else: |
| 97 |
ddf = pd.DataFrame() |
| 98 |
|
| 99 |
result = ddf.apply(_merge_bounding_boxes, ndim=ndim, axis=1, meta=meta) |
| 100 |
return result |