2016-11-24 44 views
0

我正嘗試在使用rpy2軟件包的python中創建一個r環境。我試圖創建一個我從圖像中提取的ROI的GLRLM。我在加載軟件包時遇到了一些問題,但我終於設法做到了。現在我在運行代碼時出現以下錯誤:在Python中使用rpy2軟件包:Rruntime錯誤未被用戶代碼處理

RuntimeError was unhandled by user code

在這一行:

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

我懷疑它無法找到函數「glrlm」。我試着運行R中的代碼,它工作正常。任何人都知道該怎麼做?

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) 

回答

0

在做radiomics:::glrlm您正在訪問的符號glrlm在命名空間radiomics而無法導出它(使用::只訪問導出的符號)。這應該在R中工作,並用ro.r解析/評估R字符串。但是,符號通常不會被R包導出,因爲它們並不意味着被普通用戶直接訪問。

另外消息RuntimeError was unhandled by user code似乎不是來自rpy2。這真的是所有報告有關錯誤?

在這旁邊,你可能會考慮遷移一些將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) 
+0

是。不幸的是,這是我返回的錯誤。 我發現我的錯誤。 這是我的rgbRoi [...,0]的轉換。當使用它縮放像素級時,它的工作原理如下: 'grayLvl = 8 Img = rgbRoi [...,0]' 'm =(grayLvl-1)/Img.max()'' 'scaledImg = Img * m ' 'P = np.array(np.round(scaledImg))' 'tissue = pd.DataFrame(P)' 我也對glrlm函數做了如下修改:'ro.r'''glrlmatrix < - radiomics: :glrlm(tissue,angle = 0,n_grey = 4)') – Nkma89

+0

我添加了一個註釋,它可以幫助將調用R函數的調用集成到Python中的代碼庫中。 – lgautier

相關問題