-
Notifications
You must be signed in to change notification settings - Fork 0
/
timehelper.py
43 lines (37 loc) · 1.51 KB
/
timehelper.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
import time
import numpy as np
def print_time(start_time, T, t_max, episode, episode_rewards):
time_elapsed = time.time() - start_time
T = max(1, T)
time_left = time_elapsed * (t_max - T) / T
# Just in case its over 100 days
time_left = min(time_left, 60 * 60 * 24 * 100)
last_reward = "N\A"
if len(episode_rewards) > 5:
last_reward = "{:.2f}".format(np.mean(episode_rewards[-50:]))
print("\033[F\033[F\x1b[KEp: {:,}, T: {:,}/{:,}, Reward: {}, \n\x1b[KElapsed: {}, Left: {}\n".format(episode, T, t_max, last_reward, time_str(time_elapsed), time_str(time_left)), " " * 10, end="\r")
def time_left(start_time, t_start, t_current, t_max):
if t_current >= t_max:
return "-"
time_elapsed = time.time() - start_time
t_current = max(1, t_current)
time_left = time_elapsed * (t_max - t_current) / (t_current - t_start)
# Just in case its over 100 days
time_left = min(time_left, 60 * 60 * 24 * 100)
return time_str(time_left)
def time_str(s):
"""
Convert seconds to a nicer string showing days, hours, minutes and seconds
"""
days, remainder = divmod(s, 60 * 60 * 24)
hours, remainder = divmod(remainder, 60 * 60)
minutes, seconds = divmod(remainder, 60)
string = ""
if days > 0:
string += "{:d} days, ".format(int(days))
if hours > 0:
string += "{:d} hours, ".format(int(hours))
if minutes > 0:
string += "{:d} minutes, ".format(int(minutes))
string += "{:d} seconds".format(int(seconds))
return string