Skip to content
Snippets Groups Projects
Commit df0731ff authored by leufen1's avatar leufen1
Browse files

update tests

parent 98944e17
No related branches found
No related tags found
4 merge requests!430update recent developments,!413update release branch,!412Resolve "release v2.0.0",!378Resolve "filter dh: differentiate between history offset and information offset"
Pipeline #88498 passed
...@@ -640,8 +640,7 @@ class ClimateFIRFilter(FIRFilter): ...@@ -640,8 +640,7 @@ class ClimateFIRFilter(FIRFilter):
@staticmethod @staticmethod
def _trim_data_to_minimum_length(data: xr.DataArray, extend_length_history: int, dim: str, def _trim_data_to_minimum_length(data: xr.DataArray, extend_length_history: int, dim: str,
minimum_length: int = None, extend_length_future: int = 0, extend_length_future: int = 0) -> xr.DataArray:
offset: int = 0) -> xr.DataArray:
""" """
Trim data along given axis between either -minimum_length (if given) or -extend_length_history and Trim data along given axis between either -minimum_length (if given) or -extend_length_history and
extend_length_opts (which is default set to 0). extend_length_opts (which is default set to 0).
...@@ -650,14 +649,9 @@ class ClimateFIRFilter(FIRFilter): ...@@ -650,14 +649,9 @@ class ClimateFIRFilter(FIRFilter):
:param extend_length_history: start number for trim range (transformed to negative), only used if parameter :param extend_length_history: start number for trim range (transformed to negative), only used if parameter
minimum_length is not provided minimum_length is not provided
:param dim: dim to apply trim on :param dim: dim to apply trim on
:param minimum_length: start number for trim range (transformed to negative), preferably used (default None)
:param extend_length_future: number to use in "future" :param extend_length_future: number to use in "future"
:returns: trimmed data :returns: trimmed data
""" """
# if minimum_length is None:
# return data.sel({dim: slice(-extend_length_history, extend_length_future)}, drop=True)
# else:
# return data.sel({dim: slice(-minimum_length + offset, extend_length_future)}, drop=True)
return data.sel({dim: slice(-extend_length_history, extend_length_future)}, drop=True) return data.sel({dim: slice(-extend_length_history, extend_length_future)}, drop=True)
@staticmethod @staticmethod
...@@ -749,13 +743,12 @@ class ClimateFIRFilter(FIRFilter): ...@@ -749,13 +743,12 @@ class ClimateFIRFilter(FIRFilter):
# trim data if required # trim data if required
valid_range_end = int(filt.coords[new_dim].max() - (length + 1) / 2) + 1 valid_range_end = int(filt.coords[new_dim].max() - (length + 1) / 2) + 1
trimmed = self._trim_data_to_minimum_length(filt, extend_length_history, new_dim, minimum_length + int((next_order + 1) / 2), ext_len = min(extend_length_future, valid_range_end)
extend_length_future=min(extend_length_future, valid_range_end), trimmed = self._trim_data_to_minimum_length(filt, extend_length_history, new_dim,
offset=offset) extend_length_future=ext_len)
filt_coll.append(trimmed) filt_coll.append(trimmed)
trimmed = self._trim_data_to_minimum_length(filter_input_data, extend_length_history, new_dim, minimum_length + int((next_order + 1) / 2), trimmed = self._trim_data_to_minimum_length(filter_input_data, extend_length_history, new_dim,
extend_length_future=min(extend_length_future, valid_range_end), extend_length_future=ext_len)
offset=offset)
filt_input_coll.append(trimmed) filt_input_coll.append(trimmed)
# visualization # visualization
......
...@@ -297,10 +297,11 @@ class TestClimateFIRFilter: ...@@ -297,10 +297,11 @@ class TestClimateFIRFilter:
res = obj._trim_data_to_minimum_length(xr_array, 5, "window") res = obj._trim_data_to_minimum_length(xr_array, 5, "window")
assert xr_array.shape == (21, 100, 1) assert xr_array.shape == (21, 100, 1)
assert res.shape == (6, 100, 1) assert res.shape == (6, 100, 1)
res = obj._trim_data_to_minimum_length(xr_array, 5, "window", 10)
assert res.shape == (11, 100, 1)
res = obj._trim_data_to_minimum_length(xr_array, 30, "window") res = obj._trim_data_to_minimum_length(xr_array, 30, "window")
assert res.shape == (21, 100, 1) assert res.shape == (21, 100, 1)
xr_array = obj._shift_data(xr_array.sel(window=0), range(-20, 5), time_dim, new_dim="window")
res = obj._trim_data_to_minimum_length(xr_array, 5, "window", extend_length_future=2)
assert res.shape == (8, 100, 1)
def test_create_full_filter_result_array(self, xr_array, time_dim): def test_create_full_filter_result_array(self, xr_array, time_dim):
obj = object.__new__(ClimateFIRFilter) obj = object.__new__(ClimateFIRFilter)
...@@ -320,7 +321,7 @@ class TestClimateFIRFilter: ...@@ -320,7 +321,7 @@ class TestClimateFIRFilter:
assert len(res) == 5 assert len(res) == 5
# check filter data properties # check filter data properties
assert res[0].shape == (*xr_array_long_with_var.shape, 24 + 2) assert res[0].shape == (*xr_array_long_with_var.shape, int(filter_order+1)/2 + 24 + 2)
assert res[0].dims == (*xr_array_long_with_var.dims, "window") assert res[0].dims == (*xr_array_long_with_var.dims, "window")
# check filter properties # check filter properties
...@@ -350,7 +351,7 @@ class TestClimateFIRFilter: ...@@ -350,7 +351,7 @@ class TestClimateFIRFilter:
var_dim=var_dim, new_dim="total_new_dim", window=("kaiser", 5), minimum_length=1000, var_dim=var_dim, new_dim="total_new_dim", window=("kaiser", 5), minimum_length=1000,
apriori=apriori, plot_dates=plot_dates) apriori=apriori, plot_dates=plot_dates)
assert res[0].shape == (*xr_array_long_with_var.shape, 1000 + 2) assert res[0].shape == (*xr_array_long_with_var.shape, int(10 * 24 + 1 + 1) / 2 + 1000 + 2)
assert res[0].dims == (*xr_array_long_with_var.dims, "total_new_dim") assert res[0].dims == (*xr_array_long_with_var.dims, "total_new_dim")
assert np.testing.assert_almost_equal( assert np.testing.assert_almost_equal(
res[2], obj._calculate_filter_coefficients(("kaiser", 5), filter_order, 0.05, 24)) is None res[2], obj._calculate_filter_coefficients(("kaiser", 5), filter_order, 0.05, 24)) is None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment