Skip to content

Commit

Permalink
Adding changein_direction parameter setup in differen ways
Browse files Browse the repository at this point in the history
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
  • Loading branch information
irumata authored and facebook-github-bot committed Nov 3, 2023
1 parent b844595 commit 88349fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 21 additions & 4 deletions kats/detectors/cusum_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ def detector(self, **kwargs: Any) -> Sequence[CUSUMChangePoint]:
None means the middle of the time series.
change_directions: Optional; list<str>; 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<int, int>, 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions kats/tests/detectors/test_cusum_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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_()
Expand Down

0 comments on commit 88349fc

Please sign in to comment.