Skip to content

weighted_mean() assumes dims order, and raises AttributeError if weights=None for node-centered data #1800

Closed

Description

@Sevans711

Version

v2026.09.1

How did you install UXarray?

Source

What happened?

Bug 1: uxda.weighted_mean() assumes, without checking, that the geometry dimension is the last dimension of the array. This is often true but does not need to be. One of the primary benefits of xarray is that dimensions are referenced by name instead of order, and that dimension order should never affect the values of results. This leads to (Bug 1a) ValueError in the most common case (last dimension's size != weights.size), and (Bug 1b) silently incorrect results with wrong dimensions in a more pathological case (last dimension's size == weights.size).

Bug 2: weighted_mean's docstring promises that, when weights=None and dealing with data that isn't face-centered or edge-centered, a warning will be raised and an unweighted mean will be returned instead. The actual current behavior is that a warning is raised and then the function raises AttributeError: 'NoneType' object has no attribute 'sum'.

What did you expect to happen?

Bug 1: I expected weighted_mean() results to be independent of dimension order.

Bug 2: I expected the behavior to at least match the promise from the docstring. Given that it already crashes in this case though, I would prefer to update the behavior and docstring to instead just raise DataCenteringError when weights=None for node-centered data.

Can you provide a MCVE to repoduce the bug?

import uxarray as ux

## bug 1 setup
arr = ux.tutorial.open_dataset("outCSne30-timeseries")['psi']
correct_result = arr.weighted_mean()  # this works just fine!
# UxDataArray with "time" dim with size 6.

## bug 1a
arr.transpose('n_face', 'time').weighted_mean()  # this crashes:
# ValueError: operands could not be broadcast together with shapes (5400,6) (5400,)

## bug 1b
arr.transpose('n_face', 'time').isel(n_face=slice(6)).weighted_mean()
# UxDataArray with "n_face" dim with size 6, with values != correct_result

## bug 2
arr = ux.tutorial.open_dataset('quad-hexagon-random-node')['random_data_node']
arr.weighted_mean()
# --> UserWarning: Attempting to perform a weighted mean calculation on a variable
# that does not haveassociated weights. Weighted mean is only supported for
# face or edge centered variables. Performing an unweighted mean.
# --> AttributeError: 'NoneType' object has no attribute 'sum'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Fields

Priority

Low

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions