graphgallery.utils

tqdm Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested.
Progbar A progress bar for display.
setup_logger Initialize the graphgallery logger and set its verbosity level to “DEBUG”.
get_logger Get a logger for a given name.
BunchDict Container object for datasets Dictionary-like object that exposes its keys as attributes and remembers insertion order.
tqdm(*args, **kwargs)[source]

Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested.

iterable : iterable, optional
Iterable to decorate with a progressbar. Leave blank to manually manage the updates.
desc : str, optional
Prefix for the progressbar.
total : int or float, optional
The number of expected iterations. If unspecified, len(iterable) is used if possible. If float(“inf”) or as a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If gui is True and this parameter needs subsequent updating, specify an initial arbitrary large positive number, e.g. 9e9.
leave : bool, optional
If [default: True], keeps all traces of the progressbar upon termination of iteration. If None, will leave only if position is 0.
file : io.TextIOWrapper or io.StringIO, optional
Specifies where to output the progress messages (default: sys.stderr). Uses file.write(str) and file.flush() methods. For encoding, see write_bytes.
ncols : int, optional
The width of the entire output message. If specified, dynamically resizes the progressbar to stay within this bound. If unspecified, attempts to use environment width. The fallback is a meter width of 10 and no limit for the counter and statistics. If 0, will not print any meter (only stats).
mininterval : float, optional
Minimum progress display update interval [default: 0.1] seconds.
maxinterval : float, optional
Maximum progress display update interval [default: 10] seconds. Automatically adjusts miniters to correspond to mininterval after long display update lag. Only works if dynamic_miniters or monitor thread is enabled.
miniters : int or float, optional
Minimum progress display update interval, in iterations. If 0 and dynamic_miniters, will automatically adjust to equal mininterval (more CPU efficient, good for tight loops). If > 0, will skip display of specified number of iterations. Tweak this and mininterval to get very efficient loops. If your progress is erratic with both fast and slow iterations (network, skipping items, etc) you should set miniters=1.
ascii : bool or str, optional
If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters ” 123456789#”.
disable : bool, optional
Whether to disable the entire progressbar wrapper [default: False]. If set to None, disable on non-TTY.
unit : str, optional
String that will be used to define the unit of each iteration [default: it].
unit_scale : bool or int or float, optional
If 1 or True, the number of iterations will be reduced/scaled automatically and a metric prefix following the International System of Units standard will be added (kilo, mega, etc.) [default: False]. If any other non-zero number, will scale total and n.
dynamic_ncols : bool, optional
If set, constantly alters ncols and nrows to the environment (allowing for window resizes) [default: False].
smoothing : float, optional
Exponential moving average smoothing factor for speed estimates (ignored in GUI mode). Ranges from 0 (average speed) to 1 (current/instantaneous speed) [default: 0.3].
bar_format : str, optional

Specify a custom bar string formatting. May impact performance. [default: ‘{l_bar}{bar}{r_bar}’], where l_bar=’{desc}: {percentage:3.0f}%|’ and r_bar=’| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ‘

‘{rate_fmt}{postfix}]’
Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta.

Note that a trailing “: ” is automatically removed after {desc} if the latter is empty.

initial : int or float, optional
The initial counter value. Useful when restarting a progress bar [default: 0]. If using float, consider specifying {n:.3f} or similar in bar_format, or specifying unit_scale.
position : int, optional
Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads).
postfix : dict or *, optional
Specify additional stats to display at the end of the bar. Calls set_postfix(**postfix) if possible (dict).
unit_divisor : float, optional
[default: 1000], ignored unless unit_scale is True.
write_bytes : bool, optional
If (default: None) and file is unspecified, bytes will be written in Python 2. If True will also write bytes. In all other cases will default to unicode.
lock_args : tuple, optional
Passed to refresh for intermediate output (initialisation, iterating, and updating).
nrows : int, optional
The screen height. If specified, hides nested bars outside this bound. If unspecified, attempts to use environment height. The fallback is 20.
colour : str, optional
Bar colour (e.g. ‘green’, ‘#00ff00’).
delay : float, optional
Don’t display until [default: 0] seconds have elapsed.
gui : bool, optional
WARNING: internal parameter - do not use. Use tqdm.gui.tqdm(…) instead. If set, will attempt to use matplotlib animations for a graphical output [default: False].

out : decorated iterator.

class Progbar(target: int, width: int = 30, verbose: int = 1, interval: float = 0.05, unit_name: str = 'step')[source]

A progress bar for display.

Parameters:
  • target (int) – total number of steps expected.
  • width (int, optional) – progress bar width on screen, by default 30
  • verbose (int, optional) – verbosity mode, 0 (silent), 1 (verbose), 2 (semi-verbose), by default 1
  • interval (float, optional) – minimum visual progress update interval (in seconds), by default 0.05
  • unit_name (str, optional) – display name for step counts (usually “step” or “sample”), by default ‘step’

Example

>>> from graphgallery.utils import Progbar
>>> pbar = Progbar(5)
>>> for i in range(5):
...     pbar.add(1, msg=f'current number {i}')
5/5 [==============================] - Total: 3.22ms - 643us/step- current number 4
>>> pbar = Progbar(5)
>>> for i in range(5):
...     pbar.update(i+1, msg=f'current number {i}')
5/5 [==============================] - Total: 3.22ms - 643us/step- current number 4
update(current: int, msg: Union[str, List[T], Tuple, None] = None, finalize: Optional[bool] = None)[source]

Updates the progress bar using current value.

Parameters:
  • current (int) – index of current step
  • msg (Optional[Union[str, List, Tuple]], optional) – (name, value_for_last_step) or string messages, by default None
  • finalize (Optional[bool], optional) – whether this is the last update for the progress bar. If None, defaults to current >= self.target, by default None
Raises:

ValueError – invalid message msg for progress bar.

add(n: int, msg: Union[str, List[T], Tuple, None] = None)[source]

Add n steps to the progress bar.

Parameters:
  • n (int) – number of steps to add to the progress bar
  • msg (Optional[Union[str, List, Tuple]], optional) – (name, value_for_last_step) or string messages, by default None
static format_num(n: int) → str[source]

Intelligent scientific notation (.3g).

Parameters:n (int or float or Numeric) – a Number.
Returns:out – Formatted number.
Return type:str
setup_logger[source]

Initialize the graphgallery logger and set its verbosity level to “DEBUG”.

Parameters:
  • output (Optional[str], optional) – a file name or a directory to save log. If None, will not save log file. If ends with “.txt” or “.log”, assumed to be a file name. Otherwise, logs will be saved to output/log.txt.
  • distributed_rank (int, optional) – used for distributed training, by default 0
  • mode (str, optional) – mode for the output file (if output is given), by default ‘w’.
  • color (bool, optional) – whether to use color when printing, by default True
  • name (str, optional) – the root module name of this logger, by default “graphgallery”
  • abbrev_name (Optional[str], optional) – an abbreviation of the module, to avoid long names in logs. Set to “” to not log the root module in logs. By default, None.
Returns:

a logger

Return type:

logging.Logger

Example

>>> logger = setup_logger(name='my exp')
>>> logger.info('message')
[12/19 17:01:43 my exp]: message
>>> logger.error('message')
ERROR [12/19 17:02:22 my exp]: message
>>> logger.warning('message')
WARNING [12/19 17:02:32 my exp]: message
>>> # specify output files
>>> logger = setup_logger(output='log.txt', name='my exp')
# additive, by default mode='w'
>>> logger = setup_logger(output='log.txt', name='my exp', mode='a')

# once you logger is set, you can call it by >>> logger = get_logger(name=’my exp’)

get_logger(name: str = 'GraphWar')[source]

Get a logger for a given name.

Parameters:name (str, optional) – name of the logger, by default “GraphWar”
Returns:
Return type:a logger for the given name
class BunchDict(*args, **kwargs)[source]

Container object for datasets Dictionary-like object that exposes its keys as attributes and remembers insertion order.

Examples

>>> b = BunchDict(a=1, b=2)
>>> b
Objects in BunchDict:
╒═════════╤═══════════╕
│ Names   │   Objects │
╞═════════╪═══════════╡
│ a       │         1 │
├─────────┼───────────┤
│ b       │         2 │
╘═════════╧═══════════╛
>>> b['b']
2
>>> b.b
2
>>> b.a = 3
>>> b['a']
3
>>> b.c = 6
>>> b['c']
6
>>> # Converting objects in BunchDict to `torch.Tensor` if possible.
>>> b = BunchDict(a=[1,2,3])
>>> b.to_tensor()
Objects in BunchDict:
╒═════════╤═══════════════════════════════╕
│ Names   │ Objects                       │
╞═════════╪═══════════════════════════════╡
│ a       │ Tensor, shape=torch.Size([3]) │
│         │ tensor([1, 2, 3])             │
╘═════════╧═══════════════════════════════╛
>>> b.a
tensor([1, 2, 3])
to_tensor(device: str = 'cpu', dtype=None) → graphgallery.utils.bunchdict.BunchDict[source]

Convert objects in BunchDict to torch.Tensor

Parameters:
  • device (str, optional) – device of the converted tensors, by default ‘cpu’
  • dtype (_type_, optional) – data types of the converted tensors, by default None
Returns:

Return type:

the converted BunchDict