Skip to content
Snippets Groups Projects
Commit 8bf45b43 authored by v.gramlich1's avatar v.gramlich1
Browse files

Oversampling Plots combined in PlotOversampling method

parent 4ef91f30
No related branches found
No related tags found
1 merge request!302Draft: Resolve "Class-based Oversampling technique"
Pipeline #73227 failed
...@@ -20,70 +20,50 @@ from mlair.helpers import TimeTrackingWrapper, to_list ...@@ -20,70 +20,50 @@ from mlair.helpers import TimeTrackingWrapper, to_list
from mlair.plotting.abstract_plot_class import AbstractPlotClass from mlair.plotting.abstract_plot_class import AbstractPlotClass
@TimeTrackingWrapper @TimeTrackingWrapper
class PlotOversamplingHistogram(AbstractPlotClass): class PlotOversampling(AbstractPlotClass):
def __init__(self, Y, Y_extreme, bin_edges, plot_folder: str = ".", def __init__(self, Y, Y_extreme, bin_edges, oversampling_rates, plot_folder: str = ".",
plot_name="oversampling_histogram"): plot_names=["oversampling_histogram", "oversampling_density_histogram", "oversampling_rates",
"oversampling_rates_deviation"]):
super().__init__(plot_folder, plot_name) super().__init__(plot_folder, plot_names[0])
self._plot(Y, Y_extreme, bin_edges) Y_hist, Y_extreme_hist = self._plot_oversampling_histogram(Y, Y_extreme, bin_edges)
real_oversampling = Y_extreme_hist / Y_hist
self._save()
self.plot_name = plot_names[1]
self._plot_oversampling_density_histogram(Y, Y_extreme, bin_edges)
self._save()
self.plot_name = plot_names[2]
self._plot_oversampling_rates(oversampling_rates, real_oversampling)
self._save()
self.plot_name = plot_names[3]
self._plot_oversampling_rates_deviation(oversampling_rates, real_oversampling)
self._save() self._save()
def _plot(self, Y, Y_extreme, bin_edges): def _plot_oversampling_histogram(self, Y, Y_extreme, bin_edges):
fig, ax = plt.subplots(1, 1) fig, ax = plt.subplots(1, 1)
Y.plot.hist(bins=bin_edges, histtype="step", label="Before", ax=ax)[0] Y_hist = Y.plot.hist(bins=bin_edges, histtype="step", label="Before", ax=ax)[0]
Y_extreme.plot.hist(bins=bin_edges, histtype="step", label="After", ax=ax)[0] Y_extreme_hist = Y_extreme.plot.hist(bins=bin_edges, histtype="step", label="After", ax=ax)[0]
ax.set_title(f"Histogram before-after oversampling") ax.set_title(f"Histogram before-after oversampling")
ax.legend() ax.legend()
return Y_hist, Y_extreme_hist
def _plot_oversampling_density_histogram(self, Y, Y_extreme, bin_edges):
@TimeTrackingWrapper
class PlotOversamplingDensityHistogram(AbstractPlotClass):
def __init__(self, Y, Y_extreme, bin_edges, plot_folder: str = ".",
plot_name="oversampling_density_histogram"):
super().__init__(plot_folder, plot_name)
self._plot(Y, Y_extreme, bin_edges)
self._save()
def _plot(self, Y, Y_extreme, bin_edges):
fig, ax = plt.subplots(1, 1) fig, ax = plt.subplots(1, 1)
Y.plot.hist(bins=bin_edges, density=True, histtype="step", label="Before", ax=ax)[0] Y.plot.hist(bins=bin_edges, density=True, histtype="step", label="Before", ax=ax)[0]
Y_extreme.plot.hist(bins=bin_edges, density=True, histtype="step", label="After", ax=ax)[0] Y_extreme.plot.hist(bins=bin_edges, density=True, histtype="step", label="After", ax=ax)[0]
ax.set_title(f"Density Histogram before-after oversampling") ax.set_title(f"Density Histogram before-after oversampling")
ax.legend() ax.legend()
def _plot_oversampling_rates(self, oversampling_rates, real_oversampling):
@TimeTrackingWrapper
class PlotOversamplingRates(AbstractPlotClass):
def __init__(self, Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist, plot_folder: str = ".",
plot_name="oversampling_rates"):
super().__init__(plot_folder, plot_name)
self._plot(Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist)
self._save()
def _plot(self, Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist):
fig, ax = plt.subplots(1, 1) fig, ax = plt.subplots(1, 1)
real_oversampling = Y_extreme_hist[0] / Y_hist[0]
ax.plot(range(len(real_oversampling)), oversampling_rates, label="Desired oversampling_rates") ax.plot(range(len(real_oversampling)), oversampling_rates, label="Desired oversampling_rates")
ax.plot(range(len(real_oversampling)), real_oversampling, label="Actual Oversampling Rates") ax.plot(range(len(real_oversampling)), real_oversampling, label="Actual Oversampling Rates")
ax.set_title(f"Oversampling rates") ax.set_title(f"Oversampling rates")
ax.legend() ax.legend()
def _plot_oversampling_rates_deviation(self, oversampling_rates, real_oversampling):
@TimeTrackingWrapper
class PlotOversamplingRatesDeviation(AbstractPlotClass):
def __init__(self, Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist, plot_folder: str = ".",
plot_name="oversampling_rates_deviation"):
super().__init__(plot_folder, plot_name)
self._plot(Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist)
self._save()
def _plot(self, Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist):
fig, ax = plt.subplots(1, 1) fig, ax = plt.subplots(1, 1)
real_oversampling = Y_extreme_hist[0] / Y_hist[0]
ax.plot(range(len(real_oversampling)), real_oversampling / oversampling_rates, ax.plot(range(len(real_oversampling)), real_oversampling / oversampling_rates,
label="Actual/Desired Rate") label="Actual/Desired Rate")
ax.set_title(f"Deviation from desired oversampling rates") ax.set_title(f"Deviation from desired oversampling rates")
......
...@@ -22,8 +22,7 @@ from mlair.model_modules import AbstractModelClass ...@@ -22,8 +22,7 @@ from mlair.model_modules import AbstractModelClass
from mlair.plotting.postprocessing_plotting import PlotMonthlySummary, PlotClimatologicalSkillScore, \ from mlair.plotting.postprocessing_plotting import PlotMonthlySummary, PlotClimatologicalSkillScore, \
PlotCompetitiveSkillScore, PlotTimeSeries, PlotBootstrapSkillScore, PlotConditionalQuantiles, PlotSeparationOfScales PlotCompetitiveSkillScore, PlotTimeSeries, PlotBootstrapSkillScore, PlotConditionalQuantiles, PlotSeparationOfScales
from mlair.plotting.data_insight_plotting import PlotStationMap, PlotAvailability, PlotAvailabilityHistogram, \ from mlair.plotting.data_insight_plotting import PlotStationMap, PlotAvailability, PlotAvailabilityHistogram, \
PlotPeriodogram, PlotDataHistogram, PlotOversamplingHistogram, PlotOversamplingDensityHistogram, \ PlotPeriodogram, PlotDataHistogram, PlotOversampling
PlotOversamplingRates, PlotOversamplingRatesDeviation
from mlair.run_modules.run_environment import RunEnvironment from mlair.run_modules.run_environment import RunEnvironment
...@@ -313,14 +312,7 @@ class PostProcessing(RunEnvironment): ...@@ -313,14 +312,7 @@ class PostProcessing(RunEnvironment):
oversampling_rates = self.data_store.get('oversampling_rates_capped','train') oversampling_rates = self.data_store.get('oversampling_rates_capped','train')
Y = self.data_store.get('Oversampling_Y') Y = self.data_store.get('Oversampling_Y')
Y_extreme = self.data_store.get('Oversampling_Y_extreme') Y_extreme = self.data_store.get('Oversampling_Y_extreme')
Y_hist = Y.plot.hist(bins=bin_edges, histtype="step") PlotOversampling(Y, Y_extreme, bin_edges, oversampling_rates, plot_folder=self.plot_path)
Y_extreme_hist = Y_extreme.plot.hist(bins=bin_edges, histtype="step")
PlotOversamplingHistogram(Y, Y_extreme, bin_edges, plot_folder=self.plot_path)
PlotOversamplingDensityHistogram(Y, Y_extreme, bin_edges, plot_folder=self.plot_path)
PlotOversamplingRates(Y, Y_extreme, bin_edges, oversampling_rates, Y_hist, Y_extreme_hist,
plot_folder=self.plot_path)
PlotOversamplingRatesDeviation(Y, Y_extreme, bin_edges, oversampling_rates, Y_hist,
Y_extreme_hist, plot_folder=self.plot_path)
except Exception as e: except Exception as e:
logging.error(f"Could not create plot OversamplingPlots due to the following error: {e}") logging.error(f"Could not create plot OversamplingPlots due to the following error: {e}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment