Commit 00b74982 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[refactor] modify default params from an attribute to a property

parent 866d9bdc
No related merge requests found
Showing with 55 additions and 33 deletions
+55 -33
class AbstractModel(object): class AbstractModel(object):
def __init__(self, params_sample=None): def __init__(self, params_user=None):
self.default_params = None self.user_params_sample = params_user
self.user_params_sample = params_sample
@property
def default_params(self):
return None
@property @property
def params_sample(self) -> dict: def params_sample(self) -> dict:
......
...@@ -18,9 +18,9 @@ class AbstractMarginModel(AbstractModel, ABC): ...@@ -18,9 +18,9 @@ class AbstractMarginModel(AbstractModel, ABC):
-margin_function -margin_function
""" """
def __init__(self, coordinates: AbstractCoordinates, params_sample=None, def __init__(self, coordinates: AbstractCoordinates, params_user=None,
params_class=GevParams): params_class=GevParams):
super().__init__(params_sample) super().__init__(params_user)
assert isinstance(coordinates, AbstractCoordinates), type(coordinates) assert isinstance(coordinates, AbstractCoordinates), type(coordinates)
self.coordinates = coordinates self.coordinates = coordinates
self.margin_function = None # type: AbstractMarginFunction self.margin_function = None # type: AbstractMarginFunction
......
...@@ -20,13 +20,13 @@ class AbstractTemporalLinearMarginModel(LinearMarginModel): ...@@ -20,13 +20,13 @@ class AbstractTemporalLinearMarginModel(LinearMarginModel):
"""Linearity only with respect to the temporal coordinates""" """Linearity only with respect to the temporal coordinates"""
def __init__(self, coordinates: AbstractCoordinates, def __init__(self, coordinates: AbstractCoordinates,
params_sample=None, starting_point=None, params_user=None, starting_point=None,
fit_method=MarginFitMethod.is_mev_gev_fit, fit_method=MarginFitMethod.is_mev_gev_fit,
nb_iterations_for_bayesian_fit=5000, nb_iterations_for_bayesian_fit=5000,
params_initial_fit_bayesian=None, params_initial_fit_bayesian=None,
type_for_MLE="GEV", type_for_MLE="GEV",
params_class=GevParams): params_class=GevParams):
super().__init__(coordinates, params_sample, starting_point, super().__init__(coordinates, params_user, starting_point,
params_class) params_class)
self.type_for_mle = type_for_MLE self.type_for_mle = type_for_MLE
self.params_initial_fit_bayesian = params_initial_fit_bayesian self.params_initial_fit_bayesian = params_initial_fit_bayesian
......
...@@ -12,15 +12,11 @@ class LinearMarginModel(ParametricMarginModel): ...@@ -12,15 +12,11 @@ class LinearMarginModel(ParametricMarginModel):
for param_name, coef_list in param_name_to_coef_list.items(): for param_name, coef_list in param_name_to_coef_list.items():
for idx, coef in enumerate(coef_list, -1): for idx, coef in enumerate(coef_list, -1):
params[(param_name, idx)] = coef params[(param_name, idx)] = coef
return cls(coordinates, params_sample=params, params_class=params_class, **kwargs) return cls(coordinates, params_user=params, params_class=params_class, **kwargs)
def load_margin_functions(self, param_name_to_dims=None): def load_margin_functions(self, param_name_to_dims=None):
assert param_name_to_dims is not None, 'LinearMarginModel cannot be used for sampling/fitting \n' \ assert param_name_to_dims is not None, 'LinearMarginModel cannot be used for sampling/fitting \n' \
'load_margin_functions needs to be implemented in child class' 'load_margin_functions needs to be implemented in child class'
# Load default params (with a dictionary format to enable quick replacement)
# IMPORTANT: Using a dictionary format enable using the default/user params methodology
self.default_params = self.default_param_name_and_dim_to_coef
# Load sample coef # Load sample coef
coef_sample = self.param_name_to_linear_coef(param_name_and_dim_to_coef=self.params_sample) coef_sample = self.param_name_to_linear_coef(param_name_and_dim_to_coef=self.params_sample)
self.margin_function = LinearMarginFunction(coordinates=self.coordinates, self.margin_function = LinearMarginFunction(coordinates=self.coordinates,
...@@ -30,7 +26,7 @@ class LinearMarginModel(ParametricMarginModel): ...@@ -30,7 +26,7 @@ class LinearMarginModel(ParametricMarginModel):
params_class=self.params_class) params_class=self.params_class)
@property @property
def default_param_name_and_dim_to_coef(self) -> dict: def default_params(self) -> dict:
default_intercept = 1 default_intercept = 1
default_slope = 0.01 default_slope = 0.01
param_name_and_dim_to_coef = {} param_name_and_dim_to_coef = {}
......
...@@ -71,9 +71,9 @@ class NonStationaryLocationAndScaleTemporalModel(AbstractTemporalLinearMarginMod ...@@ -71,9 +71,9 @@ class NonStationaryLocationAndScaleTemporalModel(AbstractTemporalLinearMarginMod
class GumbelTemporalModel(StationaryTemporalModel): class GumbelTemporalModel(StationaryTemporalModel):
def __init__(self, coordinates: AbstractCoordinates, def __init__(self, coordinates: AbstractCoordinates,
params_sample=None, starting_point=None, fit_method=MarginFitMethod.is_mev_gev_fit, params_user=None, starting_point=None, fit_method=MarginFitMethod.is_mev_gev_fit,
nb_iterations_for_bayesian_fit=5000, params_initial_fit_bayesian=None): nb_iterations_for_bayesian_fit=5000, params_initial_fit_bayesian=None):
super().__init__(coordinates, params_sample, starting_point, fit_method, super().__init__(coordinates, params_user, starting_point, fit_method,
nb_iterations_for_bayesian_fit, params_initial_fit_bayesian, type_for_MLE="Gumbel") nb_iterations_for_bayesian_fit, params_initial_fit_bayesian, type_for_MLE="Gumbel")
......
...@@ -17,7 +17,7 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo ...@@ -17,7 +17,7 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo
class ParametricMarginModel(AbstractMarginModel, ABC): class ParametricMarginModel(AbstractMarginModel, ABC):
def __init__(self, coordinates: AbstractCoordinates, def __init__(self, coordinates: AbstractCoordinates,
params_sample=None, starting_point=None, params_class=GevParams, params_user=None, starting_point=None, params_class=GevParams,
fit_method=MarginFitMethod.spatial_extremes_mle): fit_method=MarginFitMethod.spatial_extremes_mle):
""" """
:param starting_point: starting coordinate for the temporal trend :param starting_point: starting coordinate for the temporal trend
...@@ -26,7 +26,7 @@ class ParametricMarginModel(AbstractMarginModel, ABC): ...@@ -26,7 +26,7 @@ class ParametricMarginModel(AbstractMarginModel, ABC):
self.starting_point = starting_point self.starting_point = starting_point
self.margin_function = None # type: ParametricMarginFunction self.margin_function = None # type: ParametricMarginFunction
self.drop_duplicates = True self.drop_duplicates = True
super().__init__(coordinates, params_sample, params_class) super().__init__(coordinates, params_user, params_class)
def fitmargin_from_maxima_gev(self, data: np.ndarray, df_coordinates_spat: pd.DataFrame, def fitmargin_from_maxima_gev(self, data: np.ndarray, df_coordinates_spat: pd.DataFrame,
df_coordinates_temp: pd.DataFrame) -> ResultFromSpatialExtreme: df_coordinates_temp: pd.DataFrame) -> ResultFromSpatialExtreme:
......
...@@ -12,8 +12,8 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo ...@@ -12,8 +12,8 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo
class SplineMarginModel(ParametricMarginModel): class SplineMarginModel(ParametricMarginModel):
def __init__(self, coordinates: AbstractCoordinates, def __init__(self, coordinates: AbstractCoordinates,
params_sample=None): params_user=None):
super().__init__(coordinates, params_sample) super().__init__(coordinates, params_user)
def load_margin_functions(self, param_name_to_dims: Dict[str, List[int]] = None, def load_margin_functions(self, param_name_to_dims: Dict[str, List[int]] = None,
param_name_to_coef: Dict[str, AbstractCoef] = None, param_name_to_coef: Dict[str, AbstractCoef] = None,
......
...@@ -14,8 +14,8 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo ...@@ -14,8 +14,8 @@ from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoo
class AbstractMaxStableModel(AbstractModel): class AbstractMaxStableModel(AbstractModel):
def __init__(self, params_sample=None): def __init__(self, params_user=None):
super().__init__(params_sample) super().__init__(params_user)
self.cov_mod = None self.cov_mod = None
@property @property
...@@ -105,12 +105,15 @@ class CovarianceFunction(Enum): ...@@ -105,12 +105,15 @@ class CovarianceFunction(Enum):
class AbstractMaxStableModelWithCovarianceFunction(AbstractMaxStableModel): class AbstractMaxStableModelWithCovarianceFunction(AbstractMaxStableModel):
def __init__(self, params_sample=None, def __init__(self, params_user=None,
covariance_function: CovarianceFunction = None): covariance_function: CovarianceFunction = None):
super().__init__(params_sample) super().__init__(params_user)
assert covariance_function is not None assert covariance_function is not None
self.covariance_function = covariance_function self.covariance_function = covariance_function
self.default_params = {
@property
def default_params(self):
return {
'range': 3, 'range': 3,
'smooth': 0.5, 'smooth': 0.5,
'nugget': 0.5 'nugget': 0.5
......
...@@ -9,7 +9,10 @@ class Smith(AbstractMaxStableModel): ...@@ -9,7 +9,10 @@ class Smith(AbstractMaxStableModel):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = 'gauss' self.cov_mod = 'gauss'
self.default_params = {
@property
def default_params(self):
return {
'var': 1, 'var': 1,
'cov11': 1, 'cov11': 1,
'cov12': 0, 'cov12': 0,
...@@ -29,7 +32,10 @@ class BrownResnick(AbstractMaxStableModel): ...@@ -29,7 +32,10 @@ class BrownResnick(AbstractMaxStableModel):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = 'brown' self.cov_mod = 'brown'
self.default_params = {
@property
def default_params(self):
return {
'range': 3, 'range': 3,
'smooth': 0.5, 'smooth': 0.5,
} }
...@@ -40,7 +46,6 @@ class Schlather(AbstractMaxStableModelWithCovarianceFunction): ...@@ -40,7 +46,6 @@ class Schlather(AbstractMaxStableModelWithCovarianceFunction):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = self.covariance_function.name self.cov_mod = self.covariance_function.name
self.default_params.update({})
class Geometric(AbstractMaxStableModelWithCovarianceFunction): class Geometric(AbstractMaxStableModelWithCovarianceFunction):
...@@ -48,7 +53,12 @@ class Geometric(AbstractMaxStableModelWithCovarianceFunction): ...@@ -48,7 +53,12 @@ class Geometric(AbstractMaxStableModelWithCovarianceFunction):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = 'g' + self.covariance_function.name self.cov_mod = 'g' + self.covariance_function.name
self.default_params.update({'sigma2': 0.5})
@property
def default_params(self):
d = super().default_params
d.update({'sigma2': 0.5})
return d
class ExtremalT(AbstractMaxStableModelWithCovarianceFunction): class ExtremalT(AbstractMaxStableModelWithCovarianceFunction):
...@@ -56,7 +66,12 @@ class ExtremalT(AbstractMaxStableModelWithCovarianceFunction): ...@@ -56,7 +66,12 @@ class ExtremalT(AbstractMaxStableModelWithCovarianceFunction):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = 't' + self.covariance_function.name self.cov_mod = 't' + self.covariance_function.name
self.default_params.update({'DoF': 2})
@property
def default_params(self):
d = super().default_params
d.update({'DoF': 2})
return d
class ISchlather(AbstractMaxStableModelWithCovarianceFunction): class ISchlather(AbstractMaxStableModelWithCovarianceFunction):
...@@ -64,4 +79,9 @@ class ISchlather(AbstractMaxStableModelWithCovarianceFunction): ...@@ -64,4 +79,9 @@ class ISchlather(AbstractMaxStableModelWithCovarianceFunction):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cov_mod = 'i' + self.covariance_function.name self.cov_mod = 'i' + self.covariance_function.name
self.default_params.update({'alpha': 0.5})
@property
def default_params(self):
d = super().default_params
d.update({'alpha': 0.5})
return d
...@@ -19,7 +19,7 @@ class TestVisualizationLinearMarginModel(unittest.TestCase): ...@@ -19,7 +19,7 @@ class TestVisualizationLinearMarginModel(unittest.TestCase):
def test_example_visualization_1D(self): def test_example_visualization_1D(self):
coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points) coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points)
self.margin_model = self.margin_model_class(coordinates=coordinates, params_sample={(GevParams.SHAPE, 0): 0.02}) self.margin_model = self.margin_model_class(coordinates=coordinates, params_user={(GevParams.SHAPE, 0): 0.02})
def test_example_visualization_2D_spatial(self): def test_example_visualization_2D_spatial(self):
spatial_coordinates = LinSpaceSpatial2DCoordinates.from_nb_points(nb_points=self.nb_points) spatial_coordinates = LinSpaceSpatial2DCoordinates.from_nb_points(nb_points=self.nb_points)
...@@ -42,7 +42,7 @@ class TestVisualizationSplineMarginModel(unittest.TestCase): ...@@ -42,7 +42,7 @@ class TestVisualizationSplineMarginModel(unittest.TestCase):
def test_example_visualization_1D_spline(self): def test_example_visualization_1D_spline(self):
coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points, start=0.0) coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=self.nb_points, start=0.0)
self.margin_model = self.margin_model_class(coordinates=coordinates, params_sample={(GevParams.SHAPE, 1): 0.02}) self.margin_model = self.margin_model_class(coordinates=coordinates, params_user={(GevParams.SHAPE, 1): 0.02})
def test_example_visualization_2D_spatial_spline(self): def test_example_visualization_2D_spatial_spline(self):
spatial_coordinates = LinSpaceSpatial2DCoordinates.from_nb_points(nb_points=self.nb_points) spatial_coordinates = LinSpaceSpatial2DCoordinates.from_nb_points(nb_points=self.nb_points)
......
...@@ -26,7 +26,7 @@ class TestRMaxStab(TestUnitaryAbstract): ...@@ -26,7 +26,7 @@ class TestRMaxStab(TestUnitaryAbstract):
coordinates = AbstractSpatialCoordinates.from_df(df) coordinates = AbstractSpatialCoordinates.from_df(df)
# Load max stable model # Load max stable model
params_sample = {'range': 3, 'smooth': 0.5, 'nugget': 0} params_sample = {'range': 3, 'smooth': 0.5, 'nugget': 0}
max_stable_model = Schlather(covariance_function=CovarianceFunction.whitmat, params_sample=params_sample) max_stable_model = Schlather(covariance_function=CovarianceFunction.whitmat, params_user=params_sample)
return coordinates, max_stable_model return coordinates, max_stable_model
@property @property
......
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