forked from DIG-Kaust/Seismology-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.py
174 lines (155 loc) · 6.12 KB
/
visual.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
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
# Credits : https://github.com/polimi-ispl/deep_prior_interpolation/blob/master/utils/plotting.py
import numpy as np
import matplotlib.pyplot as plt
def clim(in_content, ratio=95):
"""Clipping based on percentiles
"""
c = np.percentile(np.absolute(in_content), ratio)
return -c, c
def explode_volume(volume, t=None, x=None, y=None,
figsize=(8, 8), cmap='bone', clipval=None, p=98,
tlim=None, xlim=None, ylim=None,
tcrop=None, xcrop=None, ycrop=None, vmin = None, vmax = None,
labels=('[s]', '[km]', '[km]'),
tlabel='t', xlabel='x', ylabel='y',
ratio=None, linespec=None, interp=None, title='',
filename=None, save_opts=None):
"""Display 3D volume
Display 3D volume in exploding format (three slices)
Credits : https://github.com/polimi-ispl/deep_prior_interpolation/blob/master/utils/plotting.py
Parameters
----------
volume : :obj:`numpy.ndarray`
3D volume of size ``(nt, nx, ny)``
t : :obj:`int`, optional
Slicing index along time axis
x : :obj:`int`, optional
Slicing index along x axis
y : :obj:`int`, optional
Slicing index along y axis
figsize : :obj:`bool`, optional
Figure size
cmap : :obj:`str`, optional
Colormap
clipval : :obj:`tuple`, optional
Clipping min and max values
p : :obj:`str`, optional
Percentile of max value (to be used if ``clipval=None``)
tlim : :obj:`tuple`, optional
Limits of time axis in volume
xlim : :obj:`tuple`, optional
Limits of x axis in volume
ylim : :obj:`tuple`, optional
Limits of y axis in volume
tlim : :obj:`tuple`, optional
Limits of cropped time axis to be visualized
xlim : :obj:`tuple`, optional
Limits of cropped x axis to be visualized
ylim : :obj:`tuple`, optional
Limits of cropped y axis to be visualized
labels : :obj:`bool`, optional
Labels to add to axes as suffixes
tlabels : :obj:`bool`, optional
Label to use for time axis
xlabels : :obj:`bool`, optional
Label to use for x axis
ylabels : :obj:`bool`, optional
Label to use for y axis
ratio : :obj:`float`, optional
Figure aspect ratio (if ``None``, inferred from the volume sizes directly)
linespec : :obj:`dict`, optional
Specifications for lines indicating the selected slices
interp : :obj:`str`, optional
Interpolation to apply to visualization
title : :obj:`str`, optional
Figure title
filename : :obj:`str`, optional
Figure full path (if provided the figure is saved at this path)
save_opts : :obj:`dict`, optional
Additional parameters to be provided to :func:`matplotlib.pyplot.savefig`
Returns
-------
fig : :obj:`matplotlib.pyplot.Figure`
Figure handle
axs : :obj:`matplotlib.pyplot.Axis`
Axes handes
"""
if linespec is None:
linespec = dict(ls='-', lw=1.5, color='#0DF690')
nt, nx, ny = volume.shape
t_label, x_label, y_label = labels
t = t if t is not None else nt // 2
x = x if x is not None else nx // 2
y = y if y is not None else ny // 2
if tlim is None:
t_label = ""
tlim = (-0.5, nt - 0.5)
if xlim is None:
x_label = ""
xlim = (-0.5, nx - 0.5)
if ylim is None:
y_label = ""
ylim = (-0.5, ny - 0.5)
# vertical lines for coordinates reference
dt, dx, dy = (tlim[1] - tlim[0]) / nt, (xlim[1] - xlim[0]) / nx, (ylim[1] - ylim[0]) / ny
tline = dt * t + tlim[0] + 0.5 * dt
xline = dx * x + xlim[0] + 0.5 * dx
yline = dy * y + ylim[0] + 0.5 * dy
# instantiate plots
fig = plt.figure(figsize=figsize)
fig.suptitle(title, fontsize=18, fontweight='bold', y=0.94)
if ratio is None:
wr = (nx, ny)
hr = (ny, nx)
else:
wr = ratio[0]
hr = ratio[1]
opts = dict(cmap=cmap, clim=clipval if clipval is not None else clim(volume, p), aspect='auto',
interpolation=interp)
gs = fig.add_gridspec(2, 2, width_ratios=wr, height_ratios=hr,
left=0.1, right=0.9, bottom=0.1, top=0.9,
wspace=0.0, hspace=0.0)
ax = fig.add_subplot(gs[1, 0])
ax_top = fig.add_subplot(gs[0, 0], sharex=ax)
ax_right = fig.add_subplot(gs[1, 1], sharey=ax)
# central plot
ax.imshow(volume[:, :, y], extent=[xlim[0], xlim[1], tlim[1], tlim[0]], vmin=vmin,vmax=vmax,**opts)
ax.axvline(x=xline, **linespec)
ax.axhline(y=tline, **linespec)
if xcrop is not None:
ax.set_xlim(xcrop)
if tcrop is not None:
ax.set_ylim(tcrop[1], tcrop[0])
# top plot
ax_top.imshow(volume[t].T, extent=[xlim[0], xlim[1], ylim[1], ylim[0]], vmin=vmin,vmax=vmax, **opts)
ax_top.axvline(x=xline, **linespec)
ax_top.axhline(y=yline, **linespec)
ax_top.invert_yaxis()
if xcrop is not None:
ax_top.set_xlim(xcrop)
if ycrop is not None:
ax_top.set_ylim(ycrop[1], ycrop[0])
# right plot
ax_right.imshow(volume[:, x], extent=[ylim[0], ylim[1], tlim[1], tlim[0]], vmin=vmin,vmax=vmax, **opts)
ax_right.axvline(x=yline, **linespec)
ax_right.axhline(y=tline, **linespec)
if ycrop is not None:
ax_right.set_xlim(ycrop)
if tcrop is not None:
ax_right.set_ylim(tcrop[1], tcrop[0])
# labels
ax_top.tick_params(axis="x", labelbottom=False)
ax_right.tick_params(axis="y", labelleft=False, labelright=True)
ax_right.yaxis.set_ticks_position('right')
ax.tick_params(axis="y", labelleft=False, labelright=False)
ax.set_xlabel(xlabel + " " + x_label)
ax_right.set_ylabel(tlabel + " " + t_label)
ax_right.yaxis.set_label_position("right")
# ax_right.yaxis.tick_right()
ax_right.set_xlabel(ylabel + " " + y_label)
ax_top.set_ylabel(ylabel + " " + y_label)
if filename is not None:
if save_opts is None:
save_opts = {'format': 'png', 'dpi': 150, 'bbox_inches': 'tight'}
plt.savefig(f"{filename}.{save_opts['format']}", **save_opts)
return fig, (ax, ax_right, ax_top)