13
13
import numpy as np
14
14
from scipy .stats import norm
15
15
from scipy .integrate import quad
16
+ from .config import config
16
17
from .ssp_setup import ssp_exit
17
18
from .ssp_data_types import (
18
19
SummarySpectralParameter , SummaryStatistics )
@@ -26,6 +27,17 @@ def _avg_and_std(values, errors=None, logarithmic=False):
26
27
Optionally:
27
28
- errors can be specified for weighted statistics
28
29
- logarithmic average and standard deviation
30
+
31
+ :param values: Values to compute the average and standard deviation.
32
+ :type values: :class:`numpy.ndarray`
33
+ :param errors: Errors on the values (optional).
34
+ :type errors: :class:`numpy.ndarray`
35
+ :param logarithmic: Compute the average and standard deviation in
36
+ logarithmic space (default: False).
37
+ :type logarithmic: bool
38
+
39
+ :return: Average and standard deviation.
40
+ :rtype: tuple
29
41
"""
30
42
average = std = np .nan
31
43
if len (values ) == 0 :
@@ -54,7 +66,20 @@ def _avg_and_std(values, errors=None, logarithmic=False):
54
66
55
67
56
68
def _weights (values , errors = None , logarithmic = False ):
57
- """Compute weights for weighted statistics."""
69
+ """
70
+ Compute weights for weighted statistics.
71
+
72
+ :param values: Values to compute the weights for.
73
+ :type values: :class:`numpy.ndarray`
74
+ :param errors: Errors on the values (optional).
75
+ :type errors: :class:`numpy.ndarray`
76
+ :param logarithmic: Compute the weights in logarithmic space
77
+ (default: False).
78
+ :type logarithmic: bool
79
+
80
+ :return: Weights.
81
+ :rtype: :class:`numpy.ndarray`
82
+ """
58
83
if errors is None :
59
84
return None
60
85
# negative errors should not happen
@@ -85,6 +110,12 @@ def _normal_confidence_level(n_sigma):
85
110
"""
86
111
Compute the confidence level of a normal (Gaussian) distribution
87
112
between -n_sigma and +n_sigma.
113
+
114
+ :param n_sigma: Number of standard deviations.
115
+ :type n_sigma: float
116
+
117
+ :return: Confidence level.
118
+ :rtype: float
88
119
"""
89
120
def gauss (x ):
90
121
return norm .pdf (x , 0 , 1 )
@@ -94,7 +125,21 @@ def gauss(x):
94
125
95
126
def _percentiles (
96
127
values , low_percentage = 25 , mid_percentage = 50 , up_percentage = 75 ):
97
- """Compute lower, mid and upper percentiles."""
128
+ """
129
+ Compute lower, mid and upper percentiles.
130
+
131
+ :param values: Values to compute the percentiles for.
132
+ :type values: :class:`numpy.ndarray`
133
+ :param low_percentage: Lower percentile (default: 25).
134
+ :type low_percentage: float
135
+ :param mid_percentage: Mid percentile (default: 50).
136
+ :type mid_percentage: float
137
+ :param up_percentage: Upper percentile (default: 75).
138
+ :type up_percentage: float
139
+
140
+ :return: Lower, mid and upper percentiles.
141
+ :rtype: tuple
142
+ """
98
143
if len (values ) == 0 :
99
144
return np .nan , np .nan , np .nan
100
145
low_percentile , mid_percentile , up_percentile = \
@@ -104,9 +149,28 @@ def _percentiles(
104
149
105
150
106
151
def _param_summary_statistics (
107
- config , sspec_output , param_id , name , format_spec , units = None ,
152
+ sspec_output , param_id , name , format_spec , units = None ,
108
153
logarithmic = False ):
109
- """Compute summary statistics for one spectral parameter."""
154
+ """
155
+ Compute summary statistics for one spectral parameter.
156
+
157
+ :param sspec_output: Output of the spectral inversion.
158
+ :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput`
159
+ :param param_id: Parameter ID.
160
+ :type param_id: str
161
+ :param name: Parameter name.
162
+ :type name: str
163
+ :param format_spec: Format specification for the parameter value.
164
+ :type format_spec: str
165
+ :param units: Units of the parameter (optional).
166
+ :type units: str
167
+ :param logarithmic: Compute the statistics in logarithmic space
168
+ (default: False).
169
+ :type logarithmic: bool
170
+
171
+ :return: Summary statistics.
172
+ :rtype: :class:`~sourcespec.ssp_data_types.SummarySpectralParameter`
173
+ """
110
174
nIQR = config .nIQR
111
175
summary = SummarySpectralParameter (
112
176
param_id = param_id , name = name , format_spec = format_spec , units = units )
@@ -162,8 +226,13 @@ def _param_summary_statistics(
162
226
return summary
163
227
164
228
165
- def compute_summary_statistics (config , sspec_output ):
166
- """Compute summary statistics from station spectral parameters."""
229
+ def compute_summary_statistics (sspec_output ):
230
+ """
231
+ Compute summary statistics from station spectral parameters.
232
+
233
+ :param sspec_output: Output of the spectral inversion.
234
+ :type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput`
235
+ """
167
236
logger .info ('Computing summary statistics...' )
168
237
if len (sspec_output .station_parameters ) == 0 :
169
238
logger .info ('No source parameter calculated' )
@@ -175,47 +244,47 @@ def compute_summary_statistics(config, sspec_output):
175
244
# Mw
176
245
sspec_output .summary_spectral_parameters .Mw = \
177
246
_param_summary_statistics (
178
- config , sspec_output ,
247
+ sspec_output ,
179
248
param_id = 'Mw' , name = 'moment magnitude' , format_spec = '{:.2f}' ,
180
249
logarithmic = False
181
250
)
182
251
183
252
# Mo (N·m)
184
253
sspec_output .summary_spectral_parameters .Mo = \
185
254
_param_summary_statistics (
186
- config , sspec_output ,
255
+ sspec_output ,
187
256
param_id = 'Mo' , name = 'seismic moment' , units = 'N·m' ,
188
257
format_spec = '{:.3e}' , logarithmic = True
189
258
)
190
259
191
260
# fc (Hz)
192
261
sspec_output .summary_spectral_parameters .fc = \
193
262
_param_summary_statistics (
194
- config , sspec_output ,
263
+ sspec_output ,
195
264
param_id = 'fc' , name = 'corner frequency' , units = 'Hz' ,
196
265
format_spec = '{:.3f}' , logarithmic = True
197
266
)
198
267
199
268
# t_star (s)
200
269
sspec_output .summary_spectral_parameters .t_star = \
201
270
_param_summary_statistics (
202
- config , sspec_output ,
271
+ sspec_output ,
203
272
param_id = 't_star' , name = 't-star' , units = 's' , format_spec = '{:.3f}' ,
204
273
logarithmic = False
205
274
)
206
275
207
276
# radius (meters)
208
277
sspec_output .summary_spectral_parameters .radius = \
209
278
_param_summary_statistics (
210
- config , sspec_output ,
279
+ sspec_output ,
211
280
param_id = 'radius' , name = 'source radius' , units = 'm' ,
212
281
format_spec = '{:.3f}' , logarithmic = True
213
282
)
214
283
215
284
# static stress drop (MPa)
216
285
sspec_output .summary_spectral_parameters .ssd = \
217
286
_param_summary_statistics (
218
- config , sspec_output ,
287
+ sspec_output ,
219
288
param_id = 'ssd' , name = 'static stress drop' ,
220
289
units = 'MPa' , format_spec = '{:.3e}' ,
221
290
logarithmic = True
@@ -224,23 +293,23 @@ def compute_summary_statistics(config, sspec_output):
224
293
# Quality factor
225
294
sspec_output .summary_spectral_parameters .Qo = \
226
295
_param_summary_statistics (
227
- config , sspec_output ,
296
+ sspec_output ,
228
297
param_id = 'Qo' , name = 'quality factor' , format_spec = '{:.1f}' ,
229
298
logarithmic = False
230
299
)
231
300
232
301
# Er (N·m)
233
302
sspec_output .summary_spectral_parameters .Er = \
234
303
_param_summary_statistics (
235
- config , sspec_output ,
304
+ sspec_output ,
236
305
param_id = 'Er' , name = 'radiated energy' , units = 'N·m' ,
237
306
format_spec = '{:.3e}' , logarithmic = True
238
307
)
239
308
240
309
# Apparent stress (MPa)
241
310
sspec_output .summary_spectral_parameters .sigma_a = \
242
311
_param_summary_statistics (
243
- config , sspec_output ,
312
+ sspec_output ,
244
313
param_id = 'sigma_a' , name = 'apparent stress' , units = 'MPa' ,
245
314
format_spec = '{:.3e}' , logarithmic = True
246
315
)
@@ -249,7 +318,7 @@ def compute_summary_statistics(config, sspec_output):
249
318
if config .compute_local_magnitude :
250
319
sspec_output .summary_spectral_parameters .Ml = \
251
320
_param_summary_statistics (
252
- config , sspec_output ,
321
+ sspec_output ,
253
322
param_id = 'Ml' , name = 'local magnitude' , format_spec = '{:.2f}' ,
254
323
logarithmic = False
255
324
)
0 commit comments