From 02643eb3e7b68fc5c389ac0c5fbd66e778a99a95 Mon Sep 17 00:00:00 2001
From: lukas leufen <l.leufen@fz-juelich.de>
Date: Thu, 26 Mar 2020 11:43:11 +0100
Subject: [PATCH] corrected bug in variable sorting

---
 src/data_handling/bootstraps.py         | 2 +-
 src/plotting/postprocessing_plotting.py | 5 +++--
 src/run_modules/post_processing.py      | 7 ++++---
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/data_handling/bootstraps.py b/src/data_handling/bootstraps.py
index f68532da..0d2eb7ef 100644
--- a/src/data_handling/bootstraps.py
+++ b/src/data_handling/bootstraps.py
@@ -42,7 +42,7 @@ class BootStrapGenerator:
                     shuffled_var = shuffled_data.sel(variables=var, boots=boot).expand_dims("variables").drop("boots")
                     shuffled_var = shuffled_var.transpose("datetime", "window", "Stations", "variables")
                     boot_hist = boot_hist.combine_first(shuffled_var)
-                    boot_hist = boot_hist.sortby("variables")
+                    boot_hist = boot_hist.reindex_like(hist)
                     yield boot_hist
                 return
 
diff --git a/src/plotting/postprocessing_plotting.py b/src/plotting/postprocessing_plotting.py
index 85418261..3338ce4c 100644
--- a/src/plotting/postprocessing_plotting.py
+++ b/src/plotting/postprocessing_plotting.py
@@ -497,6 +497,7 @@ class PlotBootstrapSkillScore(RunEnvironment):
         """
         super().__init__()
         self._labels = None
+        self._x_name = "boot_var"
         self._data = self._prepare_data(data)
         self._plot(plot_folder, model_setup)
 
@@ -507,7 +508,7 @@ class PlotBootstrapSkillScore(RunEnvironment):
         :param data: dictionary with station names as keys and 2D xarrays as values
         :return: pre-processed data set
         """
-        data = helpers.dict_to_xarray(data, "station")
+        data = helpers.dict_to_xarray(data, "station").sortby(self._x_name)
         self._labels = [str(i) + "d" for i in data.coords["ahead"].values]
         return data.to_dataframe("data").reset_index(level=[0, 1, 2])
 
@@ -526,7 +527,7 @@ class PlotBootstrapSkillScore(RunEnvironment):
         :param model_setup: architecture type to specify plot name
         """
         fig, ax = plt.subplots()
-        sns.boxplot(x="boot_var", y="data", hue="ahead", data=self._data, ax=ax, whis=1., palette="Blues_d",
+        sns.boxplot(x=self._x_name, y="data", hue="ahead", data=self._data, ax=ax, whis=1., palette="Blues_d",
                     showmeans=True, meanprops={"markersize": 1, "markeredgecolor": "k"}, flierprops={"marker": "."})
         ax.axhline(y=0, color="grey", linewidth=.5)
         ax.set(ylabel=f"skill score", xlabel="", title="summary of all stations")
diff --git a/src/run_modules/post_processing.py b/src/run_modules/post_processing.py
index 01822c3d..aa7cce0e 100644
--- a/src/run_modules/post_processing.py
+++ b/src/run_modules/post_processing.py
@@ -62,7 +62,8 @@ class PostProcessing(RunEnvironment):
                 self.bootstrap_postprocessing(create_new_bootstraps)
 
         # skill scores
-        self.skill_scores = self.calculate_skill_scores()
+        with TimeTracking(name="calculate skill scores"):
+            self.skill_scores = self.calculate_skill_scores()
 
         # plotting
         self.plot()
@@ -195,6 +196,8 @@ class PostProcessing(RunEnvironment):
 
         plot_list = self.data_store.get("plot_list", "postprocessing")
 
+        if self.bootstrap_skill_scores is not None and "PlotBootstrapSkillScore" in plot_list:
+            PlotBootstrapSkillScore(self.bootstrap_skill_scores, plot_folder=self.plot_path, model_setup="CNN")
         if "plot_conditional_quantiles" in plot_list:
             plot_conditional_quantiles(self.test_data.stations, pred_name="CNN", ref_name="obs",
                                        forecast_path=path, plot_name_affix="cali-ref", plot_folder=self.plot_path)
@@ -211,8 +214,6 @@ class PostProcessing(RunEnvironment):
                                          extra_name_tag="all_terms_", model_setup="CNN")
         if "PlotCompetitiveSkillScore" in plot_list:
             PlotCompetitiveSkillScore(self.skill_scores[0], plot_folder=self.plot_path, model_setup="CNN")
-        if self.bootstrap_skill_scores is not None and "PlotBootstrapSkillScore" in plot_list:
-            PlotBootstrapSkillScore(self.bootstrap_skill_scores, plot_folder=self.plot_path, model_setup="CNN")
         if "PlotTimeSeries" in plot_list:
             PlotTimeSeries(self.test_data.stations, path, r"forecasts_%s_test.nc", plot_folder=self.plot_path,
                            sampling=self._sampling)
-- 
GitLab