Skip to content

Commit

Permalink
Use labeled_comprehension directly in more function in ndmeasure (#74)
Browse files Browse the repository at this point in the history
* Use NumPy's `mean` with `labeled_comprehension`

When computing a label's mean, NumPy's `mean` function can be used
directly with `labeled_comprehension`. As `labeled_comprehension` is
already being used by `mean` under the hood given that is what `sum`
uses. There is no reason not to just use `labeled_comprehension`
directly in `mean` with the function that we would like to compute.
After all each call to the user provided function includes all relevant
values represented by the label. So NumPy's `mean` is accurate in this
case. Should simplify the Dask graph of `mean` and the computation time
needed. Also should benefit any other function using `mean`.

* Use NumPy's `var` with `labeled_comprehension`

When computing a label's variance, NumPy's `var` function can be used
directly with `labeled_comprehension`. As `labeled_comprehension` is
already being used by `variance` under the hood given that is what `sum`
uses. There is no reason not to just use `labeled_comprehension`
directly in `variance` with the function that we would like to compute.
After all each call to the user provided function includes all relevant
values represented by the label. So NumPy's `var` is accurate in this
case. Should simplify the Dask graph of `variance` and the computation
time needed. Also should benefit any other function using `variance`.

* Use NumPy's `std` with `labeled_comprehension`

When computing a label's standard deviation, NumPy's `std` function can
be used directly with `labeled_comprehension`. As
`labeled_comprehension` is already being used by `standard_deviation`
under the hood given that is what `var` uses. There is no reason not to
just use `labeled_comprehension` directly in `standard_deviation` with
the function that we would like to compute.  After all each call to the
user provided function includes all relevant values represented by the
label. So NumPy's `std` is accurate in this case. Should simplify the
Dask graph of `standard_deviation` and the computation time needed. Also
should benefit any other function using `standard_deviation`.
  • Loading branch information
jakirkham committed Sep 30, 2018
1 parent 2e1abea commit 6ab2f04
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions dask_image/ndmeasure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,11 @@ def mean(input, labels=None, index=None):
input, labels, index
)

sum_lbl = sum(input, labels, index)
norm_lbl = sum(
dask.array.ones(input.shape, dtype=input.dtype, chunks=input.chunks),
labels,
index
)
nan = numpy.float64(numpy.nan)

mean_lbl = sum_lbl / norm_lbl
mean_lbl = labeled_comprehension(
input, labels, index, numpy.mean, numpy.float64, nan
)

return mean_lbl

Expand Down Expand Up @@ -596,7 +593,11 @@ def standard_deviation(input, labels=None, index=None):
input, labels, index
)

std_lbl = dask.array.sqrt(variance(input, labels, index))
nan = numpy.float64(numpy.nan)

std_lbl = labeled_comprehension(
input, labels, index, numpy.std, numpy.float64, nan
)

return std_lbl

Expand Down Expand Up @@ -661,9 +662,10 @@ def variance(input, labels=None, index=None):
input, labels, index
)

input_2_mean = mean(dask.array.square(input), labels, index)
input_mean_2 = dask.array.square(mean(input, labels, index))
nan = numpy.float64(numpy.nan)

var_lbl = input_2_mean - input_mean_2
var_lbl = labeled_comprehension(
input, labels, index, numpy.var, numpy.float64, nan
)

return var_lbl

0 comments on commit 6ab2f04

Please sign in to comment.