Skip to content

Commit

Permalink
updated code as requested by moderators
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramana-Raja committed Jan 17, 2025
1 parent 7615487 commit baacc18
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
21 changes: 14 additions & 7 deletions aeon/anomaly_detection/_idk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np

from aeon.anomaly_detection.base import BaseAnomalyDetector


from aeon.utils.windowing import reverse_windowing
from typing import Optional
class IDK(BaseAnomalyDetector):
"""IDK² and s-IDK² anomaly detector.
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(
width: int = 1,
t: int = 100,
sliding: bool = False,
random_state: int = None,
random_state: Optional[int] = None,
) -> None:
self.psi1 = psi1
self.psi2 = psi2
Expand Down Expand Up @@ -175,9 +175,16 @@ def _idk_square_sliding(self, X, rng):
def _predict(self, X):
rng = np.random.default_rng(self.random_state)
if self.sliding:
return self._idk_square_sliding(X, rng)
return self._idk_t(X, rng)

sliding_output = self._idk_square_sliding(X, rng)
reversed_output = reverse_windowing(
y=sliding_output,
window_size=self.width,
stride=1,
reduction=np.nanmean,
)
return reversed_output
else:
return self._idk_t(X, rng)
@classmethod
def _get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Expand All @@ -199,4 +206,4 @@ def _get_test_params(cls, parameter_set="default"):
"psi1": 8,
"psi2": 2,
"width": 1,
}
}
51 changes: 33 additions & 18 deletions aeon/anomaly_detection/tests/test_idk.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
"""Tests for the IDK Class."""

import numpy as np

from numpy.testing import assert_allclose
from aeon.anomaly_detection import IDK

def test_idk_univariate_basic():
"""Test IDK on basic univariate data."""
rng = np.random.default_rng(seed=2)
series = rng.normal(size=(100,))
series[50:58] -= 5

ad = IDK(psi1=8, psi2=2, width=1, random_state=2)
pred = ad.fit_predict(series)

assert pred.shape == (100,)
assert pred.dtype == np.float64
assert 50 <= np.argmax(pred) <= 58

def test_idk_univariate():
"""Test IDK on univariate data."""
def test_idk_univariate_sliding():
"""Test IDK with sliding window on univariate data."""
rng = np.random.default_rng(seed=2)
series = rng.normal(size=(100,))
series[50:58] -= 5

ad_sliding = IDK(psi1=16, psi2=4, width=10, sliding=True, random_state=1)
pred_sliding = ad_sliding.fit_predict(series)

assert pred_sliding.shape == (100,)
assert pred_sliding.dtype == np.float64
assert 60 <= np.argmax(pred_sliding) <= 80

def test_idk_univariate_custom_series():
"""Test IDK on a custom univariate series with assert_allclose."""
series1 = np.array(
[
0.18905338,
-0.52274844,
-0.41306354,
-2.44146738,
1.79970738,
1.14416587 - 0.32542284,
1.14416587,
- 0.32542284,
0.77380659,
0.28121067,
-0.55382284,
]
)
y = [0.52333333, 0.19, 0.52333333]
ad = IDK(psi1=8, psi2=2, width=1, random_state=2)
pred = ad.fit_predict(series)
ad_sliding = IDK(psi1=16, psi2=4, width=10, sliding=True, random_state=1)
pred_sliding = ad_sliding.fit_predict(series)
ad_2 = IDK(psi1=4, psi2=2, width=3, t=10)
expected = [0.52333333, 0.19, 0.52333333]

ad_2 = IDK(psi1=4, psi2=2, width=3, t=10,random_state=2)
pred2 = ad_2.fit_predict(series1)
mae = np.mean(np.abs(y - pred2))

assert pred.shape == (100,)
assert pred.dtype == np.float64
assert 50 <= np.argmax(pred) <= 58
assert pred_sliding.shape == (91,)
assert pred_sliding.dtype == np.float64
assert 60 <= np.argmax(pred_sliding) <= 80
assert mae < 0.3
assert pred2.shape == (3,)
assert pred2.dtype == np.float64
assert_allclose(pred2, expected, atol=0.01)

0 comments on commit baacc18

Please sign in to comment.