Skip to content

Commit

Permalink
Remove numpy warning in moment calculation
Browse files Browse the repository at this point in the history
Previously we would divide by 0 in meta calculations for dask array
moments, which would raise a Numpy RuntimeWarning to users.

Now we avoid that situation, though we may also want to investigate a
more thorough solution.
  • Loading branch information
mrocklin committed Jun 12, 2019
1 parent 0554dfb commit 1fa6383
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion dask/array/reductions.py
Expand Up @@ -4,6 +4,7 @@
from functools import partial, wraps
from itertools import product, repeat
from math import factorial, log, ceil
import warnings

import numpy as np
from numbers import Integral, Number
Expand Down Expand Up @@ -466,7 +467,8 @@ def moment_chunk(A, order=2, sum=chunk.sum, numel=numel, dtype='f8', meta=False,

n = n.astype(np.int64)
total = sum(A, dtype=dtype, **kwargs)
u = total / n
with warnings.catch_warnings(record=True):
u = total / n
xs = [sum((A - u)**i, dtype=dtype, **kwargs) for i in range(2, order + 1)]
M = np.stack(xs, axis=-1)
return {'total': total, 'n': n, 'M': M}
Expand Down
9 changes: 9 additions & 0 deletions dask/array/tests/test_array_core.py
Expand Up @@ -3880,3 +3880,12 @@ def test_auto_chunks_h5py():
with dask.config.set({'array.chunk-size': '1 MiB'}):
x = da.from_array(d)
assert x.chunks == ((256, 256, 256, 232), (512, 488))


def test_no_warnings_from_blockwise():
x = da.ones((15, 15), chunks=(5, 5))

with warnings.catch_warnings(record=True) as record:
(x.dot(x.T + 1) - x.mean(axis=0)).std()

assert not record

0 comments on commit 1fa6383

Please sign in to comment.