Example #1
0
def arundo_adtk(data_path):
    data = get_data(data_path)
    data['date'] = data['timestamp'].apply(
        lambda i: datetime.fromtimestamp(i))  # 时间转换
    s_train = data[['date', 'value']]

    # 设置索引项
    s_train = s_train.set_index('date')
    s_train = validate_series(s_train)
    print(s_train)
    # plot(s_train)

    # STL分解+离群点检测
    steps = [("deseasonal", STLDecomposition(freq=20)),
             ("quantile_ad", QuantileAD(high=0.9997, low=0.005))]
    pipeline = Pipeline(steps)
    anomalies = pipeline.fit_detect(s_train)
    print(anomalies)
    # plot(s_train, anomaly_pred=anomalies, ap_color='red', ap_marker_on_curve=True)

    # 绘制检测结果]
    known_anomalies = data.loc[data['label'] == 1]
    known_anomalies = known_anomalies[['date', 'label']]
    known_anomalies = known_anomalies.set_index('date')
    known_anomalies = to_events(known_anomalies)
    print(known_anomalies)
    plot(s_train,
         anomaly_true=known_anomalies,
         anomaly_pred=anomalies,
         ap_color='red',
         ap_marker_on_curve=True,
         at_color="orange")

    plt.savefig(img_path + "arundo_adtk.png", dpi=1000)
    plt.show()
Example #2
0
def test_pipeline():
    s = pd.Series(
        [0, 1, 2, 3, 2, 1] * 10,
        index=pd.date_range(start="2017-1-1", periods=60, freq="D"),
    )
    my_pipe = Pipeline([
        (
            "deseasonal_residual",
            transformer.NaiveSeasonalDecomposition(freq=6),
        ),
        (
            "abs_residual",
            transformer.CustomizedTransformer1D(transform_func=abs),
        ),
        ("iqr_ad", detector.InterQuartileRangeAD(c=(None, 3))),
    ])

    my_pipe.fit_detect(s)
Example #3
0
def test_transformer_pipe():
    s = pd.Series(
        [0, 1, 2, 3, 2, 1] * 10,
        index=pd.date_range(start="2017-1-1", periods=60, freq="D"),
    )
    my_pipe = Pipenet(
        {
            "deseasonal_residual": {
                "model": transformer.ClassicSeasonalDecomposition(freq=6),
                "input": "original",
            },
            "abs_residual": {
                "model": transformer.CustomizedTransformer1D(
                    transform_func=abs
                ),
                "input": "deseasonal_residual",
            },
        }
    )
    with pytest.raises(RuntimeError, match="`fit_transform`"):
        my_pipe.fit_detect(s)
    my_pipe.fit(s)
    my_pipe.transform(s)
    my_pipe.fit_transform(s)

    my_pipe = Pipeline(
        [
            (
                "deseasonal_residual",
                transformer.ClassicSeasonalDecomposition(freq=6),
            ),
            (
                "abs_residual",
                transformer.CustomizedTransformer1D(transform_func=abs),
            ),
        ]
    )
    with pytest.raises(RuntimeError, match="`fit_transform`"):
        my_pipe.fit_detect(s)
    my_pipe.fit(s)
    my_pipe.transform(s)
    my_pipe.fit_transform(s)
print(len(anomalies_1))

##############################################################################################################

##Pipeline Definition to Combine PersistAD Detector with Double Rolling Aggregate Transformer
##Low Tolerance Model
steps = [
    (
        "DoubleRolling", DoubleRollingAggregate(agg="median", window=(4, 1))
    ),  ##Calcuates the Difference of Aggregated Metrics Between Two Sliding Windows.
    (
        "persist", PersistAD(agg="median", side="both", c=4)
    ),  #c is Factor Used to Determine the Bound of Normal Range Based on Historical Interquartile Range. Default Value is: 3.0.
]

pipeline = Pipeline(steps)

##############################################################################################################

##Training Phase
anomalies_2 = pipeline.fit_detect(
    s_train, return_list=True
)  ##fit_detect Function Trains the Model and Detects Anomalies in Training Set
plot(s_train,
     anomaly=anomalies_2,
     ts_markersize=1,
     anomaly_markersize=2,
     anomaly_tag="marker",
     anomaly_color='red')

for i in anomalies_2:
Example #5
0
def test_pipeline():
    s = pd.Series(
        [0, 1, 2, 3, 2, 1] * 5 + [0, 1, 2, 4, 2, 1] + [0, 1, 2, 3, 2, 1] * 4,
        index=pd.date_range(start="2017-1-1", periods=60, freq="D"),
    )
    my_pipe = Pipeline(
        [
            (
                "deseasonal_residual",
                transformer.ClassicSeasonalDecomposition(freq=6),
            ),
            (
                "abs_residual",
                transformer.CustomizedTransformer1D(transform_func=abs),
            ),
            ("ad", detector.QuantileAD(high=0.99)),
        ]
    )

    my_pipe.fit_detect(s)
    assert (
        my_pipe.score(
            s,
            pd.Series([0] * 33 + [1] + [0] * 26, index=s.index),
            scoring="recall",
        )
        == 1
    )
    assert (
        my_pipe.score(
            s,
            pd.Series([0] * 33 + [1] + [0] * 26, index=s.index),
            scoring="precision",
        )
        == 1
    )
    assert (
        my_pipe.score(
            s,
            pd.Series([0] * 33 + [1] + [0] * 26, index=s.index),
            scoring="f1",
        )
        == 1
    )
    assert (
        my_pipe.score(
            s,
            pd.Series([0] * 33 + [1] + [0] * 26, index=s.index),
            scoring="iou",
        )
        == 1
    )

    assert my_pipe.get_params() == {
        "deseasonal_residual": {"freq": 6, "trend": False},
        "abs_residual": {
            "fit_func": None,
            "fit_func_params": None,
            "transform_func": abs,
            "transform_func_params": None,
        },
        "ad": {"high": 0.99, "low": None},
    }