"Error while parsing the string" when using RPy2 and dplyr - python

I'm using R to do some data manipulation. This is a part of a larger project that is mostly in Python, so I'm not using R directly but RPy2 instead. Things work fine until I get to the dplyr part.
This works:
from rpy2.robjects import r
Rcode = '''library(RODBC)
library(dplyr)
# ...
# a bunch of R code that fetches data from SQL Server
# ...'''.format(db_name = 'foo')
print r(Rcode)
That gives me the data I want.
But when I try to do some data manipulation with dplyr, like this:
from rpy2.robjects import r
Rcode = '''library(RODBC)
library(dplyr)
# ...
# a bunch of R code that fetches data from SQL Server
# ...
myData <- myData
%>% group_by(someDataField)'''.format(db_name = 'foo')
print r(Rcode)
I get an error:
Traceback (most recent call last):
File "myScript.py", line 53, in <module>
print r(Rcode)
File "C:\Python27\lib\site-packages\rpy2\robjects\__init__.py", line 268, in __call__
p = rinterface.parse(string)
ValueError: Error while parsing the string.
I've tried escaping the % signs, escaping the > sign, and putting them within quotation marks, but nothing worked.
What am I missing?
I've seen the other questions about the same error (like here and here) but they didn't help.
I'm using Python 2.7.10 and RPy2 2.5.6.

myData <- group_by(MyData,someDataField)

Related

I'm having troubles with Uber Python API

I'm coding in python, using version 2.6, working with the Uber API, and when I try to import the library uber_rides.auth it throws this error:
Traceback (most recent call last):
File "C:\Inetpub\vhosts\underdevelopment.biz\httpdocs\web\webtemp3\uber\socket.py", line 4, in <module>
from uber_rides.auth import AuthorizationCodeGrant
File "C:\Inetpub\vhosts\underdevelopment.biz\httpdocs\web\webtemp3\uber\uber_rides\auth.py", line 133
query_params = [qp: query_params[qp][0] for qp in query_params]
^
SyntaxError: invalid syntax
The original code of my script is this:
print('Content-Type: text/plain')
print('')
from uber_rides.auth import AuthorizationCodeGrant
def main():
auth_flow = AuthorizationCodeGrant(
'xxxxxx-xxxxxxx',
'xxxxx-xxxxx',
'xxx-xxxxxxxxx',
'',
)
auth_url = auth_flow.get_authorization_url()
if __name__ == "__main__":
main()
It seems the error is from the library but I can't find it yet.
Yes, that's invalid Python syntax. However, it is not clear how you ended up with that file.
Someone or something altered that file. That's not the original source code as distributed by Uber, where that line uses the correct syntax for a dictionary comprehension:
query_params = {qp: query_params[qp][0] for qp in query_params}
Re-install the project, the error is not there upstream.
Note that the above syntax is only available in Python 2.7 and up. You could try to replace it with a dict() call with a generator expression, see Alternative to dict comprehension prior to Python 2.7:
query_params = dict((qp, query_params[qp][0]) for qp in query_params)
Take into account that there may be other issues with the code, upgrading to Python 2.7 is probably the better option.

Using the R select function in Python using rpy2

I'm trying to covert UniProt accession numbers to Entrez IDs using the BioconductoR package org.Hs.eg.db (which is an S4 object). I'm also trying to do this as part of a Python script with rpy2. Calling the select function gives me errors. Here's the code (the program is 400 lines, I'm excerpting the relevant stuff):
from rpy2.robjects.packages import importr
from rpy2.robjects import StrVector, DataFrame, r
# get UniProt accension numbers from first two columns of data
uniprotA = []
uniprotB = []
for row in interactions:
uniprotA.append(row[0][10:])
uniprotB.append(row[1][10:])
# convert to vectors in r
uniprotA = StrVector(uniprotA)
uniprotB = StrVector(uniprotB)
homosap = importr('org.Hs.eg.db')
geneidA = r.select(homosap, keys = uniprotA, columns = "ENTREZID", keytype="UNIPROT")
And here are the error messages:
Traceback (most recent call last):
File "mitab_preprocess.py", line 356, in <module>
reformat_data(interactions)
File "mitab_preprocess.py", line 140, in reformat_data
geneidA = r.select(homosap, keys = uniprotA, columns = "ENTREZID", keytype="UNIPROT")
File "//anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "//anaconda/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 102, in __call__
new_args = [conversion.py2ri(a) for a in args]
File "//anaconda/lib/python2.7/site-packages/singledispatch.py", line 210, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "//anaconda/lib/python2.7/site-packages/rpy2/robjects/conversion.py", line 60, in _py2ri
raise NotImplementedError("Conversion 'py2ri' not defined for objects of type '%s'" % str(type(obj)))
NotImplementedError: Conversion 'py2ri' not defined for objects of type '<class 'rpy2.robjects.packages.InstalledSTPackage'>'
homosap is an R package exposed as a Python namespace.
I think that you want to use an object in that namespace as a parameter, not the namespace.
Here it should be homosap.org_Hs_eg_db (I am guessing, I have not tried).
There are many things at play here:
. is not a syntactically valid symbol for Python variable names so it is translated to _ by rpy2
when importing an R package all its symbols are added to the search path. If coming from Python, this is a bit like from <package> import *. rpy2's importr is returning a namespace in which the symbols of the package are exposed as attributes.

Python requests 0.x code port to 2.x

I had a really simple piece of code working with Python requests 0.x but when I updated to 2.x it no longer works.
The code would return me the color contained within 'field1':
import time
import requests
# Read the thingspeak feed to get the current colour
while True:
cheerlights = requests.get('http://api.thingspeak.com/channels/1417/field/1/last.json').json['field1']
print(cheerlights)
time.sleep(16)
When I run this not I get this error:
Traceback (most recent call last):
File "cheelightsJsonHELP.py", line 7, in
cheerlights = requests.get('http://api.thingspeak.com/channels/1417/field/1/last.json').json['field1']
TypeError: 'instancemethod' object has no attribute 'getitem'
I have read the documentation on migrating from 0.x to 2.x but unfortunately this is not a strong area of mine, can anybody help?
response.json() is now a method, where in the past it was a property; add () to call it:
response = requests.get('http://api.thingspeak.com/channels/1417/field/1/last.json')
cheerlights = response.json()['field1']
Demo:
>>> import requests
>>> response = requests.get('http://api.thingspeak.com/channels/1417/field/1/last.json')
>>> response.json()['field1']
'orange'

lmtest/lrtest throws data.frame error when data passed through rpy2

I have an error that occurs only when I call lrtest (from the lmtest package) from a user-defined function via rpy2.
R:
continuous.test <- function(dat) {
require('lmtest')
options(warn=-1)
model <- lm(formula='pheno ~ .', data=dat)
anova <- lrtest(model,'interaction')
pval <- anova$"Pr(>Chisq)"[2]
}
When I call this function from the R interpreter, everything runs correctly. However, I receive an error when calling from the following snippet of python code. Note, this particular python file makes many other calls to rpy2 with success.
Python:
...
kway_dat = R.DataFrame(dataframe) # this is a valid dataframe, it's used in other calls.
...
R.r("source('/path/to/user/defined/file/perm_test.r')")
continuous_test = R.r['continuous.test']
pval = continuous_test(kway_dat)
Error:
Error in is.data.frame(data) : object 'dat' not found
Traceback (most recent call last):
File "./test_r_.py", line 83, in <module>
pval = continuous_test(kway_dat)
File "/usr/lib/python2.6/site-packages/rpy2-2.2.6dev_20120806-py2.6-linux-x86_64.egg/rpy2/robjects/functions.py", line 82, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/rpy2-2.2.6dev_20120806-py2.6-linux-x86_64.egg/rpy2/robjects/functions.py", line 34, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in is.data.frame(data) : object 'dat' not found
Troubleshooting:
I have tested the code in R and everything works fine.
I have passed a dataframe from python to R through rpy2 and called is.data.frame(dat) from an R function, and it returns true, so the issue is with lmtest or lrtest + rpy2.
Any help would be great. Thanks all!
It would be easier to help with a self-contained example (so one can reproduce exactly what you are experiencing).
A possible answer still: you might want to check that the content of the file
/path/to/user/defined/file/perm_test.r is really what you think it is.
I am also adding a stub for a self-contained example:
r_code = """
require('lmtest')
options(warn=-1)
continuous.test <- function(dat) {
model <- lm(formula='pheno ~ .', data=dat)
anova <- lmtest::lrtest(model,'interaction')
pval <- anova$"Pr(>Chisq)"[2]
}
"""
from rpy2.robjects import packages
my_r_pack = packages.SignatureTranslatedAnonymousPackage(r_code, "my_r_pack")
# [build a demo kway_dat here]
my_r_pack.continuous_test(kway_dat)
Answer Found
The issue was lrtest's internal call to update the model. Once inside lrtest, dat was out of scope. By updating the model manually and using lrtest's alternative call lrtest(model0,model1), the issue is entirely avoided.
Thanks to Achim Zeileis who replied incredibly promptly.

Error when using astWCS trying to create WCS object

I'm running python2.5 and trying to use the astLib library to analyse WCS information in astronomical images. I try and get the object instanciated with the following skeleton code:
from astLib import astWCS
w = astWCS.WCS('file.fits') # error here
where file.fits is a string pointing to a valid fits file.
I have tried using the alternate method of passing a pyfits header object and this fails also:
import pyfits
from astLib import astWCS
f = pyfits.open('file.fits')
header = f[0].header
f.close()
w = astWCS.WCS(header, mode='pyfits') # error here also
The error is this:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/astLib/astWCS.py", line 79, in __init__
self.updateFromHeader()
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/astLib/astWCS.py", line 119, in updateFromHeader
self.WCSStructure=wcs.wcsinit(cardstring)
File "/home/astro/phrfbf/build/lib/python2.6/site-packages/PyWCSTools/wcs.py", line 70, in wcsinit
return _wcs.wcsinit(*args)
TypeError: in method 'wcsinit', argument 1 of type 'char *'
When I run in ipython, I get the full error here on the pastebin
I know the astWCS module is a wrapped version of WCStools but i'd prefer to use the Python module as the rest of my code is in Python
Can anyone help with this problem?
Just found out the updated version of this library has fixed the problem, thanks for everyone's help
Oh sorry, I should have seen. Looking at the pastebin in more detail, the only error I can think of is that, for some reason the header has unicode in it. It can't be converted to char *, and you get the error. I tried searching for something in the header, but everything looks okay. Can you do this and post the output in another pastebin?
import pyfits
f = pyfits.open('file.fits')
header = f[0].header
f.close()
for x, i in enumerate(header.iteritems()):
if len(str(i[1])) >= 70:
print x, str(i[1])
cardlist = header.ascardlist()
cardstring = ""
for card in cardlist:
cardstring = cardstring + str(card)
print repr(cardstring)
Or, if you can check the header of your fits file for "funny" characters, getting rid of them should solve the issue.

Categories

Resources