From 0db995925dce3f84f33271ded668335c9cf2cbae Mon Sep 17 00:00:00 2001 From: Le Roux Erwan <erwan.le-roux@irstea.fr> Date: Fri, 23 Nov 2018 18:34:51 +0100 Subject: [PATCH] [COORDINATES] refactor test_coordinates --- .../max_stable_process_plot.py | 4 +- .../spatial_robustness/alps_msp_robustness.py | 4 +- .../unidimensional_robustness.py | 4 +- .../extreme_models/max_stable_model/utils.py | 19 +++++++++ .../coordinates/abstract_coordinates.py | 9 +++++ ...es.py => generated_spatial_coordinates.py} | 7 ++-- ...{axis_coordinates.py => coordinates_1D.py} | 6 +-- spatio_temporal_dataset/coordinates/utils.py | 10 +++++ .../test_R_model/test_margin_function.py | 4 +- .../test_estimator/test_full_estimators.py | 10 +++-- .../test_estimator/test_margin_estimators.py | 10 ++--- .../test_max_stable_estimators.py | 23 +++-------- .../test_coordinates.py | 40 ++++++++----------- 13 files changed, 86 insertions(+), 64 deletions(-) create mode 100644 extreme_estimator/extreme_models/max_stable_model/utils.py rename spatio_temporal_dataset/coordinates/spatial_coordinates/{generated_coordinates.py => generated_spatial_coordinates.py} (84%) rename spatio_temporal_dataset/coordinates/unidimensional_coordinates/{axis_coordinates.py => coordinates_1D.py} (72%) create mode 100644 spatio_temporal_dataset/coordinates/utils.py 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 23b35b5b..272003b0 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_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates 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', CircleCoordinatesRadius1) + CoordinateClassItem = CoordinateDisplayItem('coordinate_class', CircleCoordinates) 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 69b4d619..c95238e8 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,7 +4,7 @@ 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_coordinates import CircleCoordinatesRadius1, \ +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates, \ CircleCoordinatesRadius2 @@ -49,7 +49,7 @@ def multiple_spatial_robustness_alps(): MaxStableProcessPlot.NbStationItem.name: nb_stations, MaxStableProcessPlot.NbObservationItem.name: nb_observation, MaxStableProcessPlot.MaxStableModelItem.name: msp_models, - MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinatesRadius1, + MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinates, CircleCoordinatesRadius2, 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 846127f1..34703d67 100644 --- a/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py +++ b/experiment/robustness_plot/estimation_robustness/unidimensional_robustness/unidimensional_robustness.py @@ -4,7 +4,7 @@ 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_coordinates import CircleCoordinatesRadius1, \ +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates, \ CircleCoordinatesRadius2 @@ -49,7 +49,7 @@ def multiple_unidimensional_robustness(): MaxStableProcessPlot.NbStationItem.name: nb_stations, MaxStableProcessPlot.NbObservationItem.name: nb_observation, MaxStableProcessPlot.MaxStableModelItem.name: msp_models, - MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinatesRadius1, + MaxStableProcessPlot.CoordinateClassItem.name: [CircleCoordinates, CircleCoordinatesRadius2, AlpsStation2DCoordinatesBetweenZeroAndOne, AlpsStationCoordinatesBetweenZeroAndTwo][:], diff --git a/extreme_estimator/extreme_models/max_stable_model/utils.py b/extreme_estimator/extreme_models/max_stable_model/utils.py new file mode 100644 index 00000000..41333165 --- /dev/null +++ b/extreme_estimator/extreme_models/max_stable_model/utils.py @@ -0,0 +1,19 @@ +from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \ + AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction +from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith, BrownResnick, Schlather, \ + Geometric, ExtremalT, ISchlather + +MAX_STABLE_TYPES = [Smith, BrownResnick, Schlather, Geometric, ExtremalT, ISchlather] + + +def load_max_stable_models(): + # Load all max stable model + max_stable_models = [] + for max_stable_class in MAX_STABLE_TYPES: + if issubclass(max_stable_class, AbstractMaxStableModelWithCovarianceFunction): + max_stable_models.extend([max_stable_class(covariance_function=covariance_function) + for covariance_function in CovarianceFunction]) + else: + max_stable_models.append(max_stable_class()) + return max_stable_models + diff --git a/spatio_temporal_dataset/coordinates/abstract_coordinates.py b/spatio_temporal_dataset/coordinates/abstract_coordinates.py index a51e8ccb..10e5d9f9 100644 --- a/spatio_temporal_dataset/coordinates/abstract_coordinates.py +++ b/spatio_temporal_dataset/coordinates/abstract_coordinates.py @@ -101,6 +101,15 @@ class AbstractCoordinates(object): # Visualization + def visualize(self): + nb_coordinates_columns = len(self.coordinates_columns(self.df_coordinates)) + if nb_coordinates_columns == 1: + self.visualization_1D() + elif nb_coordinates_columns == 2: + self.visualization_2D() + else: + self.visualization_3D() + def visualization_1D(self): assert len(self.coordinates_columns(self.df_coordinates)) >= 1 x = self.coordinates_values[:] diff --git a/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_coordinates.py b/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py similarity index 84% rename from spatio_temporal_dataset/coordinates/spatial_coordinates/generated_coordinates.py rename to spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py index e9d939af..e12328a5 100644 --- a/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_coordinates.py +++ b/spatio_temporal_dataset/coordinates/spatial_coordinates/generated_spatial_coordinates.py @@ -7,7 +7,7 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo import matplotlib.pyplot as plt -class CircleCoordinatesRadius1(AbstractCoordinates): +class CircleCoordinates(AbstractCoordinates): @classmethod def from_nb_points(cls, nb_points, max_radius=1.0): @@ -15,7 +15,8 @@ class CircleCoordinatesRadius1(AbstractCoordinates): r = get_loaded_r() angles = np.array(r.runif(nb_points, max=2 * math.pi)) radius = np.sqrt(np.array(r.runif(nb_points, max=max_radius))) - df = pd.DataFrame.from_dict({cls.COORDINATE_X: radius * np.cos(angles), cls.COORDINATE_Y: radius * np.sin(angles)}) + df = pd.DataFrame.from_dict({cls.COORDINATE_X: radius * np.cos(angles), + cls.COORDINATE_Y: radius * np.sin(angles)}) return cls.from_df(df) def visualization_2D(self): @@ -27,7 +28,7 @@ class CircleCoordinatesRadius1(AbstractCoordinates): super().visualization_2D() -class CircleCoordinatesRadius2(CircleCoordinatesRadius1): +class CircleCoordinatesRadius2(CircleCoordinates): @classmethod def from_nb_points(cls, nb_points, max_radius=1.0): diff --git a/spatio_temporal_dataset/coordinates/unidimensional_coordinates/axis_coordinates.py b/spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py similarity index 72% rename from spatio_temporal_dataset/coordinates/unidimensional_coordinates/axis_coordinates.py rename to spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py index d0541f86..53730282 100644 --- a/spatio_temporal_dataset/coordinates/unidimensional_coordinates/axis_coordinates.py +++ b/spatio_temporal_dataset/coordinates/unidimensional_coordinates/coordinates_1D.py @@ -6,14 +6,14 @@ from extreme_estimator.extreme_models.utils import get_loaded_r from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates -class UniDimensionalCoordinates(AbstractCoordinates): +class AbstractUniDimensionalCoordinates(AbstractCoordinates): pass -class UniformAxisCoordinates(UniDimensionalCoordinates): +class UniformCoordinates(AbstractUniDimensionalCoordinates): @classmethod - def from_nb_points(cls, nb_points, start=0.0, end=1.0): + def from_nb_points(cls, nb_points, start=-1.0, end=1.0): # Sample uniformly inside the circle r = get_loaded_r() axis_coordinates = np.array(r.runif(nb_points, min=start, max=end)) diff --git a/spatio_temporal_dataset/coordinates/utils.py b/spatio_temporal_dataset/coordinates/utils.py new file mode 100644 index 00000000..377fd82d --- /dev/null +++ b/spatio_temporal_dataset/coordinates/utils.py @@ -0,0 +1,10 @@ +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 + +COORDINATES = [UniformCoordinates, CircleCoordinates, AlpsStation3DCoordinatesWithAnisotropy] + + +def load_coordinates(nb_points): + return [coordinate_class.from_nb_points(nb_points=nb_points) for coordinate_class in COORDINATES] diff --git a/test/test_extreme_estimator/test_R_model/test_margin_function.py b/test/test_extreme_estimator/test_R_model/test_margin_function.py index e789085e..f412d8d0 100644 --- a/test/test_extreme_estimator/test_R_model/test_margin_function.py +++ b/test/test_extreme_estimator/test_R_model/test_margin_function.py @@ -2,14 +2,14 @@ import unittest from extreme_estimator.gev_params import GevParams from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearShapeAxis0MarginModel -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates class TestLinearMarginModel(unittest.TestCase): DISPLAY = False def test_visualization_2D(self): - spatial_coordinates = CircleCoordinatesRadius1.from_nb_points(nb_points=50) + spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=50) margin_model = LinearShapeAxis0MarginModel(coordinates=spatial_coordinates) for gev_param_name in GevParams.GEV_PARAM_NAMES: margin_model.margin_function_sample.visualize_2D(gev_param_name=gev_param_name, show=self.DISPLAY) diff --git a/test/test_extreme_estimator/test_estimator/test_full_estimators.py b/test/test_extreme_estimator/test_estimator/test_full_estimators.py index a5a51dfd..db48460b 100644 --- a/test/test_extreme_estimator/test_estimator/test_full_estimators.py +++ b/test/test_extreme_estimator/test_estimator/test_full_estimators.py @@ -3,8 +3,9 @@ from itertools import product from extreme_estimator.estimator.full_estimator import SmoothMarginalsThenUnitaryMsp, \ FullEstimatorInASingleStepWithSmoothMargin +from extreme_estimator.extreme_models.max_stable_model.utils import load_max_stable_models from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates from test.test_extreme_estimator.test_estimator.test_margin_estimators import TestSmoothMarginEstimator from test.test_extreme_estimator.test_estimator.test_max_stable_estimators import TestMaxStableEstimators @@ -15,9 +16,10 @@ class TestFullEstimators(unittest.TestCase): def setUp(self): super().setUp() - self.spatial_coordinates = CircleCoordinatesRadius1.from_nb_points(nb_points=5, max_radius=1) - self.max_stable_models = TestMaxStableEstimators.load_max_stable_models() - self.smooth_margin_models = TestSmoothMarginEstimator.load_smooth_margin_models(spatial_coordinates=self.spatial_coordinates) + self.spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) + self.max_stable_models = load_max_stable_models() + self.smooth_margin_models = TestSmoothMarginEstimator.load_smooth_margin_models( + coordinates=self.spatial_coordinates) def test_full_estimators(self): for margin_model, max_stable_model in product(self.smooth_margin_models, self.max_stable_models): 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 89cfc17f..3def42b0 100644 --- a/test/test_extreme_estimator/test_estimator/test_margin_estimators.py +++ b/test/test_extreme_estimator/test_estimator/test_margin_estimators.py @@ -6,7 +6,7 @@ from extreme_estimator.extreme_models.margin_model.smooth_margin_model import Co from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator from extreme_estimator.return_level_plot.spatial_2D_plot import Spatial2DPlot from spatio_temporal_dataset.dataset.simulation_dataset import MarginDataset -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates class TestSmoothMarginEstimator(unittest.TestCase): @@ -18,12 +18,12 @@ class TestSmoothMarginEstimator(unittest.TestCase): def setUp(self): super().setUp() - self.spatial_coordinates = CircleCoordinatesRadius1.from_nb_points(nb_points=5, max_radius=1) - self.smooth_margin_models = self.load_smooth_margin_models(spatial_coordinates=self.spatial_coordinates) + self.spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) + self.smooth_margin_models = self.load_smooth_margin_models(coordinates=self.spatial_coordinates) @classmethod - def load_smooth_margin_models(cls, spatial_coordinates): - return [margin_class(coordinates=spatial_coordinates) for margin_class in cls.MARGIN_TYPES] + def load_smooth_margin_models(cls, coordinates): + return [margin_class(coordinates=coordinates) for margin_class in cls.MARGIN_TYPES] def test_dependency_estimators(self): for margin_model in self.smooth_margin_models: 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 6eb78d40..01ae2c3a 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 @@ -2,34 +2,21 @@ import unittest from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \ AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction -from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith, BrownResnick, Schlather, \ - Geometric, ExtremalT, ISchlather from extreme_estimator.estimator.max_stable_estimator import MaxStableEstimator +from extreme_estimator.extreme_models.max_stable_model.utils import load_max_stable_models from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset -from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates class TestMaxStableEstimators(unittest.TestCase): DISPLAY = False - MAX_STABLE_TYPES = [Smith, BrownResnick, Schlather, Geometric, ExtremalT, ISchlather] + MAX_STABLE_ESTIMATORS = [MaxStableEstimator] def setUp(self): super().setUp() - self.spatial_coord = CircleCoordinatesRadius1.from_nb_points(nb_points=5, max_radius=1) - self.max_stable_models = self.load_max_stable_models() - - @classmethod - def load_max_stable_models(cls): - # Load all max stable model - max_stable_models = [] - for max_stable_class in cls.MAX_STABLE_TYPES: - if issubclass(max_stable_class, AbstractMaxStableModelWithCovarianceFunction): - max_stable_models.extend([max_stable_class(covariance_function=covariance_function) - for covariance_function in CovarianceFunction]) - else: - max_stable_models.append(max_stable_class()) - return max_stable_models + self.spatial_coord = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) + self.max_stable_models = load_max_stable_models() def test_max_stable_estimators(self): for max_stable_model in self.max_stable_models: diff --git a/test/test_spatio_temporal_dataset/test_coordinates.py b/test/test_spatio_temporal_dataset/test_coordinates.py index b2bf2364..5c9f23ee 100644 --- a/test/test_spatio_temporal_dataset/test_coordinates.py +++ b/test/test_spatio_temporal_dataset/test_coordinates.py @@ -1,43 +1,37 @@ import unittest -from spatio_temporal_dataset.coordinates.unidimensional_coordinates.axis_coordinates import UniformAxisCoordinates +from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates +from spatio_temporal_dataset.coordinates.unidimensional_coordinates.coordinates_1D import UniformCoordinates 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_coordinates import CircleCoordinatesRadius1 +from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates -class TestSpatialCoordinates(unittest.TestCase): +class TestCoordinates(unittest.TestCase): DISPLAY = False - def test_circle(self): - coord = CircleCoordinatesRadius1.from_nb_points(nb_points=500) - if self.DISPLAY: - coord.visualization_2D() - self.assertTrue(True) + def __init__(self, methodName='runTest'): + super().__init__(methodName) + self.coord = None # type: AbstractCoordinates - def test_anisotropy(self): - coord = AlpsStation3DCoordinatesWithAnisotropy.from_csv() + def tearDown(self): if self.DISPLAY: - coord.visualization_3D() + self.coord.visualize() self.assertTrue(True) - def test_normalization(self): - coord = AlpsStation2DCoordinatesBetweenZeroAndOne.from_csv() - if self.DISPLAY: - coord.visualization_2D() - self.assertTrue(True) + def test_unif(self): + self.coord = UniformCoordinates.from_nb_points(nb_points=10) + def test_circle(self): + self.coord = CircleCoordinates.from_nb_points(nb_points=500) -class TestUniDimensionalCoordinates(unittest.TestCase): - DISPLAY = False + def test_normalization(self): + self.coord = AlpsStation2DCoordinatesBetweenZeroAndOne.from_csv() - def test_unif(self): - coord = UniformAxisCoordinates.from_nb_points(nb_points=10) - if self.DISPLAY: - coord.visualization_1D() - self.assertTrue(True) + def test_anisotropy(self): + self.coord = AlpsStation3DCoordinatesWithAnisotropy.from_csv() if __name__ == '__main__': -- GitLab