pyEdgeEval.utils package

Submodules

pyEdgeEval.utils.convert_formats module

pyEdgeEval.utils.convert_formats.mask2onehot(mask, labels)[source]

Converts a segmentation mask (H,W) to (K,H,W) where the last dim is a one hot encoding vector

pyEdgeEval.utils.convert_formats.edge_multilabel2binary(edges: ndarray) ndarray[source]

Converts multilabel edge to binary edge data (collapse multi-label)

pyEdgeEval.utils.convert_formats.edge_onehot2multilabel(edges: ndarray) ndarray[source]

Converts multilabel edges to encoded single channel edge data while preserving multi-label

pyEdgeEval.utils.convert_formats.mask_label2trainId(mask: ndarray, label2trainId: dict) ndarray[source]

Python version of labelid2trainid function for segmentation data

Parameters

mask – single channel image containing segmentation label

Returns

np.ndarray

pyEdgeEval.utils.convert_formats.edge_label2trainId(edge: ndarray, label2trainId: dict) ndarray[source]

pyEdgeEval.utils.distance_transforms module

pyEdgeEval.utils.distance_transforms.mask2bdry(mask: ndarray, ignore_mask: ndarray, radius: int, use_cv2: bool = True, quality: int = 0) ndarray[source]

pyEdgeEval.utils.logger module

Modified from mmcv.utils

  • logger name

pyEdgeEval.utils.logger.get_logger(name, log_file=None, log_level=20, file_mode='w')[source]

Initialize and get a logger by name.

If the logger has not been initialized, this method will initialize the logger by adding one or two handlers, otherwise the initialized logger will be directly returned. During initialization, a StreamHandler will always be added. If log_file is specified and the process rank is 0, a FileHandler will also be added.

Parameters
  • name (str) – Logger name.

  • log_file (str | None) – The log filename. If specified, a FileHandler will be added to the logger.

  • log_level (int) – The logger level. Note that only the process of rank 0 is affected, and other processes will set the level to “Error” thus be silent most of the time.

  • file_mode (str) – The file mode used in opening log file. Defaults to ‘w’.

Returns

logging.Logger – The expected logger.

pyEdgeEval.utils.logger.print_log(msg, logger=None, level=20)[source]

Print a log message.

Parameters
  • msg (str) – The message to be logged.

  • logger (logging.Logger | str | None) – The logger to be used. Some special loggers are: - “silent”: no message will be printed. - other str: the logger obtained with get_root_logger(logger). - None: The print() method will be used to print log messages.

  • level (int) – Logging level. Only available when logger is a Logger object or “root”.

pyEdgeEval.utils.logger.get_root_logger(log_file=None, log_level=20)[source]

Get the root logger.

The logger will be initialized if it has not been initialized. By default a StreamHandler will be added. If log_file is specified, a FileHandler will also be added. The name of the root logger is the top-level package name, e.g., “potato”.

Parameters
  • log_file (str | None) – The log filename. If specified, a FileHandler will be added to the root logger.

  • log_level (int) – The root logger level. Note that only the process of rank 0 is affected, while other processes will set the level to “Error” and be silent most of the time.

Returns

logging.Logger – The root logger.

pyEdgeEval.utils.mat_utils module

pyEdgeEval.utils.mat_utils.loadmat(path: str, use_mat73: bool = False)[source]
pyEdgeEval.utils.mat_utils.sparse2numpy(data: csc_matrix)[source]

helper function to convert compressed sparse column matrix to numpy array

pyEdgeEval.utils.path module

Modified from mmcv.utils

  • no changes

pyEdgeEval.utils.path.check_file_exist(filename, msg_tmpl='file "{}" does not exist')[source]
pyEdgeEval.utils.path.mkdir_or_exist(dir_name, mode=511)[source]
pyEdgeEval.utils.path.scandir(dir_path, suffix=None, recursive=False, case_sensitive=True)[source]

Scan a directory to find the interested files.

Parameters
  • dir_path (str | Path) – Path of the directory.

  • suffix (str | tuple(str), optional) – File suffix that we are interested in. Default: None.

  • recursive (bool, optional) – If set to True, recursively scan the directory. Default: False.

  • case_sensitive (bool, optional) – If set to False, ignore the case of suffix. Default: True.

Returns

A generator for all the interested files with relative paths.

pyEdgeEval.utils.progressbar module

Modified from mmcv.utils

  • added no_bar mode

class pyEdgeEval.utils.progressbar.ProgressBar(task_num=0, bar_width=50, start=True, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Bases: object

A progress bar which can print the progress.

property terminal_width
start()[source]
update(num_tasks=1)[source]
pyEdgeEval.utils.progressbar.track_progress(func, tasks, bar_width=50, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, no_bar=False, **kwargs)[source]

Track the progress of tasks execution with a progress bar.

Tasks are done with a simple for-loop.

Parameters
  • func (callable) – The function to be applied to each task.

  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • bar_width (int) – Width of progress bar.

Returns

list – The task results.

pyEdgeEval.utils.progressbar.init_pool(process_num, initializer=None, initargs=None)[source]
pyEdgeEval.utils.progressbar.track_parallel_progress(func, tasks, nproc, initializer=None, initargs=None, bar_width=50, chunksize=1, skip_first=False, keep_order=True, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, no_bar=False)[source]

Track the progress of parallel task execution with a progress bar.

The built-in multiprocessing module is used for process pools and tasks are done with Pool.map() or Pool.imap_unordered().

Parameters
  • func (callable) – The function to be applied to each task.

  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • nproc (int) – Process (worker) number.

  • initializer (None or callable) – Refer to multiprocessing.Pool for details.

  • initargs (None or tuple) – Refer to multiprocessing.Pool for details.

  • chunksize (int) – Refer to multiprocessing.Pool for details.

  • bar_width (int) – Width of progress bar.

  • skip_first (bool) – Whether to skip the first sample for each worker when estimating fps, since the initialization step may takes longer.

  • keep_order (bool) – If True, Pool.imap() is used, otherwise Pool.imap_unordered() is used.

Returns

list – The task results.

pyEdgeEval.utils.progressbar.track_iter_progress(tasks, bar_width=50, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Track the progress of tasks iteration or enumeration with a progress bar.

Tasks are yielded with a simple for-loop.

Parameters
  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • bar_width (int) – Width of progress bar.

Yields

list – The task results.

pyEdgeEval.utils.timer module

Modified from mmcv.utils

  • no changes

exception pyEdgeEval.utils.timer.TimerError(message)[source]

Bases: Exception

class pyEdgeEval.utils.timer.Timer(start=True, print_tmpl=None)[source]

Bases: object

A flexible Timer class.

Examples

>>> import time
>>> from .timer import Timer
>>> with Timer():
>>>     # simulate a code block that will run for 1s
>>>     time.sleep(1)
1.000
>>> with Timer(print_tmpl='it takes {:.1f} seconds'):
>>>     # simulate a code block that will run for 1s
>>>     time.sleep(1)
it takes 1.0 seconds
>>> timer = Timer()
>>> time.sleep(0.5)
>>> print(timer.since_start())
0.500
>>> time.sleep(0.5)
>>> print(timer.since_last_check())
0.500
>>> print(timer.since_start())
1.000
property is_running

indicate whether the timer is running

Type

bool

start()[source]

Start the timer.

since_start()[source]

Total time since the timer is started.

Returns

float – Time in seconds.

since_last_check()[source]

Time since the last checking.

Either since_start() or since_last_check() is a checking operation.

Returns

float – Time in seconds.

pyEdgeEval.utils.timer.check_time(timer_id)[source]

Add check points in a single line. This method is suitable for running a task on a list of items. A timer will be registered when the method is called for the first time.

Examples

>>> import time
>>> from .timer import check_time
>>> for i in range(1, 6):
>>>     # simulate a code block
>>>     time.sleep(i)
>>>     check_time('task1')
2.000
3.000
4.000
5.000
Parameters

str – Timer identifier.

Module contents

pyEdgeEval.utils.mask2onehot(mask, labels)[source]

Converts a segmentation mask (H,W) to (K,H,W) where the last dim is a one hot encoding vector

pyEdgeEval.utils.edge_multilabel2binary(edges: ndarray) ndarray[source]

Converts multilabel edge to binary edge data (collapse multi-label)

pyEdgeEval.utils.edge_onehot2multilabel(edges: ndarray) ndarray[source]

Converts multilabel edges to encoded single channel edge data while preserving multi-label

pyEdgeEval.utils.mask_label2trainId(mask: ndarray, label2trainId: dict) ndarray[source]

Python version of labelid2trainid function for segmentation data

Parameters

mask – single channel image containing segmentation label

Returns

np.ndarray

pyEdgeEval.utils.edge_label2trainId(edge: ndarray, label2trainId: dict) ndarray[source]
pyEdgeEval.utils.mask2bdry(mask: ndarray, ignore_mask: ndarray, radius: int, use_cv2: bool = True, quality: int = 0) ndarray[source]
pyEdgeEval.utils.get_logger(name, log_file=None, log_level=20, file_mode='w')[source]

Initialize and get a logger by name.

If the logger has not been initialized, this method will initialize the logger by adding one or two handlers, otherwise the initialized logger will be directly returned. During initialization, a StreamHandler will always be added. If log_file is specified and the process rank is 0, a FileHandler will also be added.

Parameters
  • name (str) – Logger name.

  • log_file (str | None) – The log filename. If specified, a FileHandler will be added to the logger.

  • log_level (int) – The logger level. Note that only the process of rank 0 is affected, and other processes will set the level to “Error” thus be silent most of the time.

  • file_mode (str) – The file mode used in opening log file. Defaults to ‘w’.

Returns

logging.Logger – The expected logger.

pyEdgeEval.utils.get_root_logger(log_file=None, log_level=20)[source]

Get the root logger.

The logger will be initialized if it has not been initialized. By default a StreamHandler will be added. If log_file is specified, a FileHandler will also be added. The name of the root logger is the top-level package name, e.g., “potato”.

Parameters
  • log_file (str | None) – The log filename. If specified, a FileHandler will be added to the root logger.

  • log_level (int) – The root logger level. Note that only the process of rank 0 is affected, while other processes will set the level to “Error” and be silent most of the time.

Returns

logging.Logger – The root logger.

pyEdgeEval.utils.print_log(msg, logger=None, level=20)[source]

Print a log message.

Parameters
  • msg (str) – The message to be logged.

  • logger (logging.Logger | str | None) – The logger to be used. Some special loggers are: - “silent”: no message will be printed. - other str: the logger obtained with get_root_logger(logger). - None: The print() method will be used to print log messages.

  • level (int) – Logging level. Only available when logger is a Logger object or “root”.

pyEdgeEval.utils.loadmat(path: str, use_mat73: bool = False)[source]
pyEdgeEval.utils.sparse2numpy(data: csc_matrix)[source]

helper function to convert compressed sparse column matrix to numpy array

pyEdgeEval.utils.check_file_exist(filename, msg_tmpl='file "{}" does not exist')[source]
pyEdgeEval.utils.mkdir_or_exist(dir_name, mode=511)[source]
pyEdgeEval.utils.scandir(dir_path, suffix=None, recursive=False, case_sensitive=True)[source]

Scan a directory to find the interested files.

Parameters
  • dir_path (str | Path) – Path of the directory.

  • suffix (str | tuple(str), optional) – File suffix that we are interested in. Default: None.

  • recursive (bool, optional) – If set to True, recursively scan the directory. Default: False.

  • case_sensitive (bool, optional) – If set to False, ignore the case of suffix. Default: True.

Returns

A generator for all the interested files with relative paths.

pyEdgeEval.utils.track_iter_progress(tasks, bar_width=50, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Track the progress of tasks iteration or enumeration with a progress bar.

Tasks are yielded with a simple for-loop.

Parameters
  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • bar_width (int) – Width of progress bar.

Yields

list – The task results.

pyEdgeEval.utils.track_parallel_progress(func, tasks, nproc, initializer=None, initargs=None, bar_width=50, chunksize=1, skip_first=False, keep_order=True, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, no_bar=False)[source]

Track the progress of parallel task execution with a progress bar.

The built-in multiprocessing module is used for process pools and tasks are done with Pool.map() or Pool.imap_unordered().

Parameters
  • func (callable) – The function to be applied to each task.

  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • nproc (int) – Process (worker) number.

  • initializer (None or callable) – Refer to multiprocessing.Pool for details.

  • initargs (None or tuple) – Refer to multiprocessing.Pool for details.

  • chunksize (int) – Refer to multiprocessing.Pool for details.

  • bar_width (int) – Width of progress bar.

  • skip_first (bool) – Whether to skip the first sample for each worker when estimating fps, since the initialization step may takes longer.

  • keep_order (bool) – If True, Pool.imap() is used, otherwise Pool.imap_unordered() is used.

Returns

list – The task results.

pyEdgeEval.utils.track_progress(func, tasks, bar_width=50, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, no_bar=False, **kwargs)[source]

Track the progress of tasks execution with a progress bar.

Tasks are done with a simple for-loop.

Parameters
  • func (callable) – The function to be applied to each task.

  • tasks (list or tuple[Iterable, int]) – A list of tasks or (tasks, total num).

  • bar_width (int) – Width of progress bar.

Returns

list – The task results.