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

[EXTREME ESTIMATOR][MAX STABLE] improve the exception error message when...

[EXTREME ESTIMATOR][MAX STABLE] improve the exception error message when trying to sample with 3D coordinates and the wrong arguments
parent 02c210e9
No related merge requests found
Showing with 17 additions and 8 deletions
+17 -8
...@@ -2,11 +2,13 @@ from enum import Enum ...@@ -2,11 +2,13 @@ from enum import Enum
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from rpy2.rinterface import RRuntimeWarning
from rpy2.rinterface._rinterface import RRuntimeError
from extreme_estimator.extreme_models.abstract_model import AbstractModel from extreme_estimator.extreme_models.abstract_model import AbstractModel
from extreme_estimator.extreme_models.result_from_fit import ResultFromFit, ResultFromSpatialExtreme from extreme_estimator.extreme_models.result_from_fit import ResultFromFit, ResultFromSpatialExtreme
from extreme_estimator.extreme_models.utils import r, safe_run_r_estimator, get_coord, \ from extreme_estimator.extreme_models.utils import r, safe_run_r_estimator, get_coord, \
get_margin_formula get_margin_formula, SafeRunException
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
...@@ -71,7 +73,8 @@ class AbstractMaxStableModel(AbstractModel): ...@@ -71,7 +73,8 @@ class AbstractMaxStableModel(AbstractModel):
**fit_params) **fit_params)
return ResultFromSpatialExtreme(res) return ResultFromSpatialExtreme(res)
def rmaxstab(self, nb_obs: int, coordinates_values: np.ndarray, use_rmaxstab_with_2_coordinates=False) -> np.ndarray: def rmaxstab(self, nb_obs: int, coordinates_values: np.ndarray,
use_rmaxstab_with_2_coordinates=False) -> np.ndarray:
""" """
Return an numpy of maxima. With rows being the stations and columns being the years of maxima Return an numpy of maxima. With rows being the stations and columns being the years of maxima
""" """
...@@ -79,8 +82,13 @@ class AbstractMaxStableModel(AbstractModel): ...@@ -79,8 +82,13 @@ class AbstractMaxStableModel(AbstractModel):
# When we have more than 2 coordinates, then sample based on the 2 first coordinates only # When we have more than 2 coordinates, then sample based on the 2 first coordinates only
coordinates_values = coordinates_values[:, :2] coordinates_values = coordinates_values[:, :2]
maxima_frech = np.array( try:
r.rmaxstab(nb_obs, coordinates_values, *list(self.cov_mod_param.values()), **self.params_sample)) maxima_frech = np.array(
r.rmaxstab(nb_obs, coordinates_values, *list(self.cov_mod_param.values()), **self.params_sample))
except (RRuntimeError, RRuntimeWarning) as e:
raise SafeRunException('\n\nSome R exception have been launched at RunTime: \n{} \n{}'.format(e.__repr__(),
'To sample from 3D data we advise to set the function argument '
'"use_rmaxstab_with_2_coordinates" to True'))
return np.transpose(maxima_frech) return np.transpose(maxima_frech)
......
...@@ -5,7 +5,7 @@ import numpy as np ...@@ -5,7 +5,7 @@ import numpy as np
from rpy2.rinterface import RRuntimeError from rpy2.rinterface import RRuntimeError
from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearNonStationaryLocationMarginModel from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearNonStationaryLocationMarginModel
from extreme_estimator.extreme_models.utils import set_seed_for_test from extreme_estimator.extreme_models.utils import set_seed_for_test, SafeRunException
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \ from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \
BetweenZeroAndOneNormalization BetweenZeroAndOneNormalization
...@@ -30,7 +30,7 @@ class TestDataset(unittest.TestCase): ...@@ -30,7 +30,7 @@ class TestDataset(unittest.TestCase):
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(SafeRunException):
smith_process = load_test_max_stable_models()[0] smith_process = load_test_max_stable_models()[0]
coordinates = load_test_3D_spatial_coordinates(nb_points=self.nb_points)[0] coordinates = load_test_3D_spatial_coordinates(nb_points=self.nb_points)[0]
MaxStableDataset.from_sampling(nb_obs=self.nb_obs, MaxStableDataset.from_sampling(nb_obs=self.nb_obs,
...@@ -39,7 +39,9 @@ class TestDataset(unittest.TestCase): ...@@ -39,7 +39,9 @@ class TestDataset(unittest.TestCase):
def test_max_stable_dataset_R3_with_R2_spatial_structure(self): def test_max_stable_dataset_R3_with_R2_spatial_structure(self):
"""Test to create a dataset in R3, but where the spatial structure is only with respect to the 2 fisrt coordinates""" """Test to create a dataset in R3, but where the spatial structure is only with respect to the 2 fisrt coordinates"""
coordinates = load_test_3D_spatial_coordinates(nb_points=self.nb_points, transformation_class=BetweenZeroAndOneNormalization)[0] coordinates = \
load_test_3D_spatial_coordinates(nb_points=self.nb_points, transformation_class=BetweenZeroAndOneNormalization)[
0]
smith_process = load_test_max_stable_models()[0] smith_process = load_test_max_stable_models()[0]
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,
...@@ -47,7 +49,6 @@ class TestDataset(unittest.TestCase): ...@@ -47,7 +49,6 @@ class TestDataset(unittest.TestCase):
use_rmaxstab_with_2_coordinates=True) use_rmaxstab_with_2_coordinates=True)
class TestSpatioTemporalDataset(unittest.TestCase): class TestSpatioTemporalDataset(unittest.TestCase):
nb_obs = 2 nb_obs = 2
nb_points = 3 nb_points = 3
......
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