diff --git a/mlair/data_handler/input_bootstraps.py b/mlair/data_handler/input_bootstraps.py index 187f09050bb39a953ac58c2b7fca54b6a207aed1..b8ad614f2317e804d415b23308df760f4dd8da7f 100644 --- a/mlair/data_handler/input_bootstraps.py +++ b/mlair/data_handler/input_bootstraps.py @@ -123,11 +123,12 @@ class BootstrapIteratorVariable(BootstrapIterator): _X = list(map(lambda x: x.expand_dims({self.boot_dim: range(nboot)}, axis=-1), _X)) _Y = _Y.expand_dims({self.boot_dim: range(nboot)}, axis=-1) for index in range(len(_X)): - single_variable = _X[index].sel({self._dimension: [dimension]}) - bootstrapped_variable = self.apply_bootstrap_method(single_variable.values) - bootstrapped_data = xr.DataArray(bootstrapped_variable, coords=single_variable.coords, - dims=single_variable.dims) - _X[index] = bootstrapped_data.combine_first(_X[index]).transpose(*_X[index].dims) + if dimension in _X[index].coords[self._dimension]: + single_variable = _X[index].sel({self._dimension: [dimension]}) + bootstrapped_variable = self.apply_bootstrap_method(single_variable.values) + bootstrapped_data = xr.DataArray(bootstrapped_variable, coords=single_variable.coords, + dims=single_variable.dims) + _X[index] = bootstrapped_data.combine_first(_X[index]).transpose(*_X[index].dims) self._position += 1 except IndexError: raise StopIteration() diff --git a/mlair/plotting/data_insight_plotting.py b/mlair/plotting/data_insight_plotting.py index 82ab4c88557c2ab9f4364607f18079ff1261f5e6..8dbf4fc4b088d10efe5208aebc8b46e50b75360c 100644 --- a/mlair/plotting/data_insight_plotting.py +++ b/mlair/plotting/data_insight_plotting.py @@ -867,31 +867,46 @@ def f_proc(var, d_var, f_index, time_dim="datetime", use_last_value=True): # pr def f_proc_2(g, m, pos, variables_dim, time_dim, f_index, use_last_value): # pragma: no cover + + # load lazy data + id_classes = list(filter(lambda x: "id_class" in x, dir(g))) + for id_cls_name in id_classes: + id_cls = getattr(g, id_cls_name) + if hasattr(id_cls, "lazy"): + id_cls.load_lazy() if id_cls.lazy is True else None + raw_data_single = dict() - if hasattr(g.id_class, "lazy"): - g.id_class.load_lazy() if g.id_class.lazy is True else None - if m == 0: - d = g.id_class._data - if d is None: - window_dim = g.id_class.window_dim - history = g.id_class.history - last_entry = history.coords[window_dim][-1] - d1 = history.sel({window_dim: last_entry}, drop=True) - label = g.id_class.label - first_entry = label.coords[window_dim][0] - d2 = label.sel({window_dim: first_entry}, drop=True) - d = (d1, d2) - else: - gd = g.id_class - filter_sel = {"filter": gd.input_data.coords["filter"][m - 1]} - d = (gd.input_data.sel(filter_sel), gd.target_data) - d = d[pos] if isinstance(d, tuple) else d - for var in d[variables_dim].values: - d_var = d.loc[{variables_dim: var}].squeeze().dropna(time_dim) - var_str, f, pgram = f_proc(var, d_var, f_index, use_last_value=use_last_value) - raw_data_single[var_str] = [(f, pgram)] - if hasattr(g.id_class, "lazy"): - g.id_class.clean_up() if g.id_class.lazy is True else None + for dh in list(filter(lambda x: "unfiltered" not in x, id_classes)): + current_cls = getattr(g, dh) + if m == 0: + d = current_cls._data + if d is None: + window_dim = current_cls.window_dim + history = current_cls.history + last_entry = history.coords[window_dim][-1] + d1 = history.sel({window_dim: last_entry}, drop=True) + label = current_cls.label + first_entry = label.coords[window_dim][0] + d2 = label.sel({window_dim: first_entry}, drop=True) + d = (d1, d2) + else: + filter_sel = {"filter": current_cls.input_data.coords["filter"][m - 1]} + d = (current_cls.input_data.sel(filter_sel), current_cls.target_data) + d = d[pos] if isinstance(d, tuple) else d + for var in d[variables_dim].values: + d_var = d.loc[{variables_dim: var}].squeeze().dropna(time_dim) + var_str, f, pgram = f_proc(var, d_var, f_index, use_last_value=use_last_value) + if var_str not in raw_data_single.keys(): + raw_data_single[var_str] = [(f, pgram)] + else: + raise KeyError(f"There are multiple pgrams for key {var_str}. Please check your data handler.") + + # perform clean up + for id_cls_name in id_classes: + id_cls = getattr(g, id_cls_name) + if hasattr(id_cls, "lazy"): + id_cls.clean_up() if id_cls.lazy is True else None + return raw_data_single diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py index c67645fbbe9dce184aca4522010b4929295fd2dd..5d687dc2c86cc3681dfaa6b103bd207593e969f6 100644 --- a/mlair/run_modules/post_processing.py +++ b/mlair/run_modules/post_processing.py @@ -285,13 +285,13 @@ class PostProcessing(RunEnvironment): boot_skill_score = self.calculate_feature_importance_skill_scores(bootstrap_type=boot_type, bootstrap_method=boot_method) self.feature_importance_skill_scores[boot_type][boot_method] = boot_skill_score - except (FileNotFoundError, ValueError): + except (FileNotFoundError, ValueError, OSError): if _iter != 0: - raise RuntimeError(f"calculate_feature_importance ({boot_type}, {boot_type}) was called for the " - f"2nd time. This means, that something internally goes wrong. Please check " - f"for possible errors") - logging.info(f"Could not load all files for feature importance ({boot_type}, {boot_type}), restart " - f"calculate_feature_importance with create_new_bootstraps=True.") + raise RuntimeError(f"calculate_feature_importance ({boot_type}, {boot_method}) was called for " + f"the 2nd time. This means, that something internally goes wrong. Please " + f"check for possible errors.") + logging.info(f"Could not load all files for feature importance ({boot_type}, {boot_method}), " + f"restart calculate_feature_importance with create_new_bootstraps=True.") self.calculate_feature_importance(True, _iter=1, bootstrap_type=boot_type, bootstrap_method=boot_method)