-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added setup.py and README descriptions
- Loading branch information
1 parent
2be77bc
commit 8b0f1a0
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
# pyroconductor | ||
# pyroconductor | ||
|
||
A high-level interface to access commonly-used R tools from python using the low-level r2py. Goal is to 'iron out' r2py conversion interfaces (e.g. like those provided by `pandas2ri`) and even manually create pythonic interfaces for R functions and objects that are hard to automatically translate. | ||
|
||
Install by simply doing: | ||
|
||
```bash | ||
python setup.py install | ||
``` | ||
|
||
To use `your.favorite.r.package` in a python script, simply call: | ||
|
||
```python | ||
import pyroconductor as pyr | ||
|
||
pyr.your_favorite_r_package.function_in_package(args) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
from rpy2 import robjects as ro | ||
from rpy2.robjects import pandas2ri | ||
from rpy2.robjects.functions import Function, SignatureTranslatedFunction | ||
from rpy2.robjects.vectors import DataFrame, ListVector, Vector | ||
|
||
pandas2ri.activate() | ||
|
||
def _convert_to_python(x): | ||
if isinstance(x, DataFrame): | ||
return pandas2ri.ri2py_dataframe(x) | ||
elif isinstance(x, ListVector) or isinstance(x, Vector): | ||
return [_convert_to_python(item) for item in x] | ||
else: | ||
return np.array(x) | ||
|
||
class R(object): | ||
def __getattribute__(self, attr): | ||
try: | ||
return super(R, self).__getattribute__(attr) | ||
except AttributeError as ae: | ||
orig_ae = str(ae) | ||
|
||
try: | ||
return self.__getitem__(attr) | ||
except LookupError: | ||
raise AttributeError(orig_ae) | ||
|
||
def __getitem__(self, item): | ||
res = ro.r.__getitem__(item) | ||
if isinstance(res, Function) or\ | ||
isinstance(SignatureTranslatedFunction): | ||
return lambda *args, **kwargs: _convert_to_python(res(*args, **kwargs)) | ||
else: | ||
return res | ||
r = R() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from rpy2 import robjects | ||
from rpy2.robjects import pandas2ri | ||
import numpy as np | ||
pandas2ri.activate() | ||
|
||
def cov_shrink(data_array, weights=None, **kwargs): | ||
robjects.r('library(corpcor)') | ||
if weights is None: | ||
res = robjects.r['cov.shrink'](data_array, **kwargs) | ||
else: | ||
res = robjects.r['cov.shrink'](data_array, w=robjects.FloatVector(weights), | ||
**kwargs) | ||
return np.array(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rpy2 import robjects | ||
from rpy2.robjects import pandas2ri | ||
import numpy as np | ||
pandas2ri.activate() | ||
|
||
def glasso(s, rho): | ||
robjects.r('library(glasso)') | ||
cov, prec, loglik, errflg, approx, dl, niter = robjects.r['glasso'](s, rho) | ||
return np.array(cov), np.array(prec), loglik[0], errflg[0], approx[0], dl[0], niter[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from setuptools import setup | ||
|
||
setup(name='pyroconductor', | ||
version='0.1', | ||
description='High level interface to access R from python leveraging r2py.', | ||
author='Pablo Cordero', | ||
author_email='[email protected]', | ||
url='https://github.com/dimenwarper/pyroconductor', | ||
packages=['corpcor', 'glasso'], | ||
install_requires=['numpy', 'scipy', 'rpy2', 'pandas', 'matplotlib'] | ||
) |