diff --git a/experiment/simulation/abstract_simulation.py b/experiment/simulation/abstract_simulation.py index 3e99de35bc1a743721fae71f5d918fc3bdcac190..ecdc90113570956c651f4ca284f54d9c0ee933b7 100644 --- a/experiment/simulation/abstract_simulation.py +++ b/experiment/simulation/abstract_simulation.py @@ -13,6 +13,7 @@ import matplotlib.cm as cm import matplotlib.pyplot as plt import numpy as np import seaborn as sns +from numpy.linalg import LinAlgError from extreme_estimator.estimator.abstract_estimator import AbstractEstimator from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function import \ @@ -193,7 +194,11 @@ class AbstractSimulation(object): else: markersize = 10 ax.plot([data.mean()], [0], color=self.color, marker='o', markersize=markersize) - sns.kdeplot(data, bw=1, ax=ax, color=self.color, **display_kwargs).set(xlim=0) + try: + sns.kdeplot(data, bw=1, ax=ax, color=self.color, **display_kwargs).set(xlim=0) + except LinAlgError as e: + if 'singular_matrix' in e.__repr__(): + continue ax.legend() # X axis @@ -242,6 +247,7 @@ class AbstractSimulation(object): # Pickle Dataset if op.exists(self.dataset_pickle_path): print('A dataset already exists, we will keep it intact, delete it manually if you want to change it') + # todo: print the parameters of the existing data, the parameters that were used to generate it else: with open(self.dataset_pickle_path, 'wb') as fp: pickle.dump(dataset, fp) diff --git a/experiment/simulation/lin_space2_simulation.py b/experiment/simulation/lin_space2_simulation.py index c28f8ce23a2c241378c9b508c9b426f23d54ee9d..bac71fc1e0721a62cb79c742246e53e9019eb666 100644 --- a/experiment/simulation/lin_space2_simulation.py +++ b/experiment/simulation/lin_space2_simulation.py @@ -9,7 +9,7 @@ from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D impo from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset -class LinSpace3Simulation(AbstractSimulation): +class LinSpace5Simulation(AbstractSimulation): FITTED_ESTIMATORS = [] def __init__(self, nb_fit=1): @@ -34,8 +34,9 @@ class LinSpace3Simulation(AbstractSimulation): if __name__ == '__main__': - simu = LinSpace3Simulation(nb_fit=7) + simu = LinSpace5Simulation(nb_fit=10) simu.dump() - for estimator_class in MARGIN_ESTIMATORS_FOR_SIMULATION + FULL_ESTIMATORS_FOR_SIMULATION: + estimators_class = MARGIN_ESTIMATORS_FOR_SIMULATION + FULL_ESTIMATORS_FOR_SIMULATION + for estimator_class in estimators_class[:]: simu.fit(estimator_class, show=False) simu.visualize_comparison_graph() diff --git a/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py b/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py index c758a31cb9263b83c4f91e6c0701af8133a118f6..ca6cfed3d9b12ab884ffe3148cb3775f485792a1 100644 --- a/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py +++ b/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py @@ -1,6 +1,7 @@ from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \ FullEstimatorInASingleStepWithSmoothMargin -from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel +from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \ + ConstantMarginModel from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset @@ -14,6 +15,17 @@ class FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smit max_stable_model=Smith()) +class FullEstimatorInASingleStepWithSmoothMargin_Constant_Smith( + FullEstimatorInASingleStepWithSmoothMargin): + + @classmethod + def from_dataset(cls, dataset: AbstractDataset): + return cls(dataset, margin_model=ConstantMarginModel(dataset.coordinates), + max_stable_model=Smith()) + + FULL_ESTIMATORS_FOR_SIMULATION = [ - FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smith + FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smith, + FullEstimatorInASingleStepWithSmoothMargin_Constant_Smith, + ] diff --git a/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py b/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py index 34a645511ffe22e021e74d7affa3182c325f3456..1bd0802658bf787d6feaeb8e358def511e186575 100644 --- a/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py +++ b/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py @@ -1,5 +1,6 @@ from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator import SmoothMarginEstimator -from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel +from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \ + ConstantMarginModel from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset @@ -10,6 +11,14 @@ class SmoothMarginEstimator_LinearAllParametersAllDims(SmoothMarginEstimator): return cls(dataset, LinearAllParametersAllDimsMarginModel(dataset.coordinates)) +class SmoothMarginEstimator_Constant(SmoothMarginEstimator): + + @classmethod + def from_dataset(cls, dataset: AbstractDataset): + return cls(dataset, ConstantMarginModel(dataset.coordinates)) + + MARGIN_ESTIMATORS_FOR_SIMULATION = [ - SmoothMarginEstimator_LinearAllParametersAllDims + SmoothMarginEstimator_LinearAllParametersAllDims, + SmoothMarginEstimator_Constant, ] diff --git a/extreme_estimator/extreme_models/margin_model/margin_function/abstract_margin_function.py b/extreme_estimator/extreme_models/margin_model/margin_function/abstract_margin_function.py index 1a75950badc61a2d5df40bfd6dc335ce6b7dc860..be5104374aa5fb0b31e3e4aafe2f06bd4a8d06bf 100644 --- a/extreme_estimator/extreme_models/margin_model/margin_function/abstract_margin_function.py +++ b/extreme_estimator/extreme_models/margin_model/margin_function/abstract_margin_function.py @@ -88,7 +88,6 @@ class AbstractMarginFunction(object): if self.datapoint_display: ax.plot(linspace, grid[gev_value_name], marker=self.datapoint_marker, color=self.color) else: - print('here') ax.plot(linspace, grid[gev_value_name], color=self.color, linewidth=self.linewidth) # X axis ax.set_xlabel('coordinate X') @@ -109,7 +108,6 @@ class AbstractMarginFunction(object): if self.datapoint_display: # todo: keep only the index of interest here linspace = self.coordinates.coordinates_values(spatio_temporal_split)[:, 0] - print(self.spatio_temporal_split, linspace) if self.filter is not None: linspace = linspace[self.filter] resolution = len(linspace) diff --git a/extreme_estimator/extreme_models/margin_model/margin_function/utils.py b/extreme_estimator/extreme_models/margin_model/margin_function/utils.py index d4a990704d1a2ea199bec1b4d9c4c7ee50b309f8..458628dda0920ce246da513a6adece0f74cd5726 100644 --- a/extreme_estimator/extreme_models/margin_model/margin_function/utils.py +++ b/extreme_estimator/extreme_models/margin_model/margin_function/utils.py @@ -6,6 +6,7 @@ from extreme_estimator.gev_params import GevParams def relative_abs_error(reference_value, fitted_value): + # todo: handle the case when the location, or the shape we aim to estimate might be equal to zero return (reference_value - fitted_value).abs() / reference_value diff --git a/spatio_temporal_dataset/coordinates/abstract_coordinates.py b/spatio_temporal_dataset/coordinates/abstract_coordinates.py index 14956f0280de94cd807a2335562975b325fb3ad4..db5506200790f5e4991e7a0726d0981662af2583 100644 --- a/spatio_temporal_dataset/coordinates/abstract_coordinates.py +++ b/spatio_temporal_dataset/coordinates/abstract_coordinates.py @@ -96,7 +96,7 @@ class AbstractCoordinates(object): return cls.from_df(df) @property - def index(self): + def index(self) -> pd.Index: return self.df_all_coordinates.index @property @@ -124,7 +124,7 @@ class AbstractCoordinates(object): return ind_train_from_s_split(s_split=self.s_split_temporal) @property - def df_split(self): + def df_split(self) -> pd.DataFrame: split_name_to_s_split = { self.SPATIAL_SPLIT: self.s_split_spatial, self.TEMPORAL_SPLIT: self.s_split_temporal, @@ -173,7 +173,7 @@ class AbstractCoordinates(object): return self.df_all_coordinates[self.COORDINATE_Z].values.copy() @property - def t_coordinates(self): + def t_coordinates(self) -> np.ndarray: return self.df_all_coordinates[self.COORDINATE_T].values.copy() def visualize(self):