source: orange/Orange/misc/r.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 2.7 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

Line 
1"""
2=======================
3R compatibility (``r``)
4=======================
5
6.. index:: R
7
8Conversion of Orange's structure into R objects (with rpy2 package).
9
10.. autofunction:: dataframe
11
12.. autofunction:: variable_to_vector
13
14.. autofunction:: matrix
15"""
16
17import Orange
18
19import rpy2.robjects as robjects
20import rpy2.rlike.container as rlc
21
22if hasattr(robjects, "DataFrame"): # rpy2 version 2.1
23    DataFrame = robjects.DataFrame
24else: # rpy2 version 2.0
25    DataFrame = robjects.RDataFrame
26   
27if hasattr(robjects, "Matrix"): # rpy2 version 2.1
28    Matrix = robjects.Matrix
29else: # rpy2 version 2.0
30    Matrix = robjects.RMatrix
31   
32if hasattr(robjects, "globalenv"): # rpy2 version 2.1
33    globalenv = robjects.globalenv
34else: # rpy2 version 2.0
35    globalenv = robjects.globalEnv
36   
37NA_Real = robjects.NA_Real if hasattr(robjects, "NA_Real") \
38    else robjects.r("NA") #rpy 2.0
39NA_Int = robjects.NA_Integer if hasattr(robjects, "NA_Integer") \
40    else robjects.r("NA") #rpy 2.0
41NA_Char = robjects.NA_Integer if hasattr(robjects, "NA_Character") \
42    else robjects.r("NA") #rpy 2.0
43
44def variable_to_vector(data, attr):
45    """
46    Convert a Variable to R's vector. Replace special (unkown values)
47    with R's NA.
48    """
49    def cv(value, fn, na):
50        return na if value.isSpecial() else fn(value)
51   
52    if attr.var_type == Orange.data.variable.Variable.Continuous:
53        return robjects.FloatVector([cv(ex[attr], float, NA_Real) for ex in data])
54    elif attr.var_type == Orange.data.variable.Variable.Discrete:
55        return robjects.r("factor")(robjects.StrVector([cv(ex[attr], str, NA_Char) for ex in data]))
56    elif attr.var_type == Orange.data.variable.Variable.String:
57        return robjects.StrVector([cv(ex[attr], str, NA_Char) for ex in data])
58    else:
59        return None
60
61def dataframe(data, variables=None):
62    """
63    Convert an Orange.data.Table to R's DataFrame.
64    Converts only the input variables if given.
65    """
66    if not variables:
67        variables = [ attr for attr in data.domain.variables if attr.var_type in \
68                 [ Orange.data.variable.Variable.Continuous, 
69                   Orange.data.variable.Variable.Discrete, 
70                   Orange.data.variable.Variable.String ] ]
71           
72    odata = []
73    for attr in variables:
74        odata.append((attr.name, variable_to_vector(data, attr)))
75
76    r_obj = DataFrame(rlc.TaggedList([v for _,v in odata], [t for t,_ in odata]))
77    return r_obj
78
79def matrix(matrix):
80    """
81    Convert an SymMatrix to R's Matrix.
82    """
83    v = robjects.FloatVector([e for row in matrix for e in row])
84
85    r_obj = robjects.r['matrix'](v, nrow=matrix.dim)
86    return r_obj
87
88if __name__ == "__main__":
89    data = Orange.data.Table("titanic")
90    df = dataframe(data)
91    print df
92
Note: See TracBrowser for help on using the repository browser.