From 13d6d2112360189204afd3ba8f84ecf85097047e Mon Sep 17 00:00:00 2001
From: Le Roux Erwan <erwan.le-roux@irstea.fr>
Date: Mon, 5 Nov 2018 16:50:19 +0100
Subject: [PATCH] [EXTREME FIT][GEV FIT] fix sourcing issue

---
 extreme_estimator/R_fit/gev_fit/gev_marginal.py          | 1 +
 .../R_fit/gev_fit/{gev_fit.R => gev_mle_fit.R}           | 0
 extreme_estimator/R_fit/gev_fit/gev_mle_fit.py           | 6 ++++--
 extreme_estimator/R_fit/utils.py                         | 9 ++++++++-
 4 files changed, 13 insertions(+), 3 deletions(-)
 rename extreme_estimator/R_fit/gev_fit/{gev_fit.R => gev_mle_fit.R} (100%)

diff --git a/extreme_estimator/R_fit/gev_fit/gev_marginal.py b/extreme_estimator/R_fit/gev_fit/gev_marginal.py
index 424eecd0..8b15be58 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 5bfe53f3..e56280b3 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 8e308400..87a89fb9 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):
-- 
GitLab