Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'n' component needs to be a numpy array with retained dimensions #174

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions activestorage/active.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,39 +385,38 @@ def _from_storage(self, stripped_indexer, drop_axes, out_shape, out_dtype,
if method is not None:
# Apply the method (again) to aggregate the result
out = method(out)

shape1 = (1,) * len(out_shape)

if self._components:
# Return a dictionary of components containing the
# reduced data and the sample size ('n'). (Rationale:
# cf-python needs the sample size for all reductions;
# see the 'mtol' parameter of cf.Field.collapse.)
#
# Note that in this case the reduced data must always
# have the same number of dimensions as the original
# array, i.e. 'drop_axes' is always considered False,
# Note that in all components must always have the
# same number of dimensions as the original array,
# i.e. 'drop_axes' is always considered False,
# regardless of its setting. (Rationale: dask
# reductions require the per-dask-chunk partial
# reductions to retain these dimensions so that
# partial results can be concatenated correctly.)
n = np.prod(out_shape)
shape1 = (1,) * len(out_shape)
n = np.reshape(n, shape1)
out = out.reshape(shape1)

n = np.sum(counts).reshape(shape1)
if self._method == "mean":
# For the average, the returned component is
# "sum", not "mean"
out = {"sum": out, "n": sum(counts)}
out = {"sum": out, "n": n}
else:
out = {self._method: out, "n": sum(counts)}
out = {self._method: out, "n": n}
else:
# Return the reduced data as a numpy array. For most
# methods the data is already in this form.
if self._method == "mean":
# For the average, it is actually the sum that has
# been created, so we need to divide by the sample
# size.
out = out / sum(counts)
out = out / np.sum(counts).reshape(shape1)

return out

Expand Down
Loading