Пример #1
0
def test_nonunique_output():
    with pytest.raises(ValueError, match="ambiguous"):
        Pipenet(
            {
                "deseasonal_residual": {
                    "model": (
                        transformer.ClassicSeasonalDecomposition(freq=6)
                    ),
                    "input": "original",
                },
                "abs_residual": {
                    "model": transformer.CustomizedTransformer1D(
                        transform_func=abs
                    ),
                    "input": "deseasonal_residual",
                },
                "iqr_ad": {
                    "model": detector.InterQuartileRangeAD(c=(None, 3)),
                    "input": "abs_residual",
                },
                "sign_check": {
                    "model": detector.ThresholdAD(high=0.0, low=-float("inf")),
                    "input": "deseasonal_residual",
                },
            }
        )
Пример #2
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)
Пример #3
0
def test_seasonal_transformer_shift():
    s_train = pd.Series(
        [0, 1, 2, 3, 4] * 8,
        index=pd.date_range(start="2017-1-1 00:05:00", periods=40, freq="min"),
    )

    model = transformer.ClassicSeasonalDecomposition(freq=5, trend=False)
    model.fit(s_train)

    s_test = pd.Series(
        [2, 3, 4.1, 0, 1, 2, 2.9, 4, 0, 1],
        index=pd.date_range(start="2017-1-1 00:02:00", periods=10, freq="min"),
    )

    pd.testing.assert_series_equal(
        model.transform(s_test),
        pd.Series(
            [0, 0, 0.1, 0, 0, 0, -0.1, 0, 0, 0],
            index=pd.date_range(
                start="2017-1-1 00:02:00", periods=10, freq="min"
            ),
        ),
        check_dtype=False,
    )

    s_test = pd.Series(
        [2, 3, 4.1, 0, 1, 2, 2.9, 4, 0, 1],
        index=pd.date_range(start="2017-1-1 00:52:00", periods=10, freq="min"),
    )

    pd.testing.assert_series_equal(
        model.transform(s_test),
        pd.Series(
            [0, 0, 0.1, 0, 0, 0, -0.1, 0, 0, 0],
            index=pd.date_range(
                start="2017-1-1 00:52:00", periods=10, freq="min"
            ),
        ),
        check_dtype=False,
    )
Пример #4
0
    detector.PersistAD(),
    detector.LevelShiftAD(),
    detector.VolatilityShiftAD(),
    detector.AutoregressionAD(),
    detector.SeasonalAD(freq=2),
    transformer.RollingAggregate(agg="median"),
    transformer.RollingAggregate(agg="quantile", agg_params={"q": 0.5}),
    transformer.DoubleRollingAggregate(agg="median"),
    transformer.DoubleRollingAggregate(
        agg="quantile", agg_params={"q": [0.1, 0.5, 0.9]}
    ),
    transformer.DoubleRollingAggregate(
        agg="hist", agg_params={"bins": [30, 50, 70]}
    ),
    transformer.StandardScale(),
    transformer.ClassicSeasonalDecomposition(freq=2),
]

one2many_models = [
    transformer.RollingAggregate(
        agg="quantile", agg_params={"q": [0.1, 0.5, 0.9]}
    ),
    transformer.RollingAggregate(
        agg="hist", agg_params={"bins": [20, 50, 80]}
    ),
    transformer.Retrospect(n_steps=3),
]

many2one_models = [
    detector.MinClusterDetector(KMeans(n_clusters=2)),
    detector.OutlierDetector(
Пример #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},
    }