Skip to content

Commit

Permalink
simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
aronsho committed Jun 21, 2024
1 parent ca459c5 commit 65e4898
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions seismostats/analysis/estimate_mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,41 @@ def empirical_cdf(
except BaseException:
pass

if get_option("warnings") is True:
# check if binning is correct
if not np.allclose(sample, bin_to_precision(sample, delta_m)):
warnings.warn(
"Magnitudes are not binned correctly. "
"Test might fail because of this."
)

if mc is None:
mc = np.min(sample)

idx = np.argsort(sample)
x = sample[idx]

if delta_m > 0:
# this step is necessary to include all magnitude bins, even if they
# are empty. Only done when the magnitudesa are discrete.
x = np.concatenate((x, np.arange(mc, max(x) + delta_m, delta_m)))
x = bin_to_precision(x, delta_m)

if weights is not None:
weights_sorted = weights[idx]
y = np.cumsum(weights_sorted) / weights_sorted.sum()
else:
y = np.arange(1, len(sample) + 1) / len(sample)

x, y_count = np.unique(x, return_counts=True)
# add empty bins
if delta_m > 0:
y_count -= 1
for mag_bin in bin_to_precision(
np.arange(mc, np.max(sample) + delta_m, delta_m), delta_m
):
if mag_bin not in x:
x = np.append(x, mag_bin)
y_count = np.append(y_count, 0)
idx = np.argsort(x)
x = x[idx]
y_count = y_count[idx]

y = y[np.cumsum(y_count) - 1]

return x, y


Expand Down

0 comments on commit 65e4898

Please sign in to comment.