简体   繁体   English

在Python中使用rpy2包:用户代码未处理Rruntime错误

[英]Using rpy2 package in Python: Rruntime Error was unhandled by user code

I'm trying to create an r environment in python using the rpy2 package. 我正在尝试使用rpy2包在python中创建一个r环境。 I'm trying to create a GLRLM of a ROI I have extracted from an image. 我正在尝试创建从图像中提取的ROI的GLRLM。 I had some problems getting the package to load, but I finally managed to do so. 我在加载程序包时遇到了一些问题,但最终设法做到了。 Now I get the following error when running the code: 现在,在运行代码时出现以下错误:

RuntimeError was unhandled by user code

At this line: 在这一行:

ro.r('glrlmatrix <- radiomics:::glrlm(tissue)')

I'm suspecting it cannot find the function "glrlm". 我怀疑它找不到函数“ glrlm”。 I tried running the code in R and it works fine. 我试过在R中运行代码,并且效果很好。 Anyone has an idea of what to do? 任何人都知道该怎么做?

tissue = pd.DataFrame(rgbRoi[...,0])
rdf= pandas2ri.py2ri(tissue)
ro.globalenv['tissue'] = rdf
radiomics=importr("radiomics",lib_loc="C:/Users/nka/Documents/R/win-library/3.3");
ro.r('tissue <- as.matrix(tissue)')
ro.r('print(dim(tissue))')
ro.r('library(radiomics)')
ro.r('glrlmatrix <- radiomics:::glrlm(tissue)')
ro.r('glrlmatrix[0,]    <- 0')                           ### Assign zero value to first row which belongs to mask region
ro.r('glrlfeature       <- array(NA,dim=c(11,1))')
ro.r('glrlfeature[1,1]  <- radiomics:::glrlm_GLN(glrlmatrix)')
ro.r('glrlfeature[2,1]  <- radiomics:::glrlm_HGLRE(glrlmatrix)')
ro.r('glrlfeature[3,1]  <- radiomics:::glrlm_LRE(glrlmatrix)')
ro.r('glrlfeature[4,1]  <- radiomics:::glrlm_LRHGLE(glrlmatrix)')
ro.r('glrlfeature[5,1]  <- radiomics:::glrlm_LRLGLE(glrlmatrix)')
ro.r('glrlfeature[6,1]  <- radiomics:::glrlm_LGLRE(glrlmatrix)')
ro.r('glrlfeature[7,1]  <- radiomics:::glrlm_RLN(glrlmatrix)')
ro.r('glrlfeature[8,1]  <- radiomics:::glrlm_RP(glrlmatrix)')
ro.r('glrlfeature[9,1]  <- radiomics:::glrlm_SRE(glrlmatrix)')
ro.r('glrlfeature[10,1] <- radiomics:::glrlm_SRHGLE(glrlmatrix)')
ro.r('glrlfeature[11,1] <- radiomics:::glrlm_SRLGLE(glrlmatrix)')
glr = ro.r.matrix(ro.r('glrlfeature'))
glr = np.array(glr)

When doing radiomics:::glrlm you are accessing the symbol glrlm in the namespace radiomics while it is not exported (use :: to only access exported symbols). 在进行radiomics:::glrlm您正在访问名称空间radiomics的符号glrlm ,而未导出该符号(使用::仅访问导出的符号)。 This should be working in R and when parsing/evaluating an R string with ro.r . 这应该在R中以及在使用ro.r解析/评估R字符串时ro.r However, symbols are generally not exported by an R package because they not meant to be accessed directly by regular users. 但是,符号通常不由R包导出,因为它们并不意味着普通用户可以直接访问它们。

Also The message RuntimeError was unhandled by user code does not seem to originate from rpy2. 另外RuntimeError was unhandled by user code消息似乎不是源自rpy2。 Is this really all there is to report about the error ? 这真的是所有有关错误的报告吗?

Beside this, you may considering migrating some of the R strings to Python (if you think that this is helping with legibility): 除此之外,您可以考虑将一些R字符串迁移到Python(如果您认为这样做有助于提高可读性):

# this is implicitly loading the R package
radiomics=importr("radiomics",
                  lib_loc="C:/Users/nka/Documents/R/win-library/3.3");
# the line below is then unnecessary because of the above
# ro.r('library(radiomics)')

# binding the symbol "tissue" in R's globalenv to the matrix conversion
# of the data frame of the same name can be written more like Python
# code:
base = importr("base")
tissue_mat = base.as_matrix(tissue)
ro.globalenv['tissue'] = tissue_mat
# the following should work, unless the R code in `radiomics::glrlm`
# cannot handle anonymous objects
glrlmatrix = radiomics.glrlm(tissue_mat)
# glrmatrix is a Python/rpy2 objects that also be used as a numpy array
# and modified in-place
# (see http://rpy2.readthedocs.io/en/version_2.8.x/numpy.html#from-rpy2-to-numpy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM