Commit 7e43b6ce authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[contrasting] put "'~ 1' in" instead of "'1' in" which fix bugs. add dims to get the form_dict

parent 797e77d3
No related merge requests found
Showing with 19 additions and 15 deletions
+19 -15
......@@ -27,7 +27,7 @@ coord = data.frame(coord, stringsAsFactors = TRUE)
# res = fevd_fixed(x_gev, data=coord, method='MLE', verbose=TRUE, use.phi=FALSE)
# res = fevd_fixed(x_gev, data=coord, location.fun= ~T, scale.fun= ~T, method='MLE', type="GEV", verbose=FALSE, use.phi=FALSE)
# res = fevd_fixed(x_gev, data=coord, location.fun= ~sin(X) + cos(T), method='MLE', type="GEV", verbose=FALSE, use.phi=FALSE)
res = fevd_fixed(x_gev, data=coord, location.fun= ~poly(X, 1, raw = TRUE) + 1 , method='MLE', type="GEV", verbose=FALSE, use.phi=FALSE)
res = fevd_fixed(x_gev, data=coord, location.fun= ~poly(X, 1, raw = TRUE) + poly(T, 2, raw = TRUE) , method='MLE', type="GEV", verbose=FALSE, use.phi=FALSE)
print(res)
# Some display for the results
......
......@@ -66,22 +66,26 @@ class LinearMarginFunction(ParametricMarginFunction):
@property
def form_dict(self) -> Dict[str, str]:
coordinate_name_to_dim = self.coefficient_name_to_dim(self.coordinates)
form_dict = {}
for param_name in self.params_class.PARAM_NAMES:
linear_dims = self.param_name_to_dims.get(param_name, [])
# Load spatial form_dict (only if we have some spatial coordinates)
if self.coordinates.has_spatial_coordinates:
spatial_names = [name for name in self.coordinates.spatial_coordinates_names
if self.coefficient_name_to_dim(self.coordinates)[name] in linear_dims]
spatial_form = self.param_name_to_coef[param_name].spatial_form_dict(spatial_names)
if coordinate_name_to_dim[name] in linear_dims]
spatial_dims = [coordinate_name_to_dim[name] for name in spatial_names]
spatial_form = self.param_name_to_coef[param_name].spatial_form_dict(spatial_names, spatial_dims)
form_dict.update(spatial_form)
# Load temporal form dict (only if we have some temporal coordinates)
if self.coordinates.has_temporal_coordinates:
temporal_names = [name for name in self.coordinates.temporal_coordinates_names
if self.coefficient_name_to_dim(self.coordinates)[name] in linear_dims]
temporal_form = self.param_name_to_coef[param_name].temporal_form_dict(temporal_names)
if coordinate_name_to_dim[name] in linear_dims]
temporal_dims = [coordinate_name_to_dim[name] for name in temporal_names]
temporal_form = self.param_name_to_coef[param_name].temporal_form_dict(temporal_names, temporal_dims)
# Specifying a formula '~ 1' creates a bug in fitspatgev of SpatialExtreme R package
assert not any(['1' in formula for formula in temporal_form.values()])
assert not any(['~ 1' in formula for formula in temporal_form.values()])
form_dict.update(temporal_form)
return form_dict
......
......@@ -34,6 +34,6 @@ class AbstractCoef(object):
""" Form dict """
def form_dict(self, names: List[str]) -> Dict[str, str]:
def form_dict(self, names: List[str], dims) -> Dict[str, str]:
formula_str = '1' if not names else '+'.join(names)
return {self.param_name + '.form': self.param_name + ' ~ ' + formula_str}
\ No newline at end of file
......@@ -75,7 +75,7 @@ class LinearCoef(AbstractCoef):
j += 1
return coef_dict
def spatial_form_dict(self, coordinate_spatial_names: List[str]) -> Dict[str, str]:
def spatial_form_dict(self, coordinate_spatial_names: List[str], spatial_dims) -> Dict[str, str]:
"""
Example of formula that could be specified:
loc.form = loc ~ 1
......@@ -85,9 +85,9 @@ class LinearCoef(AbstractCoef):
:return:
"""
assert all([name in AbstractCoordinates.COORDINATE_SPATIAL_NAMES for name in coordinate_spatial_names])
return self.form_dict(coordinate_spatial_names)
return self.form_dict(coordinate_spatial_names, spatial_dims)
def temporal_form_dict(self, coordinate_temporal_names: List[str]) -> Dict[str, str]:
def temporal_form_dict(self, coordinate_temporal_names: List[str], temporal_dims) -> Dict[str, str]:
"""
Example of formula that could be specified:
temp.form.loc = loc ~ coord_t
......@@ -96,7 +96,7 @@ class LinearCoef(AbstractCoef):
:return:
"""
assert all([name in [AbstractCoordinates.COORDINATE_T] for name in coordinate_temporal_names])
k, v = self.form_dict(coordinate_temporal_names).popitem()
k, v = self.form_dict(coordinate_temporal_names, temporal_dims).popitem()
k = 'temp.form.' + k.split('.')[0]
v = 'NULL' if '1' in v else v
v = 'NULL' if '~ 1' in v else v
return {k: v}
......@@ -70,7 +70,7 @@ class PolynomialAllCoef(LinearCoef):
dim_to_polynomial_coef[dim] = PolynomialCoef(param_name=param_name, degree_to_coef=degree_to_coef)
return cls(param_name=param_name, dim_to_polynomial_coef=dim_to_polynomial_coef, intercept=intercept)
def form_dict(self, coordinates_names: List[str]) -> Dict[str, str]:
def form_dict(self, coordinates_names: List[str], dims) -> Dict[str, str]:
if len(coordinates_names) >= 2:
raise NotImplementedError(
'Check how do we sum two polynomails without having two times an intercept parameter')
......@@ -78,7 +78,7 @@ class PolynomialAllCoef(LinearCoef):
if len(coordinates_names) == 0:
formula_str = '1'
else:
for dim, name in enumerate(coordinates_names):
for dim, name in zip(dims, coordinates_names):
polynomial_coef = self.dim_to_polynomial_coef[dim]
formula_list.append('poly({}, {}, raw = TRUE)'.format(name, polynomial_coef.max_degree))
formula_str = ' '.join(formula_list)
......
......@@ -19,6 +19,7 @@ class ResultFromMleExtremes(AbstractResultFromExtremes):
@property
def margin_coef_ordered_dict(self):
values = self.name_to_value['results']
print(values)
d = self.get_python_dictionary(values)
if 'par' in d:
values = {i: param for i, param in enumerate(np.array(d['par']))}
......
......@@ -41,7 +41,6 @@ class TestGevTemporalQuadraticExtremesMle(unittest.TestCase):
sample_fit = massif_fit.sample_id_to_sample_fit[0]
model_fit = sample_fit.model_class_to_model_fit[model_class] # type: TwoFoldModelFit
estimator = model_fit.estimator_fold_1
print(estimator.dataset)
return estimator
# def test_location_spatio_temporal_linearity(self):
......
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