Quantcast

use R from python

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

use R from python

Massimo Di Stefano
Hi All,

please aplogize me if my qustion is a bit OT here,
but maybe is there someone that uses R from inside python
using rpy or rpy2 interface.

i'm at my first experience using rpy2.

i need to port torpy some R code .. but i'm having little problems
i hope yopu can help me.

The R code is :

################################################
grass65
R
library(spgrass6)
r=readRAST6("elevation.10m")
r_sorted=sort(r@data$elevation.10m,decreasing=T)
p_seq=seq(1,length(r_sorted)) #area
x=p_seq/length(r_sorted)
x=(x-min(x))/(max(x)-min(x))
y=(r_sorted-min(r_sorted))/(max(r_sorted)-min(r_sorted))
result=data.frame(x,y)
png("graphic.png") # oppure ("graphic.pdf")
plot(result,type="l",x)
plot(result,type="l", xlab="area", ylab="elevation")
dev.off()
################################################


i tried to write it in python-rpy2 :


################################################

# import library (rpy2 + spgrass6)

import rpy2.rinterface as rinterface
import rpy2.robjects as robjects
robjects.r.require('spgrass6', quietly = True)[0]

# def some functions :
rsort = robjects.r['sort']
rseq = robjects.r['seq']
rlength = robjects.r['length']
rdiv = robjects.r['/']
rmin = robjects.r['min']
rmax = robjects.r['max']
rdiff = robjects.r['-']

# read input file
mymap = robjects.r.readRAST6(inputmap)
elev = rsort('mymap@data$inputmap', decreasing=True)
r_sorted = rsort('mymap@data$elevation.dem', decreasing=True)
p_seq = rseq(1,rlength(r_sorted))
x = rdiv(p_seq,length(r_sorted))
x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )
y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
.... errors
################################################


but maybe it is totally wrong...
this the same code running in ipython.

tring to run it i have :


In [38]: import rpy2.rinterface as rinterface

In [39]: import rpy2.robjects as robjects

In [40]: robjects.r.require('spgrass6', quietly = True)[0]
Out[40]: True

In [41]: rsort = robjects.r['sort']

In [42]: rseq = robjects.r['seq']

In [43]: rlength = robjects.r['length']

In [44]: rdiv = robjects.r['/']

In [45]: rmin = robjects.r['min']

In [46]: rmax = robjects.r['max']

In [47]: rdiff = robjects.r['-']

In [48]: inputmap = 'elevation.dem'

In [49]: mymap = robjects.r.readRAST6(inputmap)
raster map/current region mismatch detected in components:
           cols            rows origin.northing  origin.easting
          FALSE           FALSE           FALSE           FALSE
set plugin=TRUE to override; continuing with plugin=FALSE
/Users/sasha/Downloads/spearfish60/PERMANENT/.tmp/MacBook-Pro-15-di-Massimo-Di-Stefano.local/elevation.dem has GDAL driver GTiff
and has 140 rows and 190 columns

In [50]: elev = rsort('mymap@data$inputmap', decreasing=True)

In [51]: r_sorted = rsort('mymap@data$elevation.dem', decreasing=True)

In [52]: p_seq = rseq(1,rlength(r_sorted))

In [53]: x = rdiv(p_seq,length(r_sorted))

In [54]: x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )

In [55]: y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
  argomento non numerico trasformato in operatore binario
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/rpy2/robjects/__init__.py", line 423, in __call__
RRuntimeError: Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
  argomento non numerico trasformato in operatore binario

################################################
################################################


[[elided Yahoo spam]]

regards,
Massimo.
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [R] use R from python

Barry Rowlingson
On Thu, Jan 21, 2010 at 8:35 PM, Massimo Di Stefano
<[hidden email]> wrote:
> Hi All,
>
> please aplogize me if my qustion is a bit OT here,
> but maybe is there someone that uses R from inside python
> using rpy or rpy2 interface.

> In [54]: x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )
>
> In [55]: y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
> Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
>  argomento non numerico trasformato in operatore binario
> ------------------------------------------------------------
> Traceback (most recent call last):
>  File "<ipython console>", line 1, in <module>
>  File "build/bdist.macosx-10.6-universal/egg/rpy2/robjects/__init__.py", line 423, in __call__
> RRuntimeError: Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
>  argomento non numerico trasformato in operatore binario

 My non-existent Italian is telling me this is non-numeric argument in
binary operator. Something like:

> "hello" - "goodbye"
Error in "hello" - "goodbye" : non-numeric argument to binary operator

 - because you are subtracting the strings "mymap@data$elevation.dem".

 Tracking back, those strings come from:

r_sorted = rsort('mymap@data$elevation.dem', decreasing=True)

 - which is sorting the string vector! Like this:

> sort('mymap@data$elevation.dem', decreasing=TRUE)
[1] "mymap@data$elevation.dem"

 You want to sort the *value* of that object.  You want to sort the
$elevation.dem column of the @data slot of the python R object mymap.

 In a functional form which will translate to your style of rpy2 would
be this in R:

get("$")(slot(mymap,"data"),"elevation.dem")

 You may need to get 'get' and 'slot' from r.robjects in the way you
do other functions. This looks a bit weird to me, but I'm used to
rpy-1 - maybe rpy 2 is like this!

 Hope that points you in the right direction.

Barry

--
blog: http://geospaced.blogspot.com/
web: http://www.maths.lancs.ac.uk/~rowlings
web: http://www.rowlingson.com/
twitter: http://twitter.com/geospacedman
pics: http://www.flickr.com/photos/spacedman

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [R] use R from python

Massimo Di Stefano
Hi Barry,

thanks for your preciouse help!
Please apologize my newbie questions.

i'm able to go ahead tring to port the R code to run under python,
now i'm blocked on the "plot" function.

maybe i'm doing the same error but i haven't yet find a way to debug
my code line.

this what i tried :

# import library (rpy2 + spgrass6)

import rpy2.rinterface as rinterface
import rpy2.robjects as robjects
robjects.r.require('spgrass6', quietly = True)[0]


# def some functions :
rsort = robjects.r['sort']
rseq = robjects.r['seq']
rlength = robjects.r['length']
rdiv = robjects.r['/']
rmin = robjects.r['min']
rmax = robjects.r['max']
rdiff = robjects.r['-']
rget = robjects.r['get']
rslot = robjects.r['slot']
rpng = robjects.r['png']
rplot = robjects.r['plot']
df = robjects.r['data.frame']

# input data and output image
inputmap = 'elevation.dem'
output_image = '/Users/sasha/Desktop/file.png'

# code
mymap = robjects.r.readRAST6(inputmap)
elev = rget("$")(rslot(mymap,"data"),"elevation.dem")
r_sorted = rsort(elev, decreasing=True)
p_seq = rseq(1,rlength(r_sorted))
x = rdiv(p_seq,rlength(r_sorted))
x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )
y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
rpng(output_image)

result = df(x,y)
rplot(result,type = 'l',x)
------------------------------------------------------------
   File "<ipython console>", line 1
SyntaxError: non-keyword arg after keyword arg (<ipython console>, line 1)

my "rplot" actions seems to be wrong :-(

while if i try :

args = [x, y]
kwargs = {'ylab':"foo/bar", 'type':"b", 'col':"blue", 'log':"x"}
robjects.r.plot(*args, **kwargs)

i have a png file, but to "see the file" on my desktop
i had to type "twice" the command :
robjects.r.plot(*args, **kwargs)



thanks!!!

Massimo.

Il giorno 22/gen/2010, alle ore 00.38, Barry Rowlingson ha scritto:

> On Thu, Jan 21, 2010 at 8:35 PM, Massimo Di Stefano

>> Hi All,
>>
>> please aplogize me if my qustion is a bit OT here,
>> but maybe is there someone that uses R from inside python
>> using rpy or rpy2 interface.
>
>> In [54]: x = rdiv( ( rdiff( x, rmin(x) ) ) , ( rdiff( rmax(x) , rmin(x) ) ) )
>>
>> In [55]: y = rdiv( ( rdiff( r_sorted, rmin(r_sorted) ) ) , ( rdiff( rmax(r_sorted) , rmin(r_sorted) ) ) )
>> Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
>>  argomento non numerico trasformato in operatore binario
>> ------------------------------------------------------------
>> Traceback (most recent call last):
>>  File "<ipython console>", line 1, in <module>
>>  File "build/bdist.macosx-10.6-universal/egg/rpy2/robjects/__init__.py", line 423, in __call__
>> RRuntimeError: Errore in .Primitive("-")("mymap@data$elevation.dem", "mymap@data$elevation.dem") :
>>  argomento non numerico trasformato in operatore binario
>
> My non-existent Italian is telling me this is non-numeric argument in
> binary operator. Something like:
>
>> "hello" - "goodbye"
> Error in "hello" - "goodbye" : non-numeric argument to binary operator
>
> - because you are subtracting the strings "mymap@data$elevation.dem".
>
> Tracking back, those strings come from:
>
> r_sorted = rsort('mymap@data$elevation.dem', decreasing=True)
>
> - which is sorting the string vector! Like this:
>
>> sort('mymap@data$elevation.dem', decreasing=TRUE)
> [1] "mymap@data$elevation.dem"
>
> You want to sort the *value* of that object.  You want to sort the
> $elevation.dem column of the @data slot of the python R object mymap.
>
> In a functional form which will translate to your style of rpy2 would
> be this in R:
>
> get("$")(slot(mymap,"data"),"elevation.dem")
>
> You may need to get 'get' and 'slot' from r.robjects in the way you
> do other functions. This looks a bit weird to me, but I'm used to
[[elided Yahoo spam]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...