You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
weighted_mean() assumes dims order, and raises AttributeError if weights=None for node-centered data #1800
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?
importuxarrayasux## bug 1 setuparr=ux.tutorial.open_dataset("outCSne30-timeseries")['psi']
correct_result=arr.weighted_mean() # this works just fine!# UxDataArray with "time" dim with size 6.## bug 1aarr.transpose('n_face', 'time').weighted_mean() # this crashes:# ValueError: operands could not be broadcast together with shapes (5400,6) (5400,)## bug 1barr.transpose('n_face', 'time').isel(n_face=slice(6)).weighted_mean()
# UxDataArray with "n_face" dim with size 6, with values != correct_result## bug 2arr=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'
Activity
Implements zonal and mean methods in UxDataset; fixes weighted_mean()…