diff --git a/test/test_helpers/test_statistics.py b/test/test_helpers/test_statistics.py index 2a77f0806b886bee1a3961ff0a972e8f0ee62873..bab76bd999d36efeeea5b83252e232c4d285dcd7 100644 --- a/test/test_helpers/test_statistics.py +++ b/test/test_helpers/test_statistics.py @@ -4,7 +4,8 @@ import pytest import xarray as xr from mlair.helpers.statistics import standardise, standardise_inverse, standardise_apply, centre, centre_inverse, \ - centre_apply, apply_inverse_transformation, min_max, min_max_inverse, min_max_apply, log, log_inverse, log_apply + centre_apply, apply_inverse_transformation, min_max, min_max_inverse, min_max_apply, log, log_inverse, log_apply, \ + create_single_bootstrap_realization, calculate_average, create_n_bootstrap_realizations lazy = pytest.lazy_fixture @@ -221,3 +222,47 @@ class TestLog: data_ref, opts = log(data_orig, dim) data_test = log_apply(data_orig, opts["mean"], opts["std"]) assert np.testing.assert_array_almost_equal(data_ref, data_test) is None + + +class TestCreateSingleBootstrapRealization: + + @pytest.fixture + def data(self): + return xr.DataArray(np.array(range(20)).reshape(2 , -1).T, + dims={'time': range(10), 'model': ['m1', 'm2']}, + coords={'time': range(10), 'model': ['m1', 'm2']}) + + def test_create_single_bootstrap_realization(self, data): + np.random.seed(42) + proc_data = create_single_bootstrap_realization(data, "time") + assert isinstance(proc_data, xr.DataArray) + assert (proc_data.coords['time'].values == np.array([6, 3, 7, 4, 6, 9, 2, 6, 7, 4])).all() + # check if all time index values of proc_data are from data + assert np.in1d(proc_data.indexes['time'].values, data.indexes['time'].values).all() + + def test_calculate_average(self, data): + assert isinstance(data, xr.DataArray) + assert calculate_average(data) == data.mean() + assert (calculate_average(data, axis=0) == data.mean(axis=0)).all() + + def test_create_n_bootstrap_realizations(self, data): + boot_data = create_n_bootstrap_realizations(data, dim_name_time='time', dim_name_model='model', + n_boots=1000, dim_name_boots='boots') + assert isinstance(boot_data, xr.DataArray) + assert boot_data.shape == (1000, 2) + + boot_data = create_n_bootstrap_realizations(data.sel(model='m1').squeeze(), dim_name_time='time', + dim_name_model='model', n_boots=1000, dim_name_boots='boots') + assert isinstance(boot_data, xr.DataArray) + assert boot_data.shape == (1000,) + + + + + +class TestCalculateAverage: + pass + + +class TestCreateNBootstrapRealizations: + pass \ No newline at end of file