Skip to content

Commit 193e54a

Browse files
committed
Use the global config object in ssp_summary_statistics.py
1 parent ba6c7a6 commit 193e54a

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

sourcespec2/source_spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def main():
6969

7070
# Compute summary statistics from station spectral parameters
7171
from .ssp_summary_statistics import compute_summary_statistics
72-
compute_summary_statistics(config, sspec_output)
72+
compute_summary_statistics(sspec_output)
7373

7474
# Save output
7575
from .ssp_output import write_output, save_spectra

sourcespec2/ssp_summary_statistics.py

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import numpy as np
1414
from scipy.stats import norm
1515
from scipy.integrate import quad
16+
from .config import config
1617
from .ssp_setup import ssp_exit
1718
from .ssp_data_types import (
1819
SummarySpectralParameter, SummaryStatistics)
@@ -26,6 +27,17 @@ def _avg_and_std(values, errors=None, logarithmic=False):
2627
Optionally:
2728
- errors can be specified for weighted statistics
2829
- 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
2941
"""
3042
average = std = np.nan
3143
if len(values) == 0:
@@ -54,7 +66,20 @@ def _avg_and_std(values, errors=None, logarithmic=False):
5466

5567

5668
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+
"""
5883
if errors is None:
5984
return None
6085
# negative errors should not happen
@@ -85,6 +110,12 @@ def _normal_confidence_level(n_sigma):
85110
"""
86111
Compute the confidence level of a normal (Gaussian) distribution
87112
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
88119
"""
89120
def gauss(x):
90121
return norm.pdf(x, 0, 1)
@@ -94,7 +125,21 @@ def gauss(x):
94125

95126
def _percentiles(
96127
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+
"""
98143
if len(values) == 0:
99144
return np.nan, np.nan, np.nan
100145
low_percentile, mid_percentile, up_percentile =\
@@ -104,9 +149,28 @@ def _percentiles(
104149

105150

106151
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,
108153
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+
"""
110174
nIQR = config.nIQR
111175
summary = SummarySpectralParameter(
112176
param_id=param_id, name=name, format_spec=format_spec, units=units)
@@ -162,8 +226,13 @@ def _param_summary_statistics(
162226
return summary
163227

164228

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+
"""
167236
logger.info('Computing summary statistics...')
168237
if len(sspec_output.station_parameters) == 0:
169238
logger.info('No source parameter calculated')
@@ -175,47 +244,47 @@ def compute_summary_statistics(config, sspec_output):
175244
# Mw
176245
sspec_output.summary_spectral_parameters.Mw =\
177246
_param_summary_statistics(
178-
config, sspec_output,
247+
sspec_output,
179248
param_id='Mw', name='moment magnitude', format_spec='{:.2f}',
180249
logarithmic=False
181250
)
182251

183252
# Mo (N·m)
184253
sspec_output.summary_spectral_parameters.Mo =\
185254
_param_summary_statistics(
186-
config, sspec_output,
255+
sspec_output,
187256
param_id='Mo', name='seismic moment', units='N·m',
188257
format_spec='{:.3e}', logarithmic=True
189258
)
190259

191260
# fc (Hz)
192261
sspec_output.summary_spectral_parameters.fc =\
193262
_param_summary_statistics(
194-
config, sspec_output,
263+
sspec_output,
195264
param_id='fc', name='corner frequency', units='Hz',
196265
format_spec='{:.3f}', logarithmic=True
197266
)
198267

199268
# t_star (s)
200269
sspec_output.summary_spectral_parameters.t_star =\
201270
_param_summary_statistics(
202-
config, sspec_output,
271+
sspec_output,
203272
param_id='t_star', name='t-star', units='s', format_spec='{:.3f}',
204273
logarithmic=False
205274
)
206275

207276
# radius (meters)
208277
sspec_output.summary_spectral_parameters.radius =\
209278
_param_summary_statistics(
210-
config, sspec_output,
279+
sspec_output,
211280
param_id='radius', name='source radius', units='m',
212281
format_spec='{:.3f}', logarithmic=True
213282
)
214283

215284
# static stress drop (MPa)
216285
sspec_output.summary_spectral_parameters.ssd =\
217286
_param_summary_statistics(
218-
config, sspec_output,
287+
sspec_output,
219288
param_id='ssd', name='static stress drop',
220289
units='MPa', format_spec='{:.3e}',
221290
logarithmic=True
@@ -224,23 +293,23 @@ def compute_summary_statistics(config, sspec_output):
224293
# Quality factor
225294
sspec_output.summary_spectral_parameters.Qo =\
226295
_param_summary_statistics(
227-
config, sspec_output,
296+
sspec_output,
228297
param_id='Qo', name='quality factor', format_spec='{:.1f}',
229298
logarithmic=False
230299
)
231300

232301
# Er (N·m)
233302
sspec_output.summary_spectral_parameters.Er =\
234303
_param_summary_statistics(
235-
config, sspec_output,
304+
sspec_output,
236305
param_id='Er', name='radiated energy', units='N·m',
237306
format_spec='{:.3e}', logarithmic=True
238307
)
239308

240309
# Apparent stress (MPa)
241310
sspec_output.summary_spectral_parameters.sigma_a =\
242311
_param_summary_statistics(
243-
config, sspec_output,
312+
sspec_output,
244313
param_id='sigma_a', name='apparent stress', units='MPa',
245314
format_spec='{:.3e}', logarithmic=True
246315
)
@@ -249,7 +318,7 @@ def compute_summary_statistics(config, sspec_output):
249318
if config.compute_local_magnitude:
250319
sspec_output.summary_spectral_parameters.Ml =\
251320
_param_summary_statistics(
252-
config, sspec_output,
321+
sspec_output,
253322
param_id='Ml', name='local magnitude', format_spec='{:.2f}',
254323
logarithmic=False
255324
)

0 commit comments

Comments
 (0)