Skip to content

Commit

Permalink
tests: adding functions testing Catalog methods - unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
martahan committed Jun 7, 2024
1 parent 86242d4 commit 5ca2cc9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
55 changes: 50 additions & 5 deletions seismostats/seismicity/tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import re
import uuid
import pytest

import numpy as np
import pandas as pd

from seismostats.seismicity.catalog import (REQUIRED_COLS_CATALOG, Catalog,
Expand Down Expand Up @@ -77,19 +79,62 @@ def test_forecast_catalog_strip():
assert isinstance(dropped, Catalog)


def test_catalog_bin():
mag_values = [0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]
delta_m = 0.1

@pytest.mark.parametrize(
"mag_values, delta_m",
[
(np.array([0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]),
0.1),
(np.array([0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]),
None),
(np.array([0.235, -0.235, 4.499, 5.5, 6, 0.1, 1.6]),
0.2),
([0.235, -0.235, 4.499, 5.5, 6, 0.1, 1.6],
0.2)
]
)
def test_catalog_bin(mag_values: np.ndarray, delta_m: float):
catalog = Catalog({'magnitude': mag_values})

assert (catalog.bin_magnitudes(
delta_m)['magnitude'].tolist()
== bin_to_precision(mag_values, delta_m)).all()

catalog.bin_magnitudes(delta_m, inplace=True)
return_value = catalog.bin_magnitudes(delta_m, inplace=True)
assert (catalog['magnitude'].tolist()
== bin_to_precision(mag_values, delta_m)).all()
assert return_value is None

assert catalog.delta_m == delta_m


@pytest.mark.parametrize(
"mag_values, delta_m",
[
(np.array([0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]),
0),
]
)
def test_catalog_bin_none(mag_values: np.ndarray, delta_m: float):
catalog = Catalog({'magnitude': mag_values})

with pytest.raises(ValueError):
catalog.bin_magnitudes(delta_m=delta_m)


def test_catalog_estimate_mc():
catalog = Catalog({'magnitude': [0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]})

with pytest.raises(ValueError):
catalog.estimate_mc()


def test_catalog_estimate_b():
catalog = Catalog({'magnitude': [0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]})

with pytest.raises(ValueError):
catalog.estimate_b(mc=None, delta_m=None)
catalog.estimate_b(mc=1.0, delta_m=None)
catalog.estimate_b(mc=None, delta_m=0.1)


def test_to_quakeml():
Expand Down
4 changes: 4 additions & 0 deletions seismostats/utils/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ def bin_to_precision(x: np.ndarray | list, delta_x: float = 0.1) -> np.ndarray:
"""
if x is None:
raise ValueError("x cannot be None")
if delta_x == 0:
raise ValueError("delta_x cannot be 0")

if isinstance(x, list):
x = np.array(x)
if delta_x is None:
delta_x = 0.1
d = decimal.Decimal(str(delta_x))
decimal_places = abs(d.as_tuple().exponent)
return np.round(normal_round_to_int(x / delta_x) * delta_x, decimal_places)
Expand Down
4 changes: 4 additions & 0 deletions seismostats/utils/tests/test_binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def test_normal_round(x: float, n: int, rounded_value: float):
(np.array([0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]),
0.1,
np.array([0.2, -0.2, 4.5, 4.5, 6, 0.1, 1.6])),
(np.array([0.235, -0.235, 4.499, 4.5, 6, 0.1, 1.6]),
None,
np.array([0.2, -0.2, 4.5, 4.5, 6, 0.1, 1.6])),
(np.array([0.235, -0.235, 4.499, 5.5, 6, 0.1, 1.6]),
0.2,
np.array([0.2, -0.2, 4.4, 5.6, 6, 0.2, 1.6])),
Expand All @@ -47,6 +50,7 @@ def test_bin_to_precision(x: np.ndarray, delta_x: float,
def test_bin_to_precision_none():
with pytest.raises(ValueError):
bin_to_precision(None)
bin_to_precision([], 0)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 5ca2cc9

Please sign in to comment.