2020-06-10 16:04:54 +00:00
|
|
|
"""
|
2021-12-01 20:47:00 +00:00
|
|
|
Matplotlib GUI progressbar decorator for iterators.
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
Usage:
|
2021-12-01 20:47:00 +00:00
|
|
|
>>> from tqdm.gui import trange, tqdm
|
|
|
|
>>> for i in trange(10):
|
|
|
|
... ...
|
2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
# future division is important to divide integers and get as
|
|
|
|
# a result precise floating numbers (instead of truncated int)
|
2021-12-01 20:47:00 +00:00
|
|
|
import re
|
2020-06-10 16:04:54 +00:00
|
|
|
from warnings import warn
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
# to inherit from the tqdm class
|
|
|
|
from .std import TqdmExperimentalWarning
|
|
|
|
from .std import tqdm as std_tqdm
|
2024-03-03 17:15:23 +00:00
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
# import compatibility functions and utilities
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
__author__ = {"github.com/": ["casperdcl", "lrq3000"]}
|
|
|
|
__all__ = ['tqdm_gui', 'tgrange', 'tqdm', 'trange']
|
|
|
|
|
|
|
|
|
|
|
|
class tqdm_gui(std_tqdm): # pragma: no cover
|
2021-12-01 20:47:00 +00:00
|
|
|
"""Experimental Matplotlib GUI version of tqdm!"""
|
2020-06-10 16:04:54 +00:00
|
|
|
# TODO: @classmethod: write() on GUI?
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-12-01 20:47:00 +00:00
|
|
|
from collections import deque
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
import matplotlib as mpl
|
|
|
|
import matplotlib.pyplot as plt
|
2021-12-01 20:47:00 +00:00
|
|
|
kwargs = kwargs.copy()
|
2020-06-10 16:04:54 +00:00
|
|
|
kwargs['gui'] = True
|
2021-12-01 20:47:00 +00:00
|
|
|
colour = kwargs.pop('colour', 'g')
|
2020-06-10 16:04:54 +00:00
|
|
|
super(tqdm_gui, self).__init__(*args, **kwargs)
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
if self.disable:
|
2020-06-10 16:04:54 +00:00
|
|
|
return
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
warn("GUI is experimental/alpha", TqdmExperimentalWarning, stacklevel=2)
|
2020-06-10 16:04:54 +00:00
|
|
|
self.mpl = mpl
|
|
|
|
self.plt = plt
|
|
|
|
|
|
|
|
# Remember if external environment uses toolbars
|
|
|
|
self.toolbar = self.mpl.rcParams['toolbar']
|
|
|
|
self.mpl.rcParams['toolbar'] = 'None'
|
|
|
|
|
|
|
|
self.mininterval = max(self.mininterval, 0.5)
|
|
|
|
self.fig, ax = plt.subplots(figsize=(9, 2.2))
|
|
|
|
# self.fig.subplots_adjust(bottom=0.2)
|
2021-12-01 20:47:00 +00:00
|
|
|
total = self.__len__() # avoids TypeError on None #971
|
2020-06-10 16:04:54 +00:00
|
|
|
if total is not None:
|
|
|
|
self.xdata = []
|
|
|
|
self.ydata = []
|
|
|
|
self.zdata = []
|
|
|
|
else:
|
|
|
|
self.xdata = deque([])
|
|
|
|
self.ydata = deque([])
|
|
|
|
self.zdata = deque([])
|
|
|
|
self.line1, = ax.plot(self.xdata, self.ydata, color='b')
|
|
|
|
self.line2, = ax.plot(self.xdata, self.zdata, color='k')
|
|
|
|
ax.set_ylim(0, 0.001)
|
|
|
|
if total is not None:
|
|
|
|
ax.set_xlim(0, 100)
|
2021-12-01 20:47:00 +00:00
|
|
|
ax.set_xlabel("percent")
|
|
|
|
self.fig.legend((self.line1, self.line2), ("cur", "est"),
|
2020-06-10 16:04:54 +00:00
|
|
|
loc='center right')
|
|
|
|
# progressbar
|
2021-12-01 20:47:00 +00:00
|
|
|
self.hspan = plt.axhspan(0, 0.001, xmin=0, xmax=0, color=colour)
|
2020-06-10 16:04:54 +00:00
|
|
|
else:
|
|
|
|
# ax.set_xlim(-60, 0)
|
|
|
|
ax.set_xlim(0, 60)
|
|
|
|
ax.invert_xaxis()
|
2021-12-01 20:47:00 +00:00
|
|
|
ax.set_xlabel("seconds")
|
|
|
|
ax.legend(("cur", "est"), loc='lower left')
|
2020-06-10 16:04:54 +00:00
|
|
|
ax.grid()
|
|
|
|
# ax.set_xlabel('seconds')
|
2021-12-01 20:47:00 +00:00
|
|
|
ax.set_ylabel((self.unit if self.unit else "it") + "/s")
|
2020-06-10 16:04:54 +00:00
|
|
|
if self.unit_scale:
|
2021-12-01 20:47:00 +00:00
|
|
|
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
|
2020-06-10 16:04:54 +00:00
|
|
|
ax.yaxis.get_offset_text().set_x(-0.15)
|
|
|
|
|
|
|
|
# Remember if external environment is interactive
|
|
|
|
self.wasion = plt.isinteractive()
|
|
|
|
plt.ion()
|
|
|
|
self.ax = ax
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.disable:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.disable = True
|
|
|
|
|
|
|
|
with self.get_lock():
|
|
|
|
self._instances.remove(self)
|
|
|
|
|
|
|
|
# Restore toolbars
|
|
|
|
self.mpl.rcParams['toolbar'] = self.toolbar
|
|
|
|
# Return to non-interactive mode
|
|
|
|
if not self.wasion:
|
|
|
|
self.plt.ioff()
|
2021-12-01 20:47:00 +00:00
|
|
|
if self.leave:
|
|
|
|
self.display()
|
|
|
|
else:
|
2020-06-10 16:04:54 +00:00
|
|
|
self.plt.close(self.fig)
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def clear(self, *_, **__):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def display(self, *_, **__):
|
2020-06-10 16:04:54 +00:00
|
|
|
n = self.n
|
|
|
|
cur_t = self._time()
|
|
|
|
elapsed = cur_t - self.start_t
|
|
|
|
delta_it = n - self.last_print_n
|
|
|
|
delta_t = cur_t - self.last_print_t
|
|
|
|
|
|
|
|
# Inline due to multiple calls
|
|
|
|
total = self.total
|
|
|
|
xdata = self.xdata
|
|
|
|
ydata = self.ydata
|
|
|
|
zdata = self.zdata
|
|
|
|
ax = self.ax
|
|
|
|
line1 = self.line1
|
|
|
|
line2 = self.line2
|
|
|
|
# instantaneous rate
|
|
|
|
y = delta_it / delta_t
|
|
|
|
# overall rate
|
|
|
|
z = n / elapsed
|
|
|
|
# update line data
|
|
|
|
xdata.append(n * 100.0 / total if total else cur_t)
|
|
|
|
ydata.append(y)
|
|
|
|
zdata.append(z)
|
|
|
|
|
|
|
|
# Discard old values
|
|
|
|
# xmin, xmax = ax.get_xlim()
|
|
|
|
# if (not total) and elapsed > xmin * 1.1:
|
|
|
|
if (not total) and elapsed > 66:
|
|
|
|
xdata.popleft()
|
|
|
|
ydata.popleft()
|
|
|
|
zdata.popleft()
|
|
|
|
|
|
|
|
ymin, ymax = ax.get_ylim()
|
|
|
|
if y > ymax or z > ymax:
|
|
|
|
ymax = 1.1 * y
|
|
|
|
ax.set_ylim(ymin, ymax)
|
|
|
|
ax.figure.canvas.draw()
|
|
|
|
|
|
|
|
if total:
|
|
|
|
line1.set_data(xdata, ydata)
|
|
|
|
line2.set_data(xdata, zdata)
|
|
|
|
try:
|
|
|
|
poly_lims = self.hspan.get_xy()
|
|
|
|
except AttributeError:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.hspan = self.plt.axhspan(0, 0.001, xmin=0, xmax=0, color='g')
|
2020-06-10 16:04:54 +00:00
|
|
|
poly_lims = self.hspan.get_xy()
|
|
|
|
poly_lims[0, 1] = ymin
|
|
|
|
poly_lims[1, 1] = ymax
|
|
|
|
poly_lims[2] = [n / total, ymax]
|
|
|
|
poly_lims[3] = [poly_lims[2, 0], ymin]
|
|
|
|
if len(poly_lims) > 4:
|
|
|
|
poly_lims[4, 1] = ymin
|
|
|
|
self.hspan.set_xy(poly_lims)
|
|
|
|
else:
|
|
|
|
t_ago = [cur_t - i for i in xdata]
|
|
|
|
line1.set_data(t_ago, ydata)
|
|
|
|
line2.set_data(t_ago, zdata)
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
d = self.format_dict
|
|
|
|
# remove {bar}
|
|
|
|
d['bar_format'] = (d['bar_format'] or "{l_bar}<bar/>{r_bar}").replace(
|
|
|
|
"{bar}", "<bar/>")
|
|
|
|
msg = self.format_meter(**d)
|
|
|
|
if '<bar/>' in msg:
|
2024-03-03 17:15:23 +00:00
|
|
|
msg = "".join(re.split(r'\|?<bar/>\|?', msg, maxsplit=1))
|
2021-12-01 20:47:00 +00:00
|
|
|
ax.set_title(msg, fontname="DejaVu Sans Mono", fontsize=11)
|
2020-06-10 16:04:54 +00:00
|
|
|
self.plt.pause(1e-9)
|
|
|
|
|
|
|
|
|
|
|
|
def tgrange(*args, **kwargs):
|
2024-03-03 17:15:23 +00:00
|
|
|
"""Shortcut for `tqdm.gui.tqdm(range(*args), **kwargs)`."""
|
|
|
|
return tqdm_gui(range(*args), **kwargs)
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Aliases
|
|
|
|
tqdm = tqdm_gui
|
|
|
|
trange = tgrange
|