Commit 1073545c authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[projection snowfall] refactor the formula for the effects. add...

[projection snowfall] refactor the formula for the effects. add param_name_to_ordered_climate_effects for the spline_margin_function.py and for the polynomial_margin_function.py. add some assertion
parent 6274b11e
No related merge requests found
Showing with 39 additions and 13 deletions
+39 -13
......@@ -103,6 +103,14 @@ class ParametricMarginFunction(IndependentMarginFunction):
coordinates=coordinates)
param_name_to_coef[param_name] = coef
# Load param_name_to_ordered_climate_effects
param_name_to_ordered_climate_effects = cls.load_param_name_to_ordered_climate_effects(coef_dict,
name_of_the_climatic_effects)
return cls(coordinates, param_name_to_dims, param_name_to_coef,
starting_point=starting_point, log_scale=log_scale,
param_name_to_ordered_climate_effects=param_name_to_ordered_climate_effects)
@classmethod
def load_param_name_to_ordered_climate_effects(cls, coef_dict, name_of_the_climatic_effects):
if name_of_the_climatic_effects is None:
param_name_to_ordered_climate_effects = None
else:
......@@ -110,9 +118,7 @@ class ParametricMarginFunction(IndependentMarginFunction):
for param_name in GevParams.PARAM_NAMES:
ordered_climate_effects = [coef_dict[param_name + name] for name in name_of_the_climatic_effects]
param_name_to_ordered_climate_effects[param_name] = ordered_climate_effects
return cls(coordinates, param_name_to_dims, param_name_to_coef,
starting_point=starting_point, log_scale=log_scale,
param_name_to_ordered_climate_effects=param_name_to_ordered_climate_effects)
return param_name_to_ordered_climate_effects
@property
def form_dict(self) -> Dict[str, str]:
......
......@@ -14,13 +14,15 @@ class PolynomialMarginFunction(LinearMarginFunction):
def __init__(self, coordinates: AbstractCoordinates, param_name_to_dim_and_max_degree: Dict[str, List[Tuple[int, int]]],
param_name_to_coef: Dict[str, PolynomialAllCoef], starting_point: Union[None, int] = None,
params_class: type = GevParams, log_scale=None):
params_class: type = GevParams, log_scale=None,
param_name_to_ordered_climate_effects=None):
param_name_to_dims = {}
for param_name in param_name_to_dim_and_max_degree.keys():
dims = [c[0] for c in param_name_to_dim_and_max_degree[param_name]]
param_name_to_dims[param_name] = dims
self.param_name_to_dim_and_max_degree = param_name_to_dim_and_max_degree
super().__init__(coordinates, param_name_to_dims, param_name_to_coef, starting_point, params_class, log_scale)
super().__init__(coordinates, param_name_to_dims, param_name_to_coef, starting_point, params_class, log_scale,
param_name_to_ordered_climate_effects)
COEF_CLASS = PolynomialAllCoef
......@@ -48,5 +50,9 @@ class PolynomialMarginFunction(LinearMarginFunction):
dims=dims,
coordinates=coordinates)
param_name_to_coef[param_name] = coef
return cls(coordinates, param_name_to_dim_and_max_degree, param_name_to_coef, starting_point, log_scale=log_scale)
param_name_to_ordered_climate_effects = cls.load_param_name_to_ordered_climate_effects(coef_dict, name_of_the_climatic_effects)
return cls(coordinates, param_name_to_dim_and_max_degree, param_name_to_coef, starting_point, log_scale=log_scale,
param_name_to_ordered_climate_effects=param_name_to_ordered_climate_effects)
......@@ -18,13 +18,15 @@ class SplineMarginFunction(LinearMarginFunction):
def __init__(self, coordinates: AbstractCoordinates,
param_name_to_dim_and_max_degree: Dict[str, List[Tuple[int, int]]],
param_name_to_coef: Dict[str, SplineAllCoef], starting_point: Union[None, int] = None,
params_class: type = GevParams, log_scale=None):
params_class: type = GevParams, log_scale=None,
param_name_to_ordered_climate_effects=None):
param_name_to_dims = {}
for param_name in param_name_to_dim_and_max_degree.keys():
dims = [c[0] for c in param_name_to_dim_and_max_degree[param_name]]
param_name_to_dims[param_name] = dims
self.param_name_to_dim_and_max_degree = param_name_to_dim_and_max_degree
super().__init__(coordinates, param_name_to_dims, param_name_to_coef, starting_point, params_class, log_scale)
super().__init__(coordinates, param_name_to_dims, param_name_to_coef, starting_point, params_class, log_scale,
param_name_to_ordered_climate_effects)
COEF_CLASS = SplineAllCoef
......@@ -50,7 +52,9 @@ class SplineMarginFunction(LinearMarginFunction):
coef_dict, spline_param_name_to_dim_to_knots_and_coefficient = coef_dict
# Load polynomial coefficient
polynomial_margin_function = PolynomialMarginFunction.from_coef_dict(coordinates, param_name_to_dims, coef_dict, starting_point, log_scale)
polynomial_margin_function = PolynomialMarginFunction.from_coef_dict(coordinates, param_name_to_dims, coef_dict,
starting_point, log_scale,
name_of_the_climatic_effects)
param_name_to_coef = polynomial_margin_function.param_name_to_coef
param_name_to_dim_and_max_degree = param_name_to_dims
# Load the remaining spline coefficient
......@@ -60,4 +64,6 @@ class SplineMarginFunction(LinearMarginFunction):
for dim, (knots, coefficients) in dim_to_knots_and_coefficients.items():
dim_to_spline_coef[dim] = SplineCoef(param_name, coefficients, knots)
param_name_to_coef[param_name] = SplineAllCoef(param_name, dim_to_spline_coef)
return cls(coordinates, param_name_to_dim_and_max_degree, param_name_to_coef, starting_point, log_scale=log_scale)
return cls(coordinates, param_name_to_dim_and_max_degree, param_name_to_coef, starting_point, log_scale=log_scale,
param_name_to_ordered_climate_effects=polynomial_margin_function.param_name_to_ordered_climate_effects)
......@@ -78,10 +78,13 @@ class AbstractTemporalLinearMarginModel(LinearMarginModel):
# Add potential climate effects
name_of_the_climatic_effects = [c for c in df_coordinates_temp.columns if
c not in AbstractCoordinates.COORDINATES_NAMES]
name_of_the_climatic_effects_for_formula = [" + {}".format(c) for c in name_of_the_climatic_effects]
formula_effect_str = ' '.join(name_of_the_climatic_effects_for_formula)
name_of_the_climatic_effects_for_formula = ["{}".format(c) for c in name_of_the_climatic_effects]
formula_effect_str = ' + '.join(name_of_the_climatic_effects_for_formula)
# We apply the effect on all the parameters
formula_list = [f + ' ' + formula_effect_str for f in formula_list]
if len(name_of_the_climatic_effects) > 0:
formula_list = [f.replace(' 1', '') for f in formula_list]
formula_list = [f + ' + ' if f[-2:] != '~ ' else f for f in formula_list]
formula_list = [f + formula_effect_str for f in formula_list]
formula = r.list(*[robjects.Formula(f) for f in formula_list])
df = pd.DataFrame({maxima_column_name: np.array(x)})
df = pd.concat([df, df_coordinates_spat, df_coordinates_temp], axis=1)
......
......@@ -48,4 +48,9 @@ def get_margin_coef_ordered_dict(param_name_to_dims, mle_values, type_for_mle="G
coef_name = param_name + name
coef_dict[coef_name] = mle_values[i]
i += 1
if type_for_mle == "Gumbel":
assert len(coef_dict) == len(mle_values) + 1
else:
assert len(coef_dict) == len(mle_values)
return coef_dict
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