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
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With