[英]NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>' only after I run the code twice
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:如果我在 Jupyter notebook 中运行此单元格两次,我会收到以下错误消息:
---------------------------------------------------------------------------
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?
可能是新版本的 python 或 rpy2 是问题所在? How can I fix the problem such that I am able to run this code multiple times within one Jupyter notebook.
我该如何解决这个问题,以便我能够在一个 Jupyter notebook 中多次运行此代码。
This bug occurs for the vollowing version: ipykernel
version 5.3.4,此错误发生在以下版本:
ipykernel
版本 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结合
rpy2
版本 3.5.5
import rpy2
print(rpy2.__version__)
3.5.5
This is caused by an issue in older releases of ipykernel
.这是由旧版本的
ipykernel
中的问题引起的。 I'd recommend to upgrade it rather than downgrade rpy2
.我建议升级它而不是降级
rpy2
。
See https://github.com/rpy2/rpy2/issues/952请参阅https://github.com/rpy2/rpy2/issues/952
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 ).在 Jupyter-notebook 开始时为了回滚到版本
3.4.2
,这个问题没有发生(参见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 )有关如何更改 python 包版本的更多信息,请参阅在 Goolge Colab 中回滚到特定版本的 python 包和使用 pip 安装特定包版本)
It would still be interesting to understand how one could use the latest version of rpy2
correctly.了解如何正确使用最新版本的
rpy2
仍然很有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.