-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from ESMValGroup/detrend
Add detrend preprocessor function
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Preprocessor functions that remove trends from the data.""" | ||
import logging | ||
|
||
import dask.array as da | ||
import scipy.signal | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def detrend(cube, dimension='time', method='linear'): | ||
""" | ||
Detrend data along a given dimension. | ||
Parameters | ||
---------- | ||
cube: iris.cube.Cube | ||
input cube. | ||
dimension: str | ||
Dimension to detrend | ||
method: str | ||
Method to detrend. Available: linear, constant. See documentation of | ||
'scipy.signal.detrend' for details | ||
Returns | ||
------- | ||
iris.cube.Cube | ||
Detrended cube | ||
""" | ||
coord = cube.coord(dimension) | ||
axis = cube.coord_dims(coord)[0] | ||
detrended = da.apply_along_axis( | ||
scipy.signal.detrend, | ||
axis=axis, | ||
arr=cube.lazy_data(), | ||
type=method, | ||
shape=(cube.shape[axis],) | ||
) | ||
return cube.copy(detrended) |
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,57 @@ | ||
"""Unit tests for the :func:`esmvalcore.preprocessor._detrend` module.""" | ||
|
||
import unittest | ||
|
||
import iris | ||
import iris.coords | ||
from iris.cube import Cube | ||
import numpy as np | ||
import pytest | ||
from cf_units import Unit | ||
|
||
from numpy.testing import assert_array_almost_equal | ||
|
||
from esmvalcore.preprocessor._detrend import detrend | ||
|
||
|
||
def _create_sample_cube(): | ||
cube = Cube( | ||
np.array((np.arange(1, 25), np.arange(25, 49))), | ||
var_name='co2', | ||
units='J' | ||
) | ||
cube.add_dim_coord( | ||
iris.coords.DimCoord( | ||
np.arange(15., 720., 30.), | ||
standard_name='time', | ||
units=Unit('days since 1950-01-01 00:00:00', calendar='gregorian'), | ||
), | ||
1, | ||
) | ||
cube.add_dim_coord( | ||
iris.coords.DimCoord( | ||
np.arange(1, 3), | ||
standard_name='latitude', | ||
), | ||
0, | ||
) | ||
return cube | ||
|
||
|
||
@pytest.mark.parametrize('method', ['linear', 'constant']) | ||
def test_decadal_average(method): | ||
"""Test for decadal average.""" | ||
cube = _create_sample_cube() | ||
|
||
result = detrend(cube, 'time', method) | ||
if method == 'linear': | ||
expected = np.zeros([2, 24]) | ||
else: | ||
expected = np.array( | ||
(np.arange(1, 25) - 12.5, np.arange(25, 49) - 36.5) | ||
) | ||
assert_array_almost_equal(result.data, expected) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |