-
Notifications
You must be signed in to change notification settings - Fork 214
/
test_meca.py
331 lines (307 loc) · 9.8 KB
/
test_meca.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
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
"""
Test Figure.meca.
"""
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from packaging.version import Version
from pygmt import Figure
from pygmt.clib import __gmt_version__
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
@pytest.mark.mpl_image_compare(filename="test_meca_spec_single_focalmecha.png")
@pytest.mark.parametrize("inputtype", ["dict_mecha", "dict_full", "array1d", "pandas"])
def test_meca_spec_single_focalmecha(inputtype):
"""
Test passing a single focal mechanism to the spec parameter.
"""
if inputtype == "dict_mecha":
args = {
"spec": {"strike": 0, "dip": 90, "rake": 0, "magnitude": 5},
"longitude": 0,
"latitude": 5,
"depth": 0,
}
elif inputtype == "dict_full":
args = {
"spec": {
"longitude": 0,
"latitude": 5,
"depth": 0,
"strike": 0,
"dip": 90,
"rake": 0,
"magnitude": 5,
}
}
elif inputtype == "array1d":
args = {
"spec": np.array([0, 5, 0, 0, 90, 0, 5]),
"convention": "a",
}
elif inputtype == "pandas":
args = {
"spec": pd.DataFrame(
{
"longitude": 0,
"latitude": 5,
"depth": 0,
"strike": 0,
"dip": 90,
"rake": 0,
"magnitude": 5,
},
index=[0],
)
}
fig = Figure()
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
fig.meca(scale="2.5c", **args)
return fig
@pytest.mark.mpl_image_compare(filename="test_meca_spec_single_focalmecha.png")
def test_meca_spec_single_focalmecha_file():
"""
Test supplying a file containing focal mechanisms and locations to the spec
parameter.
"""
fig = Figure()
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
with GMTTempFile() as temp:
Path(temp.name).write_text("0 5 0 0 90 0 5")
fig.meca(spec=temp.name, convention="aki", scale="2.5c")
return fig
@pytest.mark.benchmark
@pytest.mark.mpl_image_compare(filename="test_meca_spec_multiple_focalmecha.png")
@pytest.mark.parametrize(
"inputtype", ["dict_mecha", "dict_mecha_mixed", "dataframe", "array2d"]
)
def test_meca_spec_multiple_focalmecha(inputtype):
"""
Test passing multiple focal mechanisms to the spec parameter.
"""
if inputtype == "dict_mecha":
args = {
"spec": {
"strike": [330, 350],
"dip": [30, 50],
"rake": [90, 90],
"magnitude": [3, 2],
},
"longitude": [-123.5, -124.5],
"latitude": [47.5, 48.5],
"depth": [12.0, 11.0],
}
elif inputtype == "dict_mecha_mixed":
args = {
"spec": {
"strike": [330, 350],
"dip": [30, 50],
"rake": [90, 90],
"magnitude": [3, 2],
},
"longitude": np.array([-123.5, -124.5]),
"latitude": [47.5, 48.5],
"depth": [12, 11],
}
elif inputtype == "dataframe":
args = {
"spec": pd.DataFrame(
data={
"strike": [330, 350],
"dip": [30, 50],
"rake": [90, 90],
"magnitude": [3, 2],
"longitude": [-123.5, -124.5],
"latitude": [47.5, 48.5],
"depth": [12.0, 11.0],
},
)
}
elif inputtype == "array2d":
args = {
"spec": np.array(
[
[-123.5, 47.5, 12.0, 330, 30, 90, 3],
[-124.5, 48.5, 11.0, 350, 50, 90, 2],
]
),
"convention": "aki",
}
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M8c", frame=True)
fig.meca(scale="2c", **args)
return fig
@pytest.mark.mpl_image_compare(filename="test_meca_offset.png")
@pytest.mark.parametrize(
"inputtype",
[
"args",
"dict",
pytest.param(
"ndarray",
marks=pytest.mark.skipif(
condition=Version(__gmt_version__) < Version("6.5.0"),
reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7557",
),
),
],
)
def test_meca_offset(inputtype):
"""
Test offsetting beachballs.
"""
if inputtype == "args":
args = {
"spec": {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3},
"longitude": -124,
"latitude": 48,
"depth": 12.0,
"plot_longitude": -124.5,
"plot_latitude": 47.5,
}
elif inputtype == "dict":
# Test https://github.com/GenericMappingTools/pygmt/issues/2016
# offset parameters are in the dict.
args = {
"spec": {
"strike": 330,
"dip": 30,
"rake": 90,
"magnitude": 3,
"plot_longitude": -124.5,
"plot_latitude": 47.5,
},
"longitude": -124,
"latitude": 48,
"depth": 12.0,
}
elif inputtype == "ndarray":
# Test ndarray input reported in
# https://github.com/GenericMappingTools/pygmt/issues/2016
args = {
"spec": np.array([[-124, 48, 12.0, 330, 30, 90, 3, -124.5, 47.5]]),
"convention": "aki",
}
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
fig.meca(scale="1c", **args)
return fig
# Passing event names via pandas doesn't work for GMT<=6.4, thus marked as
# xfail. See https://github.com/GenericMappingTools/pygmt/issues/2524.
@pytest.mark.mpl_image_compare(filename="test_meca_eventname.png")
@pytest.mark.parametrize(
"inputtype",
[
"args",
pytest.param(
"dataframe",
marks=pytest.mark.skipif(
condition=Version(__gmt_version__) < Version("6.5.0"),
reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/7557",
),
),
],
)
def test_meca_eventname(inputtype):
"""
Test passing event names.
"""
if inputtype == "args":
args = {
"spec": {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3},
"longitude": -124,
"latitude": 48,
"depth": 12.0,
"event_name": "Event20220311",
}
elif inputtype == "dataframe":
# Test pandas.DataFrame input. Requires GMT>=6.5.
# See https://github.com/GenericMappingTools/pygmt/issues/2524.
# The numeric columns must be in float type to trigger the bug.
args = {
"spec": pd.DataFrame(
{
"longitude": [-124.0],
"latitude": [48.0],
"depth": [12.0],
"strike": [330.0],
"dip": [30.0],
"rake": [90.0],
"magnitude": [3.0],
"event_name": ["Event20220311"],
}
)
}
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
fig.meca(scale="1c", **args)
return fig
@pytest.mark.mpl_image_compare
def test_meca_dict_offset_eventname():
"""
Test offsetting beachballs for a dict input.
"""
fig = Figure()
focal_mechanism = {"strike": 330, "dip": 30, "rake": 90, "magnitude": 3}
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
fig.meca(
spec=focal_mechanism,
scale="1c",
longitude=-124,
latitude=48,
depth=12.0,
plot_longitude=-124.5,
plot_latitude=47.5,
event_name="Event20220311",
)
return fig
@pytest.mark.mpl_image_compare(filename="test_meca_eventname.png")
def test_meca_spec_dict_all_scalars():
"""
Test supplying a dict with scalar values for all focal parameters.
This is a regression test for
https://github.com/GenericMappingTools/pygmt/pull/2174
"""
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
fig.meca(
spec={
"strike": 330,
"dip": 30,
"rake": 90,
"magnitude": 3,
"longitude": -124,
"latitude": 48,
"depth": 12.0,
"event_name": "Event20220311",
},
scale=1.0, # make sure a non-str scale works
)
return fig
def test_meca_spec_ndarray_no_convention():
"""
Raise an exception if convention is not given for an ndarray input.
"""
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
with pytest.raises(GMTInvalidInput):
fig.meca(spec=np.array([[-124, 48, 12.0, 330, 30, 90, 3]]), scale="1c")
def test_meca_spec_ndarray_mismatched_columns():
"""
Raise an exception if the ndarray input doesn't have the expected number of columns.
"""
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
with pytest.raises(GMTInvalidInput):
fig.meca(
spec=np.array([[-124, 48, 12.0, 330, 30, 90]]), convention="aki", scale="1c"
)
fig = Figure()
fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True)
with pytest.raises(GMTInvalidInput):
fig.meca(
spec=np.array([[-124, 48, 12.0, 330, 30, 90, 3, -124.5, 47.5, 30.0, 50.0]]),
convention="aki",
scale="1c",
)