Printing output from a function wrapped with numba @jit

0 views
Skip to first unread message

abhishek...@gmail.com

unread,
Jan 26, 2016, 8:51:04 AM1/26/16
to Numba Public Discussion - Public
How can I print the variables PMean and PStd in the following code? I have been trying for nearly an hour with numba and my function mmm()! 

I have @jit outside my function, and return the variables PMean and PStd at the bottom of the code. But how can I get their values?

Here is my code: 

import numpy as np
import math
from numba import jit


Qpvec = np.logspace(-2,1,101,10)
lenQp
= len(Qpvec)
delta
= 1e-3
P0vec
= Qpvec/delta
SimNum = 100
maxtime
= 0.1
PTotStoch = np.zeros((SimNum,lenQp))
k_minus
= 1e-3
k_cat
= 1-1e-3
k_plus
= 1e-3
zeta
= 1e-4
D0
= 10000
kappa_M
= (k_cat+k_minus)/zeta
QpDeg = np.true_divide(1000*D0*Qpvec*k_cat,1000*Qpvec + kappa_M)


@jit
def mmm():
   
for lenQpInd in range(0,lenQp):
       
for SimNumInd in range(0,SimNum):
           
Qp = Qpvec[lenQpInd]
            P0
= P0vec[lenQpInd]
            DP0
= 0
            P
= math.floor(P0)
            DP
= DP0
            D
= D0
            time
= 0
   
           
while time < maxtime:
                u_time
= pl.rand()
                u_event
= pl.rand()
                rates
=np.array([Qp,zeta*P*D,k_minus*DP,k_cat*DP])
               
PTot = P + DP
                kT
= np.sum(rates)
                tot
= np.cumsum(rates)
                deltaT
= -np.log(1-u_time)/kT
                time
+= deltaT
               
if time > maxtime:
                   
PTotStoch[SimNumInd,lenQpInd] = PTot
                   
break
               
elif u_event*kT < tot[0]:
                    P
+= 1
               
elif u_event*kT < tot[1]:
                    P
-= 1
                    DP
+= 1
                    D
-= 1
               
elif u_event*kT < tot[2]:
                    P
+= 1
                    DP
-= 1
                    D
+= 1
               
elif u_event*kT < tot[3]:
                    DP
-= 1
                    D
+= 1  
   
PMean = PTotStoch.mean(axis=0)
   
PStd = PTotStoch.std(axis=0)
   
return PMean
   
return PStd
Thank you.

Joshua Adelman

unread,
Jan 26, 2016, 9:43:43 AM1/26/16
to Numba Public Discussion - Public, abhishek...@gmail.com
You need to return a tuple like:

return PMean, PStd


at the end of your mmm() method. Currently, the second `return` statement will not be executed. This is not specific to Numba and would be the case for any python function. Then you would call it like:

PMean, PStd = mmm()

and then you could access the variables outside of the function call. As far as printing goes, if you add:

from __future__ import print_function

to the top of your script, you can write `print(VariableName)` inside your function. Note that this changes the print semantics throughout your script to the Python3 syntax, so you'll want to read up on the difference.

Hope that helps,
Josh

abhishek...@gmail.com

unread,
Jan 26, 2016, 11:18:52 AM1/26/16
to Numba Public Discussion - Public, abhishek...@gmail.com
So, I included the future statement to the top of my script, and then wrote 
    return PMean, PStd
    print(PMean)
    print(PStd)

at the bottom. It does not print anything still - I tried searching online for information on the future statement, but couldn't understand most of it. What am I missing?

Joshua Adelman

unread,
Jan 26, 2016, 11:21:05 AM1/26/16
to Numba Public Discussion - Public, abhishek...@gmail.com
Once you execute the `return` line in a function, anything later is never reached. You will need to either print before the return or outside of the function after it is called. As far as the __future__ model goes, see the docs:


Hope that helps,
Josh 
Reply all
Reply to author
Forward
0 new messages