This works for me, includes doctest code.
I would appreciate comments of any type as I am new to R and python.
The only problems are
(i) Warning messages:
1: closing unused connection 12 (c:\DOCUME~1\bdb112\LOCALS~1\Temp\RtmpbhGMMz\file6c6c10d9)

and

(ii) occasionally (about 1 in 10 or 1 in 20 times?) there is a 10 minute delay on a clustering operation that normally takes 10s of seconds!

Win 32: R 2.7.0
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
RPy 2.0.3
--

import pdb
from matplotlib.cbook import is_numlike
from numpy import array, shape

#from rpy2

def Robj_to_dictionary(robj, verbose=0, depth=0):
    """ takes an RVector and converts it to a python dictionary
    iterate over any elements at this level, making a dictionary
    Some R features such as labels of columns and rows are ignored.
    Also, conversion to integer is a little fudgey but should be safe
    You can check the accuracy by 
    mClustRObj.r_repr() and comparing (can see the array dims also)
    In the following example, several preparatory lines are supplied
    in the test body (end of this code).
    >>> mClustDic = Robj_to_dictionary(mClustRObj, verbose=verbose)
    >>> print mClustDic["classification"]
    [1 1 1 1 1 2 2 2 2]
    >>> print mClustDic["parameters"]["mean"]
    [[ 2.96117876  7.40416875]
     [ 0.05886086  0.40401195]]
    """    
    elts = len(robj)
    if verbose: print("%d elements found " % elts)
    # open a blank dictionary
    dic = {}
    for (i,ro) in enumerate(robj):
        name=robj.names[i]
        if verbose>2: print("depth %d, element %d/%d is %s, type=%d" % (depth, 
i, elts, name, ro.typeof))
        if name != "NULL": 
            typ = ro.typeof
            if typ==0: 
                value = "Null"
            elif typ==13:  # scalar
                value = ro[0]
            elif typ==14:  # array
                value = array(ro)
                # fudgey test - look for . in string rep
                if (ro.__str__().find('.') == -1 and is_numlike(value)):
                    value=value.astype(int)
                    # array(Robject-scalar) returns a an array([xxx]) - so fix 
it
                if (len(shape(value))==1) and len(value)==1: value=value[0]

            elif typ==16:  # string
                value = ro[0]
            elif typ==19:
                value = Robj_to_dictionary(ro,depth=depth+1)
            else:
                print("unknown type %d,  %s" % (typ, array(ro)))
                value="unk"
                x=0
                y=1/x
        dic.update({name: value})
        if verbose>10: pdb.set_trace()
    return(dic)


if __name__ == "__main__":
    import doctest

    import pdb
    from numpy import arange, cos, sin, array
    import rpy2.robjects as robjects
    r = robjects.r

    r.library('mclust')
    verbose=0 
    nn=10
    m = {'var1': robjects.FloatVector(arange(1,nn)),
         'var2': robjects.FloatVector(sin(arange(1,nn)))}
    datafr = robjects.r['data.frame'](**m)
    robjects.globalEnv["datafr"] = datafr
    if verbose>0: print(datafr.colnames())
    mClustRObj=r.Mclust(r.__getitem__("datafr"),G=2) #,modelNames='EEI')
    if verbose>0: 
        for i in arange(len(mClustRObj)): 
            print("%s %s" % (mClustRObj.names[i],mClustRObj[i][0]))
            if verbose>2: r.plot(r.__getitem__("datafr"))
            
    # note: the last two lines are in the doctest part.        
    doctest.testmod()
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to