diff --git a/extreme_estimator/R_fit/gev_fit/gev_marginal.py b/extreme_estimator/R_fit/gev_fit/gev_marginal.py index 424eecd084d9ee2b59cbe5753adef3a7127f7507..8b15be5850ea08103b6c5f20d437baf9f5b2c7c4 100644 --- a/extreme_estimator/R_fit/gev_fit/gev_marginal.py +++ b/extreme_estimator/R_fit/gev_fit/gev_marginal.py @@ -6,6 +6,7 @@ def frechet_unitary_transformation(data, location, scale, shape): Compute the unitary Frechet transformed data (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) diff --git a/extreme_estimator/R_fit/gev_fit/gev_fit.R b/extreme_estimator/R_fit/gev_fit/gev_mle_fit.R similarity index 100% rename from extreme_estimator/R_fit/gev_fit/gev_fit.R rename to extreme_estimator/R_fit/gev_fit/gev_mle_fit.R diff --git a/extreme_estimator/R_fit/gev_fit/gev_mle_fit.py b/extreme_estimator/R_fit/gev_fit/gev_mle_fit.py index 5bfe53f38905c32542fccdc55d8597a6d246d4fe..e56280b3bdd1703be99fb7ac4156cef9e065c2ee 100644 --- a/extreme_estimator/R_fit/gev_fit/gev_mle_fit.py +++ b/extreme_estimator/R_fit/gev_fit/gev_mle_fit.py @@ -1,9 +1,11 @@ import rpy2.robjects as ro import numpy as np import rpy2.robjects.numpy2ri as rpyn - +import os.path as op # Defining some constants +from extreme_estimator.R_fit.utils import get_associated_r_file + GEV_SCALE = 'scale' GEV_LOCATION = 'loc' GEV_SHAPE = 'shape' @@ -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) r.assign('x_gev', x_gev) 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) mle_params = dict(r.attr(res, 'coef').items()) return mle_params diff --git a/extreme_estimator/R_fit/utils.py b/extreme_estimator/R_fit/utils.py index 8e308400161397f53be82873aa5b9451e078231c..87a89fb947fb1504a02892e901c285e5c25441a6 100644 --- a/extreme_estimator/R_fit/utils.py +++ b/extreme_estimator/R_fit/utils.py @@ -1,16 +1,23 @@ +import os.path as op import rpy2.robjects as ro import pandas as pd import numpy as np import rpy2.robjects.numpy2ri as npr -def get_loaded_r(): +def get_loaded_r() -> ro.R: r = ro.r ro.numpy2ri.activate() r.library('SpatialExtremes') 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): # """Convert DataFrame or numpy array into FloatVector for r""" # if isinstance(data, pd.DataFrame):