Skip to content

Commit

Permalink
Added argument parsing elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-costas committed Mar 9, 2020
1 parent 3928028 commit 01031ce
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions tools/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
MutableSequence, MutableMapping, List, \
Dict, Type, Union
from argparse import ArgumentParser
from sys import stdout

from torch import Tensor
from torch.nn import Module
from loguru import logger

__author__ = 'Konstantinos Drossos -- Tampere University'
__docformat__ = 'reStructuredText'
__all__ = ['CheckAllNone', 'get_argument_parser', 'apply_layer']
__all__ = ['CheckAllNone', 'get_argument_parser',
'apply_layer', 'init_loggers']


T = TypeVar('T')
Expand Down Expand Up @@ -54,20 +57,32 @@ def get_argument_parser() \
"""
the_args: List[List[Union[List[str], Dict[str, Union[Type, str]]]]] = [
# ----------------------------------------
[['--config-file', '-c'], # 1st argument
[['--model', '-m'], # 1st argument
{'type': str,
'help': 'The name (without extension) '
'of the YAML file with the settings.'}],
'help': 'The name of the model to use. '
'Accepted values are: `baseline`, '
'`baseline_dilated`, `dessed`, '
'`dessed_dilated`'}],
# ----------------------------------------
[['--model', '-m'], # 2nd argument
[['--config-file', '-c'], # 1st argument
{'type': str,
'help': 'The name (without extension) '
'of the YAML file with the settings.'}],
# ----------------------------------------
[['--job-id', '-j'], # 3rd argument
# ---------------------------------
[['--file-dir', '-d'], # 2nd argument
{'type': str,
'default': 'settings',
'help': 'Directory that holds the settings file (default: `settings`).'}],
# ---------------------------------
[['--file-ext', '-e'], # 3rd argument
{'type': str,
'default': str(0),
'help': 'The current job id on SLURM.'}]]
'default': '.yaml',
'help': 'Extension of the settings file (default: `.yaml`).'}],
# ----------------------------------------
[['--verbose', '-v'], # 4th argument
{'default': True,
'action': 'store_true',
'help': 'Be verbose flag (default True).'}]]

arg_parser = ArgumentParser()
[arg_parser.add_argument(*i[0], **i[1]) for i in the_args]
Expand All @@ -89,4 +104,30 @@ def apply_layer(layer_input: Tensor,
"""
return layer(layer_input)


def init_loggers(verbose: bool) \
-> None:
"""Initializes the logging process.
:param verbose: Be verbose?
:type verbose: bool
"""
logger.remove()

for indent in range(3):
log_string = '{level} | [{time}] {name} -- {space}{message}'.format(
level='{level}',
time='{time:HH:mm:ss}',
name='{name}',
message='{message}',
space=' ' * (indent*2))
logger.add(
stdout,
format=log_string,
level='INFO',
filter=lambda record, i=indent: record['extra']['indent'] == i)

if not verbose:
logger.disable()

# EOF

0 comments on commit 01031ce

Please sign in to comment.