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

new method sort_like to ensure ordering when extracting meteo and chem variables

parent df0731ff
Branches
Tags
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 #88566 passed
...@@ -7,7 +7,7 @@ from mlair.data_handler.data_handler_with_filter import DataHandlerFirFilterSing ...@@ -7,7 +7,7 @@ from mlair.data_handler.data_handler_with_filter import DataHandlerFirFilterSing
from mlair.data_handler.data_handler_with_filter import DataHandlerClimateFirFilter, DataHandlerFirFilter from mlair.data_handler.data_handler_with_filter import DataHandlerClimateFirFilter, DataHandlerFirFilter
from mlair.data_handler import DefaultDataHandler from mlair.data_handler import DefaultDataHandler
from mlair import helpers from mlair import helpers
from mlair.helpers import to_list from mlair.helpers import to_list, sort_like
from mlair.configuration.defaults import DEFAULT_SAMPLING, DEFAULT_INTERPOLATION_LIMIT, DEFAULT_INTERPOLATION_METHOD from mlair.configuration.defaults import DEFAULT_SAMPLING, DEFAULT_INTERPOLATION_LIMIT, DEFAULT_INTERPOLATION_METHOD
from mlair.helpers.filter import filter_width_kzf from mlair.helpers.filter import filter_width_kzf
...@@ -340,7 +340,7 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi ...@@ -340,7 +340,7 @@ class DataHandlerMixedSamplingWithClimateAndFirFilter(DataHandlerMixedSamplingWi
chem_vars = cls.data_handler_climate_fir.chem_vars chem_vars = cls.data_handler_climate_fir.chem_vars
chem = set(variables).intersection(chem_vars) chem = set(variables).intersection(chem_vars)
meteo = set(variables).difference(chem_vars) meteo = set(variables).difference(chem_vars)
return to_list(chem), to_list(meteo) return sort_like(to_list(chem), variables), sort_like(to_list(meteo), variables)
@classmethod @classmethod
def build(cls, station: str, **kwargs): def build(cls, station: str, **kwargs):
......
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
from .testing import PyTestRegex, PyTestAllEqual from .testing import PyTestRegex, PyTestAllEqual
from .time_tracking import TimeTracking, TimeTrackingWrapper from .time_tracking import TimeTracking, TimeTrackingWrapper
from .logger import Logger from .logger import Logger
from .helpers import remove_items, float_round, dict_to_xarray, to_list, extract_value, select_from_dict, make_keras_pickable from .helpers import remove_items, float_round, dict_to_xarray, to_list, extract_value, select_from_dict, make_keras_pickable, sort_like
...@@ -64,6 +64,23 @@ def to_list(obj: Any) -> List: ...@@ -64,6 +64,23 @@ def to_list(obj: Any) -> List:
return obj return obj
def sort_like(list_obj: list, sorted_obj: list):
"""
Sort elements of list_obj as ordered in sorted_obj. Length of sorted_obj as allowed to be higher than length of
list_obj, but must contain at least all objects of list_obj. Will raise AssertionError, if not all elements of
list_obj are also in sorted_obj. Also it is required for list_obj and sorted_obj to have only unique elements.
:param list_obj: list to sort
:param sorted_obj: list to use ordering from
:return: sorted list
"""
assert set(list_obj).issubset(sorted_obj)
assert len(set(list_obj)) == len(list_obj)
assert len(set(sorted_obj)) == len(sorted_obj)
return [e for e in sorted_obj if e in list_obj]
def dict_to_xarray(d: Dict, coordinate_name: str) -> xr.DataArray: def dict_to_xarray(d: Dict, coordinate_name: str) -> xr.DataArray:
""" """
Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>. Convert a dictionary of 2D-xarrays to single 3D-xarray. The name of new coordinate axis follows <coordinate_name>.
......
...@@ -12,7 +12,7 @@ import mock ...@@ -12,7 +12,7 @@ import mock
import pytest import pytest
import string import string
from mlair.helpers import to_list, dict_to_xarray, float_round, remove_items, extract_value, select_from_dict from mlair.helpers import to_list, dict_to_xarray, float_round, remove_items, extract_value, select_from_dict, sort_like
from mlair.helpers import PyTestRegex from mlair.helpers import PyTestRegex
from mlair.helpers import Logger, TimeTracking from mlair.helpers import Logger, TimeTracking
from mlair.helpers.helpers import is_xarray, convert2xrda from mlair.helpers.helpers import is_xarray, convert2xrda
...@@ -432,3 +432,25 @@ class TestConvert2xrDa: ...@@ -432,3 +432,25 @@ class TestConvert2xrDa:
e.value.args[0] e.value.args[0]
assert "`use_1d_default=True' is used with `arr' of type da.array. For da.arrays please pass" + \ assert "`use_1d_default=True' is used with `arr' of type da.array. For da.arrays please pass" + \
" `use_1d_default=False' and specify keywords for xr.DataArray via kwargs." in e.value.args[0] " `use_1d_default=False' and specify keywords for xr.DataArray via kwargs." in e.value.args[0]
class TestSortLike:
def test_sort_like(self):
l_obj = [1, 2, 3, 8, 4]
res = sort_like(l_obj, [1, 2, 3, 4, 5, 6, 7, 8])
assert res == [1, 2, 3, 4, 8]
assert l_obj == [1, 2, 3, 8, 4]
def test_sort_like_not_unique(self):
l_obj = [1, 2, 3, 8, 4, 3]
with pytest.raises(AssertionError) as e:
sort_like(l_obj, [1, 2, 3, 4, 5, 6, 7, 8])
l_obj = [1, 2, 3, 8, 4]
with pytest.raises(AssertionError) as e:
sort_like(l_obj, [1, 2, 3, 4, 5, 6, 7, 8, 5])
def test_sort_like_missing_element(self):
l_obj = [1, 2, 3, 8, 4]
with pytest.raises(AssertionError) as e:
sort_like(l_obj, [1, 2, 3, 5, 6, 7, 8])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment