diff --git a/experiment/fit_diagnosis/split_curve_example.py b/experiment/fit_diagnosis/split_curve_example.py index 790ca10e730cc88b85d9fc4f18c6881f2159234a..96e7c9bd2a6adc44e2197892fd533835081e7140 100644 --- a/experiment/fit_diagnosis/split_curve_example.py +++ b/experiment/fit_diagnosis/split_curve_example.py @@ -1,20 +1,10 @@ -from typing import Union - -from experiment.fit_diagnosis.split_curve import SplitCurve -from extreme_estimator.estimator.full_estimator import AbstractFullEstimator -from extreme_estimator.estimator.margin_estimator import AbstractMarginEstimator, ConstantMarginEstimator -from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset - -import random - from experiment.fit_diagnosis.split_curve import SplitCurve from extreme_estimator.estimator.full_estimator import FullEstimatorInASingleStepWithSmoothMargin -from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel, \ LinearAllParametersAllDimsMarginModel from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith from extreme_estimator.gev_params import GevParams -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import LinSpaceCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset @@ -24,7 +14,7 @@ class SplitCurveExample(SplitCurve): super().__init__(nb_fit) self.nb_points = 50 self.nb_obs = 60 - self.coordinates = LinSpaceCoordinates.from_nb_points(nb_points=self.nb_points, train_split_ratio=0.8) + self.coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points, train_split_ratio=0.8) # MarginModel Linear with respect to the shape (from 0.01 to 0.02) params_sample = { (GevParams.GEV_LOC, 0): 10, diff --git a/experiment/regression_margin/regression_margin.py b/experiment/regression_margin/regression_margin.py index 48449e7c27ae1b78dbed064ef4fcd2d2577676c9..2a878abe7fc5fb1cb54846497c96eb041bd4d6bc 100644 --- a/experiment/regression_margin/regression_margin.py +++ b/experiment/regression_margin/regression_margin.py @@ -1,14 +1,12 @@ -import random - import numpy as np from extreme_estimator.estimator.full_estimator import FullEstimatorInASingleStepWithSmoothMargin from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction -from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearShapeDim1MarginModel, \ - LinearAllParametersAllDimsMarginModel, LinearScaleDim1MarginModel, ConstantMarginModel +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 extreme_estimator.gev_params import GevParams -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import LinSpaceCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates import matplotlib.pyplot as plt from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset @@ -18,7 +16,7 @@ nb_obs = 60 nb_estimator = 2 show = False -coordinates = LinSpaceCoordinates.from_nb_points(nb_points=nb_points) +coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=nb_points) ########## GENERATING THE DATA ##################### diff --git a/experiment/regression_margin/visualization_margin_model.py b/experiment/regression_margin/visualization_margin_model.py index d4f7114969a46748e83e8bb408dff58fff57d77d..fe5ea8ba9a304c65d166bc0442ecb1ead0378598 100644 --- a/experiment/regression_margin/visualization_margin_model.py +++ b/experiment/regression_margin/visualization_margin_model.py @@ -3,8 +3,8 @@ import unittest from extreme_estimator.gev_params import GevParams from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearShapeDim1MarginModel, \ LinearAllParametersAllDimsMarginModel -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import LinSpaceCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates class VisualizationMarginModel(unittest.TestCase): @@ -14,14 +14,14 @@ class VisualizationMarginModel(unittest.TestCase): @classmethod def example_visualization_2D(cls): - spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=cls.nb_points) + spatial_coordinates = CircleSpatialCoordinates.from_nb_points(nb_points=cls.nb_points) margin_model = cls.margin_model(coordinates=spatial_coordinates) if cls.DISPLAY: margin_model.margin_function_sample.visualize() @classmethod def example_visualization_1D(cls): - coordinates = LinSpaceCoordinates.from_nb_points(nb_points=cls.nb_points) + coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=cls.nb_points) margin_model = cls.margin_model(coordinates=coordinates, params_sample={(GevParams.GEV_SHAPE, 1): 0.02}) if cls.DISPLAY: margin_model.margin_function_sample.visualize() diff --git a/experiment/robustness_plot/estimation_robustness/max_stable_process_plot.py b/experiment/robustness_plot/estimation_robustness/max_stable_process_plot.py index 272003b0b9b080e233dc8ba0f24c39bd822b9d41..479a67e069c3a0736b83077dc7cac9e1103a17fa 100644 --- a/experiment/robustness_plot/estimation_robustness/max_stable_process_plot.py +++ b/experiment/robustness_plot/estimation_robustness/max_stable_process_plot.py @@ -5,7 +5,7 @@ from experiment.robustness_plot.display_item import DisplayItem from experiment.robustness_plot.multiple_plot import MultiplePlot from experiment.robustness_plot.single_plot import SinglePlot from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset @@ -23,7 +23,7 @@ class CoordinateDisplayItem(DisplayItem): class MaxStableProcessPlot(object): MaxStableModelItem = MaxStableDisplayItem('max_stable_model', Smith) - CoordinateClassItem = CoordinateDisplayItem('coordinate_class', CircleCoordinates) + CoordinateClassItem = CoordinateDisplayItem('coordinate_class', CircleSpatialCoordinates) NbStationItem = DisplayItem('Number of stations', 50) NbObservationItem = DisplayItem('nb_obs', 60) diff --git a/experiment/robustness_plot/estimation_robustness/spatial_robustness/alps_msp_robustness.py b/experiment/robustness_plot/estimation_robustness/spatial_robustness/alps_msp_robustness.py index 920f2580c4d86552e55448a109e60a39bad4437a..8e148738f9ab68ccc4a09a00948e41b074b77eb7 100644 --- a/experiment/robustness_plot/estimation_robustness/spatial_robustness/alps_msp_robustness.py +++ b/experiment/robustness_plot/estimation_robustness/spatial_robustness/alps_msp_robustness.py @@ -4,8 +4,8 @@ from experiment.robustness_plot.estimation_robustness.max_stable_process_plot im from experiment.robustness_plot.single_plot import SinglePlot from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_2D_coordinates import \ AlpsStation2DCoordinatesBetweenZeroAndOne, AlpsStationCoordinatesBetweenZeroAndTwo -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates, \ - CircleCoordinatesRadius2 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates, \ + CircleSpatialCoordinatesRadius2 # def single_spatial_robustness_alps(): @@ -49,8 +49,8 @@ def multiple_spatial_robustness_alps(): MaxStableProcessPlot.NbStationItem.name: nb_stations, MaxStableProcessPlot.NbObservationItem.name: nb_observation, MaxStableProcessPlot.MaxStableModelItem.name: msp_models, - MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinates, - CircleCoordinatesRadius2, + MaxStableProcessPlot.CoordinateClassItem.name: [CircleSpatialCoordinates, + CircleSpatialCoordinatesRadius2, AlpsStation2DCoordinatesBetweenZeroAndOne, AlpsStationCoordinatesBetweenZeroAndTwo][:], }) diff --git a/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py b/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py index 05ce69c674cbb1cc7f0271564915e24756cbd8b3..068faa64ae39cd04ad2697ce1ba990ca96148acc 100644 --- a/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py +++ b/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py @@ -4,8 +4,8 @@ from experiment.robustness_plot.estimation_robustness.max_stable_process_plot im from experiment.robustness_plot.single_plot import SinglePlot from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_2D_coordinates import \ AlpsStation2DCoordinatesBetweenZeroAndOne, AlpsStationCoordinatesBetweenZeroAndTwo -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates, \ - CircleCoordinatesRadius2 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates, \ + CircleSpatialCoordinatesRadius2 # def single_spatial_robustness_alps(): @@ -49,8 +49,8 @@ def multiple_unidimensional_robustness(): MaxStableProcessPlot.NbStationItem.name: nb_stations, MaxStableProcessPlot.NbObservationItem.name: nb_observation, MaxStableProcessPlot.MaxStableModelItem.name: msp_models, - MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinates, - CircleCoordinatesRadius2, + MaxStableProcessPlot.CoordinateClassItem.name: [CircleSpatialCoordinates, + CircleSpatialCoordinatesRadius2, AlpsStation2DCoordinatesBetweenZeroAndOne, AlpsStationCoordinatesBetweenZeroAndTwo][:], }) diff --git a/extreme_estimator/extreme_models/max_stable_model/abstract_max_stable_model.py b/extreme_estimator/extreme_models/max_stable_model/abstract_max_stable_model.py index 5751dba1c828b95800e4812ab914f3981d4facc8..ead7adce0026e9db417b4e30c740d15a0704837c 100644 --- a/extreme_estimator/extreme_models/max_stable_model/abstract_max_stable_model.py +++ b/extreme_estimator/extreme_models/max_stable_model/abstract_max_stable_model.py @@ -84,6 +84,7 @@ class AbstractMaxStableModel(AbstractModel): """ Return an numpy of maxima. With rows being the stations and columns being the years of maxima """ + maxima_frech = np.array( r.rmaxstab(nb_obs, coordinates_values, *list(self.cov_mod_param.values()), **self.params_sample)) return np.transpose(maxima_frech) diff --git a/spatio_temporal_dataset/coordinates/abstract_coordinates.py b/spatio_temporal_dataset/coordinates/abstract_coordinates.py index 326ac94a127c6c03782919c40f028c497139759f..0c55f3dd2338d0d5b88cdc07ebc1e86c37234151 100644 --- a/spatio_temporal_dataset/coordinates/abstract_coordinates.py +++ b/spatio_temporal_dataset/coordinates/abstract_coordinates.py @@ -23,11 +23,11 @@ class AbstractCoordinates(object): SPATIAL_SPLIT = 'spatial_split' # Temporal columns COORDINATE_T = 'coord_t' - TEMPORAL_SPLIT = 'coord_temporal_split' + TEMPORAL_SPLIT = 'temporal_split' + # Coordinates columns COORDINATES_NAMES = COORDINATE_SPATIAL_NAMES + [COORDINATE_T] - def __init__(self, df_coord: pd.DataFrame, s_split_spatial: pd.Series = None, s_split_temporal: pd.Series = None, - slicer_class: type = SpatialSlicer): + def __init__(self, df_coord: pd.DataFrame, slicer_class: type, s_split_spatial: pd.Series = None, s_split_temporal: pd.Series = None): self.df_all_coordinates = df_coord # type: pd.DataFrame self.s_split_spatial = s_split_spatial # type: pd.Series self.s_split_temporal = s_split_temporal # type: pd.Series @@ -38,7 +38,7 @@ class AbstractCoordinates(object): # ClassMethod constructor @classmethod - def from_df(cls, df: pd.DataFrame, train_split_ratio: float = None, slicer_class: type = SpatialSlicer): + def from_df_and_slicer(cls, df: pd.DataFrame, slicer_class: type, train_split_ratio: float = None): """ train_split_ratio is shared between the spatial part of the data, and the temporal part """ @@ -46,16 +46,19 @@ class AbstractCoordinates(object): assert len(set(df.index)) == len(df) # Create a spatial split - s_split_spatial = s_split_from_df(df, cls.COORDINATE_X, cls.SPATIAL_SPLIT, train_split_ratio, concat=False) + if slicer_class in [SpatialSlicer, SpatioTemporalSlicer]: + s_split_spatial = s_split_from_df(df, cls.COORDINATE_X, cls.SPATIAL_SPLIT, train_split_ratio, concat=False) + else: + s_split_spatial = None # Create a temporal split - if slicer_class is SpatioTemporalSlicer: + if slicer_class in [TemporalSlicer, SpatioTemporalSlicer]: s_split_temporal = s_split_from_df(df, cls.COORDINATE_T, cls.TEMPORAL_SPLIT, train_split_ratio, concat=True) else: s_split_temporal = None - return cls(df_coord=df, s_split_spatial=s_split_spatial, s_split_temporal=s_split_temporal, - slicer_class=slicer_class) + return cls(df_coord=df, slicer_class=slicer_class, + s_split_spatial=s_split_spatial, s_split_temporal=s_split_temporal) @classmethod def from_csv(cls, csv_path: str = None): diff --git a/spatio_temporal_dataset/coordinates/spatial_coordinates/abstract_spatial_coordinates.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/abstract_spatial_coordinates.py new file mode 100644 index 0000000000000000000000000000000000000000..50a4367546f076707a00ab9766ad592bfe18db20 --- /dev/null +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/abstract_spatial_coordinates.py @@ -0,0 +1,13 @@ +import pandas as pd + +from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.slicer.spatial_slicer import SpatialSlicer + + +class AbstractSpatialCoordinates(AbstractCoordinates): + + @classmethod + def from_df(cls, df: pd.DataFrame, train_split_ratio: float = None): + assert cls.COORDINATE_X in df.columns + assert cls.COORDINATE_T not in df.columns + return super().from_df_and_slicer(df, SpatialSlicer, train_split_ratio) diff --git a/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_2D_coordinates.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_2D_coordinates.py index 529ac577f6e7533ab0aa11811aa34abb4add532e..7cd62ddd26135437f0f708f6cdd508f67ce89ee3 100644 --- a/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_2D_coordinates.py +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_2D_coordinates.py @@ -1,5 +1,5 @@ from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_3D_coordinates import AlpsStation3DCoordinates -from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation_2D import \ +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.transformation_2D import \ BetweenZeroAndOne2DNormalization from spatio_temporal_dataset.coordinates.transformed_coordinates.transformed_coordinates import TransformedCoordinates diff --git a/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_3D_coordinates.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_3D_coordinates.py index 6d00efb4f2d3bf609aaa2a25702de2927113b750..30e241a14872e726bdc2a8330d1e28e0c1f6e5e6 100644 --- a/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_3D_coordinates.py +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/alps_station_3D_coordinates.py @@ -3,12 +3,14 @@ import os.path as op import pandas as pd from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -from spatio_temporal_dataset.coordinates.transformed_coordinates.tranformation_3D import AnisotropyTransformation +from spatio_temporal_dataset.coordinates.spatial_coordinates.abstract_spatial_coordinates import \ + AbstractSpatialCoordinates +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.tranformation_3D import AnisotropyTransformation from spatio_temporal_dataset.coordinates.transformed_coordinates.transformed_coordinates import TransformedCoordinates from utils import get_full_path -class AlpsStation3DCoordinates(AbstractCoordinates): +class AlpsStation3DCoordinates(AbstractSpatialCoordinates): """ For the Alps Stations, X, Y coordinates are in Lambert 2. Altitude is in meters """ diff --git a/spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/coordinates_1D.py similarity index 62% rename from spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py rename to spatio_temporal_dataset/coordinates/spatial_coordinates/coordinates_1D.py index 64c56d52f34535df83b137cfb35cdb52dbd7949d..3a05d2dd8a154a15958dc5870338183b779fc529 100644 --- a/spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/coordinates_1D.py @@ -4,13 +4,15 @@ import numpy as np from rpy2.robjects import r from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.abstract_spatial_coordinates import \ + AbstractSpatialCoordinates -class AbstractUniDimensionalCoordinates(AbstractCoordinates): +class AbstractUniDimensionalSpatialCoordinates(AbstractSpatialCoordinates): pass -class LinSpaceCoordinates(AbstractUniDimensionalCoordinates): +class LinSpaceSpatialCoordinates(AbstractUniDimensionalSpatialCoordinates): @classmethod def from_nb_points(cls, nb_points, train_split_ratio: float = None, start=-1.0, end=1.0): @@ -19,11 +21,16 @@ class LinSpaceCoordinates(AbstractUniDimensionalCoordinates): return cls.from_df(df, train_split_ratio) -class UniformCoordinates(AbstractUniDimensionalCoordinates): +class UniformSpatialCoordinates(AbstractUniDimensionalSpatialCoordinates): @classmethod def from_nb_points(cls, nb_points, train_split_ratio: float = None, start=-1.0, end=1.0): # Sample uniformly inside the circle + df = cls.df_spatial(nb_points, start, end) + return cls.from_df(df, train_split_ratio) + + @classmethod + def df_spatial(cls, nb_points, start=-1.0, end=1.0): axis_coordinates = np.array(r.runif(nb_points, min=start, max=end)) df = pd.DataFrame.from_dict({cls.COORDINATE_X: axis_coordinates}) - return cls.from_df(df, train_split_ratio) + return df diff --git a/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py index be2be12012e05f6d24593356dac336f0ad463715..5a9e7407dac32a925ede43c0f252e3477aa0d2d6 100644 --- a/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py @@ -6,8 +6,11 @@ from rpy2.robjects import r from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates import matplotlib.pyplot as plt +from spatio_temporal_dataset.coordinates.spatial_coordinates.abstract_spatial_coordinates import \ + AbstractSpatialCoordinates -class CircleCoordinates(AbstractCoordinates): + +class CircleSpatialCoordinates(AbstractSpatialCoordinates): @classmethod def df_spatial(cls, nb_points, max_radius=1.0): @@ -31,7 +34,7 @@ class CircleCoordinates(AbstractCoordinates): super().visualization_2D() -class CircleCoordinatesRadius2(CircleCoordinates): +class CircleSpatialCoordinatesRadius2(CircleSpatialCoordinates): @classmethod def from_nb_points(cls, nb_points, train_split_ratio: float = None, max_radius=1.0): diff --git a/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/abstract_spatio_temporal_coordinates.py b/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/abstract_spatio_temporal_coordinates.py new file mode 100644 index 0000000000000000000000000000000000000000..7074c5440b42d2406423ebf72cb3d8d00d3c0dc7 --- /dev/null +++ b/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/abstract_spatio_temporal_coordinates.py @@ -0,0 +1,13 @@ +import pandas as pd + +from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.slicer.spatio_temporal_slicer import SpatioTemporalSlicer + + +class AbstractSpatioTemporalCoordinates(AbstractCoordinates): + + @classmethod + def from_df(cls, df: pd.DataFrame, train_split_ratio: float = None): + assert cls.COORDINATE_T in df.columns + assert cls.COORDINATE_X in df.columns + return super().from_df_and_slicer(df, SpatioTemporalSlicer, train_split_ratio) \ No newline at end of file diff --git a/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/generated_spatio_temporal_coordinates.py b/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/generated_spatio_temporal_coordinates.py index 7211d1c21b7937308661bfa8e9a11b532ce4e8f1..f2b4b648ccce456ed9025fac0297959ca15936fe 100644 --- a/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/generated_spatio_temporal_coordinates.py +++ b/spatio_temporal_dataset/coordinates/spatio_temporal_coordinates/generated_spatio_temporal_coordinates.py @@ -1,22 +1,25 @@ import pandas as pd from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import UniformSpatialCoordinates +from spatio_temporal_dataset.coordinates.spatio_temporal_coordinates.abstract_spatio_temporal_coordinates import \ + AbstractSpatioTemporalCoordinates from spatio_temporal_dataset.slicer.spatio_temporal_slicer import SpatioTemporalSlicer -from spatio_temporal_dataset.slicer.split import s_split_from_df -class CircleTemporalCoordinates(CircleCoordinates): +class UniformSpatioTemporalCoordinates(AbstractSpatioTemporalCoordinates): @classmethod def from_nb_points(cls, nb_points, train_split_ratio: float = None, nb_time_steps=1, max_radius=1.0): assert isinstance(nb_time_steps, int) and nb_time_steps >= 1 - df_spatial = CircleCoordinates.df_spatial(nb_points, max_radius) + df_spatial = UniformSpatialCoordinates.df_spatial(nb_points) df_time_steps = [] for t in range(nb_time_steps): df_time_step = df_spatial.copy() df_time_step[cls.COORDINATE_T] = t df_time_steps.append(df_time_step) df_time_steps = pd.concat(df_time_steps, ignore_index=True) - return AbstractCoordinates.from_df(df=df_time_steps, train_split_ratio=train_split_ratio, - slicer_class=SpatioTemporalSlicer) + print(df_time_steps) + return cls.from_df(df=df_time_steps, train_split_ratio=train_split_ratio) + + diff --git a/spatio_temporal_dataset/coordinates/temporal_coordinates/abstract_temporal_coordinates.py b/spatio_temporal_dataset/coordinates/temporal_coordinates/abstract_temporal_coordinates.py new file mode 100644 index 0000000000000000000000000000000000000000..f73a9cc2b0be078701baf0384c6e529f5b88e982 --- /dev/null +++ b/spatio_temporal_dataset/coordinates/temporal_coordinates/abstract_temporal_coordinates.py @@ -0,0 +1,13 @@ +import pandas as pd + +from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.slicer.temporal_slicer import TemporalSlicer + + +class AbstractTemporalCoordinates(AbstractCoordinates): + + @classmethod + def from_df(cls, df: pd.DataFrame, train_split_ratio: float = None): + assert cls.COORDINATE_T in df.columns + assert not any([coordinate_name in df.columns for coordinate_name in cls.COORDINATE_SPATIAL_NAMES]) + return super().from_df_and_slicer(df, TemporalSlicer, train_split_ratio) \ No newline at end of file diff --git a/spatio_temporal_dataset/coordinates/temporal_coordinates/generated_temporal_coordinates.py b/spatio_temporal_dataset/coordinates/temporal_coordinates/generated_temporal_coordinates.py new file mode 100644 index 0000000000000000000000000000000000000000..519387d9e0c870d25cb18eb62fdb38398bac9f55 --- /dev/null +++ b/spatio_temporal_dataset/coordinates/temporal_coordinates/generated_temporal_coordinates.py @@ -0,0 +1,31 @@ +from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates + + +# class TemporalCoordinates(AbstractCoordinates): +# pass +# +# +# @classmethod +# def from_nb_points(cls, nb_points, train_split_ratio: float = None, start=-1.0, end=1.0): +# # Sample uniformly inside the circle +# df = cls.df_spatial(nb_points, start, end) +# return cls.from_df(df, train_split_ratio) +# +# @classmethod +# def df_spatial(cls, nb_points, start=-1.0, end=1.0): +# axis_coordinates = np.array(r.runif(nb_points, min=start, max=end)) +# df = pd.DataFrame.from_dict({cls.COORDINATE_X: axis_coordinates}) +# return df +# +# @classmethod +# def from_nb_points(cls, nb_points, train_split_ratio: float = None, nb_time_steps=1, max_radius=1.0): +# assert isinstance(nb_time_steps, int) and nb_time_steps >= 1 +# df_spatial = UniformSpatialCoordinates.df_spatial(nb_points) +# df_time_steps = [] +# for t in range(nb_time_steps): +# df_time_step = df_spatial.copy() +# df_time_step[cls.COORDINATE_T] = t +# df_time_steps.append(df_time_step) +# df_time_steps = pd.concat(df_time_steps, ignore_index=True) +# print(df_time_steps) +# return cls.from_df(df=df_time_steps, train_split_ratio=train_split_ratio, slicer_class=SpatioTemporalSlicer) \ No newline at end of file diff --git a/spatio_temporal_dataset/coordinates/unidimensional_coordinates/__init__.py b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/__init__.py similarity index 100% rename from spatio_temporal_dataset/coordinates/unidimensional_coordinates/__init__.py rename to spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/__init__.py diff --git a/spatio_temporal_dataset/coordinates/transformed_coordinates/abstract_transformation.py b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/abstract_transformation.py similarity index 100% rename from spatio_temporal_dataset/coordinates/transformed_coordinates/abstract_transformation.py rename to spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/abstract_transformation.py diff --git a/spatio_temporal_dataset/coordinates/transformed_coordinates/tranformation_3D.py b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/tranformation_3D.py similarity index 95% rename from spatio_temporal_dataset/coordinates/transformed_coordinates/tranformation_3D.py rename to spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/tranformation_3D.py index 6754cb1c27803beb6de81e22aecd3b1166fb107d..ba2167d943d782c85784fcd0bd79970014d7e12c 100644 --- a/spatio_temporal_dataset/coordinates/transformed_coordinates/tranformation_3D.py +++ b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/tranformation_3D.py @@ -1,6 +1,6 @@ import pandas as pd import numpy as np -from spatio_temporal_dataset.coordinates.transformed_coordinates.abstract_transformation import AbstractTransformation +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.abstract_transformation import AbstractTransformation import math diff --git a/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation_2D.py b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/transformation_2D.py similarity index 95% rename from spatio_temporal_dataset/coordinates/transformed_coordinates/transformation_2D.py rename to spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/transformation_2D.py index 3c56441c4f10c6f354e175ba3cde0b0f74ff8a10..471cf3676d2086b8c908eadca419a79fefc0452a 100644 --- a/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation_2D.py +++ b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformation/transformation_2D.py @@ -1,4 +1,4 @@ -from spatio_temporal_dataset.coordinates.transformed_coordinates.abstract_transformation import AbstractTransformation +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.abstract_transformation import AbstractTransformation import pandas as pd diff --git a/spatio_temporal_dataset/coordinates/transformed_coordinates/transformed_coordinates.py b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformed_coordinates.py index fb99da7973c1da1b8e1825efd0f8ae6128eac9b4..de86b84c21554c1bf0734f31ba70b3106530d0ae 100644 --- a/spatio_temporal_dataset/coordinates/transformed_coordinates/transformed_coordinates.py +++ b/spatio_temporal_dataset/coordinates/transformed_coordinates/transformed_coordinates.py @@ -1,5 +1,5 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -from spatio_temporal_dataset.coordinates.transformed_coordinates.abstract_transformation import AbstractTransformation +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.abstract_transformation import AbstractTransformation class TransformedCoordinates(AbstractCoordinates): @@ -9,6 +9,7 @@ class TransformedCoordinates(AbstractCoordinates): transformation_function: AbstractTransformation): df_coordinates_transformed = coordinates.df_all_coordinates.copy() df_coordinates_transformed = transformation_function.transform(df_coord=df_coordinates_transformed) - return cls(df_coord=df_coordinates_transformed, s_split_spatial=coordinates.s_split_spatial) + return cls(df_coord=df_coordinates_transformed, slicer_class=type(coordinates.slicer), + s_split_spatial=coordinates.s_split_spatial, s_split_temporal=coordinates.s_split_temporal) diff --git a/spatio_temporal_dataset/slicer/abstract_slicer.py b/spatio_temporal_dataset/slicer/abstract_slicer.py index f2877b921849786a82887608bd2c2eaf924e5807..0fa0f7307ff6d110cb9295f0fc9f314ca24c6e1d 100644 --- a/spatio_temporal_dataset/slicer/abstract_slicer.py +++ b/spatio_temporal_dataset/slicer/abstract_slicer.py @@ -53,23 +53,23 @@ class AbstractSlicer(object): # Methods that need to be defined in the child class def specialized_loc_split(self, df: pd.DataFrame, split: Split): - return None + return @property - def some_required_ind_are_not_defined(self): - pass + def some_required_ind_are_not_defined(self) -> bool: + return @property def train_split(self) -> Split: - pass + return @property def test_split(self) -> Split: - pass + return @property def splits(self) -> List[Split]: - pass + return def df_sliced(df: pd.DataFrame, split: Split = Split.all, slicer: AbstractSlicer = None) -> pd.DataFrame: diff --git a/test/test_experiment/test_split_curve.py b/test/test_experiment/test_split_curve.py index 4b9b1d69093ec2ff3269f51008e50bb868ccd8ac..a521e573b95abdb3b6cd63cda6676b82e6790b3c 100644 --- a/test/test_experiment/test_split_curve.py +++ b/test/test_experiment/test_split_curve.py @@ -1,21 +1,13 @@ import unittest -from typing import Union -from experiment.fit_diagnosis.split_curve import SplitCurve -from extreme_estimator.estimator.full_estimator import AbstractFullEstimator -from extreme_estimator.estimator.margin_estimator import AbstractMarginEstimator, ConstantMarginEstimator -from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset - -import random +from extreme_estimator.estimator.margin_estimator import ConstantMarginEstimator from experiment.fit_diagnosis.split_curve import SplitCurve -from extreme_estimator.estimator.full_estimator import FullEstimatorInASingleStepWithSmoothMargin -from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel, \ LinearAllParametersAllDimsMarginModel from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith from extreme_estimator.gev_params import GevParams -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import LinSpaceCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset @@ -28,7 +20,7 @@ class TestSplitCurve(unittest.TestCase): super().__init__(nb_fit) self.nb_points = 50 self.nb_obs = 60 - self.coordinates = LinSpaceCoordinates.from_nb_points(nb_points=self.nb_points, train_split_ratio=0.8) + self.coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points, train_split_ratio=0.8) # MarginModel Linear with respect to the shape (from 0.01 to 0.02) params_sample = { (GevParams.GEV_LOC, 0): 10, diff --git a/test/test_extreme_estimator/test_estimator/test_margin_estimators.py b/test/test_extreme_estimator/test_estimator/test_margin_estimators.py index f9a51a737613b4fe6bd4855cf9ba475b9c3b9b2c..2f1bd7518cbdac131a2cc6677486103290be82cc 100644 --- a/test/test_extreme_estimator/test_estimator/test_margin_estimators.py +++ b/test/test_extreme_estimator/test_estimator/test_margin_estimators.py @@ -1,3 +1,4 @@ +import time import unittest from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator @@ -7,24 +8,26 @@ from test.test_utils import load_smooth_margin_models, load_test_1D_and_2D_coord class TestSmoothMarginEstimator(unittest.TestCase): DISPLAY = False - nb_points = 5 + nb_points = 2 + nb_obs = 2 def setUp(self): super().setUp() self.coordinates = load_test_1D_and_2D_coordinates(nb_points=self.nb_points) - def test_dependency_estimators(self): + def test_smooth_margin_estimator(self): for coordinates in self.coordinates: smooth_margin_models = load_smooth_margin_models(coordinates=coordinates) for margin_model in smooth_margin_models[1:]: - dataset = MarginDataset.from_sampling(nb_obs=10, + dataset = MarginDataset.from_sampling(nb_obs=self.nb_obs, margin_model=margin_model, coordinates=coordinates) # Fit estimator estimator = SmoothMarginEstimator(dataset=dataset, margin_model=margin_model) estimator.fit() # Plot - margin_model.margin_function_sample.visualize(show=self.DISPLAY) + if self.DISPLAY: + margin_model.margin_function_sample.visualize(show=True) self.assertTrue(True) diff --git a/test/test_extreme_estimator/test_estimator/test_max_stable_estimators.py b/test/test_extreme_estimator/test_estimator/test_max_stable_estimators.py index b464f80ba1c815ced3a273b3e8e5c69cab740d90..e9fdb3cab95f8ec32f06294676a6a35f714afdcc 100644 --- a/test/test_extreme_estimator/test_estimator/test_max_stable_estimators.py +++ b/test/test_extreme_estimator/test_estimator/test_max_stable_estimators.py @@ -4,7 +4,7 @@ from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction from extreme_estimator.estimator.max_stable_estimator import MaxStableEstimator from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates from test.test_utils import load_test_max_stable_models, load_test_1D_and_2D_coordinates, \ load_test_max_stable_estimators diff --git a/test/test_spatio_temporal_dataset/test_coordinates.py b/test/test_spatio_temporal_dataset/test_coordinates.py index 13ccbace8d66f40f0728ef740c423af7ba2a4994..a82080ef8f3aefc5867798dcc77c6c5f96db52d5 100644 --- a/test/test_spatio_temporal_dataset/test_coordinates.py +++ b/test/test_spatio_temporal_dataset/test_coordinates.py @@ -3,15 +3,14 @@ from collections import Counter from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.spatio_temporal_coordinates.generated_spatio_temporal_coordinates import \ - CircleTemporalCoordinates -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import UniformCoordinates + UniformSpatioTemporalCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import UniformSpatialCoordinates from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_2D_coordinates import \ AlpsStation2DCoordinatesBetweenZeroAndOne from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_3D_coordinates import \ AlpsStation3DCoordinatesWithAnisotropy -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates from spatio_temporal_dataset.slicer.spatio_temporal_slicer import SpatioTemporalSlicer -from spatio_temporal_dataset.slicer.split import Split class TestSpatialCoordinates(unittest.TestCase): @@ -27,10 +26,10 @@ class TestSpatialCoordinates(unittest.TestCase): self.assertTrue(True) def test_unif(self): - self.coord = UniformCoordinates.from_nb_points(nb_points=10) + self.coord = UniformSpatialCoordinates.from_nb_points(nb_points=10) def test_circle(self): - self.coord = CircleCoordinates.from_nb_points(nb_points=500) + self.coord = CircleSpatialCoordinates.from_nb_points(nb_points=500) def test_normalization(self): self.coord = AlpsStation2DCoordinatesBetweenZeroAndOne.from_csv() @@ -49,9 +48,9 @@ class SpatioTemporalCoordinates(unittest.TestCase): self.assertTrue(good_count) def test_temporal_circle(self): - self.coordinates = CircleTemporalCoordinates.from_nb_points(nb_points=self.nb_points, - nb_time_steps=self.nb_times_steps, - train_split_ratio=0.5) + self.coordinates = UniformSpatioTemporalCoordinates.from_nb_points(nb_points=self.nb_points, + nb_time_steps=self.nb_times_steps, + train_split_ratio=0.5) # def test_temporal_alps(self): # pass diff --git a/test/test_spatio_temporal_dataset/test_dataset.py b/test/test_spatio_temporal_dataset/test_dataset.py index 90282276e760dd6bd4388614613a6d44f5c3bf12..d05e61b70bad9aa12073ba481ca0d9e6e3cdb31a 100644 --- a/test/test_spatio_temporal_dataset/test_dataset.py +++ b/test/test_spatio_temporal_dataset/test_dataset.py @@ -3,7 +3,7 @@ import unittest from itertools import product from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset -from test.test_utils import load_test_max_stable_models, load_test_coordinates, load_test_3D_coordinates, \ +from test.test_utils import load_test_max_stable_models, load_test_spatial_coordinates, load_test_3D_coordinates, \ load_test_1D_and_2D_coordinates diff --git a/test/test_spatio_temporal_dataset/test_slicer.py b/test/test_spatio_temporal_dataset/test_slicer.py index 28532c318ca7775ac4a1104df13fcd6921cef021..8f9562abaa9d748c734bee72ccce99e6a478ba42 100644 --- a/test/test_spatio_temporal_dataset/test_slicer.py +++ b/test/test_spatio_temporal_dataset/test_slicer.py @@ -1,147 +1,114 @@ -# import pandas as pd -# import numpy as np -# from rpy2.rinterface import RRuntimeError -# import unittest -# from itertools import product -# -# from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel -# from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith -# from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -# from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import LinSpaceCoordinates -# from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset, FullSimulatedDataset -# from spatio_temporal_dataset.slicer.spatial_slicer import SpatialSlicer -# from spatio_temporal_dataset.slicer.spatio_temporal_slicer import SpatioTemporalSlicer -# from spatio_temporal_dataset.slicer.split import ALL_SPLITS_EXCEPT_ALL, Split -# from spatio_temporal_dataset.slicer.temporal_slicer import TemporalSlicer -# from spatio_temporal_dataset.spatio_temporal_observations.abstract_spatio_temporal_observations import \ -# AbstractSpatioTemporalObservations -# -# -# class TestSlicerForDataset(unittest.TestCase): -# -# def __init__(self, methodName: str = ...) -> None: -# super().__init__(methodName) -# self.dataset = None -# -# nb_spatial_points = 2 -# nb_temporal_obs = 2 -# complete_shape = (nb_spatial_points, nb_temporal_obs) -# -# def load_dataset(self, slicer_class, split_ratio_spatial, split_ratio_temporal): -# coordinates = LinSpaceCoordinates.from_nb_points(nb_points=self.nb_spatial_points, -# train_split_ratio=split_ratio_spatial) -# return FullSimulatedDataset.from_double_sampling(nb_obs=self.nb_temporal_obs, -# train_split_ratio=split_ratio_temporal, -# margin_model=ConstantMarginModel(coordinates=coordinates), -# coordinates=coordinates, max_stable_model=Smith(), -# slicer_class=slicer_class) -# -# def get_shape(self, dataset, split): -# return dataset.maxima_frech(split).shape -# -# def test_spatiotemporal_slicer_for_dataset(self): -# ind_tuple_to_observation_shape = { -# (None, None): self.complete_shape, -# (None, 0.5): self.complete_shape, -# (0.5, None): self.complete_shape, -# (0.5, 0.5): (1, 1), -# } -# self.check_shapes(ind_tuple_to_observation_shape, SpatioTemporalSlicer) -# -# def test_spatial_slicer_for_dataset(self): +from typing import List + +import unittest + +from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel +from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith +from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset +from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset +from spatio_temporal_dataset.slicer.split import ALL_SPLITS_EXCEPT_ALL, Split +from test.test_utils import load_test_1D_and_2D_coordinates, load_test_spatiotemporal_coordinates + + +class TestSlicerForDataset(unittest.TestCase): + + def __init__(self, methodName: str = ...) -> None: + super().__init__(methodName) + self.dataset = None + + nb_spatial_points = 2 + nb_temporal_points = 2 + nb_obs = 2 + + @property + def complete_shape(self): + pass + + def load_datasets(self, train_split_ratio) -> List[AbstractDataset]: + pass + + def get_shape(self, dataset, split): + return dataset.maxima_frech(split).shape + + def check_shapes(self, train_split_ratio_to_observation_shape): + for train_split_ratio, data_shape in train_split_ratio_to_observation_shape.items(): + for dataset in self.load_datasets(train_split_ratio): + self.assertEqual(self.complete_shape, self.get_shape(dataset, Split.all)) + for split in ALL_SPLITS_EXCEPT_ALL: + if split in dataset.slicer.splits: + self.assertEqual(data_shape, self.get_shape(dataset, split)) + else: + with self.assertRaises(AssertionError): + self.get_shape(dataset, split) + + +class TestSlicerForSpatialDataset(TestSlicerForDataset): + + @property + def complete_shape(self): + return self.nb_spatial_points, self.nb_obs + + def load_datasets(self, train_split_ratio): + coordinates_list = load_test_1D_and_2D_coordinates(nb_points=self.nb_spatial_points, + train_split_ratio=train_split_ratio) + dataset_list = [FullSimulatedDataset.from_double_sampling(nb_obs=self.nb_obs, + margin_model=ConstantMarginModel( + coordinates=coordinates), + coordinates=coordinates, max_stable_model=Smith()) + for coordinates in coordinates_list] + return dataset_list + + def test_spatial_slicer_for_spatial_dataset(self): + train_split_ratio_to_observation_shape = { + None: self.complete_shape, + 0.5: (1, 2), + } + self.check_shapes(train_split_ratio_to_observation_shape) + +# todo: create temporalCoordinates +# class TestSlicerForTemporalDataset(TestSlicerForDataset): +# +# def load_datasets(self, train_split_ratio): +# coordinates_list = load_test_1D_and_2D_coordinates(nb_points=self.nb_spatial_points, +# train_split_ratio=train_split_ratio) +# dataset_list = [FullSimulatedDataset.from_double_sampling(nb_obs=self.nb_temporal_obs, +# margin_model=ConstantMarginModel( +# coordinates=coordinates), +# coordinates=coordinates, max_stable_model=Smith()) +# for coordinates in coordinates_list] +# return dataset_list +# +# def test_temporal_slicer_for_temporal_dataset(self): # ind_tuple_to_observation_shape = { -# (None, None): self.complete_shape, -# (None, 0.5): self.complete_shape, -# (0.5, None): (1, 2), -# (0.5, 0.5): (1, 2), +# None: self.complete_shape, +# 0.5: (2, 1), # } -# self.check_shapes(ind_tuple_to_observation_shape, SpatialSlicer) -# -# def test_temporal_slicer_for_dataset(self): +# self.check_shapes(ind_tuple_to_observation_shape) + + +# class TestSlicerForSpatioTemporalDataset(TestSlicerForDataset): +# +# def complete_shape(self): +# return self.nb_spatial_points * self.nb_temporal_points, self.nb_obs +# +# def load_datasets(self, train_split_ratio): +# coordinates_list = load_test_spatiotemporal_coordinates(nb_points=self.nb_spatial_points, +# train_split_ratio=train_split_ratio, +# nb_time_steps=self.nb_temporal_points) +# dataset_list = [FullSimulatedDataset.from_double_sampling(nb_obs=self.nb_obs, +# margin_model=ConstantMarginModel( +# coordinates=coordinates), +# coordinates=coordinates, max_stable_model=Smith()) +# for coordinates in coordinates_list] +# return dataset_list +# +# def test_spatiotemporal_slicer_for_spatio_temporal_dataset(self): # ind_tuple_to_observation_shape = { -# (None, None): self.complete_shape, -# (None, 0.5): (2, 1), -# (0.5, None): self.complete_shape, -# (0.5, 0.5): (2, 1), -# } -# self.check_shapes(ind_tuple_to_observation_shape, TemporalSlicer) -# -# def check_shapes(self, ind_tuple_to_observation_shape, slicer_type): -# for split_ratio, data_shape in ind_tuple_to_observation_shape.items(): -# dataset = self.load_dataset(slicer_type, *split_ratio) -# self.assertEqual(self.complete_shape, self.get_shape(dataset, Split.all)) -# for split in ALL_SPLITS_EXCEPT_ALL: -# if split in dataset.slicer.splits: -# self.assertEqual(data_shape, self.get_shape(dataset, split)) -# else: -# with self.assertRaises(AssertionError): -# self.get_shape(dataset, split) -# -# -# class TestSlicerForCoordinates(unittest.TestCase): -# -# def nb_coordinates(self, coordinates: AbstractCoordinates, split): -# return len(coordinates.coordinates_values(split)) -# -# def test_slicer_for_coordinates(self): -# for split in Split: -# coordinates1 = LinSpaceCoordinates.from_nb_points(nb_points=2, train_split_ratio=0.5) -# if split in SpatialSlicer.SPLITS: -# self.assertEqual(self.nb_coordinates(coordinates1, split), 1) -# elif split in SpatioTemporalSlicer.SPLITS: -# self.assertEqual(self.nb_coordinates(coordinates1, split), 1) -# elif split in TemporalSlicer.SPLITS: -# self.assertEqual(self.nb_coordinates(coordinates1, split), 2) -# else: -# self.assertEqual(self.nb_coordinates(coordinates1, split), 2) -# coordinates2 = LinSpaceCoordinates.from_nb_points(nb_points=2) -# self.assertEqual(self.nb_coordinates(coordinates2, split), 2) -# -# -# class TestSlicerForObservations(unittest.TestCase): -# -# def load_observations(self, split_ratio_temporal): -# df = pd.DataFrame.from_dict( -# { -# 'year1': [1 for _ in range(4)], -# 'year2': [2 for _ in range(4)], -# -# }) -# return AbstractSpatioTemporalObservations(df_maxima_frech=df) -# -# def nb_obs(self, observations, split, slicer): -# return len(np.transpose(observations.maxima_frech(split, slicer))) -# -# def test_slicer_for_observations(self): -# observations = self.load_observations(0.5) -# # For the None Slicer, a slice should be returned only for split=SpatialTemporalSplit.all -# # self.assertEqual(len(observations.maxima_frech(SpatialTemporalSplit.all, None)), 2) -# self.assertEqual(2, self.nb_obs(observations, Split.all, None)) -# for split in ALL_SPLITS_EXCEPT_ALL: -# with self.assertRaises(AssertionError): -# observations.maxima_frech(split, None) -# # For other slicers we try out all the possible combinations -# slicer_type_to_size = { -# SpatialSlicer: 2, -# TemporalSlicer: 1, -# SpatioTemporalSlicer: 1, +# None: self.complete_shape, +# 0.5: (1, 1), # } -# for slicer_type, size in slicer_type_to_size.items(): -# for coordinates_train_ind in [None, pd.Series([True, True, True, False])][::-1]: -# slicer = slicer_type(coordinates_train_ind=coordinates_train_ind, -# observations_train_ind=observations.train_ind) -# self.assertEqual(2, self.nb_obs(observations, Split.all, slicer)) -# for split in ALL_SPLITS_EXCEPT_ALL: -# if split in slicer.splits: -# # By default for SpatioTemporalSlicer should slice if both train_ind are available -# # Otherwise if coordinates_train_ind is None, then it should return all the data -# if slicer_type is SpatioTemporalSlicer and coordinates_train_ind is None: -# size = 2 -# self.assertEqual(size, self.nb_obs(observations, split, slicer)) -# else: -# with self.assertRaises(AssertionError): -# observations.maxima_frech(split, slicer) -# -# -# if __name__ == '__main__': -# unittest.main() +# self.check_shapes(ind_tuple_to_observation_shape) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_unitary/test_rmaxstab/test_rmaxstab_without_margin.py b/test/test_unitary/test_rmaxstab/test_rmaxstab_without_margin.py index a4259d99da45decc3a59e8eb7124e679d3755e2d..8270299ba2df00e75ba3cfbf2f6179b0718c3487 100644 --- a/test/test_unitary/test_rmaxstab/test_rmaxstab_without_margin.py +++ b/test/test_unitary/test_rmaxstab/test_rmaxstab_without_margin.py @@ -7,6 +7,8 @@ from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Schlather from extreme_estimator.extreme_models.utils import r from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.abstract_spatial_coordinates import \ + AbstractSpatialCoordinates from spatio_temporal_dataset.spatio_temporal_observations.annual_maxima_observations import MaxStableAnnualMaxima from test.test_unitary.test_unitary_abstract import TestUnitaryAbstract @@ -21,7 +23,7 @@ class TestRMaxStab(TestUnitaryAbstract): def python_code(cls): # Load coordinate object df = pd.DataFrame(data=r.locations, columns=AbstractCoordinates.COORDINATE_SPATIAL_NAMES[:2]) - coordinates = AbstractCoordinates.from_df(df) + coordinates = AbstractSpatialCoordinates.from_df(df) # Load max stable model params_sample = {'range': 3, 'smooth': 0.5, 'nugget': 0} max_stable_model = Schlather(covariance_function=CovarianceFunction.whitmat, params_sample=params_sample) diff --git a/test/test_utils.py b/test/test_utils.py index 5abfc72ae43bd69bd8c3bd5945081f7d5c8f2fb2..57fe1c7ea7659dcac87373d4f84ca04385c9dad2 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -9,8 +9,10 @@ from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Geometric, ExtremalT, ISchlather from spatio_temporal_dataset.coordinates.spatial_coordinates.alps_station_3D_coordinates import \ AlpsStation3DCoordinatesWithAnisotropy -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import UniformCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleSpatialCoordinates +from spatio_temporal_dataset.coordinates.spatio_temporal_coordinates.generated_spatio_temporal_coordinates import \ + UniformSpatioTemporalCoordinates +from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import UniformSpatialCoordinates """ Common objects to load for the test. @@ -19,8 +21,9 @@ In this case, unit test (at least on the constructor) must be ensured in the tes """ TEST_MAX_STABLE_MODEL = [Smith, BrownResnick, Schlather, Geometric, ExtremalT, ISchlather] -TEST_1D_AND_2D_COORDINATES = [UniformCoordinates, CircleCoordinates] +TEST_1D_AND_2D_COORDINATES = [UniformSpatialCoordinates, CircleSpatialCoordinates] TEST_3D_COORDINATES = [AlpsStation3DCoordinatesWithAnisotropy] +TEST_SPATIO_TEMPORAL_COORDINATES = [UniformSpatioTemporalCoordinates] TEST_MARGIN_TYPES = [ConstantMarginModel, LinearAllParametersAllDimsMarginModel][:] TEST_MAX_STABLE_ESTIMATOR = [MaxStableEstimator] TEST_FULL_ESTIMATORS = [SmoothMarginalsThenUnitaryMsp, FullEstimatorInASingleStepWithSmoothMargin][:] @@ -51,13 +54,20 @@ def load_test_max_stable_models(): return max_stable_models -def load_test_coordinates(nb_points, coordinate_types): - return [coordinate_class.from_nb_points(nb_points=nb_points) for coordinate_class in coordinate_types] +def load_test_spatial_coordinates(nb_points, coordinate_types, train_split_ratio=None): + return [coordinate_class.from_nb_points(nb_points=nb_points, train_split_ratio=train_split_ratio) + for coordinate_class in coordinate_types] -def load_test_1D_and_2D_coordinates(nb_points): - return load_test_coordinates(nb_points, TEST_1D_AND_2D_COORDINATES) +def load_test_1D_and_2D_coordinates(nb_points, train_split_ratio=None): + return load_test_spatial_coordinates(nb_points, TEST_1D_AND_2D_COORDINATES, train_split_ratio=train_split_ratio) def load_test_3D_coordinates(nb_points): - return load_test_coordinates(nb_points, TEST_3D_COORDINATES) + return load_test_spatial_coordinates(nb_points, TEST_3D_COORDINATES) + + +def load_test_spatiotemporal_coordinates(nb_points, train_split_ratio=None, nb_time_steps=None): + return [coordinate_class.from_nb_points(nb_points=nb_points, train_split_ratio=train_split_ratio, + nb_time_steps=nb_time_steps) + for coordinate_class in TEST_SPATIO_TEMPORAL_COORDINATES] diff --git a/thesis_report/gev_plot.py b/thesis_report/gev_plot.py index ef58eed37a2f38818434e600e2526a6e7e5eeffc..d2d6b9e56cb22b53dc20a523c23ac687f6504f00 100644 --- a/thesis_report/gev_plot.py +++ b/thesis_report/gev_plot.py @@ -35,10 +35,11 @@ def max_stable_plot(): ax.plot(x, y, label=label) ax.legend(loc=4) ax.set_xlabel('$z$') - ax.set_ylabel('$P(Z \leq z)^n$') + if j == 0: + ax.set_ylabel('$P(\\frac{ \max{(Z_1, ..., Z_n)} - b_n}{a_n} \leq z)$') plt.show() if __name__ == '__main__': - gev_plot() - # max_stable_plot() +# gev_plot() + max_stable_plot()