-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_plotting.py
More file actions
453 lines (378 loc) · 17.5 KB
/
Copy pathcompiler_plotting.py
File metadata and controls
453 lines (378 loc) · 17.5 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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sys
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.lines as mlines # For custom line legend entries
import matplotlib.patches as mpatches # For custom patch legend entries
axis_label_font_size = 20 # Font size for axis labels
axis_tick_font_size = 18 # Font size for tick labels (numbers on the axes)
legend_font_size = 14 # Font size for the legend
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams.update({
'font.size': axis_label_font_size, # General font size
'axes.titlesize': axis_label_font_size, # Title font size
'axes.labelsize': axis_label_font_size, # X and Y labels font size
'xtick.labelsize': axis_tick_font_size, # X-tick labels font size
'ytick.labelsize': axis_tick_font_size, # Y-tick labels font size
'legend.fontsize': legend_font_size, # Legend font size
'figure.titlesize': axis_label_font_size # Figure title font size
})
plt.ioff()
def get_color_shades(base_color, n_shades):
"""
Generate n accents (shades or tints) of a given base color.
Args:
base_color (str): A valid matplotlib color.
n_accents (int): The number of accents to generate.
Returns:
List of color accents.
"""
# Convert the base color to a normalized RGB tuple
base_rgb = np.array(mcolors.to_rgb(base_color))
# Create lighter shades by blending the color with white
accents = [mcolors.to_hex((1 - i/n_shades) * np.array(base_rgb) + (i/n_shades) * np.array([1, 1, 1])) for i in range(n_shades)]
return accents
def generate_color_palette(n_colors, n_shades):
"""
Generate a color palette with n distinct base colors, each having n_accents variations.
Args:
n_colors (int): Number of distinct colors to generate.
n_accents (int): Number of accent shades per color.
Returns:
List of colors with accents.
"""
# Use a colormap to generate distinct colors (like 'tab10', 'viridis', etc.)
if n_colors == 3:
cmap = ["red", "blue", "green"] # You can replace 'tab10' with any colormap
else:
cmap = ["red", "blue", "green","purple", "yellow"]
# Generate the palette of base colors and their accents
color_palette = []
for i in range(n_colors):
base_color = cmap[i] # Get the i-th color from the colormap
accents = get_color_shades(base_color, n_shades)
color_palette.append(accents)
return color_palette
height = 10
width = 8
dpi = 300
cycles = list(range(1,11))
sizes = [4,5,6,7]
errors = [0.01, 0.03, 0.05, 0.08, 0.1]# [0.01, 0.03, 0.05, 0.08, 0.1]
topologies = range(1, 4)
dataframes = {}
for c in cycles:
for a in topologies:
for s in sizes:
for e in errors:
try:
dataframes[f'c{c}_a{a}_s{s}_e{e}'] = pd.read_csv(f'./errors_meas_actualfix_crosstalk/output_c{c}_a{a}_s{s}_e{e}.csv') # cluster_error_0_1
except:
# assert False
dataframes[f'c{c}_a{a}_s{s}_e{e}'] = pd.DataFrame(None, index=range(8), columns=["vnes","tmi","shuttles"])
# fig = plt.subplots(figsize=(height, width))
# plots VNEs over cycle number
vne_cycles = []
tmi_cycles = []
for e in errors:
average_tmi_top = []
average_vne_top = []
for s in sizes:
for a in topologies:
average_vne_cycles = []
tmi_cycles_temp = []
vnes1_cycle = []
vnes2_cycle = []
vnes3_cycle = []
vnes4_cycle = []
vnes5_cycle = []
vnes6_cycle = []
vnes7_cycle = []
for c in cycles:
vne = dataframes[f'c{c}_a{a}_s{s}_e{e}']['vnes']
vnes1_cycle.append(vne[0])
vnes2_cycle.append(vne[1])
vnes3_cycle.append(vne[2])
vnes4_cycle.append(vne[3])
vnes5_cycle.append(vne[4])
vnes6_cycle.append(vne[5])
vnes7_cycle.append(vne[6])
average_vne_cycles.append(np.mean(vne))
tmi = dataframes[f'c{c}_a{a}_s{s}_e{e}']['tmi'][0]
tmi_cycles_temp.append(tmi)
fig, ax1 = plt.subplots(figsize=(height, width))
plt.plot(cycles, vnes1_cycle, label=r'$\mathcal{S}_{ABC}$')
plt.plot(cycles, vnes2_cycle, label=r'$\mathcal{S}_{A}$')
plt.plot(cycles, vnes3_cycle, label=r'$\mathcal{S}_{B}$')
plt.plot(cycles, vnes4_cycle, label=r'$\mathcal{S}_{C}$')
plt.plot(cycles, vnes5_cycle, label=r'$\mathcal{S}_{AB}$')
plt.plot(cycles, vnes6_cycle, label=r'$\mathcal{S}_{AC}$')
plt.plot(cycles, vnes7_cycle, label=r'$\mathcal{S}_{BC}$')
#
plt.title(f'VNE for Size {s} and CG {a}')
plt.xlabel('Cycle')
plt.ylabel(r'$\mathcal{S}(\rho)$')
plt.legend()
plt.grid()
plt.savefig(f'./plots/VNE/VNE_size{s}_top{a}.pdf',dpi=dpi)
vne_cycles.append(average_vne_cycles)
tmi_cycles.append(tmi_cycles_temp)
average_vne_top.append(np.mean(average_vne_cycles))
average_tmi_top.append(np.nanmean(tmi_cycles_temp))
# Top_1_vne = (average_vne_top[0]+average_vne_top[3]+average_vne_top[6]+average_vne_top[9])/4
# Top_2_vne = (average_vne_top[1]+average_vne_top[4]+average_vne_top[7]+average_vne_top[10])/4
# Top_3_vne = (average_vne_top[2]+average_vne_top[5]+average_vne_top[8]+average_vne_top[11])/4
Top_1_tmi = (average_tmi_top[0]+average_tmi_top[3]+average_tmi_top[6]+average_tmi_top[9])/4
Top_2_tmi = (average_tmi_top[1]+average_tmi_top[4]+average_tmi_top[7]+average_tmi_top[10])/4
Top_3_tmi = (average_tmi_top[2]+average_tmi_top[5]+average_tmi_top[8]+average_tmi_top[11])/4
# print("Average difference of VNE between Top 1 and 2 = " + str((1-Top_1_vne/Top_2_vne)*100))
# print("Average difference of VNE between Top 2 and 3 = " + str((1-Top_2_vne/Top_3_vne)*100))
# print("Average difference of VNE between Top 1 and 3 = " + str((1-Top_1_vne/Top_3_vne)*100))
# print()
# print(f"Average difference of TMI between Top 1 and 2 for error rate {e} = " + str((1-Top_1_tmi/Top_2_tmi)*100))
# print(f"Average difference of TMI between Top 2 and 3 for error rate {e} = " + str((1-Top_2_tmi/Top_3_tmi)*100))
# print(f"Average difference of TMI between Top 1 and 3 for error rate {e} = " + str((1-Top_1_tmi/Top_3_tmi)*100))
print()
print(f"Average TMI Connectivity 1 for error rate {e} = " + str(Top_1_tmi))
print(f"Average TMI Connectivity 2 for error rate {e} = " + str(Top_2_tmi))
print(f"Average TMI Connectivity 3 for error rate {e} = " + str(Top_3_tmi))
size_4_tmi = np.mean(average_tmi_top[0:2] )
size_5_tmi = np.mean(average_tmi_top[3:4])
size_6_tmi = np.mean(average_tmi_top[6:8])
size_7_tmi = np.mean(average_tmi_top[9:11])
print()
print(f"Average TMI size 4 for error rate {e} = " + str(size_4_tmi))
print(f"Average TMI size 5 for error rate {e} = " + str(size_5_tmi))
print(f"Average TMI size 6 for error rate {e} = " + str(size_6_tmi))
print(f"Average TMI size 7 for error rate {e} = " + str(size_7_tmi))
# plt.xlabel('Cycles')
# plt.ylabel('TMI')
# plt.legend()
# plt.grid()
# plt.savefig(f'/home/matthew13031990/Dropbox/PhD_Related/Projects/spin-AME/compilation_results/a{a}_s{s}_VNE.png',dpi=dpi)
# plt.savefig(f'/home/matthew13031990/Dropbox/PhD_Related/Projects/spin-AME/compilation_results/a{a}_s{s}_tmi.png',dpi=dpi)
# plt.close()
# vne_cycles = np.array(vne_cycles)
# tmi_cycles = np.array(tmi_cycles)
# dif_vne_1 = []
# dif_vne_2 = []
# dif_vne_3 = []
# dif_tmi_1 = []
# dif_tmi_2 = []
# dif_tmi_3 = []
# for index,s in enumerate([0,1,2,3]):
# dif_vne_1.append((1-vne_cycles[0+s]/vne_cycles[1+s])*100)
# dif_vne_2.append((1-vne_cycles[1+s]/vne_cycles[2+s])*100)
# dif_vne_3.append((1-vne_cycles[0+s]/vne_cycles[2+s])*100)
# dif_tmi_1.append((1-tmi_cycles[0+s]/tmi_cycles[1+s])*100)
# dif_tmi_2.append((1-tmi_cycles[1+s]/tmi_cycles[2+s])*100)
# dif_tmi_3.append((1-tmi_cycles[0+s]/tmi_cycles[2+s])*100)
# for index,s in enumerate(sizes):
# fig = plt.figure(figsize=(height, width))
# plt.plot(cycles, dif_vne_1[index], label='vne Δ(Top1vs2)', marker='s')
# plt.plot(cycles, dif_vne_2[index], label='vne Δ(Top2vs3)', marker='s')
# plt.plot(cycles, dif_vne_3[index], label='vne Δ(Top1vs3)', marker='s')
# plt.plot(cycles, dif_tmi_1[index], label='tmis Rate Δ(Top1vs2)', marker='o')
# plt.plot(cycles, dif_tmi_2[index], label='tmis Rate Δ(Top2vs3)', marker='o')
# plt.plot(cycles, dif_tmi_3[index], label='tmis Rate Δ(Top1vs3)', marker='o')
# plt.legend()
# plt.title('Size: ' + str(s))
# plt.xlabel('Cycles ')
# plt.ylabel('Δ [%]')
# fig.savefig(f'./plots/a{a}_s{s}.png',dpi=dpi)
n_accents = len(errors)
n_colors = len(topologies) # This can be any number depending on your variable
if n_colors == 1:
palette = [[None for _ in range(n_accents)] for _ in range(n_colors)]
else:
palette = generate_color_palette(n_colors, n_accents)
palette = list(map(list, zip(*palette)))
height = 10
width = 8
# Create the main figure and the first y-axis
fig, ax1 = plt.subplots(figsize=(height, width)) # Adjust height and width as needed
ax1.set_xlabel('Cycle')
ax1.set_ylabel(r'$\mathcal{I}_{3}$')
ax1.tick_params(axis='y')
# Create the second y-axis (on the right)
ax2 = ax1.twinx()
ax2.set_ylabel('Shuttles')
ax2.tick_params(axis='y')
# ax2.invert_yaxis()
# Loop through errors and topologies to plot the data
for e_ind, e in enumerate(errors):
tmi_per = []
shuttles_per = []
for a in topologies:
tmi_collection2 = []
shuttles_collection2 = []
for c in cycles:
tmi_collection1 = []
shuttles_collection1 = []
for s in sizes:
# Access data from dataframes
tmi_collection1.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['tmi'][0])
shuttles_collection1.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['shuttles'][0])
# Average per cycle
tmi_collection2.append(np.mean(tmi_collection1))
shuttles_collection2.append(np.mean(shuttles_collection1))
# Append averaged data
tmi_per.append(tmi_collection2)
shuttles_per.append(shuttles_collection2)
# Plot on both y-axes, matching colors between ax1 and ax2
for ind, tmis in enumerate(tmi_per):
# Plot on the first y-axis
if len(errors) > 1:
ax1.plot(cycles, tmis, color=palette[e_ind][ind])
# Plot on the second y-axis (match the color)
ax2.plot(cycles, shuttles_per[ind], color=palette[e_ind][ind],linestyle='--')
else:
ax1.plot(cycles, tmis, color=palette[e_ind][ind], label=f"TMI: CG {ind+1}")
# Plot on the second y-axis (match the color)
ax2.plot(cycles, shuttles_per[ind], color=palette[e_ind][ind],linestyle='--', label=f"Shuttles: CG {ind+1}")
# Set the title
plt.title("TMI and Shuttles Averaged Over Sizes")
# Collect the legend handles and labels from both axes
lines_1, labels_1 = ax1.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
# Create a combined legend
# ax1.legend(lines_1 + lines_2, labels_1 + labels_2, loc='best')
# Adjust layout to be tight
plt.tight_layout()
if len(errors) > 1:
line1 = mlines.Line2D([], [], color='red',markersize=10, label='TMI: CG 1')
line2 = mlines.Line2D([], [], color='blue',markersize=10, label='TMI: CG 2')
line3 = mlines.Line2D([], [], color='green', markersize=10, label='TMI: CG 3')
line4 = mlines.Line2D([], [], color='red',markersize=10, label='Shuttles: CG 1',linestyle='--' )
line5 = mlines.Line2D([], [], color='blue',markersize=10, label='Shuttles: CG 2',linestyle='--' )
line6 = mlines.Line2D([], [], color='green', markersize=10, label='Shuttles: CG 3',linestyle='--' )
# Define custom patch legend entries
# patch1 = mpatches.Patch(label='transparency')
custom_elements = [line1, line2, line3, line4, line5, line6]
ax1.legend(handles=custom_elements, loc='lower right')
else:
ax1.legend(lines_1 + lines_2 , labels_1 + labels_2 ,loc='lower right')
plt.xticks(cycles)
# Enable grid for both axes
ax1.grid(True) # Grid for the left y-axis (TMI)
ax2.grid(False) # Disable grid for the right y-axis (Shuttles)
# Save the figure
# plt.savefig('./plots/TMI/TMI_per_cycle.png',dpi=dpi)
plt.savefig('./plots/TMI/TMI_per_cycle.pdf',dpi=dpi)
# Show the plot (optional if you want to see it in an interactive window)
# plt.show()
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
fig, ax1 = plt.subplots(figsize=(height, width)) # Adjust height and width as needed
ax1.set_xlabel('Size')
ax1.set_ylabel('$\mathcal{I}_{3}$')
ax1.tick_params(axis='y')
# Create the second y-axis (on the right)
ax2 = ax1.twinx()
ax2.set_ylabel('Shuttles')
ax2.tick_params(axis='y')
# ax2.invert_yaxis() # Invert the right y-axis if required
# Loop through errors and topologies to plot the data
for e_ind, e in enumerate(errors):
tmi_per = []
shuttles_per = []
for a in topologies:
tmi_collection2 = []
shuttles_collection2 = []
for s in sizes:
tmi_collection1 = []
shuttles_collection1 = []
for c in cycles:
tmi_collection1.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['tmi'][0])
shuttles_collection1.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['shuttles'][0])
tmi_collection2.append(np.mean(tmi_collection1))
shuttles_collection2.append(np.mean(shuttles_collection1))
tmi_per.append(tmi_collection2)
shuttles_per.append(shuttles_collection2)
# Plot TMI per size on the left y-axis (ax1)
for ind, tmis in enumerate(tmi_per):
ax1.plot(sizes, tmis, color=palette[e_ind][ind], label=f"TMI: CG {ind+1}")
ax2.plot(sizes, shuttles_per[ind], color=palette[e_ind][ind], linestyle='--', label=f"Shuttles: CG {ind+1}")
# Plot Shuttles per size on the right y-axis (ax2)
# Set the title
plt.title("TMI and Shuttles Averaged Over Cycles")
# Collect the legend handles and labels from both axes
lines_1, labels_1 = ax1.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
# Create a combined legend
ax1.legend(lines_1 + lines_2 , labels_1 + labels_2 ,loc='lower right')
# Adjust layout to be tight
plt.tight_layout()
# Set x-ticks
plt.xticks(sizes)
# Enable grid for both axes
ax1.grid(True) # Grid for the left y-axis (TMI)
ax2.grid(False) # Disable grid for the right y-axis (Shuttles)
# Save the plot to a file
# plt.savefig('./plots/TMI/tmi_per_size.png',dpi=dpi)
plt.savefig('./plots/TMI/tmi_per_size.pdf',dpi=dpi)
# Show the plot (optional for interactive sessions)
# plt.show()
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# colors = ['b', '#FF7F0E', 'g'] # Red, Green, Blue for different 'a' values
fig = plt.figure(figsize=(height, width))
ax = fig.add_subplot(111, projection='3d')
for e_ind, e in enumerate(errors):
tmi_top = []
for a_ind, a in enumerate(topologies):
tmi_s = []
for s in sizes:
tmi_c = []
for c in cycles:
tmi_c.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['tmi'][0])
tmi_s.append(tmi_c)
tmi_top.append(tmi_s)
Z = np.array(tmi_top[a-1])
X, Y = np.meshgrid(cycles,sizes)
ax.plot_wireframe(X, Y, Z, color=palette[e_ind][a_ind], label=f"CG {a}" )
ax.set_title("3D Wireframe Plot with all the TMI data")
ax.set_xlabel('Cycle')
ax.set_ylabel('Size', labelpad= 10)
ax.set_zlabel('$\mathcal{I}_{3}$')
# Add a legend to the plot
ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1), borderaxespad=0.)
plt.tight_layout()
# plt.savefig('./plots/TMI/tmi_3d_per_error.png',dpi=dpi)
plt.savefig('./plots/TMI/tmi_3d_per_error.pdf',dpi=dpi)
# plt.tight_layout()
# plt.show()
n_accents = len(sizes)
n_colors = len(topologies) # This can be any number depending on your variable
if n_colors == 1:
palette = [[None for _ in range(n_accents)] for _ in range(n_colors)]
else:
palette = generate_color_palette(n_colors, n_accents)
palette = list(map(list, zip(*palette)))
fig = plt.figure(figsize=(height, width))
ax = fig.add_subplot(111, projection='3d')
for s_ind,s in enumerate(sizes):
tmi_top = []
for a_ind, a in enumerate(topologies):
tmi_s = []
for c in cycles:
tmi_c = []
for e_ind, e in enumerate(errors):
tmi_c.append(dataframes[f'c{c}_a{a}_s{s}_e{e}']['tmi'][0])
tmi_s.append(tmi_c)
tmi_top.append(tmi_s)
Z = np.array(tmi_top[a-1])
X, Y = np.meshgrid(errors,cycles)
ax.plot_wireframe(X, Y, Z,color=palette[s_ind][a_ind], label=f"Size {s}, CG {a}" )
ax.set_title("3D Wireframe Plot with all the TMI data")
ax.set_ylabel('Cycle', labelpad=15)
ax.set_xlabel('Error rates')
ax.set_zlabel('$\mathcal{I}_{3}$')
ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1), borderaxespad=0.)
# Option 2: Adjusting the plot area (tight layout may help)
plt.tight_layout()
# Add a legend to the plot
# plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
plt.tight_layout()
# plt.savefig('./plots/TMI/tmi_3d_per_sizes.png',dpi=dpi)
plt.savefig('./plots/TMI/tmi_3d_per_sizes.pdf',dpi=dpi)