From 88349fc757944fb5bcd25bcf03a4c5f55717e2bb Mon Sep 17 00:00:00 2001 From: Nick Knize Date: Fri, 3 Nov 2023 13:18:31 -0700 Subject: [PATCH] Adding changein_direction parameter setup in differen ways Summary: We are adding abilities to setup both direction in cusum not only as None, but also as "both" or empty string or list. It is the same by logic and will be used when passing this value throw thrift as string. I've also added ability to setup by list or string. This maybe helpful. No actual changes in algorithm or somewere. I've also modified test to use both old and new type of parameter. Differential Revision: D48919825 fbshipit-source-id: 4b730b815848ab22e995d407058c78e504a45065 --- kats/detectors/cusum_detection.py | 25 ++++++++++++++++---- kats/tests/detectors/test_cusum_detection.py | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/kats/detectors/cusum_detection.py b/kats/detectors/cusum_detection.py index 579a81ca..c7841ab0 100644 --- a/kats/detectors/cusum_detection.py +++ b/kats/detectors/cusum_detection.py @@ -525,7 +525,8 @@ def detector(self, **kwargs: Any) -> Sequence[CUSUMChangePoint]: None means the middle of the time series. change_directions: Optional; list; a list contain either or both 'increase' and 'decrease' to specify what type of change - want to detect. + want to detect, to point both directions can be also setted up + as empty list ([]), None or ["both"] interest_window: Optional; list, a list containing the start and end of interest windows where we will look for change points. Note that llr will still be calculated using all data @@ -572,8 +573,15 @@ def detector(self, **kwargs: Any) -> Sequence[CUSUMChangePoint]: ts = self.data.value.to_numpy() ts = ts.astype("float64") changes_meta = {} - - if change_directions is None: + if type(change_directions) is str: + change_directions = [change_directions] + + if ( + change_directions is None + or change_directions == [""] + or change_directions == ["both"] + or change_directions == [] + ): change_directions = ["increase", "decrease"] for change_direction in change_directions: @@ -1024,7 +1032,16 @@ def detector(self, **kwargs: Any) -> List[List[CUSUMChangePoint]]: ts_multi = ts_multi[:, np.newaxis] changes_meta_multi = {} - if change_directions is None: + + if type(change_directions) is str: + change_directions = [change_directions] + + if ( + change_directions is None + or change_directions == [""] + or change_directions == ["both"] + or change_directions == [] + ): change_directions = ["increase", "decrease"] for change_direction in change_directions: diff --git a/kats/tests/detectors/test_cusum_detection.py b/kats/tests/detectors/test_cusum_detection.py index e4dceca0..e9afbd89 100644 --- a/kats/tests/detectors/test_cusum_detection.py +++ b/kats/tests/detectors/test_cusum_detection.py @@ -95,7 +95,7 @@ def setUp(self) -> None: self.periodicity * self.total_cycles - 1, ], magnitude_quantile=1, - change_directions=["increase", "decrease"], + change_directions="both", delta_std_ratio=0, ) self.season_metadata = self.season_inc_trend_change_points[0] @@ -542,7 +542,7 @@ def setUp(self) -> None: ).detector() self.dec_change_points_int_window = CUSUMDetector( TimeSeriesData(df[["decrease", "time"]]) - ).detector(change_directions=["decrease"], interest_window=(35, 55)) + ).detector(change_directions="decrease", interest_window=(35, 55)) timeseries = TimeSeriesData(df) change_points_vectorized_ = VectorizedCUSUMDetector(timeseries).detector_()