-
Notifications
You must be signed in to change notification settings - Fork 22
/
double_couple.py
212 lines (148 loc) · 5.31 KB
/
double_couple.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
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
#
# graphics/uq/double_couple.py - uncertainty quantification of double couple sources
#
import numpy as np
from matplotlib import pyplot
from pandas import DataFrame
from xarray import DataArray
from mtuq.graphics._gmt import read_cpt
from mtuq.grid_search import MTUQDataArray, MTUQDataFrame
from mtuq.util import fullpath, warn
from mtuq.util.math import closed_interval, open_interval, to_delta, to_gamma
from os.path import exists
def plot_misfit_dc(filename, ds, title='',
colorbar_type=1, marker_type=1):
""" Plots misfit over strike, dip, and slip
(matplotlib implementation)
"""
_check(ds)
ds = ds.copy()
if issubclass(type(ds), DataArray):
_plot_dc(filename, _squeeze(ds), cmap='viridis')
elif issubclass(type(ds), DataFrame):
warn('plot_misfit_dc not implemented for irregularly-spaced grids')
def plot_likelihood_dc(filename, ds, sigma=None, title=''):
assert sigma is not None
_check(ds)
ds = ds.copy()
if issubclass(type(ds), DataArray):
ds.values = np.exp(-ds.values/(2.*sigma**2))
_plot_dc(filename, _squeeze(ds), cmap='hot',
colorbar_type=colorbar_type, marker_type=marker_type)
elif issubclass(type(ds), DataFrame):
warn('plot_likelihood_dc not implemented for irregularly-spaced grids')
def plot_marginal_dc():
raise NotImplementedError
def _squeeze(da):
if 'origin_idx' in da.dims:
da = da.min(dim='origin_idx')
if 'rho' in da.dims:
da = da.min(dim='rho')
if 'v' in da.dims:
assert len(da.coords['v'])==1
da = da.squeeze(dim='v')
if 'w' in da.dims:
assert len(da.coords['w'])==1
da = da.squeeze(dim='w')
return da
def _check(ds):
""" Checks data structures
"""
if type(ds) not in (DataArray, DataFrame, MTUQDataArray, MTUQDataFrame):
raise TypeError("Unexpected grid format")
#
# matplotlib backend
#
def _plot_dc(filename, da, colorbar_type=1, marker_type=1, cmap='hot', **kwargs):
# FIXME: do labels correspond to the correct axes ?!
# prepare axes
fig, axes = pyplot.subplots(2, 2,
figsize=(8., 8.),
)
pyplot.subplots_adjust(
wspace=0.4,
hspace=0.4,
)
if exists(_local_path(cmap)):
cmap = read_cpt(_local_path(cmap))
# upper left panel
marginal = da.min(dim=('sigma'))
x = marginal.coords['h']
y = marginal.coords['kappa']
minmax1 = _minmax(x, y, marginal)
axis = axes[0][0]
_pcolor(axis, x, y, marginal.values, cmap, **kwargs)
axis.set_xlabel('Dip', **axis_label_kwargs)
axis.set_xticks(theta_ticks)
axis.set_xticklabels(theta_ticklabels)
axis.set_ylabel('Strike', **axis_label_kwargs)
axis.set_yticks(kappa_ticks)
axis.set_yticklabels(kappa_ticklabels)
# upper right panel
marginal = da.min(dim=('h'))
x = marginal.coords['sigma']
y = marginal.coords['kappa']
minmax2 = _minmax(x, y, marginal)
axis = axes[0][1]
_pcolor(axis, x, y, marginal.values, cmap, **kwargs)
axis.set_xlabel('Slip', **axis_label_kwargs)
axis.set_xticks(sigma_ticks)
axis.set_xticklabels(sigma_ticklabels)
axis.set_ylabel('Strike', **axis_label_kwargs)
axis.set_yticks(kappa_ticks)
axis.set_yticklabels(kappa_ticklabels)
# lower right panel
marginal = da.min(dim=('kappa'))
y = marginal.coords['h']
x = marginal.coords['sigma']
minmax3 = _minmax(x, y, marginal.T)
axis = axes[1][1]
_pcolor(axis, x, y, marginal.values.T, cmap, **kwargs)
axis.set_xlabel('Slip', **axis_label_kwargs)
axis.set_xticks(sigma_ticks)
axis.set_xticklabels(sigma_ticklabels)
axis.set_ylabel('Dip', **axis_label_kwargs)
axis.set_yticks(theta_ticks)
axis.set_yticklabels(theta_ticklabels)
# lower left panel
axes[1][0].axis('off')
# optional markers
if marker_type > 0:
_add_marker(axes[0][0], minmax1[marker_type-1])
_add_marker(axes[0][1], minmax2[marker_type-1])
_add_marker(axes[1][1], minmax3[marker_type-1])
pyplot.savefig(filename)
def _add_marker(axis, coords):
axis.scatter(*coords, s=250,
marker='o',
facecolors='none',
edgecolors=[0,1,0],
linewidths=1.75,
clip_on=False,
zorder=100,
)
def _minmax(x, y, values):
x = x.values
y = y.values
iymin, ixmin = np.unravel_index(values.argmin(), values.shape)
iymax, ixmax = np.unravel_index(values.argmax(), values.shape)
xmin, ymin = x[ixmin], y[iymin]
xmax, ymax = x[ixmax], y[iymax]
return (xmin, ymin), (xmax, ymax)
axis_label_kwargs = {
'fontsize': 14
}
def _pcolor(axis, x, y, values, cmap, **kwargs):
# workaround matplotlib compatibility issue
try:
axis.pcolor(x, y, values, cmap=cmap, shading='auto', **kwargs)
except:
axis.pcolor(x, y, values, cmap=cmap, **kwargs)
kappa_ticks = [0, 45, 90, 135, 180, 225, 270, 315, 360]
kappa_ticklabels = ['0', '', '90', '', '180', '', '270', '', '360']
sigma_ticks = [-90, -67.5, -45, -22.5, 0, 22.5, 45, 67.5, 90]
sigma_ticklabels = ['-90', '', '-45', '', '0', '', '45', '', '90']
theta_ticks = [np.cos(np.radians(tick)) for tick in [0, 15, 30, 45, 60, 75, 90]]
theta_ticklabels = ['0', '', '30', '', '60', '', '90']
def _local_path(name):
return fullpath('mtuq/graphics/_gmt/cpt', name+'.cpt')