Skip to content

Commit

Permalink
Fix normalize_meta to support Xarray test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mrocklin committed Jun 14, 2019
1 parent 1f821f4 commit e7a8902
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions dask/array/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ def normalize_to_array(x):


def normalize_meta(x, ndim, dtype=None):
if ndim > x.ndim:
meta = x[(Ellipsis, ) + tuple(None for _ in range(ndim - x.ndim))]
meta = meta[tuple(slice(0, 0, None) for _ in range(meta.ndim))]
elif ndim < x.ndim:
meta = np.sum(x, axis=tuple(d for d in range((x.ndim - ndim))))
else:
meta = x

if dtype:
try:
meta = x[tuple(slice(0, 0, None) for _ in range(x.ndim))]
if meta.ndim != ndim:
if ndim > x.ndim:
meta = x[(Ellipsis, ) + tuple(None for _ in range(ndim - meta.ndim))]
elif ndim == 0:
meta = meta.sum()
else:
meta = meta.reshape((0,) * ndim)
except Exception:
meta = np.empty((0,) * ndim, dtype=dtype or x.dtype)

if dtype and meta.dtype != dtype:
meta = meta.astype(dtype)

return meta
Expand Down

0 comments on commit e7a8902

Please sign in to comment.