From 9dda7ae697d38499daed70f97dc6ee0b26d4322e Mon Sep 17 00:00:00 2001
From: Le Roux Erwan <erwan.le-roux@irstea.fr>
Date: Tue, 19 Mar 2019 15:55:42 +0100
Subject: [PATCH] [EXTREME MODEL] rename smooth_margin_model into
 linear_margin_model

---
 .../study_visualization/study_visualizer.py   |   2 +-
 .../regression_margin/regression_margin.py    |   2 +-
 .../simulation/lin_space2_simulation.py       |   2 +-
 experiment/simulation/lin_space_simulation.py |   2 +-
 .../full_estimator/abstract_full_estimator.py |   2 +-
 .../full_estimator_for_simulation.py          |   2 +-
 .../abstract_margin_estimator.py              |   2 +-
 .../margin_estimator_for_simulation.py        |   2 +-
 .../margin_model/linear_margin_model.py       | 127 ++++++++++++++++++
 .../test_extreme_models/test_margin_model.py  |   2 +-
 .../test_slicer.py                            |   2 +-
 .../test_fitmaxstab_with_margin.py            |   2 +-
 .../test_rmaxstab_with_margin.py              |   2 +-
 test/test_utils.py                            |   2 +-
 14 files changed, 140 insertions(+), 13 deletions(-)
 create mode 100644 extreme_estimator/extreme_models/margin_model/linear_margin_model.py

diff --git a/experiment/meteo_france_SCM_study/visualization/study_visualization/study_visualizer.py b/experiment/meteo_france_SCM_study/visualization/study_visualization/study_visualizer.py
index fe6ab50a..2abb47e7 100644
--- a/experiment/meteo_france_SCM_study/visualization/study_visualization/study_visualizer.py
+++ b/experiment/meteo_france_SCM_study/visualization/study_visualization/study_visualizer.py
@@ -14,7 +14,7 @@ from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \
     FullEstimatorInASingleStepWithSmoothMargin
 from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator import SmoothMarginEstimator
 from extreme_estimator.extreme_models.margin_model.param_function.param_function import ParamFunction
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearAllParametersAllDimsMarginModel
 from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import CovarianceFunction
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import BrownResnick
 from extreme_estimator.margin_fits.abstract_params import AbstractParams
diff --git a/experiment/regression_margin/regression_margin.py b/experiment/regression_margin/regression_margin.py
index b38308a7..31a65482 100644
--- a/experiment/regression_margin/regression_margin.py
+++ b/experiment/regression_margin/regression_margin.py
@@ -2,7 +2,7 @@ import numpy as np
 
 from extreme_estimator.estimator.full_estimator.abstract_full_estimator import FullEstimatorInASingleStepWithSmoothMargin
 from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearAllParametersAllDimsMarginModel, \
     ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
 from extreme_estimator.margin_fits.gev.gev_params import GevParams
diff --git a/experiment/simulation/lin_space2_simulation.py b/experiment/simulation/lin_space2_simulation.py
index b3ab8757..b25c00b8 100644
--- a/experiment/simulation/lin_space2_simulation.py
+++ b/experiment/simulation/lin_space2_simulation.py
@@ -2,7 +2,7 @@ from experiment.simulation.abstract_simulation import AbstractSimulation
 from extreme_estimator.estimator.full_estimator.full_estimator_for_simulation import FULL_ESTIMATORS_FOR_SIMULATION
 from extreme_estimator.estimator.margin_estimator.margin_estimator_for_simulation import \
     MARGIN_ESTIMATORS_FOR_SIMULATION
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
 from extreme_estimator.margin_fits.gev.gev_params import GevParams
 from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates
diff --git a/experiment/simulation/lin_space_simulation.py b/experiment/simulation/lin_space_simulation.py
index 96e3618c..b00ec2d7 100644
--- a/experiment/simulation/lin_space_simulation.py
+++ b/experiment/simulation/lin_space_simulation.py
@@ -1,5 +1,5 @@
 from experiment.simulation.abstract_simulation import AbstractSimulation
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
 from extreme_estimator.margin_fits.gev.gev_params import GevParams
 from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates
diff --git a/extreme_estimator/estimator/full_estimator/abstract_full_estimator.py b/extreme_estimator/estimator/full_estimator/abstract_full_estimator.py
index 16652bce..8ba3b479 100644
--- a/extreme_estimator/estimator/full_estimator/abstract_full_estimator.py
+++ b/extreme_estimator/estimator/full_estimator/abstract_full_estimator.py
@@ -3,7 +3,7 @@ from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator impo
 from extreme_estimator.estimator.max_stable_estimator.abstract_max_stable_estimator import MaxStableEstimator
 from extreme_estimator.extreme_models.margin_model.abstract_margin_model import AbstractMarginModel
 from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearMarginModel
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearMarginModel
 from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import AbstractMaxStableModel
 from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
 
diff --git a/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py b/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py
index ef250526..dd436b01 100644
--- a/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py
+++ b/extreme_estimator/estimator/full_estimator/full_estimator_for_simulation.py
@@ -1,6 +1,6 @@
 from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \
     FullEstimatorInASingleStepWithSmoothMargin
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearAllParametersAllDimsMarginModel, \
     ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
 from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
diff --git a/extreme_estimator/estimator/margin_estimator/abstract_margin_estimator.py b/extreme_estimator/estimator/margin_estimator/abstract_margin_estimator.py
index 1340adfe..a1b8264b 100644
--- a/extreme_estimator/estimator/margin_estimator/abstract_margin_estimator.py
+++ b/extreme_estimator/estimator/margin_estimator/abstract_margin_estimator.py
@@ -3,7 +3,7 @@ from abc import ABC
 from extreme_estimator.estimator.abstract_estimator import AbstractEstimator
 from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function import \
     AbstractMarginFunction
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearMarginModel, \
     LinearAllParametersAllDimsMarginModel
 from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
 
diff --git a/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py b/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py
index 1bd08026..3ce73ecb 100644
--- a/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py
+++ b/extreme_estimator/estimator/margin_estimator/margin_estimator_for_simulation.py
@@ -1,5 +1,5 @@
 from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator import SmoothMarginEstimator
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearAllParametersAllDimsMarginModel, \
     ConstantMarginModel
 from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
 
diff --git a/extreme_estimator/extreme_models/margin_model/linear_margin_model.py b/extreme_estimator/extreme_models/margin_model/linear_margin_model.py
new file mode 100644
index 00000000..1e77a4ea
--- /dev/null
+++ b/extreme_estimator/extreme_models/margin_model/linear_margin_model.py
@@ -0,0 +1,127 @@
+import numpy as np
+import pandas as pd
+
+from extreme_estimator.extreme_models.result_from_fit import ResultFromFit
+from extreme_estimator.extreme_models.margin_model.abstract_margin_model import AbstractMarginModel
+from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
+from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef
+from extreme_estimator.extreme_models.utils import safe_run_r_estimator, r, get_coord, \
+    get_margin_formula
+from extreme_estimator.margin_fits.gev.gev_params import GevParams
+
+
+class LinearMarginModel(AbstractMarginModel):
+
+    def load_margin_functions(self, gev_param_name_to_linear_dims=None):
+        assert gev_param_name_to_linear_dims is not None, 'LinearMarginModel cannot be used for sampling/fitting \n' \
+                                                          'load_margin_functions needs to be implemented in child class'
+        # Load sample coef
+        self.default_params_sample = self.default_param_name_and_dim_to_coef()
+        linear_coef_sample = self.gev_param_name_to_linear_coef(param_name_and_dim_to_coef=self.params_sample)
+        self.margin_function_sample = LinearMarginFunction(coordinates=self.coordinates,
+                                                           gev_param_name_to_linear_coef=linear_coef_sample,
+                                                           gev_param_name_to_linear_dims=gev_param_name_to_linear_dims)
+
+        # Load start fit coef
+        self.default_params_start_fit = self.default_param_name_and_dim_to_coef()
+        linear_coef_start_fit = self.gev_param_name_to_linear_coef(param_name_and_dim_to_coef=self.params_start_fit)
+        self.margin_function_start_fit = LinearMarginFunction(coordinates=self.coordinates,
+                                                              gev_param_name_to_linear_coef=linear_coef_start_fit,
+                                                              gev_param_name_to_linear_dims=gev_param_name_to_linear_dims)
+
+    @staticmethod
+    def default_param_name_and_dim_to_coef() -> dict:
+        default_intercept = 1
+        default_slope = 0.01
+        gev_param_name_and_dim_to_coef = {}
+        for gev_param_name in GevParams.PARAM_NAMES:
+            gev_param_name_and_dim_to_coef[(gev_param_name, 0)] = default_intercept
+            for dim in [1, 2, 3]:
+                gev_param_name_and_dim_to_coef[(gev_param_name, dim)] = default_slope
+        return gev_param_name_and_dim_to_coef
+
+    @staticmethod
+    def gev_param_name_to_linear_coef(param_name_and_dim_to_coef):
+        gev_param_name_to_linear_coef = {}
+        for gev_param_name in GevParams.PARAM_NAMES:
+            dim_to_coef = {dim: param_name_and_dim_to_coef[(gev_param_name, dim)] for dim in [0, 1, 2, 3]}
+            linear_coef = LinearCoef(gev_param_name=gev_param_name, dim_to_coef=dim_to_coef)
+            gev_param_name_to_linear_coef[gev_param_name] = linear_coef
+        return gev_param_name_to_linear_coef
+
+    @classmethod
+    def from_coef_list(cls, coordinates, gev_param_name_to_coef_list):
+        params = {}
+        for gev_param_name in GevParams.PARAM_NAMES:
+            for dim, coef in enumerate(gev_param_name_to_coef_list[gev_param_name]):
+                params[(gev_param_name, dim)] = coef
+        return cls(coordinates, params_sample=params, params_start_fit=params)
+
+    def fitmargin_from_maxima_gev(self, maxima_gev: np.ndarray, df_coordinates_spat: pd.DataFrame,
+                                  df_coordinates_temp: pd.DataFrame) -> ResultFromFit:
+        # The reshaping on the line below is only valid if we have a single observation per spatio-temporal point
+        if maxima_gev.shape[1] == 1:
+            maxima_gev = maxima_gev.reshape([len(df_coordinates_temp), len(df_coordinates_spat)])
+        data = np.transpose(maxima_gev)
+
+        fit_params = get_margin_formula(self.margin_function_start_fit.form_dict)
+
+        # Covariables
+        covariables = get_coord(df_coordinates=df_coordinates_spat)
+        fit_params['temp.cov'] = get_coord(df_coordinates=df_coordinates_temp)
+
+        # Start parameters
+        coef_dict = self.margin_function_start_fit.coef_dict
+        fit_params['start'] = r.list(**coef_dict)
+
+        return safe_run_r_estimator(function=r.fitspatgev, use_start=self.use_start_value, data=data,
+                                    covariables=covariables, **fit_params)
+
+
+class ConstantMarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({})
+
+
+class LinearShapeDim1MarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({GevParams.SHAPE: [1]})
+
+
+class LinearScaleDim1MarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({GevParams.SCALE: [1]})
+
+
+class LinearShapeDim1and2MarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({GevParams.SHAPE: [1, 2]})
+
+
+class LinearAllParametersDim1MarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({GevParams.SHAPE: [1],
+                                       GevParams.LOC: [1],
+                                       GevParams.SCALE: [1]})
+
+
+class LinearMarginModelExample(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        super().load_margin_functions({GevParams.SHAPE: [1],
+                                       GevParams.LOC: [2],
+                                       GevParams.SCALE: [1]})
+
+
+class LinearAllParametersAllDimsMarginModel(LinearMarginModel):
+
+    def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
+        all_dims = list(range(1, self.coordinates.nb_coordinates + 1))
+        super().load_margin_functions({GevParams.SHAPE: all_dims.copy(),
+                                       GevParams.LOC: all_dims.copy(),
+                                       GevParams.SCALE: all_dims.copy()})
diff --git a/test/test_extreme_estimator/test_extreme_models/test_margin_model.py b/test/test_extreme_estimator/test_extreme_models/test_margin_model.py
index 886aa4e4..552a4b24 100644
--- a/test/test_extreme_estimator/test_extreme_models/test_margin_model.py
+++ b/test/test_extreme_estimator/test_extreme_models/test_margin_model.py
@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.abstract_marg
     AbstractMarginFunction
 from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
 from extreme_estimator.margin_fits.gev.gev_params import GevParams
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearShapeDim1MarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearShapeDim1MarginModel, \
     LinearAllParametersAllDimsMarginModel
 from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_2D import LinSpaceSpatial2DCoordinates
 from spatio_temporal_dataset.coordinates.spatial_coordinates.generated_spatial_coordinates import \
diff --git a/test/test_spatio_temporal_dataset/test_slicer.py b/test/test_spatio_temporal_dataset/test_slicer.py
index 6551bee3..5a99be0a 100644
--- a/test/test_spatio_temporal_dataset/test_slicer.py
+++ b/test/test_spatio_temporal_dataset/test_slicer.py
@@ -2,7 +2,7 @@ from typing import List
 
 import unittest
 
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
 from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
 from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset
diff --git a/test/test_unitary/test_fitmaxstab/test_fitmaxstab_with_margin.py b/test/test_unitary/test_fitmaxstab/test_fitmaxstab_with_margin.py
index 0598d4e2..45ccd462 100644
--- a/test/test_unitary/test_fitmaxstab/test_fitmaxstab_with_margin.py
+++ b/test/test_unitary/test_fitmaxstab/test_fitmaxstab_with_margin.py
@@ -2,7 +2,7 @@ import unittest
 
 from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \
     FullEstimatorInASingleStepWithSmoothMargin
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import ConstantMarginModel, \
     LinearMarginModelExample
 from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import CovarianceFunction
 from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Schlather
diff --git a/test/test_unitary/test_rmaxstab/test_rmaxstab_with_margin.py b/test/test_unitary/test_rmaxstab/test_rmaxstab_with_margin.py
index 0f485e94..c4825bf6 100644
--- a/test/test_unitary/test_rmaxstab/test_rmaxstab_with_margin.py
+++ b/test/test_unitary/test_rmaxstab/test_rmaxstab_with_margin.py
@@ -2,7 +2,7 @@ import unittest
 
 import numpy as np
 
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import ConstantMarginModel, \
     LinearAllParametersAllDimsMarginModel
 from extreme_estimator.extreme_models.utils import r
 from extreme_estimator.margin_fits.gev.gev_params import GevParams
diff --git a/test/test_utils.py b/test/test_utils.py
index 39d098eb..ca1f800b 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -7,7 +7,7 @@ from experiment.meteo_france_SCM_study.crocus.crocus_variables import CrocusSweV
 from extreme_estimator.estimator.full_estimator.abstract_full_estimator import SmoothMarginalsThenUnitaryMsp, \
     FullEstimatorInASingleStepWithSmoothMargin
 from extreme_estimator.estimator.max_stable_estimator.abstract_max_stable_estimator import MaxStableEstimator
-from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
+from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearAllParametersAllDimsMarginModel, \
     ConstantMarginModel
 from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import \
     AbstractMaxStableModelWithCovarianceFunction, CovarianceFunction
-- 
GitLab