diff --git a/seismostats/seismicity/tests/test_catalog.py b/seismostats/seismicity/tests/test_catalog.py index 7a1f0ca..a130428 100644 --- a/seismostats/seismicity/tests/test_catalog.py +++ b/seismostats/seismicity/tests/test_catalog.py @@ -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, @@ -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(): diff --git a/seismostats/utils/binning.py b/seismostats/utils/binning.py index 78240c8..cd6b62a 100644 --- a/seismostats/utils/binning.py +++ b/seismostats/utils/binning.py @@ -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) diff --git a/seismostats/utils/tests/test_binning.py b/seismostats/utils/tests/test_binning.py index 24a8996..326db99 100644 --- a/seismostats/utils/tests/test_binning.py +++ b/seismostats/utils/tests/test_binning.py @@ -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])), @@ -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(