Skip to content

Commit

Permalink
Added setup.py and README descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dimenwarper committed Aug 4, 2016
1 parent 2be77bc commit 8b0f1a0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
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)
```
36 changes: 36 additions & 0 deletions __init__.py
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()
13 changes: 13 additions & 0 deletions corpcor.py
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)
9 changes: 9 additions & 0 deletions glasso.py
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]
11 changes: 11 additions & 0 deletions setup.py
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']
)

0 comments on commit 8b0f1a0

Please sign in to comment.