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

Distributed weighted average() along tuple of axes: shape of weights to match shape of input #1037

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions heat/core/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,12 @@ def average(
Axis or axes along which to average ``x``. The default,
``axis=None``, will average over all of the elements of the input array.
If axis is negative it counts from the last to the first axis.
#TODO Issue #351: If axis is a tuple of ints, averaging is performed on all of the axes
specified in the tuple instead of a single axis or all the axes as
before.
weights : DNDarray, optional
An array of weights associated with the values in ``x``. Each value in
``x`` contributes to the average according to its associated weight.
The weights array can either be 1D (in which case its length must be
the size of ``x`` along the given axis) or of the same shape as ``x``.
Weighted average over tuple axis requires weights array to be of the same shape as ``x``.
If ``weights=None``, then all data in ``x`` are assumed to have a
weight equal to one, the result is equivalent to :func:`mean`.
returned : bool, optional
Expand Down Expand Up @@ -269,7 +267,9 @@ def average(
if axis is None:
raise TypeError("Axis must be specified when shapes of x and weights differ.")
elif isinstance(axis, tuple):
raise NotImplementedError("Weighted average over tuple axis not implemented yet.")
raise ValueError(
"Weighted average over tuple axis requires weights to be of the same shape as x."
)
if weights.ndim != 1:
raise TypeError("1D weights expected when shapes of x and weights differ.")
if weights.gshape[0] != x.gshape[axis]:
Expand Down
2 changes: 1 addition & 1 deletion heat/core/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def test_average(self):
ht.average(random_5d, weights=random_weights.numpy(), axis=axis)
with self.assertRaises(TypeError):
ht.average(random_5d, weights=random_weights, axis=None)
with self.assertRaises(NotImplementedError):
with self.assertRaises(ValueError):
ht.average(random_5d, weights=random_weights, axis=(1, 2))
random_weights = ht.random.randn(random_5d.gshape[axis], random_5d.gshape[axis + 1])
with self.assertRaises(TypeError):
Expand Down