-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot_clock_period.py
142 lines (123 loc) · 4.01 KB
/
plot_clock_period.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import yaml
from collections import defaultdict
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
import os
import sys
import re
# Command-line arguments
if len(sys.argv) < 4:
raise ValueError("Usage: python script.py <output_pdf> <title> <input_files...>")
output_pdf = sys.argv[1]
output_yaml = sys.argv[2]
output_csv = sys.argv[3]
title = sys.argv[4]
input_files = sys.argv[5:]
# Load data from input files
file_data = {}
for file in input_files:
with open(file, "r") as f:
data = yaml.safe_load(f)
file_data[file] = data
# Group data by series
series_map = defaultdict(list)
for file in input_files:
name = data["name"]
match = re.match(r"^(.*?)(\d+)_[a-z]+_stats$", os.path.basename(file))
if not match:
raise ValueError(f"Invalid name format: {name}")
# Quick and dirty way to extract the series name and index
series = match.group(1).rstrip("_")
index = int(match.group(2))
series_map[series].append((index, file_data[file]))
with open(output_yaml, "w") as f:
yaml.dump({name: dict(points) for name, points in series_map.items()}, f)
# now in csv format with two levels of dicts
with open(output_csv, "w") as f:
for name, points in series_map.items():
for index, data in points:
f.write(
f"{name},{index},{data['power']},{data['clock_period']},{data['area']}\n"
)
# Sort and normalize the series by index
normalized_series = {}
normalized_series_x = {}
for name, points in series_map.items():
points.sort(key=lambda x: x[0]) # Sort by index
normalized_series_x[name] = list(map(lambda x: x[0], points))
normalized_series[name] = np.array([p[1] for p in points])
def plot_one(pdf_pages, data_key):
# Extract the specified series
series_names = sorted(
list(normalized_series.keys()),
reverse=True,
key=lambda name: max(map(lambda n: n[data_key], normalized_series[name])),
)
serieses = list(
map(
lambda series_name: np.array(
list(map(lambda item: item[data_key], normalized_series[series_name]))
),
series_names,
)
)
# Plot the results
plt.figure(figsize=(8, 5))
fig, ax1 = plt.subplots()
# Calculate the ratio (series1_value / series2_value)
if len(serieses) >= 2:
ratios = serieses[0] / serieses[1]
alus = normalized_series_x[series_names[0]]
# Plot the first series with the left y-axis
ax1.plot(
alus,
ratios,
marker="o",
label=f"Ratio ({series_names[0]}/{series_names[1]})",
color="b",
)
ax1.set_ylabel(f"Ratio", color="b")
ax1.tick_params(axis="y", labelcolor="b")
ax1.set_ylim(bottom=0)
# Set x-axis to display only integer values
ax1.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
ax2 = ax1.twinx()
for series, series_name in zip(serieses, series_names):
# sys.exit(1)
ax2.plot(
normalized_series_x[series_name],
series,
# marker="x",
label=series_name + " " + data_key,
# color="g",
)
# Set x-axis to display only integer values
ax2.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
ax2.set_ylabel(
data_key
+ "/"
+ {
"clock_period": "seconds",
"power": "W",
"instances": "instances",
"area": r"$\mathrm{\mu m}^2$",
}[data_key],
color="r",
)
ax2.set_ylim(bottom=0)
ax2.tick_params(axis="y", labelcolor="r")
fig.legend(loc="upper left", bbox_to_anchor=(0.1, 0.9))
plt.grid(True)
plt.title(f"{title} - {data_key}")
plt.tight_layout()
pdf_pages.savefig(fig)
plt.close(fig)
with PdfPages(output_pdf) as pdf_pages:
for data_key in [
"power",
"clock_period",
"area",
]:
plot_one(pdf_pages, data_key)