-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
61 lines (52 loc) · 1.88 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
@author: Sotiris
"""
import sys
import logging
import os
from typing import Union, TextIO
class LoggingConfig(object):
__logger = logging.getLogger(__name__)
root = None
LEVELS = {'debug': logging.DEBUG,
'info': logging.INFO,
'error': logging.ERROR,
'warning': logging.WARNING}
HANDLERS = {'stream': logging.StreamHandler,
'file': logging.FileHandler}
def __init__(self, level, fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handler_type='stream',
param: Union[str, TextIO, None] = sys.stdout):
assert level in LoggingConfig.LEVELS
assert handler_type in LoggingConfig.HANDLERS
self.level = LoggingConfig.LEVELS[level]
self.fmt = fmt
self.handler_type = handler_type
self.handler = LoggingConfig.HANDLERS[handler_type]
self.param = param
self.set_logger()
def set_logger(self):
self.root = logging.getLogger()
self.root.setLevel(self.level)
if self.handler_type == 'file':
self.create_dirs_if_needed(self.param)
handler = self.handler(self.param)
handler.setLevel(self.level)
formatter = logging.Formatter(self.fmt)
handler.setFormatter(formatter)
self.root.addHandler(handler)
return self.root
def get_logger(self):
return self.root
@staticmethod
def create_dirs_if_needed(filepath):
if '/' in filepath:
path = '/'.join(filepath.split('/')[:-1])
if not os.path.exists(path):
os.makedirs(path)
def transform_label(target, pos_label):
def transform_fn(a, v):
if target == 'arousal':
return int(a > 2) if pos_label == 'high' else int(a <= 2)
else:
return int(v > 2) if pos_label == 'high' else int(v <= 2)
return transform_fn