Esempio n. 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",
                },
            }
        )
Esempio n. 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)
Esempio n. 3
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)
Esempio n. 4
0
def test_detector_return_intermediate():
    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([
        {
            "name": "deseasonal_residual",
            "model": (transformer.NaiveSeasonalDecomposition(freq=6)),
            "input": "original",
        },
        {
            "name": "abs_residual",
            "model": transformer.CustomizedTransformer1D(transform_func=abs),
            "input": "deseasonal_residual",
        },
        {
            "name": "iqr_ad",
            "model": detector.InterQuartileRangeAD(c=(None, 3)),
            "input": "abs_residual",
        },
        {
            "name": "sign_check",
            "model": detector.ThresholdAD(high=0.0, low=-float("inf")),
            "input": "deseasonal_residual",
        },
        {
            "name": "and",
            "model": aggregator.AndAggregator(),
            "input": ["iqr_ad", "sign_check"],
        },
    ])
    result = my_pipe.fit_detect(s, return_intermediate=True)
    assert set(result.keys()) == {
        "original",
        "deseasonal_residual",
        "abs_residual",
        "iqr_ad",
        "sign_check",
        "and",
    }
Esempio n. 5
0
def test_skip_fit():
    s = pd.Series(
        [0, 1, 2, 3, 2, 1] * 10,
        index=pd.date_range(start="2017-1-1", periods=60, freq="D"),
    )

    deseasonal_residual = transformer.NaiveSeasonalDecomposition(freq=6)

    my_pipe = Pipenet([
        {
            "name": "deseasonal_residual",
            "model": deseasonal_residual,
            "input": "original",
        },
        {
            "name": "abs_residual",
            "model": transformer.CustomizedTransformer1D(transform_func=abs),
            "input": "deseasonal_residual",
        },
        {
            "name": "iqr_ad",
            "model": detector.InterQuartileRangeAD(c=(None, 3)),
            "input": "abs_residual",
        },
        {
            "name": "sign_check",
            "model": detector.ThresholdAD(high=0.0, low=-float("inf")),
            "input": "deseasonal_residual",
        },
        {
            "name": "and",
            "model": aggregator.AndAggregator(),
            "input": ["iqr_ad", "sign_check"],
        },
    ])
    with pytest.raises(RuntimeError):
        my_pipe.fit_detect(s, skip_fit=["deseasonal_residual"])
    my_pipe.fit_detect(s)
Esempio n. 6
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},
    }