开发者

numpy2ri conversion problem with rpy2 2.2.2

I am using rpy2-2.2.2 with the new free Enthought python distri开发者_StackOverflow社区bution that includes numpy 1.6.0 and python 2.7.2. I easy_installed rpy2 which resulted in v. 2.2.2 being installed and all tests were successful.

The problem I'm having is with code I wrote that worked fine with rpy2 2.1.8 and python 2.6. The issue is in converting from numpy to R for arrays.

Here is a snippet of the relevant code:

import rpy2                    
import rpy2.rinterface as rinterface
import rpy2.robjects as rob    
import rpy2.rlike.container as rlc
import numpy as np            
import rpy2.robjects.numpy2ri  
r = rob.r
...
    HGr     = rob.conversion.py2ri(HG_reg)
    RHSr    = rob.conversion.py2ri(RHS)
    #
    CalData = rlc.TaggedList([HGr,RHSr],tags=('hg','rhs'))
    CalData = rob.DataFrame(CalData)

    r('''library(pls)''')
    #rob.globalEnv["HGr"] = HGr
    #rob.globalEnv["RHSr"] = RHSr
    rob.globalenv["CalData"] = CalData


    # perform the PLS regression
    if wetlflag:
        HGresults = r.plsr(r("hg ~ rhs.1 + rhs.2 + rhs.3 + rhs.4"),data=CalData,validation="LOO")

I will gladly admit it's not the most elegant way to do things, but it worked before and now when I need to provide results all is broken (!). The error I get is the following:

   Traceback (most recent call last):
  File "Mercury_PLS_WL_DF.py", line 224, in <module>
    HGr     = rob.conversion.py2ri(HG_reg)
  File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/rpy2-2.2.2dev_20110726-py2.7-macosx-10.5-i386.egg/rpy2/robjects/__init__.py", line 134, in default_py2ri
    raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o))))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.

I found the discussion here and got the impression that numpy arrays are now automatically converted to R arrays, but commenting out the rob.conversion.py2ri(HG_reg) statements and using the numpy arrays directly also seems to fail. Am I missing something obvious? Why would this break between 2.1.8 and 2.2.2?


From http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html#from-numpy-to-rpy2:

Warning

In earlier versions of rpy2, the import was all that was needed to have the conversion. A side-effect when importing a module can lead to problems, and there is now an extra step to make the conversion active: call the function rpy2.robjects.activate().

So put rpy2.robjects.activate() after the import and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜