forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
192 lines (171 loc) · 5.21 KB
/
Copy pathlogger.py
File metadata and controls
192 lines (171 loc) · 5.21 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Managing the logger, utilities
Author
* Fang-Pen Lin 2012 https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
* Peter Plantinga 2020
* Aku Rouhe 2020
"""
import sys
import os
import yaml
import tqdm
import logging
import logging.config
import math
import torch
from speechbrain.utils.data_utils import recursive_update
from speechbrain.utils.superpowers import run_shell
ORDERS_ABBREV = {
-24: "y",
-21: "z",
-18: "a",
-15: "f",
-12: "p",
-9: "n",
-6: "µ",
-3: "m",
0: "",
3: "k",
6: "M",
9: "G",
12: "T",
15: "P",
18: "E",
21: "Z",
24: "Y",
}
# Short scale
# Negative powers of ten in lowercase, positive in uppercase
ORDERS_WORDS = {
-24: "septillionths",
-21: "sextillionths",
-18: "quintillionths",
-15: "quadrillionths",
-12: "trillionths",
-9: "billionths",
-6: "millionths",
-3: "thousandths",
0: "",
3: "Thousand",
6: "Million",
9: "Billion",
12: "Trillion",
15: "Quadrillion",
18: "Quintillion",
21: "Sextillion",
24: "Septillion",
}
class TqdmCompatibleStreamHandler(logging.StreamHandler):
"""TQDM compatible StreamHandler.
Writes and prints should be passed through tqdm.tqdm.write
so that the tqdm progressbar doesn't get messed up.
"""
def emit(self, record):
try:
msg = self.format(record)
stream = self.stream
tqdm.tqdm.write(msg, end=self.terminator, file=stream)
self.flush()
except RecursionError:
raise
except Exception:
self.handleError(record)
def setup_logging(
config_path="log-config.yaml", overrides={}, default_level=logging.INFO,
):
"""Setup logging configuration.
Arguments
---------
config_path : str
The path to a logging config file.
default_level : int
The level to use if the config file is not found.
overrides : dict
A dictionary of the same structure as the config dict
with any updated values that need to be applied.
"""
if os.path.exists(config_path):
with open(config_path, "rt") as f:
config = yaml.safe_load(f)
recursive_update(config, overrides)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
def format_order_of_magnitude(number, abbreviate=True):
"""Formats number to the appropriate order of magnitude for printing.
Arguments
---------
number : int, float
The number to format.
abbreviate : bool
Whether to use abbreviations (k,M,G) or words (Thousand, Million,
Billion). Numbers will be either like: "123.5k" or "123.5 Thousand".
Returns
-------
str
The formatted number. Note that the order of magnitude token is part
of the string.
Example
-------
>>> print(format_order_of_magnitude(123456))
123.5k
>>> print(format_order_of_magnitude(0.00000123, abbreviate=False))
1.2 millionths
>>> print(format_order_of_magnitude(5, abbreviate=False))
5
"""
style = ORDERS_ABBREV if abbreviate else ORDERS_WORDS
precision = "{num:3.1f}"
order = 3 * math.floor(math.log(math.fabs(number), 1000))
# Fallback for very large numbers:
while order not in style and order != 0:
order = order - math.copysign(3, order) # Bring 3 units towards 0
order_token = style[order]
if order != 0:
formatted_number = precision.format(num=number / 10 ** order)
else:
if isinstance(number, int):
formatted_number = str(number)
else:
formatted_number = precision.format(num=number)
if abbreviate or not order_token:
return formatted_number + order_token
else:
return formatted_number + " " + order_token
def get_environment_description():
"""Returns a string describing the current Python / SpeechBrain environment.
Useful for making experiments as replicable as possible.
Returns
-------
str
The string is formatted ready to be written to a file.
Example
-------
>>> get_environment_description().splitlines()[0]
'SpeechBrain system description'
"""
python_version_str = "Python version:\n" + sys.version + "\n"
try:
freezed, _, _ = run_shell("pip freeze")
python_packages_str = "Installed Python packages:\n"
python_packages_str += freezed.decode()
except OSError:
python_packages_str = "Could not list python packages with pip freeze"
try:
git_hash, _, _ = run_shell("git rev-parse --short HEAD")
git_str = "Git revision:\n" + git_hash.decode()
except OSError:
git_str = "Could not get git revision"
if torch.cuda.is_available():
cuda_str = "Cuda version:\n" + torch.version.cuda
else:
cuda_str = "CUDA not available"
result = "SpeechBrain system description\n"
result += "==============================\n"
result += python_version_str
result += "==============================\n"
result += python_packages_str
result += "==============================\n"
result += git_str
result += "==============================\n"
result += cuda_str
return result