Commit 13d6d211 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[EXTREME FIT][GEV FIT] fix sourcing issue

parent 77547959
No related merge requests found
Showing with 13 additions and 3 deletions
+13 -3
...@@ -6,6 +6,7 @@ def frechet_unitary_transformation(data, location, scale, shape): ...@@ -6,6 +6,7 @@ def frechet_unitary_transformation(data, location, scale, shape):
Compute the unitary Frechet transformed data Compute the unitary Frechet transformed data
(1 + \zeta \frac{z - \mu}{\sigma}) ^ {\frac{1}{\zeta}} (1 + \zeta \frac{z - \mu}{\sigma}) ^ {\frac{1}{\zeta}}
""" """
# todo: there is already a function doing that in R
return (1 + shape * (data - location) / scale) ** (1 / shape) return (1 + shape * (data - location) / scale) ** (1 / shape)
......
import rpy2.robjects as ro import rpy2.robjects as ro
import numpy as np import numpy as np
import rpy2.robjects.numpy2ri as rpyn import rpy2.robjects.numpy2ri as rpyn
import os.path as op
# Defining some constants # Defining some constants
from extreme_estimator.R_fit.utils import get_associated_r_file
GEV_SCALE = 'scale' GEV_SCALE = 'scale'
GEV_LOCATION = 'loc' GEV_LOCATION = 'loc'
GEV_SHAPE = 'shape' GEV_SHAPE = 'shape'
...@@ -16,7 +18,7 @@ def mle_gev(x_gev: np.ndarray, start_loc=0, start_scale=1, start_shape=0): ...@@ -16,7 +18,7 @@ def mle_gev(x_gev: np.ndarray, start_loc=0, start_scale=1, start_shape=0):
x_gev = rpyn.numpy2ri(x_gev) x_gev = rpyn.numpy2ri(x_gev)
r.assign('x_gev', x_gev) r.assign('x_gev', x_gev)
r.assign('python_wrapping', True) r.assign('python_wrapping', True)
r.source(file="/home/erwan/Documents/projects/spatiotemporalextremes/extreme_estimator/gev_fit/gev_fit.extreme_estimator") r.source(file=get_associated_r_file(python_filepath=op.abspath(__file__)))
res = r.mle_gev(loc=start_loc, scale=start_scale, shape=start_shape) res = r.mle_gev(loc=start_loc, scale=start_scale, shape=start_shape)
mle_params = dict(r.attr(res, 'coef').items()) mle_params = dict(r.attr(res, 'coef').items())
return mle_params return mle_params
......
import os.path as op
import rpy2.robjects as ro import rpy2.robjects as ro
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import rpy2.robjects.numpy2ri as npr import rpy2.robjects.numpy2ri as npr
def get_loaded_r(): def get_loaded_r() -> ro.R:
r = ro.r r = ro.r
ro.numpy2ri.activate() ro.numpy2ri.activate()
r.library('SpatialExtremes') r.library('SpatialExtremes')
return r return r
def get_associated_r_file(python_filepath: str) -> str:
assert op.exists(python_filepath)
r_filepath = python_filepath.replace('.py', '.R')
assert op.exists(r_filepath), r_filepath
return r_filepath
# def conversion_to_FloatVector(data): # def conversion_to_FloatVector(data):
# """Convert DataFrame or numpy array into FloatVector for r""" # """Convert DataFrame or numpy array into FloatVector for r"""
# if isinstance(data, pd.DataFrame): # if isinstance(data, pd.DataFrame):
......
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