Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weird error pattern (legacy from get_estimation) #77

Open
judithabk6 opened this issue Mar 19, 2024 · 3 comments
Open

weird error pattern (legacy from get_estimation) #77

judithabk6 opened this issue Mar 19, 2024 · 3 comments

Comments

@judithabk6
Copy link
Owner

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:

  • raise an error instead of a warning when R package is missing
  • refactor get_estimation (which is needed anyway, to get rid of a lengthy pattern + the legacy "config" parameter)

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

@bthirion
Copy link
Collaborator

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

@judithabk6
Copy link
Owner Author

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

  • do we want a warning and that the function returns None, or do we want to raise an error and interrupt the execution completely. I am not sure which is best
  • if we keep the warning, we have to adjust the behavior of the get_estimation function because this function raises an error when an estimator return None, but not the right error in that case.

@bthirion
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants