-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores.py
74 lines (53 loc) · 2.19 KB
/
scores.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import argparse
import numpy as np
from typing import List
from stable_baselines.results_plotter import load_results, ts2xy
from utils import subdirs_list
scores_filename = "scores.csv"
def level_final_score(logs_dir: str) -> int:
_, score_values = ts2xy(load_results(logs_dir), "timesteps")
f_score = int(score_values[-1] * 100)
return f_score
def level_best_score(logs_dir: str) -> int:
_, score_values = ts2xy(load_results(logs_dir), "timesteps")
b_score = int(score_values.max() * 100)
return b_score
def level_score(logs_dir: str) -> float:
_, score_values = ts2xy(load_results(logs_dir), "timesteps")
mean_score = int(score_values.mean() * 100)
return mean_score
def mean_score(mean_scores: List[float]) -> float:
mean_score = int(np.array(mean_scores).mean())
return mean_score
def log_scores(logs_base_dir: str):
"""
Creates a scores.csv file inside the logs_base_dir folder
"""
dirs = subdirs_list(logs_base_dir)
scores = list(map(lambda d: level_score(os.path.join(logs_base_dir, d)), dirs))
final_scores = list(map(lambda d: level_final_score(os.path.join(logs_base_dir, d)), dirs))
best_scores = list(map(lambda d: level_best_score(os.path.join(logs_base_dir, d)), dirs))
dir_score_tuple = zip(dirs, scores, final_scores, best_scores)
score = mean_score(scores)
f_mean_score = mean_score(final_scores)
b_mean_score = mean_score(best_scores)
with open(os.path.join(logs_base_dir, scores_filename), "w") as f:
f.write("Level,Mean Score,Final Score,Best Score\n")
for (d, m_score, f_score, b_score) in dir_score_tuple:
f.write(f"{d},{m_score},{f_score},{b_score}\n")
f.write(f"Final score,{score},{f_mean_score},{b_mean_score}")
def main():
parser = argparse.ArgumentParser(description="Calculate the scores from the logs")
parser.add_argument(
"logs_path",
type=str,
help="The base folder which contains the level's log folders",
)
args = parser.parse_args()
log_scores(args.logs_path)
print(
f"\nCreated {scores_filename} ({os.path.join(args.logs_path, scores_filename)})"
)
if __name__ == "__main__":
main()