-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
72 lines (62 loc) · 2.36 KB
/
Copy pathutils.py
File metadata and controls
72 lines (62 loc) · 2.36 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
import sys
import os
import json
import torch
import logging
from torch_geometric import seed_everything
def display_progress(text, current_step, last_step, enabled=True,
fix_zero_start=True):
"""Draws a progress indicator on the screen with the text preceeding the
progress
Arguments:
test: str, text displayed to describe the task being executed
current_step: int, current step of the iteration
last_step: int, last possible step of the iteration
enabled: bool, if false this function will not execute. This is
for running silently without stdout output.
fix_zero_start: bool, if true adds 1 to each current step so that the
display starts at 1 instead of 0, which it would for most loops
otherwise.
"""
if not enabled:
return
# Fix display for most loops which start with 0, otherwise looks weird
if fix_zero_start:
current_step = current_step + 1
term_line_len = 80
final_chars = [':', ';', ' ', '.', ',']
if text[-1:] not in final_chars:
text = text + ' '
if len(text) < term_line_len:
bar_len = term_line_len - (len(text)
+ len(str(current_step))
+ len(str(last_step))
+ len(" / "))
else:
bar_len = 30
filled_len = int(round(bar_len * current_step / float(last_step)))
bar = '=' * filled_len + '.' * (bar_len - filled_len)
bar = f"{text}[{bar:s}] {current_step:d} / {last_step:d}"
if current_step < last_step-1:
# Erase to end of line and print
sys.stdout.write("\033[K" + bar + "\r")
else:
sys.stdout.write(bar + "\n")
sys.stdout.flush()
def init_default_config(args):
seed_everything(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
log_format = '%(asctime)s: %(message)s'
logging.basicConfig(level=args.loglevel, format=log_format)
def save_json(filename, message):
if not os.path.isdir('output'):
os.mkdir('output')
data = []
logfile = f'output/{filename}.json'
if os.path.isfile(logfile):
with open(logfile) as json_file:
data = json.load(json_file)
data.append(message)
with open(logfile, 'w') as outfile:
json.dump(data, outfile)