Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar when using Numba (tqdm is not working) -Python

I have been trying to run a code using numba and I have also added a print to see the progress of my code :

from numba import jit,njit,prange
import numpy as np
# for minimum reproducible example
a=1e5
ar = np.random.rand(a)
at = np.random.rand(a)
an = np.random.rand(a)
###############################3


tau    = 1        # time lag
window = 6000


@njit(parallel=True)
def func_DB(ar,at,an):
    DBtotal= np.zeros((len(an)-tau))
    k = 0
    for i in prange(0,len(an)-tau,1):
        DBtotal[i] = np.sqrt((ar[i + tau]- ar[i])**2 +(at[i + tau]- at[i])**2 +(an[i + tau]- an[i])**2)
       ## To print the progress
         if i%1e5==0:
            k+=1
            print(k*1e5/len(DBtotal))
    return DBtotal


@njit(parallel=True)
def func_PVI(tau, window):
    PVI = np.zeros((len(DBtotal)))
    k = 0
    for i in prange(int(window/2),len(DBtotal)-int(window/2)): 
        PVI[i] = DBtotal[i]/np.sqrt((np.mean(DBtotal[i-int(window/2):i+int(window/2)]**2)))
       # To print the progress
        if i%1e5==0:
            k+=1
            print(k*1e5/len(DBtotal))
    return PVI 
DBtotal = func_DB(ar,at,an)
PVI     = func_PVI(DBtotal,tau, window)

However, while the code is running I dont get what I expected (i.e. values that go from 0 to 1 as the code is progressing) Instead, I get this:

Out[:] 0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.009479390005044932
      0.018958780010089864

Could someone suggest a way to see the progress of the code?

Also, any suggestions to make the code more efficient would be much appreciated!


1 Answers

I broke the function down into pieces and wrapped a tqdm around it.

Instead of

@jit(nopython=True)
def dothings(A, rows, cols):
    for r in range(rows):
        for c in range(cols):
            stuff...

dothings(data, data.shape[0], data.shape[1])

I used

rows=data.shape[0]
@jit(nopython=True)
def dothings(A, cols, r):
#    for r in range(rows):
        for c in range(cols):
            stuff...
    
for r in tqdm.tqdm(range(rows), total=rows):
    dothings(data, data.shape[1], r)
like image 54
user15621395 Avatar answered Jul 06 '26 12:07

user15621395



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!