Skip to content

Commit

Permalink
Use NumPy's ndindex in labeled_comprehension (#81)
Browse files Browse the repository at this point in the history
To simplify iterations over labels and consolidation of the results in
`labeled_comprehension`, make use of NumPy's `ndindex` instead of
rolling our own. Makes the code a bit easier to follow and is a bit
faster than our hand rolled implementation.
  • Loading branch information
jakirkham committed Oct 1, 2018
1 parent 2d4c240 commit 58e7111
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions dask_image/ndmeasure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import collections
import functools
import itertools
from warnings import warn

import numpy
Expand Down Expand Up @@ -301,20 +300,17 @@ def labeled_comprehension(input,
)
args = (input, positions)

index_ranges = [_pycompat.irange(e) for e in index.shape]

result = numpy.empty(index.shape, dtype=object)
for i in itertools.product(*index_ranges):
for i in numpy.ndindex(index.shape):
lbl_mtch_i = lbl_mtch[i]
args_lbl_mtch_i = tuple(e[lbl_mtch_i] for e in args)
result[i] = _utils._labeled_comprehension_func(
func, out_dtype, default_1d, *args_lbl_mtch_i
)

for i in _pycompat.irange(result.ndim - 1, -1, -1):
index_ranges_i = itertools.product(*(index_ranges[:i]))
result2 = result[..., 0]
for j in index_ranges_i:
for j in numpy.ndindex(index.shape[:i]):
result2[j] = dask.array.stack(result[j].tolist(), axis=0)
result = result2
result = result[()][..., 0]
Expand Down

0 comments on commit 58e7111

Please sign in to comment.