Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
aronsho committed Feb 15, 2025
1 parent 59e77a2 commit 3a16b5b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 81 deletions.
28 changes: 12 additions & 16 deletions seismostats/analysis/avalue/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,32 @@ def _filter_magnitudes(self) -> np.ndarray:
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = np.where(self.magnitudes >= self.mc - self.delta_m / 2)[0]
self.magnitudes = self.magnitudes[idx]
self.idx = np.where(self.magnitudes >= self.mc - self.delta_m / 2)[0]
self.magnitudes = self.magnitudes[self.idx]

assert (
len(self.magnitudes) > 0
)
'No magnitudes above the completeness magnitude.'
if len(self.magnitudes) == 0:
raise ValueError('No magnitudes above the completeness magnitude.')

self.idx = idx
return idx
return self.idx

def _sanity_checks(self):
'''
Perform sanity checks on the input data.
'''
# test magnitude binnning
if not binning_test(self.magnitudes, self.delta_m,
check_larger_binning=False):
raise ValueError('Magnitudes are not binned correctly.')

if np.any(np.isnan(self.magnitudes)):
raise ValueError('Magnitudes contain NaN values.')
assert (
binning_test(self.magnitudes, self.delta_m,
check_larger_binning=False)
)
'Magnitudes are not binned correctly.'

# give warnings
if get_option('warnings') is True:
if np.min(self.magnitudes) - self.mc > self.delta_m / 2:
warnings.warn(
'No magnitudes in the lowest magnitude bin are present. '
'Check if mc is chosen correctly.'
)
if np.any(np.isnan(self.magnitudes)):
warnings.warn('Magnitudes contain NaN values.')

def _reference_scaling(self, a: float) -> float:
'''
Expand Down
8 changes: 3 additions & 5 deletions seismostats/analysis/avalue/more_positive.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ def _filter_magnitudes(self) -> np.ndarray:
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = super()._filter_magnitudes()

self.times = self.times[idx]

return idx
super()._filter_magnitudes()
self.times = self.times[self.idx]
return self.idx

def _estimate(self) -> float:
# order the magnitudes and times
Expand Down
8 changes: 3 additions & 5 deletions seismostats/analysis/avalue/positive.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ def _filter_magnitudes(self) -> np.ndarray:
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = super()._filter_magnitudes()

self.times = self.times[idx]

return idx
super()._filter_magnitudes()
self.times = self.times[self.idx]
return self.idx

def _estimate(self) -> float:
# order the magnitudes and times
Expand Down
4 changes: 2 additions & 2 deletions seismostats/analysis/avalue/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_estimate_a_warnings():
mags = simulate_magnitudes_binned(n=100, b=1, mc=0, delta_m=0.1)

# test that uncorrect binninng leads to error
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
estimator = ClassicAValueEstimator()
estimator.calculate(mags, mc=0, delta_m=0.2)

Expand All @@ -25,7 +25,7 @@ def test_estimate_a_warnings():

# No magnitudes above completeness magnitude
mags = np.array([0, 0.9, 0.1, 0.2, 0.5])
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
estimator.calculate(mags, mc=1, delta_m=0.1)


Expand Down
40 changes: 18 additions & 22 deletions seismostats/analysis/bvalue/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,43 @@ def _filter_magnitudes(self):
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = np.where(self.magnitudes >= self.mc - self.delta_m / 2)[0]
self.magnitudes = self.magnitudes[idx]
self.idx = np.where(self.magnitudes >= self.mc - self.delta_m / 2)[0]
self.magnitudes = self.magnitudes[self.idx]

if self.weights is not None:
self.weights = self.weights[idx]
self.weights = self.weights[self.idx]

assert (
len(self.magnitudes) > 0
)
'No magnitudes above the completeness magnitude.'
if len(self.magnitudes) == 0:
raise ValueError('No magnitudes above the completeness magnitude.')

self.idx = idx
return idx
return self.idx

def _sanity_checks(self):
'''
Perform sanity checks on the input data.
'''
if np.any(np.isnan(self.magnitudes)):
raise ValueError('Magnitudes contain NaN values.')

# test magnitude binnning
assert (
binning_test(self.magnitudes, self.delta_m,
check_larger_binning=False)
)
'Magnitudes are not binned correctly.'
if not binning_test(self.magnitudes, self.delta_m,
check_larger_binning=False):
raise ValueError('Magnitudes are not binned correctly.')

# test weights
if self.weights is not None:
assert len(self.magnitudes) == len(self.weights), (
'The number of magnitudes and weights must be equal.'
)
assert np.all(self.weights >= 0), 'Weights must be nonnegative.'
if len(self.magnitudes) != len(self.weights):
raise IndexError(
'The number of magnitudes and weights must be equal.')
if np.any(self.weights < 0):
raise ValueError('Weights must be nonnegative.')

# test if lowest magnitude is much larger than mc
# give warnings
if get_option('warnings') is True:
if np.min(self.magnitudes) - self.mc > self.delta_m / 2:
warnings.warn(
'No magnitudes in the lowest magnitude bin are present. '
'Check if mc is chosen correctly.'
)
if np.any(np.isnan(self.magnitudes)):
warnings.warn('Magnitudes contain NaN values.')

@classmethod
@abstractmethod
Expand Down
14 changes: 5 additions & 9 deletions seismostats/analysis/bvalue/more_positive.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ def calculate(self,
this value are not considered). If None, the cutoff is set
to delta_m.
'''
if times is None:
self.times = None
else:
self.times: np.ndarray = np.array(times)

self.times: np.ndarray | None = np.array(
times) if times is not None else times
self.dmc: float = dmc if dmc is not None else delta_m

if self.dmc < 0:
Expand All @@ -70,11 +67,10 @@ def _filter_magnitudes(self) -> np.ndarray:
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = super()._filter_magnitudes()

super()._filter_magnitudes()
if self.times is not None:
self.times = self.times[idx]
return idx
self.times = self.times[self.idx]
return self.idx

def _estimate(self) -> float:
if self.times is not None:
Expand Down
14 changes: 5 additions & 9 deletions seismostats/analysis/bvalue/positive.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ def calculate(self,
this value are not considered). If None, the cutoff is set
to delta_m.
'''
if times is None:
self.times = None
else:
self.times: np.ndarray = np.array(times)

self.times: np.ndarray | None = np.array(
times) if times is not None else times
self.dmc: float = dmc if dmc is not None else delta_m

if self.dmc < 0:
Expand All @@ -67,11 +64,10 @@ def _filter_magnitudes(self) -> np.ndarray:
'''
Filter out magnitudes below the completeness magnitude.
'''
idx = super()._filter_magnitudes()

super()._filter_magnitudes()
if self.times is not None:
self.times = self.times[idx]
return idx
self.times = self.times[self.idx]
return self.idx

def _estimate(self) -> float:
# order the magnitudes and times
Expand Down
4 changes: 2 additions & 2 deletions seismostats/analysis/bvalue/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_estimate_b_warnings():
mags = simulate_magnitudes_binned(n=100, b=1, mc=0, delta_m=0.1)

# test that uncorrect binninng leads to error
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
estimator = ClassicBValueEstimator()
estimator.calculate(mags, mc=0, delta_m=0.2)

Expand All @@ -27,7 +27,7 @@ def test_estimate_b_warnings():

# No magnitudes above completeness magnitude
mags = np.array([0, 0.9, 0.1, 0.2, 0.5])
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
estimator.calculate(mags, mc=1, delta_m=0.1)


Expand Down
23 changes: 12 additions & 11 deletions seismostats/plots/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def plot_b_series_constant_nm(
ax: plt.Axes | None = None,
color: str = "blue",
label: str | None = None,
*args,
**kwargs,
) -> plt.Axes:
"""
Expand Down Expand Up @@ -119,12 +120,17 @@ def plot_b_series_constant_nm(
the b-values are plotted against the event index.
confidence: confidence interval that should be plotted. Default
is 0.95 (i.e., the 95% confidence interval is plotted)
ax: axis where the plot should be plotted
color: color of the data
ax: axis where the plot should be plotted
color: color of the data
label: abel of the data that will be put in the legend
*args: Additional positional arguments for the b-value estimator.
**kwargs: Additional keyword arguments for the b-value estimator.
Returns:
ax that was plotted on
"""
mags = np.array(mags)
times = np.array(times)

if isinstance(mc, (float, int)):
if min(mags) < mc:
Expand All @@ -135,21 +141,15 @@ def plot_b_series_constant_nm(
if any(mags < mc):
raise ValueError("There are earthquakes below their respective "
"completeness magnitude")

if n_m < min_num:
raise ValueError("n_m cannot be smaller than min_num")

if not isinstance(mags, np.ndarray):
raise ValueError("mags must be an array")
if not isinstance(times, np.ndarray):
raise ValueError("times must be an array")
if len(mags) != len(times):
raise ValueError("mags and times must have the same length")
raise IndexError("mags and times must have the same length")

if x_variable is None:
x_variable = np.arange(len(mags))
elif len(x_variable) != len(mags):
raise ValueError(
raise IndexError(
"x_variable must have the same length as magnitudes")
else:
idx_sort = np.argsort(x_variable)
Expand Down Expand Up @@ -186,7 +186,8 @@ def plot_b_series_constant_nm(
times_window = times_window[idx]

# estimate the b-value
estimator.calculate(mags_window, mc=mc[ii], delta_m=delta_m, **kwargs)
estimator.calculate(
mags_window, mc=mc[ii], delta_m=delta_m, *args, **kwargs)
if estimator.n < min_num:
b_values[idx_start + ii] = np.nan
std_bs[idx_start + ii] = np.nan
Expand Down

0 comments on commit 3a16b5b

Please sign in to comment.