diff --git a/mlair/configuration/defaults.py b/mlair/configuration/defaults.py index 9dc252b89455a3f18c995fa7992c51fbc1476a57..bff85e4727437d33b7c07228713405ad252e64ad 100644 --- a/mlair/configuration/defaults.py +++ b/mlair/configuration/defaults.py @@ -54,7 +54,8 @@ DEFAULT_BOOTSTRAP_TYPE = "singleinput" DEFAULT_BOOTSTRAP_METHOD = "shuffle" DEFAULT_PLOT_LIST = ["PlotMonthlySummary", "PlotStationMap", "PlotClimatologicalSkillScore", "PlotTimeSeries", "PlotCompetitiveSkillScore", "PlotBootstrapSkillScore", "PlotConditionalQuantiles", - "PlotAvailability", "PlotAvailabilityHistogram", "PlotDataHistogram", "PlotPeriodogram"] + "PlotAvailability", "PlotAvailabilityHistogram", "PlotDataHistogram", "PlotPeriodogram", + "PlotSampleUncertaintyFromBootstrap"] DEFAULT_SAMPLING = "daily" DEFAULT_DATA_ORIGIN = {"cloudcover": "REA", "humidity": "REA", "pblheight": "REA", "press": "REA", "relhum": "REA", "temp": "REA", "totprecip": "REA", "u": "REA", "v": "REA", "no": "", "no2": "", "o3": "", diff --git a/mlair/plotting/postprocessing_plotting.py b/mlair/plotting/postprocessing_plotting.py index 5e8b121ae26ed0d4a967b9c440071d61c1ad703b..ae531b2526dee93672ab989d621d92eba541f19f 100644 --- a/mlair/plotting/postprocessing_plotting.py +++ b/mlair/plotting/postprocessing_plotting.py @@ -998,6 +998,34 @@ class PlotSeparationOfScales(AbstractPlotClass): self._save() +class PlotSampleUncertaintyFromBootstrap(AbstractPlotClass): + + def __init__(self, data: xr.DataArray, plot_folder: str = ".", model_type_dim: str = "type", + error_measure: str = "mse", dim_name_boots: str = 'boots'): + plot_folder = os.path.join(plot_folder) + super().__init__(plot_folder, "sample_uncertainty_from_bootstrap") + self.model_type_dim = model_type_dim + self.error_measure = error_measure + self.dim_name_boots = dim_name_boots + + self._plot(data) + + def _plot(self, data): + data_table = data.to_pandas() + size = max([len(np.unique(data_table.columns)), 6]) + fig, ax = plt.subplots(figsize=(size, size * 0.8)) + # fig, ax = plt.subplots() + sns.boxplot(data=data_table, ax=ax, whis=1., color="white", + showmeans=True, meanprops={"markersize": 3, "markeredgecolor": "k"}, + flierprops={"marker": ".", "markerfacecolor": 'black', "markeredgecolor": 'none', "markersize": 1}, + width=.3) + ax.set_ylabel(f"{self.error_measure} " + r" in ppb$^2$") + ax.set_xticklabels(ax.get_xticklabels(), rotation=45) + plt.tight_layout() + self._save() +# a = xr.DataArray(np.array(range(20)).reshape(2,-1).T, dims={'time':range(10), 'model': ['m1', 'm2']}, coords={'time':range(10), 'model': ['m1', 'm2']}) +# create_n_bootstrap_realizations(a, dim_name_time='time', dim_name_model='model', n_boots=100) + if __name__ == "__main__": stations = ['DEBW107', 'DEBY081', 'DEBW013', 'DEBW076', 'DEBW087'] path = "../../testrun_network/forecasts" diff --git a/mlair/run_modules/post_processing.py b/mlair/run_modules/post_processing.py index e580a2b3acd04f4f8e82154d9e3f16000d75c84b..e9110b43273894365b7ef336f16096944a4a5c10 100644 --- a/mlair/run_modules/post_processing.py +++ b/mlair/run_modules/post_processing.py @@ -22,7 +22,8 @@ from mlair.helpers import TimeTracking, statistics, extract_value, remove_items, from mlair.model_modules.linear_model import OrdinaryLeastSquaredModel from mlair.model_modules import AbstractModelClass from mlair.plotting.postprocessing_plotting import PlotMonthlySummary, PlotClimatologicalSkillScore, \ - PlotCompetitiveSkillScore, PlotTimeSeries, PlotBootstrapSkillScore, PlotConditionalQuantiles, PlotSeparationOfScales + PlotCompetitiveSkillScore, PlotTimeSeries, PlotBootstrapSkillScore, PlotConditionalQuantiles, \ + PlotSeparationOfScales, PlotSampleUncertaintyFromBootstrap from mlair.plotting.data_insight_plotting import PlotStationMap, PlotAvailability, PlotAvailabilityHistogram, \ PlotPeriodogram, PlotDataHistogram from mlair.run_modules.run_environment import RunEnvironment @@ -535,6 +536,14 @@ class PostProcessing(RunEnvironment): logging.error(f"Could not create plot PlotDataHistogram due to the following error: {e}" f"\n{sys.exc_info()[0]}\n{sys.exc_info()[1]}\n{sys.exc_info()[2]}") + try: + if "PlotSampleUncertaintyFromBootstrap" in plot_list: + PlotSampleUncertaintyFromBootstrap(data=None, plot_folder=self.plot_path, + model_type_dim=self.model_type_dim) + 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]}") + def calculate_test_score(self): """Evaluate test score of model and save locally."""