diff --git a/README.rst b/README.rst index 7bca21e7..47843a38 100644 --- a/README.rst +++ b/README.rst @@ -137,13 +137,12 @@ Documentation progressbar every time a value is requested. """ - def __init__(self, iterable=None, desc=None, total=None, - initial=0, leave=False, file=sys.stderr, - ncols=None, mininterval=0.1, maxinterval=10.0, - miniters=None, ascii=None, disable=False, - unit='it', unit_scale=False, - dynamic_ncols=False, smoothing=0.3, - nested=False): + def __init__(self, iterable=None, desc=None, total=None, leave=False, + file=sys.stderr, ncols=None, mininterval=0.1, + maxinterval= 10.0, miniters=None, ascii=None, + disable=False, unit='it', unit_scale=False, + dynamic_ncols=False, smoothing=0.3, nested =False, + bar_format=None, initial=0, gui=False): Parameters ~~~~~~~~~~ @@ -159,9 +158,6 @@ Parameters statistics are displayed (no ETA, no progressbar). If `gui` is True and this parameter needs subsequent updating, specify an initial arbitrary large positive integer, e.g. int(9e9). -* initial : int, optional - The initial counter value. Useful when restarting a progress - bar [default: 0]. * leave : bool, optional If [default: False], removes all traces of the progressbar upon termination of iteration. @@ -214,6 +210,9 @@ Parameters '| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'. Possible vars: bar, n, n_fmt, total, total_fmt, percentage, rate, rate_fmt, elapsed, remaining, l_bar, r_bar, desc. +* initial : int, optional + The initial counter value. Useful when restarting a progress + bar [default: 0]. Returns diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index be8bd66e..9c62ca63 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -238,11 +238,12 @@ class tqdm(object): like the original iterable, but prints a dynamically updating progressbar every time a value is requested. """ - def __init__(self, iterable=None, desc=None, total=None, initial=0, - leave=False, file=sys.stderr, ncols=None, mininterval=0.1, - maxinterval=10.0, miniters=None, ascii=None, disable=False, - unit='it', unit_scale=False, dynamic_ncols=False, - smoothing=0.3, nested=False, bar_format=None, gui=False): + def __init__(self, iterable=None, desc=None, total=None, leave=False, + file=sys.stderr, ncols=None, mininterval=0.1, + maxinterval= 10.0, miniters=None, ascii=None, + disable=False, unit='it', unit_scale=False, + dynamic_ncols=False, smoothing=0.3, nested =False, + bar_format=None, initial=0, gui=False): """ Parameters ---------- @@ -257,9 +258,6 @@ class tqdm(object): statistics are displayed (no ETA, no progressbar). If `gui` is True and this parameter needs subsequent updating, specify an initial arbitrary large positive integer, e.g. int(9e9). - initial : int, optional - The initial counter value. Useful when restarting a progress - bar [default: 0]. leave : bool, optional If [default: False], removes all traces of the progressbar upon termination of iteration. @@ -311,6 +309,9 @@ class tqdm(object): '| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'. Possible vars: bar, n, n_fmt, total, total_fmt, percentage, rate, rate_fmt, elapsed, remaining, l_bar, r_bar, desc. + initial : int, optional + The initial counter value. Useful when restarting a progress + bar [default: 0]. gui : bool, optional WARNING: internal parameter - do not use. Use tqdm_gui(...) instead. If set, will attempt to use @@ -381,8 +382,7 @@ class tqdm(object): self.nested = nested self.bar_format = bar_format - # Init the time/iterations counters - self.start_t = self.last_print_t = time() + # Init the iterations counters self.last_print_n = initial self.n = initial @@ -396,6 +396,9 @@ class tqdm(object): (dynamic_ncols(file) if dynamic_ncols else ncols), self.desc, ascii, unit, unit_scale, None, bar_format)) + # Init the time counter + self.start_t = self.last_print_t = time() + def __len__(self): return len(self.iterable) if self.iterable else self.total diff --git a/tqdm/_tqdm_gui.py b/tqdm/_tqdm_gui.py index dd951da8..edb069cb 100644 --- a/tqdm/_tqdm_gui.py +++ b/tqdm/_tqdm_gui.py @@ -104,6 +104,7 @@ class tqdm_gui(tqdm): # pragma: no cover # ncols = self.ncols mininterval = self.mininterval + maxinterval = self.maxinterval miniters = self.miniters dynamic_miniters = self.dynamic_miniters unit = self.unit @@ -114,8 +115,9 @@ class tqdm_gui(tqdm): # pragma: no cover last_print_n = self.last_print_n n = self.n # dynamic_ncols = self.dynamic_ncols - # smoothing = self.smoothing - # avg_rate = self.avg_rate + smoothing = self.smoothing + avg_rate = self.avg_rate + bar_format = self.bar_format plt = self.plt ax = self.ax @@ -137,6 +139,13 @@ class tqdm_gui(tqdm): # pragma: no cover delta_t = cur_t - last_print_t if delta_t >= mininterval: # pragma: no cover elapsed = cur_t - start_t + # EMA (not just overall average) + if smoothing and delta_t: + avg_rate = delta_it / delta_t \ + if avg_rate is None \ + else smoothing * delta_it / delta_t + \ + (1 - smoothing) * avg_rate + # Inline due to multiple calls total = self.total # instantaneous rate @@ -185,15 +194,25 @@ class tqdm_gui(tqdm): # pragma: no cover ax.set_title(format_meter( n, total, elapsed, 0, - self.desc, ascii, unit, unit_scale), - fontname="DejaVu Sans Mono", - fontsize=11) + self.desc, ascii, unit, unit_scale, avg_rate, + bar_format), + fontname="DejaVu Sans Mono", fontsize=11) plt.pause(1e-9) # If no `miniters` was specified, adjust automatically # to the maximum iteration rate seen so far. if dynamic_miniters: - miniters = max(miniters, delta_it) + if maxinterval and delta_t > maxinterval: + # Set miniters to correspond to maxinterval + miniters = delta_it * maxinterval / delta_t + elif mininterval and delta_t: + # EMA-weight miniters to converge + # towards the timeframe of mininterval + miniters = smoothing * delta_it * mininterval \ + / delta_t + (1 - smoothing) * miniters + else: + miniters = smoothing * delta_it + \ + (1 - smoothing) * miniters # Store old values for next call last_print_n = n @@ -222,6 +241,13 @@ class tqdm_gui(tqdm): # pragma: no cover delta_t = cur_t - self.last_print_t if delta_t >= self.mininterval: elapsed = cur_t - self.start_t + # EMA (not just overall average) + if self.smoothing and delta_t: + self.avg_rate = delta_it / delta_t \ + if self.avg_rate is None \ + else self.smoothing * delta_it / delta_t + \ + (1 - self.smoothing) * self.avg_rate + # Inline due to multiple calls total = self.total ax = self.ax @@ -271,15 +297,27 @@ class tqdm_gui(tqdm): # pragma: no cover ax.set_title(format_meter( self.n, total, elapsed, 0, - self.desc, self.ascii, self.unit, self.unit_scale), - fontname="DejaVu Sans Mono", - fontsize=11) + self.desc, self.ascii, self.unit, self.unit_scale, + self.avg_rate, self.bar_format), + fontname="DejaVu Sans Mono", fontsize=11) self.plt.pause(1e-9) # If no `miniters` was specified, adjust automatically to the # maximum iteration rate seen so far. + # e.g.: After running `tqdm.update(5)`, subsequent + # calls to `tqdm.update()` will only cause an update after + # at least 5 more iterations. if self.dynamic_miniters: - self.miniters = max(self.miniters, delta_it) + if self.maxinterval and delta_t > self.maxinterval: + self.miniters = self.miniters * self.maxinterval \ + / delta_t + elif self.mininterval and delta_t: + self.miniters = self.smoothing * delta_it \ + * self.mininterval / delta_t + \ + (1 - self.smoothing) * self.miniters + else: + self.miniters = self.smoothing * delta_it + \ + (1 - self.smoothing) * self.miniters # Store old values for next call self.last_print_n = self.n