From aebd85ffdce7ba809930bd7f902c803459d809eb Mon Sep 17 00:00:00 2001
From: leufen1 <l.leufen@fz-juelich.de>
Date: Wed, 24 Nov 2021 14:39:49 +0100
Subject: [PATCH] added more time tracking info to know more about
 postprocessing.

---
 mlair/helpers/time_tracking.py       |  4 +++-
 mlair/run_modules/post_processing.py | 12 +++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/mlair/helpers/time_tracking.py b/mlair/helpers/time_tracking.py
index 3105ebcd..cf366db8 100644
--- a/mlair/helpers/time_tracking.py
+++ b/mlair/helpers/time_tracking.py
@@ -68,12 +68,13 @@ class TimeTracking(object):
     The only disadvantage of the latter implementation is, that the duration is logged but not returned.
     """
 
-    def __init__(self, start=True, name="undefined job", logging_level=logging.INFO):
+    def __init__(self, start=True, name="undefined job", logging_level=logging.INFO, log_on_enter=False):
         """Construct time tracking and start if enabled."""
         self.start = None
         self.end = None
         self._name = name
         self._logging = {logging.INFO: logging.info, logging.DEBUG: logging.debug}.get(logging_level, logging.info)
+        self._log_on_enter = log_on_enter
         if start:
             self._start()
 
@@ -124,6 +125,7 @@ class TimeTracking(object):
 
     def __enter__(self):
         """Context manager."""
+        self._logging(f"start {self._name}") if self._log_on_enter is True else None
         return self
 
     def __exit__(self, exc_type, exc_val, exc_tb) -> None:
diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py
index dbffc5ca..3f20d7b5 100644
--- a/mlair/run_modules/post_processing.py
+++ b/mlair/run_modules/post_processing.py
@@ -18,7 +18,7 @@ import xarray as xr
 from mlair.configuration import path_config
 from mlair.data_handler import Bootstraps, KerasIterator
 from mlair.helpers.datastore import NameNotFoundInDataStore
-from mlair.helpers import TimeTracking, statistics, extract_value, remove_items, to_list, tables
+from mlair.helpers import TimeTracking, TimeTrackingWrapper, statistics, extract_value, remove_items, to_list, tables
 from mlair.model_modules.linear_model import OrdinaryLeastSquaredModel
 from mlair.model_modules import AbstractModelClass
 from mlair.plotting.postprocessing_plotting import PlotMonthlySummary, PlotClimatologicalSkillScore, \
@@ -114,17 +114,17 @@ class PostProcessing(RunEnvironment):
 
         # feature importance bootstraps
         if self.data_store.get("evaluate_feature_importance", "postprocessing"):
-            with TimeTracking(name="calculate feature importance using bootstraps"):
+            with TimeTracking(name="evaluate_feature_importance", log_on_enter=True):
                 create_new_bootstraps = self.data_store.get("create_new_bootstraps", "feature_importance")
                 bootstrap_method = self.data_store.get("bootstrap_method", "feature_importance")
                 bootstrap_type = self.data_store.get("bootstrap_type", "feature_importance")
                 self.calculate_feature_importance(create_new_bootstraps, bootstrap_type=bootstrap_type,
                                                   bootstrap_method=bootstrap_method)
-            if self.feature_importance_skill_scores is not None:
-                self.report_feature_importance_results(self.feature_importance_skill_scores)
+                if self.feature_importance_skill_scores is not None:
+                    self.report_feature_importance_results(self.feature_importance_skill_scores)
 
         # skill scores and error metrics
-        with TimeTracking(name="calculate skill scores"):
+        with TimeTracking(name="calculate_error_metrics", log_on_enter=True):
             skill_score_competitive, _, skill_score_climatological, errors = self.calculate_error_metrics()
             self.skill_scores = (skill_score_competitive, skill_score_climatological)
         self.report_error_metrics(errors)
@@ -134,12 +134,14 @@ class PostProcessing(RunEnvironment):
         # plotting
         self.plot()
 
+    @TimeTrackingWrapper
     def estimate_sample_uncertainty(self, separate_ahead=False):
         """
         Estimate sample uncertainty by using a bootstrap approach. Forecasts are split into individual blocks along time
         and randomly drawn with replacement. The resulting behaviour of the error indicates the robustness of each
         analyzed model to quantify which model might be superior compared to others.
         """
+        logging.info("start estimate_sample_uncertainty")
         n_boots = self.data_store.get_default("n_boots", default=1000, scope="uncertainty_estimate")
         block_length = self.data_store.get_default("block_length", default="1m", scope="uncertainty_estimate")
         evaluate_competitors = self.data_store.get_default("evaluate_competitors", default=True,
-- 
GitLab