-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate_summary.py
83 lines (67 loc) · 2.42 KB
/
calculate_summary.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
75
76
77
78
79
80
81
82
import statistics
import sys
import os
import json
def main():
print("Loading parameters...")
name = sys.argv[1]
output_file = sys.argv[2]
results_folder = sys.argv[3]
filter = None if len(sys.argv) < 5 else sys.argv[4]
summary = {
"name": "NIF",
"type": "inr",
"results": {
"config": list(),
"state_bpp": list(),
"bpp": list(),
"psnr": list(),
"ms-ssim": list(),
"ssim": list(),
}
}
results = list()
for root_folder_name in os.listdir(results_folder):
try:
if filter and filter not in root_folder_name:
continue
root_folder = f"{results_folder}/{root_folder_name}"
stats = {
"bpp": list(),
"state_bpp": list(),
"psnr": list(),
"ms-ssim": list(),
"ssim": list(),
}
print(f"Loading stats files in {root_folder}...")
for root, dirs, files in os.walk(root_folder):
for file in files:
if file == name:
path = f"{root}/{file}"
file_stats = json.load(open(path, "r"))
for key in stats:
if key not in file_stats:
print(f"WARNING: No field {key} in {path}")
continue
stats[key].append(file_stats[key])
print("Calculating and dumping summary...")
mean_stats = dict()
for key in stats:
try:
mean_stats[key] = statistics.mean(stats[key])
except Exception as ex_stats:
print(f"Couldn't calculate mean for stat {key}: {ex_stats}")
mean_stats["config"] = root_folder_name
if "psnr" not in mean_stats:
print(f"WARNING: Broken config {root_folder_name}")
continue
results.append(mean_stats)
except Exception as e:
print(f"WARNING: Could not load {root_folder_name}: {e}")
results = sorted(results, key = lambda r: r["psnr"])
for result in results:
for key in result:
summary["results"][key].append(result[key])
json.dump(summary, open(output_file, "w"), indent=4)
if __name__ == "__main__":
main()