-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* return nan when summing across nans * add testing for temporal module * add pytest lazy fixture testing update test fixture
- Loading branch information
Showing
4 changed files
with
67 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import numpy as np | ||
import pytest | ||
import xarray as xr | ||
|
||
from esmtools.temporal import to_annual | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'dataset', | ||
( | ||
pytest.lazy_fixture('gridded_da_datetime'), | ||
pytest.lazy_fixture('gridded_da_cftime'), | ||
), | ||
) | ||
def test_to_annual(dataset): | ||
"""General checks that `to_annual` time conversion is working as expected.""" | ||
data = dataset() | ||
result = to_annual(data) | ||
assert result.notnull().all() | ||
assert 'year' in result.dims | ||
|
||
|
||
def test_to_annual_accuracy(ts_monthly_da): | ||
"""Tests that weighted sum correctly takes the annual mean.""" | ||
data = ts_monthly_da().isel(time=slice(0, 12)) | ||
MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||
manual_sum = [] | ||
for i in range(len(data)): | ||
manual_sum.append(data[i].values * MONTH_LENGTHS[i] / 365) | ||
expected = sum(manual_sum) | ||
actual = to_annual(data) | ||
assert np.abs(actual.values - expected) < 1e-5 | ||
|
||
|
||
def test_to_annual_retains_nans(gridded_da_landmask): | ||
"""Tests that `to_annual` function retains nans where the original dataset had nans | ||
.. note:: | ||
Previous versions of `esmtools` did not do this, since xarray automatically | ||
skips nans with the grouped sum operator, returning zeroes where there used | ||
to be nans. | ||
""" | ||
data = gridded_da_landmask | ||
data['time'] = xr.cftime_range( | ||
start='1990-01', freq='MS', periods=data['time'].size | ||
) | ||
result = to_annual(data) | ||
assert result.isel(lat=0, lon=0).isnull().all() |