Processing#

[1]:
# Import to be able to import python package from src
import sys
sys.path.insert(0, '../../src')
[2]:
import pandas as pd
import numpy as np
import ontime as on
from ontime.core.plotting._marks.area import area

Generation of random time series#

[3]:
ts = on.generators.random_walk().generate(start=pd.Timestamp('2022-01-01'), end=pd.Timestamp('2022-12-31'))

Apply function on the whole time series#

with Lambda function

[4]:
add_two = on.processors.mapper(lambda x : x + 2)
new_ts = add_two.process(ts)
(new_ts - ts).head()
[4]:
<TimeSeries (DataArray) (time: 5, component: 1, sample: 1)> Size: 40B
array([[[2.]],

       [[2.]],

       [[2.]],

       [[2.]],

       [[2.]]])
Coordinates:
  * time       (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * component  (component) object 8B 'random_walk'
Dimensions without coordinates: sample
Attributes:
    static_covariates:  None
    hierarchy:          None

with normal function

[5]:
def add_2(x):
    return x + 2

add_two = on.processors.mapper(add_2)
new_ts = add_two.process(ts)
(new_ts - ts).head()
[5]:
<TimeSeries (DataArray) (time: 5, component: 1, sample: 1)> Size: 40B
array([[[2.]],

       [[2.]],

       [[2.]],

       [[2.]],

       [[2.]]])
Coordinates:
  * time       (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * component  (component) object 8B 'random_walk'
Dimensions without coordinates: sample
Attributes:
    static_covariates:  None
    hierarchy:          None

Apply Function on Windows of the Time Series#

[6]:
mean = on.processors.windower({
    'function': 'mean',
    'mode': 'rolling',
    'window': 10
})
[7]:
new_ts = on.TimeSeries.from_darts(mean.process(ts))
[8]:
ts.plot(height=50)
[8]:
[9]:
new_ts.plot(height=50)
[9]:

Split Time Series in defined durations of e.g. day, week, month, year#

[10]:
ts = on.generators.random_walk().generate(start=pd.Timestamp('2022-01-01'), end=pd.Timestamp('2022-12-31'))
[11]:
ts.plot(height=50, width=600)
[11]:

Split by month#

All offset aliases can be used to make different split length (https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases)

[12]:
seq = ts.split_by_period('M')
/Users/fred.montet/src/vast-ch/ontime/src/ontime/core/time_series/time_series.py:57: FutureWarning: 'M' is deprecated and will be removed in a future version, please use 'ME' instead.
  splits_df = [g for n, g in df.groupby(pd.Grouper(freq=period))]
[13]:
len(seq)
[13]:
12

Group the splits#

[14]:
ts_g = ts.group_splits(seq)
[15]:
ts_g.head()
[15]:
<TimeSeries (DataArray) (time: 5, component: 1, sample: 1)> Size: 40B
array([[[0.10963182]],

       [[0.21330887]],

       [[1.29061992]],

       [[0.42607427]],

       [[1.33176918]]])
Coordinates:
  * time       (time) datetime64[ns] 40B 2022-01-01 2022-01-02 ... 2022-01-05
  * component  (component) object 8B 'random_walk'
Dimensions without coordinates: sample
Attributes:
    static_covariates:  None
    hierarchy:          None

Compute Correlation Through Time#

Load some data from the Energy dataset in Darts

[16]:
from darts.datasets import EnergyDataset
ts = EnergyDataset().load()

Get a few columns and samples

[17]:
cols = ['generation biomass', 'generation solar', 'generation nuclear']
ts = ts[cols][0:1000]

Compute correlations within a daily window

[18]:
correlation = on.processors.correlation('1D')
[19]:
ts_corr = correlation.process(ts)
[20]:
ts_corr[0:100].plot(height=50, width=600)
[20]:

Compute the Density of a TimeSeries#

Create a BinaryTimeSeries with anomalies and detect them

[21]:
from darts.datasets import EnergyDataset
ts = EnergyDataset().load()
ts = ts['generation biomass']
[22]:
def add_point_anomalies(ts, n, value):
    df = ts.pd_dataframe()
    random_indices = np.random.choice(df.index, size=n, replace=False)
    df.loc[random_indices] = value
    return on.TimeSeries.from_dataframe(df)
[23]:
ts_w_ano =  add_point_anomalies(ts, 100, 10000)
[24]:
td_point = on.detectors.threshold(high_threshold=800)
ts_ano_point = td_point.detect(ts_w_ano)

Compute the density of anomalies

[25]:
ts_ano_density = on.processors.density(
    window_length=round(len(ts_ano_point)/16),
    mode='absolute'
).process(ts_ano_point)
[26]:
(
    on.Plot(ts_ano_density)
        .add(area)
        .properties(height=40, width=600)
        .show()
)
[26]:
[ ]: