diff --git a/mlair/configuration/defaults.py b/mlair/configuration/defaults.py index 5d70d9f0d76817591b92cdb5ccddd95ad0d5d006..4d762581ecc64ba4f4335d77a48bd1c1d913e01c 100644 --- a/mlair/configuration/defaults.py +++ b/mlair/configuration/defaults.py @@ -25,6 +25,7 @@ DEFAULT_PERMUTE_DATA = False DEFAULT_BATCH_SIZE = int(256 * 2) DEFAULT_EPOCHS = 20 DEFAULT_TARGET_VAR = "o3" +DEFAULT_TARGET_VAR_UNIT = "ppb" DEFAULT_TARGET_DIM = "variables" DEFAULT_WINDOW_LEAD_TIME = 3 DEFAULT_WINDOW_DIM = "window" diff --git a/mlair/plotting/postprocessing_plotting.py b/mlair/plotting/postprocessing_plotting.py index 4a8196056ddcfe856c279fd7108436deeba160a5..0d81764541eda54697248989aa9e55ae66ff6a5d 100644 --- a/mlair/plotting/postprocessing_plotting.py +++ b/mlair/plotting/postprocessing_plotting.py @@ -5,6 +5,7 @@ __date__ = '2020-11-23' import logging import math import os +import sys import warnings from typing import Dict, List, Tuple @@ -171,11 +172,13 @@ class PlotConditionalQuantiles(AbstractPlotClass): # pragma: no cover warnings.filterwarnings("ignore", message="Attempted to set non-positive bottom ylim on a log-scaled axis.") def __init__(self, stations: List, data_pred_path: str, plot_folder: str = ".", plot_per_seasons=True, - rolling_window: int = 3, model_name: str = "nn", obs_name: str = "obs", **kwargs): + rolling_window: int = 3, model_name: str = "nn", obs_name: str = "obs", target_var_unit: str = "ppb", + **kwargs): """Initialise.""" super().__init__(plot_folder, "conditional_quantiles") self._data_pred_path = data_pred_path self._stations = stations + self.target_var_unit = target_var_unit self._rolling_window = rolling_window self._model_name = model_name self._obs_name = obs_name @@ -185,14 +188,13 @@ class PlotConditionalQuantiles(AbstractPlotClass): # pragma: no cover self._bins = self._get_bins_from_rage_of_data() self._plot() - @staticmethod - def _get_opts(kwargs): + def _get_opts(self, kwargs): """Extract options from kwargs.""" return {"q": kwargs.get("q", [.1, .25, .5, .75, .9]), "linetype": kwargs.get("linetype", [':', '-.', '--', '-.', ':']), "legend": kwargs.get("legend", ['.10th and .90th quantile', '.25th and .75th quantile', '.50th quantile', 'reference 1:1']), - "data_unit": kwargs.get("data_unit", "ppb"), } + "data_unit": kwargs.get("data_unit", self.target_var_unit), } def _load_data(self) -> xr.DataArray: """ @@ -312,10 +314,20 @@ class PlotConditionalQuantiles(AbstractPlotClass): # pragma: no cover def _plot_seasons(self): """Create seasonal plots.""" for season in self._seasons: - self._plot_base(data=self._data.where(self._data['index.season'] == season), x_model=self._model_name, - y_model=self._obs_name, plot_name_affix="cali-ref", season=season) - self._plot_base(data=self._data.where(self._data['index.season'] == season), x_model=self._obs_name, - y_model=self._model_name, plot_name_affix="like-base", season=season) + try: + self._plot_base(data=self._data.where(self._data['index.season'] == season), x_model=self._model_name, + y_model=self._obs_name, plot_name_affix="cali-ref", season=season) + except Exception as e: + logging.error(f"Could not create plot PlotConditionalQuantiles._plot_seasons: {season}, cali-ref" + f" due to the following error:" + f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}") + try: + self._plot_base(data=self._data.where(self._data['index.season'] == season), x_model=self._obs_name, + y_model=self._model_name, plot_name_affix="like-base", season=season) + except Exception as e: + logging.error(f"Could not create plot PlotConditionalQuantiles._plot_seasons: {season}, like-base" + f" due to the following error:" + f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}") def _plot_all(self): """Plot overall conditional quantiles on full data.""" diff --git a/mlair/run_modules/experiment_setup.py b/mlair/run_modules/experiment_setup.py index 251cf9ac0dcbd524ae4ba6bd9d3910fbee2accb8..9f9e9bc02132991986802ee2ec75891e1910dbaf 100644 --- a/mlair/run_modules/experiment_setup.py +++ b/mlair/run_modules/experiment_setup.py @@ -23,7 +23,7 @@ from mlair.configuration.defaults import DEFAULT_STATIONS, DEFAULT_VAR_ALL_DICT, DEFAULT_USE_MULTIPROCESSING, DEFAULT_USE_MULTIPROCESSING_ON_DEBUG, DEFAULT_MAX_NUMBER_MULTIPROCESSING, \ DEFAULT_FEATURE_IMPORTANCE_BOOTSTRAP_TYPE, DEFAULT_FEATURE_IMPORTANCE_BOOTSTRAP_METHOD, DEFAULT_OVERWRITE_LAZY_DATA, \ DEFAULT_UNCERTAINTY_ESTIMATE_BLOCK_LENGTH, DEFAULT_UNCERTAINTY_ESTIMATE_EVALUATE_COMPETITORS, \ - DEFAULT_UNCERTAINTY_ESTIMATE_N_BOOTS, DEFAULT_DO_UNCERTAINTY_ESTIMATE + DEFAULT_UNCERTAINTY_ESTIMATE_N_BOOTS, DEFAULT_DO_UNCERTAINTY_ESTIMATE, DEFAULT_TARGET_VAR_UNIT # <<<<<<< HEAD # DEFAULT_BOOTSTRAP_TYPE, DEFAULT_BOOTSTRAP_METHOD, DEFAULT_OVERWRITE_LAZY_DATA # @@ -232,7 +232,7 @@ class ExperimentSetup(RunEnvironment): max_number_multiprocessing: int = None, start_script: Union[Callable, str] = None, overwrite_lazy_data: bool = None, uncertainty_estimate_block_length: str = None, uncertainty_estimate_evaluate_competitors: bool = None, uncertainty_estimate_n_boots: int = None, - do_uncertainty_estimate: bool = None, **kwargs): + do_uncertainty_estimate: bool = None, target_var_unit: str = None, **kwargs): # create run framework super().__init__() @@ -329,6 +329,7 @@ class ExperimentSetup(RunEnvironment): self._set_param("target_var", target_var, default=DEFAULT_TARGET_VAR) self._set_param("target_dim", target_dim, default=DEFAULT_TARGET_DIM) self._set_param("window_lead_time", window_lead_time, default=DEFAULT_WINDOW_LEAD_TIME) + self._set_param("target_var_unit", target_var_unit, default=DEFAULT_TARGET_VAR_UNIT) # interpolation self._set_param("dimensions", dimensions, default=DEFAULT_DIMENSIONS) diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py index 7c42b9a34c75e332bb18b8ac465486f4eaa189a7..e148c9f1bef8b0d8b5fd14686dc10588d01c8b58 100644 --- a/mlair/run_modules/post_processing.py +++ b/mlair/run_modules/post_processing.py @@ -81,6 +81,7 @@ class PostProcessing(RunEnvironment): self.train_val_data = self.data_store.get("data_collection", "train_val") self.plot_path: str = self.data_store.get("plot_path") self.target_var = self.data_store.get("target_var") + self.target_var_unit = self.data_store.get("target_var_unit") self._sampling = self.data_store.get("sampling") self.window_lead_time = extract_value(self.data_store.get("output_shape", "model")) self.skill_scores = None @@ -484,7 +485,8 @@ class PostProcessing(RunEnvironment): try: if "PlotConditionalQuantiles" in plot_list: - PlotConditionalQuantiles(self.test_data.keys(), data_pred_path=path, plot_folder=self.plot_path) + PlotConditionalQuantiles(self.test_data.keys(), data_pred_path=path, plot_folder=self.plot_path, + target_var_unit=self.target_var_unit) except Exception as e: logging.error(f"Could not create plot PlotConditionalQuantiles due to the following error:" f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}") @@ -492,7 +494,7 @@ class PostProcessing(RunEnvironment): 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) + plot_folder=self.plot_path, target_var_unit=self.target_var_unit) except Exception as e: logging.error(f"Could not create plot PlotMonthlySummary due to the following error:" f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}") @@ -585,7 +587,7 @@ class PostProcessing(RunEnvironment): 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", - error_unit=r"ppb$^2$", block_length=block_length) + error_unit=fr"{self.target_var_unit}$^2$", block_length=block_length) except Exception as e: logging.error(f"Could not create plot PlotSampleUncertaintyFromBootstrap due to the following error: {e}" f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}")