Skip to content

Commit

Permalink
Use the global config object in ssp_summary_statistics.py
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodsf committed Jul 9, 2024
1 parent ba6c7a6 commit 193e54a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 17 deletions.
2 changes: 1 addition & 1 deletion sourcespec2/source_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def main():

# Compute summary statistics from station spectral parameters
from .ssp_summary_statistics import compute_summary_statistics
compute_summary_statistics(config, sspec_output)
compute_summary_statistics(sspec_output)

# Save output
from .ssp_output import write_output, save_spectra
Expand Down
101 changes: 85 additions & 16 deletions sourcespec2/ssp_summary_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
from scipy.stats import norm
from scipy.integrate import quad
from .config import config
from .ssp_setup import ssp_exit
from .ssp_data_types import (
SummarySpectralParameter, SummaryStatistics)
Expand All @@ -26,6 +27,17 @@ def _avg_and_std(values, errors=None, logarithmic=False):
Optionally:
- errors can be specified for weighted statistics
- logarithmic average and standard deviation
:param values: Values to compute the average and standard deviation.
:type values: :class:`numpy.ndarray`
:param errors: Errors on the values (optional).
:type errors: :class:`numpy.ndarray`
:param logarithmic: Compute the average and standard deviation in
logarithmic space (default: False).
:type logarithmic: bool
:return: Average and standard deviation.
:rtype: tuple
"""
average = std = np.nan
if len(values) == 0:
Expand Down Expand Up @@ -54,7 +66,20 @@ def _avg_and_std(values, errors=None, logarithmic=False):


def _weights(values, errors=None, logarithmic=False):
"""Compute weights for weighted statistics."""
"""
Compute weights for weighted statistics.
:param values: Values to compute the weights for.
:type values: :class:`numpy.ndarray`
:param errors: Errors on the values (optional).
:type errors: :class:`numpy.ndarray`
:param logarithmic: Compute the weights in logarithmic space
(default: False).
:type logarithmic: bool
:return: Weights.
:rtype: :class:`numpy.ndarray`
"""
if errors is None:
return None
# negative errors should not happen
Expand Down Expand Up @@ -85,6 +110,12 @@ def _normal_confidence_level(n_sigma):
"""
Compute the confidence level of a normal (Gaussian) distribution
between -n_sigma and +n_sigma.
:param n_sigma: Number of standard deviations.
:type n_sigma: float
:return: Confidence level.
:rtype: float
"""
def gauss(x):
return norm.pdf(x, 0, 1)
Expand All @@ -94,7 +125,21 @@ def gauss(x):

def _percentiles(
values, low_percentage=25, mid_percentage=50, up_percentage=75):
"""Compute lower, mid and upper percentiles."""
"""
Compute lower, mid and upper percentiles.
:param values: Values to compute the percentiles for.
:type values: :class:`numpy.ndarray`
:param low_percentage: Lower percentile (default: 25).
:type low_percentage: float
:param mid_percentage: Mid percentile (default: 50).
:type mid_percentage: float
:param up_percentage: Upper percentile (default: 75).
:type up_percentage: float
:return: Lower, mid and upper percentiles.
:rtype: tuple
"""
if len(values) == 0:
return np.nan, np.nan, np.nan
low_percentile, mid_percentile, up_percentile =\
Expand All @@ -104,9 +149,28 @@ def _percentiles(


def _param_summary_statistics(
config, sspec_output, param_id, name, format_spec, units=None,
sspec_output, param_id, name, format_spec, units=None,
logarithmic=False):
"""Compute summary statistics for one spectral parameter."""
"""
Compute summary statistics for one spectral parameter.
:param sspec_output: Output of the spectral inversion.
:type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput`
:param param_id: Parameter ID.
:type param_id: str
:param name: Parameter name.
:type name: str
:param format_spec: Format specification for the parameter value.
:type format_spec: str
:param units: Units of the parameter (optional).
:type units: str
:param logarithmic: Compute the statistics in logarithmic space
(default: False).
:type logarithmic: bool
:return: Summary statistics.
:rtype: :class:`~sourcespec.ssp_data_types.SummarySpectralParameter`
"""
nIQR = config.nIQR
summary = SummarySpectralParameter(
param_id=param_id, name=name, format_spec=format_spec, units=units)
Expand Down Expand Up @@ -162,8 +226,13 @@ def _param_summary_statistics(
return summary


def compute_summary_statistics(config, sspec_output):
"""Compute summary statistics from station spectral parameters."""
def compute_summary_statistics(sspec_output):
"""
Compute summary statistics from station spectral parameters.
:param sspec_output: Output of the spectral inversion.
:type sspec_output: :class:`~sourcespec.ssp_data_types.SourceSpecOutput`
"""
logger.info('Computing summary statistics...')
if len(sspec_output.station_parameters) == 0:
logger.info('No source parameter calculated')
Expand All @@ -175,47 +244,47 @@ def compute_summary_statistics(config, sspec_output):
# Mw
sspec_output.summary_spectral_parameters.Mw =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='Mw', name='moment magnitude', format_spec='{:.2f}',
logarithmic=False
)

# Mo (N·m)
sspec_output.summary_spectral_parameters.Mo =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='Mo', name='seismic moment', units='N·m',
format_spec='{:.3e}', logarithmic=True
)

# fc (Hz)
sspec_output.summary_spectral_parameters.fc =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='fc', name='corner frequency', units='Hz',
format_spec='{:.3f}', logarithmic=True
)

# t_star (s)
sspec_output.summary_spectral_parameters.t_star =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='t_star', name='t-star', units='s', format_spec='{:.3f}',
logarithmic=False
)

# radius (meters)
sspec_output.summary_spectral_parameters.radius =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='radius', name='source radius', units='m',
format_spec='{:.3f}', logarithmic=True
)

# static stress drop (MPa)
sspec_output.summary_spectral_parameters.ssd =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='ssd', name='static stress drop',
units='MPa', format_spec='{:.3e}',
logarithmic=True
Expand All @@ -224,23 +293,23 @@ def compute_summary_statistics(config, sspec_output):
# Quality factor
sspec_output.summary_spectral_parameters.Qo =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='Qo', name='quality factor', format_spec='{:.1f}',
logarithmic=False
)

# Er (N·m)
sspec_output.summary_spectral_parameters.Er =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='Er', name='radiated energy', units='N·m',
format_spec='{:.3e}', logarithmic=True
)

# Apparent stress (MPa)
sspec_output.summary_spectral_parameters.sigma_a =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='sigma_a', name='apparent stress', units='MPa',
format_spec='{:.3e}', logarithmic=True
)
Expand All @@ -249,7 +318,7 @@ def compute_summary_statistics(config, sspec_output):
if config.compute_local_magnitude:
sspec_output.summary_spectral_parameters.Ml =\
_param_summary_statistics(
config, sspec_output,
sspec_output,
param_id='Ml', name='local magnitude', format_spec='{:.2f}',
logarithmic=False
)
Expand Down

0 comments on commit 193e54a

Please sign in to comment.