Skip to content
Snippets Groups Projects
Commit a2299e50 authored by lukas leufen's avatar lukas leufen
Browse files

implemented inverse transform and it's tests in statistics module

parent 608d219d
Branches
Tags
2 merge requests!6updated inception model and data prep class,!4data prep class
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment