judithabk6 / med_bench

BSD 3-Clause "New" or "Revised" License
8 stars 3 forks source link

weird error pattern (legacy from get_estimation) #77

Open judithabk6 opened 3 months ago

judithabk6 commented 3 months ago

When a R dependency is missing, the estimator return None with a warning. It hence badly interacts with get_estimation error pattern

In [8]: from med_bench.get_simulated_data import simulate_data
In [12]: from numpy.random import default_rng

In [13]: data = simulate_data(
    ...:     n=SAMPLE_SIZE,
    ...:     rg=default_rng(45),
    ...:     mis_spec_m=False,
    ...:     mis_spec_y=False,
    ...:     dim_x=1,
    ...:     dim_m=1,
    ...:     seed=9,
    ...:     type_m="binary",
    ...:     sigma_y=0.5,
    ...:     sigma_m=0.5,
    ...:     beta_t_factor=0.5,
    ...:     beta_m_factor=0.5,
    ...: )
    ...: 
    ...: x, t, m, y = data[0], data[1], data[2], data[3]
In [15]: from med_bench.get_estimation import get_estimation

In [16]: get_estimation(x, t.ravel(), m, y.ravel(), 'simulation_based', 0)
R[write to console]: Error: package or namespace load failed for ‘mediation’ in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
 namespace ‘htmltools’ 0.5.6.1 is being loaded, but >= 0.5.7 is required

R[write to console]: Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
  namespace ‘htmltools’ 0.5.6.1 is being loaded, but >= 0.5.7 is required

/data/parietal/store/work/jabecass/med_bench/src/med_bench/utils/utils.py:58: UserWarning: The 'mediation' R package is not installed. Please install it using R by running:
import rpy2.robjects.packages as rpackages
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=33)
utils.install_packages('mediation')
  warnings.warn(
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 1
----> 1 get_estimation(x, t.ravel(), m, y.ravel(), 'simulation_based', 0)

File /data/parietal/store/work/jabecass/med_bench/src/med_bench/get_estimation.py:676, in get_estimation(x, t, m, y, estimator, config)
    674     if config not in (0, 1, 2):
    675         raise ValueError("Estimator only supports 1D binary mediator.")
--> 676     raise ValueError("Estimator does not support 1D binary mediator.")
    677 return effects

ValueError: Estimator does not support 1D binary mediator.

Not sure what the right fix is:

wdyt @bthirion @houssamzenati and @brash6 @zbakhm (in terms of architecture and user interface)?

bthirion commented 3 months ago

IIUC, you have no way to know in the Python code whether the R module is present or not ?

judithabk6 commented 3 months ago

yes, we can know it, and so far it is used in the new PR for a warning. I have 2 questions

bthirion commented 3 months ago

I think we should raise an error if the user wants to use a function that is not available --- before we reach the R layer.