-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
175 lines (139 loc) · 5.27 KB
/
utils.py
File metadata and controls
175 lines (139 loc) · 5.27 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
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# %% For external qt plot
def animate_scalar_qt(scalar,
pause_time,
colormap = 'magma',
c_range = 'global',
c_min = None, c_max = None):
assert c_range in {'global','local','custom'}, f'Invalid input: {c_range}. Must be one either \'global\',\'local\',\'custom\'.'
T = scalar.shape[2]
if c_range == 'global':
S_min = scalar.min()
S_max = scalar.max()
elif c_range == 'custom':
assert c_min is not None and c_max is not None, 'Colour limits must be defined'
S_min = c_min
S_max = c_max
fig, ax = plt.subplots()
img = ax.imshow(scalar[:, :, 0], cmap=colormap)
plt.axis('off') # Axis or no axis
for i in range(T):
S_tmp = scalar[:, :, i]
img.set_array(S_tmp)
# Set colour range
if c_range == 'custom':
S_min = S_tmp.min()
S_max = S_tmp.max()
img.set_clim(S_min, S_max)
fig.canvas.draw()
fig.canvas.flush_events()
plt.pause(pause_time)
# %% Inline viewing for Spyder
def animate_scalar_inline(scalar,
pause_time,
colormap = 'magma',
c_range = 'global',
c_min = None, c_max = None):
assert c_range in {'global','local','custom'}, f'Invalid input: {c_range}. Must be one either \'global\',\'local\',\'custom\'.'
T = scalar.shape[2]
for i in range(T):
S_tmp = scalar[:, :, i]
if c_range == 'global':
S_min = scalar.min()
S_max = scalar.max()
elif c_range == 'local':
S_min = S_tmp.min()
S_max = S_tmp.max()
elif c_range == 'custom':
assert c_min is not None and c_max is not None, 'Colour limits must be defined'
S_min = c_min
S_max = c_max
plt.imshow(S_tmp, cmap = colormap, vmin = S_min, vmax = S_max)
plt.colorbar()
plt.axis('off')
plt.show()
plt.pause(pause_time)
# %% Saving a gif
def save_gif(scalar, save_name = None, colormap = 'magma', c_range = 'global', c_min = None, c_max = None):
# [!] Add duration parameter
assert save_name is not None, 'Give a filename.'
assert c_range in {'global','local','custom'}, f'Invalid input: {c_range}. Must be one either \'global\',\'local\',\'custom\'.'
if c_min is None and c_max is None:
S_min = scalar.min()
S_max = scalar.max()
if c_range == 'global': # Min and max of full array
S_min = scalar.min()
S_max = scalar.max()
elif c_range == 'custom': # Custom colour limits
assert c_min is not None and c_max is not None, 'Colour limits must be defined'
S_min = c_min
S_max = c_max
print('Saving animation result...')
T = scalar.shape[2]
color_vals = plt.get_cmap(colormap)
frames = []
for i in range(T):
S_tmp = scalar[:, :, i]
if c_range == 'local': # Colour limits of each individual frame
S_min = S_tmp.min()
S_max = S_tmp.max()
S_norm = (S_tmp - S_min) / (S_max - S_min)
colored_field = color_vals(S_norm)
colored_field_rgb = (colored_field[:, :, :3] * 255).astype('uint8')
tmp_image = Image.fromarray(colored_field_rgb)
frames.append(tmp_image)
frames[0].save(f'{save_name}.gif', save_all=True, append_images=frames[1:], duration=20, loop=0)
print(f'Saved as: {save_name}.gif')
# %%
def plot_vec_field_on_image(X_in,Y_in,U_in,V_in,S_in,
DS = 1,
colormap = 'magma',
vec_color = 'black',
vec_scale = 1,
vec_alpha = 1,
min_S = None, max_S = None):
# Downsample
X_plot = X_in[::DS,::DS]
Y_plot = Y_in[::DS,::DS]
U_plot = U_in[::DS,::DS]
V_plot = V_in[::DS,::DS]
# Instantaneous min max limits if none specified
if min_S is None and max_S is None:
min_S = S_in.min()
max_S = S_in.max()
# Display
plt.figure(figsize=(6,6))
# Image
plt.imshow(S_in, cmap = colormap, vmin = min_S, vmax = max_S)
plt.colorbar(shrink=0.7)
# Vector field
plt.quiver(X_plot, Y_plot, U_plot, V_plot,
color = vec_color,
scale = vec_scale,
alpha = vec_alpha,
angles = 'xy')
plt.axis('off')
plt.show()
# %%
def plot_vec_field(X_in,Y_in,U_in,V_in,
DS = 1,
vec_color = 'black',
vec_scale = 1,
vec_alpha = 1):
# Downsample
X_plot = X_in[::DS,::DS]
Y_plot = Y_in[::DS,::DS]
U_plot = U_in[::DS,::DS]
V_plot = V_in[::DS,::DS]
# Display
plt.figure(figsize=(6,6))
# Vector field
plt.quiver(X_plot, Y_plot, U_plot, V_plot,
color = vec_color,
scale = vec_scale,
alpha = vec_alpha,
angles = 'xy')
plt.axis('off')
plt.show()