-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting_helpers.py
More file actions
550 lines (467 loc) · 17.9 KB
/
Copy pathplotting_helpers.py
File metadata and controls
550 lines (467 loc) · 17.9 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from matplotlib.collections import LineCollection
from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
import matplotlib.patheffects as patheffects
def rolling_average(arr, window_size):
if window_size % 2 == 0:
raise ValueError("Window size must be odd.")
return np.convolve(
np.pad(arr, window_size // 2, mode='edge'),
np.ones(window_size) / window_size,
mode='valid'
)
def plot_fpr_tpr_warning_time(fprs, tprs, warning_times, title='', save_name=''):
"""
Plot TPR vs FPR with line segments colored by Warning Time,
and a black 'outline' around the colored line.
"""
# Prepare line segments
points = np.column_stack([fprs, tprs]).reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# 1) Create a thicker black LineCollection for the "outline"
lc_outline = LineCollection(
segments,
linewidth=6, # Outline thickness
color='black' # Solid black color
)
# 2) Create the color-mapped LineCollection (on top)
lc = LineCollection(
segments,
cmap=plt.cm.plasma,
norm=plt.Normalize(vmin=400, vmax=1500),
linewidth=4 # Slightly thinner
)
lc.set_array(warning_times) # Assign data for color mapping
# Plot
fig, ax = plt.subplots(figsize=(8,6))
ax.add_collection(lc_outline) # Add black outline first
ax.add_collection(lc) # Add color-mapped line second
# set font size for everything
plt.rcParams.update({'font.size': 16})
# Invisible line to set axis limits
ax.plot(fprs, tprs, alpha=0)
# Colorbar
cbar = plt.colorbar(lc, ax=ax)
cbar.set_label("Median Warning Time (ms)")
# Axis labels and limits
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
ax.set_xlim(fprs.min(), fprs.max())
ax.set_ylim(tprs.min(), tprs.max())
if save_name != "":
plt.savefig(f'plots/{save_name}.pdf', bbox_inches='tight')
plt.title(title)
plt.grid(True, linestyle="--", alpha=0.5)
plt.show()
def plot_two_roc_curves(
fprs1, tprs1, warning_times1,
fprs2, tprs2, warning_times2,
title='',
save_name='',
vmin=400, # Adjust based on your data
vmax=1500
):
"""
Plot two separate ROC curves (TPR vs FPR), each with line segments
colored by their respective Warning Time. Both curves have black outlines.
"""
# Prepare line segments for the first curve
points1 = np.column_stack([fprs1, tprs1]).reshape(-1, 1, 2)
segments1 = np.concatenate([points1[:-1], points1[1:]], axis=1)
# Outline
lc_outline1 = LineCollection(
segments1,
linewidth=6,
color='black'
)
# Color-mapped collection
lc1 = LineCollection(
segments1,
cmap=plt.cm.plasma,
norm=plt.Normalize(vmin=vmin, vmax=vmax),
linewidth=4
)
lc1.set_array(warning_times1)
# Prepare line segments for the second curve
points2 = np.column_stack([fprs2, tprs2]).reshape(-1, 1, 2)
segments2 = np.concatenate([points2[:-1], points2[1:]], axis=1)
lc_outline2 = LineCollection(
segments2,
linewidth=6,
color='black'
)
lc2 = LineCollection(
segments2,
cmap=plt.cm.plasma,
norm=plt.Normalize(vmin=vmin, vmax=vmax),
linewidth=4
)
lc2.set_array(warning_times2)
# Create the figure and axes
fig, ax = plt.subplots(figsize=(8, 6))
plt.rcParams.update({'font.size': 16})
# Add the black-outlined collections (first) and color collections (second)
ax.add_collection(lc_outline1)
ax.add_collection(lc1)
ax.add_collection(lc_outline2)
ax.add_collection(lc2)
# Force the axes to include all points by plotting invisible lines
ax.plot(fprs1, tprs1, alpha=0)
ax.plot(fprs2, tprs2, alpha=0)
# Create colorbar from one of the line collections (same norm/cmap)
cbar = plt.colorbar(lc2, ax=ax)
cbar.set_label("Median Warning Time (ms)")
# Axis labels
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
# Compute combined axis limits
all_fprs = np.concatenate([fprs1, fprs2])
all_tprs = np.concatenate([tprs1, tprs2])
ax.set_xlim(all_fprs.min(), all_fprs.max())
ax.set_ylim(all_tprs.min(), all_tprs.max())
# Title, grid, and optional save
plt.title(title)
plt.grid(True, linestyle="--", alpha=0.5)
if save_name:
plt.savefig(f'plots/{save_name}.pdf', bbox_inches='tight')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import LinearSegmentedColormap
def plot_heatmap_profile(x, profile, heatmap, profile_name=""):
"""
Plots Temperature vs Radial Position with line segments colored by Density.
Args:
x (array-like): Radial positions.
temperature (array-like): Temperature values corresponding to `x`.
density (array-like): Density values corresponding to `x`.
title (str): Title of the plot.
"""
# Define a bright red-to-blue colormap
bright_red_blue = LinearSegmentedColormap.from_list("BrightRdBu", ["blue", "white", "red"], N=256)
# Create segments for the line
points = np.array([x, profile]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# Create a LineCollection, with colors mapped to density
lc = LineCollection(segments, cmap=bright_red_blue, norm=plt.Normalize(vmin=min(heatmap), vmax=max(heatmap)))
lc = LineCollection(segments, cmap=bright_red_blue, norm=plt.Normalize(vmin=-0.01, vmax=0.01))
lc.set_array(heatmap) # Set density values to color the segments
# Create the plot
fig, ax = plt.subplots(figsize=(4, 2))
line = ax.add_collection(lc)
ax.plot(x, profile, alpha=0) # Add an invisible line for proper axis scaling
# Add a colorbar for density
colorbar = plt.colorbar(line, ax=ax)
colorbar.set_label('Tearing impact')
# Set axis limits and labels
ax.set_xlim(x.min(), x.max())
ax.set_ylim(profile.min(), profile.max())
ax.set_xlabel("$\mathbf{\psi_N}$")
ax.set_ylabel(profile_name)
#ax.set_title(title)
# Show the plot
plt.grid(True, linestyle='--', alpha=0.5)
plt.show()
def plot_2_profiles_heatmaps(
x, profile_index, profile_names, short_profile_names, profiles, heatmaps, time_indices,
title="", averaging_window_size=9, save_name=""
):
"""
Plot 2 profiles with associated 'heatmaps' (color-coded impact).
Parameters
----------
x : 1D array
The radial coordinate, shape = (33,) typically.
profile_index : int
Which profile to plot (e.g. 0 for 'Te', 1 for 'Ti', etc.).
profile_names : list
List of profile names (e.g., ['Te', 'Ti', 'ne', ...]).
profiles : 2D array
Shape = (n_times, 33 * n_profiles).
E.g., row i is the data for all profiles at time i.
heatmaps : 2D array
Same shape as `profiles`, containing "impact" values for color coding.
time_indices : tuple or list of length 2
The two time steps to plot, e.g. [t1, t2].
title : str, optional
Plot title.
averaging_window_size : int, optional
Size of the rolling average window (must be odd).
save_name : str, optional
If not empty, save figure to 'plots/{save_name}.svg'.
Returns
-------
None
"""
def rolling_average(arr, window_size):
if window_size % 2 == 0:
raise ValueError("Window size must be odd.")
return np.convolve(
np.pad(arr, window_size // 2, mode='edge'),
np.ones(window_size) / window_size,
mode='valid'
)
profile_name = profile_names[profile_index]
# Extract 2 profiles (no third one), smooth out profiles
profile1 = rolling_average(profiles[time_indices[0], 33*profile_index : 33*(profile_index+1)], 3)
profile2 = rolling_average(profiles[time_indices[1], 33*profile_index : 33*(profile_index+1)], 3)
# Compute rolling averages for corresponding heatmaps
heatmap1 = rolling_average(
heatmaps[time_indices[0], 33*profile_index : 33*(profile_index+1)],
averaging_window_size
)
heatmap2 = rolling_average(
heatmaps[time_indices[1], 33*profile_index : 33*(profile_index+1)],
averaging_window_size
)
# Define a bright red-to-blue colormap
bright_red_blue = LinearSegmentedColormap.from_list(
"BrightRdBu", ["blue", "white", "red"], N=256
)
# Combine both heatmaps to find a global color range or use a fixed one
all_heatmaps = np.concatenate([heatmap1, heatmap2])
# For example, a fixed range:
vmin, vmax = -0.0075, 0.0075
fig, ax = plt.subplots(figsize=(6, 4))
# Helper function to build a LineCollection for a given profile & heatmap
def make_linecollection(profile, heatmap):
# Create segments for the line
points = np.column_stack([x, profile]).reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(
segments,
cmap=bright_red_blue,
norm=plt.Normalize(vmin=vmin, vmax=vmax),
linewidth=3,
)
lc.set_array(heatmap)
lc.set_path_effects([
patheffects.Stroke(linewidth=5, foreground='black'),
patheffects.Normal()
])
return lc
# Create 2 line collections
lc1 = make_linecollection(profile1, heatmap1)
lc2 = make_linecollection(profile2, heatmap2)
# Add them to the same Axes for overplotting
for lc in [lc1, lc2]:
ax.add_collection(lc)
# If you'd like a legend for each line name, one simple approach is to
# plot invisible lines with labels:
ax.plot(
x, profile1, alpha=0,
label=(
rf'{short_profile_names[profile_index]} at $t_1$ is '
f'{"destabilizing" if np.sum(heatmap1) > 0 else "stabilizing"} '
f'by {np.abs(np.sum(heatmap1)):.2f}'
)
)
ax.plot(
x, profile2, alpha=0,
label=(
rf'{short_profile_names[profile_index]} at $t_2$ is '
f'{"destabilizing" if np.sum(heatmap2) > 0 else "stabilizing"} '
f'by {np.abs(np.sum(heatmap2)):.2f}'
)
)
# Create one colorbar for both lines
sm = cm.ScalarMappable(norm=plt.Normalize(vmin=vmin, vmax=vmax), cmap=bright_red_blue)
sm.set_array([])
cbar = plt.colorbar(sm, ax=ax)
cbar.set_label("TM impact", fontweight='bold')
# Set axis limits
ax.set_xlim(x.min(), x.max())
# Combine both profiles to set a global y-range if you wish
all_profiles = np.concatenate([profile1, profile2])
# ax.set_ylim(0, all_profiles.max()) # or any range you prefer
# Labels and title
ax.set_xlabel(r"$\mathbf{\psi_N}$")
ax.set_ylabel(f"{profile_name}", fontweight='bold')
ax.set_title(title, fontweight='bold')
plt.grid(True, linestyle="--", alpha=0.5)
# -------------------------
# Annotate each curve on the left side (instead of at the peak)
# -------------------------
profiles_list = [profile1, profile2]
time_names = ['t1', 't2']
x_min, x_max = ax.get_xlim()
y_min, y_max = ax.get_ylim()
x_range = x_max - x_min
y_range = y_max - y_min
# for i, prof in enumerate(profiles_list):
# # Place label near left edge
# x_pos = x_min + 0.05 * x_range # 5% in from left
# # Shift label above the start of the profile
# y_pos = prof[0] + 0.05 * y_range - 0.3
# ax.text(
# x_pos,
# y_pos,
# time_names[i],
# ha="left",
# va="bottom",
# fontsize=10,
# bbox=dict(boxstyle="round,pad=0.3", alpha=0.7, color="white")
# )
ax.legend(loc='upper right', fontsize=10)
# Optionally save the figure
if save_name != "":
plt.savefig(f'plots/{save_name}.pdf', bbox_inches='tight')
plt.show()
def plot_3_profiles_heatmaps(
x,
profile_index,
profile_names,
short_profile_names,
profiles,
heatmaps,
time_indices,
title="",
averaging_window_size=9,
save_name=""
):
"""
Plot 3 profiles with associated 'heatmaps' (color-coded impact).
Parameters
----------
x : 1D array
The radial coordinate, shape = (33,) typically.
profile_index : int
Which profile to plot (e.g. 0 for 'Te', 1 for 'Ti', etc.).
profile_names : list
List of profile names (e.g., ['Te', 'Ti', 'ne', ...]).
short_profile_names : list
Short version of profile names (for legend).
profiles : 2D array
Shape = (n_times, 33 * n_profiles).
E.g., row i is the data for all profiles at time i.
heatmaps : 2D array
Same shape as `profiles`, containing "impact" values for color coding.
time_indices : tuple or list of length 3
The three time steps to plot, e.g. [t1, t2, t3].
title : str, optional
Plot title.
averaging_window_size : int, optional
Size of the rolling average window (must be odd).
save_name : str, optional
If not empty, save figure to 'plots/{save_name}.pdf'.
Returns
-------
None
"""
def rolling_average(arr, window_size):
if window_size % 2 == 0:
raise ValueError("Window size must be odd.")
# Pad with edge values and then convolve
return np.convolve(
np.pad(arr, window_size // 2, mode='edge'),
np.ones(window_size) / window_size,
mode='valid'
)
profile_name = profile_names[profile_index]
# Extract 3 profiles for the chosen profile_index
p1 = rolling_average(profiles[time_indices[0], 33*profile_index : 33*(profile_index+1)], 5) # should be 3
p2 = rolling_average(profiles[time_indices[1], 33*profile_index : 33*(profile_index+1)], 5)
p3 = rolling_average(profiles[time_indices[2], 33*profile_index : 33*(profile_index+1)], 5)
# Compute rolling averages for the corresponding heatmaps
h1 = rolling_average(
heatmaps[time_indices[0], 33*profile_index : 33*(profile_index+1)],
averaging_window_size
)
h2 = rolling_average(
heatmaps[time_indices[1], 33*profile_index : 33*(profile_index+1)],
averaging_window_size
)
h3 = rolling_average(
heatmaps[time_indices[2], 33*profile_index : 33*(profile_index+1)],
averaging_window_size
)
# Define a bright red-to-blue colormap
bright_red_blue = LinearSegmentedColormap.from_list(
"BrightRdBu", ["blue", "white", "red"], N=256
)
# Combine all heatmaps to determine the color range (or fix them if you prefer)
all_heatmaps = np.concatenate([h1, h2, h3])
# Example: fixed range
vmin, vmax = -0.0075, 0.0075
fig, ax = plt.subplots(figsize=(6, 4))
# Helper function to build a LineCollection
def make_linecollection(profile, heatmap):
# Create segments for the line
points = np.column_stack([x, profile]).reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(
segments,
cmap=bright_red_blue,
norm=plt.Normalize(vmin=vmin, vmax=vmax),
linewidth=3,
)
lc.set_array(heatmap)
lc.set_path_effects([
patheffects.Stroke(linewidth=5, foreground='black'),
patheffects.Normal()
])
return lc
# Create 3 line collections
lc1 = make_linecollection(p1, h1)
lc2 = make_linecollection(p2, h2)
lc3 = make_linecollection(p3, h3)
# Add them to the same Axes for over-plotting
for lc in [lc1, lc2, lc3]:
ax.add_collection(lc)
# We create invisible lines for each profile to insert legend entries
# This also showcases the net "destabilizing or stabilizing" effect from the sum of the heatmap
sums = [np.sum(h1), np.sum(h2), np.sum(h3)]
p_list = [p1, p2, p3]
t_labels = [f"t{idx+1}" for idx in range(3)] # or any custom labels you prefer
t_labels = [r'$t_1$', r'$t_2$', r'$t_3$']
for i, (prof, s) in enumerate(zip(p_list, sums)):
ax.plot(
x, prof, alpha=0,
label=(
f'{short_profile_names[profile_index]} at {t_labels[i]} is '
f'{"destabilizing" if s > 0 else "stabilizing"} '
f'by {abs(s):.2f}'
)
)
# Create one colorbar for all lines
sm = cm.ScalarMappable(norm=plt.Normalize(vmin=vmin, vmax=vmax), cmap=bright_red_blue)
sm.set_array([])
ticks = np.linspace(vmin, vmax, 7)
cbar = plt.colorbar(sm, ax=ax, ticks=ticks)
cbar.set_label("TM impact", fontweight='bold', fontsize=8)
cbar.ax.tick_params(labelsize=8) # Set colorbar tick label size
# Set axis limits
ax.set_xlim(x.min(), x.max())
# Optionally set a global y-range
all_profiles = np.concatenate([p1, p2, p3])
# e.g., ax.set_ylim(0, all_profiles.max())
# Labels and title
ax.set_xlabel(r"$\mathbf{\psi_N}$")
ax.set_ylabel(f"{profile_name}", fontweight='bold')
ax.set_title(title, fontweight='bold')
plt.grid(True, linestyle="--", alpha=0.5)
# Annotate each curve on the left side
x_min, x_max = ax.get_xlim()
y_min, y_max = ax.get_ylim()
x_range = x_max - x_min
y_range = y_max - y_min
# for i, prof in enumerate(p_list):
# x_pos = x_min + 0.05 * x_range # 5% in from left
# # Shift label slightly from the starting value
# y_pos = prof[0] + 0.05 * y_range - 0.3 * i # offset each label a bit
# ax.text(
# x_pos,
# y_pos,
# t_labels[i],
# ha="left",
# va="bottom",
# fontsize=9,
# bbox=dict(boxstyle="round,pad=0.3", alpha=0.7, color="white")
# )
ax.legend(loc='upper right', fontsize=9)
# Optionally save the figure
if save_name != "":
plt.savefig(f'plots/{save_name}.pdf', bbox_inches='tight')
plt.show()