Commit 02c210e9 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[EXTREME ESTIMATOR][MAX STABLE] add argument use_rmaxstab_with_2_coordinates...

[EXTREME ESTIMATOR][MAX STABLE] add argument use_rmaxstab_with_2_coordinates to rmaxstab that makes it possible to sample dataset with 3D coordinates
parent 74a2cc4c
No related merge requests found
Showing with 54 additions and 5 deletions
+54 -5
...@@ -71,13 +71,17 @@ class AbstractMaxStableModel(AbstractModel): ...@@ -71,13 +71,17 @@ class AbstractMaxStableModel(AbstractModel):
**fit_params) **fit_params)
return ResultFromSpatialExtreme(res) return ResultFromSpatialExtreme(res)
def rmaxstab(self, nb_obs: int, coordinates_values: np.ndarray) -> 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
""" """
if use_rmaxstab_with_2_coordinates and coordinates_values.shape[1] > 2:
# When we have more than 2 coordinates, then sample based on the 2 first coordinates only
coordinates_values = coordinates_values[:, :2]
maxima_frech = np.array( maxima_frech = np.array(
r.rmaxstab(nb_obs, coordinates_values, *list(self.cov_mod_param.values()), **self.params_sample)) r.rmaxstab(nb_obs, coordinates_values, *list(self.cov_mod_param.values()), **self.params_sample))
return np.transpose(maxima_frech) return np.transpose(maxima_frech)
def remove_unused_parameters(self, start_dict, coordinate_dim): def remove_unused_parameters(self, start_dict, coordinate_dim):
......
...@@ -29,8 +29,10 @@ class SimulatedDataset(AbstractDataset): ...@@ -29,8 +29,10 @@ class SimulatedDataset(AbstractDataset):
class MaxStableDataset(SimulatedDataset): class MaxStableDataset(SimulatedDataset):
@classmethod @classmethod
def from_sampling(cls, nb_obs: int, max_stable_model: AbstractMaxStableModel, coordinates: AbstractCoordinates): def from_sampling(cls, nb_obs: int, max_stable_model: AbstractMaxStableModel, coordinates: AbstractCoordinates,
observations = MaxStableAnnualMaxima.from_sampling(nb_obs, max_stable_model, coordinates) use_rmaxstab_with_2_coordinates=False):
observations = MaxStableAnnualMaxima.from_sampling(nb_obs, max_stable_model, coordinates,
use_rmaxstab_with_2_coordinates)
return cls(observations=observations, coordinates=coordinates, max_stable_model=max_stable_model) return cls(observations=observations, coordinates=coordinates, max_stable_model=max_stable_model)
......
...@@ -33,8 +33,10 @@ class MarginAnnualMaxima(AnnualMaxima): ...@@ -33,8 +33,10 @@ class MarginAnnualMaxima(AnnualMaxima):
class MaxStableAnnualMaxima(AnnualMaxima): class MaxStableAnnualMaxima(AnnualMaxima):
@classmethod @classmethod
def from_sampling(cls, nb_obs: int, max_stable_model: AbstractMaxStableModel, coordinates: AbstractCoordinates): def from_sampling(cls, nb_obs: int, max_stable_model: AbstractMaxStableModel, coordinates: AbstractCoordinates,
maxima_frech = max_stable_model.rmaxstab(nb_obs=nb_obs, coordinates_values=coordinates.coordinates_values()) use_rmaxstab_with_2_coordinates=False):
maxima_frech = max_stable_model.rmaxstab(nb_obs=nb_obs, coordinates_values=coordinates.coordinates_values(),
use_rmaxstab_with_2_coordinates=use_rmaxstab_with_2_coordinates)
df_maxima_frech = pd.DataFrame(data=maxima_frech, index=coordinates.index) df_maxima_frech = pd.DataFrame(data=maxima_frech, index=coordinates.index)
return cls(df_maxima_frech=df_maxima_frech) return cls(df_maxima_frech=df_maxima_frech)
......
import unittest
from typing import List
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \
BetweenZeroAndOneNormalization
from test.test_utils import load_test_3D_spatial_coordinates, load_test_1D_and_2D_spatial_coordinates, \
load_test_max_stable_models
class TestMaxStableModel(unittest.TestCase):
def setUp(self) -> None:
self.nb_obs = 3
self.nb_points = 2
self.transformation_class = BetweenZeroAndOneNormalization
def test_rmaxstab_with_various_coordinates(self):
smith_process = load_test_max_stable_models()[0]
coordinates = load_test_1D_and_2D_spatial_coordinates(
nb_points=self.nb_points) # type: List[AbstractCoordinates]
coordinates += load_test_3D_spatial_coordinates(nb_points=2, transformation_class=self.transformation_class)
for coord in coordinates:
res = smith_process.rmaxstab(nb_obs=self.nb_obs, coordinates_values=coord.coordinates_values(),
use_rmaxstab_with_2_coordinates=True)
self.assertEqual((self.nb_points, self.nb_obs), res.shape)
if __name__ == '__main__':
unittest.main()
...@@ -7,6 +7,8 @@ from rpy2.rinterface import RRuntimeError ...@@ -7,6 +7,8 @@ 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
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 \
BetweenZeroAndOneNormalization
from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset, MarginDataset from spatio_temporal_dataset.dataset.simulation_dataset import MaxStableDataset, MarginDataset
from test.test_utils import load_test_max_stable_models, load_test_3D_spatial_coordinates, \ from test.test_utils import load_test_max_stable_models, load_test_3D_spatial_coordinates, \
load_test_1D_and_2D_spatial_coordinates, load_test_spatiotemporal_coordinates load_test_1D_and_2D_spatial_coordinates, load_test_spatiotemporal_coordinates
...@@ -35,6 +37,16 @@ class TestDataset(unittest.TestCase): ...@@ -35,6 +37,16 @@ class TestDataset(unittest.TestCase):
max_stable_model=smith_process, max_stable_model=smith_process,
coordinates=coordinates) coordinates=coordinates)
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"""
coordinates = load_test_3D_spatial_coordinates(nb_points=self.nb_points, transformation_class=BetweenZeroAndOneNormalization)[0]
smith_process = load_test_max_stable_models()[0]
MaxStableDataset.from_sampling(nb_obs=self.nb_obs,
max_stable_model=smith_process,
coordinates=coordinates,
use_rmaxstab_with_2_coordinates=True)
class TestSpatioTemporalDataset(unittest.TestCase): class TestSpatioTemporalDataset(unittest.TestCase):
nb_obs = 2 nb_obs = 2
......
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