예제 #1
0
 def formals(self):
     """ Return the signature of the underlying R function
     (as the R function 'formals()' would).
     """
     res = _formals_fixed(self)
     res = conversion.rpy2py(res)
     return res
예제 #2
0
 def test_dataframe_to_numpy(self):
     df = robjects.vectors.DataFrame(
         {'a': 1,
          'b': 2,
          'c': robjects.vectors.FactorVector('e')})
     rec = conversion.rpy2py(df)
     assert numpy.recarray == type(rec)
     assert rec.a[0] == 1
     assert rec.b[0] == 2
     assert rec.c[0] == 'e'
예제 #3
0
 def __call__(self, *args, **kwargs):
     new_args = [conversion.py2rpy(a) for a in args]
     new_kwargs = {}
     for k, v in kwargs.items():
         # TODO: shouldn't this be handled by the conversion itself ?
         if isinstance(v, rinterface.Sexp):
             new_kwargs[k] = v
         else:
             new_kwargs[k] = conversion.py2rpy(v)
     res = super(Function, self).__call__(*new_args, **new_kwargs)
     res = conversion.rpy2py(res)
     return res
예제 #4
0
def eval(x, envir=ri.globalenv):
    """ Evaluate R code. If the input object is an R expression it
    evaluates it directly, if it is a string it parses it before
    evaluating it.

    By default the evaluation is performed in R's global environment
    but a specific environment can be specified."""
    if isinstance(x, str):
        p = _parse(x)
    else:
        p = x
    res = _reval(p, envir=envir)
    res = conversion.rpy2py(res)
    return res
예제 #5
0
    def get_deseq_result(self, contrast=None, **kwargs):

        self.comparison = deseq.resultsNames(self.dds)
        if contrast:
            if len(contrast) == 3:
                contrast = robjects.numpy2ri.numpy2ri(np.array(contrast))
            else:
                assert len(contrast) == 2, 'Contrast must be length of 3 or 2'
                contrast = robjects.ListVector({None: con for con in contrast})
            print('Using contrast: ', contrast)
            self.deseq_result = deseq.results(self.dds,
                                              contrast=contrast,
                                              **kwargs)
        else:
            self.deseq_result = deseq.results(self.dds, **kwargs)
        self.deseq_result = to_dataframe(self.deseq_result)
        self.deseq_result = conversion.rpy2py(self.deseq_result)

        return (self.deseq_result)
예제 #6
0
def set_accessors(cls, cls_name, where, acs):
    # set accessors (to be abandonned for the metaclass above ?)

    if where is None:
        where = rinterface.globalenv
    else:
        where = "package:" + str(where)
        where = StrSexpVector((where, ))

    for r_name, python_name, as_property, docstring in acs:
        if python_name is None:
            python_name = r_name
        r_meth = getmethod(StrSexpVector((r_name, )), 
                           signature = StrSexpVector((cls_name, )),
                           where = where)
        r_meth = conversion.rpy2py(r_meth)
        if as_property:
            setattr(cls, python_name, property(r_meth, None, None))
        else:
            setattr(cls, python_name, lambda self: r_meth(self))
예제 #7
0
def eval(x: str, envir: ri.SexpEnvironment = ri.globalenv) -> ri.Sexp:
    """ Evaluate R code. If the input object is an R expression it
    evaluates it directly, if it is a string it parses it before
    evaluating it.

    By default the evaluation is performed in R's global environment
    but a specific environment can be specified.

    Args:
        x (str): a string to be parsed and evaluated as R code
        envir (rpy2.rinterface.SexpEnvironment): An R environment in
          which to evaluate the R code.
    Returns:
        The R objects resulting from the evaluation."""
    if isinstance(x, str):
        p = _parse(x)
    else:
        p = x
    res = _reval(p, envir=envir)
    res = conversion.rpy2py(res)
    return res
예제 #8
0
파일: methods.py 프로젝트: rpy2/rpy2
    def __new__(mcs, name, bases, cls_dict):

        try:
            cls_rname = cls_dict['__rname__']
        except KeyError:
            cls_rname = name

        try:
            accessors = cls_dict['__accessors__']
        except KeyError:
            accessors = []

        for rname, where, \
                python_name, as_property, \
                docstring in accessors:

            if where is None:
                where = rinterface.globalenv
            else:
                where = StrSexpVector(('package:%s' % where, ))

            if python_name is None:
                python_name = rname

            signature = StrSexpVector((cls_rname, ))
            r_meth = getmethod(StrSexpVector((rname, )),
                               signature=signature,
                               where=where)
            r_meth = conversion.rpy2py(r_meth)
            if as_property:
                cls_dict[python_name] = property(r_meth,
                                                 None,
                                                 None,
                                                 doc=docstring)
            else:
                cls_dict[python_name] = lambda self: r_meth(self)

        return type.__new__(mcs, name, bases, cls_dict)
예제 #9
0
def test_convert_empty_df_with_rows(r2py):
    df = r("S4Vectors::DataFrame(a=1:10)[, -1]")
    assert df.slots["nrows"][0] == 10

    df_py = r2py(anndata2ri, lambda: conversion.rpy2py(df))
    assert isinstance(df_py, pd.DataFrame)
예제 #10
0
 def do_slot(self, name):
     return conversion.rpy2py(super(RS4, self).do_slot(name))
예제 #11
0
 def __getitem__(self, key):
     value = self._robj.do_slot(key)
     return conversion.rpy2py(value)
예제 #12
0
 def test_dataframe_to_numpy(self):
     df = robjects.vectors.DataFrame(dict((('a', 1), ('b', 2))))
     rec = conversion.rpy2py(df)
     assert numpy.recarray == type(rec)
     assert rec.a[0] == 1
     assert rec.b[0] == 2