diff --git a/src/statistics.py b/src/statistics.py index 5a3c4a65ae1215affad582f11e3188e73483f031..060081de9e21f5cbc7c560066451bbdbf14b7eb1 100644 --- a/src/statistics.py +++ b/src/statistics.py @@ -21,10 +21,20 @@ def standardise(data: Data, dim: Union[str, int]) -> Tuple[Data, Data, Data]: #. std: Standard deviation of data #. data: Standardised data """ - return data.mean(dim), data.std(dim), (data - data.mean(dim)) / data.std(dim) +def standardise_inverse(data: Data, mean: Data, std: Data) -> Data: + """ + This is the inverse function of `standardise` and therefore vanishes the standardising. + :param data: + :param mean: + :param std: + :return: + """ + return data * std + mean + + def centre(data: Data, dim: Union[str, int]) -> Tuple[Data, None, Data]: """ This function centres a xarray.dataarray (along dim) or pandas.DataFrame (along axis) to mean=0 @@ -37,5 +47,14 @@ def centre(data: Data, dim: Union[str, int]) -> Tuple[Data, None, Data]: #. std: Standard deviation of data #. data: Standardised data """ - return data.mean(dim), None, data - data.mean(dim) + + +def centre_inverse(data: Data, mean: Data) -> Data: + """ + This function is the inverse function of `centre` and therefore adds the given values of mean to the data. + :param data: + :param mean: + :return: + """ + return data + mean diff --git a/test/test_statistics.py b/test/test_statistics.py index 518d817fa1b2ca358ef2a260cb1cca78f572ca0c..d31f4e9919da27e679019b892d98557dee9a7f1d 100644 --- a/test/test_statistics.py +++ b/test/test_statistics.py @@ -2,7 +2,7 @@ import pytest import xarray as xr import pandas as pd import numpy as np -from src.statistics import standardise, centre +from src.statistics import standardise, standardise_inverse, centre, centre_inverse @pytest.fixture(scope='module') @@ -24,7 +24,8 @@ def xarray(input_data): class TestStandardise: - @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), (pytest.lazy_fixture('xarray'), 'index')]) + @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), + (pytest.lazy_fixture('xarray'), 'index')]) def test_standardise(self, data_org, dim): mean, std, data = standardise(data_org, dim) assert np.testing.assert_almost_equal(mean, [2, -5, 10], decimal=1) is None @@ -32,12 +33,28 @@ class TestStandardise: assert np.testing.assert_almost_equal(data.mean(dim), [0, 0, 0]) is None assert np.testing.assert_almost_equal(data.std(dim), [1, 1, 1]) is None + @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), + (pytest.lazy_fixture('xarray'), 'index')]) + def test_standardise_inverse(self, data_org, dim): + mean, std, data = standardise(data_org, dim) + data_recovered = standardise_inverse(data, mean, std) + assert np.testing.assert_array_almost_equal(data_org, data_recovered) is None + class TestCentre: - @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), (pytest.lazy_fixture('xarray'), 'index')]) + @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), + (pytest.lazy_fixture('xarray'), 'index')]) def test_centre(self, data_org, dim): mean, std, data = centre(data_org, dim) assert np.testing.assert_almost_equal(mean, [2, -5, 10], decimal=1) is None assert std is None assert np.testing.assert_almost_equal(data.mean(dim), [0, 0, 0]) is None + + @pytest.mark.parametrize('data_org, dim', [(pytest.lazy_fixture('pandas'), 0), + (pytest.lazy_fixture('xarray'), 'index')]) + def test_centre_inverse(self, data_org, dim): + mean, _, data = centre(data_org, dim) + data_recovered = centre_inverse(data, mean) + assert np.testing.assert_array_almost_equal(data_org, data_recovered) is None +