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

Anomaly detection - peak/trough detection and caching of matrix profle #1081

Merged
merged 11 commits into from
Sep 3, 2024

Conversation

ram-senth
Copy link
Member

@ram-senth ram-senth commented Aug 28, 2024

This PR has multiple changes:

  • Trough and peak detection using interquartile range
  • Caching of matrix profile
  • Changes to the detect end point to support historic time steps separated from current time steps of a time series

@ram-senth ram-senth force-pushed the anomaly_detection/ram/support_config_flags branch from f393fef to d773ddb Compare August 29, 2024 05:41
@ram-senth ram-senth force-pushed the anomaly_detection/ram/support_config_flags branch 6 times, most recently from 37507c2 to 17276d6 Compare August 29, 2024 22:10
@inject
@sentry_sdk.trace
def _combo_detect(
self, ts_with_history: TimeSeriesWithHistory, config: AnomalyDetectionConfig
Copy link
Member

Choose a reason for hiding this comment

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

Missing a = injected for DI?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, the config param is part of request payload.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do need to remove the @inject above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Comment on lines 137 to 144
transaction_name = (
"Stream AD for alert"
if isinstance(request.context, AlertInSeer)
else self._batch_detect(request.context)
else (
"Stream AD for timeseries with history"
if isinstance(request.context, TimeSeriesWithHistory)
else "Batch AD for timeseries"
)
Copy link
Member

Choose a reason for hiding this comment

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

nit: nested ternary makes this section somewhat hard to follow. Could this be done using a switch or an if/else?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, will change it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@ram-senth ram-senth force-pushed the anomaly_detection/ram/support_config_flags branch from 17276d6 to aa84a44 Compare September 3, 2024 17:14
Copy link
Contributor

Choose a reason for hiding this comment

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

will there be a follow up PR for the peak/trough logic?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I will tweak the peak/trough logic some more based on your experiments in a follow-up PR. Wanted to get this out as frontend work is dependent on the API change.

return timeseries
batch_detector = MPBatchAnomalyDetector()
anomalies = batch_detector.detect(convert_external_ts_to_internal(timeseries), config)
return timeseries, anomalies

@inject
@sentry_sdk.trace
def _online_detect(
Copy link
Contributor

Choose a reason for hiding this comment

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

i think it would be good to add some brief docstrings to these to clarify the context in which each is used

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

else:
transaction_name = "Batch AD for timeseries"

with sentry_sdk.start_transaction(op="task", name=transaction_name):
Copy link
Contributor

Choose a reason for hiding this comment

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

any particular reason why we want a separate transaction here?

Copy link
Member Author

Choose a reason for hiding this comment

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

This one endpoint caters to three different types of anomaly detection - stateless batch, stateless online and stateful alert based. Each serve a different use case and will have different performance characteristics. So I think it is worth tracking them as different transactions with separate names. Any potential problem with this approach?

if np.isnan(mp_dist):
return "none"
if mp_dist < threshold_lower:
return "none"
if mp_dist < threshold_upper:
return "anomaly_lower_confidence"
return "anomaly_higher_confidence"

def _adjust_flag_for_vicinity(
self, flag: AnomalyFlags, ts_value: float, context: npt.NDArray[np.float64]
Copy link
Contributor

Choose a reason for hiding this comment

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

this logic is pretty difficult to follow without a docstring - specifically what is context and how should i think about it? (i think i know the answer from talking to you but will probably be confused looking at this in 2 months)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@trillville trillville left a comment

Choose a reason for hiding this comment

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

code LGTM - do we feel confident about the peak/trough logic?

@ram-senth ram-senth force-pushed the anomaly_detection/ram/support_config_flags branch from aa84a44 to b72f2ca Compare September 3, 2024 17:54
@ram-senth
Copy link
Member Author

code LGTM - do we feel confident about the peak/trough logic?

The logic that is going in is incrementally better than what we had previously. It does need more tweaking. I will be following up on it immediately and have an updated version out before the internal release.

@ram-senth ram-senth merged commit 7a3ee44 into main Sep 3, 2024
11 checks passed
@ram-senth ram-senth deleted the anomaly_detection/ram/support_config_flags branch September 3, 2024 19:50
Copy link

sentry-io bot commented Sep 4, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ IntegrityError: (psycopg.errors.NotNullViolation) column "anomaly_type" of relation "dynamic_alert_time_series" c... 00a7fb4f4911_migration_py in upgrade View Issue
  • ‼️ Exception: Search for optimal window failed. app.store_data_endpoint View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants