Skip to content

Commit

Permalink
delete unnecessary functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aronsho committed Aug 28, 2024
1 parent 55f441e commit 3b6b070
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 81 deletions.
38 changes: 10 additions & 28 deletions seismostats/analysis/estimate_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def estimate_b(
weights: list | None = None,
b_parameter: str = "b_value",
return_std: bool = False,
method="tinti",
method="classic",
return_n: bool = False,
) -> float | tuple[float, float] | tuple[float, float, float]:
"""Return the maximum likelihood estimator for the Gutenberg-Richter
Expand All @@ -63,9 +63,9 @@ def estimate_b(
method: method to use for estimation of beta/b-value. Options
are:
- 'tinti',default, this is the is the classic estimator, see
:func:`seismostats.analysis.estimate_b_tinti`
- 'positive' (this is b-positive, which applies the 'tinti'
- 'classic',default, this is the is the classic estimator, see
:func:`seismostats.analysis.estimate_b_classic`
- 'positive' (this is b-positive, which applies the 'classic'
method to the positive differences, see
:func:`seismostats.analysis.estimate_b_positive`. To
achieve the effect of reduced STAI, the magnitudes must
Expand Down Expand Up @@ -98,8 +98,8 @@ def estimate_b(
"check if mc is chosen correctly"
)

if method == "tinti":
return estimate_b_tinti(
if method == "classic":
return estimate_b_classic(
magnitudes,
mc=mc,
delta_m=delta_m,
Expand All @@ -118,10 +118,10 @@ def estimate_b(
)

else:
raise ValueError("method must be either 'tinti' or 'positive'")
raise ValueError("method must be either 'classic' or 'positive'")


def estimate_b_tinti(
def estimate_b_classic(
magnitudes: np.ndarray,
mc: float,
delta_m: float = 0,
Expand Down Expand Up @@ -232,24 +232,6 @@ def estimate_b_utsu(
return b


def differences(magnitudes: np.ndarray) -> np.ndarray:
"""returns all the differences between the magnitudes, only counting each
difference once
Args:
magnitudes: vector of magnitudes differences, sorted in time (first
entry is the earliest earthquake)
Returns: array of all differences of the elements of the input
"""
mag_diffs = np.array([])
for ii in range(1, len(magnitudes)):
loop_mag1 = magnitudes[ii:]
loop_mag2 = magnitudes[:-ii]
mag_diffs = np.append(mag_diffs, loop_mag1 - loop_mag2)
return mag_diffs


def estimate_b_positive(
magnitudes: np.ndarray,
delta_m: float = 0,
Expand Down Expand Up @@ -301,7 +283,7 @@ def estimate_b_positive(
# previous one. delta_m is added to avoid numerical errors
mag_diffs = abs(mag_diffs[mag_diffs > dmc - delta_m / 2])

out = estimate_b_tinti(
out = estimate_b_classic(
mag_diffs,
mc=dmc,
delta_m=delta_m,
Expand Down Expand Up @@ -375,7 +357,7 @@ def estimate_b_more_positive(
# only take the values where the next earthquake is larger
mag_diffs = abs(mag_diffs[mag_diffs > - delta_m / 2])

out = estimate_b_tinti(
out = estimate_b_classic(
mag_diffs,
mc=dmc,
delta_m=delta_m,
Expand Down
67 changes: 14 additions & 53 deletions seismostats/analysis/tests/test_estimate_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

# import functions to be tested
from seismostats.analysis.estimate_beta import (
differences,
estimate_b,
estimate_b_laplace,
estimate_b_positive,
estimate_b_classic,
estimate_b_utsu,
Expand Down Expand Up @@ -99,10 +97,6 @@ def test_estimate_b(


def magnitudes(b: float):
# print the current directory
import os
print(os.getcwd())

df_mags = pd.read_csv(
'seismostats/analysis/tests/data/simulated_magnitudes.csv')
if b == 0.5:
Expand Down Expand Up @@ -130,89 +124,56 @@ def test_estimate_b_classic(
precision: float,
b_parameter: str,
):
print('test_estimate_b_classic')
mags = bin_to_precision(mags, delta_m)
mags = mags[mags >= mc - delta_m / 2]
b_estimate = estimate_b_classic(mags, mc, delta_m, b_parameter=b_parameter)

assert abs(b - b_estimate) / b <= precision


# @ pytest.mark.parametrize(
# "b, mags, mc, delta_m, precision, b_parameter",
# [
# (1, magnitudes(1), 0, 0.01, 0.002, "b_value"),
# (1.5, magnitudes(1.5), 0.5, 0.01, 0.01, "b_value"),
# (b_value_to_beta(0.5), magnitudes(0.5), 2, 0.2, 0.003, "beta"),
# ],
# )
# def test_estimate_b_utsu(
# b: float,
# mags: np.ndarray,
# mc: float,
# delta_m: float,
# precision: float,
# b_parameter: str,
# ):
# print('test_estimate_b_utsu')
# mags = bin_to_precision(mags, delta_m)
# mags = mags[mags >= mc - delta_m / 2]
# b_estimate = estimate_b_utsu(mags, mc, delta_m, b_parameter=b_parameter)
# assert abs(b - b_estimate) / b <= precision


@ pytest.mark.parametrize(
"magnitudes,mag_diffs",
[(np.array([1, -2, 3]), np.array([-3, 5, 2]))],
)
def test_differences(magnitudes: np.ndarray, mag_diffs: np.ndarray):
y = differences(magnitudes)
assert (y == mag_diffs).all()


@ pytest.mark.parametrize(
"b, mags, mc, delta_m, dmc, precision, b_parameter",
"b, mags, mc, delta_m, precision, b_parameter",
[
(1, magnitudes(1), 0, 0.1, 0.3, 0.008, "b_value"),
(1.5, magnitudes(1.5), 0.5, 0.01, None, 0.02, "b_value"),
(b_value_to_beta(0.5), magnitudes(0.5), 2, 0.2, None, 0.02, "beta"),
(1, magnitudes(1), 0, 0.01, 0.002, "b_value"),
(1.5, magnitudes(1.5), 0.5, 0.01, 0.01, "b_value"),
(b_value_to_beta(0.5), magnitudes(0.5), 2, 0.2, 0.003, "beta"),
],
)
def test_estimate_b_positive(
def test_estimate_b_utsu(
b: float,
mags: np.ndarray,
mc: float,
delta_m: float,
dmc: float,
precision: float,
b_parameter: str,
):
mags = bin_to_precision(mags, delta_m)
mags = mags[mags >= mc - delta_m / 2]
b_estimate = estimate_b_positive(
mags, delta_m=delta_m, dmc=dmc, b_parameter=b_parameter
)
b_estimate = estimate_b_utsu(mags, mc, delta_m, b_parameter=b_parameter)
assert abs(b - b_estimate) / b <= precision


@ pytest.mark.parametrize(
"b, mags, mc, delta_m, precision, b_parameter",
"b, mags, mc, delta_m, dmc, precision, b_parameter",
[
(1, magnitudes(1)[:1500], 0, 0.01, 0.06, "b_value"),
(1, magnitudes(1), 0, 0.1, 0.3, 0.008, "b_value"),
(1.5, magnitudes(1.5), 0.5, 0.01, None, 0.02, "b_value"),
(b_value_to_beta(0.5), magnitudes(0.5), 2, 0.2, None, 0.02, "beta"),
],
)
def test_estimate_b_laplace(
def test_estimate_b_positive(
b: float,
mags: np.ndarray,
mc: float,
delta_m: float,
dmc: float,
precision: float,
b_parameter: str,
):
mags = bin_to_precision(mags, delta_m)
mags = mags[mags >= mc - delta_m / 2]
b_estimate = estimate_b_laplace(
mags, delta_m=delta_m, b_parameter=b_parameter
b_estimate = estimate_b_positive(
mags, delta_m=delta_m, dmc=dmc, b_parameter=b_parameter
)
assert abs(b - b_estimate) / b <= precision

Expand Down

0 comments on commit 3b6b070

Please sign in to comment.