As a general note, if you want to do R-like manipulation of data in
Python, you should look into 'numpy'. It's very standard, and provides
an array type that works like R vectors, plus functions like 'log' and
'sum' that apply to all elements quickly. rpy2 also has some code to
make it easy to interact with numpy:
  http://rpy.sourceforge.net/rpy2/doc-2.1/html/numpy.html

Using a 'for' loop is always going to be slow in both R and Python.

-- Nathaniel

On Thu, Feb 23, 2012 at 6:00 PM, Bruno Santos <bacmsan...@gmail.com> wrote:
> I am a bit confused now I am using rinterface and not robjects so how do I
> managed to do the same with the rinterface? If I try to multiply two
> IntSexpVectors I just get a combination of vectors as in python rather than
> an actual sum of the two as in R.
>
>
>
> 2012/2/23 Laurent Gautier <lgaut...@gmail.com>
>>
>> On 2012-02-23 18:16, Bruno Santos wrote:
>>
>> Thank you Laurent for your quick reply.
>>
>> The if k[i] == 0 in R is implement as being  + p * (x == 0) which will add
>> p if k[i]==0 or zero if not. I was trying to came up with a closer
>> implementation but I cannot do sums or multiplications between Robjects is
>> that right?
>>
>>
>> sum(log((1-p) * dnbinom(x, mu = mu, size = size, log = FALSE) + p * (x ==
>> 0)))
>>
>>
>> (rlog(1-p).ro * dnbinom(x, mu = mu, size = size, log =
>> rinterface.FALSE)).ro + (p.ro * (x.ro == 0))
>>
>> ...or something is that style (I did not test the line above). That's
>> still a lot at the robjects level, and moving down to rinterface should be a
>> little faster.
>>
>>
>>
>>
>>     for elem in dnbinom(x=k, mu=mu, size=size,
>> log=rinterface.BoolSexpVector([False])):
>>         if k[i] == 0:result+=rlog((1-p)*elem+p)[0]
>>         else: result+=rlog((1-p)*elem)[0]
>>
>>
>>
>>
>>
>>
>> 2012/2/23 Laurent Gautier <lgaut...@gmail.com>
>>>
>>> On 2012-02-23 17:57, Bruno Santos wrote:
>>>
>>> Hello everyone'
>>>
>>> I am trying to port an R function to estimate some parameters to the rpy2
>>> but the performance in python is appaling. So I was wondering if there is a
>>> best way of doing this. What I have at the moment is:
>>> R code
>>> finbinom <-function(pars,x)
>>> {
>>> p<-pars[1]
>>> mu <- pars[2]
>>> size <- pars[3]
>>> if(any(pars <= 0)) return(NA)
>>> if(p >= 1) return(NA)
>>> sum(log((1-p) * dnbinom(x, mu = mu, size = size, log = FALSE) + p * (x ==
>>> 0)))
>>> }
>>>
>>> opNB <- optim(par = c(0.9, 100, 1), fn = finbinom, control = list(fnscale
>>> = -1), x = distances)
>>>
>>> Python
>>> def zero_inflated(Pars,k):
>>>     p,mu,size = Pars
>>>     if p<=0 or mu<=0 or size<=0:return rinterface.NA_Real
>>>     if p>=1:return rinterface.NA_Real
>>>     i=0
>>>     result=0
>>>     #return rsum(rlog(rinterface.FloatSexpVector([1-p]) * dnbinom(k, mu =
>>> mu, size = size, log = rinterface.BoolSexpVector([False]))))
>>>
>>>     for elem in dnbinom(x=k, mu=mu, size=size,
>>> log=rinterface.BoolSexpVector([False])):
>>>         if k[i] == 0:result+=rlog((1-p)*elem+p)[0]
>>>         else: result+=rlog((1-p)*elem)[0]
>>>
>>>
>>> if size is large, this is potentially a bottleneck. Note that when
>>> comparing the performances with pure R one should consider comparing
>>> identical code: the R bit doesn't seem to have the conditional branch if
>>> k[i] == 0 (or did I miss something).
>>>
>>>
>>>     return result
>>> cost_zinb = rinterface.rternalize(zero_inflated)
>>> start_params = [0.9, 100, 1]
>>> res = stats.optim(start_params, cost_zinb, control = rlist(fnscale = -1),
>>> k=vDistances)
>>> print 'Distances between sRNAs:',res[0],#res[1],res[2],res[3]
>>>
>>> Thank you very much in advance.
>>> Best,
>>> Bruno
>>>
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Virtualization & Cloud Management Using Capacity Planning
>>> Cloud computing makes use of virtualization - but cloud computing
>>> also focuses on allowing computing to be delivered as a service.
>>> http://www.accelacomm.com/jaw/sfnl/114/51521223/
>>>
>>>
>>>
>>> _______________________________________________
>>> rpy-list mailing list
>>> rpy-list@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/rpy-list
>>>
>>>
>>
>>
>
>
> ------------------------------------------------------------------------------
> Virtualization & Cloud Management Using Capacity Planning
> Cloud computing makes use of virtualization - but cloud computing
> also focuses on allowing computing to be delivered as a service.
> http://www.accelacomm.com/jaw/sfnl/114/51521223/
> _______________________________________________
> rpy-list mailing list
> rpy-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rpy-list
>

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to