Exemple #1
0
def ri2numpy(o):
    if isinstance(o, ListSexpVector):
        if 'data.frame' in o.rclass:
            # R "factor" vectors will not convert well by default
            # (will become integers), so we build a temporary list o2
            # with the factors as strings.
            o2 = list()
            # An added complication is that the conversion defined
            # in this module will make __getitem__ at the robjects
            # level return numpy arrays
            for column in rinterface.ListSexpVector(o):
                if 'factor' in column.rclass:
                    levels = tuple(column.do_slot("levels"))
                    column = tuple(levels[x-1] for x in column)
                o2.append(column)
            names = o.do_slot('names')
            if names is NULL:
                res = numpy.rec.fromarrays(o2)
            else:
                res = numpy.rec.fromarrays(o2, names=tuple(names))
        else:
            # not a data.frame, yet is it still possible to convert it
            res = ro.default_ri2ro(o)
    elif (o.typeof in _vectortypes) and (o.typeof != VECSXP):
        res = numpy.asarray(o)
    else:
        res = ro.default_ri2ro(o)
    return res
Exemple #2
0
def ri2numpy(o):
    if isinstance(o, ListSexpVector):
        if 'data.frame' in o.rclass:
            # R "factor" vectors will not convert well by default
            # (will become integers), so we build a temporary list o2
            # with the factors as strings.
            o2 = list()
            # An added complication is that the conversion defined
            # in this module will make __getitem__ at the robjects
            # level return numpy arrays
            for column in rinterface.ListSexpVector(o):
                if 'factor' in column.rclass:
                    levels = tuple(column.do_slot("levels"))
                    column = tuple(levels[x - 1] for x in column)
                o2.append(column)
            names = o.do_slot('names')
            if names is NULL:
                res = numpy.rec.fromarrays(o2)
            else:
                res = numpy.rec.fromarrays(o2, names=tuple(names))
        else:
            # not a data.frame, yet is it still possible to convert it
            res = ro.default_ri2ro(o)
    elif (o.typeof in _vectortypes) and (o.typeof != VECSXP):
        res = numpy.asarray(o)
    else:
        res = ro.default_ri2ro(o)
    return res
Exemple #3
0
 def f(obj):
     pyobj = robjects.default_ri2ro(obj)
     inherits = rinterface.baseenv["inherits"]
     classname = rinterface.SexpVector(["density"], rinterface.STRSXP)
     if inherits(pyobj, classname)[0]:
         pyobj = Density(pyobj)
     return pyobj
Exemple #4
0
def ri2pandas(o):
    if isinstance(o, DataFrame):
        # use the numpy converter
        recarray = numpy2ri.ri2numpy(o)
        res = PandasDataFrame.from_records(recarray)
    else:
        res = ro.default_ri2ro(o)
    return res
 def testMapperR2Python_s4(self):
     robjects.r('setClass("A", representation(x="integer"))')
     classname = rinterface.StrSexpVector(["A", ])
     one = rinterface.IntSexpVector([1, ])
     sexp = rinterface.globalenv.get("new")(classname, 
                                            x=one)
     self.assertTrue(isinstance(robjects.default_ri2ro(sexp), 
                                robjects.RS4))
Exemple #6
0
def ri2pandas(o):
    if isinstance(o, DataFrame):
        # use the numpy converter
        recarray = numpy2ri.ri2numpy(o)
        res = PandasDataFrame.from_records(recarray)
    else:
        res = ro.default_ri2ro(o)
    return res
 def f(obj):
     pyobj = robjects.default_ri2ro(obj)
     inherits = rinterface.baseenv["inherits"]
     classname = rinterface.SexpVector([
         "density",
     ], rinterface.STRSXP)
     if inherits(pyobj, classname)[0]:
         pyobj = Density(pyobj)
     return pyobj
 def testMapperR2Python_s4(self):
     robjects.r('setClass("A", representation(x="integer"))')
     classname = rinterface.StrSexpVector([
         "A",
     ])
     one = rinterface.IntSexpVector([
         1,
     ])
     sexp = rinterface.globalenv.get("new")(classname, x=one)
     self.assertTrue(isinstance(robjects.default_ri2ro(sexp), robjects.RS4))
Exemple #9
0
 def testMapperR2Python_environment(self):
     sexp = rinterface.globalenv.get(".GlobalEnv")
     self.assertTrue(isinstance(robjects.default_ri2ro(sexp), robjects.Environment))
Exemple #10
0
 def testMapperR2Python_function(self):
     sexp = rinterface.globalenv.get("plot")
     ob = robjects.default_ri2ro(sexp)
     self.assertTrue(isinstance(ob, robjects.Function))
Exemple #11
0
 def testMapperR2Python_boolean(self):
     sexp = rinterface.globalenv.get("T")
     ob = robjects.default_ri2ro(sexp)
     self.assertTrue(isinstance(ob, robjects.Vector))
 def testMapperR2Python_environment(self):
     sexp = rinterface.globalenv.get(".GlobalEnv")
     self.assertTrue(
         isinstance(robjects.default_ri2ro(sexp), robjects.Environment))
 def testMapperR2Python_function(self):
     sexp = rinterface.globalenv.get("plot")
     ob = robjects.default_ri2ro(sexp)
     self.assertTrue(isinstance(ob, robjects.Function))
 def testMapperR2Python_boolean(self):
     sexp = rinterface.globalenv.get("T")
     ob = robjects.default_ri2ro(sexp)
     self.assertTrue(isinstance(ob, robjects.Vector))