Create an Anomaly Detection Workflow#
[8]:
# Import to be able to import python package from src
import sys
sys.path.insert(0, '../src')
[9]:
import pandas as pd
import numpy as np
import ontime as on
from IPython.display import Image
Prerequisite#
Install Prefect within your project
[10]:
!pip install prefect
Requirement already satisfied: prefect in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (3.1.11)
Requirement already satisfied: aiosqlite<1.0.0,>=0.17.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.20.0)
Requirement already satisfied: alembic<2.0.0,>=1.7.5 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.14.0)
Requirement already satisfied: apprise<2.0.0,>=1.1.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.9.1)
Requirement already satisfied: asyncpg<1.0.0,>=0.23 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.30.0)
Requirement already satisfied: click<8.2,>=8.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (8.1.7)
Requirement already satisfied: cryptography>=36.0.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (44.0.0)
Requirement already satisfied: dateparser<2.0.0,>=1.1.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.2.0)
Requirement already satisfied: docker<8.0,>=4.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (7.1.0)
Requirement already satisfied: graphviz>=0.20.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.20.3)
Requirement already satisfied: jinja2<4.0.0,>=3.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (3.1.4)
Requirement already satisfied: jinja2-humanize-extension>=0.4.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.4.0)
Requirement already satisfied: humanize<5.0.0,>=4.9.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (4.11.0)
Requirement already satisfied: pytz<2025,>=2021.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2024.2)
Requirement already satisfied: readchar<5.0.0,>=4.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (4.2.1)
Requirement already satisfied: sqlalchemy<3.0.0,>=2.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from sqlalchemy[asyncio]<3.0.0,>=2.0->prefect) (2.0.36)
Requirement already satisfied: typer!=0.12.2,<0.14.0,>=0.12.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.13.1)
Requirement already satisfied: anyio<5.0.0,>=4.4.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (4.7.0)
Requirement already satisfied: asgi-lifespan<3.0,>=1.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.1.0)
Requirement already satisfied: cachetools<6.0,>=5.3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (5.5.0)
Requirement already satisfied: cloudpickle<4.0,>=2.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (3.1.0)
Requirement already satisfied: coolname<3.0.0,>=1.0.4 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.2.0)
Requirement already satisfied: croniter<6.0.0,>=1.0.12 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (5.0.1)
Requirement already satisfied: exceptiongroup>=1.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.2.2)
Requirement already satisfied: fastapi<1.0.0,>=0.111.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.115.6)
Requirement already satisfied: fsspec>=2022.5.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2024.10.0)
Requirement already satisfied: griffe<2.0.0,>=0.49.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.5.4)
Requirement already satisfied: httpcore<2.0.0,>=1.0.5 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.0.7)
Requirement already satisfied: httpx!=0.23.2,>=0.23 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from httpx[http2]!=0.23.2,>=0.23->prefect) (0.28.1)
Requirement already satisfied: jsonpatch<2.0,>=1.32 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.33)
Requirement already satisfied: jsonschema<5.0.0,>=4.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (4.23.0)
Requirement already satisfied: opentelemetry-api<2.0.0,>=1.27.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.29.0)
Requirement already satisfied: orjson<4.0,>=3.7 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (3.10.13)
Requirement already satisfied: packaging<24.3,>=21.3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (24.2)
Requirement already satisfied: pathspec>=0.8.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.12.1)
Requirement already satisfied: pendulum<4,>=3.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (3.0.0)
Requirement already satisfied: prometheus-client>=0.20.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.21.1)
Requirement already satisfied: pydantic!=2.10.0,<3.0.0,>=2.7 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.10.4)
Requirement already satisfied: pydantic_core<3.0.0,>=2.12.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.27.2)
Requirement already satisfied: pydantic_extra_types<3.0.0,>=2.8.2 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.10.1)
Requirement already satisfied: pydantic_settings>2.2.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.7.1)
Requirement already satisfied: python_dateutil<3.0.0,>=2.8.2 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (2.9.0.post0)
Requirement already satisfied: python-slugify<9.0,>=5.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (8.0.4)
Requirement already satisfied: python-socks<3.0,>=2.5.3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from python-socks[asyncio]<3.0,>=2.5.3->prefect) (2.6.1)
Requirement already satisfied: pyyaml<7.0.0,>=5.4.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (6.0.2)
Requirement already satisfied: rfc3339-validator<0.2.0,>=0.1.4 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.1.4)
Requirement already satisfied: rich<14.0,>=11.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (13.9.4)
Requirement already satisfied: ruamel.yaml>=0.17.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.18.10)
Requirement already satisfied: sniffio<2.0.0,>=1.3.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (1.3.1)
Requirement already satisfied: toml>=0.10.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.10.2)
Requirement already satisfied: typing_extensions<5.0.0,>=4.5.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (4.12.2)
Requirement already satisfied: ujson<6.0.0,>=5.8.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (5.10.0)
Requirement already satisfied: uvicorn!=0.29.0,>=0.14.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (0.34.0)
Requirement already satisfied: websockets<14.0,>=10.4 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from prefect) (13.1)
Requirement already satisfied: Mako in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from alembic<2.0.0,>=1.7.5->prefect) (1.3.8)
Requirement already satisfied: idna>=2.8 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from anyio<5.0.0,>=4.4.0->prefect) (3.10)
Requirement already satisfied: certifi in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from apprise<2.0.0,>=1.1.0->prefect) (2024.8.30)
Requirement already satisfied: requests in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from apprise<2.0.0,>=1.1.0->prefect) (2.32.3)
Requirement already satisfied: requests-oauthlib in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from apprise<2.0.0,>=1.1.0->prefect) (2.0.0)
Requirement already satisfied: markdown in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from apprise<2.0.0,>=1.1.0->prefect) (3.7)
Requirement already satisfied: async-timeout>=4.0.3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from asyncpg<1.0.0,>=0.23->prefect) (5.0.1)
Requirement already satisfied: cffi>=1.12 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from cryptography>=36.0.1->prefect) (1.17.1)
Requirement already satisfied: regex!=2019.02.19,!=2021.8.27 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from dateparser<2.0.0,>=1.1.1->prefect) (2024.11.6)
Requirement already satisfied: tzlocal in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from dateparser<2.0.0,>=1.1.1->prefect) (5.2)
Requirement already satisfied: urllib3>=1.26.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from docker<8.0,>=4.0->prefect) (2.2.3)
Requirement already satisfied: starlette<0.42.0,>=0.40.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from fastapi<1.0.0,>=0.111.0->prefect) (0.41.3)
Requirement already satisfied: colorama>=0.4 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from griffe<2.0.0,>=0.49.0->prefect) (0.4.6)
Requirement already satisfied: h11<0.15,>=0.13 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from httpcore<2.0.0,>=1.0.5->prefect) (0.14.0)
Requirement already satisfied: h2<5,>=3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from httpx[http2]!=0.23.2,>=0.23->prefect) (4.1.0)
Requirement already satisfied: MarkupSafe>=2.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jinja2<4.0.0,>=3.0.0->prefect) (3.0.2)
Requirement already satisfied: jsonpointer>=1.9 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jsonpatch<2.0,>=1.32->prefect) (3.0.0)
Requirement already satisfied: attrs>=22.2.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jsonschema<5.0.0,>=4.0.0->prefect) (24.2.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jsonschema<5.0.0,>=4.0.0->prefect) (2024.10.1)
Requirement already satisfied: referencing>=0.28.4 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jsonschema<5.0.0,>=4.0.0->prefect) (0.35.1)
Requirement already satisfied: rpds-py>=0.7.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from jsonschema<5.0.0,>=4.0.0->prefect) (0.22.3)
Requirement already satisfied: deprecated>=1.2.6 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from opentelemetry-api<2.0.0,>=1.27.0->prefect) (1.2.15)
Requirement already satisfied: importlib-metadata<=8.5.0,>=6.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from opentelemetry-api<2.0.0,>=1.27.0->prefect) (8.5.0)
Requirement already satisfied: tzdata>=2020.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from pendulum<4,>=3.0.0->prefect) (2024.2)
Requirement already satisfied: time-machine>=2.6.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from pendulum<4,>=3.0.0->prefect) (2.16.0)
Requirement already satisfied: annotated-types>=0.6.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from pydantic!=2.10.0,<3.0.0,>=2.7->prefect) (0.7.0)
Requirement already satisfied: python-dotenv>=0.21.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from pydantic_settings>2.2.1->prefect) (1.0.1)
Requirement already satisfied: six>=1.5 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from python_dateutil<3.0.0,>=2.8.2->prefect) (1.17.0)
Requirement already satisfied: text-unidecode>=1.3 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from python-slugify<9.0,>=5.0->prefect) (1.3)
Requirement already satisfied: markdown-it-py>=2.2.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from rich<14.0,>=11.0->prefect) (3.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from rich<14.0,>=11.0->prefect) (2.18.0)
Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from ruamel.yaml>=0.17.0->prefect) (0.2.12)
Requirement already satisfied: greenlet!=0.4.17 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from sqlalchemy<3.0.0,>=2.0->sqlalchemy[asyncio]<3.0.0,>=2.0->prefect) (3.1.1)
Requirement already satisfied: shellingham>=1.3.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from typer!=0.12.2,<0.14.0,>=0.12.0->prefect) (1.5.4)
Requirement already satisfied: pycparser in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=36.0.1->prefect) (2.22)
Requirement already satisfied: wrapt<2,>=1.10 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from deprecated>=1.2.6->opentelemetry-api<2.0.0,>=1.27.0->prefect) (1.17.0)
Requirement already satisfied: hyperframe<7,>=6.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from h2<5,>=3->httpx[http2]!=0.23.2,>=0.23->prefect) (6.0.1)
Requirement already satisfied: hpack<5,>=4.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from h2<5,>=3->httpx[http2]!=0.23.2,>=0.23->prefect) (4.0.0)
Requirement already satisfied: zipp>=3.20 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from importlib-metadata<=8.5.0,>=6.0->opentelemetry-api<2.0.0,>=1.27.0->prefect) (3.21.0)
Requirement already satisfied: mdurl~=0.1 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich<14.0,>=11.0->prefect) (0.1.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from requests->apprise<2.0.0,>=1.1.0->prefect) (3.4.0)
Requirement already satisfied: oauthlib>=3.0.0 in /home/benjy/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages (from requests-oauthlib->apprise<2.0.0,>=1.1.0->prefect) (3.2.2)
[notice] A new release of pip is available: 24.1 -> 24.3.1
[notice] To update, run: pip install --upgrade pip
Generation of random time series#
[11]:
ts = on.generators.random_walk().generate(start=pd.Timestamp('2022-01-01'), end=pd.Timestamp('2022-12-31'))
Create a Prefect Flow and Tasks#
[12]:
from prefect import task, flow, get_run_logger
from ontime.context import common
@task
def task_detect(ts):
det = on.detectors.quantile(low_quantile=0.1, high_quantile=0.9)
det.fit(ts)
return det.detect(ts)
@task
def task_predict(ts):
model = common.GenericPredictor()
model.fit(ts)
return model.predict(48)
@flow
def detection_flow(ts, log_prints=True):
ts_ano = task_detect(ts)
ts_pred = task_predict(ts)
ts_pred_ano = task_detect(ts_pred)
return ts_ano, ts_pred, ts_pred_ano
Run the flow#
[14]:
ts_ano, ts_pred, ts_pred_ano = detection_flow(ts)
10:05:21.293 | INFO | Flow run 'tangible-elephant' - Beginning flow run 'tangible-elephant' for flow 'detection-flow'
10:05:21.381 | INFO | Task run 'task_detect-48e' - Finished in state Completed()
10:05:22.481 | INFO | Task run 'task_predict-70b' - Finished in state Completed()
10:05:22.556 | INFO | Task run 'task_detect-33f' - Finished in state Completed()
10:05:22.611 | INFO | Flow run 'tangible-elephant' - Finished in state Completed()
Show results#
[18]:
(
on.Plot()
.add(on.marks.line, ts.rename({"random_walk": "truth"}))
.add(on.marks.line, ts_ano.rename({"random_walk": "anomalies"}))
.add(on.marks.line, ts_pred.rename({"random_walk": "predictions"}))
.add(on.marks.line, ts_pred_ano.rename({"random_walk": "predicted anomalies"}))
.properties(width=500)
.show()
)
[18]: