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

Refactor/anomaly detection api #1477

Merged
merged 106 commits into from
May 10, 2024
Merged

Conversation

julien12234
Copy link
Contributor

@julien12234 julien12234 commented Jan 10, 2023

Summary

Edit, @dennisbader (2024-05-10 15:19:00)

  • Improvements to the Anomaly Detection Module through major refactor. The refactor includes major performance optimization for the majority of the processes and improvements to the API, consistency, reliability, and the documentation. Some of these necessary changes come at the cost of breaking changes:
    • 🚀 Added an example notebook that showcases how to use Darts for Time Series Anomaly Detection
    • Added a new dataset for anomaly detection with the number of taxi passengers in New York from the year 2014 to 2015.
    • FittableWindowScorer (KMeans, PyOD, and Wasserstein Scorers) now accept any of darts "per-time" step metrics as difference function diff_fn.
    • ForecastingAnomalyModel is now much faster thanks to optimized historical forecasts to generate the prediction input for the scorers. We also added more control over the historical forecasts generation through additional parameters in all model methods.
    • 🔴 Breaking changes:
      • FittableWindowScorer (KMeans, PyOD, and Wasserstein Scorers) now expects diff_fn to be one of Darts "per-time" step metrics
      • ForecastingAnomalyModel : model is now enforced to be a GlobalForecastingModel
      • *.eval_accuracy(): (Aggregators, Detectors, Filtering/Forecasting Anomaly Models, Scorers)
        • renamed method to eval_metric():
        • renamed params actual_anomalies to anomalies, and anomaly_score to pred_scores
      • *.show_anomalies(): (Filtering/Forecasting Anomaly Models, Scorers)
        • renamed params actual_anomalies to anomalies
      • *.fit() (Filtering/Forecasting Anomaly Models)
        • renamed params actual_anomalies to anomalies
      • Scorer.*_from_prediction() (Scorers)
        • renamed method eval_accuracy_from_prediction() to eval_metric_from_prediction()
        • renamed params actual_series to series, and actual_anomalies to anomalies
      • darts.ad.utils.eval_accuracy_from_scores :
        • renamed function to eval_metric_from_scores
        • renamed params actual_anoamlies to anomalies, and anomaly_score to pred_scores
      • darts.ad.utils.eval_accuracy_from_binary_prediction :
        • renamed function to eval_metric_from_binary_prediction
        • renamed params actual_anoamlies to anomalies, and binary_pred_anomalies to pred_anomalies
      • darts.ad.utils.show_anomalies_from_scores:
        • renamed params series to actual_series, actual_anomalies to anomalies, model_output to pred_series, and anomaly_scores to pred_scores
  • Improvements to TimeSeries :
    • New method with_times_and_values(), which returns a new series with a new time index and new values but with identical columns and metadata as the series called from (static covariates, hierarchy).
    • New method slice_intersect_times(), which returns the sliced time index of a series, where the index has been intersected with another series.
    • Method with_values() now also acts on array-like values rather than only on numpy arrays.

Original description:

Some refactoring and upgrading of the anomaly detection API:

  • code refactoring in tests/ad
  • bug fix in utils.py for functions eval_accuracy_from_scores and eval_accuracy_from_binary_prediction
  • Fix bug in NormScorer when passing a probabilistic time series
  • Fix show_anomalies_from_scores when windows are not ordered in an increasing order
  • Refactor/vectorize the windowing (series -> tabular data) for scorers Wasserstein, Kmeans and PyOD

@codecov-commenter
Copy link

codecov-commenter commented Jan 10, 2023

Codecov Report

Attention: Patch coverage is 94.44444% with 43 lines in your changes are missing coverage. Please review.

Project coverage is 93.71%. Comparing base (a162664) to head (d24a24d).

❗ Current head d24a24d differs from pull request most recent head 8a0a54f. Consider uploading reports for the commit 8a0a54f to get more accurate results

Files Patch % Lines
darts/ad/scorers/scorers.py 96.24% 8 Missing ⚠️
darts/ad/detectors/detectors.py 89.39% 7 Missing ⚠️
darts/ad/anomaly_model/filtering_am.py 78.57% 6 Missing ⚠️
darts/ad/utils.py 95.48% 6 Missing ⚠️
darts/ad/anomaly_model/anomaly_model.py 93.97% 5 Missing ⚠️
darts/ad/aggregators/aggregators.py 90.69% 4 Missing ⚠️
darts/ad/anomaly_model/forecasting_am.py 87.09% 4 Missing ⚠️
darts/timeseries.py 92.85% 2 Missing ⚠️
darts/datasets/__init__.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1477      +/-   ##
==========================================
- Coverage   93.79%   93.71%   -0.08%     
==========================================
  Files         138      138              
  Lines       14285    14295      +10     
==========================================
- Hits        13398    13396       -2     
- Misses        887      899      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dennisbader dennisbader added this to In review in darts via automation Feb 7, 2023
Copy link
Contributor

@hrzn hrzn 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 this @julien12234 ! I only have minor comments. Would be great to merge sometime soon. You have a couple of merge conflicts to sort out first.

darts/ad/scorers/__init__.py Show resolved Hide resolved
darts/ad/scorers/difference_scorer.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Outdated Show resolved Hide resolved
darts/ad/scorers/kmeans_scorer.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Outdated Show resolved Hide resolved
darts/ad/scorers/scorers.py Show resolved Hide resolved
darts/ad/scorers/kmeans_scorer.py Outdated Show resolved Hide resolved
@piaz97 piaz97 self-assigned this Mar 23, 2023
Copy link
Collaborator

@madtoinou madtoinou left a comment

Choose a reason for hiding this comment

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

Some minor comments about the metric argument that should probably be a Literal, the order of the argument (notably for series and anomalie) might benefit from a slight change for some methods.

darts/ad/anomaly_model/anomaly_model.py Show resolved Hide resolved
darts/ad/anomaly_model/anomaly_model.py Outdated Show resolved Hide resolved
darts/ad/anomaly_model/anomaly_model.py Outdated Show resolved Hide resolved
darts/ad/anomaly_model/filtering_am.py Outdated Show resolved Hide resolved
darts/ad/anomaly_model/filtering_am.py Show resolved Hide resolved
darts/ad/utils.py Outdated Show resolved Hide resolved
darts/ad/utils.py Outdated Show resolved Hide resolved
darts/ad/utils.py Outdated Show resolved Hide resolved
darts/ad/utils.py Outdated Show resolved Hide resolved
darts/tests/ad/test_scorers.py Outdated Show resolved Hide resolved
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@dennisbader dennisbader merged commit 2633271 into master May 10, 2024
1 of 9 checks passed
darts automation moved this from In review to Done May 10, 2024
@dennisbader dennisbader deleted the refactor/anomaly_detection_API branch May 10, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
darts
Done
Development

Successfully merging this pull request may close these issues.

None yet

7 participants