Skip to content

[ENH] implemented anomaly detection _fit_predict override output checks #2818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions aeon/testing/estimator_checking/_yield_anomaly_detection_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,20 @@ def check_anomaly_detector_learning_types(estimator, datatype):

def check_anomaly_detector_output(estimator, datatype):
"""Test the anomaly detector output on valid data."""
estimator = _clone_estimator(estimator)
estimator1 = _clone_estimator(estimator, random_state=42)
estimator2 = _clone_estimator(estimator, random_state=42)
estimator_class = type(estimator)

estimator.fit(
estimator1.fit(
FULL_TEST_DATA_DICT[datatype]["train"][0],
FULL_TEST_DATA_DICT[datatype]["train"][1],
)

y_pred = estimator.predict(FULL_TEST_DATA_DICT[datatype]["test"][0])
y_pred = estimator1.predict(FULL_TEST_DATA_DICT[datatype]["test"][0])
assert isinstance(y_pred, np.ndarray)
assert len(y_pred) == FULL_TEST_DATA_DICT[datatype]["test"][0].shape[1]

ot = estimator.get_tag("anomaly_output_type")
ot = estimator1.get_tag("anomaly_output_type")
if ot == "anomaly_scores":
assert np.issubdtype(y_pred.dtype, np.floating) or np.issubdtype(
y_pred.dtype, np.integer
Expand All @@ -100,3 +102,18 @@ def check_anomaly_detector_output(estimator, datatype):
), "y_pred must contain only 0s, 1s, True, or False"
else:
raise ValueError(f"Unknown anomaly output type: {ot}")

# check _fit_predict output is same as fit().predict()
if "_fit_predict" not in estimator_class.__dict__:
return

expected_output = estimator1.predict(FULL_TEST_DATA_DICT[datatype]["train"][0])

fit_predict_output = estimator2.fit_predict(
FULL_TEST_DATA_DICT[datatype]["train"][0],
FULL_TEST_DATA_DICT[datatype]["train"][1],
)

assert np.allclose(
fit_predict_output, expected_output
), "outputs of _fit_predict() does not match fit().predict()"
Loading