# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:tsfuse]
#     language: python
#     name: conda-env-tsfuse-py
# ---

# # Getting started

# + raw_mimetype="text/restructuredtext" active=""
# The example below shows the basic usage of TSFuse.
# -

# ## Data representation

# + raw_mimetype="text/restructuredtext" active=""
# As input, the system requires a dataset where each instance is a window that consists of time series and a label.
# -

# ### Time series

# + raw_mimetype="text/restructuredtext" active=""
# Time series are represented using a dictionary where each entry represents a univariate or multivariate time series. As an example, let's create a dictionary with two univariate time series:

# +
import pandas as pd
from tsfuse.data import Collection

X = {
    'x1': Collection(pd.DataFrame({
        'id':   [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5],
        'time': [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        'data': [1, 2, 3, 1, 2 ,3, 1, 2, 3, 3, 2, 1, 3, 2, 1, 3, 2, 1],
    })),
    'x2': Collection(pd.DataFrame({
        'id':   [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5],
        'time': [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        'data': [1, 2, 3, 1, 2 ,3, 1, 2, 3, 1, 2, 3, 1, 2 ,3, 1, 2, 3],
    })),
}

# + raw_mimetype="text/restructuredtext" active=""
# The two univariate time series are named `x1` and `x2` and each series is represented as a :class:`~tsfuse.data.Collection` object. Each ``Collection`` is initialized with a DataFrame that has three columns:
#
# - `id` which is the identifier of each instance, i.e., each window,
# - `time` which contains the time stamps,
# - `data` contains the time series data itself.
#
# For multivariate time series data, there can be multiple columns similar to the `data` column. For example, the data of a tri-axial accelerometer would have three columns `x`, `y`, `z` instead of `data` as it simultaneously measures the `x`, `y`, `z` acceleration.
# -

# ### Labels

# + raw_mimetype="text/restructuredtext" active=""
# There should be one target value for each window, so we create a Series where the index contains all unique `id` values of the time series data and the data consists of the labels:
# -

y = pd.Series(index=[0, 1, 2, 3, 4, 5], data=[0, 0, 0, 1, 1, 1])

# ## Feature construction

# + raw_mimetype="text/restructuredtext" active=""
# To construct features, TSFuse provides a :meth:`~tsfuse.construct` function which takes time series data `X` and target data `y` as input, and returns a DataFrame where each column corresponds to a feature. In addition, this function can return a computation graph which contains all transformation steps required to compute the features for new data:
# -

from tsfuse import construct
features, graph = construct(X, y, transformers='minimal', return_graph=True)

# + raw_mimetype="text/restructuredtext" active=""
# The DataFrame with the constructed features looks like this:
# -

features

# + raw_mimetype="text/restructuredtext" active=""
# And this is the corresponding computation graph:
# -

graph

# + raw_mimetype="text/restructuredtext" active=""
# To apply this computation graph, simply call :func:`~tsfuse.computation.Graph.transform` with a time series dictionary ``X`` as argument:
# -

graph.transform(X)

# ## Feature computation

# + raw_mimetype="text/restructuredtext" active=""
# Instead of automatically generating a computation graph using the :meth:`~tsfuse.construct` function, it is also possible to define a custom computation graph. To build such a graph, create a :class:`~tsfuse.computation.Graph` object as follows:
# -

from tsfuse.computation import Graph
graph = Graph()

# + raw_mimetype="text/restructuredtext" active=""
# First, the graph needs placeholders for the input data. These can be created by adding :class:`~tsfuse.computation.Input` nodes to the graph:
# -

from tsfuse.computation import Input
input1 = graph.add_node(Input('x1'))
input2 = graph.add_node(Input('x2'))

# + raw_mimetype="text/restructuredtext" active=""
# Then, add transformer nodes to create new data from the input data. Each transformer has one or more parent nodes which are used as input data. For example, we can define the features of cell [4] as follows:

# +
from tsfuse.transformers import *

graph.add_node(Min(Ratio(input1, input2)), i=2)
graph.add_node(Max(Ratio(input1, input2)), i=3)
graph.add_node(Mean(Ratio(input1, input2)), i=4)
graph.add_node(StandardDeviation(Ratio(input1, input2)), i=4)
graph.add_node(Variance(Ratio(input1, input2)), i=5)
graph.add_node(Skewness(Ratio(input1, input2)), i=6)
graph.add_node(Kurtosis(Ratio(input1, input2)), i=7)

graph

# + raw_mimetype="text/restructuredtext" active=""
# This is the same graph as the one we constructed previously, and we can use it in the same way to compute features:
# -

graph.transform(X)

StarcoderdataJupyterScriptsDedupFiltered