Marcnuth / AnomalyDetection

Twitter's Anomaly Detection in Pure Python
Apache License 2.0
306 stars 76 forks source link

Fails if only_last=None #4

Closed hokiegeek2 closed 6 years ago

hokiegeek2 commented 6 years ago

First of all, great work on this! I am testing this out now for use in a streaming TS anomaly detection analytic for data center monitoring. A pure Python port of Twitter's uber cool algorithm is awesome.

Found an easy to fix error when only_last=None

anomaly_detect_ts.anomaly_detect_ts(raw_data, max_anoms=0.02, direction="both", plot=True) Traceback (most recent call last): File "", line 1, in File "/home/kjyost/development/git/AnomalyDetection/anomaly_detection/anomaly_detect_ts.py", line 257, in anomaly_detect_ts anom_pct = all_anoms.size / x_subset_single_day.size * 100 UnboundLocalError: local variable 'x_subset_single_day' referenced before assignment

Since it appears that all that's really being done with this outside of this if block is to determine if there are anomalies or not. Accordingly, I added this code:

if all_anoms.empty:
    if verbose:
        print('No anomalies detected.')

    return {
        'anoms': pd.Series(),
        'plot': None
    }

I tested this with only_last=day, hr, or None and it works as expected. Pull request coming soon.

--John

hokiegeek2 commented 6 years ago

Fixed in my pull request

Marcnuth commented 6 years ago

approved