7

I am using a np.vectorize-ed function and would like to see the progress of the function with tqdm. However, I have not been able to figure out how to do this.

All the suggestions I have found relate to converting the calculation into a for-loop, or into a pd.DataFrame.

Avi Vajpeyi
  • 195
  • 1
  • 10

2 Answers2

1

I finally found a method that works to get the tqdm progress bar to update with a np.vectorize function. I wrap the vectorize function using a with

with tqdm(total=len(my_inputs)) as pbar:
    my_output = np.vectorize(my_function)(my_inputs)

in my_function() I then add the following lines

global pbar
pbar.update(1)

and voila! I now have a progress bar that updates with each iteration. Only slight performance dip on my code.

Note: when you instantiate the function it might complain that pbar is not yet defined. Simply put a pbar = 0 before you instantiate, and then the function will call the pbar defined by the with

Hope it helps everyone reading here.

0

To the best of my knowledge,tqdm does not wrap over numpy.vectorize.

To display the progress bar for numpy arrays, numpy.ndenumerate can be used.

Given the inputs and function:

import numpy as np
from tqdm import tqdm

a = np.array([1, 2, 3, 4])
b = 2
def myfunc(a, b):
    "Return a-b if a>b, otherwise return a+b"
    if a > b:
        return a - b
    else:
        return a + b

Replace this vectorised part below

# using numpy.vectorize
vfunc = np.vectorize(myfunc)
vfunc(a, b)

with this

# using numpy.ndenumerate instead
[myfunc(x,b) for index, x in tqdm(np.ndenumerate(a))]

To see the tqdm progress.

anilbey
  • 1,286
  • 3
  • 17
  • 34
  • 2
    Ah this is still just using a `for-loop` which I know how to do. I was hoping on getting `tqdm` to work with `np.vectorize`. Additionally, I tried out your method, I noticed a significant time-difference. Is this because of the logging or because your method does not use `np.vectorize`? EG: `np_vectorized_sum timing: 0:00:00.005858` and `np_ndenumerate_sum timing: 0:00:02.856294` (sum over 100 by 100 mesh grid) – Avi Vajpeyi Sep 24 '20 at 03:00
  • 2
    I think the difference is due to the use of SIMD in `np.vectorize`. – anilbey Sep 24 '20 at 10:00