-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
458 lines (359 loc) · 16.6 KB
/
plot.py
File metadata and controls
458 lines (359 loc) · 16.6 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 05 10:25:52 2025
It implements the methods described in the paper
"Jitter Propagation in Task Chains".
Shumo Wang, Enrico Bini, Qingxu Deng, Martina Maggio,
IEEE Real-Time Systems Symposium (RTSS), 2025
@author: Shumo Wang
"""
import os
import matplotlib.pyplot as plt
import csv
import numpy as np
import argparse
import pandas as pd
from matplotlib.gridspec import GridSpec
def plot_R_histogram_our(csv_file,R_plot_name,tag='passive'):
"""
Read csv_file, retain only the data with per_jitter = 20%,
group by num_tasks, plot an R value histogram (50 bins evenly spaced from 0 to 1.05),
and calculate the proportion of R values greater than 1.
R = DFFbase/DFFbound in our paper.
arguments:
csv_file: The CSV file containing the data
R_plot_name: The name of the output plot file
tag: A tag to include in the plot title (default is 'passive')
return:
None
"""
num_tasks_to_r_values = {}
r_exceed_count = 0 # Counter for R values exceeding 1.0
total_rows = 0
TOLERANCE = 1e-9
with open(csv_file, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
total_rows += 1
per_jitter = float(row['per_jitter'])
r_value = float(row['R']) if row['R'] else None
num_tasks = int(row['num_tasks'])
if per_jitter == 0.2 and r_value is not None: # Only consider per_jitter = 20%
if num_tasks not in num_tasks_to_r_values:
num_tasks_to_r_values[num_tasks] = []
num_tasks_to_r_values[num_tasks].append(r_value)
if r_value > 1 + TOLERANCE:
print(f"Warning: R value {r_value} exceeds 1.0 for per_jitter={per_jitter}. This may indicate an error in the data.")
r_exceed_count += 1
if total_rows == 0:
print("No data found.")
return
R_exceed_percentage = r_exceed_count / total_rows * 100 if total_rows > 0 else 0
num_num_tasks = len(num_tasks_to_r_values)
if num_num_tasks == 0:
print("No valid data found for the specified conditions.")
return
num_columns = 2
num_rows = (num_num_tasks + num_columns - 1) // num_columns
fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows))
axes = axes.flatten()
colors = plt.cm.tab10(np.linspace(0, 1, num_num_tasks))
for idx, (num_tasks, r_values) in enumerate(num_tasks_to_r_values.items()):
ax = axes[idx]
num_bins = 50
bin_range = (0, 1.05)
bin_width = (bin_range[1] - bin_range[0]) / num_bins
counts, bin_edges = np.histogram(r_values, bins=num_bins, range=bin_range)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
ax.bar(bin_centers, counts, width=bin_width, alpha=0.7, align='center', color=colors[idx], label=f'num_tasks={num_tasks}')
r_values_greater_than_1 = len([r for r in r_values if r > 1])
percentage_greater_than_1 = (r_values_greater_than_1 / len(r_values)) * 100 if len(r_values) > 0 else 0
ax.set_title(f"num_tasks = {num_tasks} - our ({tag}) - Data Count: {len(r_values)}")
ax.set_xlabel(f"R_exceed_percentage = {percentage_greater_than_1:.2f}%")
ax.set_ylabel("Frequency")
ax.legend()
ax.grid(True)
for idx in range(num_num_tasks, num_rows * num_columns):
axes[idx].axis('off')
plt.tight_layout()
plt.suptitle(f"Distribution of R values for different num_tasks (per_jitter=20%),R_exceed_percentage={R_exceed_percentage}", fontsize=16, y=1.05)
plt.savefig(R_plot_name)
def plot_R_histogram_LET(csv_file,R_plot_name_LET,tag='passive'):
"""
No filtering per_jitter required
R = DFF_Gunzel_LET/DFFbound in our paper.
arguments:
csv_file: The CSV file containing the data
R_plot_name_LET: The name of the output plot file
tag: A tag to include in the plot title (default is 'passive')
return:
None
"""
num_tasks_to_r_values = {}
r_exceed_count = 0 # Counter for R values exceeding 1.0
total_rows = 0
TOLERANCE = 1e-9
with open(csv_file, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
total_rows += 1
r_value = float(row['R']) if row['R'] else None
num_tasks = int(row['num_tasks'])
if r_value is not None:
if num_tasks not in num_tasks_to_r_values:
num_tasks_to_r_values[num_tasks] = []
num_tasks_to_r_values[num_tasks].append(r_value)
if r_value > 1 + TOLERANCE:
print(f"Warning: R value {r_value} exceeds 1.0. This may indicate an error in the data.")
r_exceed_count += 1
if total_rows == 0:
print("No data found.")
return
R_exceed_percentage = r_exceed_count / total_rows * 100 if total_rows > 0 else 0
num_num_tasks = len(num_tasks_to_r_values)
if num_num_tasks == 0:
print("No valid data found for the specified conditions.")
return
num_columns = 2
num_rows = (num_num_tasks + num_columns - 1) // num_columns
fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows))
axes = axes.flatten()
colors = plt.cm.tab10(np.linspace(0, 1, num_num_tasks))
for idx, (num_tasks, r_values) in enumerate(num_tasks_to_r_values.items()):
ax = axes[idx]
num_bins = 50
bin_range = (0, 1.05)
bin_width = (bin_range[1] - bin_range[0]) / num_bins
counts, bin_edges = np.histogram(r_values, bins=num_bins, range=bin_range)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
ax.bar(bin_centers, counts, width=bin_width, alpha=0.7, align='center', color=colors[idx], label=f'num_tasks={num_tasks}')
r_values_greater_than_1 = len([r for r in r_values if r > 1])
percentage_greater_than_1 = (r_values_greater_than_1 / len(r_values)) * 100 if len(r_values) > 0 else 0
ax.set_title(f"num_tasks = {num_tasks} - LET ({tag}) - Data Count: {len(r_values)}")
ax.set_xlabel(f"R_exceed_percentage = {percentage_greater_than_1:.2f}%")
ax.set_ylabel("Frequency")
ax.legend()
ax.grid(True)
for idx in range(num_num_tasks, num_rows * num_columns):
axes[idx].axis('off')
plt.tight_layout()
plt.suptitle(f"Distribution of R values for different num_tasks (LET),R_exceed_percentage={R_exceed_percentage}", fontsize=16, y=1.05)
plt.savefig(R_plot_name_LET)
def plot_R_histogram_IC(csv_file,R_plot_name_IC,tag='passive'):
"""
No filtering per_jitter required
R = DFF_Gunzel_IC/DFFbound in our paper.
arguments:
csv_file: The CSV file containing the data
R_plot_name_IC: The name of the output plot file
tag: A tag to include in the plot title (default is 'passive')
return:
None
"""
num_tasks_to_r_values = {}
r_exceed_count = 0 # Counter for R values exceeding 1.0
total_rows = 0
TOLERANCE = 1e-9
with open(csv_file, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
total_rows += 1
r_value = float(row['R']) if row['R'] else None
num_tasks = int(row['num_tasks'])
if r_value is not None:
if num_tasks not in num_tasks_to_r_values:
num_tasks_to_r_values[num_tasks] = []
num_tasks_to_r_values[num_tasks].append(r_value)
if r_value > 1 + TOLERANCE:
print(f"Warning: R value {r_value} exceeds 1.0 . This may indicate an error in the data.")
r_exceed_count += 1
if total_rows == 0:
print("No data found.")
return
R_exceed_percentage = r_exceed_count / total_rows * 100 if total_rows > 0 else 0
num_num_tasks = len(num_tasks_to_r_values)
if num_num_tasks == 0:
print("No valid data found for the specified conditions.")
return
num_columns = 2
num_rows = (num_num_tasks + num_columns - 1) // num_columns
fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows))
axes = axes.flatten()
colors = plt.cm.tab10(np.linspace(0, 1, num_num_tasks))
for idx, (num_tasks, r_values) in enumerate(num_tasks_to_r_values.items()):
ax = axes[idx]
num_bins = 50
bin_range = (0, 1.05)
bin_width = (bin_range[1] - bin_range[0]) / num_bins
counts, bin_edges = np.histogram(r_values, bins=num_bins, range=bin_range)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
ax.bar(bin_centers, counts, width=bin_width, alpha=0.7, align='center', color=colors[idx], label=f'num_tasks={num_tasks}')
r_values_greater_than_1 = len([r for r in r_values if r > 1])
percentage_greater_than_1 = (r_values_greater_than_1 / len(r_values)) * 100 if len(r_values) > 0 else 0
ax.set_title(f"num_tasks = {num_tasks} - IC ({tag}) - Data Count: {len(r_values)}")
ax.set_xlabel(f"R_exceed_percentage = {percentage_greater_than_1:.2f}%")
ax.set_ylabel("Frequency")
ax.legend()
ax.grid(True)
for idx in range(num_num_tasks, num_rows * num_columns):
axes[idx].axis('off')
plt.tight_layout()
plt.suptitle(f"Distribution of R values for different num_tasks (IC),R_exceed_percentage={R_exceed_percentage}", fontsize=16, y=1.05)
plt.savefig(R_plot_name_IC)
def plot_runtime(csv_path, runtime_name, tag='passive'):
"""
Read csv, group by num_tasks, and calculate the average of run_time_G (Gunzel) and run_time_our (our).
arguments:
csv_path: The path to the CSV file containing the data
runtime_name: The name of the output plot file
tag: A tag to include in the plot title (default is 'passive')
return:
None
"""
df = pd.read_csv(csv_path)
avg = (df
.groupby('num_tasks')[['run_time_G', 'run_time_our']]
.mean()
.reset_index()
.sort_values('num_tasks'))
plt.figure(figsize=(6, 4))
plt.plot(avg['num_tasks'], avg['run_time_G'],
marker='o', label='run_time_G (Average)')
plt.plot(avg['num_tasks'], avg['run_time_our'],
marker='^', label='run_time_our (Average)')
plt.yscale('log')
plt.xlabel('num_tasks')
plt.ylabel('Average Runtime (s)')
plt.title(f' Average Runtime vs. num_tasks ({tag})')
plt.legend()
plt.grid(alpha=0.3)
plt.tight_layout()
plt.savefig(runtime_name, dpi=300)
def plot_false_percent(csv_file, percent_plot_name, tag='passive'):
"""
Group by num_tasks and plot a line with false_percentage varying with jitter.
arguments:
csv_file: The CSV file containing the data
percent_plot_name: The name of the output plot file
tag: A tag to include in the plot title (default is 'passive')
return:
None
"""
jitter_to_false_percentage = {}
with open(csv_file, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
per_jitter = float(row['per_jitter'])
false_percentage = float(row['false_percentage'])
num_tasks = int(row['num_tasks'])
if num_tasks not in jitter_to_false_percentage:
jitter_to_false_percentage[num_tasks] = {}
if per_jitter not in jitter_to_false_percentage[num_tasks]:
jitter_to_false_percentage[num_tasks][per_jitter] = []
jitter_to_false_percentage[num_tasks][per_jitter].append(false_percentage)
plt.figure(figsize=(10, 6))
for num_tasks, jitter_data in jitter_to_false_percentage.items():
jitter_percent = [jitter * 100 for jitter in sorted(jitter_data.keys())]
false_percentages = [np.mean(jitter_data[jitter]) * 100 for jitter in sorted(jitter_data.keys())]
plt.plot(jitter_percent, false_percentages, label=f"num_tasks={num_tasks}", marker='o')
plt.title(f"False Percentage vs. Jitter ({tag})")
plt.xlabel("Jitter Percentage (%)")
plt.ylabel("False Percentage (%)")
plt.legend()
plt.grid(True)
plt.xticks(jitter_percent)
plt.savefig(f"{percent_plot_name}")
def compare_plot_histogram_our(csv_files, compare_histogram_our_name):
"""
Comparing two experiments in our paper,
csv_files (passive and active),
each with per_jitter = 20% data, plotting histograms side by side by num_tasks.
arguments:
csv_files: List of CSV files to be compared
compare_histogram_our_name: The name of the output plot file
return:
None
"""
dfs = [pd.read_csv(file) for file in csv_files]
dfs = [df[df['per_jitter'] == 0.2] for df in dfs]
num_tasks_list = sorted(set.union(*[set(df['num_tasks'].unique()) for df in dfs]))
fig = plt.figure(figsize=(20, 10 * len(num_tasks_list)))
outer_grid = GridSpec(len(num_tasks_list), len(csv_files), wspace=0.4, hspace=0.4)
LABELS = ['passive', 'active']
colors = plt.cm.tab10(np.linspace(0, 1, len(num_tasks_list)))
TOLERANCE = 1e-9
for idx, num_tasks in enumerate(num_tasks_list):
for file_idx, df in enumerate(dfs):
ax = fig.add_subplot(outer_grid[idx, file_idx])
df_task = df[df['num_tasks'] == num_tasks]
r_values = df_task['R'].dropna().values
r_exceed_count = (r_values > (1 + TOLERANCE)).sum()
R_exceed_percentage = (r_exceed_count / len(r_values)) * 100 if len(r_values) > 0 else 0
num_bins = 50
bin_range = (0, 1.05)
bin_width = (bin_range[1] - bin_range[0]) / num_bins
counts, bin_edges = np.histogram(r_values, bins=num_bins, range=bin_range)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
ax.bar(bin_centers, counts, width=bin_width, alpha=0.7, align='center', color=colors[idx], label=f'num_tasks={num_tasks}')
label = LABELS[file_idx]
ax.set_title(f"num_tasks = {num_tasks} (per_jitter=20%) - our({label}) - Data Count: {len(r_values)}")
ax.set_xlabel("R_exceed_percentage = {:.2f}%".format(R_exceed_percentage))
ax.set_ylabel("Frequency")
ax.legend()
ax.grid(True)
plt.savefig(compare_histogram_our_name)
def compare_false_percent_our(csv_files, compare_plot_name):
"""
Comparing the two experiments in our paper,
csv_files (passive and active),
draw a line graph showing the False Percentage as Jitter changes.
arguments:
csv_files: List of CSV files to be compared
compare_plot_name: The name of the output plot file
return:
None
"""
num_csv_files = len(csv_files)
num_columns = 2
num_rows = (num_csv_files + num_columns - 1) // num_columns
fig, axes = plt.subplots(num_rows, num_columns, figsize=(15, 5 * num_rows))
axes = axes.flatten()
LABELS = ['passive', 'active']
for idx, csv_file in enumerate(csv_files):
ax = axes[idx]
try:
df = pd.read_csv(csv_file)
except FileNotFoundError:
print(f"File not found: {csv_file}")
continue
except pd.errors.EmptyDataError:
print(f"No data in CSV file: {csv_file}")
continue
label = LABELS[idx]
grouped_by_num_tasks = df.groupby('num_tasks')
for num_tasks, group in grouped_by_num_tasks:
group_sorted = group.sort_values(by='per_jitter')
per_jitters = group_sorted['per_jitter'] * 100
false_percentages = group_sorted['finalpercent']
ax.plot(per_jitters, false_percentages, label=f'num_tasks={num_tasks}', marker='o')
ax.set_title(f"False Percentage vs. Jitter ({label})")
ax.set_xlabel("Jitter Percentage (%)")
ax.set_ylabel("False Percentage (%)")
ax.legend()
ax.grid(True)
for idx in range(num_csv_files, num_rows * num_columns):
axes[idx].axis('off')
y_min = min(ax.get_ylim()[0] for ax in axes if ax.has_data())
y_max = max(ax.get_ylim()[1] for ax in axes if ax.has_data())
for ax in axes:
ax.set_ylim(y_min, y_max)
plt.tight_layout()
plt.savefig(compare_plot_name)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Plot histograms from a CSV file.")
# parser.add_argument("csv_file", type=str, help="Path to the CSV file containing the data.")
parser.add_argument("csv_files", type=str, nargs='+', help="Paths to the CSV files containing the data.")
parser.add_argument("name", type=str)
args = parser.parse_args()
compare_plot_histogram_our(args.csv_files, args.name)