Skip to content
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

[ENH] Implementation of LOF using PyODAdapter #2209

Merged
merged 57 commits into from
Nov 24, 2024

Conversation

aryanpola
Copy link
Contributor

@aryanpola aryanpola commented Oct 16, 2024

Reference Issues/PRs

Fixes #2109

What does this implement/fix? Explain your changes.

Implemented Local Outlier Factor using PyODadapter

PR checklist

For all contributions
  • I've added myself to the list of contributors. Alternatively, you can use the @all-contributors bot to do this for you.
  • The PR title starts with either [ENH], [MNT], [DOC], [BUG], [REF], [DEP] or [GOV] indicating whether the PR topic is related to enhancement, maintenance, documentation, bugs, refactoring, deprecation or governance.

@aeon-actions-bot aeon-actions-bot bot added anomaly detection Anomaly detection package enhancement New feature, improvement request or other non-bug code enhancement labels Oct 16, 2024
@aeon-actions-bot
Copy link
Contributor

Thank you for contributing to aeon

I have added the following labels to this PR based on the title: [ $\color{#FEF1BE}{\textsf{enhancement}}$ ].
I have added the following labels to this PR based on the changes made: [ $\color{#6F6E8D}{\textsf{anomaly detection}}$ ]. Feel free to change these if they do not properly represent the PR.

The Checks tab will show the status of our automated tests. You can click on individual test runs in the tab or "Details" in the panel below to see more information if there is a failure.

If our pre-commit code quality check fails, any trivial fixes will automatically be pushed to your PR unless it is a draft.

Don't hesitate to ask questions on the aeon Slack channel if you have any.

PR CI actions

These checkboxes will add labels to enable/disable CI functionality for this PR. This may not take effect immediately, and a new commit may be required to run the new configuration.

  • Run pre-commit checks for all files
  • Run mypy typecheck tests
  • Run all pytest tests and configurations
  • Run all notebook example tests
  • Run numba-disabled codecov tests
  • Stop automatic pre-commit fixes (always disabled for drafts)
  • Disable numba cache loading
  • Push an empty commit to re-run CI checks

@aryanpola
Copy link
Contributor Author

@CodeLionX some tests ran with no window_size parameter causing errors, would it be a good idea to set the default window size if not provided, to be the same as that of stride?

Copy link
Member

@SebastianSchmidl SebastianSchmidl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! The window_size and stride parameters should have a proper default that should get picked up during the tests. I cannot explain why this is not happening in your tests.

This already looks good. However, LOF is special because of its different calling conventions for unsupervised (outlier detection) and semi-supervised (novelty detection) problems. Please refer to my comment below on how to best integrate this into our API.

The tests in their current version are not very helpful. They mostly test the functionality of the PyODAdapter. Similar to the tests in COPOD (#2202 (comment)), I would like to test that the integration into aeon works as expected. We should also test that the metric-parameter works for our distance implementations and that the different calling conventions (unsupervised and semi-supervised) work correctly.

@aryanpola
Copy link
Contributor Author

@CodeLionX The output values from aeon_lof and pyod_lof don't match. I've tried normalizing the series but the error persists.

FAILED aeon/anomaly_detection/tests/test_lof.py::test_lof_compare_with_pyod_direct - AssertionError: 
Not equal to tolerance rtol=1e-05, atol=0

Mismatched elements: 80 / 80 (100%)
Max absolute difference: 0.11725756
Max relative difference: 0.06374909
 x: array([1.516172, 1.018181, 1.222005, 1.838367, 1.574194, 1.062766,
       1.206536, 0.994702, 0.994427, 1.020478, 0.952964, 1.365051,
       1.122292, 0.956126, 1.028726, 1.004674, 1.378998, 0.982227,...
 y: array([1.433783, 1.000109, 1.184898, 1.728743, 1.486901, 1.06169 ,
       1.170488, 0.986849, 0.976589, 1.001037, 0.951811, 1.295437,
       1.097627, 0.952606, 1.020956, 1.000109, 1.308204, 0.981894,...

@aryanpola
Copy link
Contributor Author

@CodeLionX Hey! I am unable to resolve these conflicts. I have tried everything, but I am stuck. Please tell me how to proceed from here.

@SebastianSchmidl
Copy link
Member

Hey,

I cannot write directly to your branch, but resolving this should not be that difficult.

Don't you use pre-commit locally? It looks like you can push changes that do not follow our coding standards, and the changes by our pipeline are creating the issues for you. Please enable pre-commit in your dev box (pre-commit install).

Once, you have this set up, make the following changes:

--- a/aeon/anomaly_detection/__init__.py
+++ b/aeon/anomaly_detection/__init__.py
@@ -1,14 +1,15 @@
 """Time Series Anomaly Detection."""
 
 __all__ = [
-    "LOF" "DWT_MLEAD",
+    "DWT_MLEAD",
+    "IsolationForest",
     "KMeansAD",
+    "LeftSTAMPi",
+    "LOF",
     "MERLIN",
-    "STRAY",
     "PyODAdapter",
     "STOMP",
-    "LeftSTAMPi",
-    "IsolationForest",
+    "STRAY",
 ]
--- a/aeon/anomaly_detection/tests/test_lof.py
+++ b/aeon/anomaly_detection/tests/test_lof.py
@@ -235,8 +235,8 @@ def test_lof_semi_supervised():
     # Ensure shapes and types
     assert scores_aeon.shape == (20,)
     assert scores_pyod.shape == (20,)
-    assert scores_aeon.dtype == np.float_
-    assert scores_pyod.dtype == np.float_
+    assert scores_aeon.dtype == np.float64
+    assert scores_pyod.dtype == np.float64

Commit, and push again.

@MatthewMiddlehurst
Copy link
Member

Seems to be a conflict here. Not sure if the unresolved conflict needs any action or not.

@SebastianSchmidl
Copy link
Member

Seems to be a conflict here. Not sure if the unresolved conflict needs any action or not.

Fixed.

Were just some duplicated entries in __all__, still ready to merge from my side.

@MatthewMiddlehurst MatthewMiddlehurst merged commit 224cdc1 into aeon-toolkit:main Nov 24, 2024
15 checks passed
@aryanpola aryanpola deleted the lof branch December 6, 2024 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
anomaly detection Anomaly detection package enhancement New feature, improvement request or other non-bug code enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ENH] Add PyODAdapter-implementation for LOF
3 participants