Skip to content

Commit

Permalink
Merge pull request #93 from cognitedata/making_indsl_user_warnings
Browse files Browse the repository at this point in the history
feat: adding specific IndslUserWarning [AH-2555]
  • Loading branch information
neringaalt authored Jan 15, 2024
2 parents 292d34a + 5778815 commit 898799c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion indsl/equipment/volume_vessel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from indsl.exceptions import FLUIDS_REQUIRED
from indsl.resample.auto_align import auto_align
from indsl.type_check import check_types
from indsl.warnings import IndslUserWarning


NUMBA_DISABLED = os.environ.get("NUMBA_DISABLE_JIT") == "1"
Expand All @@ -29,7 +30,8 @@
import fluids.vectorized as fv
except ImportError as e:
warnings.warn(
f"Couldn't import fluids.numba_vectorized: {e!s}. Default to import fluids.vectorized.", UserWarning
f"Couldn't import fluids.numba_vectorized: {e!s}. Default to import fluids.vectorized.",
category=IndslUserWarning,
)
fv = None # Only core dependencies available. Will raise an error later

Expand Down
5 changes: 3 additions & 2 deletions indsl/resample/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from indsl.exceptions import UserValueError
from indsl.ts_utils.ts_utils import functional_mean, is_na_all
from indsl.type_check import check_types
from indsl.warnings import IndslUserWarning

from . import interpolate_v1 # noqa

Expand Down Expand Up @@ -65,7 +66,7 @@ def interpolate(
"""
# Check if all values are NaN
if is_na_all(data):
warnings.warn("All values in the time series are NaN.", UserWarning)
warnings.warn("All values in the time series are NaN.", category=IndslUserWarning)
return data

# Allow for other ways of defining forward filling for stepwise functions
Expand All @@ -92,7 +93,7 @@ def interpolate(

# Check for empty time series
if len(observations) < 2:
warnings.warn("The time series contains less than two values.", UserWarning)
warnings.warn("The time series contains less than two values.", category=IndslUserWarning)
return data

# x and y datapoints used to construct linear piecewise function
Expand Down
4 changes: 3 additions & 1 deletion indsl/resample/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from indsl.ts_utils.ts_utils import fill_gaps, get_fixed_freq, is_na_all
from indsl.type_check import check_types
from indsl.validations import validate_series_has_time_index, validate_series_is_not_empty
from indsl.warnings import IndslUserWarning

from . import resample_v1 # noqa

Expand Down Expand Up @@ -102,7 +103,8 @@ def resample(
if not granularity_current:
# TODO: pick max resolution and apply to the rest of the timeseries?
warnings.warn(
"Can't infer time series resolution with missing data. Please provide resolution", UserWarning
"Can't infer time series resolution with missing data. Please provide resolution",
category=IndslUserWarning,
)
return data

Expand Down
4 changes: 3 additions & 1 deletion indsl/resample/resample_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from indsl.ts_utils.ts_utils import fill_gaps, get_fixed_freq, is_na_all
from indsl.type_check import check_types
from indsl.validations import validate_series_has_time_index
from indsl.warnings import IndslUserWarning


@versioning.register(version="1.0", deprecated=True)
Expand Down Expand Up @@ -102,7 +103,8 @@ def resample(
if not granularity_current:
# TODO: pick max resolution and apply to the rest of the timeseries?
warnings.warn(
"Can't infer time series resolution with missing data. Please provide resolution", UserWarning
"Can't infer time series resolution with missing data. Please provide resolution",
category=IndslUserWarning,
)
return data

Expand Down
5 changes: 4 additions & 1 deletion indsl/signals/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from indsl.ts_utils.utility_functions import TimeUnits
from indsl.type_check import check_types
from indsl.validations import validate_series_has_time_index
from indsl.warnings import IndslUserWarning


@check_types
Expand Down Expand Up @@ -574,7 +575,9 @@ def _make_index(
# Catch strange sample frequency inputs
if not isinstance(freq, pd.Timedelta):
freq = pd.Timedelta(1, "m")
warnings.warn("Can't recognize the sample frequency, setting it to the '1 m' default.")
warnings.warn(
"Can't recognize the sample frequency, setting it to the '1 m' default.", category=IndslUserWarning
)

if freq.total_seconds() <= 0:
raise UserValueError(
Expand Down
3 changes: 2 additions & 1 deletion indsl/statistics/outliers_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from indsl.exceptions import CSAPS_REQUIRED, KNEED_REQUIRED, SCIKIT_LEARN_REQUIRED, UserValueError
from indsl.type_check import check_types
from indsl.validations import validate_series_has_time_index, validate_series_is_not_empty
from indsl.warnings import IndslUserWarning


@versioning.register(version="1.0", deprecated=True)
Expand Down Expand Up @@ -220,7 +221,7 @@ def _get_outlier_indices(
if str.isdigit(time_window): # if user gives '60' it will be considered as '60min'
warnings.warn(
f"Missing time unit in argument 'time_window' in remove_outliers function, assuming {time_window}min.",
UserWarning,
category=IndslUserWarning,
)
time_window = str(time_window) + "min"

Expand Down
5 changes: 3 additions & 2 deletions indsl/ts_utils/ts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from indsl.exceptions import UserRuntimeError, UserValueError
from indsl.type_check import check_types
from indsl.warnings import IndslUserWarning


_unit_in_ms_without_week = {"s": 1000, "m": 60000, "h": 3600000, "d": 86400000}
Expand Down Expand Up @@ -292,8 +293,8 @@ def time_parse(time_window: str = "60min", function_name: str = "") -> pd.Timede
"""
if str.isdigit(time_window): # if user gives '60' it will be considered as '60min'
warnings.warn(
f"Missing time unit in argument 'time_window' in {function_name} function, assuming {time_window}min.",
UserWarning,
f"Missing time unit in argument 'time_window' in {function_name} function, assuming {time_window} min.",
category=IndslUserWarning,
)
time_window = str(time_window) + "min"
try:
Expand Down
2 changes: 2 additions & 0 deletions indsl/warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class IndslUserWarning(UserWarning):
"""Warning that will be shown to the user."""
5 changes: 3 additions & 2 deletions tests/resample/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from indsl.resample.interpolate import interpolate
from indsl.warnings import IndslUserWarning

from ..generate_data import create_non_uniform_data, create_uniform_data, set_na_random_data

Expand All @@ -17,14 +18,14 @@
],
)
def test_too_few_data_warnings(input):
with pytest.warns(UserWarning):
with pytest.warns(IndslUserWarning):
interpolated_data = interpolate(input)
assert len(interpolated_data) == len(input)


# Test for all NaN data
def test_all_nan_data():
with pytest.warns(UserWarning):
with pytest.warns(IndslUserWarning):
test_data = create_uniform_data(np.ones(10) * np.nan)
interpolated_data = interpolate(test_data)
assert len(interpolated_data) == len(test_data)
Expand Down
6 changes: 3 additions & 3 deletions tests/signals/test_signal_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
perturb_timestamp,
sine_wave,
)

from indsl.warnings import IndslUserWarning

test_data_4index = [
("1975/5/9", "1975-5-10", pd.Timedelta("1s"), 1.0, pd.Timedelta("1 day")),
Expand All @@ -33,7 +33,7 @@

@pytest.mark.parametrize("bad_rate", [(-99999), (None)])
def test_wrong_sample_rate(bad_rate):
with pytest.warns(UserWarning):
with pytest.warns(IndslUserWarning):
_make_index(freq=bad_rate)


Expand Down Expand Up @@ -65,7 +65,7 @@ def test_index_generator(start_date, end_date, sample_rate, expected_mean, expec
test_data_warns,
)
def test_sample_freq_warns(start_date, end_date, sample_rate, expected_mean, expected_dt):
with pytest.warns(UserWarning):
with pytest.warns(IndslUserWarning):
# Test that the generated DatetimeIndex has the correct sampling frequency and duration based on input parameters
idx = _make_index(start=start_date, end=end_date, freq=sample_rate)
# Convert to time, where t=0 is idx[0]
Expand Down

0 comments on commit 898799c

Please sign in to comment.