Skip to content

Commit 2a258fb

Browse files
Merge pull request #174 from valeriupredoi/n-dimensions
`'n'` component needs to be a `numpy` array with retained dimensions
2 parents d5a5597 + 714b833 commit 2a258fb

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

activestorage/active.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -385,39 +385,38 @@ def _from_storage(self, stripped_indexer, drop_axes, out_shape, out_dtype,
385385
if method is not None:
386386
# Apply the method (again) to aggregate the result
387387
out = method(out)
388-
388+
shape1 = (1,) * len(out_shape)
389+
389390
if self._components:
390391
# Return a dictionary of components containing the
391392
# reduced data and the sample size ('n'). (Rationale:
392393
# cf-python needs the sample size for all reductions;
393394
# see the 'mtol' parameter of cf.Field.collapse.)
394395
#
395-
# Note that in this case the reduced data must always
396-
# have the same number of dimensions as the original
397-
# array, i.e. 'drop_axes' is always considered False,
396+
# Note that in all components must always have the
397+
# same number of dimensions as the original array,
398+
# i.e. 'drop_axes' is always considered False,
398399
# regardless of its setting. (Rationale: dask
399400
# reductions require the per-dask-chunk partial
400401
# reductions to retain these dimensions so that
401402
# partial results can be concatenated correctly.)
402-
n = np.prod(out_shape)
403-
shape1 = (1,) * len(out_shape)
404-
n = np.reshape(n, shape1)
405403
out = out.reshape(shape1)
406404

405+
n = np.sum(counts).reshape(shape1)
407406
if self._method == "mean":
408407
# For the average, the returned component is
409408
# "sum", not "mean"
410-
out = {"sum": out, "n": sum(counts)}
409+
out = {"sum": out, "n": n}
411410
else:
412-
out = {self._method: out, "n": sum(counts)}
411+
out = {self._method: out, "n": n}
413412
else:
414413
# Return the reduced data as a numpy array. For most
415414
# methods the data is already in this form.
416415
if self._method == "mean":
417416
# For the average, it is actually the sum that has
418417
# been created, so we need to divide by the sample
419418
# size.
420-
out = out / sum(counts)
419+
out = out / np.sum(counts).reshape(shape1)
421420

422421
return out
423422

0 commit comments

Comments
 (0)