diff --git a/mlair/plotting/postprocessing_plotting.py b/mlair/plotting/postprocessing_plotting.py index 92a327a6e1b3bf0da5314546cd83da80e87be6cf..43f1864f7354c1f711bb886f4f97eda56439ab89 100644 --- a/mlair/plotting/postprocessing_plotting.py +++ b/mlair/plotting/postprocessing_plotting.py @@ -631,17 +631,16 @@ class PlotFeatureImportanceSkillScore(AbstractPlotClass): # pragma: no cover self._boot_dim = boot_dim self._boot_type = self._set_bootstrap_type(bootstrap_type) self._boot_method = self._set_bootstrap_method(bootstrap_method) + self._number_of_bootstraps = 0 self._branches_names = branch_names self._ylim = ylim - title_d = {"single input": "Single Inputs", "branch": "Input Branches", "variable": "Variables"} - self._title = f"{model_name}\nImportance of {title_d[self._boot_type]}" self._data = self._prepare_data(data, sampling) + self._set_title(model_name) if "branch" in self._data.columns: plot_name = self.plot_name for branch in self._data["branch"].unique(): - branch_name = self._branches_names[branch] if self._branches_names is not None else branch - self._title = f"{model_name}\nImportance of {title_d[self._boot_type]} ({branch_name})" + self._set_title(model_name, branch) self._plot(branch=branch) self.plot_name = f"{plot_name}_{branch}" self._save() @@ -661,6 +660,21 @@ class PlotFeatureImportanceSkillScore(AbstractPlotClass): # pragma: no cover def _set_bootstrap_type(boot_type): return {"singleinput": "single input"}.get(boot_type, boot_type) + def _set_title(self, model_name, branch=None): + title_d = {"single input": "Single Inputs", "branch": "Input Branches", "variable": "Variables"} + base_title = f"{model_name}\nImportance of {title_d[self._boot_type]}" + + additional = [] + if branch is not None: + branch_name = self._branches_names[branch] if self._branches_names is not None else branch + additional.append(branch_name) + if self._number_of_bootstraps > 1: + additional.append(f"n={self._number_of_bootstraps}") + additional_title = ", ".join(additional) + if len(additional_title) > 0: + additional_title = f" ({additional_title})" + self._title = base_title + additional_title + @staticmethod def _set_bootstrap_method(boot_method): return {"zero_mean": "zero mean", "shuffle": "shuffled"}.get(boot_method, boot_method) @@ -698,6 +712,7 @@ class PlotFeatureImportanceSkillScore(AbstractPlotClass): # pragma: no cover self._labels = [str(i) + sampling_letter for i in data.coords[self._ahead_dim].values] if station_dim not in data.dims: data = data.expand_dims(station_dim) + self._number_of_bootstraps = np.unique(data.coords[self._boot_dim].values).shape[0] return data.to_dataframe("data").reset_index(level=np.arange(len(data.dims)).tolist()) @staticmethod diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py index 64ac980521361443283cab1532ba05ced13cb484..e3aa2154559622fdd699d430bc4d386499f5114d 100644 --- a/mlair/run_modules/post_processing.py +++ b/mlair/run_modules/post_processing.py @@ -272,7 +272,8 @@ class PostProcessing(RunEnvironment): if _iter == 0: self.feature_importance_skill_scores = {} for boot_type in to_list(bootstrap_type): - self.feature_importance_skill_scores[boot_type] = {} + if _iter == 0: + self.feature_importance_skill_scores[boot_type] = {} for boot_method in to_list(bootstrap_method): try: if create_new_bootstraps: @@ -281,7 +282,7 @@ 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: + except (FileNotFoundError, ValueError): 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 " @@ -387,8 +388,9 @@ class PostProcessing(RunEnvironment): skill_scores.general_skill_score(data, forecast_name=boot_var, reference_name=reference_name, dim=self.index_dim)) tmp = xr.DataArray(np.expand_dims(np.array(boot_scores), axis=-1), - coords=[range(1, self.window_lead_time + 1), range(number_of_bootstraps), - [boot_var]], + coords={self.ahead_dim: range(1, self.window_lead_time + 1), + self.uncertainty_estimate_boot_dim: range(number_of_bootstraps), + self.boot_var_dim: [boot_var]}, dims=[self.ahead_dim, self.uncertainty_estimate_boot_dim, self.boot_var_dim]) skill.append(tmp) @@ -585,7 +587,7 @@ class PostProcessing(RunEnvironment): try: if "PlotSampleUncertaintyFromBootstrap" in plot_list and self.uncertainty_estimate is not None: - block_length= self.data_store.get_default("block_length", default="1m", scope="uncertainty_estimate") + block_length = self.data_store.get_default("block_length", default="1m", scope="uncertainty_estimate") PlotSampleUncertaintyFromBootstrap( data=self.uncertainty_estimate, plot_folder=self.plot_path, model_type_dim=self.model_type_dim, dim_name_boots=self.uncertainty_estimate_boot_dim, error_measure="mean squared error",