Spaces:
Runtime error
Runtime error
Upload utils/argutils.py with huggingface_hub
Browse files- utils/argutils.py +40 -0
utils/argutils.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
import numpy as np
|
3 |
+
import argparse
|
4 |
+
|
5 |
+
_type_priorities = [ # In decreasing order
|
6 |
+
Path,
|
7 |
+
str,
|
8 |
+
int,
|
9 |
+
float,
|
10 |
+
bool,
|
11 |
+
]
|
12 |
+
|
13 |
+
def _priority(o):
|
14 |
+
p = next((i for i, t in enumerate(_type_priorities) if type(o) is t), None)
|
15 |
+
if p is not None:
|
16 |
+
return p
|
17 |
+
p = next((i for i, t in enumerate(_type_priorities) if isinstance(o, t)), None)
|
18 |
+
if p is not None:
|
19 |
+
return p
|
20 |
+
return len(_type_priorities)
|
21 |
+
|
22 |
+
def print_args(args: argparse.Namespace, parser=None):
|
23 |
+
args = vars(args)
|
24 |
+
if parser is None:
|
25 |
+
priorities = list(map(_priority, args.values()))
|
26 |
+
else:
|
27 |
+
all_params = [a.dest for g in parser._action_groups for a in g._group_actions ]
|
28 |
+
priority = lambda p: all_params.index(p) if p in all_params else len(all_params)
|
29 |
+
priorities = list(map(priority, args.keys()))
|
30 |
+
|
31 |
+
pad = max(map(len, args.keys())) + 3
|
32 |
+
indices = np.lexsort((list(args.keys()), priorities))
|
33 |
+
items = list(args.items())
|
34 |
+
|
35 |
+
print("Arguments:")
|
36 |
+
for i in indices:
|
37 |
+
param, value = items[i]
|
38 |
+
print(" {0}:{1}{2}".format(param, ' ' * (pad - len(param)), value))
|
39 |
+
print("")
|
40 |
+
|