From b05ff786d1b74e889c0e429edefba938168eeb94 Mon Sep 17 00:00:00 2001 From: Neringa Altanaite Date: Mon, 8 Jan 2024 15:22:30 +0100 Subject: [PATCH 1/4] [AH-2555] adding specific IndslUserWarning such that it would be easy for backend to know if the warning comes from Indsl and it should be shown to the user, or that it should be ignored. --- indsl/equipment/volume_vessel.py | 4 +++- indsl/resample/interpolate.py | 5 +++-- indsl/resample/resample.py | 4 +++- indsl/resample/resample_v1.py | 4 +++- indsl/signals/generator.py | 5 ++++- indsl/statistics/outliers_v1.py | 3 ++- indsl/ts_utils/ts_utils.py | 5 +++-- indsl/warnings.py | 5 +++++ tests/resample/test_interpolate.py | 5 +++-- tests/signals/test_signal_generator.py | 6 +++--- 10 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 indsl/warnings.py diff --git a/indsl/equipment/volume_vessel.py b/indsl/equipment/volume_vessel.py index ebd28440..b89786d6 100644 --- a/indsl/equipment/volume_vessel.py +++ b/indsl/equipment/volume_vessel.py @@ -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" @@ -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 diff --git a/indsl/resample/interpolate.py b/indsl/resample/interpolate.py index b4217281..ddf54abd 100644 --- a/indsl/resample/interpolate.py +++ b/indsl/resample/interpolate.py @@ -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 @@ -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 @@ -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 diff --git a/indsl/resample/resample.py b/indsl/resample/resample.py index 36e90151..68f7793a 100644 --- a/indsl/resample/resample.py +++ b/indsl/resample/resample.py @@ -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 @@ -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 diff --git a/indsl/resample/resample_v1.py b/indsl/resample/resample_v1.py index c4c0e0e4..93e843e6 100644 --- a/indsl/resample/resample_v1.py +++ b/indsl/resample/resample_v1.py @@ -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) @@ -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 diff --git a/indsl/signals/generator.py b/indsl/signals/generator.py index 22719dfa..80f6528f 100644 --- a/indsl/signals/generator.py +++ b/indsl/signals/generator.py @@ -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 @@ -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( diff --git a/indsl/statistics/outliers_v1.py b/indsl/statistics/outliers_v1.py index 57b419e5..e9e8d0a6 100644 --- a/indsl/statistics/outliers_v1.py +++ b/indsl/statistics/outliers_v1.py @@ -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) @@ -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" diff --git a/indsl/ts_utils/ts_utils.py b/indsl/ts_utils/ts_utils.py index 9945311d..39bcf717 100644 --- a/indsl/ts_utils/ts_utils.py +++ b/indsl/ts_utils/ts_utils.py @@ -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} @@ -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: diff --git a/indsl/warnings.py b/indsl/warnings.py new file mode 100644 index 00000000..f4e32a85 --- /dev/null +++ b/indsl/warnings.py @@ -0,0 +1,5 @@ +class IndslUserWarning(UserWarning): + """Warning that will be shown to the user.""" + + def __init__(self, message): + self.message = message diff --git a/tests/resample/test_interpolate.py b/tests/resample/test_interpolate.py index 7c3e167b..7156dc7a 100644 --- a/tests/resample/test_interpolate.py +++ b/tests/resample/test_interpolate.py @@ -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 @@ -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) diff --git a/tests/signals/test_signal_generator.py b/tests/signals/test_signal_generator.py index d56e8b18..4de51652 100644 --- a/tests/signals/test_signal_generator.py +++ b/tests/signals/test_signal_generator.py @@ -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")), @@ -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) @@ -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] From bed04a7f02e8f27512f2ce1b4ef0b0abf12e23c6 Mon Sep 17 00:00:00 2001 From: Neringa Altanaite Date: Mon, 8 Jan 2024 15:29:24 +0100 Subject: [PATCH 2/4] removing init --- indsl/warnings.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/indsl/warnings.py b/indsl/warnings.py index f4e32a85..a0b99830 100644 --- a/indsl/warnings.py +++ b/indsl/warnings.py @@ -1,5 +1,4 @@ class IndslUserWarning(UserWarning): """Warning that will be shown to the user.""" - def __init__(self, message): - self.message = message + pass From da291ddc8a9f349ff89bb22fdf2d058aa94efa56 Mon Sep 17 00:00:00 2001 From: Neringa Altanaite Date: Tue, 9 Jan 2024 12:47:58 +0100 Subject: [PATCH 3/4] Update indsl/warnings.py removing pass Co-authored-by: Dag Brattli --- indsl/warnings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/indsl/warnings.py b/indsl/warnings.py index a0b99830..9988499e 100644 --- a/indsl/warnings.py +++ b/indsl/warnings.py @@ -1,4 +1,3 @@ class IndslUserWarning(UserWarning): """Warning that will be shown to the user.""" - pass From 488be28c72d81e4192cc44f660d664c168300078 Mon Sep 17 00:00:00 2001 From: Neringa Altanaite Date: Tue, 9 Jan 2024 12:51:01 +0100 Subject: [PATCH 4/4] fix: linting --- indsl/warnings.py | 1 - 1 file changed, 1 deletion(-) diff --git a/indsl/warnings.py b/indsl/warnings.py index 9988499e..b05f0104 100644 --- a/indsl/warnings.py +++ b/indsl/warnings.py @@ -1,3 +1,2 @@ class IndslUserWarning(UserWarning): """Warning that will be shown to the user.""" -