Commit ad558b58 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[REFACTOR] refactor test folder. add utils.py

parent 4457384d
No related merge requests found
Showing with 58 additions and 33 deletions
+58 -33
...@@ -34,6 +34,9 @@ class AbstractMaxStableModel(AbstractModel): ...@@ -34,6 +34,9 @@ class AbstractMaxStableModel(AbstractModel):
# Prepare the fit params # Prepare the fit params
fit_params = self.cov_mod_param.copy() fit_params = self.cov_mod_param.copy()
start_dict = self.params_start_fit start_dict = self.params_start_fit
# Remove the 'var' parameter from the start_dict in the 2D case, otherwise fitmaxstab crashes
if len(df_coordinates.columns) == 2 and 'var' in start_dict.keys():
start_dict.pop('var')
if fit_marge: if fit_marge:
start_dict.update(margin_start_dict) start_dict.update(margin_start_dict)
fit_params.update({k: robjects.Formula(v) for k, v in fit_marge_form_dict.items()}) fit_params.update({k: robjects.Formula(v) for k, v in fit_marge_form_dict.items()})
......
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]
...@@ -34,7 +34,7 @@ class MaxStableDataset(SimulatedDataset): ...@@ -34,7 +34,7 @@ class MaxStableDataset(SimulatedDataset):
class MarginDataset(SimulatedDataset): class MarginDataset(SimulatedDataset):
@classmethod @classmethod
def from_sampling(cls, nb_obs: int, margin_model: AbstractMarginModel,coordinates: AbstractCoordinates): def from_sampling(cls, nb_obs: int, margin_model: AbstractMarginModel, coordinates: AbstractCoordinates):
temporal_obs = MarginAnnualMaxima.from_sampling(nb_obs, coordinates, margin_model) temporal_obs = MarginAnnualMaxima.from_sampling(nb_obs, coordinates, margin_model)
return cls(temporal_observations=temporal_obs, coordinates=coordinates, margin_model=margin_model) return cls(temporal_observations=temporal_obs, coordinates=coordinates, margin_model=margin_model)
......
...@@ -3,11 +3,11 @@ from itertools import product ...@@ -3,11 +3,11 @@ from itertools import product
from extreme_estimator.estimator.full_estimator import SmoothMarginalsThenUnitaryMsp, \ from extreme_estimator.estimator.full_estimator import SmoothMarginalsThenUnitaryMsp, \
FullEstimatorInASingleStepWithSmoothMargin 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.dataset.simulation_dataset import FullSimulatedDataset
from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates 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_margin_estimators import TestSmoothMarginEstimator
from test.test_extreme_estimator.test_estimator.test_max_stable_estimators import TestMaxStableEstimators from test.test_extreme_estimator.test_estimator.test_max_stable_estimators import TestMaxStableEstimators
from test.test_utils import load_test_max_stable_models, load_smooth_margin_models
class TestFullEstimators(unittest.TestCase): class TestFullEstimators(unittest.TestCase):
...@@ -17,9 +17,8 @@ class TestFullEstimators(unittest.TestCase): ...@@ -17,9 +17,8 @@ class TestFullEstimators(unittest.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) self.spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1)
self.max_stable_models = load_max_stable_models() self.max_stable_models = load_test_max_stable_models()
self.smooth_margin_models = TestSmoothMarginEstimator.load_smooth_margin_models( self.smooth_margin_models = load_smooth_margin_models(coordinates=self.spatial_coordinates)
coordinates=self.spatial_coordinates)
def test_full_estimators(self): def test_full_estimators(self):
for margin_model, max_stable_model in product(self.smooth_margin_models, self.max_stable_models): for margin_model, max_stable_model in product(self.smooth_margin_models, self.max_stable_models):
......
...@@ -7,23 +7,17 @@ from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator ...@@ -7,23 +7,17 @@ from extreme_estimator.estimator.margin_estimator import SmoothMarginEstimator
from extreme_estimator.return_level_plot.spatial_2D_plot import Spatial2DPlot from extreme_estimator.return_level_plot.spatial_2D_plot import Spatial2DPlot
from spatio_temporal_dataset.dataset.simulation_dataset import MarginDataset from spatio_temporal_dataset.dataset.simulation_dataset import MarginDataset
from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import CircleCoordinates
from test.test_utils import load_smooth_margin_models
class TestSmoothMarginEstimator(unittest.TestCase): class TestSmoothMarginEstimator(unittest.TestCase):
DISPLAY = False DISPLAY = False
MARGIN_TYPES = [ConstantMarginModel, LinearShapeAxis0MarginModel,
LinearShapeAxis0and1MarginModel, LinearAllParametersAxis0MarginModel,
LinearAllParametersAxis0And1MarginModel][:]
SMOOTH_MARGIN_ESTIMATORS = [SmoothMarginEstimator] SMOOTH_MARGIN_ESTIMATORS = [SmoothMarginEstimator]
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.spatial_coordinates = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) 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) self.smooth_margin_models = load_smooth_margin_models(coordinates=self.spatial_coordinates)
@classmethod
def load_smooth_margin_models(cls, coordinates):
return [margin_class(coordinates=coordinates) for margin_class in cls.MARGIN_TYPES]
def test_dependency_estimators(self): def test_dependency_estimators(self):
for margin_model in self.smooth_margin_models: for margin_model in self.smooth_margin_models:
......
...@@ -3,9 +3,9 @@ import unittest ...@@ -3,9 +3,9 @@ import unittest
from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \ from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \
AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction
from extreme_estimator.estimator.max_stable_estimator import MaxStableEstimator 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.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 CircleCoordinates
from test.test_utils import load_test_max_stable_models
class TestMaxStableEstimators(unittest.TestCase): class TestMaxStableEstimators(unittest.TestCase):
...@@ -16,7 +16,7 @@ class TestMaxStableEstimators(unittest.TestCase): ...@@ -16,7 +16,7 @@ class TestMaxStableEstimators(unittest.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.spatial_coord = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1) self.spatial_coord = CircleCoordinates.from_nb_points(nb_points=5, max_radius=1)
self.max_stable_models = load_max_stable_models() self.max_stable_models = load_test_max_stable_models()
def test_max_stable_estimators(self): def test_max_stable_estimators(self):
for max_stable_model in self.max_stable_models: for max_stable_model in self.max_stable_models:
......
...@@ -2,9 +2,8 @@ from rpy2.rinterface import RRuntimeError ...@@ -2,9 +2,8 @@ from rpy2.rinterface import RRuntimeError
import unittest import unittest
from itertools import product from itertools import product
from extreme_estimator.extreme_models.max_stable_model.utils import load_max_stable_models
from spatio_temporal_dataset.coordinates.utils import load_coordinates
from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset
from test.test_utils import load_test_max_stable_models, load_test_coordinates
class TestDataset(unittest.TestCase): class TestDataset(unittest.TestCase):
...@@ -12,8 +11,8 @@ class TestDataset(unittest.TestCase): ...@@ -12,8 +11,8 @@ class TestDataset(unittest.TestCase):
nb_points = 10 nb_points = 10
def test_max_stable_dataset_R1_and_R2(self): def test_max_stable_dataset_R1_and_R2(self):
max_stable_models = load_max_stable_models()[:] max_stable_models = load_test_max_stable_models()[:]
coordinatess = load_coordinates(self.nb_points)[:-1] coordinatess = load_test_coordinates(self.nb_points)[:-1]
for coordinates, max_stable_model in product(coordinatess, max_stable_models): for coordinates, max_stable_model in product(coordinatess, max_stable_models):
MaxStableDataset.from_sampling(nb_obs=self.nb_obs, MaxStableDataset.from_sampling(nb_obs=self.nb_obs,
max_stable_model=max_stable_model, max_stable_model=max_stable_model,
...@@ -21,13 +20,13 @@ class TestDataset(unittest.TestCase): ...@@ -21,13 +20,13 @@ class TestDataset(unittest.TestCase):
self.assertTrue(True) self.assertTrue(True)
def test_max_stable_dataset_crash_R3(self): def test_max_stable_dataset_crash_R3(self):
# test to warn me when spatialExtremes handles R3 """Test to warn me when spatialExtremes handles R3"""
with self.assertRaises(RRuntimeError): with self.assertRaises(RRuntimeError):
smith_process = load_max_stable_models()[0] smith_process = load_test_max_stable_models()[0]
coordinates_R3 = load_coordinates(self.nb_points)[-1] coordinates = load_test_coordinates(self.nb_points)[-1]
MaxStableDataset.from_sampling(nb_obs=self.nb_obs, MaxStableDataset.from_sampling(nb_obs=self.nb_obs,
max_stable_model=smith_process, max_stable_model=smith_process,
coordinates=coordinates_R3) coordinates=coordinates)
if __name__ == '__main__': if __name__ == '__main__':
......
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAxis0And1MarginModel, \
ConstantMarginModel
from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \ from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \
AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction
from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith, BrownResnick, Schlather, \ from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith, BrownResnick, Schlather, \
Geometric, ExtremalT, ISchlather 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
MAX_STABLE_TYPES = [Smith, BrownResnick, Schlather, Geometric, ExtremalT, ISchlather] """
Common objects to load for the test.
Sometimes it doesn't cover all the class (e.g margin_model, coordinates...)
In this case, unit test (at least on the constructor) must be ensured in the test relative to the class
"""
TEST_MAX_STABLE_MODEL = [Smith, BrownResnick, Schlather, Geometric, ExtremalT, ISchlather]
TEST_COORDINATES = [UniformCoordinates, CircleCoordinates, AlpsStation3DCoordinatesWithAnisotropy]
MARGIN_TYPES = [ConstantMarginModel, LinearAllParametersAxis0And1MarginModel][:]
def load_max_stable_models():
def load_smooth_margin_models(coordinates):
return [margin_class(coordinates=coordinates) for margin_class in MARGIN_TYPES]
def load_test_max_stable_models():
# Load all max stable model # Load all max stable model
max_stable_models = [] max_stable_models = []
for max_stable_class in MAX_STABLE_TYPES: for max_stable_class in TEST_MAX_STABLE_MODEL:
if issubclass(max_stable_class, AbstractMaxStableModelWithCovarianceFunction): if issubclass(max_stable_class, AbstractMaxStableModelWithCovarianceFunction):
max_stable_models.extend([max_stable_class(covariance_function=covariance_function) max_stable_models.extend([max_stable_class(covariance_function=covariance_function)
for covariance_function in CovarianceFunction]) for covariance_function in CovarianceFunction])
...@@ -17,3 +35,6 @@ def load_max_stable_models(): ...@@ -17,3 +35,6 @@ def load_max_stable_models():
max_stable_models.append(max_stable_class()) max_stable_models.append(max_stable_class())
return max_stable_models return max_stable_models
def load_test_coordinates(nb_points):
return [coordinate_class.from_nb_points(nb_points=nb_points) for coordinate_class in TEST_COORDINATES]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment