How to use the rpy2.robjects.conversion.rpy2py function in rpy2

To help you get started, we’ve selected a few rpy2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rpy2 / rpy2 / tests / robjects / test_conversion_numpy.py View on Github external
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
github ICB-DCM / pyABC / test / test_bytesstorage.py View on Github external
# check value
    if isinstance(object_, int):
        assert object_ == rebuilt
    elif isinstance(object_, float):
        assert object_ == rebuilt
    elif isinstance(object_, str):
        assert object_ == rebuilt
    elif isinstance(object_, np.ndarray):
        assert (object_ == rebuilt).all()
    elif isinstance(object_, pd.DataFrame):
        assert (object_ == rebuilt).all().all()
    elif isinstance(object_, pd.Series):
        assert (object_.to_frame() == rebuilt).all().all()
    elif isinstance(object_, robjects.DataFrame):
        with localconverter(pandas2ri.converter):
            assert (robjects.conversion.rpy2py(object_) == rebuilt) \
                   .all().all()
    else:
        raise Exception("Could not compare")
github rpy2 / rpy2 / tests / robjects / test_pandas_conversions.py View on Github external
def test_factor2Category(self):
        factor = robjects.vectors.FactorVector(('a', 'b', 'a'))
        with localconverter(default_converter + rpyp.converter) as cv:
            rp_c = robjects.conversion.rpy2py(factor)
        assert isinstance(rp_c, pandas.Categorical)
github cs224 / pybnl / pybnl / bn.py View on Github external
'''
            function(object, data) {
                rout_ = predict(object, data, prob=TRUE, debug=FALSE)
                rout = list(rout_, attributes(rout_)$prob)
                rout
            }
            ''')

        r_df_in = pydf_to_factorrdf(X)
        r_out = rpredictwithprobfn(self.rfit, r_df_in)
        r_df_out    = r_out[0]
        r_proba_out = r_out[1]

        with rpy2.robjects.conversion.localconverter(ro.default_converter + rpy2.robjects.pandas2ri.converter):
            y = ro.conversion.rpy2py(r_df_out).astype(cdt)
            proba = pd.DataFrame(ro.conversion.rpy2py(r_proba_out).T, columns=list(r_proba_out.dimnames[0]), index=X.index)

        return proba
github cs224 / pybnl / pybnl / bn.py View on Github external
# print(dims)
        coords = {}
        dim_names = []
        if len(dims) == 1:
            dname = node
            dim_names += [dname]
            levels = list(dims[0])
            coords.update({dname: levels})
        else:
            for dname in dims.names:
                dim_names += [dname]
                levels = list(dims.rx(dname)[0])
                coords.update({dname: levels})

        with rpy2.robjects.conversion.localconverter(ro.default_converter + rpy2.robjects.pandas2ri.converter):
            values = ro.conversion.rpy2py(prob)

        ar = xr.DataArray(values, dims = dim_names, coords= coords)
        ds['cpt' + node] = ar

    lpd = convert_xarray_dataset_to_pandas_dtcpm_dict(ds)

    did_adapt_ds_p = False
    for df_name in lpd.keys():
        ldf = lpd[df_name]
        null_index_combinations = ldf[pd.isnull(ldf['p'])][ldf.columns[:-2]]

        if len(null_index_combinations) == 0:
            continue
        null_index_combinations = null_index_combinations.drop_duplicates()
        did_adapt_ds_p = True
        lar = ds[df_name]
github rpy2 / rpy2 / rpy2 / robjects / methods.py View on Github external
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)
github rpy2 / rpy2 / rpy2 / robjects / vectors.py View on Github external
def __truediv__(self, x):
        res = globalenv_ri.find('/')(self._parent, conversion.py2rpy(x))
        return conversion.rpy2py(res)
github rpy2 / rpy2 / rpy2 / robjects / vectors.py View on Github external
def _get_rownames(self):
        res = baseenv_ri["rownames"](self)
        return conversion.rpy2py(res)
github rpy2 / rpy2 / rpy2 / robjects / vectors.py View on Github external
def head(self, *args, **kwargs):
        """ Call the R generic 'head()'. """
        res = utils_ri['head'](self, *args, **kwargs)
        return conversion.rpy2py(res)
github rpy2 / rpy2 / rpy2 / robjects / vectors.py View on Github external
def transpose(self):
        """ transpose the matrix """
        res = self._transpose(self)
        return conversion.rpy2py(res)