Commit a2674180 authored by augustin_s's avatar augustin_s 🐍
Browse files

set default units to Hz; format rate such that it does not jump around at <= 100 Hz

parent 1c145045
......@@ -2,8 +2,26 @@ from time import sleep
import tqdm
def tqdm_sleep(seconds, ndiv=100):
delta = seconds / float(ndiv)
for _ in tqdm.trange(ndiv):
sleep(delta)
class tqdm_mod(tqdm.tqdm):
def __init__(self, *args, **kwargs):
kwargs.setdefault("unit", "@") # use "@/s" to signal Hz
kwargs.setdefault("unit_scale", True) # this enables use of format_sizeof
super().__init__(*args, **kwargs)
def format_meter(self, *args, **kwargs):
res = super().format_meter(*args, **kwargs)
# these have to have the same length otherwise the combined line gets messed up
res = res.replace("@/s", " Hz")
return res
def set(self, elapsed):
"""
update with elapsed n, i.e., the delta between start and current n
......@@ -19,11 +37,18 @@ def clamp(val, vmin, vmax):
return val
def format_sizeof(num, *args, **kwargs):
# format floats such that they accommodate up to 100.x without jumping around
if isinstance(num, float):
return f"{num:5.1f}" # len("100") + len(".1") == 3+2 == 5
# for everything else, use the default string representation
return str(num)
def tqdm_sleep(seconds, ndiv=100):
delta = seconds / float(ndiv)
for _ in tqdm.trange(ndiv):
sleep(delta)
# format_meter is a staticmethod, thus has no self and uses the tqdm class instead
# hence need to overwrite at the source to convince it to use the custom format_sizeof
tqdm.tqdm.format_sizeof = format_sizeof
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment