From 4d79f00f4745a3cae33d9ba4ca6f82499d227998 Mon Sep 17 00:00:00 2001
From: leufen1 <l.leufen@fz-juelich.de>
Date: Thu, 10 Dec 2020 17:47:16 +0100
Subject: [PATCH] encapsulate all plot routines into a try except statement
 with a general Exception case

---
 mlair/run_modules/post_processing.py | 116 ++++++++++++++++++---------
 1 file changed, 77 insertions(+), 39 deletions(-)

diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py
index cb24ca3c..cd8ee266 100644
--- a/mlair/run_modules/post_processing.py
+++ b/mlair/run_modules/post_processing.py
@@ -239,6 +239,7 @@ class PostProcessing(RunEnvironment):
             model = keras.models.load_model(model_name, custom_objects=model_class.custom_objects)
         return model
 
+    # noinspection PyBroadException
     def plot(self):
         """
         Create all plots.
@@ -263,46 +264,83 @@ class PostProcessing(RunEnvironment):
         plot_list = self.data_store.get("plot_list", "postprocessing")
         time_dimension = self.data_store.get("time_dim")
 
-        if ("filter" in self.test_data[0].get_X(as_numpy=False)[0].coords) and ("PlotSeparationOfScales" in plot_list):
-            PlotSeparationOfScales(self.test_data, plot_folder=self.plot_path)
-
-        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 "PlotConditionalQuantiles" in plot_list:
-            PlotConditionalQuantiles(self.test_data.keys(), data_pred_path=path, plot_folder=self.plot_path)
-        if "PlotStationMap" in plot_list:
-            if self.data_store.get("hostname")[:2] in self.data_store.get("hpc_hosts") or self.data_store.get(
-                    "hostname")[:6] in self.data_store.get("hpc_hosts"):
-                logging.warning(
-                    f"Skip 'PlotStationMap` because running on a hpc node: {self.data_store.get('hostname')}")
-            else:
-                gens = [(self.train_data, {"marker": 5, "ms": 9}),
-                        (self.val_data, {"marker": 6, "ms": 9}),
-                        (self.test_data, {"marker": 4, "ms": 9})]
-                PlotStationMap(generators=gens, plot_folder=self.plot_path)
-                gens = [(self.train_val_data, {"marker": 8, "ms": 9}),
-                        (self.test_data, {"marker": 9, "ms": 9})]
-                PlotStationMap(generators=gens, plot_folder=self.plot_path, plot_name="station_map_var")
-        if "PlotMonthlySummary" in plot_list:
-            PlotMonthlySummary(self.test_data.keys(), path, r"forecasts_%s_test.nc", self.target_var,
-                               plot_folder=self.plot_path)
-        if "PlotClimatologicalSkillScore" in plot_list:
-            PlotClimatologicalSkillScore(self.skill_scores[1], plot_folder=self.plot_path, model_setup="CNN")
-            PlotClimatologicalSkillScore(self.skill_scores[1], plot_folder=self.plot_path, score_only=False,
-                                         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 "PlotTimeSeries" in plot_list:
-            PlotTimeSeries(self.test_data.keys(), path, r"forecasts_%s_test.nc", plot_folder=self.plot_path,
-                           sampling=self._sampling)
-        if "PlotAvailability" in plot_list:
-            avail_data = {"train": self.train_data, "val": self.val_data, "test": self.test_data}
-            PlotAvailability(avail_data, plot_folder=self.plot_path, time_dimension=time_dimension)
-        if "PlotAvailabilityHistogram" in plot_list:
-            avail_data = {"train": self.train_data, "val": self.val_data, "test": self.test_data}
-            PlotAvailabilityHistogram(avail_data, plot_folder=self.plot_path, )# time_dimension=time_dimension)
+        try:
+            if ("filter" in self.test_data[0].get_X(as_numpy=False)[0].coords) and (
+                    "PlotSeparationOfScales" in plot_list):
+                PlotSeparationOfScales(self.test_data, plot_folder=self.plot_path)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotSeparationOfScales due to the following error: {e}")
+
+        try:
+            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")
+        except Exception as e:
+            logging.error(f"Could not create plot PlotBootstrapSkillScore due to the following error: {e}")
+
+        try:
+            if "PlotConditionalQuantiles" in plot_list:
+                PlotConditionalQuantiles(self.test_data.keys(), data_pred_path=path, plot_folder=self.plot_path)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotConditionalQuantiles due to the following error: {e}")
+
+        try:
+            if "PlotStationMap" in plot_list:
+                if self.data_store.get("hostname")[:2] in self.data_store.get("hpc_hosts") or self.data_store.get(
+                        "hostname")[:6] in self.data_store.get("hpc_hosts"):
+                    logging.warning(
+                        f"Skip 'PlotStationMap` because running on a hpc node: {self.data_store.get('hostname')}")
+                else:
+                    gens = [(self.train_data, {"marker": 5, "ms": 9}),
+                            (self.val_data, {"marker": 6, "ms": 9}),
+                            (self.test_data, {"marker": 4, "ms": 9})]
+                    PlotStationMap(generators=gens, plot_folder=self.plot_path)
+                    gens = [(self.train_val_data, {"marker": 8, "ms": 9}),
+                            (self.test_data, {"marker": 9, "ms": 9})]
+                    PlotStationMap(generators=gens, plot_folder=self.plot_path, plot_name="station_map_var")
+        except Exception as e:
+            logging.error(f"Could not create plot PlotStationMap due to the following error: {e}")
+
+        try:
+            if "PlotMonthlySummary" in plot_list:
+                PlotMonthlySummary(self.test_data.keys(), path, r"forecasts_%s_test.nc", self.target_var,
+                                   plot_folder=self.plot_path)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotMonthlySummary due to the following error: {e}")
 
+        try:
+            if "PlotClimatologicalSkillScore" in plot_list:
+                PlotClimatologicalSkillScore(self.skill_scores[1], plot_folder=self.plot_path, model_setup="CNN")
+                PlotClimatologicalSkillScore(self.skill_scores[1], plot_folder=self.plot_path, score_only=False,
+                                             extra_name_tag="all_terms_", model_setup="CNN")
+        except Exception as e:
+            logging.error(f"Could not create plot PlotClimatologicalSkillScore due to the following error: {e}")
+
+        try:
+            if "PlotCompetitiveSkillScore" in plot_list:
+                PlotCompetitiveSkillScore(self.skill_scores[0], plot_folder=self.plot_path, model_setup="CNN")
+        except Exception as e:
+            logging.error(f"Could not create plot PlotCompetitiveSkillScore due to the following error: {e}")
+
+        try:
+            if "PlotTimeSeries" in plot_list:
+                PlotTimeSeries(self.test_data.keys(), path, r"forecasts_%s_test.nc", plot_folder=self.plot_path,
+                               sampling=self._sampling)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotTimeSeries due to the following error: {e}")
+
+        try:
+            if "PlotAvailability" in plot_list:
+                avail_data = {"train": self.train_data, "val": self.val_data, "test": self.test_data}
+                PlotAvailability(avail_data, plot_folder=self.plot_path, time_dimension=time_dimension)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotAvailability due to the following error: {e}")
+
+        try:
+            if "PlotAvailabilityHistogram" in plot_list:
+                avail_data = {"train": self.train_data, "val": self.val_data, "test": self.test_data}
+                PlotAvailabilityHistogram(avail_data, plot_folder=self.plot_path, )  # time_dimension=time_dimension)
+        except Exception as e:
+            logging.error(f"Could not create plot PlotAvailabilityHistogram due to the following error: {e}")
 
     def calculate_test_score(self):
         """Evaluate test score of model and save locally."""
-- 
GitLab