Skip to content

Commit

Permalink
Rewrite histogram to use labeled_comprehension (#75)
Browse files Browse the repository at this point in the history
To simplify the `histogram` implementation a bit, rewrite it using
`labeled_comprehension`. Make use of `functools.partial` to bind
arguments to `histogram` so that it can be used with
`labeled_comprehension`. Also simplify the internal `_histogram`
function into a basic kernel function following these changes. Set the
return type of `labeled_comprehension` as `object` to match that of what
`histogram` used prior.
  • Loading branch information
jakirkham committed Sep 30, 2018
1 parent 6ab2f04 commit b7ca885
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
13 changes: 3 additions & 10 deletions dask_image/ndmeasure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
__email__ = "kirkhamj@janelia.hhmi.org"


import functools
import itertools
from warnings import warn

Expand Down Expand Up @@ -184,16 +185,8 @@ def histogram(input,
max = int(max)
bins = int(bins)

lbl_mtch = _utils._get_label_matches(labels, index)

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

result = numpy.empty(index.shape, dtype=object)
for i in itertools.product(*index_ranges):
result[i] = _utils._histogram(
input[lbl_mtch[i]], min, max, bins
)
result = result[()]
func = functools.partial(_utils._histogram, min=min, max=max, bins=bins)
result = labeled_comprehension(input, labels, index, func, object, None)

return result

Expand Down
6 changes: 1 addition & 5 deletions dask_image/ndmeasure/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def _extrema(a, positions):
return result[()]


@dask.delayed
def _histogram(input,
min,
max,
Expand All @@ -163,10 +162,7 @@ def _histogram(input,
Also reformats the arguments.
"""

if input.size:
return numpy.histogram(input, bins, (min, max))[0]
else:
return None
return numpy.histogram(input, bins, (min, max))[0]


@dask.delayed
Expand Down

0 comments on commit b7ca885

Please sign in to comment.