Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labeled comprehension channel axis #381

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions dask_image/ndmeasure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import functools
import operator
import warnings
from dask import compute, delayed

import dask.array as da
import dask.bag as db
import dask.dataframe as dd
import numpy as np
from dask import compute, delayed

from . import _utils
from ._utils import _label
Expand Down Expand Up @@ -378,7 +378,6 @@ def label(image, structure=None, wrap_axes=None):
relabeled = _label.relabel_blocks(block_labeled, new_labeling)
n = da.max(relabeled)


return (relabeled, n)


Expand All @@ -402,7 +401,9 @@ def labeled_comprehension(image,
Parameters
----------
image : ndarray
N-D image data
Intensity image with same size as ``label_image``, plus optionally
an extra dimension for multichannel data. The extra channel dimension,
if present, must be the last axis.
label_image : ndarray, optional
Image features noted by integers. If None (default), all values.
index : int or sequence of ints, optional
Expand Down Expand Up @@ -441,14 +442,16 @@ def labeled_comprehension(image,
args = (image,)
if pass_positions:
positions = _utils._ravel_shape_indices(
image.shape, chunks=image.chunks
image.shape, chunks=image.chunks, skip_trailing_dim=image.ndim != label_image.ndim
)
args = (image, positions)

result = np.empty(index.shape, dtype=object)
for i in np.ndindex(index.shape):
lbl_mtch_i = (label_image == index[i])
args_lbl_mtch_i = tuple(e[lbl_mtch_i] for e in args)
args_lbl_mtch_i = tuple(
e[lbl_mtch_i] if e.ndim == lbl_mtch_i.ndim else e.reshape(-1, e.shape[-1])[lbl_mtch_i.reshape(-1)] for e in
args)
result[i] = _utils._labeled_comprehension_func(
func, out_dtype, default_1d, *args_lbl_mtch_i
)
Expand Down
9 changes: 5 additions & 4 deletions dask_image/ndmeasure/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def _norm_input_labels_index(image, label_image=None, index=None):
FutureWarning
)

if image.shape != label_image.shape:
image_shape = image.shape if image.ndim == label_image.ndim else image.shape[:-1]
if image_shape != label_image.shape: # allow trailing channel
raise ValueError(
"The image and label_image arrays must be the same shape."
f"The image and label_image arrays must be the same shape. {image_shape} != {label_image.shape}"
)

return (image, label_image, index)
Expand All @@ -47,7 +48,7 @@ def _ravel_shape_indices_kernel(*args):
return sum(args2)


def _ravel_shape_indices(dimensions, dtype=int, chunks=None):
def _ravel_shape_indices(dimensions, dtype=int, chunks=None, skip_trailing_dim:bool=False):
"""
Gets the raveled indices shaped like input.
"""
Expand All @@ -60,7 +61,7 @@ def _ravel_shape_indices(dimensions, dtype=int, chunks=None):
dtype=dtype,
chunks=c
)
for i, c in enumerate(chunks)
for i, c in enumerate(chunks[:-1] if skip_trailing_dim else chunks)
]

indices = da.blockwise(
Expand Down