Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format tqdm progress bar to show progress per minute instead of per second?

I couldn't find this on GitHub or in the docs but I'm wondering whether there is an integrated way (preferred) or workaround to show the average time it takes for one iteration in minutes rather than seconds.

Specifically, tqdm shows something like 1283.31s/it but if each iteration takes several minutes or hours, it would be more helpful to show something like 21m/it.

like image 266
pedjjj Avatar asked Nov 29 '19 15:11

pedjjj


1 Answers

We can do this by adding a custom bar_format parameter to a custom instance of tqdm using the instructions partially described in the manual:

class TqdmExtraFormat(tqdm):
    """Provides a `minutes per iteration` format parameter"""
    @property
    def format_dict(self):
        d = super(TqdmExtraFormat, self).format_dict
        rate_min = '{:.2f}'.format(1/d["rate"] / 60) if d["rate"] else '?'
        d.update(rate_min=(rate_min + ' min/' + d['unit']))
        return d

What we're doing here is overriding format_dict with a custom version that adds a new stat that can be used in the bar_format argument. d is the existing format_dict, so we can use that to get the value of {rate}, convert it to time per iteration (1/rate), divide by 60, then format it to 2 decimal places. Then, we update d with the new stat – we can even use string concatenation to add some formatting. Note that before any iterations have completed, {rate} is None, which means that this will give an error unless you do the if d["rate"] check.

Once this is done, we can use this stat in the bar_format parameter string alongside the ones listed in Parameters if you use your custom instance of tqdm:

b='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_min}{postfix}]'
for i in TqdmExtraFormat(range(2), unit_scale = 60, bar_format=b):
    time.sleep(6)

enter image description here

like image 99
divibisan Avatar answered Sep 22 '22 16:09

divibisan