【问题标题】:NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>' only after I run the code twiceNotImplementedError: Conversion \'rpy2py\' not defined for objects of type \'<class \'rpy2.rinterface.SexpClosure\'>\' only after I run the code twice
【发布时间】:2022-12-19 10:26:06
【问题描述】:

If I run the following code once it works.

import numpy as np
import rpy2.robjects as robjects
x = np.linspace(0, 1, num = 11, endpoint=True)    
y = np.array([-1,1,1, -1,1,0, .5,.5,.4,  .5, -1])

r_x = robjects.FloatVector(x)
r_y = robjects.FloatVector(y)
r_smooth_spline = robjects.r['smooth.spline'] #extract R function
spline_xy = r_smooth_spline(x=r_x, y=r_y)
print('x =', x)
print('ysplined =',np.array(robjects.r['predict'](spline_xy,robjects.FloatVector(x)).rx2('y')))

If I run this cell twice in a Jupyter notebook, I obtain the following error message:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-5efeb940cd16> in <module>
      6 r_x = robjects.FloatVector(x)
      7 r_y = robjects.FloatVector(y)
----> 8 r_smooth_spline = robjects.r['smooth.spline'] #extract R function
      9 spline_xy = r_smooth_spline(x=r_x, y=r_y)
     10 print('x =', x)

2 frames
/usr/local/lib/python3.8/dist-packages/rpy2/robjects/conversion.py in _rpy2py(obj)
    250     non-rpy2) objects.
    251     """
--> 252     raise NotImplementedError(
    253         "Conversion 'rpy2py' not defined for objects of type '%s'" %
    254         str(type(obj))

NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'

This code always used to run without problems multiple times. Probably a new version of python or rpy2 is the problem? How can I fix the problem such that I am able to run this code multiple times within one Jupyter notebook.

This bug occurs for the vollowing version: ipykernel version 5.3.4,

!jupyter --version

Selected Jupyter core packages...
IPython          : 7.9.0
ipykernel        : 5.3.4
ipywidgets       : 7.7.1
jupyter_client   : 6.1.12
jupyter_core     : 4.11.2
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : not installed
nbconvert        : 5.6.1
nbformat         : 5.7.0
notebook         : 5.7.16
qtconsole        : not installed
traitlets        : 5.1.1

in combination with rpy2 version 3.5.5

import rpy2
print(rpy2.__version__)

3.5.5

【问题讨论】:

    标签: python jupyter-notebook rpy2


    【解决方案1】:

    This is caused by an issue in older releases of ipykernel. I'd recommend to upgrade it rather than downgrade rpy2.

    See https://github.com/rpy2/rpy2/issues/952

    【讨论】:

    • Do you know if I can upgrade the ipykernel in Google Colab?
    • Possibly not (or not in way that would matter). Updating is an open issue with Colab: github.com/googlecolab/colabtools/issues/3212
    • Which version of ipykernel fixed the problem for you?
    • pip install -U ipykernel solves things on this end. So at least the latest version does. With Colab you'll depend on Colab updating it.
    • I have version 6.16.2 installed.
    【解决方案2】:

    The easiest fix is to run once:

    !pip install -Iv rpy2==3.4.2
    

    at the start of the Jupyter-notebook in order to rollback to version 3.4.2, where this problem did not occur (see Rpy2 Error depends on execution method: NotImplementedError: Conversion "rpy2py" not defined). For more information how to change the version of a python package see Rollback to specific version of a python package in Goolge Colab and Installing specific package version with pip)

    It would still be interesting to understand how one could use the latest version of rpy2 correctly.

    【讨论】:

      猜你喜欢
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 2022-12-02
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      相关资源
      最近更新 更多