Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Bug: _get_weights_and_dims doesn't work properly #91

Closed
andersy005 opened this issue Mar 10, 2019 · 2 comments
Closed

Bug: _get_weights_and_dims doesn't work properly #91

andersy005 opened this issue Mar 10, 2019 · 2 comments

Comments

@andersy005
Copy link
Contributor

Does anybody have a straightforward example of using the dim keyword on weighted_corr? I'm having issues with it. I'm testing the p-value function on, say, a lat/lon grid of data.

import numpy as np
import xarray as xr
import esmlab

base = np.random.rand(100,10,10)
x = xr.DataArray(np.random.rand(100,10,10) + base, dims=['time','lat','lon'])
y = xr.DataArray(np.random.rand(100,10,10) + base, dims=['time','lat','lon'])
# just looking to correlate over the 'time' dimension so a grid of correlation
# coefficients is returned.
r = esmlab.statistics.weighted_corr(x, y, dim=['time'], weights=None) 

Screen Shot 2019-03-09 at 9 03 48 PM

Originally posted by @bradyrx in #90 (comment)

@andersy005 andersy005 added the bug label Mar 10, 2019
@andersy005 andersy005 added this to the sprint-mar04-mar17 milestone Mar 10, 2019
@andersy005
Copy link
Contributor Author

It appears that the issue is in_apply_nan_mask(). This function assumes that all dimensions should be used:

weights = xr.DataArray(np.ones(dims_shape), dims=op_over_dims)
weights = _apply_nan_mask(weights, x, y)

def _apply_nan_mask(weights, x, y=None):
# If y is specified, make sure x and y have same shape
if y is not None and isinstance(y, xr.DataArray):
assert x.shape == y.shape
valid = x.notnull() & y.notnull()
else:
valid = x.notnull()
# Apply nan mask
return weights.where(valid)

As a consequence, the shape of the returned weights is always equal to the input array's shape, even when the user wants to apply the operation along a particular dimension.

To fix this, we will need to come up with a way of doing valid = x.notnull() while taking into consideration dimensions supplied by the user.

@andersy005
Copy link
Contributor Author

Fixed in #97

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant